Style, formatting, and consistency fixes

This commit is contained in:
2025-06-13 15:32:02 +10:00
parent 44a606df5b
commit a0097e1e1c
17 changed files with 206 additions and 224 deletions

View File

@@ -126,7 +126,7 @@ static bool Lesson10_Init(NeHeContext* ctx)
.offset = offsetof(Vertex, u) .offset = offsetof(Vertex, u)
} }
}; };
SDL_GPUGraphicsPipelineCreateInfo info = SDL_GPUGraphicsPipelineCreateInfo psoInfo =
{ {
.vertex_shader = vertexShader, .vertex_shader = vertexShader,
.fragment_shader = fragmentShader, .fragment_shader = fragmentShader,
@@ -157,7 +157,7 @@ static bool Lesson10_Init(NeHeContext* ctx)
// Setup blend pipeline // Setup blend pipeline
const SDL_GPUTextureFormat swapchainTextureFormat = SDL_GetGPUSwapchainTextureFormat(ctx->device, ctx->window); const SDL_GPUTextureFormat swapchainTextureFormat = SDL_GetGPUSwapchainTextureFormat(ctx->device, ctx->window);
info.target_info.color_target_descriptions = &(const SDL_GPUColorTargetDescription) psoInfo.target_info.color_target_descriptions = &(const SDL_GPUColorTargetDescription)
{ {
.format = swapchainTextureFormat, .format = swapchainTextureFormat,
.blend_state = .blend_state =
@@ -171,7 +171,7 @@ static bool Lesson10_Init(NeHeContext* ctx)
.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE .dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE
} }
}; };
psoBlend = SDL_CreateGPUGraphicsPipeline(ctx->device, &info); psoBlend = SDL_CreateGPUGraphicsPipeline(ctx->device, &psoInfo);
if (!psoBlend) if (!psoBlend)
{ {
SDL_ReleaseGPUShader(ctx->device, fragmentShader); SDL_ReleaseGPUShader(ctx->device, fragmentShader);
@@ -181,19 +181,19 @@ static bool Lesson10_Init(NeHeContext* ctx)
} }
// Setup regular pipeline w/ depth testing // Setup regular pipeline w/ depth testing
info.depth_stencil_state = (SDL_GPUDepthStencilState) psoInfo.depth_stencil_state = (SDL_GPUDepthStencilState)
{ {
.compare_op = SDL_GPU_COMPAREOP_LESS, // Pass if pixel depth value tests less than the depth buffer value .compare_op = SDL_GPU_COMPAREOP_LESS, // Pass if pixel depth value tests less than the depth buffer value
.enable_depth_test = true, // Enable depth testing .enable_depth_test = true, // Enable depth testing
.enable_depth_write = true .enable_depth_write = true
}; };
info.target_info.color_target_descriptions = &(const SDL_GPUColorTargetDescription) psoInfo.target_info.color_target_descriptions = &(const SDL_GPUColorTargetDescription)
{ {
.format = swapchainTextureFormat .format = swapchainTextureFormat
}; };
info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM; psoInfo.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
info.target_info.has_depth_stencil_target = true; psoInfo.target_info.has_depth_stencil_target = true;
pso = SDL_CreateGPUGraphicsPipeline(ctx->device, &info); pso = SDL_CreateGPUGraphicsPipeline(ctx->device, &psoInfo);
SDL_ReleaseGPUShader(ctx->device, fragmentShader); SDL_ReleaseGPUShader(ctx->device, fragmentShader);
SDL_ReleaseGPUShader(ctx->device, vertexShader); SDL_ReleaseGPUShader(ctx->device, vertexShader);
if (!pso) if (!pso)

View File

@@ -50,12 +50,12 @@ static const Vertex vertices[] =
static const uint16_t indices[] = static const uint16_t indices[] =
{ {
0, 1, 2, 2, 3, 0, // Front 0, 1, 2, 2, 3, 0, // Front
4, 5, 6, 6, 7, 4, // Back 4, 5, 6, 6, 7, 4, // Back
8, 9, 10, 10, 11, 8, // Top 8, 9, 10, 10, 11, 8, // Top
12, 13, 14, 14, 15, 12, // Bottom 12, 13, 14, 14, 15, 12, // Bottom
16, 17, 18, 18, 19, 16, // Right 16, 17, 18, 18, 19, 16, // Right
20, 21, 22, 22, 23, 20 // Left 20, 21, 22, 22, 23, 20 // Left
}; };

View File

@@ -95,32 +95,32 @@ impl AppImplementation for Lesson2
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.vertex_shader = vertex_shader; pso_info.vertex_shader = vertex_shader;
info.fragment_shader = fragment_shader; pso_info.fragment_shader = fragment_shader;
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
let colour_targets: &[SDL_GPUColorTargetDescription] = let color_targets =
&[ [
SDL_GPUColorTargetDescription SDL_GPUColorTargetDescription
{ {
format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }, format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) },
blend_state: SDL_GPUColorTargetBlendState::default(), blend_state: SDL_GPUColorTargetBlendState::default(),
} }
]; ];
info.target_info.color_target_descriptions = colour_targets.as_ptr(); pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.num_color_targets = colour_targets.len() as u32; pso_info.target_info.num_color_targets = color_targets.len() as u32;
self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader); SDL_ReleaseGPUShader(ctx.device, fragment_shader);

View File

@@ -105,32 +105,32 @@ impl AppImplementation for Lesson3
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.vertex_shader = vertex_shader; pso_info.vertex_shader = vertex_shader;
info.fragment_shader = fragment_shader; pso_info.fragment_shader = fragment_shader;
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
let colour_targets: &[SDL_GPUColorTargetDescription] = let color_targets =
&[ [
SDL_GPUColorTargetDescription SDL_GPUColorTargetDescription
{ {
format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }, format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) },
blend_state: SDL_GPUColorTargetBlendState::default(), blend_state: SDL_GPUColorTargetBlendState::default(),
} }
]; ];
info.target_info.color_target_descriptions = colour_targets.as_ptr(); pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.num_color_targets = colour_targets.len() as u32; pso_info.target_info.num_color_targets = color_targets.len() as u32;
self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader); SDL_ReleaseGPUShader(ctx.device, fragment_shader);

View File

@@ -111,32 +111,32 @@ impl AppImplementation for Lesson4
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.vertex_shader = vertex_shader; pso_info.vertex_shader = vertex_shader;
info.fragment_shader = fragment_shader; pso_info.fragment_shader = fragment_shader;
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
let colour_targets: &[SDL_GPUColorTargetDescription] = let color_targets =
&[ [
SDL_GPUColorTargetDescription SDL_GPUColorTargetDescription
{ {
format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }, format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) },
blend_state: SDL_GPUColorTargetBlendState::default(), blend_state: SDL_GPUColorTargetBlendState::default(),
} }
]; ];
info.target_info.color_target_descriptions = colour_targets.as_ptr(); pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.num_color_targets = colour_targets.len() as u32; pso_info.target_info.num_color_targets = color_targets.len() as u32;
self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader); SDL_ReleaseGPUShader(ctx.device, fragment_shader);

View File

@@ -142,38 +142,38 @@ impl AppImplementation for Lesson5
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.vertex_shader = vertex_shader; pso_info.vertex_shader = vertex_shader;
info.fragment_shader = fragment_shader; pso_info.fragment_shader = fragment_shader;
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
let colour_targets: &[SDL_GPUColorTargetDescription] = let color_targets =
&[ [
SDL_GPUColorTargetDescription SDL_GPUColorTargetDescription
{ {
format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }, format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) },
blend_state: SDL_GPUColorTargetBlendState::default(), blend_state: SDL_GPUColorTargetBlendState::default(),
} }
]; ];
info.target_info.color_target_descriptions = colour_targets.as_ptr(); pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.num_color_targets = colour_targets.len() as u32; pso_info.target_info.num_color_targets = color_targets.len() as u32;
info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM; pso_info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
info.target_info.has_depth_stencil_target = true; pso_info.target_info.has_depth_stencil_target = true;
info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL; pso_info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
info.depth_stencil_state.enable_depth_test = true; pso_info.depth_stencil_state.enable_depth_test = true;
info.depth_stencil_state.enable_depth_write = true; pso_info.depth_stencil_state.enable_depth_write = true;
self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader); SDL_ReleaseGPUShader(ctx.device, fragment_shader);

View File

@@ -137,38 +137,38 @@ impl AppImplementation for Lesson6
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.vertex_shader = vertex_shader; pso_info.vertex_shader = vertex_shader;
info.fragment_shader = fragment_shader; pso_info.fragment_shader = fragment_shader;
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
let colour_targets: &[SDL_GPUColorTargetDescription] = let color_targets =
&[ [
SDL_GPUColorTargetDescription SDL_GPUColorTargetDescription
{ {
format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }, format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) },
blend_state: SDL_GPUColorTargetBlendState::default(), blend_state: SDL_GPUColorTargetBlendState::default(),
} }
]; ];
info.target_info.color_target_descriptions = colour_targets.as_ptr(); pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.num_color_targets = colour_targets.len() as u32; pso_info.target_info.num_color_targets = color_targets.len() as u32;
info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM; pso_info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
info.target_info.has_depth_stencil_target = true; pso_info.target_info.has_depth_stencil_target = true;
info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL; pso_info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
info.depth_stencil_state.enable_depth_test = true; pso_info.depth_stencil_state.enable_depth_test = true;
info.depth_stencil_state.enable_depth_write = true; pso_info.depth_stencil_state.enable_depth_write = true;
self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader); SDL_ReleaseGPUShader(ctx.device, fragment_shader);

View File

@@ -175,39 +175,39 @@ impl AppImplementation for Lesson7
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
let colour_targets: &[SDL_GPUColorTargetDescription] = let color_targets =
&[ [
SDL_GPUColorTargetDescription SDL_GPUColorTargetDescription
{ {
format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }, format: unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) },
blend_state: SDL_GPUColorTargetBlendState::default(), blend_state: SDL_GPUColorTargetBlendState::default(),
} }
]; ];
info.target_info.color_target_descriptions = colour_targets.as_ptr(); pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.num_color_targets = colour_targets.len() as u32; pso_info.target_info.num_color_targets = color_targets.len() as u32;
info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM; pso_info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
info.target_info.has_depth_stencil_target = true; pso_info.target_info.has_depth_stencil_target = true;
info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL; pso_info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
info.depth_stencil_state.enable_depth_test = true; pso_info.depth_stencil_state.enable_depth_test = true;
info.depth_stencil_state.enable_depth_write = true; pso_info.depth_stencil_state.enable_depth_write = true;
// Create unlit pipeline // Create unlit pipeline
info.vertex_shader = vertex_shader_unlit; pso_info.vertex_shader = vertex_shader_unlit;
info.fragment_shader = fragment_shader_unlit; pso_info.fragment_shader = fragment_shader_unlit;
self.pso_unlit = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso_unlit = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader_unlit); SDL_ReleaseGPUShader(ctx.device, fragment_shader_unlit);
@@ -225,9 +225,9 @@ impl AppImplementation for Lesson7
} }
// Create lit pipeline // Create lit pipeline
info.vertex_shader = vertex_shader_light; pso_info.vertex_shader = vertex_shader_light;
info.fragment_shader = fragment_shader_light; pso_info.fragment_shader = fragment_shader_light;
self.pso_light = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info) }; self.pso_light = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info) };
unsafe unsafe
{ {
SDL_ReleaseGPUShader(ctx.device, fragment_shader_light); SDL_ReleaseGPUShader(ctx.device, fragment_shader_light);

View File

@@ -181,67 +181,66 @@ impl AppImplementation for Lesson8
}, },
]; ];
let mut info = SDL_GPUGraphicsPipelineCreateInfo::default(); let mut pso_info = SDL_GPUGraphicsPipelineCreateInfo::default();
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; pso_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
info.vertex_input_state = SDL_GPUVertexInputState pso_info.vertex_input_state = SDL_GPUVertexInputState
{ {
vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(), vertex_buffer_descriptions: VERTEX_DESCRIPTIONS.as_ptr(),
num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32, num_vertex_buffers: VERTEX_DESCRIPTIONS.len() as u32,
vertex_attributes: VERTEX_ATTRIBS.as_ptr(), vertex_attributes: VERTEX_ATTRIBS.as_ptr(),
num_vertex_attributes: VERTEX_ATTRIBS.len() as u32, num_vertex_attributes: VERTEX_ATTRIBS.len() as u32,
}; };
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL; pso_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE; pso_info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE;
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; pso_info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE;
// Common pipeline depth & colour target options // Common pipeline depth & colour target options
let mut color_targets = [ SDL_GPUColorTargetDescription::default() ];
let mut color_target = [ SDL_GPUColorTargetDescription::default() ]; color_targets[0].format = unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) };
color_target[0].format = unsafe { SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window) }; pso_info.target_info.color_target_descriptions = color_targets.as_ptr();
info.target_info.color_target_descriptions = color_target.as_ptr(); pso_info.target_info.num_color_targets = 1;
info.target_info.num_color_targets = 1; pso_info.target_info.depth_stencil_format = Self::CREATE_DEPTH_BUFFER;
info.target_info.depth_stencil_format = Self::CREATE_DEPTH_BUFFER; pso_info.target_info.has_depth_stencil_target = true;
info.target_info.has_depth_stencil_target = true; pso_info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
// Setup depth/stencil & colour pipeline state for no blending // Setup depth/stencil & colour pipeline state for no blending
info.depth_stencil_state.enable_depth_test = true; pso_info.depth_stencil_state.enable_depth_test = true;
info.depth_stencil_state.enable_depth_write = true; pso_info.depth_stencil_state.enable_depth_write = true;
color_target[0].blend_state = unsafe { std::mem::zeroed() }; color_targets[0].blend_state = unsafe { std::mem::zeroed() };
// Create unlit pipeline // Create unlit pipeline
info.vertex_shader = vertex_shader_unlit; pso_info.vertex_shader = vertex_shader_unlit;
info.fragment_shader = fragment_shader_unlit; pso_info.fragment_shader = fragment_shader_unlit;
self.pso_unlit = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info).as_mut() } self.pso_unlit = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info).as_mut() }
.ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?; .ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?;
// Create lit pipeline // Create lit pipeline
info.vertex_shader = vertex_shader_light; pso_info.vertex_shader = vertex_shader_light;
info.fragment_shader = fragment_shader_light; pso_info.fragment_shader = fragment_shader_light;
self.pso_light = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info).as_mut() } self.pso_light = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info).as_mut() }
.ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?; .ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?;
// Setup depth/stencil & colour pipeline state for blending // Setup depth/stencil & colour pipeline state for blending
info.depth_stencil_state.enable_depth_test = false; pso_info.depth_stencil_state.enable_depth_test = false;
info.depth_stencil_state.enable_depth_write = false; pso_info.depth_stencil_state.enable_depth_write = false;
color_target[0].blend_state.enable_blend = true; color_targets[0].blend_state.enable_blend = true;
color_target[0].blend_state.color_blend_op = SDL_GPU_BLENDOP_ADD; color_targets[0].blend_state.color_blend_op = SDL_GPU_BLENDOP_ADD;
color_target[0].blend_state.alpha_blend_op = SDL_GPU_BLENDOP_ADD; color_targets[0].blend_state.alpha_blend_op = SDL_GPU_BLENDOP_ADD;
color_target[0].blend_state.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA; color_targets[0].blend_state.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA;
color_target[0].blend_state.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE; color_targets[0].blend_state.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE;
color_target[0].blend_state.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA; color_targets[0].blend_state.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA;
color_target[0].blend_state.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE; color_targets[0].blend_state.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE;
// Create unlit blended pipeline // Create unlit blended pipeline
info.vertex_shader = vertex_shader_unlit; pso_info.vertex_shader = vertex_shader_unlit;
info.fragment_shader = fragment_shader_unlit; pso_info.fragment_shader = fragment_shader_unlit;
self.pso_blend_unlit = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info).as_mut() } self.pso_blend_unlit = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info).as_mut() }
.ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?; .ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?;
// Create lit blended pipeline // Create lit blended pipeline
info.vertex_shader = vertex_shader_light; pso_info.vertex_shader = vertex_shader_light;
info.fragment_shader = fragment_shader_light; pso_info.fragment_shader = fragment_shader_light;
self.pso_blend_light = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &info).as_mut() } self.pso_blend_light = unsafe { SDL_CreateGPUGraphicsPipeline(ctx.device, &pso_info).as_mut() }
.ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?; .ok_or(NeHeError::sdl("SDL_CreateGPUGraphicsPipeline"))?;
// Free shaders // Free shaders

View File

@@ -17,8 +17,8 @@ struct Lesson1: AppDelegate
colorInfo.load_op = SDL_GPU_LOADOP_CLEAR colorInfo.load_op = SDL_GPU_LOADOP_CLEAR
colorInfo.store_op = SDL_GPU_STOREOP_STORE colorInfo.store_op = SDL_GPU_STOREOP_STORE
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
} }
} }

View File

@@ -23,7 +23,7 @@ struct Lesson2: AppDelegate
SIMD3<Float>( -1.0, -1.0, 0.0), // Bottom left SIMD3<Float>( -1.0, -1.0, 0.0), // Bottom left
] ]
static let indices: [Int16] = static let indices: [UInt16] =
[ [
// Triangle // Triangle
0, 1, 2, 0, 1, 2,
@@ -61,7 +61,7 @@ struct Lesson2: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
offset: 0), offset: 0),
] ]
let colourTargets: [SDL_GPUColorTargetDescription] = let colorTargets: [SDL_GPUColorTargetDescription] =
[ [
SDL_GPUColorTargetDescription( SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window), format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -72,8 +72,8 @@ struct Lesson2: AppDelegate
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo() var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!) targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count) targetInfo.num_color_targets = UInt32(colorTargets.count)
var info = SDL_GPUGraphicsPipelineCreateInfo( var info = SDL_GPUGraphicsPipelineCreateInfo(
vertex_shader: vertexShader, vertex_shader: vertexShader,
@@ -95,17 +95,6 @@ struct Lesson2: AppDelegate
} }
self.pso = pso self.pso = pso
guard let cmd = SDL_AcquireGPUCommandBuffer(ctx.device) else
{
throw .sdlError("SDL_AcquireGPUCommandBuffer", String(cString: SDL_GetError()))
}
let pass = SDL_BeginGPUCopyPass(cmd)
defer
{
SDL_EndGPUCopyPass(pass)
SDL_SubmitGPUCommandBuffer(cmd)
}
try ctx.copyPass { (pass) throws(NeHeError) in try ctx.copyPass { (pass) throws(NeHeError) in
self.vtxBuffer = try pass.createBuffer(usage: SDL_GPU_BUFFERUSAGE_VERTEX, Self.vertices[...]) self.vtxBuffer = try pass.createBuffer(usage: SDL_GPU_BUFFERUSAGE_VERTEX, Self.vertices[...])
self.idxBuffer = try pass.createBuffer(usage: SDL_GPU_BUFFERUSAGE_INDEX, Self.indices[...]) self.idxBuffer = try pass.createBuffer(usage: SDL_GPU_BUFFERUSAGE_INDEX, Self.indices[...])
@@ -135,7 +124,7 @@ struct Lesson2: AppDelegate
colorInfo.store_op = SDL_GPU_STOREOP_STORE colorInfo.store_op = SDL_GPU_STOREOP_STORE
// Begin pass & bind pipeline state // Begin pass & bind pipeline state
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil)
SDL_BindGPUGraphicsPipeline(pass, self.pso) SDL_BindGPUGraphicsPipeline(pass, self.pso)
// Bind vertex & index buffers // Bind vertex & index buffers
@@ -157,7 +146,7 @@ struct Lesson2: AppDelegate
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size)) SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
} }
} }

View File

@@ -34,7 +34,7 @@ struct Lesson3: AppDelegate
Vertex(.init(-1.0, -1.0, 0.0), .init(0.5, 0.5, 1.0, 1.0)), // Bottom left Vertex(.init(-1.0, -1.0, 0.0), .init(0.5, 0.5, 1.0, 1.0)), // Bottom left
] ]
static let indices: [Int16] = static let indices: [UInt16] =
[ [
// Triangle // Triangle
0, 1, 2, 0, 1, 2,
@@ -77,7 +77,7 @@ struct Lesson3: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)), offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)),
] ]
let colourTargets: [SDL_GPUColorTargetDescription] = let colorTargets: [SDL_GPUColorTargetDescription] =
[ [
SDL_GPUColorTargetDescription( SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window), format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -88,8 +88,8 @@ struct Lesson3: AppDelegate
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo() var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!) targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count) targetInfo.num_color_targets = UInt32(colorTargets.count)
var info = SDL_GPUGraphicsPipelineCreateInfo( var info = SDL_GPUGraphicsPipelineCreateInfo(
vertex_shader: vertexShader, vertex_shader: vertexShader,
@@ -140,7 +140,7 @@ struct Lesson3: AppDelegate
colorInfo.store_op = SDL_GPU_STOREOP_STORE colorInfo.store_op = SDL_GPU_STOREOP_STORE
// Begin pass & bind pipeline state // Begin pass & bind pipeline state
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil)
SDL_BindGPUGraphicsPipeline(pass, self.pso) SDL_BindGPUGraphicsPipeline(pass, self.pso)
// Bind vertex & index buffers // Bind vertex & index buffers
@@ -162,7 +162,7 @@ struct Lesson3: AppDelegate
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size)) SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
} }
} }

View File

@@ -20,6 +20,7 @@ struct Lesson4: AppDelegate
self.color = color self.color = color
} }
} }
static let vertices = static let vertices =
[ [
// Triangle // Triangle
@@ -33,7 +34,7 @@ struct Lesson4: AppDelegate
Vertex(.init(-1.0, -1.0, 0.0), .init(0.5, 0.5, 1.0, 1.0)), // Bottom left Vertex(.init(-1.0, -1.0, 0.0), .init(0.5, 0.5, 1.0, 1.0)), // Bottom left
] ]
static let indices: [Int16] = static let indices: [UInt16] =
[ [
// Triangle // Triangle
0, 1, 2, 0, 1, 2,
@@ -78,7 +79,7 @@ struct Lesson4: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)), offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)),
] ]
let colourTargets: [SDL_GPUColorTargetDescription] = let colorTargets: [SDL_GPUColorTargetDescription] =
[ [
SDL_GPUColorTargetDescription( SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window), format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -89,8 +90,8 @@ struct Lesson4: AppDelegate
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo() var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!) targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count) targetInfo.num_color_targets = UInt32(colorTargets.count)
var info = SDL_GPUGraphicsPipelineCreateInfo( var info = SDL_GPUGraphicsPipelineCreateInfo(
vertex_shader: vertexShader, vertex_shader: vertexShader,
@@ -141,7 +142,7 @@ struct Lesson4: AppDelegate
colorInfo.store_op = SDL_GPU_STOREOP_STORE colorInfo.store_op = SDL_GPU_STOREOP_STORE
// Begin pass & bind pipeline state // Begin pass & bind pipeline state
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, nil)
SDL_BindGPUGraphicsPipeline(pass, self.pso) SDL_BindGPUGraphicsPipeline(pass, self.pso)
// Bind vertex & index buffers // Bind vertex & index buffers
@@ -158,14 +159,14 @@ struct Lesson4: AppDelegate
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size)) SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 3, 1, 0, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, 3, 1, 0, 0, 0)
// Draw quad 1.5 units to the right and 6 units in // Draw quad 1.5 units to the right and 6 units into the camera
model = .translation(.init(1.5, 0.0, -6.0)) model = .translation(.init(1.5, 0.0, -6.0))
model.rotate(angle: self.rotQuad, axis: .init(1, 0, 0)) model.rotate(angle: self.rotQuad, axis: .init(1, 0, 0))
viewProj = self.projection * model viewProj = self.projection * model
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size)) SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
self.rotTri += 0.2 self.rotTri += 0.2
self.rotQuad -= 0.15 self.rotQuad -= 0.15

View File

@@ -109,7 +109,7 @@ struct Lesson5: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)), offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)),
] ]
let colourTargets: [SDL_GPUColorTargetDescription] = let colorTargets: [SDL_GPUColorTargetDescription] =
[ [
SDL_GPUColorTargetDescription( SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window), format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -124,8 +124,8 @@ struct Lesson5: AppDelegate
depthStencilState.enable_depth_test = true depthStencilState.enable_depth_test = true
depthStencilState.enable_depth_write = true depthStencilState.enable_depth_write = true
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo() var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!) targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count) targetInfo.num_color_targets = UInt32(colorTargets.count)
targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM
targetInfo.has_depth_stencil_target = true targetInfo.has_depth_stencil_target = true
@@ -187,7 +187,7 @@ struct Lesson5: AppDelegate
depthInfo.cycle = true depthInfo.cycle = true
// Begin pass & bind pipeline state // Begin pass & bind pipeline state
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, &depthInfo); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, &depthInfo)
SDL_BindGPUGraphicsPipeline(pass, self.pso) SDL_BindGPUGraphicsPipeline(pass, self.pso)
// Bind vertex & index buffers // Bind vertex & index buffers
@@ -197,21 +197,21 @@ struct Lesson5: AppDelegate
vtxBindings.withUnsafeBufferPointer(\.baseAddress!), UInt32(vtxBindings.count)) vtxBindings.withUnsafeBufferPointer(\.baseAddress!), UInt32(vtxBindings.count))
SDL_BindGPUIndexBuffer(pass, &idxBinding, SDL_GPU_INDEXELEMENTSIZE_16BIT) SDL_BindGPUIndexBuffer(pass, &idxBinding, SDL_GPU_INDEXELEMENTSIZE_16BIT)
// Draw triangle 1.5 units to the left and 6 units into the camera // Draw pyramid 1.5 units to the left and 6 units into the camera
var model: simd_float4x4 = .translation(.init(-1.5, 0.0, -6.0)) var model: simd_float4x4 = .translation(.init(-1.5, 0.0, -6.0))
model.rotate(angle: self.rotTri, axis: .init(0, 1, 0)) model.rotate(angle: self.rotTri, axis: .init(0, 1, 0))
var viewProj = self.projection * model var viewProj = self.projection * model
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size)) SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 12, 1, 0, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, 12, 1, 0, 0, 0)
// Draw quad 1.5 units to the right and 7 units into the camera // Draw cube 1.5 units to the right and 7 units into the camera
model = .translation(.init(1.5, 0.0, -7.0)) model = .translation(.init(1.5, 0.0, -7.0))
model.rotate(angle: self.rotQuad, axis: .init(1, 1, 1)) model.rotate(angle: self.rotQuad, axis: .init(1, 1, 1))
viewProj = self.projection * model viewProj = self.projection * model
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size)) SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 36, 1, 12, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, 36, 1, 12, 0, 0)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
self.rotTri += 0.2 self.rotTri += 0.2
self.rotQuad -= 0.15 self.rotQuad -= 0.15

View File

@@ -105,7 +105,7 @@ struct Lesson6: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.texcoord)!)), offset: UInt32(MemoryLayout<Vertex>.offset(of: \.texcoord)!)),
] ]
let colourTargets: [SDL_GPUColorTargetDescription] = let colorTargets: [SDL_GPUColorTargetDescription] =
[ [
SDL_GPUColorTargetDescription( SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window), format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -120,8 +120,8 @@ struct Lesson6: AppDelegate
depthStencilState.enable_depth_test = true depthStencilState.enable_depth_test = true
depthStencilState.enable_depth_write = true depthStencilState.enable_depth_write = true
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo() var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!) targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count) targetInfo.num_color_targets = UInt32(colorTargets.count)
targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM
targetInfo.has_depth_stencil_target = true targetInfo.has_depth_stencil_target = true
@@ -176,8 +176,8 @@ struct Lesson6: AppDelegate
self.projection = .perspective(fovy: 45, aspect: aspect, near: 0.1, far: 100) self.projection = .perspective(fovy: 45, aspect: aspect, near: 0.1, far: 100)
} }
mutating func draw(ctx: inout NeHeContext, mutating func draw(ctx: inout NeHeContext, cmd: OpaquePointer,
cmd: OpaquePointer, swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError) swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
{ {
var colorInfo = SDL_GPUColorTargetInfo() var colorInfo = SDL_GPUColorTargetInfo()
colorInfo.texture = swapchain colorInfo.texture = swapchain
@@ -195,7 +195,7 @@ struct Lesson6: AppDelegate
depthInfo.cycle = true depthInfo.cycle = true
// Begin pass & bind pipeline state // Begin pass & bind pipeline state
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, &depthInfo); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, &depthInfo)
SDL_BindGPUGraphicsPipeline(pass, self.pso) SDL_BindGPUGraphicsPipeline(pass, self.pso)
// Bind texture // Bind texture
@@ -211,7 +211,7 @@ struct Lesson6: AppDelegate
struct Uniforms { var modelViewProj: simd_float4x4, color: SIMD4<Float> } struct Uniforms { var modelViewProj: simd_float4x4, color: SIMD4<Float> }
// Move cube 5 units into the screen and apply some rotations // Move cube 5 units into the camera and apply some rotations
var model: simd_float4x4 = .translation(.init(0.0, 0.0, -5.0)) var model: simd_float4x4 = .translation(.init(0.0, 0.0, -5.0))
model.rotate(angle: rot.x, axis: .init(1.0, 0.0, 0.0)) model.rotate(angle: rot.x, axis: .init(1.0, 0.0, 0.0))
model.rotate(angle: rot.y, axis: .init(0.0, 1.0, 0.0)) model.rotate(angle: rot.y, axis: .init(0.0, 1.0, 0.0))
@@ -226,7 +226,7 @@ struct Lesson6: AppDelegate
// Draw textured cube // Draw textured cube
SDL_DrawGPUIndexedPrimitives(pass, UInt32(Self.indices.count), 1, 0, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, UInt32(Self.indices.count), 1, 0, 0, 0)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
self.rot += .init(0.3, 0.2, 0.4) self.rot += .init(0.3, 0.2, 0.4)
} }

View File

@@ -130,36 +130,30 @@ struct Lesson7: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.texcoord)!)), offset: UInt32(MemoryLayout<Vertex>.offset(of: \.texcoord)!)),
] ]
let colourTargets: [SDL_GPUColorTargetDescription] =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
blend_state: SDL_GPUColorTargetBlendState())
]
var rasterizerDesc = SDL_GPURasterizerState()
rasterizerDesc.fill_mode = SDL_GPU_FILLMODE_FILL
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var depthStencilState = SDL_GPUDepthStencilState()
depthStencilState.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL
depthStencilState.enable_depth_test = true
depthStencilState.enable_depth_write = true
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count)
targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM
targetInfo.has_depth_stencil_target = true
var info = SDL_GPUGraphicsPipelineCreateInfo() var info = SDL_GPUGraphicsPipelineCreateInfo()
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST
info.vertex_input_state = SDL_GPUVertexInputState( info.vertex_input_state = SDL_GPUVertexInputState(
vertex_buffer_descriptions: vertexDescriptions.withUnsafeBufferPointer(\.baseAddress!), vertex_buffer_descriptions: vertexDescriptions.withUnsafeBufferPointer(\.baseAddress!),
num_vertex_buffers: UInt32(vertexDescriptions.count), num_vertex_buffers: UInt32(vertexDescriptions.count),
vertex_attributes: vertexAttributes.withUnsafeBufferPointer(\.baseAddress!), vertex_attributes: vertexAttributes.withUnsafeBufferPointer(\.baseAddress!),
num_vertex_attributes: UInt32(vertexAttributes.count)) num_vertex_attributes: UInt32(vertexAttributes.count))
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL
info.rasterizer_state = rasterizerDesc info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE
info.depth_stencil_state = depthStencilState info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
info.target_info = targetInfo info.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL
info.depth_stencil_state.enable_depth_test = true
info.depth_stencil_state.enable_depth_write = true
let colorTargets =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
blend_state: SDL_GPUColorTargetBlendState()),
]
info.target_info.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
info.target_info.num_color_targets = UInt32(colorTargets.count)
info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM
info.target_info.has_depth_stencil_target = true
// Create unlit pipeline // Create unlit pipeline
info.vertex_shader = vertexShaderUnlit info.vertex_shader = vertexShaderUnlit
@@ -222,8 +216,8 @@ struct Lesson7: AppDelegate
self.projection = .perspective(fovy: 45, aspect: aspect, near: 0.1, far: 100) self.projection = .perspective(fovy: 45, aspect: aspect, near: 0.1, far: 100)
} }
mutating func draw(ctx: inout NeHeContext, mutating func draw(ctx: inout NeHeContext, cmd: OpaquePointer,
cmd: OpaquePointer, swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError) swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
{ {
var colorInfo = SDL_GPUColorTargetInfo() var colorInfo = SDL_GPUColorTargetInfo()
colorInfo.texture = swapchain colorInfo.texture = swapchain
@@ -241,7 +235,7 @@ struct Lesson7: AppDelegate
depthInfo.cycle = true depthInfo.cycle = true
// Begin pass & bind pipeline state // Begin pass & bind pipeline state
let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, &depthInfo); let pass = SDL_BeginGPURenderPass(cmd, &colorInfo, 1, &depthInfo)
SDL_BindGPUGraphicsPipeline(pass, self.lighting ? self.psoLight : self.psoUnlit) SDL_BindGPUGraphicsPipeline(pass, self.lighting ? self.psoLight : self.psoUnlit)
// Bind texture // Bind texture
@@ -282,7 +276,7 @@ struct Lesson7: AppDelegate
// Draw textured cube // Draw textured cube
SDL_DrawGPUIndexedPrimitives(pass, UInt32(Self.indices.count), 1, 0, 0, 0) SDL_DrawGPUIndexedPrimitives(pass, UInt32(Self.indices.count), 1, 0, 0, 0)
SDL_EndGPURenderPass(pass); SDL_EndGPURenderPass(pass)
let keys = SDL_GetKeyboardState(nil)! let keys = SDL_GetKeyboardState(nil)!

View File

@@ -88,7 +88,6 @@ struct Lesson8: AppDelegate
var speed = SIMD2<Float>(repeating: 0.0) var speed = SIMD2<Float>(repeating: 0.0)
var z: Float = -5.0 var z: Float = -5.0
mutating func `init`(ctx: inout NeHeContext) throws(NeHeError) mutating func `init`(ctx: inout NeHeContext) throws(NeHeError)
{ {
let (vertexShaderUnlit, fragmentShaderUnlit) = try ctx.loadShaders(name: "lesson6", let (vertexShaderUnlit, fragmentShaderUnlit) = try ctx.loadShaders(name: "lesson6",
@@ -251,7 +250,7 @@ struct Lesson8: AppDelegate
} }
mutating func draw(ctx: inout NeHeContext, cmd: OpaquePointer, mutating func draw(ctx: inout NeHeContext, cmd: OpaquePointer,
swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHe.NeHeError) swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
{ {
var colorInfo = SDL_GPUColorTargetInfo() var colorInfo = SDL_GPUColorTargetInfo()
colorInfo.texture = swapchain colorInfo.texture = swapchain