Skip to content

Commit 902a0eb

Browse files
bors[bot]kvark
andauthored
Merge gfx-rs#546
546: NonZero internal Id representation r=grovesNL a=kvark Fixes gfx-rs#544 Based on gfx-rs#545 Blocked by mozilla/cbindgen#500 Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
2 parents 09beda1 + f07943f commit 902a0eb

11 files changed

Lines changed: 149 additions & 102 deletions

File tree

‎ffi/wgpu.h‎

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
/* Generated with cbindgen:0.13.1 */
5+
/* Generated with cbindgen:0.13.2 */
66

77
/* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen.
88
* To generate this file:
@@ -11,14 +11,20 @@
1111
* 2. Run `rustup run nightly cbindgen toolkit/library/rust/ --lockfile Cargo.lock --crate wgpu-remote -o dom/webgpu/ffi/wgpu_ffi_generated.h`
1212
*/
1313

14-
#define WGPU_LOCAL
14+
typedef unsigned long long WGPUNonZeroU64;
15+
typedef unsigned long long WGPUOption_AdapterId;
16+
typedef unsigned long long WGPUOption_SurfaceId;
17+
typedef unsigned long long WGPUOption_TextureViewId;
1518

1619

1720
#include <stdarg.h>
1821
#include <stdbool.h>
1922
#include <stdint.h>
2023
#include <stdlib.h>
2124

25+
/**
26+
* Bound uniform/storage buffer offsets must be aligned to this number.
27+
*/
2228
#define WGPUBIND_BUFFER_ALIGNMENT 256
2329

2430
#define WGPUDEFAULT_BIND_GROUPS 4
@@ -278,11 +284,11 @@ typedef enum {
278284
WGPUVertexFormat_Int4 = 48,
279285
} WGPUVertexFormat;
280286

281-
typedef uint64_t WGPUId_Adapter_Dummy;
287+
typedef WGPUNonZeroU64 WGPUId_Adapter_Dummy;
282288

283289
typedef WGPUId_Adapter_Dummy WGPUAdapterId;
284290

285-
typedef uint64_t WGPUId_Device_Dummy;
291+
typedef WGPUNonZeroU64 WGPUId_Device_Dummy;
286292

287293
typedef WGPUId_Device_Dummy WGPUDeviceId;
288294

@@ -299,11 +305,11 @@ typedef struct {
299305
WGPULimits limits;
300306
} WGPUDeviceDescriptor;
301307

302-
typedef uint64_t WGPUId_BindGroup_Dummy;
308+
typedef WGPUNonZeroU64 WGPUId_BindGroup_Dummy;
303309

304310
typedef WGPUId_BindGroup_Dummy WGPUBindGroupId;
305311

306-
typedef uint64_t WGPUId_Buffer_Dummy;
312+
typedef WGPUNonZeroU64 WGPUId_Buffer_Dummy;
307313

308314
typedef WGPUId_Buffer_Dummy WGPUBufferId;
309315

@@ -313,7 +319,7 @@ typedef void (*WGPUBufferMapReadCallback)(WGPUBufferMapAsyncStatus status, const
313319

314320
typedef void (*WGPUBufferMapWriteCallback)(WGPUBufferMapAsyncStatus status, uint8_t *data, uint8_t *userdata);
315321

316-
typedef uint64_t WGPUId_CommandBuffer_Dummy;
322+
typedef WGPUNonZeroU64 WGPUId_CommandBuffer_Dummy;
317323

318324
typedef WGPUId_CommandBuffer_Dummy WGPUCommandBufferId;
319325

@@ -330,12 +336,10 @@ typedef struct {
330336
uint32_t todo;
331337
} WGPUComputePassDescriptor;
332338

333-
typedef uint64_t WGPUId_TextureView_Dummy;
339+
typedef WGPUNonZeroU64 WGPUId_TextureView_Dummy;
334340

335341
typedef WGPUId_TextureView_Dummy WGPUTextureViewId;
336342

337-
typedef const WGPUTextureViewId *WGPUOptionRef_TextureViewId;
338-
339343
typedef struct {
340344
double r;
341345
double g;
@@ -351,13 +355,13 @@ typedef struct {
351355

352356
typedef struct {
353357
WGPUTextureViewId attachment;
354-
WGPUOptionRef_TextureViewId resolve_target;
358+
WGPUOption_TextureViewId resolve_target;
355359
WGPULoadOp load_op;
356360
WGPUStoreOp store_op;
357361
WGPUColor clear_color;
358-
} WGPURenderPassColorAttachmentDescriptorBase_TextureViewId__OptionRef_TextureViewId;
362+
} WGPURenderPassColorAttachmentDescriptorBase_TextureViewId;
359363

360-
typedef WGPURenderPassColorAttachmentDescriptorBase_TextureViewId__OptionRef_TextureViewId WGPURenderPassColorAttachmentDescriptor;
364+
typedef WGPURenderPassColorAttachmentDescriptorBase_TextureViewId WGPURenderPassColorAttachmentDescriptor;
361365

362366
typedef struct {
363367
WGPUTextureViewId attachment;
@@ -384,7 +388,7 @@ typedef struct {
384388
uint32_t rows_per_image;
385389
} WGPUBufferCopyView;
386390

387-
typedef uint64_t WGPUId_Texture_Dummy;
391+
typedef WGPUNonZeroU64 WGPUId_Texture_Dummy;
388392

389393
typedef WGPUId_Texture_Dummy WGPUTextureId;
390394

@@ -418,15 +422,15 @@ typedef const char *WGPURawString;
418422

419423
typedef uint32_t WGPUDynamicOffset;
420424

421-
typedef uint64_t WGPUId_ComputePipeline_Dummy;
425+
typedef WGPUNonZeroU64 WGPUId_ComputePipeline_Dummy;
422426

423427
typedef WGPUId_ComputePipeline_Dummy WGPUComputePipelineId;
424428

425-
typedef uint64_t WGPUId_Surface;
429+
typedef WGPUNonZeroU64 WGPUId_Surface;
426430

427431
typedef WGPUId_Surface WGPUSurfaceId;
428432

429-
typedef uint64_t WGPUId_BindGroupLayout_Dummy;
433+
typedef WGPUNonZeroU64 WGPUId_BindGroupLayout_Dummy;
430434

431435
typedef WGPUId_BindGroupLayout_Dummy WGPUBindGroupLayoutId;
432436

@@ -436,7 +440,7 @@ typedef struct {
436440
WGPUBufferAddress size;
437441
} WGPUBufferBinding;
438442

439-
typedef uint64_t WGPUId_Sampler_Dummy;
443+
typedef WGPUNonZeroU64 WGPUId_Sampler_Dummy;
440444

441445
typedef WGPUId_Sampler_Dummy WGPUSamplerId;
442446

@@ -525,11 +529,11 @@ typedef struct {
525529
const char *label;
526530
} WGPUCommandEncoderDescriptor;
527531

528-
typedef uint64_t WGPUId_PipelineLayout_Dummy;
532+
typedef WGPUNonZeroU64 WGPUId_PipelineLayout_Dummy;
529533

530534
typedef WGPUId_PipelineLayout_Dummy WGPUPipelineLayoutId;
531535

532-
typedef uint64_t WGPUId_ShaderModule_Dummy;
536+
typedef WGPUNonZeroU64 WGPUId_ShaderModule_Dummy;
533537

534538
typedef WGPUId_ShaderModule_Dummy WGPUShaderModuleId;
535539

@@ -548,7 +552,7 @@ typedef struct {
548552
uintptr_t bind_group_layouts_length;
549553
} WGPUPipelineLayoutDescriptor;
550554

551-
typedef uint64_t WGPUId_RenderPipeline_Dummy;
555+
typedef WGPUNonZeroU64 WGPUId_RenderPipeline_Dummy;
552556

553557
typedef WGPUId_RenderPipeline_Dummy WGPURenderPipelineId;
554558

@@ -655,7 +659,7 @@ typedef struct {
655659
WGPUU32Array code;
656660
} WGPUShaderModuleDescriptor;
657661

658-
typedef uint64_t WGPUId_SwapChain_Dummy;
662+
typedef WGPUNonZeroU64 WGPUId_SwapChain_Dummy;
659663

660664
typedef WGPUId_SwapChain_Dummy WGPUSwapChainId;
661665

@@ -691,21 +695,21 @@ typedef WGPUDeviceId WGPUQueueId;
691695

692696
typedef WGPURawPass *WGPURenderPassId;
693697

694-
typedef uint64_t WGPUId_RenderBundle_Dummy;
698+
typedef WGPUNonZeroU64 WGPUId_RenderBundle_Dummy;
695699

696700
typedef WGPUId_RenderBundle_Dummy WGPURenderBundleId;
697701

698702
typedef struct {
699703
WGPUPowerPreference power_preference;
700-
WGPUSurfaceId compatible_surface;
704+
WGPUOption_SurfaceId compatible_surface;
701705
} WGPURequestAdapterOptions;
702706

703707
typedef uint32_t WGPUBackendBit;
704708

705-
typedef void (*WGPURequestAdapterCallback)(WGPUAdapterId id, void *userdata);
709+
typedef void (*WGPURequestAdapterCallback)(WGPUOption_AdapterId id, void *userdata);
706710

707711
typedef struct {
708-
WGPUTextureViewId view_id;
712+
WGPUOption_TextureViewId view_id;
709713
} WGPUSwapChainOutput;
710714

711715
typedef struct {

‎wgpu-core/src/command/mod.rs‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use std::{
3535
slice,
3636
thread::ThreadId,
3737
};
38-
use wgt::RenderPassColorAttachmentDescriptorBase;
3938

4039

4140
#[derive(Clone, Copy, Debug, peek_poke::PeekCopy, peek_poke::Poke)]
@@ -197,14 +196,35 @@ impl<B: GfxBackend> CommandBuffer<B> {
197196
}
198197
}
199198

200-
pub type RawRenderPassColorAttachmentDescriptor =
201-
RenderPassColorAttachmentDescriptorBase<id::TextureViewId, id::TextureViewId>;
199+
#[repr(C)]
200+
#[derive(peek_poke::PeekCopy, peek_poke::Poke)]
201+
struct PassComponent<T> {
202+
load_op: wgt::LoadOp,
203+
store_op: wgt::StoreOp,
204+
clear_value: T,
205+
}
206+
207+
#[repr(C)]
208+
#[derive(peek_poke::PeekCopy, peek_poke::Poke)]
209+
struct RawRenderPassColorAttachmentDescriptor {
210+
attachment: u64,
211+
resolve_target: u64,
212+
component: PassComponent<wgt::Color>,
213+
}
214+
215+
#[repr(C)]
216+
#[derive(peek_poke::PeekCopy, peek_poke::Poke)]
217+
struct RawRenderPassDepthStencilAttachmentDescriptor {
218+
attachment: u64,
219+
depth: PassComponent<f32>,
220+
stencil: PassComponent<u32>,
221+
}
202222

203223
#[repr(C)]
204224
#[derive(peek_poke::PeekCopy, peek_poke::Poke)]
205-
pub struct RawRenderTargets {
206-
pub colors: [RawRenderPassColorAttachmentDescriptor; MAX_COLOR_TARGETS],
207-
pub depth_stencil: RenderPassDepthStencilAttachmentDescriptor,
225+
struct RawRenderTargets {
226+
colors: [RawRenderPassColorAttachmentDescriptor; MAX_COLOR_TARGETS],
227+
depth_stencil: RawRenderPassDepthStencilAttachmentDescriptor,
208228
}
209229

210230
impl<G: GlobalIdentityHandlerFactory> Global<G> {

‎wgpu-core/src/command/render.rs‎

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use crate::{
66
command::{
77
bind::{Binder, LayoutChange},
8+
PassComponent,
89
PhantomSlice,
910
RawRenderPassColorAttachmentDescriptor,
11+
RawRenderPassDepthStencilAttachmentDescriptor,
1012
RawRenderTargets,
1113
},
1214
conv,
@@ -52,18 +54,15 @@ use std::{
5254
slice,
5355
};
5456

55-
//Note: this could look better if `cbindgen` wasn't confused by &T used in place of
56-
// a generic parameter, it's not able to manage
57-
pub type OptionRef<'a, T> = Option<&'a T>;
58-
pub type RenderPassColorAttachmentDescriptor<'a> =
59-
RenderPassColorAttachmentDescriptorBase<id::TextureViewId, OptionRef<'a, id::TextureViewId>>;
57+
pub type RenderPassColorAttachmentDescriptor =
58+
RenderPassColorAttachmentDescriptorBase<id::TextureViewId>;
6059
pub type RenderPassDepthStencilAttachmentDescriptor =
6160
RenderPassDepthStencilAttachmentDescriptorBase<id::TextureViewId>;
6261

6362
#[repr(C)]
6463
#[derive(Debug)]
6564
pub struct RenderPassDescriptor<'a> {
66-
pub color_attachments: *const RenderPassColorAttachmentDescriptor<'a>,
65+
pub color_attachments: *const RenderPassColorAttachmentDescriptor,
6766
pub color_attachments_length: usize,
6867
pub depth_stencil_attachment: Option<&'a RenderPassDepthStencilAttachmentDescriptor>,
6968
}
@@ -133,22 +132,35 @@ impl super::RawPass {
133132
pub unsafe fn new_render(parent_id: id::CommandEncoderId, desc: &RenderPassDescriptor) -> Self {
134133
let mut pass = Self::from_vec(Vec::<RenderCommand>::with_capacity(1), parent_id);
135134

136-
let mut targets = RawRenderTargets {
137-
depth_stencil: desc.depth_stencil_attachment
138-
.cloned()
139-
.unwrap_or_else(|| mem::zeroed()),
140-
colors: mem::zeroed(),
141-
};
135+
let mut targets: RawRenderTargets = mem::zeroed();
136+
if let Some(ds) = desc.depth_stencil_attachment {
137+
targets.depth_stencil = RawRenderPassDepthStencilAttachmentDescriptor {
138+
attachment: ds.attachment.into_raw(),
139+
depth: PassComponent {
140+
load_op: ds.depth_load_op,
141+
store_op: ds.depth_store_op,
142+
clear_value: ds.clear_depth,
143+
},
144+
stencil: PassComponent {
145+
load_op: ds.stencil_load_op,
146+
store_op: ds.stencil_store_op,
147+
clear_value: ds.clear_stencil,
148+
},
149+
};
150+
}
151+
142152
for (color, at) in targets.colors
143153
.iter_mut()
144154
.zip(slice::from_raw_parts(desc.color_attachments, desc.color_attachments_length))
145155
{
146156
*color = RawRenderPassColorAttachmentDescriptor {
147-
attachment: at.attachment,
148-
resolve_target: at.resolve_target.map_or(id::TextureViewId::ERROR, |rt| *rt),
149-
load_op: at.load_op,
150-
store_op: at.store_op,
151-
clear_color: at.clear_color,
157+
attachment: at.attachment.into_raw(),
158+
resolve_target: at.resolve_target.map_or(0, |id| id.into_raw()),
159+
component: PassComponent {
160+
load_op: at.load_op,
161+
store_op: at.store_op,
162+
clear_value: at.clear_color,
163+
},
152164
};
153165
}
154166

@@ -321,25 +333,32 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
321333

322334
let color_attachments = targets.colors
323335
.iter()
324-
.take_while(|at| at.attachment != id::TextureViewId::ERROR)
336+
.take_while(|at| at.attachment != 0)
325337
.map(|at| {
326338
RenderPassColorAttachmentDescriptor {
327-
attachment: at.attachment,
328-
resolve_target: if at.resolve_target == id::TextureViewId::ERROR {
329-
None
330-
} else {
331-
Some(&at.resolve_target)
332-
},
333-
load_op: at.load_op,
334-
store_op: at.store_op,
335-
clear_color: at.clear_color,
339+
attachment: id::TextureViewId::from_raw(at.attachment).unwrap(),
340+
resolve_target: id::TextureViewId::from_raw(at.resolve_target),
341+
load_op: at.component.load_op,
342+
store_op: at.component.store_op,
343+
clear_color: at.component.clear_value,
336344
}
337345
})
338346
.collect::<ArrayVec<[_; MAX_COLOR_TARGETS]>>();
339-
let depth_stencil_attachment = if targets.depth_stencil.attachment == id::TextureViewId::ERROR {
347+
let depth_stencil_attachment_body;
348+
let depth_stencil_attachment = if targets.depth_stencil.attachment == 0 {
340349
None
341350
} else {
342-
Some(&targets.depth_stencil)
351+
let at = &targets.depth_stencil;
352+
depth_stencil_attachment_body = RenderPassDepthStencilAttachmentDescriptor {
353+
attachment: id::TextureViewId::from_raw(at.attachment).unwrap(),
354+
depth_load_op: at.depth.load_op,
355+
depth_store_op: at.depth.store_op,
356+
clear_depth: at.depth.clear_value,
357+
stencil_load_op: at.stencil.load_op,
358+
stencil_store_op: at.stencil.store_op,
359+
clear_stencil: at.stencil.clear_value,
360+
};
361+
Some(&depth_stencil_attachment_body)
343362
};
344363

345364
let (context, sample_count) = {
@@ -485,7 +504,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
485504
});
486505
}
487506

488-
for &resolve_target in color_attachments
507+
for resolve_target in color_attachments
489508
.iter()
490509
.flat_map(|at| at.resolve_target)
491510
{
@@ -642,7 +661,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
642661
resolves: color_attachments
643662
.iter()
644663
.filter_map(|at| at.resolve_target)
645-
.cloned()
646664
.collect(),
647665
depth_stencil: depth_stencil_attachment.map(|at| at.attachment),
648666
};
@@ -774,7 +792,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
774792
resolves: color_attachments
775793
.iter()
776794
.filter_map(|at| at.resolve_target)
777-
.map(|resolve| view_guard[*resolve].format)
795+
.map(|resolve| view_guard[resolve].format)
778796
.collect(),
779797
depth_stencil: depth_stencil_attachment.map(|at| view_guard[at.attachment].format),
780798
};

0 commit comments

Comments
 (0)