diff options
| author | 3gg <3gg@shellblade.net> | 2025-10-24 15:40:40 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-10-24 15:40:40 -0700 |
| commit | 175c72557b21f356e295a6f8a4acd91b7e744bef (patch) | |
| tree | 3d2a77481bc112e58a7618ef3b8de20a9e415811 | |
| parent | c2cbe2ef1cb0237efd14fbe87469ee991ad3daa1 (diff) | |
Consolidate LLR into a single file.
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | README.md | 14 | ||||
| -rw-r--r-- | include/gfx/llr/light.h | 30 | ||||
| -rw-r--r-- | include/gfx/llr/llr.h | 68 | ||||
| -rw-r--r-- | include/gfx/llr/material.h | 26 | ||||
| -rw-r--r-- | include/gfx/llr/mesh.h | 23 | ||||
| -rw-r--r-- | src/asset/model.c | 3 | ||||
| -rw-r--r-- | src/llr/light.c | 42 | ||||
| -rw-r--r-- | src/llr/light_impl.h | 25 | ||||
| -rw-r--r-- | src/llr/llr.c | 117 | ||||
| -rw-r--r-- | src/llr/llr_impl.h | 31 | ||||
| -rw-r--r-- | src/llr/material.c | 57 | ||||
| -rw-r--r-- | src/llr/material_impl.h | 15 | ||||
| -rw-r--r-- | src/llr/mesh.c | 24 | ||||
| -rw-r--r-- | src/llr/mesh_impl.h | 9 | ||||
| -rw-r--r-- | src/memory.c | 4 | ||||
| -rw-r--r-- | src/renderer/renderer.c | 3 | ||||
| -rw-r--r-- | src/scene/node.c | 2 | ||||
| -rw-r--r-- | src/scene/node_impl.h | 3 | ||||
| -rw-r--r-- | src/scene/object.c | 2 | ||||
| -rw-r--r-- | src/util/skyquad.c | 4 |
21 files changed, 234 insertions, 271 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index de5f0dd..cda7b1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -50,9 +50,6 @@ add_library(gfx SHARED | |||
| 50 | src/core/shader.c | 50 | src/core/shader.c |
| 51 | src/core/texture.c | 51 | src/core/texture.c |
| 52 | src/llr/llr.c | 52 | src/llr/llr.c |
| 53 | src/llr/light.c | ||
| 54 | src/llr/material.c | ||
| 55 | src/llr/mesh.c | ||
| 56 | src/memory.c | 53 | src/memory.c |
| 57 | src/renderer/imm_renderer.c | 54 | src/renderer/imm_renderer.c |
| 58 | src/renderer/renderer.c | 55 | src/renderer/renderer.c |
| @@ -23,7 +23,7 @@ The `Gfx` object represents the graphics subsystem and is at the center of the | |||
| 23 | library's high-level API. The `Gfx` object exposes a render backend (`GfxCore`) | 23 | library's high-level API. The `Gfx` object exposes a render backend (`GfxCore`) |
| 24 | and a `Renderer`, and allows the caller to create `Scene`s. | 24 | and a `Renderer`, and allows the caller to create `Scene`s. |
| 25 | 25 | ||
| 26 | ### Render Backend | 26 | ### Render Backend ("Core") |
| 27 | 27 | ||
| 28 | The render backend (`GfxCore`) is a thin abstraction layer over low-level | 28 | The render backend (`GfxCore`) is a thin abstraction layer over low-level |
| 29 | graphics APIs like OpenGL or Vulkan. It holds GPU resources such as geometry, | 29 | graphics APIs like OpenGL or Vulkan. It holds GPU resources such as geometry, |
| @@ -43,7 +43,17 @@ manage resource lifetime. | |||
| 43 | 43 | ||
| 44 | ### Low-level Renderer | 44 | ### Low-level Renderer |
| 45 | 45 | ||
| 46 | `ImmRenderer` is a low-level, immediate mode renderer. | 46 | The low-level renderer (`LLR`) provides a low-level, immediate-mode interface |
| 47 | on top of the render backend. It understands higher-level concepts to make | ||
| 48 | rendering more convenient (object, mesh, material, light, camera, etc), but is | ||
| 49 | still relatively low-level and does not understand any particular scene data | ||
| 50 | structure. | ||
| 51 | |||
| 52 | ### Immediate-mode Renderer | ||
| 53 | |||
| 54 | The immediate-mode renderer (`Imm`) provides an API to define and render | ||
| 55 | geometry procedurally and on the spot. It should be reserved for debug or | ||
| 56 | not-performance-intensive rendering. | ||
| 47 | 57 | ||
| 48 | ### Scene | 58 | ### Scene |
| 49 | 59 | ||
diff --git a/include/gfx/llr/light.h b/include/gfx/llr/light.h deleted file mode 100644 index 132e344..0000000 --- a/include/gfx/llr/light.h +++ /dev/null | |||
| @@ -1,30 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | typedef struct Texture Texture; | ||
| 4 | |||
| 5 | typedef struct Light Light; | ||
| 6 | |||
| 7 | /// Light type. | ||
| 8 | typedef enum LightType { EnvironmentLightType } LightType; | ||
| 9 | |||
| 10 | /// Describes an environment light. | ||
| 11 | typedef struct EnvironmentLightDesc { | ||
| 12 | const Texture* environment_map; | ||
| 13 | } EnvironmentLightDesc; | ||
| 14 | |||
| 15 | /// Describes a light. | ||
| 16 | typedef struct LightDesc { | ||
| 17 | LightType type; | ||
| 18 | union { | ||
| 19 | EnvironmentLightDesc environment; | ||
| 20 | } light; | ||
| 21 | } LightDesc; | ||
| 22 | |||
| 23 | /// Create a light. | ||
| 24 | Light* gfx_make_light(const LightDesc*); | ||
| 25 | |||
| 26 | /// Destroy the light. | ||
| 27 | /// | ||
| 28 | /// The light is conveniently removed from the scene graph and its parent scene | ||
| 29 | /// node is destroyed. | ||
| 30 | void gfx_destroy_light(Light**); | ||
diff --git a/include/gfx/llr/llr.h b/include/gfx/llr/llr.h index f84b3fb..6d78a50 100644 --- a/include/gfx/llr/llr.h +++ b/include/gfx/llr/llr.h | |||
| @@ -1,5 +1,8 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include <gfx/core.h> | ||
| 4 | #include <gfx/sizes.h> | ||
| 5 | |||
| 3 | #include <math/camera.h> | 6 | #include <math/camera.h> |
| 4 | #include <math/mat4.h> | 7 | #include <math/mat4.h> |
| 5 | #include <math/vec3.h> | 8 | #include <math/vec3.h> |
| @@ -11,10 +14,75 @@ typedef struct Material Material; | |||
| 11 | typedef struct Mesh Mesh; | 14 | typedef struct Mesh Mesh; |
| 12 | typedef struct ShaderProgram ShaderProgram; | 15 | typedef struct ShaderProgram ShaderProgram; |
| 13 | typedef struct Skeleton Skeleton; | 16 | typedef struct Skeleton Skeleton; |
| 17 | typedef struct Texture Texture; | ||
| 14 | 18 | ||
| 15 | typedef struct LLR LLR; | 19 | typedef struct LLR LLR; |
| 16 | 20 | ||
| 17 | // ----------------------------------------------------------------------------- | 21 | // ----------------------------------------------------------------------------- |
| 22 | // Data structures. | ||
| 23 | |||
| 24 | /// Light type. | ||
| 25 | typedef enum LightType { EnvironmentLightType } LightType; | ||
| 26 | |||
| 27 | /// Describes an environment light. | ||
| 28 | typedef struct EnvironmentLightDesc { | ||
| 29 | const Texture* environment_map; | ||
| 30 | } EnvironmentLightDesc; | ||
| 31 | |||
| 32 | /// Describes a light. | ||
| 33 | typedef struct LightDesc { | ||
| 34 | LightType type; | ||
| 35 | union { | ||
| 36 | EnvironmentLightDesc environment; | ||
| 37 | } light; | ||
| 38 | } LightDesc; | ||
| 39 | |||
| 40 | /// Describes a material. | ||
| 41 | /// | ||
| 42 | /// TODO: It doesn't hold the shader program anymore...It's in the Mesh. | ||
| 43 | /// A material holds a shader program and a set of shader-specific uniform | ||
| 44 | /// variables. Two materials can share the same shader, but shader parameters | ||
| 45 | /// generally give two materials a different appearance. | ||
| 46 | typedef struct MaterialDesc { | ||
| 47 | ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; | ||
| 48 | int num_uniforms; | ||
| 49 | } MaterialDesc; | ||
| 50 | |||
| 51 | /// Describes a mesh. | ||
| 52 | typedef struct MeshDesc { | ||
| 53 | const Geometry* geometry; | ||
| 54 | const Material* material; | ||
| 55 | ShaderProgram* shader; | ||
| 56 | } MeshDesc; | ||
| 57 | |||
| 58 | /// Create a light. | ||
| 59 | Light* gfx_make_light(const LightDesc*); | ||
| 60 | |||
| 61 | /// Destroy the light. | ||
| 62 | /// | ||
| 63 | /// The light is conveniently removed from the scene graph and its parent scene | ||
| 64 | /// node is destroyed. | ||
| 65 | void gfx_destroy_light(Light**); | ||
| 66 | |||
| 67 | /// Create a material. | ||
| 68 | Material* gfx_make_material(const MaterialDesc*); | ||
| 69 | |||
| 70 | /// Destroy the material. | ||
| 71 | /// | ||
| 72 | /// The caller must make sure that no Mesh points to the given Material. | ||
| 73 | /// For a safe purge of unused resources, see scene_purge(). | ||
| 74 | void gfx_destroy_material(Material**); | ||
| 75 | |||
| 76 | /// Create a mesh. | ||
| 77 | Mesh* gfx_make_mesh(const MeshDesc*); | ||
| 78 | |||
| 79 | /// Destroy the mesh. | ||
| 80 | /// | ||
| 81 | /// The caller must make sure that no SceneObject points to the given Mesh. | ||
| 82 | /// For a safe purge of unused resources, see scene_purge(). | ||
| 83 | void gfx_destroy_mesh(Mesh**); | ||
| 84 | |||
| 85 | // ----------------------------------------------------------------------------- | ||
| 18 | // Low-level rendering. | 86 | // Low-level rendering. |
| 19 | 87 | ||
| 20 | /// Set the shader to be used for subsequent draw calls. | 88 | /// Set the shader to be used for subsequent draw calls. |
diff --git a/include/gfx/llr/material.h b/include/gfx/llr/material.h deleted file mode 100644 index 9ebf9b3..0000000 --- a/include/gfx/llr/material.h +++ /dev/null | |||
| @@ -1,26 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <gfx/core.h> | ||
| 4 | #include <gfx/sizes.h> | ||
| 5 | |||
| 6 | typedef struct Material Material; | ||
| 7 | |||
| 8 | /// Describes a material. | ||
| 9 | /// | ||
| 10 | /// TODO: It doesn't hold the shader program anymore...It's in the Mesh. | ||
| 11 | /// A material holds a shader program and a set of shader-specific uniform | ||
| 12 | /// variables. Two materials can share the same shader, but shader parameters | ||
| 13 | /// generally give two materials a different appearance. | ||
| 14 | typedef struct MaterialDesc { | ||
| 15 | ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; | ||
| 16 | int num_uniforms; | ||
| 17 | } MaterialDesc; | ||
| 18 | |||
| 19 | /// Create a material. | ||
| 20 | Material* gfx_make_material(const MaterialDesc*); | ||
| 21 | |||
| 22 | /// Destroy the material. | ||
| 23 | /// | ||
| 24 | /// The caller must make sure that no Mesh points to the given Material. | ||
| 25 | /// For a safe purge of unused resources, see scene_purge(). | ||
| 26 | void gfx_destroy_material(Material**); | ||
diff --git a/include/gfx/llr/mesh.h b/include/gfx/llr/mesh.h deleted file mode 100644 index 0d3b4d4..0000000 --- a/include/gfx/llr/mesh.h +++ /dev/null | |||
| @@ -1,23 +0,0 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | typedef struct Geometry Geometry; | ||
| 4 | typedef struct Material Material; | ||
| 5 | typedef struct ShaderProgram ShaderProgram; | ||
| 6 | |||
| 7 | typedef struct Mesh Mesh; | ||
| 8 | |||
| 9 | /// Describes a mesh. | ||
| 10 | typedef struct MeshDesc { | ||
| 11 | const Geometry* geometry; | ||
| 12 | const Material* material; | ||
| 13 | ShaderProgram* shader; | ||
| 14 | } MeshDesc; | ||
| 15 | |||
| 16 | /// Create a mesh. | ||
| 17 | Mesh* gfx_make_mesh(const MeshDesc*); | ||
| 18 | |||
| 19 | /// Destroy the mesh. | ||
| 20 | /// | ||
| 21 | /// The caller must make sure that no SceneObject points to the given Mesh. | ||
| 22 | /// For a safe purge of unused resources, see scene_purge(). | ||
| 23 | void gfx_destroy_mesh(Mesh**); | ||
diff --git a/src/asset/model.c b/src/asset/model.c index 0c57470..96d200f 100644 --- a/src/asset/model.c +++ b/src/asset/model.c | |||
| @@ -84,8 +84,7 @@ | |||
| 84 | #include "asset/texture.h" | 84 | #include "asset/texture.h" |
| 85 | #include "gfx/core.h" | 85 | #include "gfx/core.h" |
| 86 | #include "gfx/gfx.h" | 86 | #include "gfx/gfx.h" |
| 87 | #include "gfx/llr/material.h" | 87 | #include "gfx/llr/llr.h" |
| 88 | #include "gfx/llr/mesh.h" | ||
| 89 | #include "gfx/scene/animation.h" | 88 | #include "gfx/scene/animation.h" |
| 90 | #include "gfx/scene/camera.h" | 89 | #include "gfx/scene/camera.h" |
| 91 | #include "gfx/scene/node.h" | 90 | #include "gfx/scene/node.h" |
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 @@ | |||
| 1 | #include "light_impl.h" | ||
| 2 | |||
| 3 | #include "memory.h" | ||
| 4 | #include "scene/node_impl.h" | ||
| 5 | |||
| 6 | #include <error.h> | ||
| 7 | |||
| 8 | static void make_environment_light( | ||
| 9 | Light* light, const EnvironmentLightDesc* desc) { | ||
| 10 | assert(light); | ||
| 11 | assert(desc); | ||
| 12 | light->type = EnvironmentLightType; | ||
| 13 | light->environment.environment_map = desc->environment_map; | ||
| 14 | } | ||
| 15 | |||
| 16 | Light* gfx_make_light(const LightDesc* desc) { | ||
| 17 | assert(desc); | ||
| 18 | |||
| 19 | Light* light = mem_alloc_light(); | ||
| 20 | |||
| 21 | switch (desc->type) { | ||
| 22 | case EnvironmentLightType: | ||
| 23 | make_environment_light(light, &desc->light.environment); | ||
| 24 | break; | ||
| 25 | default: | ||
| 26 | log_error("Unhandled light type"); | ||
| 27 | gfx_destroy_light(&light); | ||
| 28 | return 0; | ||
| 29 | } | ||
| 30 | |||
| 31 | return light; | ||
| 32 | } | ||
| 33 | |||
| 34 | void gfx_destroy_light(Light** light) { | ||
| 35 | assert(light); | ||
| 36 | if (*light) { | ||
| 37 | if ((*light)->parent.val) { | ||
| 38 | gfx_del_node((*light)->parent); | ||
| 39 | } | ||
| 40 | mem_free_light(light); | ||
| 41 | } | ||
| 42 | } | ||
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 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <gfx/llr/light.h> | ||
| 4 | |||
| 5 | #include "scene/types.h" | ||
| 6 | |||
| 7 | typedef struct Texture Texture; | ||
| 8 | |||
| 9 | /// An environment light. | ||
| 10 | typedef struct EnvironmentLight { | ||
| 11 | const Texture* environment_map; | ||
| 12 | const Texture* irradiance_map; // Renderer implementation. | ||
| 13 | const Texture* prefiltered_environment_map; // Renderer implementation. | ||
| 14 | int max_reflection_lod; // Mandatory when prefiltered_environment_map is | ||
| 15 | // given. | ||
| 16 | } EnvironmentLight; | ||
| 17 | |||
| 18 | /// A scene light. | ||
| 19 | typedef struct Light { | ||
| 20 | LightType type; | ||
| 21 | union { | ||
| 22 | EnvironmentLight environment; | ||
| 23 | }; | ||
| 24 | node_idx parent; // Parent SceneNode. | ||
| 25 | } 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 @@ | |||
| 1 | #include "light_impl.h" | ||
| 2 | #include "llr_impl.h" | 1 | #include "llr_impl.h" |
| 3 | #include "mesh_impl.h" | ||
| 4 | 2 | ||
| 5 | #include "llr/material_impl.h" | 3 | #include "memory.h" |
| 6 | #include "scene/animation_impl.h" | 4 | #include "scene/animation_impl.h" |
| 5 | #include "scene/node_impl.h" | ||
| 7 | 6 | ||
| 8 | #include <gfx/core.h> | 7 | #include <gfx/core.h> |
| 9 | #include <gfx/util/ibl.h> | 8 | #include <gfx/util/ibl.h> |
| 10 | 9 | ||
| 11 | #include <cassert.h> | 10 | #include <cassert.h> |
| 11 | #include <error.h> | ||
| 12 | 12 | ||
| 13 | static const int IRRADIANCE_MAP_WIDTH = 1024; | 13 | static const int IRRADIANCE_MAP_WIDTH = 1024; |
| 14 | static const int IRRADIANCE_MAP_HEIGHT = 1024; | 14 | static const int IRRADIANCE_MAP_HEIGHT = 1024; |
| @@ -17,6 +17,117 @@ static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128; | |||
| 17 | static const int BRDF_INTEGRATION_MAP_WIDTH = 512; | 17 | static const int BRDF_INTEGRATION_MAP_WIDTH = 512; |
| 18 | static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; | 18 | static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; |
| 19 | 19 | ||
| 20 | static void make_environment_light( | ||
| 21 | Light* light, const EnvironmentLightDesc* desc) { | ||
| 22 | assert(light); | ||
| 23 | assert(desc); | ||
| 24 | light->type = EnvironmentLightType; | ||
| 25 | light->environment.environment_map = desc->environment_map; | ||
| 26 | } | ||
| 27 | |||
| 28 | Light* gfx_make_light(const LightDesc* desc) { | ||
| 29 | assert(desc); | ||
| 30 | |||
| 31 | Light* light = mem_alloc_light(); | ||
| 32 | |||
| 33 | switch (desc->type) { | ||
| 34 | case EnvironmentLightType: | ||
| 35 | make_environment_light(light, &desc->light.environment); | ||
| 36 | break; | ||
| 37 | default: | ||
| 38 | log_error("Unhandled light type"); | ||
| 39 | gfx_destroy_light(&light); | ||
| 40 | return 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | return light; | ||
| 44 | } | ||
| 45 | |||
| 46 | void gfx_destroy_light(Light** light) { | ||
| 47 | assert(light); | ||
| 48 | if (*light) { | ||
| 49 | if ((*light)->parent.val) { | ||
| 50 | gfx_del_node((*light)->parent); | ||
| 51 | } | ||
| 52 | mem_free_light(light); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | static void material_make(Material* material, const MaterialDesc* desc) { | ||
| 57 | assert(material); | ||
| 58 | assert(desc); | ||
| 59 | assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL); | ||
| 60 | material->num_uniforms = desc->num_uniforms; | ||
| 61 | for (int i = 0; i < desc->num_uniforms; ++i) { | ||
| 62 | material->uniforms[i] = desc->uniforms[i]; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | Material* gfx_make_material(const MaterialDesc* desc) { | ||
| 67 | assert(desc); | ||
| 68 | Material* material = mem_alloc_material(); | ||
| 69 | material_make(material, desc); | ||
| 70 | return material; | ||
| 71 | } | ||
| 72 | |||
| 73 | void gfx_destroy_material(Material** material) { mem_free_material(material); } | ||
| 74 | |||
| 75 | static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) { | ||
| 76 | switch (uniform->type) { | ||
| 77 | case UniformTexture: | ||
| 78 | gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture); | ||
| 79 | break; | ||
| 80 | case UniformMat4: | ||
| 81 | gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.mat4); | ||
| 82 | break; | ||
| 83 | case UniformVec3: | ||
| 84 | gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.vec3); | ||
| 85 | break; | ||
| 86 | case UniformVec4: | ||
| 87 | gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.vec4); | ||
| 88 | break; | ||
| 89 | case UniformFloat: | ||
| 90 | gfx_set_float_uniform(prog, uniform->name.str, uniform->value.scalar); | ||
| 91 | break; | ||
| 92 | case UniformMat4Array: | ||
| 93 | gfx_set_mat4_array_uniform( | ||
| 94 | prog, uniform->name.str, uniform->value.array.values, | ||
| 95 | uniform->value.array.count); | ||
| 96 | break; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | /// Activate the material. | ||
| 101 | /// | ||
| 102 | /// This configures the shader uniforms that are specific to the material. | ||
| 103 | static void gfx_material_activate( | ||
| 104 | ShaderProgram* shader, const Material* material) { | ||
| 105 | assert(material); | ||
| 106 | for (int i = 0; i < material->num_uniforms; ++i) { | ||
| 107 | const ShaderUniform* uniform = &material->uniforms[i]; | ||
| 108 | set_uniform(shader, uniform); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | static void mesh_make(Mesh* mesh, const MeshDesc* desc) { | ||
| 113 | assert(mesh); | ||
| 114 | assert(desc); | ||
| 115 | assert(desc->geometry); | ||
| 116 | assert(desc->material); | ||
| 117 | assert(desc->shader); | ||
| 118 | mesh->geometry = desc->geometry; | ||
| 119 | mesh->material = desc->material; | ||
| 120 | mesh->shader = desc->shader; | ||
| 121 | } | ||
| 122 | |||
| 123 | Mesh* gfx_make_mesh(const MeshDesc* desc) { | ||
| 124 | Mesh* mesh = mem_alloc_mesh(); | ||
| 125 | mesh_make(mesh, desc); | ||
| 126 | return mesh; | ||
| 127 | } | ||
| 128 | |||
| 129 | void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); } | ||
| 130 | |||
| 20 | /// Initialize renderer state for IBL. | 131 | /// Initialize renderer state for IBL. |
| 21 | static bool init_ibl(LLR* renderer) { | 132 | static bool init_ibl(LLR* renderer) { |
| 22 | assert(renderer); | 133 | 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 @@ | |||
| 3 | #include <gfx/llr/llr.h> | 3 | #include <gfx/llr/llr.h> |
| 4 | #include <gfx/sizes.h> | 4 | #include <gfx/sizes.h> |
| 5 | 5 | ||
| 6 | #include "scene/types.h" | ||
| 7 | |||
| 6 | #include <math/mat4.h> | 8 | #include <math/mat4.h> |
| 7 | #include <math/vec3.h> | 9 | #include <math/vec3.h> |
| 8 | 10 | ||
| @@ -17,6 +19,35 @@ typedef struct Material Material; | |||
| 17 | typedef struct ShaderProgram ShaderProgram; | 19 | typedef struct ShaderProgram ShaderProgram; |
| 18 | typedef struct Texture Texture; | 20 | typedef struct Texture Texture; |
| 19 | 21 | ||
| 22 | /// An environment light. | ||
| 23 | typedef struct EnvironmentLight { | ||
| 24 | const Texture* environment_map; | ||
| 25 | const Texture* irradiance_map; // Renderer implementation. | ||
| 26 | const Texture* prefiltered_environment_map; // Renderer implementation. | ||
| 27 | int max_reflection_lod; // Mandatory when prefiltered_environment_map is | ||
| 28 | // given. | ||
| 29 | } EnvironmentLight; | ||
| 30 | |||
| 31 | /// A scene light. | ||
| 32 | typedef struct Light { | ||
| 33 | LightType type; | ||
| 34 | union { | ||
| 35 | EnvironmentLight environment; | ||
| 36 | }; | ||
| 37 | node_idx parent; // Parent SceneNode. | ||
| 38 | } Light; | ||
| 39 | |||
| 40 | typedef struct Material { | ||
| 41 | ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; | ||
| 42 | int num_uniforms; | ||
| 43 | } Material; | ||
| 44 | |||
| 45 | typedef struct Mesh { | ||
| 46 | const Geometry* geometry; | ||
| 47 | const Material* material; | ||
| 48 | ShaderProgram* shader; // TODO: Move this back to Material? | ||
| 49 | } Mesh; | ||
| 50 | |||
| 20 | /// Immediate mode renderer. | 51 | /// Immediate mode renderer. |
| 21 | /// | 52 | /// |
| 22 | /// The renderer caches state changes in memory and only programs the underlying | 53 | /// 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 @@ | |||
| 1 | #include "material_impl.h" | ||
| 2 | |||
| 3 | #include "memory.h" | ||
| 4 | |||
| 5 | #include <gfx/core.h> | ||
| 6 | |||
| 7 | static void material_make(Material* material, const MaterialDesc* desc) { | ||
| 8 | assert(material); | ||
| 9 | assert(desc); | ||
| 10 | assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL); | ||
| 11 | material->num_uniforms = desc->num_uniforms; | ||
| 12 | for (int i = 0; i < desc->num_uniforms; ++i) { | ||
| 13 | material->uniforms[i] = desc->uniforms[i]; | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | Material* gfx_make_material(const MaterialDesc* desc) { | ||
| 18 | assert(desc); | ||
| 19 | Material* material = mem_alloc_material(); | ||
| 20 | material_make(material, desc); | ||
| 21 | return material; | ||
| 22 | } | ||
| 23 | |||
| 24 | void gfx_destroy_material(Material** material) { mem_free_material(material); } | ||
| 25 | |||
| 26 | static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) { | ||
| 27 | switch (uniform->type) { | ||
| 28 | case UniformTexture: | ||
| 29 | gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture); | ||
| 30 | break; | ||
| 31 | case UniformMat4: | ||
| 32 | gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.mat4); | ||
| 33 | break; | ||
| 34 | case UniformVec3: | ||
| 35 | gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.vec3); | ||
| 36 | break; | ||
| 37 | case UniformVec4: | ||
| 38 | gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.vec4); | ||
| 39 | break; | ||
| 40 | case UniformFloat: | ||
| 41 | gfx_set_float_uniform(prog, uniform->name.str, uniform->value.scalar); | ||
| 42 | break; | ||
| 43 | case UniformMat4Array: | ||
| 44 | gfx_set_mat4_array_uniform( | ||
| 45 | prog, uniform->name.str, uniform->value.array.values, | ||
| 46 | uniform->value.array.count); | ||
| 47 | break; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | void gfx_material_activate(ShaderProgram* shader, const Material* material) { | ||
| 52 | assert(material); | ||
| 53 | for (int i = 0; i < material->num_uniforms; ++i) { | ||
| 54 | const ShaderUniform* uniform = &material->uniforms[i]; | ||
| 55 | set_uniform(shader, uniform); | ||
| 56 | } | ||
| 57 | } | ||
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 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <gfx/llr/material.h> | ||
| 4 | |||
| 5 | typedef struct ShaderProgram ShaderProgram; | ||
| 6 | |||
| 7 | typedef struct Material { | ||
| 8 | ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; | ||
| 9 | int num_uniforms; | ||
| 10 | } Material; | ||
| 11 | |||
| 12 | /// Activate the material. | ||
| 13 | /// | ||
| 14 | /// This configures the shader uniforms that are specific to the material. | ||
| 15 | 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 @@ | |||
| 1 | #include "mesh_impl.h" | ||
| 2 | |||
| 3 | #include "memory.h" | ||
| 4 | |||
| 5 | #include <assert.h> | ||
| 6 | |||
| 7 | static void mesh_make(Mesh* mesh, const MeshDesc* desc) { | ||
| 8 | assert(mesh); | ||
| 9 | assert(desc); | ||
| 10 | assert(desc->geometry); | ||
| 11 | assert(desc->material); | ||
| 12 | assert(desc->shader); | ||
| 13 | mesh->geometry = desc->geometry; | ||
| 14 | mesh->material = desc->material; | ||
| 15 | mesh->shader = desc->shader; | ||
| 16 | } | ||
| 17 | |||
| 18 | Mesh* gfx_make_mesh(const MeshDesc* desc) { | ||
| 19 | Mesh* mesh = mem_alloc_mesh(); | ||
| 20 | mesh_make(mesh, desc); | ||
| 21 | return mesh; | ||
| 22 | } | ||
| 23 | |||
| 24 | 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 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <gfx/llr/mesh.h> | ||
| 4 | |||
| 5 | typedef struct Mesh { | ||
| 6 | const Geometry* geometry; | ||
| 7 | const Material* material; | ||
| 8 | ShaderProgram* shader; // TODO: Move this back to Material? | ||
| 9 | } Mesh; | ||
diff --git a/src/memory.c b/src/memory.c index 59bf8ca..39ae6ea 100644 --- a/src/memory.c +++ b/src/memory.c | |||
| @@ -2,9 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | #include <gfx/sizes.h> | 3 | #include <gfx/sizes.h> |
| 4 | 4 | ||
| 5 | #include "llr/light_impl.h" | 5 | #include "llr/llr_impl.h" |
| 6 | #include "llr/material_impl.h" | ||
| 7 | #include "llr/mesh_impl.h" | ||
| 8 | #include "scene/animation_impl.h" | 6 | #include "scene/animation_impl.h" |
| 9 | #include "scene/camera_impl.h" | 7 | #include "scene/camera_impl.h" |
| 10 | #include "scene/model_impl.h" | 8 | #include "scene/model_impl.h" |
diff --git a/src/renderer/renderer.c b/src/renderer/renderer.c index 6bcf5cc..7797c83 100644 --- a/src/renderer/renderer.c +++ b/src/renderer/renderer.c | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | #include "renderer_impl.h" | 1 | #include "renderer_impl.h" |
| 2 | 2 | ||
| 3 | #include "llr/light_impl.h" | 3 | #include "llr/llr_impl.h" |
| 4 | #include "llr/mesh_impl.h" | ||
| 5 | #include "memory.h" | 4 | #include "memory.h" |
| 6 | #include "scene/animation_impl.h" | 5 | #include "scene/animation_impl.h" |
| 7 | #include "scene/camera_impl.h" | 6 | #include "scene/camera_impl.h" |
diff --git a/src/scene/node.c b/src/scene/node.c index 9d45aa7..b7cf467 100644 --- a/src/scene/node.c +++ b/src/scene/node.c | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | #include "animation_impl.h" | 3 | #include "animation_impl.h" |
| 4 | #include "camera_impl.h" | 4 | #include "camera_impl.h" |
| 5 | #include "llr/light_impl.h" | 5 | #include "llr/llr_impl.h" |
| 6 | #include "memory.h" | 6 | #include "memory.h" |
| 7 | #include "model_impl.h" | 7 | #include "model_impl.h" |
| 8 | #include "object_impl.h" | 8 | #include "object_impl.h" |
diff --git a/src/scene/node_impl.h b/src/scene/node_impl.h index c79f252..d43ae1e 100644 --- a/src/scene/node_impl.h +++ b/src/scene/node_impl.h | |||
| @@ -13,6 +13,9 @@ | |||
| 13 | /// together form a strict tree hierarchy and not a more general DAG. | 13 | /// together form a strict tree hierarchy and not a more general DAG. |
| 14 | typedef struct SceneNode { | 14 | typedef struct SceneNode { |
| 15 | NodeType type; | 15 | NodeType type; |
| 16 | // TODO: Inline the actual object here and get rid of the indirection and the | ||
| 17 | // extra pools? The "scene camera" is kind of a useless data structure, for | ||
| 18 | // example. | ||
| 16 | union { | 19 | union { |
| 17 | anima_idx anima; | 20 | anima_idx anima; |
| 18 | camera_idx camera; | 21 | camera_idx camera; |
diff --git a/src/scene/object.c b/src/scene/object.c index e985fd5..3b2d498 100644 --- a/src/scene/object.c +++ b/src/scene/object.c | |||
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | #include <gfx/core.h> | 3 | #include <gfx/core.h> |
| 4 | 4 | ||
| 5 | #include "llr/mesh_impl.h" | 5 | #include "llr/llr_impl.h" |
| 6 | #include "memory.h" | 6 | #include "memory.h" |
| 7 | #include "node_impl.h" | 7 | #include "node_impl.h" |
| 8 | 8 | ||
diff --git a/src/util/skyquad.c b/src/util/skyquad.c index 094de67..847bc91 100644 --- a/src/util/skyquad.c +++ b/src/util/skyquad.c | |||
| @@ -1,9 +1,7 @@ | |||
| 1 | #include <gfx/util/skyquad.h> | 1 | #include <gfx/util/skyquad.h> |
| 2 | 2 | ||
| 3 | #include <gfx/core.h> | 3 | #include <gfx/core.h> |
| 4 | #include <gfx/llr/light.h> | 4 | #include <gfx/llr/llr.h> |
| 5 | #include <gfx/llr/material.h> | ||
| 6 | #include <gfx/llr/mesh.h> | ||
| 7 | #include <gfx/scene/node.h> | 5 | #include <gfx/scene/node.h> |
| 8 | #include <gfx/scene/object.h> | 6 | #include <gfx/scene/object.h> |
| 9 | #include <gfx/util/geometry.h> | 7 | #include <gfx/util/geometry.h> |
