Skip to content

Commit a4e2a48

Browse files
chore: naively set warn(unsafe_op_in_unsafe_fn) in wgpu-core
Do the simplest mechanical work necessary to enable and satisfy this lint; only put down `unsafe` blocks, don't try to inspect for correctness or anything else. N.B.: that there _are_ some adjustments identified that could be made here, like breaking multiple individual `unsafe` operations into their `unsafe` spans. This is left for a follow-up commit.
1 parent 4363ebc commit a4e2a48

File tree

12 files changed

+399
-313
lines changed

12 files changed

+399
-313
lines changed

wgpu-core/src/command/bundle.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ impl<A: HalApi> RenderBundle<A> {
763763
let mut offsets = self.base.dynamic_offsets.as_slice();
764764
let mut pipeline_layout_id = None::<id::Valid<id::PipelineLayoutId>>;
765765
if let Some(ref label) = self.base.label {
766-
raw.begin_debug_marker(label);
766+
unsafe { raw.begin_debug_marker(label) };
767767
}
768768

769769
for command in self.base.commands.iter() {
@@ -774,17 +774,19 @@ impl<A: HalApi> RenderBundle<A> {
774774
bind_group_id,
775775
} => {
776776
let bind_group = bind_group_guard.get(bind_group_id).unwrap();
777-
raw.set_bind_group(
778-
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
779-
index as u32,
780-
&bind_group.raw,
781-
&offsets[..num_dynamic_offsets as usize],
782-
);
777+
unsafe {
778+
raw.set_bind_group(
779+
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
780+
index as u32,
781+
&bind_group.raw,
782+
&offsets[..num_dynamic_offsets as usize],
783+
)
784+
};
783785
offsets = &offsets[num_dynamic_offsets as usize..];
784786
}
785787
RenderCommand::SetPipeline(pipeline_id) => {
786788
let pipeline = pipeline_guard.get(pipeline_id).unwrap();
787-
raw.set_render_pipeline(&pipeline.raw);
789+
unsafe { raw.set_render_pipeline(&pipeline.raw) };
788790

789791
pipeline_layout_id = Some(pipeline.layout_id.value);
790792
}
@@ -805,7 +807,7 @@ impl<A: HalApi> RenderBundle<A> {
805807
offset,
806808
size,
807809
};
808-
raw.set_index_buffer(bb, index_format);
810+
unsafe { raw.set_index_buffer(bb, index_format) };
809811
}
810812
RenderCommand::SetVertexBuffer {
811813
slot,
@@ -824,7 +826,7 @@ impl<A: HalApi> RenderBundle<A> {
824826
offset,
825827
size,
826828
};
827-
raw.set_vertex_buffer(slot, bb);
829+
unsafe { raw.set_vertex_buffer(slot, bb) };
828830
}
829831
RenderCommand::SetPushConstant {
830832
stages,
@@ -841,18 +843,22 @@ impl<A: HalApi> RenderBundle<A> {
841843
let data_slice = &self.base.push_constant_data
842844
[(values_offset as usize)..values_end_offset];
843845

844-
raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
846+
unsafe {
847+
raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
848+
}
845849
} else {
846850
super::push_constant_clear(
847851
offset,
848852
size_bytes,
849853
|clear_offset, clear_data| {
850-
raw.set_push_constants(
851-
&pipeline_layout.raw,
852-
stages,
853-
clear_offset,
854-
clear_data,
855-
);
854+
unsafe {
855+
raw.set_push_constants(
856+
&pipeline_layout.raw,
857+
stages,
858+
clear_offset,
859+
clear_data,
860+
)
861+
};
856862
},
857863
);
858864
}
@@ -863,7 +869,7 @@ impl<A: HalApi> RenderBundle<A> {
863869
first_vertex,
864870
first_instance,
865871
} => {
866-
raw.draw(first_vertex, vertex_count, first_instance, instance_count);
872+
unsafe { raw.draw(first_vertex, vertex_count, first_instance, instance_count) };
867873
}
868874
RenderCommand::DrawIndexed {
869875
index_count,
@@ -872,13 +878,15 @@ impl<A: HalApi> RenderBundle<A> {
872878
base_vertex,
873879
first_instance,
874880
} => {
875-
raw.draw_indexed(
876-
first_index,
877-
index_count,
878-
base_vertex,
879-
first_instance,
880-
instance_count,
881-
);
881+
unsafe {
882+
raw.draw_indexed(
883+
first_index,
884+
index_count,
885+
base_vertex,
886+
first_instance,
887+
instance_count,
888+
)
889+
};
882890
}
883891
RenderCommand::MultiDrawIndirect {
884892
buffer_id,
@@ -892,7 +900,7 @@ impl<A: HalApi> RenderBundle<A> {
892900
.raw
893901
.as_ref()
894902
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
895-
raw.draw_indirect(buffer, offset, 1);
903+
unsafe { raw.draw_indirect(buffer, offset, 1) };
896904
}
897905
RenderCommand::MultiDrawIndirect {
898906
buffer_id,
@@ -906,7 +914,7 @@ impl<A: HalApi> RenderBundle<A> {
906914
.raw
907915
.as_ref()
908916
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
909-
raw.draw_indexed_indirect(buffer, offset, 1);
917+
unsafe { raw.draw_indexed_indirect(buffer, offset, 1) };
910918
}
911919
RenderCommand::MultiDrawIndirect { .. }
912920
| RenderCommand::MultiDrawIndirectCount { .. } => {
@@ -931,7 +939,7 @@ impl<A: HalApi> RenderBundle<A> {
931939
}
932940

933941
if let Some(_) = self.base.label {
934-
raw.end_debug_marker();
942+
unsafe { raw.end_debug_marker() };
935943
}
936944

937945
Ok(())
@@ -1439,13 +1447,15 @@ pub mod bundle_ffi {
14391447
offsets: *const DynamicOffset,
14401448
offset_length: usize,
14411449
) {
1442-
let redundant = bundle.current_bind_groups.set_and_check_redundant(
1443-
bind_group_id,
1444-
index,
1445-
&mut bundle.base.dynamic_offsets,
1446-
offsets,
1447-
offset_length,
1448-
);
1450+
let redundant = unsafe {
1451+
bundle.current_bind_groups.set_and_check_redundant(
1452+
bind_group_id,
1453+
index,
1454+
&mut bundle.base.dynamic_offsets,
1455+
offsets,
1456+
offset_length,
1457+
)
1458+
};
14491459

14501460
if redundant {
14511461
return;
@@ -1522,7 +1532,7 @@ pub mod bundle_ffi {
15221532
0,
15231533
"Push constant size must be aligned to 4 bytes."
15241534
);
1525-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
1535+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
15261536
let value_offset = pass.base.push_constant_data.len().try_into().expect(
15271537
"Ran out of push constant space. Don't set 4gb of push constants per RenderBundle.",
15281538
);

wgpu-core/src/command/compute.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,15 @@ pub mod compute_ffi {
800800
offsets: *const DynamicOffset,
801801
offset_length: usize,
802802
) {
803-
let redundant = pass.current_bind_groups.set_and_check_redundant(
804-
bind_group_id,
805-
index,
806-
&mut pass.base.dynamic_offsets,
807-
offsets,
808-
offset_length,
809-
);
803+
let redundant = unsafe {
804+
pass.current_bind_groups.set_and_check_redundant(
805+
bind_group_id,
806+
index,
807+
&mut pass.base.dynamic_offsets,
808+
offsets,
809+
offset_length,
810+
)
811+
};
810812

811813
if redundant {
812814
return;
@@ -854,7 +856,7 @@ pub mod compute_ffi {
854856
0,
855857
"Push constant size must be aligned to 4 bytes."
856858
);
857-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
859+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
858860
let value_offset = pass.base.push_constant_data.len().try_into().expect(
859861
"Ran out of push constant space. Don't set 4gb of push constants per ComputePass.",
860862
);
@@ -905,7 +907,7 @@ pub mod compute_ffi {
905907
label: RawString,
906908
color: u32,
907909
) {
908-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
910+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
909911
pass.base.string_data.extend_from_slice(bytes);
910912

911913
pass.base.commands.push(ComputeCommand::PushDebugGroup {
@@ -929,7 +931,7 @@ pub mod compute_ffi {
929931
label: RawString,
930932
color: u32,
931933
) {
932-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
934+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
933935
pass.base.string_data.extend_from_slice(bytes);
934936

935937
pass.base.commands.push(ComputeCommand::InsertDebugMarker {

wgpu-core/src/command/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ impl BindGroupStateChange {
526526
if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
527527
current_bind_group.reset();
528528
}
529-
dynamic_offsets.extend_from_slice(slice::from_raw_parts(offsets, offset_length));
529+
dynamic_offsets
530+
.extend_from_slice(unsafe { slice::from_raw_parts(offsets, offset_length) });
530531
}
531532
false
532533
}

wgpu-core/src/command/render.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,13 +2116,15 @@ pub mod render_ffi {
21162116
offsets: *const DynamicOffset,
21172117
offset_length: usize,
21182118
) {
2119-
let redundant = pass.current_bind_groups.set_and_check_redundant(
2120-
bind_group_id,
2121-
index,
2122-
&mut pass.base.dynamic_offsets,
2123-
offsets,
2124-
offset_length,
2125-
);
2119+
let redundant = unsafe {
2120+
pass.current_bind_groups.set_and_check_redundant(
2121+
bind_group_id,
2122+
index,
2123+
&mut pass.base.dynamic_offsets,
2124+
offsets,
2125+
offset_length,
2126+
)
2127+
};
21262128

21272129
if redundant {
21282130
return;
@@ -2242,7 +2244,7 @@ pub mod render_ffi {
22422244
0,
22432245
"Push constant size must be aligned to 4 bytes."
22442246
);
2245-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
2247+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
22462248
let value_offset = pass.base.push_constant_data.len().try_into().expect(
22472249
"Ran out of push constant space. Don't set 4gb of push constants per RenderPass.",
22482250
);
@@ -2405,7 +2407,7 @@ pub mod render_ffi {
24052407
label: RawString,
24062408
color: u32,
24072409
) {
2408-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
2410+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
24092411
pass.base.string_data.extend_from_slice(bytes);
24102412

24112413
pass.base.commands.push(RenderCommand::PushDebugGroup {
@@ -2429,7 +2431,7 @@ pub mod render_ffi {
24292431
label: RawString,
24302432
color: u32,
24312433
) {
2432-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
2434+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
24332435
pass.base.string_data.extend_from_slice(bytes);
24342436

24352437
pass.base.commands.push(RenderCommand::InsertDebugMarker {
@@ -2481,7 +2483,9 @@ pub mod render_ffi {
24812483
render_bundle_ids: *const id::RenderBundleId,
24822484
render_bundle_ids_length: usize,
24832485
) {
2484-
for &bundle_id in slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) {
2486+
for &bundle_id in
2487+
unsafe { slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) }
2488+
{
24852489
pass.base
24862490
.commands
24872491
.push(RenderCommand::ExecuteBundle(bundle_id));

wgpu-core/src/device/life.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,61 +132,61 @@ impl<A: hal::Api> NonReferencedResources<A> {
132132
if !self.buffers.is_empty() {
133133
profiling::scope!("destroy_buffers");
134134
for raw in self.buffers.drain(..) {
135-
device.destroy_buffer(raw);
135+
unsafe { device.destroy_buffer(raw) };
136136
}
137137
}
138138
if !self.textures.is_empty() {
139139
profiling::scope!("destroy_textures");
140140
for raw in self.textures.drain(..) {
141-
device.destroy_texture(raw);
141+
unsafe { device.destroy_texture(raw) };
142142
}
143143
}
144144
if !self.texture_views.is_empty() {
145145
profiling::scope!("destroy_texture_views");
146146
for raw in self.texture_views.drain(..) {
147-
device.destroy_texture_view(raw);
147+
unsafe { device.destroy_texture_view(raw) };
148148
}
149149
}
150150
if !self.samplers.is_empty() {
151151
profiling::scope!("destroy_samplers");
152152
for raw in self.samplers.drain(..) {
153-
device.destroy_sampler(raw);
153+
unsafe { device.destroy_sampler(raw) };
154154
}
155155
}
156156
if !self.bind_groups.is_empty() {
157157
profiling::scope!("destroy_bind_groups");
158158
for raw in self.bind_groups.drain(..) {
159-
device.destroy_bind_group(raw);
159+
unsafe { device.destroy_bind_group(raw) };
160160
}
161161
}
162162
if !self.compute_pipes.is_empty() {
163163
profiling::scope!("destroy_compute_pipelines");
164164
for raw in self.compute_pipes.drain(..) {
165-
device.destroy_compute_pipeline(raw);
165+
unsafe { device.destroy_compute_pipeline(raw) };
166166
}
167167
}
168168
if !self.render_pipes.is_empty() {
169169
profiling::scope!("destroy_render_pipelines");
170170
for raw in self.render_pipes.drain(..) {
171-
device.destroy_render_pipeline(raw);
171+
unsafe { device.destroy_render_pipeline(raw) };
172172
}
173173
}
174174
if !self.bind_group_layouts.is_empty() {
175175
profiling::scope!("destroy_bind_group_layouts");
176176
for raw in self.bind_group_layouts.drain(..) {
177-
device.destroy_bind_group_layout(raw);
177+
unsafe { device.destroy_bind_group_layout(raw) };
178178
}
179179
}
180180
if !self.pipeline_layouts.is_empty() {
181181
profiling::scope!("destroy_pipeline_layouts");
182182
for raw in self.pipeline_layouts.drain(..) {
183-
device.destroy_pipeline_layout(raw);
183+
unsafe { device.destroy_pipeline_layout(raw) };
184184
}
185185
}
186186
if !self.query_sets.is_empty() {
187187
profiling::scope!("destroy_query_sets");
188188
for raw in self.query_sets.drain(..) {
189-
device.destroy_query_set(raw);
189+
unsafe { device.destroy_query_set(raw) };
190190
}
191191
}
192192
}

wgpu-core/src/device/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4503,10 +4503,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
45034503
});
45044504
};
45054505

4506-
let shader = match device.create_shader_module_spirv(device_id, desc, &source) {
4507-
Ok(shader) => shader,
4508-
Err(e) => break e,
4509-
};
4506+
let shader =
4507+
match unsafe { device.create_shader_module_spirv(device_id, desc, &source) } {
4508+
Ok(shader) => shader,
4509+
Err(e) => break e,
4510+
};
45104511
let id = fid.assign(shader, &mut token);
45114512
return (id.0, None);
45124513
};

0 commit comments

Comments
 (0)