Skip to content

Commit e06c0b5

Browse files
committed
cargo fmt
1 parent 1bf2dd6 commit e06c0b5

File tree

15 files changed

+264
-272
lines changed

15 files changed

+264
-272
lines changed

examples/gpu-clear.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
5151
.with_clear_color(sdl3::pixels::Color::RGB(5, 3, 255)), //blue with small RG bias
5252
];
5353
// Here we do all (none) of our drawing (clearing the screen)
54-
command_buffer.render_pass(
55-
&color_targets,
56-
None,
57-
|_cmd, _pass| {
58-
// Do absolutely nothing -- this clears the screen because of the defined operations above
59-
// which are ALWAYS done even through we just created and ended a render pass
60-
}
61-
)?;
54+
command_buffer.render_pass(&color_targets, None, |_cmd, _pass| {
55+
// Do absolutely nothing -- this clears the screen because of the defined operations above
56+
// which are ALWAYS done even through we just created and ended a render pass
57+
})?;
6258
command_buffer.submit()?;
6359
} else {
6460
// Swapchain unavailable, cancel work

examples/gpu-cube.rs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use sdl3::{
22
event::Event,
33
gpu::{
4-
Buffer, BufferBinding, BufferRegion, BufferUsageFlags, ColorTargetDescription, ColorTargetInfo, CompareOp, CopyPass, CullMode, DepthStencilState, DepthStencilTargetInfo, FillMode, GraphicsPipelineTargetInfo, IndexElementSize, LoadOp, Owned, OwnedDevice, PrimitiveType, RasterizerState, SampleCount, ShaderFormat, ShaderStage, StoreOp, TextureCreateInfo, TextureFormat, TextureType, TextureUsage, TransferBuffer, TransferBufferLocation, TransferBufferUsage, VertexAttribute, VertexBufferDescription, VertexElementFormat, VertexInputRate, VertexInputState
4+
Buffer, BufferBinding, BufferRegion, BufferUsageFlags, ColorTargetDescription,
5+
ColorTargetInfo, CompareOp, CopyPass, CullMode, DepthStencilState, DepthStencilTargetInfo,
6+
FillMode, GraphicsPipelineTargetInfo, IndexElementSize, LoadOp, Owned, OwnedDevice,
7+
PrimitiveType, RasterizerState, SampleCount, ShaderFormat, ShaderStage, StoreOp,
8+
TextureCreateInfo, TextureFormat, TextureType, TextureUsage, TransferBuffer,
9+
TransferBufferLocation, TransferBufferUsage, VertexAttribute, VertexBufferDescription,
10+
VertexElementFormat, VertexInputRate, VertexInputState,
511
},
612
keyboard::Keycode,
713
pixels::Color,
@@ -170,29 +176,29 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
170176

171177
// We need to start a copy pass in order to transfer data to the GPU
172178
let mut copy_commands = gpu.acquire_command_buffer()?;
173-
let (vertex_buffer, index_buffer) = copy_commands.copy_pass(
174-
|_cmd, copy_pass| {
175-
// Create GPU buffers to hold our vertices and indices and transfer data to them
176-
let vertex_buffer = create_buffer_with_data(
177-
&gpu,
178-
&mut transfer_buffer,
179-
&copy_pass,
180-
BufferUsageFlags::VERTEX,
181-
&CUBE_VERTICES,
182-
).unwrap();
183-
let index_buffer = create_buffer_with_data(
184-
&gpu,
185-
&mut transfer_buffer,
186-
&copy_pass,
187-
BufferUsageFlags::INDEX,
188-
&CUBE_INDICES,
189-
).unwrap();
190-
// We're done with the transfer buffer now, so release it.
191-
drop(transfer_buffer);
179+
let (vertex_buffer, index_buffer) = copy_commands.copy_pass(|_cmd, copy_pass| {
180+
// Create GPU buffers to hold our vertices and indices and transfer data to them
181+
let vertex_buffer = create_buffer_with_data(
182+
&gpu,
183+
&mut transfer_buffer,
184+
&copy_pass,
185+
BufferUsageFlags::VERTEX,
186+
&CUBE_VERTICES,
187+
)
188+
.unwrap();
189+
let index_buffer = create_buffer_with_data(
190+
&gpu,
191+
&mut transfer_buffer,
192+
&copy_pass,
193+
BufferUsageFlags::INDEX,
194+
&CUBE_INDICES,
195+
)
196+
.unwrap();
197+
// We're done with the transfer buffer now, so release it.
198+
drop(transfer_buffer);
192199

193-
(vertex_buffer, index_buffer)
194-
}
195-
)?;
200+
(vertex_buffer, index_buffer)
201+
})?;
196202

197203
// Now complete and submit the copy pass commands to actually do the transfer work
198204
copy_commands.submit()?;
@@ -255,20 +261,22 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
255261
render_pass.bind_graphics_pipeline(&pipeline);
256262

257263
// Now we'll bind our buffers and draw the cube
258-
render_pass.bind_vertex_buffers( 0, &[
259-
BufferBinding::new().with_buffer(&vertex_buffer)
260-
]);
264+
render_pass.bind_vertex_buffers(
265+
0,
266+
&[BufferBinding::new().with_buffer(&vertex_buffer)],
267+
);
261268
render_pass.bind_index_buffer(
262269
&BufferBinding::new().with_buffer(&index_buffer),
263-
IndexElementSize::_16BIT);
270+
IndexElementSize::_16BIT,
271+
);
264272

265273
// Set the rotation uniform for our cube vert shader
266274
command_buffer.push_vertex_uniform_data(0, &rotation);
267275
rotation += 0.1f32;
268276

269277
// Finally, draw the cube
270278
render_pass.draw_indexed_primitives(CUBE_INDICES.len() as u32, 1, 0, 0, 0);
271-
}
279+
},
272280
)?;
273281
command_buffer.submit()?;
274282
} else {
@@ -303,16 +311,13 @@ fn create_buffer_with_data<'gpu, T: Copy>(
303311
// Note: We set `cycle` to true since we're reusing the same transfer buffer to
304312
// initialize both the vertex and index buffer. This makes SDL synchronize the transfers
305313
// so that one doesn't interfere with the other.
306-
transfer_buffer.mapped_mut(
307-
true,
308-
|bytes| unsafe {
309-
std::ptr::copy_nonoverlapping::<u8>(
310-
data.as_ptr() as *const u8,
311-
bytes.as_mut_ptr(),
312-
len_bytes
313-
);
314-
}
315-
)?;
314+
transfer_buffer.mapped_mut(true, |bytes| unsafe {
315+
std::ptr::copy_nonoverlapping::<u8>(
316+
data.as_ptr() as *const u8,
317+
bytes.as_mut_ptr(),
318+
len_bytes,
319+
);
320+
})?;
316321

317322
// Finally, add a command to the copy pass to upload this data to the GPU
318323
//

examples/gpu-texture.rs

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
use sdl3::{
22
event::Event,
33
gpu::{
4-
Buffer, BufferBinding, BufferUsageFlags, ColorTargetDescription, ColorTargetInfo, CompareOp, CopyPass, CullMode, DepthStencilState, DepthStencilTargetInfo, FillMode, Filter, GraphicsPipelineTargetInfo, IndexElementSize, LoadOp, Owned, OwnedDevice, PrimitiveType, RasterizerState, SampleCount, SamplerAddressMode, SamplerCreateInfo, SamplerMipmapMode, ShaderFormat, ShaderStage, StoreOp, Texture, TextureCreateInfo, TextureFormat, TextureRegion, TextureSamplerBinding, TextureTransferInfo, TextureType, TextureUsage, TransferBuffer, TransferBufferLocation, TransferBufferUsage, VertexAttribute, VertexBufferDescription, VertexElementFormat, VertexInputRate, VertexInputState
4+
Buffer, BufferBinding, BufferUsageFlags, ColorTargetDescription, ColorTargetInfo,
5+
CompareOp, CopyPass, CullMode, DepthStencilState, DepthStencilTargetInfo, FillMode, Filter,
6+
GraphicsPipelineTargetInfo, IndexElementSize, LoadOp, Owned, OwnedDevice, PrimitiveType,
7+
RasterizerState, SampleCount, SamplerAddressMode, SamplerCreateInfo, SamplerMipmapMode,
8+
ShaderFormat, ShaderStage, StoreOp, Texture, TextureCreateInfo, TextureFormat,
9+
TextureRegion, TextureSamplerBinding, TextureTransferInfo, TextureType, TextureUsage,
10+
TransferBuffer, TransferBufferLocation, TransferBufferUsage, VertexAttribute,
11+
VertexBufferDescription, VertexElementFormat, VertexInputRate, VertexInputState,
512
},
613
keyboard::Keycode,
714
pixels::Color,
@@ -282,43 +289,53 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
282289

283290
// We need to start a copy pass in order to transfer data to the GPU
284291
let mut copy_commands = gpu.acquire_command_buffer()?;
285-
let (vertex_buffer, index_buffer, cube_texture, cube_texture_sampler)
286-
= copy_commands.copy_pass(|_cmd, copy_pass| {
287-
// Create GPU buffers to hold our vertices and indices and transfer data to them
288-
let vertex_buffer = create_buffer_with_data(
289-
&gpu,
290-
&mut transfer_buffer,
291-
&copy_pass,
292-
BufferUsageFlags::VERTEX,
293-
&CUBE_VERTICES,
294-
).unwrap();
295-
let index_buffer = create_buffer_with_data(
296-
&gpu,
297-
&mut transfer_buffer,
298-
&copy_pass,
299-
BufferUsageFlags::INDEX,
300-
&CUBE_INDICES,
301-
).unwrap();
302-
303-
// We're done with the transfer buffer now, so release it.
304-
drop(transfer_buffer);
305-
306-
// Load up a texture to put on the cube
307-
let cube_texture = create_texture_from_image(&gpu, "./assets/texture.bmp", &copy_pass).unwrap();
308-
309-
// And configure a sampler for pulling pixels from that texture in the frag shader
310-
let cube_texture_sampler = gpu.create_sampler(
311-
SamplerCreateInfo::new()
312-
.with_min_filter(Filter::Nearest)
313-
.with_mag_filter(Filter::Nearest)
314-
.with_mipmap_mode(SamplerMipmapMode::Nearest)
315-
.with_address_mode_u(SamplerAddressMode::Repeat)
316-
.with_address_mode_v(SamplerAddressMode::Repeat)
317-
.with_address_mode_w(SamplerAddressMode::Repeat),
318-
).unwrap();
319-
320-
(vertex_buffer, index_buffer, cube_texture, cube_texture_sampler)
321-
})?;
292+
let (vertex_buffer, index_buffer, cube_texture, cube_texture_sampler) = copy_commands
293+
.copy_pass(|_cmd, copy_pass| {
294+
// Create GPU buffers to hold our vertices and indices and transfer data to them
295+
let vertex_buffer = create_buffer_with_data(
296+
&gpu,
297+
&mut transfer_buffer,
298+
&copy_pass,
299+
BufferUsageFlags::VERTEX,
300+
&CUBE_VERTICES,
301+
)
302+
.unwrap();
303+
let index_buffer = create_buffer_with_data(
304+
&gpu,
305+
&mut transfer_buffer,
306+
&copy_pass,
307+
BufferUsageFlags::INDEX,
308+
&CUBE_INDICES,
309+
)
310+
.unwrap();
311+
312+
// We're done with the transfer buffer now, so release it.
313+
drop(transfer_buffer);
314+
315+
// Load up a texture to put on the cube
316+
let cube_texture =
317+
create_texture_from_image(&gpu, "./assets/texture.bmp", &copy_pass).unwrap();
318+
319+
// And configure a sampler for pulling pixels from that texture in the frag shader
320+
let cube_texture_sampler = gpu
321+
.create_sampler(
322+
SamplerCreateInfo::new()
323+
.with_min_filter(Filter::Nearest)
324+
.with_mag_filter(Filter::Nearest)
325+
.with_mipmap_mode(SamplerMipmapMode::Nearest)
326+
.with_address_mode_u(SamplerAddressMode::Repeat)
327+
.with_address_mode_v(SamplerAddressMode::Repeat)
328+
.with_address_mode_w(SamplerAddressMode::Repeat),
329+
)
330+
.unwrap();
331+
332+
(
333+
vertex_buffer,
334+
index_buffer,
335+
cube_texture,
336+
cube_texture_sampler,
337+
)
338+
})?;
322339

323340
// Now complete and submit the copy pass commands to actually do the transfer work
324341
copy_commands.submit()?;
@@ -333,7 +350,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
333350
.with_num_levels(1)
334351
.with_sample_count(SampleCount::NoMultiSampling)
335352
.with_format(TextureFormat::D16_UNORM)
336-
.with_usage(TextureUsage::SAMPLER | TextureUsage::DEPTH_STENCIL_TARGET)
353+
.with_usage(TextureUsage::SAMPLER | TextureUsage::DEPTH_STENCIL_TARGET),
337354
)?;
338355

339356
let mut rotation = 45.0f32;
@@ -373,7 +390,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
373390
.with_store_op(StoreOp::STORE)
374391
.with_stencil_load_op(LoadOp::CLEAR)
375392
.with_stencil_store_op(StoreOp::STORE);
376-
393+
377394
command_buffer.render_pass(
378395
&color_targets,
379396
Some(&depth_target),
@@ -405,7 +422,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
405422

406423
// Finally, draw the cube
407424
render_pass.draw_indexed_primitives(CUBE_INDICES.len() as u32, 1, 0, 0, 0);
408-
}
425+
},
409426
)?;
410427

411428
command_buffer.submit()?;
@@ -445,16 +462,15 @@ fn create_texture_from_image<'gpu>(
445462
.with_usage(TransferBufferUsage::UPLOAD)
446463
.build()?;
447464

448-
transfer_buffer.mapped_mut(
449-
false,
450-
|bytes| image.with_lock(|image_bytes| unsafe {
465+
transfer_buffer.mapped_mut(false, |bytes| {
466+
image.with_lock(|image_bytes| unsafe {
451467
std::ptr::copy_nonoverlapping::<u8>(
452468
image_bytes.as_ptr(),
453469
bytes.as_mut_ptr(),
454-
image_bytes.len()
470+
image_bytes.len(),
455471
);
456472
})
457-
)?;
473+
})?;
458474

459475
copy_pass.upload_to_gpu_texture(
460476
TextureTransferInfo::new()
@@ -495,16 +511,13 @@ fn create_buffer_with_data<'gpu, T: Copy>(
495511
// Note: We set `cycle` to true since we're reusing the same transfer buffer to
496512
// initialize both the vertex and index buffer. This makes SDL synchronize the transfers
497513
// so that one doesn't interfere with the other.
498-
transfer_buffer.mapped_mut(
499-
true,
500-
|bytes| unsafe {
501-
std::ptr::copy_nonoverlapping::<u8>(
502-
data.as_ptr() as *const u8,
503-
bytes.as_mut_ptr(),
504-
len_bytes
505-
);
506-
}
507-
)?;
514+
transfer_buffer.mapped_mut(true, |bytes| unsafe {
515+
std::ptr::copy_nonoverlapping::<u8>(
516+
data.as_ptr() as *const u8,
517+
bytes.as_mut_ptr(),
518+
len_bytes,
519+
);
520+
})?;
508521

509522
// Finally, add a command to the copy pass to upload this data to the GPU
510523
//

examples/gpu-triangle.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ extern crate sdl3;
33
use sdl3::{
44
event::Event,
55
gpu::{
6-
ColorTargetDescription, ColorTargetInfo, OwnedDevice, FillMode, GraphicsPipelineTargetInfo,
7-
LoadOp, PrimitiveType, ShaderFormat, ShaderStage, StoreOp,
6+
ColorTargetDescription, ColorTargetInfo, FillMode, GraphicsPipelineTargetInfo, LoadOp,
7+
OwnedDevice, PrimitiveType, ShaderFormat, ShaderStage, StoreOp,
88
},
99
keyboard::Keycode,
1010
pixels::Color,
@@ -99,7 +99,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
9999
.with_store_op(StoreOp::STORE)
100100
.with_clear_color(Color::RGB(5, 3, 255)), //blue with small RG bias
101101
];
102-
102+
103103
command_buffer.render_pass(&color_targets, None, |_cmd, render_pass| {
104104
render_pass.bind_graphics_pipeline(&pipeline);
105105
// Screen is cleared here due to the color target info

src/sdl3/gpu/abstraction.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use std::marker::PhantomData;
22

33
use crate::gpu::Buffer;
44

5-
6-
7-
// a typed pseudo-reference to an object located in a `Buffer` on the GPU
5+
// a typed pseudo-reference to an object located in a `Buffer` on the GPU
86
pub struct Ref<'a, T> {
97
pub(crate) buf: &'a Buffer,
108
pub(crate) offset: u32,
119
pub(crate) marker: PhantomData<&'a T>,
12-
}
10+
}

src/sdl3/gpu/auto_trait.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
2-
31
// manually checking auto-traits for a type
42
type _X = u32;
53
const _: () = _copy::<_X>();
64
const _: () = _send::<_X>();
75
const _: () = _sync::<_X>();
86
const _: () = _unpin::<_X>();
97

10-
118
const fn _copy<T: Copy>() {}
129
const fn _send<T: Send>() {}
1310
const fn _sync<T: Sync>() {}
@@ -30,7 +27,7 @@ mod assertions {
3027
}
3128

3229
use crate::gpu::*;
33-
30+
3431
// definitely not thread-safe
3532
thread_unsafe!(CommandBuffer);
3633

@@ -48,4 +45,4 @@ mod assertions {
4845
thread_unsafe!(GraphicsPipeline);
4946
thread_unsafe!(ComputePipeline);
5047
thread_unsafe!(Shader);
51-
}
48+
}

0 commit comments

Comments
 (0)