> 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 weights : TEXCOORD5;
uint InstanceIndex : SV_InstanceID;
uint Instance : SV_InstanceID;
};
struct Output
@ -30,13 +30,14 @@ cbuffer Uniforms : register(b0, space1)
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 output;
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;
}

View File

@ -32,18 +32,21 @@ pub fn initECS(self: *@This(), handle: core.SetHandle) void {
self.entity = core.Entity.fromHandle(handle);
}
pub fn updateProjections(self: *@This()) core.Transform {
self.projection = zm.perspectiveFovRh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000);
self.projection[1][1] *= -1;
pub fn updateProjections(self: *@This()) void {
self.projection = zm.perspectiveFovLh(core.radians(self.fov), 16.0 / 9.0, 0.1, 200000);
// 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;
}
pub fn resolve(self: *@This()) void {
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();
base = mul(core.zm.rotationY(-self.yaw), base);
@ -59,20 +62,20 @@ pub fn resolve(self: *@This()) void {
// calculate viewProjections
{
var base = core.zm.rotationY(self.yaw + core.radians(180.0));
base = mul(base, core.zm.rotationX(self.pitch));
base = mul(base, core.zm.rotationZ(self.roll));
self.transform = base;
// var base = core.zm.rotationY(self.yaw + core.radians(180.0));
// base = mul(base, core.zm.rotationX(self.pitch));
// base = mul(base, core.zm.rotationZ(self.roll));
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);
core.engine_log("{any}", .{self.final});
// core.engine_log("{any}", .{self.final});
self.finalAlt = mul(self.transform, self.projectionAlt);
}
}
fn makePerspective(fov: f32, aspect: f32, near: f32, far: f32) Mat {
const proj = core.zm.perspectiveFovRh(
const proj = core.zm.perspectiveFovLh(
core.radians(fov),
aspect,
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);
gci.type = .texturetype2d;
gci.format = .textureformatD16Unorm;
gci.format = .textureformatD32Float;
gci.width = @intCast(self.scissor.w);
gci.height = @intCast(self.scissor.h);
gci.layer_count_or_depth = 1;
@ -158,7 +158,7 @@ pub const Renderer = struct {
pci.depth_stencil_state.write_mask = 0xff;
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.color_target_descriptions = &[_]gpu.GPUColorTargetDescription{
.{
@ -223,7 +223,7 @@ pub const Renderer = struct {
pci.target_info.num_color_targets = 1;
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{
.{
.format = self.device.getGPUSwapchainTextureFormat(self.window),
@ -359,6 +359,8 @@ pub const Renderer = struct {
depthTarget.texture = self.depthTexture;
depthTarget.load_op = .loadopClear;
depthTarget.store_op = .storeopDontCare;
depthTarget.stencil_load_op = .loadopDontCare;
depthTarget.stencil_store_op = .storeopDontCare;
depthTarget.clear_depth = 1.0;
depthTarget.clear_stencil = 0;
@ -435,6 +437,13 @@ const ShaderLoadArgs = sdl3.shaderTypes.ShaderLoadArgs;
pub const mesh_pool = @import("mesh-pool.zig");
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 =======
pub fn start() !void {
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);
proc.cwd = cwd;
// const result = try std.process.Child.run(.{
// .argv = cmd,
// .allocator = allocator,
// .cwd = p2.getFolder(cwd),
// .max_output_bytes = 150 * 1024 * 1024,
// });
const result = try std.process.Child.run(.{
.argv = cmd,
.allocator = allocator,
.cwd = p2.getFolder(cwd),
.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 = {};
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;
}

View File

@ -51,10 +51,13 @@ pub fn prepare(self: *@This()) !void {
_ = self.camera.addComponent(core.Scene).?;
const camera = self.camera.addComponent(rend.CameraComponent).?;
const lostEmpire = try core.createEntity();
_ = lostEmpire.addComponent(core.Scene);
const empireMesh = lostEmpire.addComponent(rend.MeshComponent).?;
empireMesh.setMesh("m_empire");
{
const lostEmpire = try core.createEntity();
const scene = lostEmpire.addComponent(core.Scene).?;
scene.setPosition(.{ .y = -20 });
const empireMesh = lostEmpire.addComponent(rend.MeshComponent).?;
empireMesh.setMesh("m_empire");
}
rend.setActiveCamera(camera);
@ -74,6 +77,21 @@ pub fn prepare(self: *@This()) !void {
_ = verticalInput.data.addListener(self, verticalMovement);
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 {