> sdl_gpu mesh pipeline and shader reloading

This commit is contained in:
Peter Li 2025-04-18 23:41:16 -07:00
parent 72b97b3743
commit cd66fbb457
9 changed files with 65 additions and 37 deletions

View File

@ -7,7 +7,7 @@ struct Input
uint Bones : TEXCOORD4; uint Bones : TEXCOORD4;
uint weights : TEXCOORD5; uint weights : TEXCOORD5;
uint InstanceIndex : SV_InstanceID; uint Instance : SV_InstanceID;
}; };
struct Output struct Output
@ -30,13 +30,14 @@ cbuffer Uniforms : register(b0, space1)
float time; float time;
}; };
#define IDENTITY_MATRIX float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
Output main(Input input) Output main(Input input)
{ {
Output output; Output output;
output.TexCoord = input.UV; output.TexCoord = input.UV;
output.Position = mul(mul(float4(input.Position, 1.0), transpose(scene[input.InstanceIndex].Model)), transpose(ViewProjection)); // output.TexCoord[0] += scene[input.Instance].Model[0][0] * 0.0001;
//output.Position = mul(ViewProjection, float4(input.Position - float3(1 * sin(time + 0.05 * input.Position[0]),0, 1 * cos(time + 0.05 * input.Position[0])), 1.0));
output.Position = mul(ViewProjection, mul(scene[input.Instance].Model, float4(input.Position, 1.0)));
return output; return output;
} }

View File

@ -32,18 +32,21 @@ pub fn initECS(self: *@This(), handle: core.SetHandle) void {
self.entity = core.Entity.fromHandle(handle); self.entity = core.Entity.fromHandle(handle);
} }
pub fn updateProjections(self: *@This()) core.Transform { pub fn updateProjections(self: *@This()) void {
self.projection = zm.perspectiveFovRh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000); self.projection = zm.perspectiveFovLh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000);
self.projection[1][1] *= -1; // self.projection[1][1] *= -1;
self.projectionAlt = zm.perspectiveFovRh(core.radians(self.altFov), 16.0 / 9.0, 0.01, 200000); self.projectionAlt = zm.perspectiveFovLh(core.radians(self.altFov), 16.0 / 9.0, 0.01, 200000);
self.projectionAlt[1][1] *= -1; self.projectionAlt[1][1] *= -1;
} }
pub fn resolve(self: *@This()) void { pub fn resolve(self: *@This()) void {
const scene = self.entity.get(core.Scene).?; const scene = self.entity.get(core.Scene).?;
const position = scene.getPosition(); self.updateProjections();
var position = scene.getPosition();
position.z = -position.z;
{ {
var base = core.zm.identity(); var base = core.zm.identity();
base = mul(core.zm.rotationY(-self.yaw), base); base = mul(core.zm.rotationY(-self.yaw), base);
@ -59,20 +62,20 @@ pub fn resolve(self: *@This()) void {
// calculate viewProjections // calculate viewProjections
{ {
var base = core.zm.rotationY(self.yaw + core.radians(180.0)); // var base = core.zm.rotationY(self.yaw + core.radians(180.0));
base = mul(base, core.zm.rotationX(self.pitch)); // base = mul(base, core.zm.rotationX(self.pitch));
base = mul(base, core.zm.rotationZ(self.roll)); // base = mul(base, core.zm.rotationZ(self.roll));
self.transform = base; self.transform = core.zm.identity();
self.transform = mul(zm.translationV(position.fmul(-1).toZm()), self.transform); self.transform = mul(zm.translationV(position.toZm()), self.transform);
self.final = mul(self.transform, self.projection); self.final = mul(self.transform, self.projection);
core.engine_log("{any}", .{self.final}); // core.engine_log("{any}", .{self.final});
self.finalAlt = mul(self.transform, self.projectionAlt); self.finalAlt = mul(self.transform, self.projectionAlt);
} }
} }
fn makePerspective(fov: f32, aspect: f32, near: f32, far: f32) Mat { fn makePerspective(fov: f32, aspect: f32, near: f32, far: f32) Mat {
const proj = core.zm.perspectiveFovRh( const proj = core.zm.perspectiveFovLh(
core.radians(fov), core.radians(fov),
aspect, aspect,
near, near,

View File

@ -1,5 +0,0 @@
fov: f32 = 70.0,
altFov: f32 = 70.0,
aspect: f32 = 16.0 / 9.0,
near_clipping: f32 = 0.1,
far_clipping: f32 = 10000.0,

View File

@ -66,7 +66,7 @@ pub const Renderer = struct {
var gci = std.mem.zeroes(gpu.GPUTextureCreateInfo); var gci = std.mem.zeroes(gpu.GPUTextureCreateInfo);
gci.type = .texturetype2d; gci.type = .texturetype2d;
gci.format = .textureformatD16Unorm; gci.format = .textureformatD32Float;
gci.width = @intCast(self.scissor.w); gci.width = @intCast(self.scissor.w);
gci.height = @intCast(self.scissor.h); gci.height = @intCast(self.scissor.h);
gci.layer_count_or_depth = 1; gci.layer_count_or_depth = 1;
@ -158,7 +158,7 @@ pub const Renderer = struct {
pci.depth_stencil_state.write_mask = 0xff; pci.depth_stencil_state.write_mask = 0xff;
pci.target_info.has_depth_stencil_target = true; pci.target_info.has_depth_stencil_target = true;
pci.target_info.depth_stencil_format = .textureformatD16Unorm; pci.target_info.depth_stencil_format = .textureformatD32Float;
pci.target_info.num_color_targets = 1; pci.target_info.num_color_targets = 1;
pci.target_info.color_target_descriptions = &[_]gpu.GPUColorTargetDescription{ pci.target_info.color_target_descriptions = &[_]gpu.GPUColorTargetDescription{
.{ .{
@ -223,7 +223,7 @@ pub const Renderer = struct {
pci.target_info.num_color_targets = 1; pci.target_info.num_color_targets = 1;
pci.target_info.has_depth_stencil_target = true; pci.target_info.has_depth_stencil_target = true;
pci.target_info.depth_stencil_format = .textureformatD16Unorm; pci.target_info.depth_stencil_format = .textureformatD32Float;
pci.target_info.color_target_descriptions = &[_]gpu.GPUColorTargetDescription{ pci.target_info.color_target_descriptions = &[_]gpu.GPUColorTargetDescription{
.{ .{
.format = self.device.getGPUSwapchainTextureFormat(self.window), .format = self.device.getGPUSwapchainTextureFormat(self.window),
@ -359,6 +359,8 @@ pub const Renderer = struct {
depthTarget.texture = self.depthTexture; depthTarget.texture = self.depthTexture;
depthTarget.load_op = .loadopClear; depthTarget.load_op = .loadopClear;
depthTarget.store_op = .storeopDontCare; depthTarget.store_op = .storeopDontCare;
depthTarget.stencil_load_op = .loadopDontCare;
depthTarget.stencil_store_op = .storeopDontCare;
depthTarget.clear_depth = 1.0; depthTarget.clear_depth = 1.0;
depthTarget.clear_stencil = 0; depthTarget.clear_stencil = 0;
@ -435,6 +437,13 @@ const ShaderLoadArgs = sdl3.shaderTypes.ShaderLoadArgs;
pub const mesh_pool = @import("mesh-pool.zig"); pub const mesh_pool = @import("mesh-pool.zig");
pub const MeshPool = mesh_pool.MeshPool; pub const MeshPool = mesh_pool.MeshPool;
// debug api
pub fn reloadShaders() !void {
_ = try core.shell.runCmd(gRenderer.allocator, &.{ "python", "../tools/scripts/cookShaders.py" }, ".");
try gRenderer.createMeshPipeline();
}
// ====== renderer API ======= // ====== renderer API =======
pub fn start() !void { pub fn start() !void {
gRenderer = try core.createObject(Renderer, .{ .can_tick = true, .isCore = true }); gRenderer = try core.createObject(Renderer, .{ .can_tick = true, .isCore = true });

View File

@ -87,16 +87,18 @@ pub const ShellProcess = struct {
} }
}; };
pub fn runCmd(allocator: std.mem.allocator, cmd: []const []const u8, cwd: []const u8) ![]const u8 { pub fn runCmd(allocator: std.mem.Allocator, cmd: []const []const u8, cwd: []const u8) ![]u8 {
var proc: ChildProcess = ChildProcess.init(cmd, allocator); var proc: ChildProcess = ChildProcess.init(cmd, allocator);
proc.cwd = cwd; proc.cwd = cwd;
// const result = try std.process.Child.run(.{ const result = try std.process.Child.run(.{
// .argv = cmd, .argv = cmd,
// .allocator = allocator, .allocator = allocator,
// .cwd = p2.getFolder(cwd), .cwd = p2.getFolder(cwd),
// .max_output_bytes = 150 * 1024 * 1024, .max_output_bytes = 150 * 1024 * 1024,
// }); });
// return result.; allocator.free(result.stdout);
return result.stdout;
} }

View File

@ -35,7 +35,7 @@ vertex main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniform
{ {
main0_out out = {}; main0_out out = {};
out.out_var_TEXCOORD0 = in.in_var_TEXCOORD3; out.out_var_TEXCOORD0 = in.in_var_TEXCOORD3;
out.gl_Position = transpose(transpose(Uniforms.ViewProjection)) * (transpose(transpose(scene._m0[gl_InstanceIndex].Model)) * float4(in.in_var_TEXCOORD0, 1.0)); out.gl_Position = Uniforms.ViewProjection * (scene._m0[gl_InstanceIndex].Model * float4(in.in_var_TEXCOORD0, 1.0));
return out; return out;
} }

View File

@ -51,10 +51,13 @@ pub fn prepare(self: *@This()) !void {
_ = self.camera.addComponent(core.Scene).?; _ = self.camera.addComponent(core.Scene).?;
const camera = self.camera.addComponent(rend.CameraComponent).?; const camera = self.camera.addComponent(rend.CameraComponent).?;
const lostEmpire = try core.createEntity(); {
_ = lostEmpire.addComponent(core.Scene); const lostEmpire = try core.createEntity();
const empireMesh = lostEmpire.addComponent(rend.MeshComponent).?; const scene = lostEmpire.addComponent(core.Scene).?;
empireMesh.setMesh("m_empire"); scene.setPosition(.{ .y = -20 });
const empireMesh = lostEmpire.addComponent(rend.MeshComponent).?;
empireMesh.setMesh("m_empire");
}
rend.setActiveCamera(camera); rend.setActiveCamera(camera);
@ -74,6 +77,21 @@ pub fn prepare(self: *@This()) !void {
_ = verticalInput.data.addListener(self, verticalMovement); _ = verticalInput.data.addListener(self, verticalMovement);
verticalInput.activate(); verticalInput.activate();
{
const shaderReload = try core.ActionBinding.create(core.MakeName("shaderReload"));
shaderReload.addKey(.r, .keyDown);
_ = shaderReload.data.addListener(null, onShaderReload);
shaderReload.activate();
}
}
fn onShaderReload(ctx: ?*anyopaque, action: core.ActionEvent) void {
_ = ctx;
_ = action;
core.engine_log("reloading shaders, this is gonna cause some leaks but thats ok", .{});
rend.renderer.reloadShaders() catch unreachable;
} }
fn verticalMovement(ctx: ?*anyopaque, axis: f32) void { fn verticalMovement(ctx: ?*anyopaque, axis: f32) void {