diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-14 23:55:18 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-14 23:55:18 +1000 |
commit | b9315f9cb625db09c3c41d8adf5230a67510bef7 (patch) | |
tree | 8e02010d4e29c82ec9310b02b7924f4c8456ef6f /src/new_render/render.c | |
parent | 529a603128d5e9dc4701322f44961f165e2183e1 (diff) |
wip shadows
Diffstat (limited to 'src/new_render/render.c')
-rw-r--r-- | src/new_render/render.c | 117 |
1 files changed, 64 insertions, 53 deletions
diff --git a/src/new_render/render.c b/src/new_render/render.c index cfd0b11..21046ea 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -2,17 +2,18 @@ * @brief */ -#include <glfw3.h> #include "render.h" -#include "core.h" +#include <glfw3.h> #include "camera.h" #include "colours.h" +#include "core.h" #include "log.h" #include "maths.h" #include "maths_types.h" #include "pbr.h" #include "ral_common.h" #include "ral_impl.h" +#include "ral_types.h" #include "render_scene.h" #include "render_types.h" #include "shadows.h" @@ -35,30 +36,30 @@ struct Renderer { }; bool Renderer_Init(RendererConfig config, Renderer* ren) { - INFO("Renderer init"); + INFO("Renderer init"); - // init resource pools - DEBUG("Initialise GPU resource pools"); - arena pool_arena = arena_create(malloc(1024 * 1024), 1024 * 1024); - ren->resource_pools = arena_alloc(&pool_arena, sizeof(struct ResourcePools)); - ResourcePools_Init(&pool_arena, ren->resource_pools); + // init resource pools + DEBUG("Initialise GPU resource pools"); + arena pool_arena = arena_create(malloc(1024 * 1024), 1024 * 1024); + ren->resource_pools = arena_alloc(&pool_arena, sizeof(struct ResourcePools)); + ResourcePools_Init(&pool_arena, ren->resource_pools); // GLFW window creation // NOTE: all platforms use GLFW at the moment but thats subject to change glfwInit(); - #if defined(CEL_REND_BACKEND_OPENGL) - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); - #elif defined(CEL_REND_BACKEND_VULKAN) - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - #endif - - GLFWwindow* window = glfwCreateWindow(config.scr_width, config.scr_height, - config.window_name, NULL, NULL); +#if defined(CEL_REND_BACKEND_OPENGL) + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#elif defined(CEL_REND_BACKEND_VULKAN) + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); +#endif + + GLFWwindow* window = + glfwCreateWindow(config.scr_width, config.scr_height, config.window_name, NULL, NULL); if (window == NULL) { ERROR("Failed to create GLFW window\n"); glfwTerminate(); @@ -76,7 +77,6 @@ bool Renderer_Init(RendererConfig config, Renderer* ren) { GPU_Device_Create(&ren->device); GPU_Swapchain_Create(&ren->swapchain); - // set up default scene Camera default_cam = Camera_Create(vec3(0.0, 2.0, 4.0), vec3_normalise(vec3(0.0, -2.0, -4.0)), VEC3_Y, 45.0); @@ -90,46 +90,57 @@ bool Renderer_Init(RendererConfig config, Renderer* ren) { return true; } -void Renderer_Shutdown(Renderer* renderer) { } +void Renderer_Shutdown(Renderer* ren) {} size_t Renderer_GetMemReqs() { return sizeof(Renderer); } -void Render_FrameBegin(Renderer *renderer) { - renderer->frame_aborted = false; - if (GPU_Backend) - +void Render_FrameBegin(Renderer* ren) { + ren->frame_aborted = false; + if (!GPU_Backend_BeginFrame()) { + ren->frame_aborted = true; + WARN("Frame aborted"); + return; + } } -void Render_FrameEnd(Renderer* renderer) { +void Render_FrameEnd(Renderer* ren) { + if (ren->frame_aborted) { + return; + } + GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); } void Render_RenderEntities(RenderEnt* entities, size_t entity_count) { - Renderer* ren = Core_GetRenderer(&g_core); - - GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); - // bind shadow - GPU_EncodeBindPipeline(enc, ren->shadows) - + Renderer* ren = Core_GetRenderer(&g_core); + RenderScene scene = ren->scene; + + // -- Shadows + f32 near_plane = 1.0, far_plane = 10.0; + Mat4 light_projection = mat4_orthographic(-10.0, 10.0, -10.0, 10.0, near_plane, far_plane); + Vec3 pos = vec3_negate(scene.sun.direction); + Mat4 light_view = mat4_look_at(pos, VEC3_ZERO, VEC3_Y); + Mat4 light_space_matrix = mat4_mult(light_view, light_projection); + Shadow_ShadowmapExecute(ren->shadows, light_space_matrix, entities, entity_count); } Mesh Mesh_Create(Geometry* geometry, bool free_on_upload) { - Mesh m = { 0 }; - - // Create and upload vertex buffer - size_t vert_bytes = geometry->vertices->len * sizeof(Vertex); - INFO("Creating vertex buffer with size %d (%d x %d)", vert_bytes, geometry->vertices->len, - sizeof(Vertex)); - m.vertex_buffer = GPU_BufferCreate(vert_bytes, BUFFER_VERTEX, BUFFER_FLAG_GPU, - geometry->vertices->data); - - // Create and upload index buffer - size_t index_bytes = geometry->indices->len * sizeof(u32); - INFO("Creating index buffer with size %d (len: %d)", index_bytes, geometry->indices->len); - m.index_buffer = GPU_BufferCreate(index_bytes, BUFFER_INDEX, BUFFER_FLAG_GPU, - geometry->indices->data); - - m.is_uploaded = true; - m.geometry = geometry; - if (free_on_upload) { - Geometry_Destroy(geometry); - } - return m; + Mesh m = { 0 }; + + // Create and upload vertex buffer + size_t vert_bytes = geometry->vertices->len * sizeof(Vertex); + INFO("Creating vertex buffer with size %d (%d x %d)", vert_bytes, geometry->vertices->len, + sizeof(Vertex)); + m.vertex_buffer = + GPU_BufferCreate(vert_bytes, BUFFER_VERTEX, BUFFER_FLAG_GPU, geometry->vertices->data); + + // Create and upload index buffer + size_t index_bytes = geometry->indices->len * sizeof(u32); + INFO("Creating index buffer with size %d (len: %d)", index_bytes, geometry->indices->len); + m.index_buffer = + GPU_BufferCreate(index_bytes, BUFFER_INDEX, BUFFER_FLAG_GPU, geometry->indices->data); + + m.is_uploaded = true; + m.geometry = geometry; + if (free_on_upload) { + Geometry_Destroy(geometry); + } + return m; } |