pass view_projection matrix directly to the shaders instead of constructing it each time

This commit is contained in:
Sven Balzer
2025-03-25 17:49:30 +01:00
parent b48b5f0339
commit d71daba073
8 changed files with 668 additions and 842 deletions
+44 -53
View File
@@ -53,11 +53,15 @@ static SDL_GPUTexture *msaa_texture;
static bool Running = true;
static M4x4 view_matrix = view (V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
static M4x4 inverse_view_matrix = inverse_view(V3_(0.0f, 0.0f, 0.0f), radians(45.0f), 10.0f);
static float camera_fovy_degrees = 31.0f;
static float camera_tilt = 25.5f;
static float camera_distance = 13.5f;
static M4x4 projection_matrix = projection (radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
static M4x4 inverse_projection_matrix = inverse_projection(radians(45.0f), 16.0f / 9.0f, NEAR_PLANE);
static M4x4 view_matrix = view (V3_(0.0f, 0.0f, 0.0f), radians(camera_tilt), camera_distance);
static M4x4 inverse_view_matrix = inverse_view(V3_(0.0f, 0.0f, 0.0f), radians(camera_tilt), camera_distance);
static M4x4 projection_matrix = projection (radians(camera_fovy_degrees), 16.0f / 9.0f, NEAR_PLANE);
static M4x4 inverse_projection_matrix = inverse_projection(radians(camera_fovy_degrees), 16.0f / 9.0f, NEAR_PLANE);
static SDL_Time time;
@@ -164,25 +168,12 @@ struct Player {
static Player player;
struct PerFrame {
float aspect_ratio;
float fovy_degrees;
float camera_x;
float camera_y;
float camera_distance;
float camera_tilt;
Sint32 drag_start[2];
Sint32 mouse[2];
Uint32 map_width;
};
static PerFrame per_frame = {
.aspect_ratio = 16.0f / 9.0f,
.fovy_degrees = 31.0f,
.camera_x = 0.0f,
.camera_y = 0.0f,
.camera_distance = 13.5f,
.camera_tilt = 25.5f,
};
static PerFrame per_frame = {};
typedef struct {
Uint16 type;
@@ -707,7 +698,7 @@ static bool recreate_graphics_pipelines() {
.format = SDL_GPU_SHADERFORMAT_SPIRV,
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
.num_uniform_buffers = 1,
.num_uniform_buffers = 2,
};
SDL_GPUShader *basic_vertex_shader = SDL_CreateGPUShader(device, &basic_vertex_shader_info);
@@ -823,7 +814,7 @@ static bool recreate_graphics_pipelines() {
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
.num_storage_buffers = 1,
.num_uniform_buffers = 1,
.num_uniform_buffers = 2,
};
SDL_GPUShader *world_vertex_shader = SDL_CreateGPUShader(device, &world_vertex_shader_info);
@@ -1031,7 +1022,7 @@ static bool recreate_graphics_pipelines() {
.format = SDL_GPU_SHADERFORMAT_SPIRV,
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
.num_uniform_buffers = 1,
.num_uniform_buffers = 2,
};
SDL_GPUShader *grid_vertex_shader = SDL_CreateGPUShader(device, &grid_vertex_shader_info);
@@ -1471,9 +1462,9 @@ int main(int argc, char **argv) {
if (show_settings) {
ImGui::SetNextWindowSize(ImVec2(400, 0), ImGuiCond_FirstUseEver);
if (ImGui::Begin("Settings", &show_settings)) {
ImGui::DragFloat("fovy", &per_frame.fovy_degrees);
ImGui::DragFloat("camera_distance", &per_frame.camera_distance, 0.25f, 1.0f, INFINITY);
ImGui::DragFloat("camera_tilt", &per_frame.camera_tilt, 0.25f, 0.0f, 89.0f);
ImGui::DragFloat("fovy", &camera_fovy_degrees);
ImGui::DragFloat("camera_distance", &camera_distance, 0.25f, 1.0f, INFINITY);
ImGui::DragFloat("camera_tilt", &camera_tilt, 0.25f, 0.0f, 89.0f);
if (ImGui::Checkbox("MSAA", &enable_msaa)) {
recreate_graphics_pipelines();
@@ -1836,26 +1827,25 @@ int main(int argc, char **argv) {
update_buffer(player_instance_buffer, 0, sizeof(player_instance), &player_instance);
}
{
ZoneScopedN("matrices");
view_matrix = view (V3_((float)player.pos_x, (float)player.pos_y, 0), radians(per_frame.camera_tilt), per_frame.camera_distance);
inverse_view_matrix = inverse_view(V3_((float)player.pos_x, (float)player.pos_y, 0), radians(per_frame.camera_tilt), per_frame.camera_distance);
projection_matrix = projection (radians(per_frame.fovy_degrees), per_frame.aspect_ratio, NEAR_PLANE);
inverse_projection_matrix = inverse_projection(radians(per_frame.fovy_degrees), per_frame.aspect_ratio, NEAR_PLANE);
}
{
ZoneScopedN("per_frame");
float aspect_ratio = ((float) window_width / (float) window_height);
view_matrix = view (V3_((float)player.pos_x, (float)player.pos_y, 0), radians(camera_tilt), camera_distance);
inverse_view_matrix = inverse_view(V3_((float)player.pos_x, (float)player.pos_y, 0), radians(camera_tilt), camera_distance);
projection_matrix = projection (radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
inverse_projection_matrix = inverse_projection(radians(camera_fovy_degrees), aspect_ratio, NEAR_PLANE);
M4x4 view_projection_matrix = projection_matrix * view_matrix;
SDL_PushGPUVertexUniformData(command_buffer, 0, &view_projection_matrix, sizeof(view_projection_matrix));
V2 floor_intersection = get_floor_intersection_of_mouse(mouse_pos);
Sint32 tile_x = roundf(floor_intersection.x);
Sint32 tile_y = roundf(floor_intersection.y);
per_frame.aspect_ratio = ((float) window_width / (float) window_height);
per_frame.map_width = map_width;
per_frame.camera_x = player.pos_x;
per_frame.camera_y = player.pos_y;
per_frame.mouse[0] = tile_x;
per_frame.mouse[1] = tile_y;
@@ -1867,7 +1857,7 @@ int main(int argc, char **argv) {
per_frame.drag_start[1] = tile_y;
}
SDL_PushGPUVertexUniformData(command_buffer, 0, &per_frame, sizeof(per_frame));
SDL_PushGPUVertexUniformData(command_buffer, 1, &per_frame, sizeof(per_frame));
}
{
@@ -1930,6 +1920,22 @@ int main(int argc, char **argv) {
SDL_DrawGPUIndexedPrimitives(render_pass, 6, map_height * map_width, 0, 0, 0);
}
{ // Draw Player
ZoneScopedN("Draw Player");
SDL_GPUBufferBinding vertex_buffers[] = {
{ .buffer = vertex_buffer, .offset = 0 },
{ .buffer = player_instance_buffer, .offset = 0 },
};
SDL_GPUTextureSamplerBinding texture_bindings[] = {
{ .texture = player_texture, .sampler = pixel_sampler },
};
SDL_BindGPUGraphicsPipeline(render_pass, basic_graphics_pipeline);
SDL_BindGPUVertexBuffers(render_pass, 0, vertex_buffers, SDL_arraysize(vertex_buffers));
SDL_BindGPUFragmentSamplers(render_pass, 0, texture_bindings, SDL_arraysize(texture_bindings));
SDL_DrawGPUIndexedPrimitives(render_pass, 6, 1, 0, 0, 0);
}
if (show_tile_picker) { // Draw Grid
ZoneScopedN("Draw Grid");
SDL_GPUBufferBinding index_buffer_binding = { .buffer = grid_index_buffer, .offset = 0 };
@@ -1948,22 +1954,7 @@ int main(int argc, char **argv) {
SDL_PushGPUFragmentUniformData(command_buffer, 0, &tints, sizeof(tints));
SDL_DrawGPUIndexedPrimitives(render_pass, SDL_arraysize(grid_indices), map_height * map_width, 0, 0, 0);
}
{ // Draw Player
ZoneScopedN("Draw Player");
SDL_GPUBufferBinding vertex_buffers[] = {
{ .buffer = vertex_buffer, .offset = 0 },
{ .buffer = player_instance_buffer, .offset = 0 },
};
SDL_GPUTextureSamplerBinding texture_bindings[] = {
{ .texture = player_texture, .sampler = pixel_sampler },
};
SDL_BindGPUGraphicsPipeline(render_pass, basic_graphics_pipeline);
SDL_BindGPUVertexBuffers(render_pass, 0, vertex_buffers, SDL_arraysize(vertex_buffers));
SDL_BindGPUFragmentSamplers(render_pass, 0, texture_bindings, SDL_arraysize(texture_bindings));
SDL_DrawGPUIndexedPrimitives(render_pass, 6, 1, 0, 0, 0);
}
SDL_EndGPURenderPass(render_pass);
{