summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-22 15:12:44 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-06-22 15:12:44 +1000
commit51d0535a8d49b72ab1e47acf30f654403a94c423 (patch)
treec8e8124c7a35c2fcfa7afa857b50ce0cdb15ba7c
parent6fccac3372170153b59e829d11c6c0b0a5c2bc77 (diff)
wip: porting shadowmaps to RAL
-rw-r--r--assets/shaders/cube.frag4
-rw-r--r--examples/shadow_maps/ex_shadow_maps.c81
-rw-r--r--include/celeritas.h68
-rw-r--r--src/maths/maths_types.h2
-rw-r--r--src/renderer/backends/opengl/backend_opengl.c40
-rw-r--r--src/renderer/backends/opengl/backend_opengl.h4
-rw-r--r--src/renderer/ral.h5
-rw-r--r--src/renderer/renderpasses.c49
-rw-r--r--src/renderer/renderpasses.h17
-rw-r--r--xmake.lua8
10 files changed, 200 insertions, 78 deletions
diff --git a/assets/shaders/cube.frag b/assets/shaders/cube.frag
index 5f17e7c..cd6bb21 100644
--- a/assets/shaders/cube.frag
+++ b/assets/shaders/cube.frag
@@ -9,6 +9,6 @@ layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) out vec4 outColor;
void main() {
- // outColor = texture(texSampler, fragTexCoord); // vec4(fragTexCoord, 0.0);
- outColor = vec4(1.0);
+ outColor = texture(texSampler, fragTexCoord); // vec4(fragTexCoord, 0.0);
+ // outColor = vec4(1.0);
}
diff --git a/examples/shadow_maps/ex_shadow_maps.c b/examples/shadow_maps/ex_shadow_maps.c
index e69de29..1d40128 100644
--- a/examples/shadow_maps/ex_shadow_maps.c
+++ b/examples/shadow_maps/ex_shadow_maps.c
@@ -0,0 +1,81 @@
+#include "celeritas.h"
+#include "maths_types.h"
+#include "primitives.h"
+#include "ral.h"
+#include "render.h"
+#include "render_types.h"
+#include "renderpasses.h"
+
+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];
+
+int main() {
+ core_bringup();
+ arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
+
+ vec3 camera_pos = vec3(2., 2., 2.);
+ vec3 camera_front = vec3_normalise(vec3_negate(camera_pos));
+ 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);
+
+ // Meshes
+ mesh cubes[4];
+ for (int i = 0; i < 4; i++) {
+ geometry_data geo = geo_create_cuboid(f32x3(2,2,2));
+ cubes[i] = mesh_create(&geo, true);
+ }
+
+ // Main loop
+ while (!should_exit(&g_core)) {
+ input_update(&g_core.input);
+
+ if (!gpu_backend_begin_frame()) {
+ continue;
+ }
+ gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder();
+
+ gpu_cmd_encoder_begin_render(enc, shadows.rpass);
+ 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);
+
+ gpu_cmd_encoder_begin_render(enc, static_opaque_rpass);
+
+ gpu_cmd_encoder_end_render(enc);
+ /*
+ Shadows
+
+ render scene into texture
+
+ begin_renderpass()
+ bind_pipeline()
+ upload shader data
+ for each object:
+ - set buffers
+ - draw call
+
+ end_renderpass()
+ */
+
+ gpu_backend_end_frame();
+ }
+
+ renderer_shutdown(&g_core.renderer);
+
+ return 0;
+}
diff --git a/include/celeritas.h b/include/celeritas.h
index 9ba741b..2f89edd 100644
--- a/include/celeritas.h
+++ b/include/celeritas.h
@@ -1,65 +1,5 @@
-/**
- * @file celeritas.h
- * @author your name (you@domain.com)
- * @brief
- * @version 0.1
- * @date 2024-05-09
- *
- * @copyright Copyright (c) 2024
- *
- */
-/* The Goal of this file is to test ocaml-bindgen on it to start moving development over into OCaml */
-
-#include <stdbool.h>
-#include <stdint.h>
-
-
-// Forward Declarations
-typedef struct core core;
-
-// Handles
-typedef uint32_t model_handle;
-
-// Maths
-typedef struct vec2 { float x, y; } vec2;
-typedef struct vec3 { float x, y, z; } vec3;
-typedef struct vec4 { float x, y, z, w; } vec4;
-typedef struct mat4 { float data[16]; } mat4;
-typedef struct transform3d { vec3 translation; vec4 rotation; float scale; } transform3d;
-
-// Lifecycle functions
-void core_bringup();
-void core_shutdown();
-bool should_window_close();
-
-void frame_begin();
-void frame_draw();
-void frame_end();
-
-// Assets
-model_handle model_load(const char* filepath);
-
-// Rendering
-typedef struct render_entity {
- model_handle model;
- // TODO: material
- transform3d transform;
-} render_entity;
-
-void render_frame_begin();
-
-// Scene
-typedef struct directional_light {} directional_light;
-typedef struct point_light {} point_light;
-void scene_add_dir_light(directional_light light);
-void scene_add_point_light(directional_light light);
-void scene_add_model(model_handle model, transform3d transform);
-bool scene_remove_model(model_handle model);
-
-void scene_set_model_transform(model_handle model, transform3d new_transform);
-void scene_set_camera(vec3 pos, vec3 front);
-
-// Immediate mode drawing
-
-// Input \ No newline at end of file
+#include "defines.h"
+#include "maths_types.h"
+#include "maths.h"
+#include "core.h" \ No newline at end of file
diff --git a/src/maths/maths_types.h b/src/maths/maths_types.h
index 5ef09db..609672c 100644
--- a/src/maths/maths_types.h
+++ b/src/maths/maths_types.h
@@ -92,7 +92,7 @@ typedef struct u32x2 {
u32 x;
u32 y;
} u32x2;
-#define u32x2(x, y) ((u32x3){ x, y })
+#define u32x2(x, y) ((u32x2){ x, y })
// Type aliass
diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c
index 18d1617..0d69296 100644
--- a/src/renderer/backends/opengl/backend_opengl.c
+++ b/src/renderer/backends/opengl/backend_opengl.c
@@ -1,5 +1,6 @@
#include <stddef.h>
#include <stdio.h>
+#include <string.h>
#include "builtin_materials.h"
#include "colours.h"
#include "maths.h"
@@ -109,15 +110,12 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
&blocksize);
printf("\t with size %d bytes\n", blocksize);
- glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo);
glBindBufferBase(GL_UNIFORM_BUFFER, s_binding_point, ubo_buf->id.ubo);
if (blockIndex != GL_INVALID_INDEX) {
glUniformBlockBinding(pipeline->shader_id, blockIndex, s_binding_point);
}
ubo_buf->ubo_binding_point = s_binding_point;
s_binding_point++;
-
- // Now we want to store a handle associated with the shader for this
}
}
}
@@ -129,8 +127,38 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip
void gpu_pipeline_destroy(gpu_pipeline* pipeline) {}
// --- Renderpass
-gpu_renderpass* gpu_renderpass_create(const gpu_renderpass_desc* description) {}
-void gpu_renderpass_destroy(gpu_renderpass* pass) {}
+gpu_renderpass* gpu_renderpass_create(const gpu_renderpass_desc* description) {
+ gpu_renderpass* renderpass = renderpass_pool_alloc(&context.gpu_pools.renderpasses, NULL);
+
+ memcpy(&renderpass->description, description, sizeof(gpu_renderpass_desc));
+
+ if (!description->default_framebuffer) {
+ GLuint gl_fbo_id;
+ glGenFramebuffers(1, &gl_fbo_id);
+ renderpass->fbo = gl_fbo_id;
+ } else {
+ renderpass->fbo = OPENGL_DEFAULT_FRAMEBUFFER;
+ assert(!description->has_color_target);
+ assert(!description->has_depth_stencil);
+ }
+ glBindFramebuffer(GL_FRAMEBUFFER, renderpass->fbo);
+
+ if (description->has_color_target) {
+ gpu_texture* colour_attachment = TEXTURE_GET(description->color_target);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colour_attachment->id, 0);
+ }
+ if (description->has_depth_stencil) {
+ gpu_texture* depth_attachment = TEXTURE_GET(description->depth_stencil);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depth_attachment->id, 0);
+ }
+
+ glBindFramebuffer(GL_FRAMEBUFFER, 0); // reset to default framebuffer
+
+ return renderpass;
+}
+void gpu_renderpass_destroy(gpu_renderpass* pass) {
+ glDeleteFramebuffers(1, &pass->fbo);
+}
// --- Swapchain
bool gpu_swapchain_create(gpu_swapchain* out_swapchain) {}
@@ -144,6 +172,7 @@ gpu_cmd_encoder gpu_cmd_encoder_create() {
void gpu_cmd_encoder_destroy(gpu_cmd_encoder* encoder) {}
void gpu_cmd_encoder_begin(gpu_cmd_encoder encoder) {}
void gpu_cmd_encoder_begin_render(gpu_cmd_encoder* encoder, gpu_renderpass* renderpass) {
+ glBindFramebuffer(GL_FRAMEBUFFER, renderpass->fbo);
rgba clear_colour = STONE_800;
glClearColor(clear_colour.r, clear_colour.g, clear_colour.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -206,7 +235,6 @@ void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* d
}
glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo);
- glBindBufferBase(GL_UNIFORM_BUFFER, ubo_buf->ubo_binding_point, ubo_buf->id.ubo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, binding.data.bytes.data);
} else if (binding.type == SHADER_BINDING_TEXTURE) {
diff --git a/src/renderer/backends/opengl/backend_opengl.h b/src/renderer/backends/opengl/backend_opengl.h
index f52bd79..f588643 100644
--- a/src/renderer/backends/opengl/backend_opengl.h
+++ b/src/renderer/backends/opengl/backend_opengl.h
@@ -8,6 +8,8 @@
#define MAX_PIPELINE_UNIFORM_BUFFERS 32
+#define OPENGL_DEFAULT_FRAMEBUFFER 0
+
typedef struct gpu_swapchain {
u32x2 dimensions;
} gpu_swapchain;
@@ -23,6 +25,8 @@ typedef struct gpu_pipeline {
bool wireframe;
} gpu_pipeline;
typedef struct gpu_renderpass {
+ u32 fbo;
+ gpu_renderpass_desc description;
void *pad
} gpu_renderpass;
typedef struct gpu_cmd_encoder {
diff --git a/src/renderer/ral.h b/src/renderer/ral.h
index cd3f19f..ab62679 100644
--- a/src/renderer/ral.h
+++ b/src/renderer/ral.h
@@ -97,7 +97,10 @@ struct graphics_pipeline_desc {
};
typedef struct gpu_renderpass_desc {
- texture_handle color_target; // for now only support one
+ bool default_framebuffer;
+ bool has_color_target;
+ texture_handle color_target; // for now only support one
+ bool has_depth_stencil;
texture_handle depth_stencil;
} gpu_renderpass_desc;
diff --git a/src/renderer/renderpasses.c b/src/renderer/renderpasses.c
new file mode 100644
index 0000000..85b86ed
--- /dev/null
+++ b/src/renderer/renderpasses.c
@@ -0,0 +1,49 @@
+/**
+ * @file renderpasses.c
+ * @author your name (you@domain.com)
+ * @brief
+ * @version 0.1
+ * @date 2024-06-22
+ *
+ * @copyright Copyright (c) 2024
+ *
+ */
+
+#include "renderpasses.h"
+#include "maths_types.h"
+#include "ral.h"
+#include "ral_types.h"
+
+#define SHADOW_WIDTH 1000
+#define SHADOW_HEIGHT 1000
+
+
+gpu_renderpass* shadowmaps_renderpass_create() {
+ // Create depthmap texture
+ u32x2 extents = u32x2(SHADOW_WIDTH, SHADOW_HEIGHT);
+ texture_desc depthmap_desc = {
+ .extents = extents,
+ .format = CEL_TEXTURE_FORMAT_DEPTH_DEFAULT,
+ .tex_type = CEL_TEXTURE_TYPE_2D
+ };
+ texture_handle depthmap = gpu_texture_create(depthmap_desc, false, NULL);
+
+ gpu_renderpass_desc shadows_desc = {
+ .default_framebuffer = false,
+ .has_color_target = false,
+ .has_depth_stencil = true,
+ .depth_stencil = depthmap
+ };
+ return gpu_renderpass_create(&shadows_desc);
+}
+
+gpu_pipeline* shadowmaps_pipeline_create() {
+ struct graphics_pipeline_desc desc = {
+ .
+ };
+ gpu_graphics_pipeline_create(struct graphics_pipeline_desc description)
+}
+
+void renderpass_shadowmap_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count) {
+
+} \ No newline at end of file
diff --git a/src/renderer/renderpasses.h b/src/renderer/renderpasses.h
index 951ff6e..4a689e6 100644
--- a/src/renderer/renderpasses.h
+++ b/src/renderer/renderpasses.h
@@ -9,9 +9,7 @@
*
*/
#pragma once
-#include "maths_types.h"
#include "ral.h"
-#include "render.h"
#include "render_types.h"
// Shadowmap pass
@@ -24,5 +22,16 @@ gpu_renderpass* renderpass_blinn_phong_create();
void renderpass_blinn_phong_execute(gpu_renderpass* pass, render_entity* entities,
size_t entity_count);
-gpu_renderpass* renderpass_shadows_create();
-void renderpass_shadows_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count); \ No newline at end of file
+typedef struct ren_shadowmaps {
+ u32 width;
+ u32 height;
+ gpu_renderpass* rpass;
+ gpu_pipeline* static_pipeline;
+} ren_shadowmaps;
+
+void ren_shadowmaps_init(ren_shadowmaps* storage);
+
+gpu_renderpass* shadowmaps_renderpass_create();
+gpu_pipeline* shadowmaps_pipeline_create();
+
+void renderpass_shadowmap_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count);
diff --git a/xmake.lua b/xmake.lua
index 179e14f..335e751 100644
--- a/xmake.lua
+++ b/xmake.lua
@@ -106,6 +106,7 @@ target("core_config")
add_includedirs("deps/stb_image", {public = true})
add_includedirs("deps/stb_image_write", {public = true})
add_includedirs("deps/stb_truetype", {public = true})
+ add_includedirs("include/", {public = true})
add_includedirs("src/", {public = true})
-- add_includedirs("src/logos/", {public = true})
add_includedirs("src/maths/", {public = true})
@@ -237,6 +238,13 @@ target("pbr_textured")
add_files("examples/pbr_textured/ex_pbr_textured.c")
set_rundir("$(projectdir)")
+target("shadows")
+ set_kind("binary")
+ set_group("examples")
+ add_deps("core_static")
+ add_files("examples/shadow_maps/ex_shadow_maps.c")
+ set_rundir("$(projectdir)")
+
-- target("transforms")
-- set_kind("binary")
-- set_group("examples")