Skip to content

Commit

Permalink
chore: use std::mem::size_of{,_val} s'more
Browse files Browse the repository at this point in the history
As before, this is to minimize diffs. with Rust 1.80.
  • Loading branch information
ErichDonGubler committed Aug 28, 2024
1 parent 6710d94 commit b3b3a34
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 105 deletions.
4 changes: 2 additions & 2 deletions examples/src/boids/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/cube/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions examples/src/hello_compute/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions examples/src/hello_synchronization/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::size_of_val;

const ARR_SIZE: usize = 128;

struct ExecuteResults {
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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(..);
Expand Down
9 changes: 4 additions & 5 deletions examples/src/mipmap/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions examples/src/shadow/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()),
);
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/src/skybox/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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],
}],
Expand Down
4 changes: 2 additions & 2 deletions examples/src/stencil_triangles/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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)];

Expand Down
7 changes: 5 additions & 2 deletions examples/src/texture_arrays/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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"),
Expand Down
8 changes: 5 additions & 3 deletions examples/src/timestamp_queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}),
Expand Down Expand Up @@ -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()
};
Expand Down
4 changes: 2 additions & 2 deletions examples/src/uniform_values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
});
Expand Down
18 changes: 9 additions & 9 deletions examples/src/water/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

///
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
Expand Down Expand Up @@ -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]),
);

Expand Down
3 changes: 2 additions & 1 deletion player/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use player::GlobalPlay;
use std::{
fs::{read_to_string, File},
io::{Read, Seek, SeekFrom},
mem::size_of,
path::{Path, PathBuf},
slice,
};
Expand All @@ -35,7 +36,7 @@ impl ExpectedData {
fn len(&self) -> usize {
match self {
ExpectedData::Raw(vec) => vec.len(),
ExpectedData::U64(vec) => vec.len() * std::mem::size_of::<u64>(),
ExpectedData::U64(vec) => vec.len() * size_of::<u64>(),
ExpectedData::File(_, size) => *size,
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/compute_pass_ownership.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Tests that compute passes take ownership of resources that are associated with.
//! I.e. once a resource is passed in to a compute pass, it can be dropped.
use std::num::NonZeroU64;
use std::{mem::size_of, num::NonZeroU64};

use wgpu::util::DeviceExt as _;
use wgpu_test::{gpu_test, valid, GpuTestConfiguration, TestParameters, TestingContext};
Expand Down Expand Up @@ -253,7 +253,7 @@ fn resource_setup(ctx: &TestingContext) -> ResourceSetup {
source: wgpu::ShaderSource::Wgsl(SHADER_SRC.into()),
});

let buffer_size = 4 * std::mem::size_of::<f32>() as u64;
let buffer_size = 4 * size_of::<f32>() as u64;

let bgl = ctx
.device
Expand Down
Loading

0 comments on commit b3b3a34

Please sign in to comment.