diff options
Diffstat (limited to 'src/llr')
| -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 |
8 files changed, 145 insertions, 175 deletions
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; | ||
