chore: use std::mem::size_of{,_val} s'more

As before, this is to minimize diffs. with Rust 1.80.
This commit is contained in:
Erich Gubler committed 2024-09-03 10:18:25 -04:00
1 parent e1c0aed520
commit 85346dfd20
30 files changed
+126 -111

No files matched your search

+2 -2
View File
@@ -2,7 +2,7 @@
// adapted from https://github.com/austinEng/webgpu-samples/blob/master/src/examples/computeBoids.ts
use nanorand::{Rng, WyRand};
use std::{borrow::Cow, mem};
use std::{borrow::Cow, mem::size_of};
use wgpu::util::DeviceExt;
// number of boid particles to simulate
@@ -82,7 +82,7 @@ impl crate::framework::Example for Example {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(
(sim_param_data.len() * mem::size_of::<f32>()) as _,
(sim_param_data.len() * size_of::<f32>()) as _,
),
},
count: None,
+2 -2
View File
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, f32::consts, mem};
use std::{borrow::Cow, f32::consts, mem::size_of};
use wgpu::util::DeviceExt;
#[repr(C)]
@@ -114,7 +114,7 @@ impl crate::framework::Example for Example {
queue: &wgpu::Queue,
) -> Self {
// Create the vertex and index buffers
let vertex_size = mem::size_of::<Vertex>();
let vertex_size = size_of::<Vertex>();
let (vertex_data, index_data) = create_vertices();
let vertex_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+2 -2
View File
@@ -1,4 +1,4 @@
use std::{borrow::Cow, str::FromStr};
use std::{borrow::Cow, mem::size_of_val, str::FromStr};
use wgpu::util::DeviceExt;
// Indicates a u32 overflow in an intermediate Collatz value
@@ -72,7 +72,7 @@ async fn execute_gpu_inner(
});
// Gets the size in bytes of the buffer.
let size = std::mem::size_of_val(numbers) as wgpu::BufferAddress;
let size = size_of_val(numbers) as wgpu::BufferAddress;
// Instantiates buffer without data.
// `usage` of buffer specifies how it can be used:
+5 -3
View File
@@ -1,3 +1,5 @@
use std::mem::size_of_val;
const ARR_SIZE: usize = 128;
struct ExecuteResults {
@@ -61,13 +63,13 @@ async fn execute(
let storage_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: std::mem::size_of_val(local_patient_workgroup_results.as_slice()) as u64,
size: size_of_val(local_patient_workgroup_results.as_slice()) as u64,
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
mapped_at_creation: false,
});
let output_staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: std::mem::size_of_val(local_patient_workgroup_results.as_slice()) as u64,
size: size_of_val(local_patient_workgroup_results.as_slice()) as u64,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
@@ -182,7 +184,7 @@ async fn get_data<T: bytemuck::Pod>(
0,
staging_buffer,
0,
std::mem::size_of_val(output) as u64,
size_of_val(output) as u64,
);
queue.submit(Some(command_encoder.finish()));
let buffer_slice = staging_buffer.slice(..);
+4 -2
View File
@@ -7,6 +7,8 @@
//!
//! Only parts specific to this example will be commented.
use std::mem::size_of_val;
use wgpu::util::DeviceExt;
async fn run() {
@@ -56,7 +58,7 @@ async fn run() {
});
let output_staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: std::mem::size_of_val(&local_a) as u64,
size: size_of_val(&local_a) as u64,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
@@ -169,7 +171,7 @@ async fn get_data<T: bytemuck::Pod>(
0,
staging_buffer,
0,
std::mem::size_of_val(output) as u64,
size_of_val(output) as u64,
);
queue.submit(Some(command_encoder.finish()));
let buffer_slice = staging_buffer.slice(..);
+4 -5
View File
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, f32::consts, mem};
use std::{borrow::Cow, f32::consts, mem::size_of};
use wgpu::util::DeviceExt;
const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
@@ -54,8 +54,7 @@ type TimestampQueries = [TimestampData; MIP_PASS_COUNT as usize];
type PipelineStatisticsQueries = [u64; MIP_PASS_COUNT as usize];
fn pipeline_statistics_offset() -> wgpu::BufferAddress {
(mem::size_of::<TimestampQueries>() as wgpu::BufferAddress)
.max(wgpu::QUERY_RESOLVE_BUFFER_ALIGNMENT)
(size_of::<TimestampQueries>() as wgpu::BufferAddress).max(wgpu::QUERY_RESOLVE_BUFFER_ALIGNMENT)
}
struct Example {
@@ -363,7 +362,7 @@ impl crate::framework::Example for Example {
// This databuffer has to store all of the query results, 2 * passes timestamp queries
// and 1 * passes statistics queries. Each query returns a u64 value.
let buffer_size = pipeline_statistics_offset()
+ mem::size_of::<PipelineStatisticsQueries>() as wgpu::BufferAddress;
+ size_of::<PipelineStatisticsQueries>() as wgpu::BufferAddress;
let data_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("query buffer"),
size: buffer_size,
@@ -420,7 +419,7 @@ impl crate::framework::Example for Example {
// This is guaranteed to be ready.
let timestamp_view = query_sets
.mapping_buffer
.slice(..mem::size_of::<TimestampQueries>() as wgpu::BufferAddress)
.slice(..size_of::<TimestampQueries>() as wgpu::BufferAddress)
.get_mapped_range();
let pipeline_stats_view = query_sets
.mapping_buffer
+2 -2
View File
@@ -7,7 +7,7 @@
//! * Set the primitive_topology to PrimitiveTopology::LineList.
//! * Vertices and Indices describe the two points that make up a line.
use std::{borrow::Cow, iter};
use std::{borrow::Cow, iter, mem::size_of};
use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
@@ -56,7 +56,7 @@ impl Example {
entry_point: Some("vs_main"),
compilation_options: Default::default(),
buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4],
}],
+8 -9
View File
@@ -1,4 +1,4 @@
use std::{borrow::Cow, f32::consts, iter, mem, ops::Range, sync::Arc};
use std::{borrow::Cow, f32::consts, iter, mem::size_of, ops::Range, sync::Arc};
use bytemuck::{Pod, Zeroable};
use wgpu::util::{align_to, DeviceExt};
@@ -219,7 +219,7 @@ impl crate::framework::Example for Example {
&& device.limits().max_storage_buffers_per_shader_stage > 0;
// Create the vertex and index buffers
let vertex_size = mem::size_of::<Vertex>();
let vertex_size = size_of::<Vertex>();
let (cube_vertex_data, cube_index_data) = create_cube();
let cube_vertex_buf = Arc::new(device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
@@ -283,7 +283,7 @@ impl crate::framework::Example for Example {
},
];
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
let entity_uniform_size = size_of::<EntityUniforms>() as wgpu::BufferAddress;
let num_entities = 1 + cube_descs.len() as wgpu::BufferAddress;
// Make the `uniform_alignment` >= `entity_uniform_size` and aligned to `min_uniform_buffer_offset_alignment`.
let uniform_alignment = {
@@ -427,8 +427,7 @@ impl crate::framework::Example for Example {
target_view: shadow_target_views[1].take().unwrap(),
},
];
let light_uniform_size =
(Self::MAX_LIGHTS * mem::size_of::<LightRaw>()) as wgpu::BufferAddress;
let light_uniform_size = (Self::MAX_LIGHTS * size_of::<LightRaw>()) as wgpu::BufferAddress;
let light_storage_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: light_uniform_size,
@@ -454,7 +453,7 @@ impl crate::framework::Example for Example {
});
let shadow_pass = {
let uniform_size = mem::size_of::<GlobalUniforms>() as wgpu::BufferAddress;
let uniform_size = size_of::<GlobalUniforms>() as wgpu::BufferAddress;
// Create pipeline layout
let bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@@ -548,7 +547,7 @@ impl crate::framework::Example for Example {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(
mem::size_of::<GlobalUniforms>() as _,
size_of::<GlobalUniforms>() as _,
),
},
count: None,
@@ -737,7 +736,7 @@ impl crate::framework::Example for Example {
for (i, light) in self.lights.iter().enumerate() {
queue.write_buffer(
&self.light_storage_buf,
(i * mem::size_of::<LightRaw>()) as wgpu::BufferAddress,
(i * size_of::<LightRaw>()) as wgpu::BufferAddress,
bytemuck::bytes_of(&light.to_raw()),
);
}
@@ -757,7 +756,7 @@ impl crate::framework::Example for Example {
// let's just copy it over to the shadow uniform buffer.
encoder.copy_buffer_to_buffer(
&self.light_storage_buf,
(i * mem::size_of::<LightRaw>()) as wgpu::BufferAddress,
(i * size_of::<LightRaw>()) as wgpu::BufferAddress,
&self.shadow_pass.uniform_buf,
0,
64,
+2 -2
View File
@@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, f32::consts};
use std::{borrow::Cow, f32::consts, mem::size_of};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
const IMAGE_SIZE: u32 = 256;
@@ -231,7 +231,7 @@ impl crate::framework::Example for Example {
entry_point: Some("vs_entity"),
compilation_options: Default::default(),
buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3],
}],
+2 -2
View File
@@ -1,6 +1,6 @@
use bytemuck::{Pod, Zeroable};
use std::borrow::Cow;
use std::mem;
use std::mem::size_of;
use wgpu::util::DeviceExt;
#[repr(C)]
@@ -31,7 +31,7 @@ impl crate::framework::Example for Example {
_queue: &wgpu::Queue,
) -> Self {
// Create the vertex and index buffers
let vertex_size = mem::size_of::<Vertex>();
let vertex_size = size_of::<Vertex>();
let outer_vertices = [vertex(-1.0, -1.0), vertex(1.0, -1.0), vertex(0.0, 1.0)];
let mask_vertices = [vertex(-0.5, 0.0), vertex(0.0, -1.0), vertex(0.5, 0.0)];
+3 -1
View File
@@ -14,6 +14,8 @@
//! A lot of things aren't explained here via comments. See hello-compute and
//! repeated-compute for code that is more thoroughly commented.
use std::mem::size_of_val;
#[cfg(not(target_arch = "wasm32"))]
use crate::utils::output_image_native;
#[cfg(target_arch = "wasm32")]
@@ -64,7 +66,7 @@ async fn run(_path: Option<String>) {
let storage_texture_view = storage_texture.create_view(&wgpu::TextureViewDescriptor::default());
let output_staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: std::mem::size_of_val(&texture_data[..]) as u64,
size: size_of_val(&texture_data[..]) as u64,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
+5 -2
View File
@@ -1,5 +1,8 @@
use bytemuck::{Pod, Zeroable};
use std::num::{NonZeroU32, NonZeroU64};
use std::{
mem::size_of,
num::{NonZeroU32, NonZeroU64},
};
use wgpu::util::DeviceExt;
#[repr(C)]
@@ -124,7 +127,7 @@ impl crate::framework::Example for Example {
println!("Using fragment entry point '{fragment_entry_point}'");
let vertex_size = std::mem::size_of::<Vertex>();
let vertex_size = size_of::<Vertex>();
let vertex_data = create_vertices();
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"),
+5 -3
View File
@@ -17,6 +17,8 @@
//! The period, i.e. the unit of time, of the timestamps in wgpu is undetermined and needs to be queried with `wgpu::Queue::get_timestamp_period`
//! in order to get comparable results.
use std::mem::size_of;
use wgpu::util::DeviceExt;
struct Queries {
@@ -123,13 +125,13 @@ impl Queries {
}),
resolve_buffer: device.create_buffer(&wgpu::BufferDescriptor {
label: Some("query resolve buffer"),
size: std::mem::size_of::<u64>() as u64 * num_queries,
size: size_of::<u64>() as u64 * num_queries,
usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::QUERY_RESOLVE,
mapped_at_creation: false,
}),
destination_buffer: device.create_buffer(&wgpu::BufferDescriptor {
label: Some("query dest buffer"),
size: std::mem::size_of::<u64>() as u64 * num_queries,
size: size_of::<u64>() as u64 * num_queries,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
}),
@@ -164,7 +166,7 @@ impl Queries {
let timestamps = {
let timestamp_view = self
.destination_buffer
.slice(..(std::mem::size_of::<u64>() as wgpu::BufferAddress * self.num_queries))
.slice(..(size_of::<u64>() as wgpu::BufferAddress * self.num_queries))
.get_mapped_range();
bytemuck::cast_slice(&timestamp_view).to_vec()
};
+2 -2
View File
@@ -16,7 +16,7 @@
//! The usage of the uniform buffer within the shader itself is pretty self-explanatory given
//! some understanding of WGSL.
use std::sync::Arc;
use std::{mem::size_of, sync::Arc};
// We won't bring StorageBuffer into scope as that might be too easy to confuse
// with actual GPU-allocated WGPU storage buffers.
use encase::ShaderType;
@@ -132,7 +132,7 @@ impl WgpuContext {
// (2)
let uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: std::mem::size_of::<AppState>() as u64,
size: size_of::<AppState>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
+9 -9
View File
@@ -3,7 +3,7 @@ mod point_gen;
use bytemuck::{Pod, Zeroable};
use glam::Vec3;
use nanorand::{Rng, WyRand};
use std::{borrow::Cow, f32::consts, iter, mem};
use std::{borrow::Cow, f32::consts, iter, mem::size_of};
use wgpu::util::DeviceExt;
///
@@ -273,12 +273,12 @@ impl crate::framework::Example for Example {
queue: &wgpu::Queue,
) -> Self {
// Size of one water vertex
let water_vertex_size = mem::size_of::<point_gen::WaterVertexAttributes>();
let water_vertex_size = size_of::<point_gen::WaterVertexAttributes>();
let water_vertices = point_gen::HexWaterMesh::generate(SIZE).generate_points();
// Size of one terrain vertex
let terrain_vertex_size = mem::size_of::<point_gen::TerrainVertexAttributes>();
let terrain_vertex_size = size_of::<point_gen::TerrainVertexAttributes>();
// Noise generation
let terrain_noise = noise::OpenSimplex::default();
@@ -359,7 +359,7 @@ impl crate::framework::Example for Example {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(
mem::size_of::<WaterUniforms>() as _,
size_of::<WaterUniforms>() as _,
),
},
count: None,
@@ -415,7 +415,7 @@ impl crate::framework::Example for Example {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(
mem::size_of::<TerrainUniforms>() as _,
size_of::<TerrainUniforms>() as _
),
},
count: None,
@@ -440,21 +440,21 @@ impl crate::framework::Example for Example {
let water_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Water Uniforms"),
size: mem::size_of::<WaterUniforms>() as _,
size: size_of::<WaterUniforms>() as _,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let terrain_normal_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Normal Terrain Uniforms"),
size: mem::size_of::<TerrainUniforms>() as _,
size: size_of::<TerrainUniforms>() as _,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let terrain_flipped_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Flipped Terrain Uniforms"),
size: mem::size_of::<TerrainUniforms>() as _,
size: size_of::<TerrainUniforms>() as _,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
@@ -712,7 +712,7 @@ impl crate::framework::Example for Example {
let (water_sin, water_cos) = ((self.current_frame as f32) / 600.0).sin_cos();
queue.write_buffer(
&self.water_uniform_buf,
mem::size_of::<[f32; 16]>() as wgpu::BufferAddress * 2,
size_of::<[f32; 16]>() as wgpu::BufferAddress * 2,
bytemuck::cast_slice(&[water_sin, water_cos]),
);