From 2cf4f6832d82d6153708f14b92f337fae798a170 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 4 Oct 2025 14:09:47 +0100 Subject: [PATCH] Pre-multiply V and P matrices --- floor-vert.glsl | 5 ++--- gpu.c | 8 ++++---- gpu.h | 7 +------ main.c | 21 ++++++++++++--------- model-vert.glsl | 5 ++--- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/floor-vert.glsl b/floor-vert.glsl index 70a4cf4..cdf2e5d 100644 --- a/floor-vert.glsl +++ b/floor-vert.glsl @@ -3,8 +3,7 @@ layout(location = 0) out vec3 frag_normal; layout(push_constant) uniform vp_t { - mat4 view; - mat4 proj; + mat4 transform; } vp; vec2 vertices[4] = vec2[]( @@ -16,6 +15,6 @@ vec2 vertices[4] = vec2[]( void main() { const vec2 pos = 3 * vertices[gl_VertexIndex]; - gl_Position = vp.proj * vp.view * vec4(pos.x, 0, pos.y, 1); + gl_Position = vp.transform * vec4(pos.x, 0, pos.y, 1); frag_normal = vec3(0, 1, 0); } diff --git a/gpu.c b/gpu.c index 49ddd58..b265459 100644 --- a/gpu.c +++ b/gpu.c @@ -496,7 +496,7 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name) const VkPushConstantRange push_constant_range = { .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, .offset = 0, - .size = sizeof(vp_t), + .size = sizeof(mat4_t), }; // Load vertex and fragment shader modules @@ -956,7 +956,7 @@ void gpu_model_cleanup(const gpu_ctx_t *ctx, const gpu_model_t *model) } void gpu_render( - const gpu_ctx_t *ctx, vp_t vp, size_t model_count, + const gpu_ctx_t *ctx, mat4_t vp, size_t model_count, const gpu_model_t *models, const mat4_t **bone_matrices, const mat4_t *model_transforms) { @@ -1001,7 +1001,7 @@ void gpu_render( ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->floor_pipeline); vkCmdPushConstants( ctx->cmd_buf, ctx->model_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, - 0, sizeof(vp_t), &vp); + 0, sizeof(mat4_t), &vp); vkCmdDraw(ctx->cmd_buf, 4, 1, 0, 0); // Draw models @@ -1009,7 +1009,7 @@ void gpu_render( ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->model_pipeline); vkCmdPushConstants( ctx->cmd_buf, ctx->model_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, - 0, sizeof(vp_t), &vp); + 0, sizeof(mat4_t), &vp); for (unsigned i = 0; i < model_count; ++i) { models[i].transform_ptr[curr_img] = model_transforms[i]; memcpy( diff --git a/gpu.h b/gpu.h index 236163e..c4f7e10 100644 --- a/gpu.h +++ b/gpu.h @@ -9,11 +9,6 @@ #define MAX_FRAMES_IN_FLIGHT 4 -typedef struct { - mat4_t view; - mat4_t proj; -} vp_t; - typedef struct { VkInstance inst; VkSurfaceKHR surface; @@ -73,7 +68,7 @@ void gpu_model_load( void gpu_model_cleanup(const gpu_ctx_t *ctx, const gpu_model_t *model); void gpu_render( - const gpu_ctx_t *ctx, vp_t vp, size_t model_count, + const gpu_ctx_t *ctx, mat4_t vp, size_t model_count, const gpu_model_t *models, const mat4_t **bone_matrices, const mat4_t *model_transforms); diff --git a/main.c b/main.c index 28851a8..70e963d 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,7 @@ #define DT (1'000'000 / 60) // Assume 60 Hz for now -#define LIL_GUY_COUNT 2 +#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) @@ -59,11 +59,10 @@ int main(int argc, char *argv[]) const vec3_t cam = { 0, 4, -8 }; const vec3_t tgt = { 0, 0, 0 }; - const vp_t vp = { - .view = mat4_lookat(cam, tgt, (vec3_t) { 0, 1, 0 }), - .proj = mat4_perspective((float)W / H, PI / 8, 0.1, 100.0), - }; - mat4_t transforms[] = { + const mat4_t view = mat4_lookat(cam, tgt, (vec3_t) { 0, 1, 0 }); + const mat4_t proj = mat4_perspective((float)W / H, PI / 8, 0.1, 100.0); + const mat4_t vp = mat4_mul(proj, view); + mat4_t transforms[NELEMS(gpu_models)] = { mat4_mul( MAT4_TRANSLATE(1, 0, 0), (mat4_t) { @@ -96,9 +95,13 @@ int main(int argc, char *argv[]) animation_update(&anim, &model, DT); - const mat4_t *bone_matrices[] - = { anim.bone_matrices, anim.bone_matrices }; - gpu_render(&gpu, vp, 2, gpu_models, bone_matrices, transforms); + const mat4_t *bone_matrices[NELEMS(gpu_models)] = { + anim.bone_matrices, + anim.bone_matrices, + }; + gpu_render( + &gpu, vp, NELEMS(gpu_models), gpu_models, bone_matrices, + transforms); } // Clean up diff --git a/model-vert.glsl b/model-vert.glsl index d346515..c146e86 100644 --- a/model-vert.glsl +++ b/model-vert.glsl @@ -8,8 +8,7 @@ layout(location = 3) in vec4 weights; layout(location = 0) out vec3 frag_normal; layout(push_constant) uniform vp_t { - mat4 view; - mat4 proj; + mat4 transform; } vp; layout(binding = 0) uniform transform_t { @@ -26,7 +25,7 @@ void main() { + weights.y * bones.matrices[bone_indices.y] + weights.z * bones.matrices[bone_indices.z] + weights.w * bones.matrices[bone_indices.w]; - gl_Position = vp.proj * vp.view * transform.model * skinning_matrix + gl_Position = vp.transform * transform.model * skinning_matrix * vec4(position, 1.0); frag_normal = transpose( inverse(mat3(transform.model * skinning_matrix))) * normal;