summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/renderer/backends/opengl/backend_opengl.c18
-rw-r--r--src/renderer/renderpasses.c87
-rw-r--r--src/renderer/renderpasses.h2
3 files changed, 98 insertions, 9 deletions
diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c
index 8f52674..ea10acb 100644
--- a/src/renderer/backends/opengl/backend_opengl.c
+++ b/src/renderer/backends/opengl/backend_opengl.c
@@ -152,6 +152,11 @@ gpu_renderpass* gpu_renderpass_create(const gpu_renderpass_desc* description) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depth_attachment->id, 0);
}
+ if (description->has_depth_stencil && !description->has_color_target) {
+ glDrawBuffer(GL_NONE);
+ glReadBuffer(GL_NONE);
+ }
+
glBindFramebuffer(GL_FRAMEBUFFER, 0); // reset to default framebuffer
return renderpass;
@@ -336,6 +341,10 @@ texture_handle gpu_texture_create(texture_desc desc, bool create_view, const voi
glBindTexture(GL_TEXTURE_2D, gl_texture_id);
+ GLint internal_format = desc.format == CEL_TEXTURE_FORMAT_DEPTH_DEFAULT ? GL_DEPTH_COMPONENT : GL_RGB;
+ GLenum format = desc.format == CEL_TEXTURE_FORMAT_DEPTH_DEFAULT ? GL_DEPTH_COMPONENT : GL_RGBA;
+ GLenum data_type = desc.format == CEL_TEXTURE_FORMAT_DEPTH_DEFAULT ? GL_FLOAT : GL_UNSIGNED_BYTE;
+
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
@@ -345,12 +354,15 @@ texture_handle gpu_texture_create(texture_desc desc, bool create_view, const voi
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (data) {
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, desc.extents.x, desc.extents.y, 0,
- GL_RGBA, // TODO: convert format to GL enum
- GL_UNSIGNED_BYTE, data);
+ glTexImage2D(GL_TEXTURE_2D, 0, internal_format, desc.extents.x, desc.extents.y, 0,
+ format,
+ data_type, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
WARN("No image data provided");
+ glTexImage2D(GL_TEXTURE_2D, 0, internal_format, desc.extents.x, desc.extents.y, 0,
+ format,
+ data_type, NULL);
}
glBindTexture(GL_TEXTURE_2D, 0);
diff --git a/src/renderer/renderpasses.c b/src/renderer/renderpasses.c
index d28df3d..3f2152f 100644
--- a/src/renderer/renderpasses.c
+++ b/src/renderer/renderpasses.c
@@ -13,10 +13,15 @@
#include "maths_types.h"
#include "ral.h"
#include "ral_types.h"
+#include "file.h"
#define SHADOW_WIDTH 1000
#define SHADOW_HEIGHT 1000
+void ren_shadowmaps_init(ren_shadowmaps* storage) {
+ storage->rpass = shadowmaps_renderpass_create();
+ storage->static_pipeline = shadowmaps_pipeline_create(storage->rpass);
+}
gpu_renderpass* shadowmaps_renderpass_create() {
// Create depthmap texture
@@ -37,11 +42,83 @@ gpu_renderpass* shadowmaps_renderpass_create() {
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)
+// == 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;
+
+ shader_binding b1 = {
+ .label = "Model",
+ .type = SHADER_BINDING_BYTES,
+ .stores_data = has_data,
+ .data = {.bytes.size = sizeof(model_uniform)}
+ };
+ if (has_data) {
+ b1.data.bytes.data = data;
+ }
+ return (shader_data_layout){ .name = "model_uniform", .bindings = {b1}, .bindings_count = 1};
+}
+shader_data_layout lightspace_uniform_layout(void* data) {
+ bool has_data = data != NULL;
+
+ shader_binding b1 = {
+ .label = "LightSpace",
+ .type = SHADER_BINDING_BYTES,
+ .stores_data = has_data,
+ .data = {.bytes.size = sizeof(lightspace_tf_uniform)}
+ };
+ if (has_data) {
+ b1.data.bytes.data = data;
+ }
+ return (shader_data_layout){ .name = "lightspace_tf_uniform", .bindings = {b1}, .bindings_count = 1};
+}
+
+// ==================
+
+gpu_pipeline* shadowmaps_pipeline_create(gpu_renderpass* rpass) {
+ arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
+
+ str8 vert_path = str8lit("assets/shaders/shadows.vert");
+ str8 frag_path = str8lit("assets/shaders/shadows.frag");
+ str8_opt vertex_shader = str8_from_file(&scratch, vert_path);
+ str8_opt fragment_shader = str8_from_file(&scratch, frag_path);
+ if (!vertex_shader.has_value || !fragment_shader.has_value) {
+ ERROR_EXIT("Failed to load shaders from disk");
+ }
+
+ // We'll have two data layouts. 1. for the light-space transform, and 2. for the model matrix
+ shader_data model_uniform = {.data = NULL, .shader_data_get_layout = &model_uniform_layout };
+ shader_data lightspace_uniform = {.data = NULL, .shader_data_get_layout = &lightspace_uniform_layout };
+
+ struct graphics_pipeline_desc desc = {
+ .debug_name = "Shadowmap drawing pipeline",
+ .vertex_desc = static_3d_vertex_description(),
+ .data_layouts = { model_uniform, lightspace_uniform },
+ .data_layouts_count = 2,
+ .vs = {
+ .debug_name = "Shadows Vert shader",
+ .filepath = vert_path,
+ .code = vertex_shader.contents,
+ .is_spirv = true
+ },
+ .fs = {
+ .debug_name = "Shadows Frag shader",
+ .filepath = vert_path,
+ .code = vertex_shader.contents,
+ .is_spirv = true
+ },
+ .renderpass = rpass
+ };
+
+ arena_free_storage(&scratch);
+ return gpu_graphics_pipeline_create(desc);
}
void renderpass_shadowmap_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count) {
diff --git a/src/renderer/renderpasses.h b/src/renderer/renderpasses.h
index 4a689e6..3ee3b6e 100644
--- a/src/renderer/renderpasses.h
+++ b/src/renderer/renderpasses.h
@@ -32,6 +32,6 @@ typedef struct ren_shadowmaps {
void ren_shadowmaps_init(ren_shadowmaps* storage);
gpu_renderpass* shadowmaps_renderpass_create();
-gpu_pipeline* shadowmaps_pipeline_create();
+gpu_pipeline* shadowmaps_pipeline_create(gpu_renderpass* rpass);
void renderpass_shadowmap_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count);