Skip to content

Commit 26bc695

Browse files
bors[bot]kvark
andcommitted
Merge #77
77: Storage indexing, framebuffer tracking, and a bunch of validation fixes r=grovesNL a=kvark Closes #73 Closes #75 Closes #79 Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com> Co-authored-by: Dzmitry Malyshau <dmalyshau@mozilla.com>
2 parents dd73675 + 8ad1476 commit 26bc695

File tree

18 files changed

+425
-394
lines changed

18 files changed

+425
-394
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ clear:
4848
rm wgpu-bindings/wgpu.h
4949

5050
lib-native: Cargo.lock wgpu-native/Cargo.toml $(wildcard wgpu-native/**/*.rs)
51-
cargo build --manifest-path wgpu-native/Cargo.toml --features $(FEATURE_NATIVE)
51+
cargo build --manifest-path wgpu-native/Cargo.toml --features "local,$(FEATURE_NATIVE)"
5252

5353
lib-rust: Cargo.lock wgpu-rs/Cargo.toml $(wildcard wgpu-rs/**/*.rs)
5454
cargo build --manifest-path wgpu-rs/Cargo.toml --features $(FEATURE_RUST)

examples/hello_triangle_rust/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040
cull_mode: wgpu::CullMode::None,
4141
depth_bias: 0,
4242
depth_bias_slope_scale: 0.0,
43-
depth_bias_clamp: wgpu::MAX_DEPTH_BIAS_CLAMP,
43+
depth_bias_clamp: 0.0,
4444
},
4545
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
4646
color_states: &[

gfx-examples/src/cube.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl framework::Example for Example {
280280
cull_mode: wgpu::CullMode::Back,
281281
depth_bias: 0,
282282
depth_bias_slope_scale: 0.0,
283-
depth_bias_clamp: wgpu::MAX_DEPTH_BIAS_CLAMP,
283+
depth_bias_clamp: 0.0,
284284
},
285285
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
286286
color_states: &[

gfx-examples/src/shadow.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl framework::Example for Example {
219219
});
220220

221221
let local_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
222-
bindings: &[
222+
bindings: &[
223223
wgpu::BindGroupLayoutBinding {
224224
binding: 0,
225225
visibility: wgpu::ShaderStageFlags::VERTEX | wgpu::ShaderStageFlags::FRAGMENT,
@@ -380,7 +380,7 @@ impl framework::Example for Example {
380380
let light_uniform_size = (Self::MAX_LIGHTS * mem::size_of::<LightRaw>()) as u32;
381381
let light_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
382382
size: light_uniform_size,
383-
usage: wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_DST,
383+
usage: wgpu::BufferUsageFlags::UNIFORM | wgpu::BufferUsageFlags::TRANSFER_SRC | wgpu::BufferUsageFlags::TRANSFER_DST,
384384
});
385385

386386
let vb_desc = wgpu::VertexBufferDescriptor {
@@ -459,7 +459,7 @@ impl framework::Example for Example {
459459
cull_mode: wgpu::CullMode::Back,
460460
depth_bias: 2, // corresponds to bilinear filtering
461461
depth_bias_slope_scale: 2.0,
462-
depth_bias_clamp: wgpu::MAX_DEPTH_BIAS_CLAMP,
462+
depth_bias_clamp: 0.0,
463463
},
464464
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
465465
color_states: &[],
@@ -579,7 +579,7 @@ impl framework::Example for Example {
579579
cull_mode: wgpu::CullMode::Back,
580580
depth_bias: 0,
581581
depth_bias_slope_scale: 0.0,
582-
depth_bias_clamp: wgpu::MAX_DEPTH_BIAS_CLAMP,
582+
depth_bias_clamp: 0.0,
583583
},
584584
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
585585
color_states: &[

wgpu-bindings/wgpu.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#define WGPUBITS_PER_BYTE 8
77

8-
#define WGPUMAX_DEPTH_BIAS_CLAMP 16
9-
108
typedef enum {
119
WGPUAddressMode_ClampToEdge = 0,
1210
WGPUAddressMode_Repeat = 1,
@@ -169,6 +167,8 @@ typedef struct {
169167
WGPUExtensions extensions;
170168
} WGPUDeviceDescriptor;
171169

170+
typedef WGPUId WGPUBindGroupId;
171+
172172
typedef WGPUId WGPUBufferId;
173173

174174
typedef void (*WGPUBufferMapReadCallback)(WGPUBufferMapAsyncStatus status, const uint8_t *data, uint8_t *userdata);
@@ -243,8 +243,6 @@ typedef struct {
243243
const WGPURenderPassDepthStencilAttachmentDescriptor_TextureViewId *depth_stencil_attachment;
244244
} WGPURenderPassDescriptor;
245245

246-
typedef WGPUId WGPUBindGroupId;
247-
248246
typedef WGPUId WGPUComputePipelineId;
249247

250248
typedef WGPUId WGPUInstanceId;
@@ -557,6 +555,8 @@ typedef struct {
557555

558556
WGPUDeviceId wgpu_adapter_create_device(WGPUAdapterId adapter_id, const WGPUDeviceDescriptor *desc);
559557

558+
void wgpu_bind_group_destroy(WGPUBindGroupId bind_group_id);
559+
560560
void wgpu_buffer_destroy(WGPUBufferId buffer_id);
561561

562562
void wgpu_buffer_map_read_async(WGPUBufferId buffer_id,

wgpu-native/src/command/compute.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::command::bind::{Binder};
22
use crate::hub::HUB;
3-
use crate::track::TrackerSet;
3+
use crate::track::{Stitch, TrackerSet};
44
use crate::{
55
Stored, CommandBuffer,
66
BindGroupId, CommandBufferId, ComputePassId, ComputePipelineId,
@@ -38,8 +38,7 @@ pub extern "C" fn wgpu_compute_pass_end_pass(pass_id: ComputePassId) -> CommandB
3838

3939
HUB.command_buffers
4040
.write()
41-
.get_mut(pass.cmb_id.value)
42-
.raw
41+
[pass.cmb_id.value].raw
4342
.push(pass.raw);
4443
pass.cmb_id.value
4544
}
@@ -49,8 +48,7 @@ pub extern "C" fn wgpu_compute_pass_dispatch(pass_id: ComputePassId, x: u32, y:
4948
unsafe {
5049
HUB.compute_passes
5150
.write()
52-
.get_mut(pass_id)
53-
.raw
51+
[pass_id].raw
5452
.dispatch([x, y, z]);
5553
}
5654
}
@@ -62,29 +60,27 @@ pub extern "C" fn wgpu_compute_pass_set_bind_group(
6260
bind_group_id: BindGroupId,
6361
) {
6462
let mut pass_guard = HUB.compute_passes.write();
65-
let pass = pass_guard.get_mut(pass_id);
63+
let pass = &mut pass_guard[pass_id];
6664
let bind_group_guard = HUB.bind_groups.read();
67-
let bind_group = bind_group_guard.get(bind_group_id);
65+
let bind_group = &bind_group_guard[bind_group_id];
6866

6967
//Note: currently, WebGPU compute passes have synchronization defined
7068
// at a dispatch granularity, so we insert the necessary barriers here.
7169

72-
//TODO: have `TrackerSet::consume()` ?
7370
CommandBuffer::insert_barriers(
7471
&mut pass.raw,
75-
pass.trackers.buffers.consume_by_replace(&bind_group.used.buffers),
76-
pass.trackers.textures.consume_by_replace(&bind_group.used.textures),
72+
&mut pass.trackers,
73+
&bind_group.used,
74+
Stitch::Last,
7775
&*HUB.buffers.read(),
7876
&*HUB.textures.read(),
7977
);
80-
pass.trackers.views.consume(&bind_group.used.views);
8178

8279
if let Some(pipeline_layout_id) = pass.binder.provide_entry(index as usize, bind_group_id, bind_group) {
8380
let pipeline_layout_guard = HUB.pipeline_layouts.read();
84-
let pipeline_layout = pipeline_layout_guard.get(pipeline_layout_id);
8581
unsafe {
8682
pass.raw.bind_compute_descriptor_sets(
87-
&pipeline_layout.raw,
83+
&pipeline_layout_guard[pipeline_layout_id].raw,
8884
index as usize,
8985
iter::once(&bind_group.raw),
9086
&[],
@@ -99,9 +95,9 @@ pub extern "C" fn wgpu_compute_pass_set_pipeline(
9995
pipeline_id: ComputePipelineId,
10096
) {
10197
let mut pass_guard = HUB.compute_passes.write();
102-
let pass = pass_guard.get_mut(pass_id);
98+
let pass = &mut pass_guard[pass_id];
10399
let pipeline_guard = HUB.compute_pipelines.read();
104-
let pipeline = pipeline_guard.get(pipeline_id);
100+
let pipeline = &pipeline_guard[pipeline_id];
105101

106102
unsafe {
107103
pass.raw.bind_compute_pipeline(&pipeline.raw);
@@ -112,7 +108,7 @@ pub extern "C" fn wgpu_compute_pass_set_pipeline(
112108
}
113109

114110
let pipeline_layout_guard = HUB.pipeline_layouts.read();
115-
let pipeline_layout = pipeline_layout_guard.get(pipeline.layout_id);
111+
let pipeline_layout = &pipeline_layout_guard[pipeline.layout_id];
116112
let bing_group_guard = HUB.bind_groups.read();
117113

118114
pass.binder.pipeline_layout_id = Some(pipeline.layout_id.clone());
@@ -124,12 +120,12 @@ pub extern "C" fn wgpu_compute_pass_set_pipeline(
124120
.enumerate()
125121
{
126122
if let Some(bg_id) = entry.expect_layout(bgl_id) {
127-
let bind_group = bing_group_guard.get(bg_id);
123+
let desc_set = &bing_group_guard[bg_id].raw;
128124
unsafe {
129125
pass.raw.bind_compute_descriptor_sets(
130126
&pipeline_layout.raw,
131127
index,
132-
iter::once(&bind_group.raw),
128+
iter::once(desc_set),
133129
&[]
134130
);
135131
}

wgpu-native/src/command/mod.rs

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ use crate::device::{
1414
all_buffer_stages, all_image_stages,
1515
};
1616
use crate::hub::{HUB, Storage};
17+
use crate::resource::TexturePlacement;
1718
use crate::swap_chain::{SwapChainLink, SwapImageEpoch};
18-
use crate::track::TrackerSet;
19+
use crate::track::{DummyUsage, Stitch, TrackerSet};
1920
use crate::conv;
2021
use crate::{
2122
BufferHandle, TextureHandle,
22-
BufferId, CommandBufferId, CommandEncoderId, DeviceId,
23-
TextureId, TextureViewId,
24-
BufferUsageFlags, TextureUsageFlags, Color,
23+
CommandBufferId, CommandEncoderId, DeviceId,
24+
TextureViewId,
25+
TextureUsageFlags, Color,
2526
LifeGuard, Stored,
2627
CommandBufferHandle,
2728
};
@@ -34,7 +35,6 @@ use hal::{Device as _Device};
3435
use log::trace;
3536

3637
use std::collections::hash_map::Entry;
37-
use std::ops::Range;
3838
use std::{iter, slice};
3939
use std::thread::ThreadId;
4040

@@ -90,39 +90,43 @@ pub struct CommandBuffer<B: hal::Backend> {
9090
}
9191

9292
impl CommandBufferHandle {
93-
pub(crate) fn insert_barriers<I, J>(
93+
pub(crate) fn insert_barriers(
9494
raw: &mut <Backend as hal::Backend>::CommandBuffer,
95-
buffer_iter: I,
96-
texture_iter: J,
95+
base: &mut TrackerSet,
96+
head: &TrackerSet,
97+
stitch: Stitch,
9798
buffer_guard: &Storage<BufferHandle>,
9899
texture_guard: &Storage<TextureHandle>,
99-
) where
100-
I: Iterator<Item = (BufferId, Range<BufferUsageFlags>)>,
101-
J: Iterator<Item = (TextureId, Range<TextureUsageFlags>)>,
102-
{
103-
104-
let buffer_barriers = buffer_iter.map(|(id, transit)| {
105-
let b = buffer_guard.get(id);
106-
trace!("transit {:?} {:?}", id, transit);
107-
hal::memory::Barrier::Buffer {
108-
states: conv::map_buffer_state(transit.start) .. conv::map_buffer_state(transit.end),
109-
target: &b.raw,
110-
range: None .. None,
111-
families: None,
112-
}
113-
});
114-
let texture_barriers = texture_iter.map(|(id, transit)| {
115-
let t = texture_guard.get(id);
116-
trace!("transit {:?} {:?}", id, transit);
117-
let aspects = t.full_range.aspects;
118-
hal::memory::Barrier::Image {
119-
states: conv::map_texture_state(transit.start, aspects)
120-
..conv::map_texture_state(transit.end, aspects),
121-
target: &t.raw,
122-
range: t.full_range.clone(), //TODO?
123-
families: None,
124-
}
125-
});
100+
) {
101+
let buffer_barriers = base.buffers
102+
.consume_by_replace(&head.buffers, stitch)
103+
.map(|(id, transit)| {
104+
let b = &buffer_guard[id];
105+
trace!("transit buffer {:?} {:?}", id, transit);
106+
hal::memory::Barrier::Buffer {
107+
states: conv::map_buffer_state(transit.start) .. conv::map_buffer_state(transit.end),
108+
target: &b.raw,
109+
range: None .. None,
110+
families: None,
111+
}
112+
});
113+
let texture_barriers = base.textures
114+
.consume_by_replace(&head.textures, stitch)
115+
.map(|(id, transit)| {
116+
let t = &texture_guard[id];
117+
trace!("transit texture {:?} {:?}", id, transit);
118+
let aspects = t.full_range.aspects;
119+
hal::memory::Barrier::Image {
120+
states: conv::map_texture_state(transit.start, aspects)
121+
..conv::map_texture_state(transit.end, aspects),
122+
target: &t.raw,
123+
range: t.full_range.clone(), //TODO?
124+
families: None,
125+
}
126+
});
127+
base.views
128+
.consume_by_extend(&head.views)
129+
.unwrap();
126130

127131
let stages = all_buffer_stages() | all_image_stages();
128132
unsafe {
@@ -148,7 +152,7 @@ pub extern "C" fn wgpu_command_encoder_finish(
148152
) -> CommandBufferId {
149153
HUB.command_buffers
150154
.write()
151-
.get_mut(command_encoder_id)
155+
[command_encoder_id]
152156
.is_recording = false; //TODO: check for the old value
153157
command_encoder_id
154158
}
@@ -158,9 +162,9 @@ pub fn command_encoder_begin_render_pass(
158162
desc: RenderPassDescriptor,
159163
) -> RenderPass<Backend> {
160164
let mut cmb_guard = HUB.command_buffers.write();
161-
let cmb = cmb_guard.get_mut(command_encoder_id);
165+
let cmb = &mut cmb_guard[command_encoder_id];
162166
let device_guard = HUB.devices.read();
163-
let device = device_guard.get(cmb.device_id.value);
167+
let device = &device_guard[cmb.device_id.value];
164168
let view_guard = HUB.texture_views.read();
165169

166170
let mut current_comb = device.com_allocator.extend(cmb);
@@ -187,13 +191,13 @@ pub fn command_encoder_begin_render_pass(
187191
let swap_chain_links = &mut cmb.swap_chain_links;
188192

189193
let depth_stencil_key = depth_stencil_attachment.map(|at| {
190-
let view = view_guard.get(at.attachment);
194+
let view = &view_guard[at.attachment];
191195
if let Some(ex) = extent {
192196
assert_eq!(ex, view.extent);
193197
} else {
194198
extent = Some(view.extent);
195199
}
196-
trackers.views.query(at.attachment, &view.life_guard.ref_count);
200+
trackers.views.query(at.attachment, &view.life_guard.ref_count, DummyUsage);
197201
let query = trackers.textures.query(
198202
view.texture_id.value,
199203
&view.texture_id.ref_count,
@@ -213,20 +217,20 @@ pub fn command_encoder_begin_render_pass(
213217
});
214218

215219
let color_keys = color_attachments.iter().map(|at| {
216-
let view = view_guard.get(at.attachment);
220+
let view = &view_guard[at.attachment];
217221

218222
if view.is_owned_by_swap_chain {
219223
let link = match HUB.textures
220224
.read()
221-
.get(view.texture_id.value)
222-
.swap_chain_link
225+
[view.texture_id.value].placement
223226
{
224-
Some(ref link) => SwapChainLink {
227+
TexturePlacement::SwapChain(ref link) => SwapChainLink {
225228
swap_chain_id: link.swap_chain_id.clone(),
226229
epoch: *link.epoch.lock(),
227230
image_index: link.image_index,
228231
},
229-
None => unreachable!()
232+
TexturePlacement::Memory(_) |
233+
TexturePlacement::Void => unreachable!()
230234
};
231235
swap_chain_links.push(link);
232236
}
@@ -236,7 +240,7 @@ pub fn command_encoder_begin_render_pass(
236240
} else {
237241
extent = Some(view.extent);
238242
}
239-
trackers.views.query(at.attachment, &view.life_guard.ref_count);
243+
trackers.views.query(at.attachment, &view.life_guard.ref_count, DummyUsage);
240244
let query = trackers.textures.query(
241245
view.texture_id.value,
242246
&view.texture_id.ref_count,
@@ -308,7 +312,7 @@ pub fn command_encoder_begin_render_pass(
308312
.key()
309313
.attachments
310314
.iter()
311-
.map(|&id| &view_guard.get(id).raw);
315+
.map(|&id| &view_guard[id].raw);
312316

313317
unsafe {
314318
device
@@ -381,7 +385,7 @@ pub fn command_encoder_begin_compute_pass(
381385
command_encoder_id: CommandEncoderId,
382386
) -> ComputePass<Backend> {
383387
let mut cmb_guard = HUB.command_buffers.write();
384-
let cmb = cmb_guard.get_mut(command_encoder_id);
388+
let cmb = &mut cmb_guard[command_encoder_id];
385389

386390
let raw = cmb.raw.pop().unwrap();
387391
let stored = Stored {

0 commit comments

Comments
 (0)