sdl3 0.18.0

Bindings to SDL3, a cross-platform library to abstract the platform-specific details for building applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use sdl3::{
    event::Event,
    gpu::{
        Buffer, BufferBinding, BufferRegion, BufferUsageFlags, ColorTargetDescription,
        ColorTargetInfo, CompareOp, CopyPass, CullMode, DepthStencilState, DepthStencilTargetInfo,
        Device, FillMode, Filter, GraphicsPipelineTargetInfo, IndexElementSize, LoadOp,
        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,
    },
    keyboard::Keycode,
    pixels::Color,
    surface::Surface,
    Error,
};
use std::path::Path;

#[repr(packed)]
#[derive(Copy, Clone)]
pub struct Vertex {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub u: f32,
    pub v: f32,
}

// Below are the vertices and indices that make up the 3D mesh of the cube.
const CUBE_VERTICES: &[Vertex] = &[
    Vertex {
        x: -0.5,
        y: -0.5,
        z: -0.5,
        u: 0.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: -0.5,
        z: -0.5,
        u: 1.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: 0.5,
        z: -0.5,
        u: 1.0,
        v: 1.0,
    },
    Vertex {
        x: -0.5,
        y: 0.5,
        z: -0.5,
        u: 0.0,
        v: 1.0,
    },
    Vertex {
        x: -0.5,
        y: -0.5,
        z: 0.5,
        u: 0.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: -0.5,
        z: 0.5,
        u: 1.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: 0.5,
        z: 0.5,
        u: 1.0,
        v: 1.0,
    },
    Vertex {
        x: -0.5,
        y: 0.5,
        z: 0.5,
        u: 0.0,
        v: 1.0,
    },
    Vertex {
        x: -0.5,
        y: -0.5,
        z: 0.5,
        u: 0.0,
        v: 0.0,
    },
    Vertex {
        x: -0.5,
        y: -0.5,
        z: -0.5,
        u: 1.0,
        v: 0.0,
    },
    Vertex {
        x: -0.5,
        y: 0.5,
        z: -0.5,
        u: 1.0,
        v: 1.0,
    },
    Vertex {
        x: -0.5,
        y: 0.5,
        z: 0.5,
        u: 0.0,
        v: 1.0,
    },
    Vertex {
        x: 0.5,
        y: -0.5,
        z: 0.5,
        u: 0.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: -0.5,
        z: -0.5,
        u: 1.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: 0.5,
        z: -0.5,
        u: 1.0,
        v: 1.0,
    },
    Vertex {
        x: 0.5,
        y: 0.5,
        z: 0.5,
        u: 0.0,
        v: 1.0,
    },
    Vertex {
        x: -0.5,
        y: 0.5,
        z: 0.5,
        u: 0.0,
        v: 0.0,
    },
    Vertex {
        x: -0.5,
        y: 0.5,
        z: -0.5,
        u: 0.0,
        v: 1.0,
    },
    Vertex {
        x: 0.5,
        y: 0.5,
        z: 0.5,
        u: 1.0,
        v: 0.0,
    },
    Vertex {
        x: 0.5,
        y: 0.5,
        z: -0.5,
        u: 1.0,
        v: 1.0,
    },
];

const CUBE_INDICES: &[u16] = &[
    0, 1, 2, 0, 2, 3, // front
    4, 5, 6, 4, 6, 7, // back
    8, 9, 10, 10, 11, 8, // left
    12, 13, 14, 14, 15, 12, // right
    16, 17, 18, 18, 19, 17, // top
        // not bothering with bottom since it's not visible
];

const WINDOW_SIZE: u32 = 800;

pub fn main() -> Result<(), Box<dyn std::error::Error>> {
    let sdl_context = sdl3::init()?;
    let video_subsystem = sdl_context.video()?;
    let window = video_subsystem
        .window("rust-sdl3 demo: GPU (texture)", WINDOW_SIZE, WINDOW_SIZE)
        .position_centered()
        .build()
        .map_err(|e| e.to_string())?;

    let gpu = sdl3::gpu::Device::new(
        ShaderFormat::SPIRV | ShaderFormat::DXIL | ShaderFormat::DXBC | ShaderFormat::METALLIB,
        true,
    )?
    .with_window(&window)?;

    // Our shaders, require to be precompiled by a SPIR-V compiler beforehand
    let vert_shader = gpu
        .create_shader()
        .with_code(
            ShaderFormat::SPIRV,
            include_bytes!("shaders/cube-texture.vert.spv"),
            ShaderStage::Vertex,
        )
        .with_uniform_buffers(1)
        .with_entrypoint(c"main")
        .build()?;
    let frag_shader = gpu
        .create_shader()
        .with_code(
            ShaderFormat::SPIRV,
            include_bytes!("shaders/cube-texture.frag.spv"),
            ShaderStage::Fragment,
        )
        .with_samplers(1)
        .with_entrypoint(c"main")
        .build()?;

    // Create a pipeline, we specify that we want our target format in the swapchain
    // since we are rendering directly to the screen. However, we could specify a texture
    // buffer instead (e.g., for offscreen rendering).
    let swapchain_format = gpu.get_swapchain_texture_format(&window);
    let pipeline = gpu
        .create_graphics_pipeline()
        .with_primitive_type(PrimitiveType::TriangleList)
        .with_fragment_shader(&frag_shader)
        .with_vertex_shader(&vert_shader)
        .with_vertex_input_state(
            VertexInputState::new()
                .with_vertex_buffer_descriptions(&[VertexBufferDescription::new()
                    .with_slot(0)
                    .with_pitch(size_of::<Vertex>() as u32)
                    .with_input_rate(VertexInputRate::Vertex)
                    .with_instance_step_rate(0)])
                .with_vertex_attributes(&[
                    VertexAttribute::new()
                        .with_format(VertexElementFormat::Float3)
                        .with_location(0)
                        .with_buffer_slot(0)
                        .with_offset(0),
                    VertexAttribute::new()
                        .with_format(VertexElementFormat::Float2)
                        .with_location(1)
                        .with_buffer_slot(0)
                        .with_offset((3 * size_of::<f32>()) as u32),
                ]),
        )
        .with_rasterizer_state(
            RasterizerState::new()
                .with_fill_mode(FillMode::Fill)
                // Turn off culling so that I don't have to get my cube vertex order perfect
                .with_cull_mode(CullMode::None),
        )
        .with_depth_stencil_state(
            // Enable depth testing
            DepthStencilState::new()
                .with_enable_depth_test(true)
                .with_enable_depth_write(true)
                .with_compare_op(CompareOp::Less),
        )
        .with_target_info(
            GraphicsPipelineTargetInfo::new()
                .with_color_target_descriptions(&[
                    ColorTargetDescription::new().with_format(swapchain_format)
                ])
                .with_has_depth_stencil_target(true)
                .with_depth_stencil_format(TextureFormat::D16Unorm),
        )
        .build()?;

    // The pipeline now holds copies of our shaders, so we can release them
    drop(vert_shader);
    drop(frag_shader);

    // Next, we create a transfer buffer that is large enough to hold either
    // our vertices or indices since we will be transferring both with it.
    let vertices_len_bytes = std::mem::size_of_val(CUBE_VERTICES);
    let indices_len_bytes = std::mem::size_of_val(CUBE_INDICES);
    let transfer_buffer = gpu
        .create_transfer_buffer()
        .with_size(vertices_len_bytes.max(indices_len_bytes) as u32)
        .with_usage(TransferBufferUsage::UPLOAD)
        .build()?;

    // We need to start a copy pass in order to transfer data to the GPU
    let copy_commands = gpu.acquire_command_buffer()?;
    let copy_pass = gpu.begin_copy_pass(&copy_commands)?;

    // Create GPU buffers to hold our vertices and indices and transfer data to them
    let vertex_buffer = create_buffer_with_data(
        &gpu,
        &transfer_buffer,
        &copy_pass,
        BufferUsageFlags::VERTEX,
        CUBE_VERTICES,
    )?;
    let index_buffer = create_buffer_with_data(
        &gpu,
        &transfer_buffer,
        &copy_pass,
        BufferUsageFlags::INDEX,
        CUBE_INDICES,
    )?;

    // We're done with the transfer buffer now, so release it.
    drop(transfer_buffer);

    // Load up a texture to put on the cube
    let cube_texture = create_texture_from_image(&gpu, "./assets/texture.bmp", &copy_pass)?;

    // And configure a sampler for pulling pixels from that texture in the frag shader
    let cube_texture_sampler = gpu.create_sampler(
        SamplerCreateInfo::new()
            .with_min_filter(Filter::Nearest)
            .with_mag_filter(Filter::Nearest)
            .with_mipmap_mode(SamplerMipmapMode::Nearest)
            .with_address_mode_u(SamplerAddressMode::Repeat)
            .with_address_mode_v(SamplerAddressMode::Repeat)
            .with_address_mode_w(SamplerAddressMode::Repeat),
    )?;

    // Now complete and submit the copy pass commands to actually do the transfer work
    gpu.end_copy_pass(copy_pass);
    copy_commands.submit()?;

    // We'll need to allocate a texture buffer for our depth buffer for depth testing to work
    let mut depth_texture = gpu.create_texture(
        TextureCreateInfo::new()
            .with_type(TextureType::_2D)
            .with_width(WINDOW_SIZE)
            .with_height(WINDOW_SIZE)
            .with_layer_count_or_depth(1)
            .with_num_levels(1)
            .with_sample_count(SampleCount::NoMultiSampling)
            .with_format(TextureFormat::D16Unorm)
            .with_usage(TextureUsage::SAMPLER | TextureUsage::DEPTH_STENCIL_TARGET),
    )?;

    let mut rotation = 45.0f32;
    let mut event_pump = sdl_context.event_pump()?;
    'running: loop {
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit { .. }
                | Event::KeyDown {
                    keycode: Some(Keycode::Escape),
                    ..
                } => break 'running,
                _ => {}
            }
        }

        // The swapchain texture is basically the framebuffer corresponding to the drawable
        // area of a given window - note how we "wait" for it to come up
        //
        // This is because a swapchain needs to be "allocated", and it can quickly run out
        // if we don't properly time the rendering process.
        let mut command_buffer = gpu.acquire_command_buffer()?;
        if let Ok(swapchain) = command_buffer.wait_and_acquire_swapchain_texture(&window) {
            // Again, like in gpu-clear.rs, we'd want to define basic operations for our cube
            let color_targets = [ColorTargetInfo::default()
                .with_texture(&swapchain)
                .with_load_op(LoadOp::CLEAR)
                .with_store_op(StoreOp::STORE)
                .with_clear_color(Color::RGB(128, 128, 128))];
            // This time, however, we want depth testing, so we need to also target a depth texture buffer
            let depth_target = DepthStencilTargetInfo::new()
                .with_texture(&mut depth_texture)
                .with_cycle(true)
                .with_clear_depth(1.0)
                .with_clear_stencil(0)
                .with_load_op(LoadOp::CLEAR)
                .with_store_op(StoreOp::STORE)
                .with_stencil_load_op(LoadOp::CLEAR)
                .with_stencil_store_op(StoreOp::STORE);
            let render_pass =
                gpu.begin_render_pass(&command_buffer, &color_targets, Some(&depth_target))?;

            // Screen is cleared below due to the color target info
            render_pass.bind_graphics_pipeline(&pipeline);

            // Now we'll bind our buffers/sampler and draw the cube
            render_pass.bind_vertex_buffers(
                0,
                &[BufferBinding::new()
                    .with_buffer(&vertex_buffer)
                    .with_offset(0)],
            );
            render_pass.bind_index_buffer(
                &BufferBinding::new()
                    .with_buffer(&index_buffer)
                    .with_offset(0),
                IndexElementSize::_16BIT,
            );
            render_pass.bind_fragment_samplers(
                0,
                &[TextureSamplerBinding::new()
                    .with_texture(&cube_texture)
                    .with_sampler(&cube_texture_sampler)],
            );

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

            // Finally, draw the cube
            render_pass.draw_indexed_primitives(CUBE_INDICES.len() as u32, 1, 0, 0, 0);

            gpu.end_render_pass(render_pass);
            command_buffer.submit()?;
        } else {
            // Swapchain unavailable, cancel work
            command_buffer.cancel();
        }
    }

    Ok(())
}

fn create_texture_from_image(
    gpu: &Device,
    image_path: impl AsRef<Path>,
    copy_pass: &CopyPass,
) -> Result<Texture<'static>, Error> {
    let image = Surface::load_bmp(image_path.as_ref())?;
    let image_size = image.size();
    let size_bytes = image.pixel_format().bytes_per_pixel() as u32 * image_size.0 * image_size.1;

    let texture = gpu.create_texture(
        TextureCreateInfo::new()
            .with_format(TextureFormat::R8g8b8a8Unorm)
            .with_type(TextureType::_2D)
            .with_width(image_size.0)
            .with_height(image_size.1)
            .with_layer_count_or_depth(1)
            .with_num_levels(1)
            .with_usage(TextureUsage::SAMPLER),
    )?;

    let transfer_buffer = gpu
        .create_transfer_buffer()
        .with_size(size_bytes)
        .with_usage(TransferBufferUsage::UPLOAD)
        .build()?;

    let mut buffer_mem = transfer_buffer.map::<u8>(gpu, false);
    image.with_lock(|image_bytes| {
        buffer_mem.mem_mut().copy_from_slice(image_bytes);
    });
    buffer_mem.unmap();

    copy_pass.upload_to_gpu_texture(
        TextureTransferInfo::new()
            .with_transfer_buffer(&transfer_buffer)
            .with_offset(0),
        TextureRegion::new()
            .with_texture(&texture)
            .with_layer(0)
            .with_width(image_size.0)
            .with_height(image_size.1)
            .with_depth(1),
        false,
    );

    Ok(texture)
}

/// Creates a GPU buffer and uploads data to it using the given `copy_pass` and `transfer_buffer`.
fn create_buffer_with_data<T: Copy>(
    gpu: &Device,
    transfer_buffer: &TransferBuffer,
    copy_pass: &CopyPass,
    usage: BufferUsageFlags,
    data: &[T],
) -> Result<Buffer, Error> {
    // Figure out the length of the data in bytes
    let len_bytes = std::mem::size_of_val(data);

    // Create the buffer with the size and usage we want
    let buffer = gpu
        .create_buffer()
        .with_size(len_bytes as u32)
        .with_usage(usage)
        .build()?;

    // Map the transfer buffer's memory into a place we can copy into, and copy the data
    //
    // Note: We set `cycle` to true since we're reusing the same transfer buffer to
    // initialize both the vertex and index buffer. This makes SDL synchronize the transfers
    // so that one doesn't interfere with the other.
    let mut map = transfer_buffer.map::<T>(gpu, true);
    let mem = map.mem_mut();
    for (index, &value) in data.iter().enumerate() {
        mem[index] = value;
    }

    // Now unmap the memory since we're done copying
    map.unmap();

    // Finally, add a command to the copy pass to upload this data to the GPU
    //
    // Note: We also set `cycle` to true here for the same reason.
    copy_pass.upload_to_gpu_buffer(
        TransferBufferLocation::new()
            .with_offset(0)
            .with_transfer_buffer(transfer_buffer),
        BufferRegion::new()
            .with_offset(0)
            .with_size(len_bytes as u32)
            .with_buffer(&buffer),
        true,
    );

    Ok(buffer)
}