Skip to content

Commit b9ce824

Browse files
quaternicflorianvazelle
authored andcommitted
replace unsound gpu enums with wrapper structs
1 parent 756cde0 commit b9ce824

File tree

11 files changed

+106
-151
lines changed

11 files changed

+106
-151
lines changed

examples/gpu-clear.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
1717
// by default, and we specify that our shaders will be SPIR-V ones (even through we
1818
// aren't using any shaders)
1919
// We'll also turn on debug mode to true, so we get debug stuff
20-
let gpu = sdl3::gpu::Device::new(sdl3::gpu::ShaderFormat::SpirV, true)?.with_window(&window)?;
20+
let gpu = sdl3::gpu::Device::new(sdl3::gpu::ShaderFormat::SPIRV, true)?.with_window(&window)?;
2121

2222
let mut event_pump = sdl_context.event_pump()?;
2323
println!(
@@ -45,8 +45,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4545
let color_targets = [
4646
sdl3::gpu::ColorTargetInfo::default()
4747
.with_texture(&swapchain) // Use swapchain texture
48-
.with_load_op(sdl3::gpu::LoadOp::Clear) // Clear when load
49-
.with_store_op(sdl3::gpu::StoreOp::Store) // Store back
48+
.with_load_op(sdl3::gpu::LoadOp::CLEAR) // Clear when load
49+
.with_store_op(sdl3::gpu::StoreOp::STORE) // Store back
5050
.with_clear_color(sdl3::pixels::Color::RGB(5, 3, 255)), //blue with small RG bias
5151
];
5252
// Here we do all (none) of our drawing (clearing the screen)

examples/gpu-cube.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
8989
.map_err(|e| e.to_string())?;
9090

9191
let gpu = Device::new(
92-
ShaderFormat::SpirV | ShaderFormat::Dxil | ShaderFormat::Dxbc | ShaderFormat::MetalLib,
92+
ShaderFormat::SPIRV | ShaderFormat::DXIL | ShaderFormat::DXBC | ShaderFormat::METALLIB,
9393
true,
9494
)?
9595
.with_window(&window)?;
@@ -98,7 +98,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
9898
let vert_shader = gpu
9999
.create_shader()
100100
.with_code(
101-
ShaderFormat::SpirV,
101+
ShaderFormat::SPIRV,
102102
include_bytes!("shaders/cube.vert.spv"),
103103
ShaderStage::Vertex,
104104
)
@@ -108,7 +108,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
108108
let frag_shader = gpu
109109
.create_shader()
110110
.with_code(
111-
ShaderFormat::SpirV,
111+
ShaderFormat::SPIRV,
112112
include_bytes!("shaders/cube.frag.spv"),
113113
ShaderStage::Fragment,
114114
)
@@ -171,7 +171,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
171171
let transfer_buffer = gpu
172172
.create_transfer_buffer()
173173
.with_size(vertices_len_bytes.max(indices_len_bytes) as u32)
174-
.with_usage(TransferBufferUsage::Upload)
174+
.with_usage(TransferBufferUsage::UPLOAD)
175175
.build()?;
176176

177177
// We need to start a copy pass in order to transfer data to the GPU
@@ -183,14 +183,14 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
183183
&gpu,
184184
&transfer_buffer,
185185
&copy_pass,
186-
BufferUsageFlags::Vertex,
186+
BufferUsageFlags::VERTEX,
187187
&CUBE_VERTICES,
188188
)?;
189189
let index_buffer = create_buffer_with_data(
190190
&gpu,
191191
&transfer_buffer,
192192
&copy_pass,
193-
BufferUsageFlags::Index,
193+
BufferUsageFlags::INDEX,
194194
&CUBE_INDICES,
195195
)?;
196196

@@ -211,7 +211,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
211211
.with_num_levels(1)
212212
.with_sample_count(SampleCount::NoMultiSampling)
213213
.with_format(TextureFormat::D16Unorm)
214-
.with_usage(TextureUsage::Sampler | TextureUsage::DepthStencilTarget),
214+
.with_usage(TextureUsage::SAMPLER | TextureUsage::DEPTH_STENCIL_TARGET),
215215
)?;
216216

217217
let mut rotation = 45.0f32;
@@ -238,19 +238,19 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
238238
// Again, like in gpu-clear.rs, we'd want to define basic operations for our cube
239239
let color_targets = [ColorTargetInfo::default()
240240
.with_texture(&swapchain)
241-
.with_load_op(LoadOp::Clear)
242-
.with_store_op(StoreOp::Store)
241+
.with_load_op(LoadOp::CLEAR)
242+
.with_store_op(StoreOp::STORE)
243243
.with_clear_color(Color::RGB(128, 128, 128))];
244244
// This time, however, we want depth testing, so we need to also target a depth texture buffer
245245
let depth_target = DepthStencilTargetInfo::new()
246246
.with_texture(&mut depth_texture)
247247
.with_cycle(true)
248248
.with_clear_depth(1.0)
249249
.with_clear_stencil(0)
250-
.with_load_op(LoadOp::Clear)
251-
.with_store_op(StoreOp::Store)
252-
.with_stencil_load_op(LoadOp::Clear)
253-
.with_stencil_store_op(StoreOp::Store);
250+
.with_load_op(LoadOp::CLEAR)
251+
.with_store_op(StoreOp::STORE)
252+
.with_stencil_load_op(LoadOp::CLEAR)
253+
.with_stencil_store_op(StoreOp::STORE);
254254
let render_pass =
255255
gpu.begin_render_pass(&command_buffer, &color_targets, Some(&depth_target))?;
256256

@@ -268,7 +268,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
268268
&BufferBinding::new()
269269
.with_buffer(&index_buffer)
270270
.with_offset(0),
271-
IndexElementSize::_16Bit,
271+
IndexElementSize::_16BIT,
272272
);
273273

274274
// Set the rotation uniform for our cube vert shader

examples/gpu-texture.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
194194
.map_err(|e| e.to_string())?;
195195

196196
let gpu = sdl3::gpu::Device::new(
197-
ShaderFormat::SpirV | ShaderFormat::Dxil | ShaderFormat::Dxbc | ShaderFormat::MetalLib,
197+
ShaderFormat::SPIRV | ShaderFormat::DXIL | ShaderFormat::DXBC | ShaderFormat::METALLIB,
198198
true,
199199
)?
200200
.with_window(&window)?;
@@ -203,7 +203,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
203203
let vert_shader = gpu
204204
.create_shader()
205205
.with_code(
206-
ShaderFormat::SpirV,
206+
ShaderFormat::SPIRV,
207207
include_bytes!("shaders/cube-texture.vert.spv"),
208208
ShaderStage::Vertex,
209209
)
@@ -213,7 +213,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
213213
let frag_shader = gpu
214214
.create_shader()
215215
.with_code(
216-
ShaderFormat::SpirV,
216+
ShaderFormat::SPIRV,
217217
include_bytes!("shaders/cube-texture.frag.spv"),
218218
ShaderStage::Fragment,
219219
)
@@ -284,7 +284,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
284284
let transfer_buffer = gpu
285285
.create_transfer_buffer()
286286
.with_size(vertices_len_bytes.max(indices_len_bytes) as u32)
287-
.with_usage(TransferBufferUsage::Upload)
287+
.with_usage(TransferBufferUsage::UPLOAD)
288288
.build()?;
289289

290290
// We need to start a copy pass in order to transfer data to the GPU
@@ -296,14 +296,14 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
296296
&gpu,
297297
&transfer_buffer,
298298
&copy_pass,
299-
BufferUsageFlags::Vertex,
299+
BufferUsageFlags::VERTEX,
300300
&CUBE_VERTICES,
301301
)?;
302302
let index_buffer = create_buffer_with_data(
303303
&gpu,
304304
&transfer_buffer,
305305
&copy_pass,
306-
BufferUsageFlags::Index,
306+
BufferUsageFlags::INDEX,
307307
&CUBE_INDICES,
308308
)?;
309309

@@ -338,7 +338,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
338338
.with_num_levels(1)
339339
.with_sample_count(SampleCount::NoMultiSampling)
340340
.with_format(TextureFormat::D16Unorm)
341-
.with_usage(TextureUsage::Sampler | TextureUsage::DepthStencilTarget),
341+
.with_usage(TextureUsage::SAMPLER | TextureUsage::DEPTH_STENCIL_TARGET),
342342
)?;
343343

344344
let mut rotation = 45.0f32;
@@ -365,19 +365,19 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
365365
// Again, like in gpu-clear.rs, we'd want to define basic operations for our cube
366366
let color_targets = [ColorTargetInfo::default()
367367
.with_texture(&swapchain)
368-
.with_load_op(LoadOp::Clear)
369-
.with_store_op(StoreOp::Store)
368+
.with_load_op(LoadOp::CLEAR)
369+
.with_store_op(StoreOp::STORE)
370370
.with_clear_color(Color::RGB(128, 128, 128))];
371371
// This time, however, we want depth testing, so we need to also target a depth texture buffer
372372
let depth_target = DepthStencilTargetInfo::new()
373373
.with_texture(&mut depth_texture)
374374
.with_cycle(true)
375375
.with_clear_depth(1.0)
376376
.with_clear_stencil(0)
377-
.with_load_op(LoadOp::Clear)
378-
.with_store_op(StoreOp::Store)
379-
.with_stencil_load_op(LoadOp::Clear)
380-
.with_stencil_store_op(StoreOp::Store);
377+
.with_load_op(LoadOp::CLEAR)
378+
.with_store_op(StoreOp::STORE)
379+
.with_stencil_load_op(LoadOp::CLEAR)
380+
.with_stencil_store_op(StoreOp::STORE);
381381
let render_pass =
382382
gpu.begin_render_pass(&command_buffer, &color_targets, Some(&depth_target))?;
383383

@@ -395,7 +395,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
395395
&BufferBinding::new()
396396
.with_buffer(&index_buffer)
397397
.with_offset(0),
398-
IndexElementSize::_16Bit,
398+
IndexElementSize::_16BIT,
399399
);
400400
render_pass.bind_fragment_samplers(
401401
0,
@@ -440,13 +440,13 @@ fn create_texture_from_image(
440440
.with_height(image_size.1)
441441
.with_layer_count_or_depth(1)
442442
.with_num_levels(1)
443-
.with_usage(TextureUsage::Sampler),
443+
.with_usage(TextureUsage::SAMPLER),
444444
)?;
445445

446446
let transfer_buffer = gpu
447447
.create_transfer_buffer()
448448
.with_size(size_bytes)
449-
.with_usage(TransferBufferUsage::Upload)
449+
.with_usage(TransferBufferUsage::UPLOAD)
450450
.build()?;
451451

452452
let mut buffer_mem = transfer_buffer.map::<u8>(gpu, false);

examples/gpu-triangle.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
2525
// aren't using any shaders)
2626
// We'll also turn on debug mode to true, so we get debug stuff
2727
let gpu = Device::new(
28-
ShaderFormat::SpirV | ShaderFormat::Dxil | ShaderFormat::Dxbc | ShaderFormat::MetalLib,
28+
ShaderFormat::SPIRV | ShaderFormat::DXIL | ShaderFormat::DXBC | ShaderFormat::METALLIB,
2929
true,
3030
)?
3131
.with_window(&window)?;
@@ -36,13 +36,13 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
3636
// Our shaders, require to be precompiled by a SPIR-V compiler beforehand
3737
let vs_shader = gpu
3838
.create_shader()
39-
.with_code(ShaderFormat::SpirV, vs_source, ShaderStage::Vertex)
39+
.with_code(ShaderFormat::SPIRV, vs_source, ShaderStage::Vertex)
4040
.with_entrypoint(c"main")
4141
.build()?;
4242

4343
let fs_shader = gpu
4444
.create_shader()
45-
.with_code(ShaderFormat::SpirV, fs_source, ShaderStage::Fragment)
45+
.with_code(ShaderFormat::SPIRV, fs_source, ShaderStage::Fragment)
4646
.with_entrypoint(c"main")
4747
.build()?;
4848

@@ -95,8 +95,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
9595
let color_targets = [
9696
ColorTargetInfo::default()
9797
.with_texture(&swapchain)
98-
.with_load_op(LoadOp::Clear)
99-
.with_store_op(StoreOp::Store)
98+
.with_load_op(LoadOp::CLEAR)
99+
.with_store_op(StoreOp::STORE)
100100
.with_clear_color(Color::RGB(5, 3, 255)), //blue with small RG bias
101101
];
102102
let render_pass = gpu.begin_render_pass(&command_buffer, &color_targets, None)?;

src/sdl3/gpu/buffer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::sync::Arc;
77
use sys::gpu::{
88
SDL_CreateGPUBuffer, SDL_CreateGPUTransferBuffer, SDL_GPUBuffer, SDL_GPUBufferBinding,
99
SDL_GPUBufferCreateInfo, SDL_GPUBufferRegion, SDL_GPUTransferBuffer,
10-
SDL_GPUTransferBufferCreateInfo, SDL_GPUTransferBufferLocation, SDL_GPUTransferBufferUsage,
11-
SDL_GPUVertexBufferDescription, SDL_GPUVertexInputRate, SDL_MapGPUTransferBuffer,
12-
SDL_ReleaseGPUBuffer, SDL_ReleaseGPUTransferBuffer, SDL_UnmapGPUTransferBuffer,
10+
SDL_GPUTransferBufferCreateInfo, SDL_GPUTransferBufferLocation, SDL_GPUVertexBufferDescription,
11+
SDL_GPUVertexInputRate, SDL_MapGPUTransferBuffer, SDL_ReleaseGPUBuffer,
12+
SDL_ReleaseGPUTransferBuffer, SDL_UnmapGPUTransferBuffer,
1313
};
1414

1515
#[repr(C)]
@@ -157,7 +157,7 @@ impl<'a> BufferBuilder<'a> {
157157
}
158158

159159
pub fn with_usage(mut self, value: BufferUsageFlags) -> Self {
160-
self.inner.usage = value as u32;
160+
self.inner.usage = value.0;
161161
self
162162
}
163163

@@ -267,7 +267,7 @@ impl<'a> TransferBufferBuilder<'a> {
267267

268268
/// How the buffer will be used.
269269
pub fn with_usage(mut self, value: TransferBufferUsage) -> Self {
270-
self.inner.usage = SDL_GPUTransferBufferUsage(value as i32);
270+
self.inner.usage = value;
271271
self
272272
}
273273

src/sdl3/gpu/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Device {
5555

5656
#[doc(alias = "SDL_CreateGPUDevice")]
5757
pub fn new(flags: ShaderFormat, debug_mode: bool) -> Result<Self, Error> {
58-
let raw_device = unsafe { SDL_CreateGPUDevice(flags as u32, debug_mode, std::ptr::null()) };
58+
let raw_device = unsafe { SDL_CreateGPUDevice(flags.0, debug_mode, std::ptr::null()) };
5959
if raw_device.is_null() {
6060
Err(get_error())
6161
} else {

0 commit comments

Comments
 (0)