From 175c72557b21f356e295a6f8a4acd91b7e744bef Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 24 Oct 2025 15:40:40 -0700 Subject: Consolidate LLR into a single file. --- src/llr/light.c | 42 ----------------- src/llr/light_impl.h | 25 ----------- src/llr/llr.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++-- src/llr/llr_impl.h | 31 +++++++++++++ src/llr/material.c | 57 ----------------------- src/llr/material_impl.h | 15 ------- src/llr/mesh.c | 24 ---------- src/llr/mesh_impl.h | 9 ---- 8 files changed, 145 insertions(+), 175 deletions(-) delete mode 100644 src/llr/light.c delete mode 100644 src/llr/light_impl.h delete mode 100644 src/llr/material.c delete mode 100644 src/llr/material_impl.h delete mode 100644 src/llr/mesh.c delete mode 100644 src/llr/mesh_impl.h (limited to 'src/llr') diff --git a/src/llr/light.c b/src/llr/light.c deleted file mode 100644 index 0fa1522..0000000 --- a/src/llr/light.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "light_impl.h" - -#include "memory.h" -#include "scene/node_impl.h" - -#include - -static void make_environment_light( - Light* light, const EnvironmentLightDesc* desc) { - assert(light); - assert(desc); - light->type = EnvironmentLightType; - light->environment.environment_map = desc->environment_map; -} - -Light* gfx_make_light(const LightDesc* desc) { - assert(desc); - - Light* light = mem_alloc_light(); - - switch (desc->type) { - case EnvironmentLightType: - make_environment_light(light, &desc->light.environment); - break; - default: - log_error("Unhandled light type"); - gfx_destroy_light(&light); - return 0; - } - - return light; -} - -void gfx_destroy_light(Light** light) { - assert(light); - if (*light) { - if ((*light)->parent.val) { - gfx_del_node((*light)->parent); - } - mem_free_light(light); - } -} diff --git a/src/llr/light_impl.h b/src/llr/light_impl.h deleted file mode 100644 index 5ec8145..0000000 --- a/src/llr/light_impl.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -#include "scene/types.h" - -typedef struct Texture Texture; - -/// An environment light. -typedef struct EnvironmentLight { - const Texture* environment_map; - const Texture* irradiance_map; // Renderer implementation. - const Texture* prefiltered_environment_map; // Renderer implementation. - int max_reflection_lod; // Mandatory when prefiltered_environment_map is - // given. -} EnvironmentLight; - -/// A scene light. -typedef struct Light { - LightType type; - union { - EnvironmentLight environment; - }; - node_idx parent; // Parent SceneNode. -} Light; diff --git a/src/llr/llr.c b/src/llr/llr.c index 25cdf9f..a1b37be 100644 --- a/src/llr/llr.c +++ b/src/llr/llr.c @@ -1,14 +1,14 @@ -#include "light_impl.h" #include "llr_impl.h" -#include "mesh_impl.h" -#include "llr/material_impl.h" +#include "memory.h" #include "scene/animation_impl.h" +#include "scene/node_impl.h" #include #include #include +#include static const int IRRADIANCE_MAP_WIDTH = 1024; static const int IRRADIANCE_MAP_HEIGHT = 1024; @@ -17,6 +17,117 @@ static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128; static const int BRDF_INTEGRATION_MAP_WIDTH = 512; static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; +static void make_environment_light( + Light* light, const EnvironmentLightDesc* desc) { + assert(light); + assert(desc); + light->type = EnvironmentLightType; + light->environment.environment_map = desc->environment_map; +} + +Light* gfx_make_light(const LightDesc* desc) { + assert(desc); + + Light* light = mem_alloc_light(); + + switch (desc->type) { + case EnvironmentLightType: + make_environment_light(light, &desc->light.environment); + break; + default: + log_error("Unhandled light type"); + gfx_destroy_light(&light); + return 0; + } + + return light; +} + +void gfx_destroy_light(Light** light) { + assert(light); + if (*light) { + if ((*light)->parent.val) { + gfx_del_node((*light)->parent); + } + mem_free_light(light); + } +} + +static void material_make(Material* material, const MaterialDesc* desc) { + assert(material); + assert(desc); + assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL); + material->num_uniforms = desc->num_uniforms; + for (int i = 0; i < desc->num_uniforms; ++i) { + material->uniforms[i] = desc->uniforms[i]; + } +} + +Material* gfx_make_material(const MaterialDesc* desc) { + assert(desc); + Material* material = mem_alloc_material(); + material_make(material, desc); + return material; +} + +void gfx_destroy_material(Material** material) { mem_free_material(material); } + +static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) { + switch (uniform->type) { + case UniformTexture: + gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture); + break; + case UniformMat4: + gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.mat4); + break; + case UniformVec3: + gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.vec3); + break; + case UniformVec4: + gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.vec4); + break; + case UniformFloat: + gfx_set_float_uniform(prog, uniform->name.str, uniform->value.scalar); + break; + case UniformMat4Array: + gfx_set_mat4_array_uniform( + prog, uniform->name.str, uniform->value.array.values, + uniform->value.array.count); + break; + } +} + +/// Activate the material. +/// +/// This configures the shader uniforms that are specific to the material. +static void gfx_material_activate( + ShaderProgram* shader, const Material* material) { + assert(material); + for (int i = 0; i < material->num_uniforms; ++i) { + const ShaderUniform* uniform = &material->uniforms[i]; + set_uniform(shader, uniform); + } +} + +static void mesh_make(Mesh* mesh, const MeshDesc* desc) { + assert(mesh); + assert(desc); + assert(desc->geometry); + assert(desc->material); + assert(desc->shader); + mesh->geometry = desc->geometry; + mesh->material = desc->material; + mesh->shader = desc->shader; +} + +Mesh* gfx_make_mesh(const MeshDesc* desc) { + Mesh* mesh = mem_alloc_mesh(); + mesh_make(mesh, desc); + return mesh; +} + +void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); } + /// Initialize renderer state for IBL. static bool init_ibl(LLR* renderer) { assert(renderer); diff --git a/src/llr/llr_impl.h b/src/llr/llr_impl.h index 3f6a68f..6318ee0 100644 --- a/src/llr/llr_impl.h +++ b/src/llr/llr_impl.h @@ -3,6 +3,8 @@ #include #include +#include "scene/types.h" + #include #include @@ -17,6 +19,35 @@ typedef struct Material Material; typedef struct ShaderProgram ShaderProgram; typedef struct Texture Texture; +/// An environment light. +typedef struct EnvironmentLight { + const Texture* environment_map; + const Texture* irradiance_map; // Renderer implementation. + const Texture* prefiltered_environment_map; // Renderer implementation. + int max_reflection_lod; // Mandatory when prefiltered_environment_map is + // given. +} EnvironmentLight; + +/// A scene light. +typedef struct Light { + LightType type; + union { + EnvironmentLight environment; + }; + node_idx parent; // Parent SceneNode. +} Light; + +typedef struct Material { + ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; + int num_uniforms; +} Material; + +typedef struct Mesh { + const Geometry* geometry; + const Material* material; + ShaderProgram* shader; // TODO: Move this back to Material? +} Mesh; + /// Immediate mode renderer. /// /// The renderer caches state changes in memory and only programs the underlying diff --git a/src/llr/material.c b/src/llr/material.c deleted file mode 100644 index f09dd3f..0000000 --- a/src/llr/material.c +++ /dev/null @@ -1,57 +0,0 @@ -#include "material_impl.h" - -#include "memory.h" - -#include - -static void material_make(Material* material, const MaterialDesc* desc) { - assert(material); - assert(desc); - assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL); - material->num_uniforms = desc->num_uniforms; - for (int i = 0; i < desc->num_uniforms; ++i) { - material->uniforms[i] = desc->uniforms[i]; - } -} - -Material* gfx_make_material(const MaterialDesc* desc) { - assert(desc); - Material* material = mem_alloc_material(); - material_make(material, desc); - return material; -} - -void gfx_destroy_material(Material** material) { mem_free_material(material); } - -static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) { - switch (uniform->type) { - case UniformTexture: - gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture); - break; - case UniformMat4: - gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.mat4); - break; - case UniformVec3: - gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.vec3); - break; - case UniformVec4: - gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.vec4); - break; - case UniformFloat: - gfx_set_float_uniform(prog, uniform->name.str, uniform->value.scalar); - break; - case UniformMat4Array: - gfx_set_mat4_array_uniform( - prog, uniform->name.str, uniform->value.array.values, - uniform->value.array.count); - break; - } -} - -void gfx_material_activate(ShaderProgram* shader, const Material* material) { - assert(material); - for (int i = 0; i < material->num_uniforms; ++i) { - const ShaderUniform* uniform = &material->uniforms[i]; - set_uniform(shader, uniform); - } -} diff --git a/src/llr/material_impl.h b/src/llr/material_impl.h deleted file mode 100644 index 2b7cd89..0000000 --- a/src/llr/material_impl.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -typedef struct ShaderProgram ShaderProgram; - -typedef struct Material { - ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; - int num_uniforms; -} Material; - -/// Activate the material. -/// -/// This configures the shader uniforms that are specific to the material. -void gfx_material_activate(ShaderProgram* shader, const Material* material); diff --git a/src/llr/mesh.c b/src/llr/mesh.c deleted file mode 100644 index 5f9e5d0..0000000 --- a/src/llr/mesh.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "mesh_impl.h" - -#include "memory.h" - -#include - -static void mesh_make(Mesh* mesh, const MeshDesc* desc) { - assert(mesh); - assert(desc); - assert(desc->geometry); - assert(desc->material); - assert(desc->shader); - mesh->geometry = desc->geometry; - mesh->material = desc->material; - mesh->shader = desc->shader; -} - -Mesh* gfx_make_mesh(const MeshDesc* desc) { - Mesh* mesh = mem_alloc_mesh(); - mesh_make(mesh, desc); - return mesh; -} - -void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); } diff --git a/src/llr/mesh_impl.h b/src/llr/mesh_impl.h deleted file mode 100644 index a997d5b..0000000 --- a/src/llr/mesh_impl.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -typedef struct Mesh { - const Geometry* geometry; - const Material* material; - ShaderProgram* shader; // TODO: Move this back to Material? -} Mesh; -- cgit v1.2.3