Skip to content

Commit 49ddd3c

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 0e20ecb commit 49ddd3c

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
@@ -753,7 +753,7 @@ impl<A: HalApi> RenderBundle<A> {
753753
let mut offsets = self.base.dynamic_offsets.as_slice();
754754
let mut pipeline_layout_id = None::<id::Valid<id::PipelineLayoutId>>;
755755
if let Some(ref label) = self.base.label {
756-
raw.begin_debug_marker(label);
756+
unsafe { raw.begin_debug_marker(label) };
757757
}
758758

759759
for command in self.base.commands.iter() {
@@ -764,17 +764,19 @@ impl<A: HalApi> RenderBundle<A> {
764764
bind_group_id,
765765
} => {
766766
let bind_group = bind_group_guard.get(bind_group_id).unwrap();
767-
raw.set_bind_group(
768-
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
769-
index as u32,
770-
&bind_group.raw,
771-
&offsets[..num_dynamic_offsets as usize],
772-
);
767+
unsafe {
768+
raw.set_bind_group(
769+
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
770+
index as u32,
771+
&bind_group.raw,
772+
&offsets[..num_dynamic_offsets as usize],
773+
)
774+
};
773775
offsets = &offsets[num_dynamic_offsets as usize..];
774776
}
775777
RenderCommand::SetPipeline(pipeline_id) => {
776778
let pipeline = pipeline_guard.get(pipeline_id).unwrap();
777-
raw.set_render_pipeline(&pipeline.raw);
779+
unsafe { raw.set_render_pipeline(&pipeline.raw) };
778780

779781
pipeline_layout_id = Some(pipeline.layout_id.value);
780782
}
@@ -795,7 +797,7 @@ impl<A: HalApi> RenderBundle<A> {
795797
offset,
796798
size,
797799
};
798-
raw.set_index_buffer(bb, index_format);
800+
unsafe { raw.set_index_buffer(bb, index_format) };
799801
}
800802
RenderCommand::SetVertexBuffer {
801803
slot,
@@ -814,7 +816,7 @@ impl<A: HalApi> RenderBundle<A> {
814816
offset,
815817
size,
816818
};
817-
raw.set_vertex_buffer(slot, bb);
819+
unsafe { raw.set_vertex_buffer(slot, bb) };
818820
}
819821
RenderCommand::SetPushConstant {
820822
stages,
@@ -831,18 +833,22 @@ impl<A: HalApi> RenderBundle<A> {
831833
let data_slice = &self.base.push_constant_data
832834
[(values_offset as usize)..values_end_offset];
833835

834-
raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
836+
unsafe {
837+
raw.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
838+
}
835839
} else {
836840
super::push_constant_clear(
837841
offset,
838842
size_bytes,
839843
|clear_offset, clear_data| {
840-
raw.set_push_constants(
841-
&pipeline_layout.raw,
842-
stages,
843-
clear_offset,
844-
clear_data,
845-
);
844+
unsafe {
845+
raw.set_push_constants(
846+
&pipeline_layout.raw,
847+
stages,
848+
clear_offset,
849+
clear_data,
850+
)
851+
};
846852
},
847853
);
848854
}
@@ -853,7 +859,7 @@ impl<A: HalApi> RenderBundle<A> {
853859
first_vertex,
854860
first_instance,
855861
} => {
856-
raw.draw(first_vertex, vertex_count, first_instance, instance_count);
862+
unsafe { raw.draw(first_vertex, vertex_count, first_instance, instance_count) };
857863
}
858864
RenderCommand::DrawIndexed {
859865
index_count,
@@ -862,13 +868,15 @@ impl<A: HalApi> RenderBundle<A> {
862868
base_vertex,
863869
first_instance,
864870
} => {
865-
raw.draw_indexed(
866-
first_index,
867-
index_count,
868-
base_vertex,
869-
first_instance,
870-
instance_count,
871-
);
871+
unsafe {
872+
raw.draw_indexed(
873+
first_index,
874+
index_count,
875+
base_vertex,
876+
first_instance,
877+
instance_count,
878+
)
879+
};
872880
}
873881
RenderCommand::MultiDrawIndirect {
874882
buffer_id,
@@ -882,7 +890,7 @@ impl<A: HalApi> RenderBundle<A> {
882890
.raw
883891
.as_ref()
884892
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
885-
raw.draw_indirect(buffer, offset, 1);
893+
unsafe { raw.draw_indirect(buffer, offset, 1) };
886894
}
887895
RenderCommand::MultiDrawIndirect {
888896
buffer_id,
@@ -896,7 +904,7 @@ impl<A: HalApi> RenderBundle<A> {
896904
.raw
897905
.as_ref()
898906
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
899-
raw.draw_indexed_indirect(buffer, offset, 1);
907+
unsafe { raw.draw_indexed_indirect(buffer, offset, 1) };
900908
}
901909
RenderCommand::MultiDrawIndirect { .. }
902910
| RenderCommand::MultiDrawIndirectCount { .. } => {
@@ -921,7 +929,7 @@ impl<A: HalApi> RenderBundle<A> {
921929
}
922930

923931
if let Some(_) = self.base.label {
924-
raw.end_debug_marker();
932+
unsafe { raw.end_debug_marker() };
925933
}
926934

927935
Ok(())
@@ -1429,13 +1437,15 @@ pub mod bundle_ffi {
14291437
offsets: *const DynamicOffset,
14301438
offset_length: usize,
14311439
) {
1432-
let redundant = bundle.current_bind_groups.set_and_check_redundant(
1433-
bind_group_id,
1434-
index,
1435-
&mut bundle.base.dynamic_offsets,
1436-
offsets,
1437-
offset_length,
1438-
);
1440+
let redundant = unsafe {
1441+
bundle.current_bind_groups.set_and_check_redundant(
1442+
bind_group_id,
1443+
index,
1444+
&mut bundle.base.dynamic_offsets,
1445+
offsets,
1446+
offset_length,
1447+
)
1448+
};
14391449

14401450
if redundant {
14411451
return;
@@ -1512,7 +1522,7 @@ pub mod bundle_ffi {
15121522
0,
15131523
"Push constant size must be aligned to 4 bytes."
15141524
);
1515-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
1525+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
15161526
let value_offset = pass.base.push_constant_data.len().try_into().expect(
15171527
"Ran out of push constant space. Don't set 4gb of push constants per RenderBundle.",
15181528
);

wgpu-core/src/command/compute.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,15 @@ pub mod compute_ffi {
795795
offsets: *const DynamicOffset,
796796
offset_length: usize,
797797
) {
798-
let redundant = pass.current_bind_groups.set_and_check_redundant(
799-
bind_group_id,
800-
index,
801-
&mut pass.base.dynamic_offsets,
802-
offsets,
803-
offset_length,
804-
);
798+
let redundant = unsafe {
799+
pass.current_bind_groups.set_and_check_redundant(
800+
bind_group_id,
801+
index,
802+
&mut pass.base.dynamic_offsets,
803+
offsets,
804+
offset_length,
805+
)
806+
};
805807

806808
if redundant {
807809
return;
@@ -849,7 +851,7 @@ pub mod compute_ffi {
849851
0,
850852
"Push constant size must be aligned to 4 bytes."
851853
);
852-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
854+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
853855
let value_offset = pass.base.push_constant_data.len().try_into().expect(
854856
"Ran out of push constant space. Don't set 4gb of push constants per ComputePass.",
855857
);
@@ -900,7 +902,7 @@ pub mod compute_ffi {
900902
label: RawString,
901903
color: u32,
902904
) {
903-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
905+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
904906
pass.base.string_data.extend_from_slice(bytes);
905907

906908
pass.base.commands.push(ComputeCommand::PushDebugGroup {
@@ -924,7 +926,7 @@ pub mod compute_ffi {
924926
label: RawString,
925927
color: u32,
926928
) {
927-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
929+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
928930
pass.base.string_data.extend_from_slice(bytes);
929931

930932
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
@@ -525,7 +525,8 @@ impl BindGroupStateChange {
525525
if let Some(current_bind_group) = self.last_states.get_mut(index as usize) {
526526
current_bind_group.reset();
527527
}
528-
dynamic_offsets.extend_from_slice(slice::from_raw_parts(offsets, offset_length));
528+
dynamic_offsets
529+
.extend_from_slice(unsafe { slice::from_raw_parts(offsets, offset_length) });
529530
}
530531
false
531532
}

wgpu-core/src/command/render.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,13 +2084,15 @@ pub mod render_ffi {
20842084
offsets: *const DynamicOffset,
20852085
offset_length: usize,
20862086
) {
2087-
let redundant = pass.current_bind_groups.set_and_check_redundant(
2088-
bind_group_id,
2089-
index,
2090-
&mut pass.base.dynamic_offsets,
2091-
offsets,
2092-
offset_length,
2093-
);
2087+
let redundant = unsafe {
2088+
pass.current_bind_groups.set_and_check_redundant(
2089+
bind_group_id,
2090+
index,
2091+
&mut pass.base.dynamic_offsets,
2092+
offsets,
2093+
offset_length,
2094+
)
2095+
};
20942096

20952097
if redundant {
20962098
return;
@@ -2210,7 +2212,7 @@ pub mod render_ffi {
22102212
0,
22112213
"Push constant size must be aligned to 4 bytes."
22122214
);
2213-
let data_slice = slice::from_raw_parts(data, size_bytes as usize);
2215+
let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) };
22142216
let value_offset = pass.base.push_constant_data.len().try_into().expect(
22152217
"Ran out of push constant space. Don't set 4gb of push constants per RenderPass.",
22162218
);
@@ -2373,7 +2375,7 @@ pub mod render_ffi {
23732375
label: RawString,
23742376
color: u32,
23752377
) {
2376-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
2378+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
23772379
pass.base.string_data.extend_from_slice(bytes);
23782380

23792381
pass.base.commands.push(RenderCommand::PushDebugGroup {
@@ -2397,7 +2399,7 @@ pub mod render_ffi {
23972399
label: RawString,
23982400
color: u32,
23992401
) {
2400-
let bytes = ffi::CStr::from_ptr(label).to_bytes();
2402+
let bytes = unsafe { ffi::CStr::from_ptr(label) }.to_bytes();
24012403
pass.base.string_data.extend_from_slice(bytes);
24022404

24032405
pass.base.commands.push(RenderCommand::InsertDebugMarker {
@@ -2449,7 +2451,9 @@ pub mod render_ffi {
24492451
render_bundle_ids: *const id::RenderBundleId,
24502452
render_bundle_ids_length: usize,
24512453
) {
2452-
for &bundle_id in slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) {
2454+
for &bundle_id in
2455+
unsafe { slice::from_raw_parts(render_bundle_ids, render_bundle_ids_length) }
2456+
{
24532457
pass.base
24542458
.commands
24552459
.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
@@ -4472,10 +4472,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
44724472
});
44734473
};
44744474

4475-
let shader = match device.create_shader_module_spirv(device_id, desc, &source) {
4476-
Ok(shader) => shader,
4477-
Err(e) => break e,
4478-
};
4475+
let shader =
4476+
match unsafe { device.create_shader_module_spirv(device_id, desc, &source) } {
4477+
Ok(shader) => shader,
4478+
Err(e) => break e,
4479+
};
44794480
let id = fid.assign(shader, &mut token);
44804481
return (id.0, None);
44814482
};

0 commit comments

Comments
 (0)