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

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

View File

@@ -23,7 +23,7 @@ struct Lesson2: AppDelegate
SIMD3<Float>( -1.0, -1.0, 0.0), // Bottom left
]
static let indices: [Int16] =
static let indices: [UInt16] =
[
// Triangle
0, 1, 2,
@@ -61,7 +61,7 @@ struct Lesson2: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
offset: 0),
]
let colourTargets: [SDL_GPUColorTargetDescription] =
let colorTargets: [SDL_GPUColorTargetDescription] =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -72,8 +72,8 @@ struct Lesson2: AppDelegate
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count)
targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colorTargets.count)
var info = SDL_GPUGraphicsPipelineCreateInfo(
vertex_shader: vertexShader,
@@ -95,17 +95,6 @@ struct Lesson2: AppDelegate
}
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
self.vtxBuffer = try pass.createBuffer(usage: SDL_GPU_BUFFERUSAGE_VERTEX, Self.vertices[...])
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
// 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)
// Bind vertex & index buffers
@@ -157,7 +146,7 @@ struct Lesson2: AppDelegate
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
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
]
static let indices: [Int16] =
static let indices: [UInt16] =
[
// Triangle
0, 1, 2,
@@ -77,7 +77,7 @@ struct Lesson3: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)),
]
let colourTargets: [SDL_GPUColorTargetDescription] =
let colorTargets: [SDL_GPUColorTargetDescription] =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -88,8 +88,8 @@ struct Lesson3: AppDelegate
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count)
targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colorTargets.count)
var info = SDL_GPUGraphicsPipelineCreateInfo(
vertex_shader: vertexShader,
@@ -140,7 +140,7 @@ struct Lesson3: AppDelegate
colorInfo.store_op = SDL_GPU_STOREOP_STORE
// 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)
// Bind vertex & index buffers
@@ -162,7 +162,7 @@ struct Lesson3: AppDelegate
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
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
}
}
static let vertices =
[
// 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
]
static let indices: [Int16] =
static let indices: [UInt16] =
[
// Triangle
0, 1, 2,
@@ -78,7 +79,7 @@ struct Lesson4: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)),
]
let colourTargets: [SDL_GPUColorTargetDescription] =
let colorTargets: [SDL_GPUColorTargetDescription] =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -89,8 +90,8 @@ struct Lesson4: AppDelegate
rasterizerDesc.cull_mode = SDL_GPU_CULLMODE_NONE
rasterizerDesc.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
var targetInfo = SDL_GPUGraphicsPipelineTargetInfo()
targetInfo.color_target_descriptions = colourTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colourTargets.count)
targetInfo.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colorTargets.count)
var info = SDL_GPUGraphicsPipelineCreateInfo(
vertex_shader: vertexShader,
@@ -141,7 +142,7 @@ struct Lesson4: AppDelegate
colorInfo.store_op = SDL_GPU_STOREOP_STORE
// 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)
// Bind vertex & index buffers
@@ -158,14 +159,14 @@ struct Lesson4: AppDelegate
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
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.rotate(angle: self.rotQuad, axis: .init(1, 0, 0))
viewProj = self.projection * model
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 6, 1, 3, 0, 0)
SDL_EndGPURenderPass(pass);
SDL_EndGPURenderPass(pass)
self.rotTri += 0.2
self.rotQuad -= 0.15

View File

@@ -109,7 +109,7 @@ struct Lesson5: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.color)!)),
]
let colourTargets: [SDL_GPUColorTargetDescription] =
let colorTargets: [SDL_GPUColorTargetDescription] =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -124,8 +124,8 @@ struct Lesson5: AppDelegate
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.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colorTargets.count)
targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM
targetInfo.has_depth_stencil_target = true
@@ -187,7 +187,7 @@ struct Lesson5: AppDelegate
depthInfo.cycle = true
// 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)
// Bind vertex & index buffers
@@ -197,21 +197,21 @@ struct Lesson5: AppDelegate
vtxBindings.withUnsafeBufferPointer(\.baseAddress!), UInt32(vtxBindings.count))
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))
model.rotate(angle: self.rotTri, axis: .init(0, 1, 0))
var viewProj = self.projection * model
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
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.rotate(angle: self.rotQuad, axis: .init(1, 1, 1))
viewProj = self.projection * model
SDL_PushGPUVertexUniformData(cmd, 0, &viewProj, UInt32(MemoryLayout<simd_float4x4>.size))
SDL_DrawGPUIndexedPrimitives(pass, 36, 1, 12, 0, 0)
SDL_EndGPURenderPass(pass);
SDL_EndGPURenderPass(pass)
self.rotTri += 0.2
self.rotQuad -= 0.15

View File

@@ -105,7 +105,7 @@ struct Lesson6: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
offset: UInt32(MemoryLayout<Vertex>.offset(of: \.texcoord)!)),
]
let colourTargets: [SDL_GPUColorTargetDescription] =
let colorTargets: [SDL_GPUColorTargetDescription] =
[
SDL_GPUColorTargetDescription(
format: SDL_GetGPUSwapchainTextureFormat(ctx.device, ctx.window),
@@ -120,8 +120,8 @@ struct Lesson6: AppDelegate
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.color_target_descriptions = colorTargets.withUnsafeBufferPointer(\.baseAddress!)
targetInfo.num_color_targets = UInt32(colorTargets.count)
targetInfo.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM
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)
}
mutating func draw(ctx: inout NeHeContext,
cmd: OpaquePointer, swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
mutating func draw(ctx: inout NeHeContext, cmd: OpaquePointer,
swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
{
var colorInfo = SDL_GPUColorTargetInfo()
colorInfo.texture = swapchain
@@ -195,7 +195,7 @@ struct Lesson6: AppDelegate
depthInfo.cycle = true
// 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)
// Bind texture
@@ -211,7 +211,7 @@ struct Lesson6: AppDelegate
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))
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))
@@ -226,7 +226,7 @@ struct Lesson6: AppDelegate
// Draw textured cube
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)
}

View File

@@ -130,36 +130,30 @@ struct Lesson7: AppDelegate
format: SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
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()
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST
info.vertex_input_state = SDL_GPUVertexInputState(
vertex_buffer_descriptions: vertexDescriptions.withUnsafeBufferPointer(\.baseAddress!),
num_vertex_buffers: UInt32(vertexDescriptions.count),
vertex_attributes: vertexAttributes.withUnsafeBufferPointer(\.baseAddress!),
num_vertex_attributes: UInt32(vertexAttributes.count))
info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST
info.rasterizer_state = rasterizerDesc
info.depth_stencil_state = depthStencilState
info.target_info = targetInfo
info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL
info.rasterizer_state.cull_mode = SDL_GPU_CULLMODE_NONE
info.rasterizer_state.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE
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
info.vertex_shader = vertexShaderUnlit
@@ -222,8 +216,8 @@ struct Lesson7: AppDelegate
self.projection = .perspective(fovy: 45, aspect: aspect, near: 0.1, far: 100)
}
mutating func draw(ctx: inout NeHeContext,
cmd: OpaquePointer, swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
mutating func draw(ctx: inout NeHeContext, cmd: OpaquePointer,
swapchain: OpaquePointer, swapchainSize: Size<UInt32>) throws(NeHeError)
{
var colorInfo = SDL_GPUColorTargetInfo()
colorInfo.texture = swapchain
@@ -241,7 +235,7 @@ struct Lesson7: AppDelegate
depthInfo.cycle = true
// 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)
// Bind texture
@@ -282,7 +276,7 @@ struct Lesson7: AppDelegate
// Draw textured cube
SDL_DrawGPUIndexedPrimitives(pass, UInt32(Self.indices.count), 1, 0, 0, 0)
SDL_EndGPURenderPass(pass);
SDL_EndGPURenderPass(pass)
let keys = SDL_GetKeyboardState(nil)!

View File

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