1
1
use sdl3:: {
2
2
event:: Event ,
3
3
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 ,
5
12
} ,
6
13
keyboard:: Keycode ,
7
14
pixels:: Color ,
@@ -282,43 +289,53 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
282
289
283
290
// We need to start a copy pass in order to transfer data to the GPU
284
291
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
+ } ) ?;
322
339
323
340
// Now complete and submit the copy pass commands to actually do the transfer work
324
341
copy_commands. submit ( ) ?;
@@ -333,7 +350,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
333
350
. with_num_levels ( 1 )
334
351
. with_sample_count ( SampleCount :: NoMultiSampling )
335
352
. with_format ( TextureFormat :: D16_UNORM )
336
- . with_usage ( TextureUsage :: SAMPLER | TextureUsage :: DEPTH_STENCIL_TARGET )
353
+ . with_usage ( TextureUsage :: SAMPLER | TextureUsage :: DEPTH_STENCIL_TARGET ) ,
337
354
) ?;
338
355
339
356
let mut rotation = 45.0f32 ;
@@ -373,7 +390,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
373
390
. with_store_op ( StoreOp :: STORE )
374
391
. with_stencil_load_op ( LoadOp :: CLEAR )
375
392
. with_stencil_store_op ( StoreOp :: STORE ) ;
376
-
393
+
377
394
command_buffer. render_pass (
378
395
& color_targets,
379
396
Some ( & depth_target) ,
@@ -405,7 +422,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
405
422
406
423
// Finally, draw the cube
407
424
render_pass. draw_indexed_primitives ( CUBE_INDICES . len ( ) as u32 , 1 , 0 , 0 , 0 ) ;
408
- }
425
+ } ,
409
426
) ?;
410
427
411
428
command_buffer. submit ( ) ?;
@@ -445,16 +462,15 @@ fn create_texture_from_image<'gpu>(
445
462
. with_usage ( TransferBufferUsage :: UPLOAD )
446
463
. build ( ) ?;
447
464
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 {
451
467
std:: ptr:: copy_nonoverlapping :: < u8 > (
452
468
image_bytes. as_ptr ( ) ,
453
469
bytes. as_mut_ptr ( ) ,
454
- image_bytes. len ( )
470
+ image_bytes. len ( ) ,
455
471
) ;
456
472
} )
457
- ) ?;
473
+ } ) ?;
458
474
459
475
copy_pass. upload_to_gpu_texture (
460
476
TextureTransferInfo :: new ( )
@@ -495,16 +511,13 @@ fn create_buffer_with_data<'gpu, T: Copy>(
495
511
// Note: We set `cycle` to true since we're reusing the same transfer buffer to
496
512
// initialize both the vertex and index buffer. This makes SDL synchronize the transfers
497
513
// 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
+ } ) ?;
508
521
509
522
// Finally, add a command to the copy pass to upload this data to the GPU
510
523
//
0 commit comments