diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-23 01:11:28 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-06-23 01:11:28 +1000 |
commit | 17f0db6607534c5bf1ba96153fabd3fdbb399ed9 (patch) | |
tree | c6531c6c688e9719817ac4c06127affc1313f5d3 | |
parent | d20356fd426ccea1866fbda798864a378303bbbd (diff) |
wip: debug quad shader
-rw-r--r-- | assets/shaders/debug_quad.frag | 12 | ||||
-rw-r--r-- | assets/shaders/debug_quad.vert | 13 | ||||
-rw-r--r-- | assets/shaders/shadows.vert | 6 | ||||
-rw-r--r-- | examples/shadow_maps/ex_shadow_maps.c | 83 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 1 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.h | 1 | ||||
-rw-r--r-- | src/renderer/ral.c | 20 | ||||
-rw-r--r-- | src/renderer/ral.h | 2 | ||||
-rw-r--r-- | src/renderer/renderpasses.c | 44 | ||||
-rw-r--r-- | src/renderer/renderpasses.h | 19 |
10 files changed, 160 insertions, 41 deletions
diff --git a/assets/shaders/debug_quad.frag b/assets/shaders/debug_quad.frag new file mode 100644 index 0000000..ac7842d --- /dev/null +++ b/assets/shaders/debug_quad.frag @@ -0,0 +1,12 @@ +#version 410 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D depthMap; + +void main() +{ + float depthValue = texture(depthMap, TexCoords).r; + FragColor = vec4(vec3(depthValue), 1.0); // orthographic +}
\ No newline at end of file diff --git a/assets/shaders/debug_quad.vert b/assets/shaders/debug_quad.vert new file mode 100644 index 0000000..8b8201c --- /dev/null +++ b/assets/shaders/debug_quad.vert @@ -0,0 +1,13 @@ +#version 410 core +layout (location = 0) in vec3 inPosition; +layout (location = 1) in vec3 inNormal; +layout (location = 2) in vec2 inTexCoords; + +out vec2 TexCoords; + +void main() +{ + TexCoords = inTexCoords; + vec2 xy = inPosition.xz; + gl_Position = vec4(xy, 0.0, 1.0); +} diff --git a/assets/shaders/shadows.vert b/assets/shaders/shadows.vert index 00b1a81..1d1c59a 100644 --- a/assets/shaders/shadows.vert +++ b/assets/shaders/shadows.vert @@ -8,12 +8,12 @@ layout(location = 2) in vec2 inTexCoords; // Uniforms uniform Model { mat4 mat; -}; +} model; uniform LightSpace { mat4 mat; -}; +} lightSpace; void main() { - gl_Position = LightSpace.mat * Model.mat * vec4(inPosition, 1.0); + gl_Position = lightSpace.mat * model.mat * vec4(inPosition, 1.0); } diff --git a/examples/shadow_maps/ex_shadow_maps.c b/examples/shadow_maps/ex_shadow_maps.c index 3b647ee..30df618 100644 --- a/examples/shadow_maps/ex_shadow_maps.c +++ b/examples/shadow_maps/ex_shadow_maps.c @@ -1,7 +1,9 @@ #include "celeritas.h" +#include "maths.h" #include "maths_types.h" #include "primitives.h" #include "ral.h" +#include "ral_types.h" #include "render.h" #include "render_types.h" #include "renderpasses.h" @@ -9,19 +11,17 @@ extern core g_core; // Scene / light setup -const vec3 pointlight_positions[4] = { - { -10.0, 10.0, 10.0 }, - { 10.0, 10.0, 10.0 }, - { -10.0, -10.0, 10.0 }, - { 10.0, -10.0, 10.0 }, -}; -point_light point_lights[4]; +vec3 light_position = { -2, 4, -1 }; +mesh s_scene[5]; +transform s_transforms[5]; /* TODO: - keyboard button to switch between main camera and light camera */ +void draw_scene(); + int main() { core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); @@ -31,19 +31,27 @@ int main() { camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); ren_shadowmaps shadows = { .width = 1000, .height = 1000 }; - // ren_shadowmaps_init(&shadows); + ren_shadowmaps_init(&shadows); // Set up the scene // We want: // 1. a ground plane // 2. lights // 3. some boxes - - mesh scene[5]; for (int i = 0; i < 4; i++) { geometry_data geo = geo_create_cuboid(f32x3(2, 2, 2)); - cubes[i] = mesh_create(&geo, true); + s_scene[i] = mesh_create(&geo, true); + s_transforms[i] = transform_create(vec3(4 * i, 0, 0), quat_ident(), 1.0); } + geometry_data plane = geo_create_plane(f32x2(20, 20)); + s_scene[4] = mesh_create(&plane, true); + + geometry_data quad_geo = geo_create_plane(f32x2(2,2)); + mesh quad = mesh_create(&quad_geo, true); + + shader_data model_data = { .data = NULL, .shader_data_get_layout = &model_uniform_layout }; + shader_data lightspace_data = { .data = NULL, + .shader_data_get_layout = &lightspace_uniform_layout }; // Main loop while (!should_exit(&g_core)) { @@ -55,32 +63,37 @@ int main() { gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); gpu_cmd_encoder_begin_render(enc, shadows.rpass); + + // calculations + f32 near_plane = 1.0, far_plane = 7.5; + mat4 light_projection = mat4_orthographic(-10.0, 10.0, -10.0, 10.0, near_plane, far_plane); + mat4 light_view = mat4_look_at(light_position, VEC3_ZERO, VEC3_Y); + mat4 light_space_matrix = mat4_mult(light_view, light_projection); + lightspace_tf_uniform lsu = { .lightSpaceMatrix = light_space_matrix }; + encode_bind_pipeline(enc, PIPELINE_GRAPHICS, shadows.static_pipeline); - for (int i = 0; i < 4; i++) { - encode_set_vertex_buffer(enc, cubes[i].vertex_buffer); - encode_set_index_buffer(enc, cubes[i].index_buffer); - encode_draw_indexed(enc, cubes[i].geometry->indices->len); - } - gpu_cmd_encoder_end_render(enc); + lightspace_data.data = &lsu; + encode_bind_shader_data(enc, 0, &lightspace_data); - // gpu_cmd_encoder_begin_render(enc, static_opaque_rpass); + draw_scene(); gpu_cmd_encoder_end_render(enc); - /* - Shadows - render scene into texture + gpu_cmd_encoder_begin_render(enc, shadows.debug_quad->renderpass); + encode_bind_pipeline(enc, PIPELINE_GRAPHICS, shadows.debug_quad); + debug_quad_uniform dqu = {.depthMap = shadows.depth_tex}; + shader_data debug_quad_data = { .data = &dqu, .shader_data_get_layout = debug_quad_layout}; + encode_bind_shader_data(enc, 0, &debug_quad_data); + encode_set_vertex_buffer(enc, quad.vertex_buffer); + encode_set_index_buffer(enc, quad.index_buffer); + encode_draw_indexed(enc, quad.geometry->indices->len); + + gpu_cmd_encoder_end_render(enc); - begin_renderpass() - bind_pipeline() - upload shader data - for each object: - - set buffers - - draw call + // gpu_cmd_encoder_begin_render(enc, static_opaque_rpass); - end_renderpass() - */ + // gpu_cmd_encoder_end_render(enc); gpu_backend_end_frame(); } @@ -89,3 +102,15 @@ int main() { return 0; } + +void draw_scene() { + gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); + for (int i = 0; i < 5; i++) { + model_uniform mu = { .model = transform_to_mat(&s_transforms[i]) }; + shader_data model_data = { .data = &mu, .shader_data_get_layout = model_uniform_layout }; + encode_bind_shader_data(enc, 0, &model_data); + encode_set_vertex_buffer(enc, s_scene[i].vertex_buffer); + encode_set_index_buffer(enc, s_scene[i].index_buffer); + encode_draw_indexed(enc, s_scene[i].geometry->indices->len); + } +} diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index 00b41b2..7d8aa01 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -120,6 +120,7 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip } } + pipeline->renderpass = description.renderpass; pipeline->wireframe = description.wireframe; return pipeline; diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h index 9cfbb06..a5a9140 100644 --- a/src/renderer/backends/opengl/backend_opengl.h +++ b/src/renderer/backends/opengl/backend_opengl.h @@ -21,6 +21,7 @@ typedef struct gpu_pipeline_layout { } gpu_pipeline_layout; typedef struct gpu_pipeline { u32 shader_id; + gpu_renderpass* renderpass; vertex_description vertex_desc; buffer_handle uniform_bindings[MAX_PIPELINE_UNIFORM_BUFFERS]; bool wireframe; diff --git a/src/renderer/ral.c b/src/renderer/ral.c index e73843b..a52d605 100644 --- a/src/renderer/ral.c +++ b/src/renderer/ral.c @@ -1,4 +1,8 @@ #include "ral.h" +#include "file.h" +#include "log.h" +#include "str.h" +#include "mem.h" #if defined(CEL_REND_BACKEND_VULKAN) #include "backend_vulkan.h" @@ -75,3 +79,19 @@ void print_shader_binding(shader_binding b) { printf("Binding name: %s type %s vis %d stores data %d\n", b.label, shader_binding_type_name[b.type], b.vis, b.stores_data); } + +shader_desc shader_quick_load(const char* filepath) { + arena a = arena_create(malloc(1024 * 1024), 1024 * 1024); + str8 path = str8_cstr_view(filepath); + str8_opt shader = str8_from_file(&a, path); + if (!shader.has_value) { + ERROR_EXIT("Failed to load shaders from disk"); + } + + return (shader_desc) { + .debug_name = filepath, + .code = shader.contents, + .filepath = path, + .is_spirv = true, + }; +}
\ No newline at end of file diff --git a/src/renderer/ral.h b/src/renderer/ral.h index ab62679..9d6ed41 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -76,6 +76,8 @@ typedef struct shader_desc { bool is_combined_vert_frag; // Contains both vertex and fragment stages } shader_desc; +shader_desc shader_quick_load(const char* filepath); + struct graphics_pipeline_desc { const char* debug_name; vertex_description vertex_desc; diff --git a/src/renderer/renderpasses.c b/src/renderer/renderpasses.c index e005c14..5193a1c 100644 --- a/src/renderer/renderpasses.c +++ b/src/renderer/renderpasses.c @@ -11,6 +11,7 @@ #include "renderpasses.h" #include "file.h" +#include "log.h" #include "maths_types.h" #include "ral.h" #include "ral_types.h" @@ -18,9 +19,41 @@ #define SHADOW_WIDTH 1000 #define SHADOW_HEIGHT 1000 +shader_data_layout debug_quad_layout(void* data) { + debug_quad_uniform* d = data; + bool has_data = data != NULL; + + shader_binding b1 = { .label = "depthMap", + .type = SHADER_BINDING_TEXTURE, + .stores_data = has_data}; + if (has_data) { + b1.data.texture.handle = d->depthMap; + } + return (shader_data_layout){ .name = "debug quad uniforms", .bindings = { b1 }, .bindings_count = 1 }; +} + +gpu_pipeline* debug_quad_pipeline_create() { + gpu_renderpass_desc rpass_desc = {.default_framebuffer = true }; + gpu_renderpass* rpass = gpu_renderpass_create(&rpass_desc); + shader_data shader_layout = {.data = NULL, .shader_data_get_layout = debug_quad_layout}; + struct graphics_pipeline_desc desc = { + .debug_name = "Shadow maps debug quad", + .vertex_desc = static_3d_vertex_description(), + .data_layouts = { shader_layout }, + .data_layouts_count = 1, + .vs = shader_quick_load("assets/shaders/debug_quad.vert"), + .fs = shader_quick_load("assets/shaders/debug_quad.frag"), + .renderpass = rpass, + }; + + return gpu_graphics_pipeline_create(desc); +} + void ren_shadowmaps_init(ren_shadowmaps* storage) { storage->rpass = shadowmaps_renderpass_create(); storage->static_pipeline = shadowmaps_pipeline_create(storage->rpass); + storage->debug_quad = debug_quad_pipeline_create(); + storage->depth_tex = storage->rpass->description.depth_stencil; } gpu_renderpass* shadowmaps_renderpass_create() { @@ -39,13 +72,6 @@ gpu_renderpass* shadowmaps_renderpass_create() { } // == shader bindings -typedef struct model_uniform { - mat4 model; -} model_uniform; - -typedef struct lightspace_tf_uniform { - mat4 lightSpaceMatrix; -} lightspace_tf_uniform; shader_data_layout model_uniform_layout(void* data) { bool has_data = data != NULL; @@ -101,8 +127,8 @@ gpu_pipeline* shadowmaps_pipeline_create(gpu_renderpass* rpass) { .code = vertex_shader.contents, .is_spirv = true }, .fs = { .debug_name = "Shadows Frag shader", - .filepath = vert_path, - .code = vertex_shader.contents, + .filepath = frag_path, + .code = fragment_shader.contents, .is_spirv = true }, .renderpass = rpass }; diff --git a/src/renderer/renderpasses.h b/src/renderer/renderpasses.h index 3ee3b6e..5a5ffee 100644 --- a/src/renderer/renderpasses.h +++ b/src/renderer/renderpasses.h @@ -10,6 +10,7 @@ */ #pragma once #include "ral.h" +#include "ral_types.h" #include "render_types.h" // Shadowmap pass @@ -22,13 +23,31 @@ gpu_renderpass* renderpass_blinn_phong_create(); void renderpass_blinn_phong_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count); + typedef struct ren_shadowmaps { u32 width; u32 height; gpu_renderpass* rpass; gpu_pipeline* static_pipeline; + gpu_pipeline* debug_quad; + texture_handle depth_tex; } ren_shadowmaps; +typedef struct model_uniform { + mat4 model; +} model_uniform; +typedef struct lightspace_tf_uniform { + mat4 lightSpaceMatrix; +} lightspace_tf_uniform; + +typedef struct debug_quad_uniform { + texture_handle depthMap; +} debug_quad_uniform; + +shader_data_layout model_uniform_layout(void* data); +shader_data_layout lightspace_uniform_layout(void* data); +shader_data_layout debug_quad_layout(void* data); + void ren_shadowmaps_init(ren_shadowmaps* storage); gpu_renderpass* shadowmaps_renderpass_create(); |