diff options
Diffstat (limited to 'src/new_render')
-rw-r--r-- | src/new_render/pbr.c | 12 | ||||
-rw-r--r-- | src/new_render/pbr.h | 1 | ||||
-rw-r--r-- | src/new_render/render.c | 21 | ||||
-rw-r--r-- | src/new_render/render.h | 7 | ||||
-rw-r--r-- | src/new_render/render_types.h | 59 | ||||
-rw-r--r-- | src/new_render/shadows.c | 6 | ||||
-rw-r--r-- | src/new_render/skybox.h | 2 |
7 files changed, 74 insertions, 34 deletions
diff --git a/src/new_render/pbr.c b/src/new_render/pbr.c index ccc4dcf..09da50e 100644 --- a/src/new_render/pbr.c +++ b/src/new_render/pbr.c @@ -103,12 +103,16 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te enc, 3, (ShaderData){ .data = &lights_data, .get_layout = &Binding_Lights_GetLayout }); // TODO: Add shadowmap texture to uniforms + Mesh_pool* mesh_pool = Render_GetMeshPool(); + Material_pool* material_pool = Render_GetMaterialPool(); for (size_t ent_i = 0; ent_i < entity_count; ent_i++) { RenderEnt renderable = entities[ent_i]; + Mesh* mesh = Mesh_pool_get(mesh_pool, renderable.mesh); + Material* mat = Material_pool_get(material_pool, renderable.material); // upload material data - PBRMaterialUniforms material_data = { .mat = *renderable.material }; + PBRMaterialUniforms material_data = { .mat = *mat }; GPU_EncodeBindShaderData( enc, 2, (ShaderData){ .data = &material_data, .get_layout = PBRMaterial_GetLayout }); @@ -118,10 +122,10 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te enc, 1, (ShaderData){ .data = &model_data, .get_layout = &Binding_Model_GetLayout }); // set buffers - GPU_EncodeSetVertexBuffer(enc, renderable.mesh->vertex_buffer); - GPU_EncodeSetIndexBuffer(enc, renderable.mesh->index_buffer); + GPU_EncodeSetVertexBuffer(enc, mesh->vertex_buffer); + GPU_EncodeSetIndexBuffer(enc, mesh->index_buffer); // draw - GPU_EncodeDrawIndexed(enc, renderable.mesh->geometry.indices->len); + GPU_EncodeDrawIndexed(enc, mesh->geometry.index_count); } GPU_CmdEncoder_EndRender(enc); diff --git a/src/new_render/pbr.h b/src/new_render/pbr.h index 0aa9dfe..d8c9405 100644 --- a/src/new_render/pbr.h +++ b/src/new_render/pbr.h @@ -48,7 +48,6 @@ typedef struct PBR_Textures { TextureHandle ao_map; } PBR_Textures; - // --- Internal typedef struct MaterialMap MaterialMap; diff --git a/src/new_render/render.c b/src/new_render/render.c index 5202cd3..235f1cb 100644 --- a/src/new_render/render.c +++ b/src/new_render/render.c @@ -23,6 +23,10 @@ #include <stb_image.h> #define FRAME_ARENA_SIZE MB(1) +#define POOL_SIZE_BYTES \ + MB(10) // we will reserve 10 megabytes up front to store resource, mesh, and material pools +#define MAX_MESHES 1024 +#define MAX_MATERIALS 256 extern Core g_core; @@ -37,8 +41,10 @@ struct Renderer { PBR_Storage* pbr; Shadow_Storage* shadows; Terrain_Storage* terrain; - // Text_Storage text; + // Text_Storage* text; ResourcePools* resource_pools; + Mesh_pool mesh_pool; + Material_pool material_pool; arena frame_arena; TextureHandle white_1x1; }; @@ -53,9 +59,11 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window // init resource pools DEBUG("Initialise GPU resource pools"); - arena pool_arena = arena_create(malloc(1024 * 1024), 1024 * 1024); + arena pool_arena = arena_create(malloc(POOL_SIZE_BYTES), POOL_SIZE_BYTES); ren->resource_pools = arena_alloc(&pool_arena, sizeof(struct ResourcePools)); ResourcePools_Init(&pool_arena, ren->resource_pools); + ren->mesh_pool = Mesh_pool_create(&pool_arena, MAX_MESHES, sizeof(Mesh)); + ren->material_pool = Material_pool_create(&pool_arena, MAX_MATERIALS, sizeof(Material)); // GLFW window creation GLFWwindow* window; @@ -288,4 +296,13 @@ TextureHandle Render_GetWhiteTexture() { arena* Render_GetFrameArena() { Renderer* ren = Core_GetRenderer(&g_core); return &ren->frame_arena; +} + +Mesh_pool* Render_GetMeshPool() { + Renderer* ren = Core_GetRenderer(&g_core); + return &ren->mesh_pool; +} +Material_pool* Render_GetMaterialPool() { + Renderer* ren = Core_GetRenderer(&g_core); + return &ren->material_pool; }
\ No newline at end of file diff --git a/src/new_render/render.h b/src/new_render/render.h index b8c34b9..ef11b5f 100644 --- a/src/new_render/render.h +++ b/src/new_render/render.h @@ -26,6 +26,9 @@ typedef struct RenderCtx { Mat4 projection; } RenderCtx; +// #define MESH_GET(h) (Mesh_pool_get(g_core.renderer->meshes, h)) +// #define MATERIAL_GET(h) (Material_pool_get(g_core.renderer->material, h)) + // --- Lifecycle PUB bool Renderer_Init(RendererConfig config, Renderer* renderer, GLFWwindow** out_window, @@ -79,4 +82,6 @@ RenderScene* Render_GetScene(); Shadow_Storage* Render_GetShadowStorage(); Terrain_Storage* Render_GetTerrainStorage(); TextureHandle Render_GetWhiteTexture(); -arena* Render_GetFrameArena();
\ No newline at end of file +arena* Render_GetFrameArena(); +Mesh_pool* Render_GetMeshPool(); +Material_pool* Render_GetMaterialPool();
\ No newline at end of file diff --git a/src/new_render/render_types.h b/src/new_render/render_types.h index f4f108a..2b7a77a 100644 --- a/src/new_render/render_types.h +++ b/src/new_render/render_types.h @@ -5,20 +5,24 @@ #pragma once #include "defines.h" -#include "maths.h" #include "maths_types.h" -#include "ral.h" +#include "mem.h" #include "ral_types.h" // --- Handles CORE_DEFINE_HANDLE(ModelHandle); -#define ABSENT_MODEL_HANDLE 999999999 +CORE_DEFINE_HANDLE(MaterialHandle); +CORE_DEFINE_HANDLE(MeshHandle); +#define INVALID_MODEL_HANDLE ((ModelHandle){ .raw = 9999991 }) +#define INVALID_MATERIAL_HANDLE ((MaterialHandle){ .raw = 9999992 }) +#define INVALID_MESH_HANDLE ((MeshHandle){ .raw = 9999993 }) typedef struct Geometry { VertexFormat format; Vertex_darray* vertices; - bool has_indices; u32_darray* indices; + bool has_indices; + size_t index_count; } Geometry; typedef struct u32_opt { @@ -29,13 +33,15 @@ typedef struct u32_opt { typedef struct Mesh { BufferHandle vertex_buffer; BufferHandle index_buffer; - Geometry geometry; // NULL means it has been freed CPU-side - i32 material_index; // -1 => no material - bool is_uploaded; // has the data been uploaded to the GPU + Geometry geometry; // NULL means it has been freed CPU-side + // i32 material_index; // -1 => no material + MaterialHandle material; + bool is_uploaded; // has the data been uploaded to the GPU } Mesh; -#ifndef TYPED_MESH_ARRAY +#ifndef TYPED_MESH_CONTAINERS KITC_DECL_TYPED_ARRAY(Mesh) -#define TYPED_MESH_ARRAY +TYPED_POOL(Mesh, Mesh) +#define TYPED_MESH_CONTAINERS #endif typedef struct TextureData { @@ -55,12 +61,12 @@ static const char* material_kind_names[] = { "Blinn Phong", "PBR (Textures)", "P /** * @brief - * @note mostly references https://google.github.io/filament/Filament.html#materialsystem/standardmodel + * @note based on https://google.github.io/filament/Filament.html#materialsystem/standardmodel */ typedef struct Material { char name[64]; - MaterialKind kind; // at the moment all materials are PBR materials - Vec3 base_colour; // linear RGB {0,0,0} to {1,1,1} + MaterialKind kind; // at the moment all materials are PBR materials + Vec3 base_colour; // linear RGB {0,0,0} to {1,1,1} f32 metallic; f32 roughness; f32 ambient_occlusion; @@ -70,16 +76,19 @@ typedef struct Material { TextureHandle ambient_occlusion_map; } Material; -#ifndef TYPED_MATERIAL_ARRAY +#ifndef TYPED_MATERIAL_CONTAINERS KITC_DECL_TYPED_ARRAY(Material) -#define TYPED_MATERIAL_ARRAY +TYPED_POOL(Material, Material) +#define TYPED_MATERIAL_CONTAINERS #endif +/** @brief Convenient wrapper around a number of meshes each with a material */ typedef struct Model { Str8 name; - // meshes - Mesh_darray* meshes; - Material_darray* materials; + MeshHandle* meshes; + size_t mesh_count; + MaterialHandle* materials; + size_t material_count; } Model; #ifndef TYPED_MODEL_ARRAY KITC_DECL_TYPED_ARRAY(Model) @@ -106,13 +115,19 @@ typedef struct DirectionalLight { // --- -// A renderable 'thing' +typedef enum RenderEntityFlag { + REND_ENT_CASTS_SHADOWS = 1 << 0, + REND_ENT_VISIBLE = 1 << 1, +} RenderEntityFlag; +typedef u32 RenderEntityFlags; + +/** @brief A renderable 'thing' */ typedef struct RenderEnt { - Mesh* mesh; - Material* material; + MeshHandle mesh; + MaterialHandle material; Mat4 affine; // In the future this should be updated by the transform graph - // Bbox_3D bounding_box; - bool casts_shadows; + Bbox_3D bounding_box; + RenderEntityFlags flags; } RenderEnt; #ifndef TYPED_RENDERENT_ARRAY diff --git a/src/new_render/shadows.c b/src/new_render/shadows.c index 6a9fe02..92fce81 100644 --- a/src/new_render/shadows.c +++ b/src/new_render/shadows.c @@ -1,9 +1,9 @@ #include "shadows.h" #include <string.h> -#include "core.h" #include "file.h" #include "glad/glad.h" #include "log.h" +#include "maths.h" #include "maths_types.h" #include "primitives.h" #include "ral_common.h" @@ -197,12 +197,12 @@ void Shadow_ShadowmapExecute(Shadow_Storage* storage, Mat4 light_space_transform for (size_t ent_i = 0; ent_i < entity_count; ent_i++) { RenderEnt renderable = entities[ent_i]; - if (renderable.casts_shadows) { + if (renderable.flags && REND_ENT_CASTS_SHADOWS) { // Model* model = MODEL_GET(renderable.model); uniforms.model = renderable.affine; // update the model transform - Mesh* mesh = renderable.mesh; + Mesh* mesh = Mesh_pool_get(Render_GetMeshPool(), renderable.mesh); GPU_EncodeBindShaderData(&shadow_encoder, 0, shader_data); GPU_EncodeSetVertexBuffer(&shadow_encoder, mesh->vertex_buffer); GPU_EncodeSetIndexBuffer(&shadow_encoder, mesh->index_buffer); diff --git a/src/new_render/skybox.h b/src/new_render/skybox.h index 3b5b55b..c2ef3a2 100644 --- a/src/new_render/skybox.h +++ b/src/new_render/skybox.h @@ -5,7 +5,7 @@ #pragma once #include "camera.h" #include "defines.h" -#include "ral_types.h" +#include "ral_impl.h" #include "render_types.h" typedef struct Skybox { |