mirror of
https://github.com/ScrelliCopter/NeHe-SDL_GPU.git
synced 2025-06-19 21:49:17 +10:00
Simplify lessons that use textured shaders w/o tinting
This commit is contained in:
@@ -41,8 +41,8 @@ let package = Package(
|
|||||||
.process("../../../data/shaders/lesson7.metallib"),
|
.process("../../../data/shaders/lesson7.metallib"),
|
||||||
.process("../../../data/Crate.bmp") ]),
|
.process("../../../data/Crate.bmp") ]),
|
||||||
.executableTarget(name: "Lesson8", dependencies: [ "NeHe" ], path: "src/swift/Lesson8", resources: [
|
.executableTarget(name: "Lesson8", dependencies: [ "NeHe" ], path: "src/swift/Lesson8", resources: [
|
||||||
.process("../../../data/shaders/lesson6.metallib"),
|
|
||||||
.process("../../../data/shaders/lesson7.metallib"),
|
.process("../../../data/shaders/lesson7.metallib"),
|
||||||
|
.process("../../../data/shaders/lesson8.metallib"),
|
||||||
.process("../../../data/Glass.bmp") ]),
|
.process("../../../data/Glass.bmp") ]),
|
||||||
.executableTarget(name: "Lesson9", dependencies: [ "NeHe" ], path: "src/swift/Lesson9", resources: [
|
.executableTarget(name: "Lesson9", dependencies: [ "NeHe" ], path: "src/swift/Lesson9", resources: [
|
||||||
.process("../../../data/shaders/lesson9.metallib"),
|
.process("../../../data/shaders/lesson9.metallib"),
|
||||||
|
|||||||
1
build.rs
1
build.rs
@@ -53,6 +53,7 @@ pub fn main()
|
|||||||
"lesson3.metallib",
|
"lesson3.metallib",
|
||||||
"lesson6.metallib",
|
"lesson6.metallib",
|
||||||
"lesson7.metallib",
|
"lesson7.metallib",
|
||||||
|
"lesson8.metallib",
|
||||||
"lesson9.metallib",
|
"lesson9.metallib",
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
data/shaders/lesson8.metallib
Normal file
BIN
data/shaders/lesson8.metallib
Normal file
Binary file not shown.
Binary file not shown.
@@ -134,7 +134,7 @@ def compile_shaders() -> None:
|
|||||||
build_metal = True
|
build_metal = True
|
||||||
build_dxil = False
|
build_dxil = False
|
||||||
build_dxbc = False
|
build_dxbc = False
|
||||||
lessons = [ "lesson2", "lesson3", "lesson6", "lesson7", "lesson9" ]
|
lessons = [ "lesson2", "lesson3", "lesson6", "lesson7", "lesson8", "lesson9" ]
|
||||||
src_dir = Path("src/shaders")
|
src_dir = Path("src/shaders")
|
||||||
dest_dir = Path("data/shaders")
|
dest_dir = Path("data/shaders")
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ add_lesson(lesson04 SOURCES lesson4.c SHADERS lesson3.metallib)
|
|||||||
add_lesson(lesson05 SOURCES lesson5.c SHADERS lesson3.metallib)
|
add_lesson(lesson05 SOURCES lesson5.c SHADERS lesson3.metallib)
|
||||||
add_lesson(lesson06 SOURCES lesson6.c SHADERS lesson6.metallib DATA NeHe.bmp)
|
add_lesson(lesson06 SOURCES lesson6.c SHADERS lesson6.metallib DATA NeHe.bmp)
|
||||||
add_lesson(lesson07 SOURCES lesson7.c SHADERS lesson6.metallib lesson7.metallib DATA Crate.bmp)
|
add_lesson(lesson07 SOURCES lesson7.c SHADERS lesson6.metallib lesson7.metallib DATA Crate.bmp)
|
||||||
add_lesson(lesson08 SOURCES lesson8.c SHADERS lesson6.metallib lesson7.metallib DATA Glass.bmp)
|
add_lesson(lesson08 SOURCES lesson8.c SHADERS lesson7.metallib lesson8.metallib DATA Glass.bmp)
|
||||||
add_lesson(lesson09 SOURCES lesson9.c SHADERS lesson6.metallib lesson9.metallib DATA Star.bmp)
|
add_lesson(lesson09 SOURCES lesson9.c SHADERS lesson6.metallib lesson9.metallib DATA Star.bmp)
|
||||||
add_lesson(lesson10 SOURCES lesson10.c SHADERS lesson6.metallib DATA Mud.bmp World.txt)
|
add_lesson(lesson10 SOURCES lesson10.c SHADERS lesson6.metallib DATA Mud.bmp World.txt)
|
||||||
|
|||||||
@@ -307,10 +307,9 @@ static void Lesson10_Draw(NeHeContext* restrict ctx, SDL_GPUCommandBuffer* restr
|
|||||||
Mtx_Translate(modelView, -camera.x, -(0.25f + camera.walkBob), -camera.z);
|
Mtx_Translate(modelView, -camera.x, -(0.25f + camera.walkBob), -camera.z);
|
||||||
|
|
||||||
// Push shader uniforms
|
// Push shader uniforms
|
||||||
struct { float modelViewProj[16], color[4]; } u;
|
float modelViewProj[16];
|
||||||
Mtx_Multiply(u.modelViewProj, projection, modelView);
|
Mtx_Multiply(modelViewProj, projection, modelView);
|
||||||
SDL_memcpy(u.color, (float[4]){ 1.0f, 1.0f, 1.0f, 1.0f }, sizeof(float) * 4);
|
SDL_PushGPUVertexUniformData(cmd, 0, &modelViewProj, sizeof(modelViewProj));
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, &u, sizeof(u));
|
|
||||||
|
|
||||||
// Draw world
|
// Draw world
|
||||||
SDL_DrawGPUPrimitives(pass, 3u * (uint32_t)world.numTriangles, 1, 0, 0);
|
SDL_DrawGPUPrimitives(pass, 3u * (uint32_t)world.numTriangles, 1, 0, 0);
|
||||||
|
|||||||
@@ -229,8 +229,7 @@ static void Lesson6_Draw(NeHeContext* restrict ctx, SDL_GPUCommandBuffer* restri
|
|||||||
.offset = 0
|
.offset = 0
|
||||||
}, SDL_GPU_INDEXELEMENTSIZE_16BIT);
|
}, SDL_GPU_INDEXELEMENTSIZE_16BIT);
|
||||||
|
|
||||||
float model[16];
|
float model[16], modelViewProj[16];
|
||||||
struct { float modelViewProj[16], color[4]; } u;
|
|
||||||
|
|
||||||
// Move cube 5 units into the screen and apply some rotations
|
// Move cube 5 units into the screen and apply some rotations
|
||||||
Mtx_Translation(model, 0.0f, 0.0f, -5.0f);
|
Mtx_Translation(model, 0.0f, 0.0f, -5.0f);
|
||||||
@@ -239,9 +238,8 @@ static void Lesson6_Draw(NeHeContext* restrict ctx, SDL_GPUCommandBuffer* restri
|
|||||||
Mtx_Rotate(model, zRot, 0.0f, 0.0f, 1.0f);
|
Mtx_Rotate(model, zRot, 0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
// Push shader uniforms
|
// Push shader uniforms
|
||||||
Mtx_Multiply(u.modelViewProj, projection, model);
|
Mtx_Multiply(modelViewProj, projection, model);
|
||||||
SDL_memcpy(u.color, (float[4]){ 1.0f, 1.0f, 1.0f, 1.0f }, sizeof(float) * 4);
|
SDL_PushGPUVertexUniformData(cmd, 0, &modelViewProj, sizeof(modelViewProj));
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, &u, sizeof(u));
|
|
||||||
|
|
||||||
// Draw textured cube
|
// Draw textured cube
|
||||||
SDL_DrawGPUIndexedPrimitives(pass, SDL_arraysize(indices), 1, 0, 0, 0);
|
SDL_DrawGPUIndexedPrimitives(pass, SDL_arraysize(indices), 1, 0, 0, 0);
|
||||||
|
|||||||
@@ -319,10 +319,9 @@ static void Lesson7_Draw(NeHeContext* restrict ctx, SDL_GPUCommandBuffer* restri
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct { float modelViewProj[16], color[4]; } u;
|
float modelViewProj[16];
|
||||||
Mtx_Multiply(u.modelViewProj, projection, model);
|
Mtx_Multiply(modelViewProj, projection, model);
|
||||||
SDL_memcpy(u.color, (const float[4]){ 1.0f, 1.0f, 1.0f, 1.0f }, sizeof(float) * 4);
|
SDL_PushGPUVertexUniformData(cmd, 0, &modelViewProj, sizeof(modelViewProj));
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, &u, sizeof(u));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw textured cube
|
// Draw textured cube
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ static bool Lesson8_Init(NeHeContext* restrict ctx)
|
|||||||
{
|
{
|
||||||
SDL_GPUShader* vertexShaderUnlit, * fragmentShaderUnlit;
|
SDL_GPUShader* vertexShaderUnlit, * fragmentShaderUnlit;
|
||||||
SDL_GPUShader* vertexShaderLight, * fragmentShaderLight;
|
SDL_GPUShader* vertexShaderLight, * fragmentShaderLight;
|
||||||
if (!NeHe_LoadShaders(ctx, &vertexShaderUnlit, &fragmentShaderUnlit, "lesson6",
|
if (!NeHe_LoadShaders(ctx, &vertexShaderUnlit, &fragmentShaderUnlit, "lesson8",
|
||||||
&(const NeHeShaderProgramCreateInfo){ .vertexUniforms = 1, .fragmentSamplers = 1 }))
|
&(const NeHeShaderProgramCreateInfo){ .vertexUniforms = 1, .fragmentSamplers = 1 }))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -330,10 +330,10 @@ impl AppImplementation for Lesson10
|
|||||||
model_view.translate(-cam_x, -cam_y, -cam_z);
|
model_view.translate(-cam_x, -cam_y, -cam_z);
|
||||||
|
|
||||||
// Push shader uniforms
|
// Push shader uniforms
|
||||||
#[allow(dead_code)]
|
let model_view_proj = self.projection * model_view;
|
||||||
struct Uniforms { model_view_proj: Mtx, color: [f32; 4] }
|
SDL_PushGPUVertexUniformData(cmd, 0,
|
||||||
let u = Uniforms { model_view_proj: self.projection * model_view, color: [1.0; 4] };
|
addr_of!(model_view_proj) as *const c_void,
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, addr_of!(u) as *const c_void, size_of::<Uniforms>() as u32);
|
size_of::<Mtx>() as u32);
|
||||||
|
|
||||||
// Draw world
|
// Draw world
|
||||||
SDL_DrawGPUPrimitives(pass, self.world.vertices.len() as u32, 1, 0, 0);
|
SDL_DrawGPUPrimitives(pass, self.world.vertices.len() as u32, 1, 0, 0);
|
||||||
|
|||||||
@@ -252,12 +252,11 @@ impl AppImplementation for Lesson6
|
|||||||
model.rotate(self.rot.1, 0.0, 1.0, 0.0);
|
model.rotate(self.rot.1, 0.0, 1.0, 0.0);
|
||||||
model.rotate(self.rot.2, 0.0, 0.0, 1.0);
|
model.rotate(self.rot.2, 0.0, 0.0, 1.0);
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
struct Uniforms { model_view_proj: Mtx, color: [f32; 4] }
|
|
||||||
|
|
||||||
// Push shader uniforms
|
// Push shader uniforms
|
||||||
let u = Uniforms { model_view_proj: self.projection * model, color: [1.0; 4] };
|
let model_view_proj = self.projection * model;
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, addr_of!(u) as *const c_void, size_of::<Uniforms>() as u32);
|
SDL_PushGPUVertexUniformData(cmd, 0,
|
||||||
|
addr_of!(model_view_proj) as *const c_void,
|
||||||
|
size_of::<Mtx>() as u32);
|
||||||
|
|
||||||
// Draw the textured cube
|
// Draw the textured cube
|
||||||
SDL_DrawGPUIndexedPrimitives(pass, INDICES.len() as u32, 1, 0, 0, 0);
|
SDL_DrawGPUIndexedPrimitives(pass, INDICES.len() as u32, 1, 0, 0, 0);
|
||||||
|
|||||||
@@ -335,10 +335,8 @@ impl AppImplementation for Lesson7
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#[allow(dead_code)]
|
let model_view_proj = self.projection * model;
|
||||||
struct Uniforms { model_view_proj: Mtx, color: [f32; 4] }
|
SDL_PushGPUVertexUniformData(cmd, 0, addr_of!(model_view_proj) as *const c_void, size_of::<Mtx>() as u32);
|
||||||
let u = Uniforms { model_view_proj: self.projection * model, color: [1.0; 4] };
|
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, addr_of!(u) as *const c_void, size_of::<Uniforms>() as u32);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the textured cube
|
// Draw the textured cube
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ impl AppImplementation for Lesson8
|
|||||||
|
|
||||||
fn init(&mut self, ctx: &NeHeContext) -> Result<(), NeHeError>
|
fn init(&mut self, ctx: &NeHeContext) -> Result<(), NeHeError>
|
||||||
{
|
{
|
||||||
let (vertex_shader_unlit, fragment_shader_unlit) = ctx.load_shaders("lesson6", 1, 0, 1)?;
|
let (vertex_shader_unlit, fragment_shader_unlit) = ctx.load_shaders("lesson8", 1, 0, 1)?;
|
||||||
let (vertex_shader_light, fragment_shader_light) = ctx.load_shaders("lesson7", 2, 0, 1)?;
|
let (vertex_shader_light, fragment_shader_light) = ctx.load_shaders("lesson7", 2, 0, 1)?;
|
||||||
|
|
||||||
const VERTEX_DESCRIPTIONS: &'static [SDL_GPUVertexBufferDescription] =
|
const VERTEX_DESCRIPTIONS: &'static [SDL_GPUVertexBufferDescription] =
|
||||||
|
|||||||
@@ -15,14 +15,12 @@ struct VertexInput
|
|||||||
struct VertexUniform
|
struct VertexUniform
|
||||||
{
|
{
|
||||||
metal::float4x4 viewproj;
|
metal::float4x4 viewproj;
|
||||||
float4 color;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Vertex2Fragment
|
struct Vertex2Fragment
|
||||||
{
|
{
|
||||||
float4 position [[position]];
|
float4 position [[position]];
|
||||||
float2 texcoord;
|
float2 texcoord;
|
||||||
half4 color;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
vertex Vertex2Fragment VertexMain(
|
vertex Vertex2Fragment VertexMain(
|
||||||
@@ -32,7 +30,6 @@ vertex Vertex2Fragment VertexMain(
|
|||||||
Vertex2Fragment out;
|
Vertex2Fragment out;
|
||||||
out.position = u.viewproj * float4(in.position, 1.0);
|
out.position = u.viewproj * float4(in.position, 1.0);
|
||||||
out.texcoord = in.texcoord;
|
out.texcoord = in.texcoord;
|
||||||
out.color = half4(u.color);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,5 +38,5 @@ fragment half4 FragmentMain(
|
|||||||
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
||||||
metal::sampler sampler [[sampler(0)]])
|
metal::sampler sampler [[sampler(0)]])
|
||||||
{
|
{
|
||||||
return in.color * texture.sample(sampler, in.texcoord);
|
return texture.sample(sampler, in.texcoord);
|
||||||
}
|
}
|
||||||
|
|||||||
45
src/shaders/lesson8.metal
Normal file
45
src/shaders/lesson8.metal
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
|
||||||
|
* SPDX-License-Identifier: Zlib
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <metal_stdlib>
|
||||||
|
#include <simd/simd.h>
|
||||||
|
|
||||||
|
struct VertexInput
|
||||||
|
{
|
||||||
|
float3 position [[attribute(0)]];
|
||||||
|
float2 texcoord [[attribute(1)]];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexUniform
|
||||||
|
{
|
||||||
|
metal::float4x4 viewproj;
|
||||||
|
float4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Vertex2Fragment
|
||||||
|
{
|
||||||
|
float4 position [[position]];
|
||||||
|
float2 texcoord;
|
||||||
|
half4 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
vertex Vertex2Fragment VertexMain(
|
||||||
|
VertexInput in [[stage_in]],
|
||||||
|
constant VertexUniform& u [[buffer(0)]])
|
||||||
|
{
|
||||||
|
Vertex2Fragment out;
|
||||||
|
out.position = u.viewproj * float4(in.position, 1.0);
|
||||||
|
out.texcoord = in.texcoord;
|
||||||
|
out.color = half4(u.color);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment half4 FragmentMain(
|
||||||
|
Vertex2Fragment in [[stage_in]],
|
||||||
|
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
||||||
|
metal::sampler sampler [[sampler(0)]])
|
||||||
|
{
|
||||||
|
return in.color * texture.sample(sampler, in.texcoord);
|
||||||
|
}
|
||||||
@@ -298,11 +298,8 @@ struct Lesson10: AppDelegate
|
|||||||
modelView.translate(-camera.position)
|
modelView.translate(-camera.position)
|
||||||
|
|
||||||
// Push shader uniforms
|
// Push shader uniforms
|
||||||
struct Uniforms { var modelViewProj: simd_float4x4, color: SIMD4<Float> }
|
var modelViewProj = self.projection * modelView
|
||||||
var u = Uniforms(
|
SDL_PushGPUVertexUniformData(cmd, 0, &modelViewProj, UInt32(MemoryLayout<simd_float4x4>.size))
|
||||||
modelViewProj: self.projection * modelView,
|
|
||||||
color: .init(repeating: 1.0))
|
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, &u, UInt32(MemoryLayout<Uniforms>.size))
|
|
||||||
|
|
||||||
// Draw world
|
// Draw world
|
||||||
SDL_DrawGPUPrimitives(pass, UInt32(self.world.vertices.count), 1, 0, 0)
|
SDL_DrawGPUPrimitives(pass, UInt32(self.world.vertices.count), 1, 0, 0)
|
||||||
|
|||||||
@@ -209,8 +209,6 @@ struct Lesson6: 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)
|
||||||
|
|
||||||
struct Uniforms { var modelViewProj: simd_float4x4, color: SIMD4<Float> }
|
|
||||||
|
|
||||||
// Move cube 5 units into the camera 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))
|
||||||
@@ -218,10 +216,8 @@ struct Lesson6: AppDelegate
|
|||||||
model.rotate(angle: rot.z, axis: .init(0.0, 0.0, 1.0))
|
model.rotate(angle: rot.z, axis: .init(0.0, 0.0, 1.0))
|
||||||
|
|
||||||
// Push shader uniforms
|
// Push shader uniforms
|
||||||
var u = Uniforms(
|
var modelViewProj = self.projection * model
|
||||||
modelViewProj: self.projection * model,
|
SDL_PushGPUVertexUniformData(cmd, 0, &modelViewProj, UInt32(MemoryLayout<matrix_float4x4>.size))
|
||||||
color: .init(repeating: 1.0))
|
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, &u, UInt32(MemoryLayout<Uniforms>.size))
|
|
||||||
|
|
||||||
// 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)
|
||||||
|
|||||||
@@ -266,11 +266,8 @@ struct Lesson7: AppDelegate
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
struct Uniforms { var modelViewProj: simd_float4x4, color: SIMD4<Float> }
|
var modelViewProj = self.projection * model
|
||||||
var u = Uniforms(
|
SDL_PushGPUVertexUniformData(cmd, 0, &modelViewProj, UInt32(MemoryLayout<simd_float4x4>.size))
|
||||||
modelViewProj: self.projection * model,
|
|
||||||
color: .init(repeating: 1.0))
|
|
||||||
SDL_PushGPUVertexUniformData(cmd, 0, &u, UInt32(MemoryLayout<Uniforms>.size))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw textured cube
|
// Draw textured cube
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ struct Lesson8: AppDelegate
|
|||||||
|
|
||||||
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: "lesson8",
|
||||||
vertexUniforms: 1, fragmentSamplers: 1)
|
vertexUniforms: 1, fragmentSamplers: 1)
|
||||||
defer
|
defer
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user