aboutsummaryrefslogtreecommitdiff
path: root/src/llr/llr.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-10-24 15:40:40 -0700
committer3gg <3gg@shellblade.net>2025-10-24 15:40:40 -0700
commit175c72557b21f356e295a6f8a4acd91b7e744bef (patch)
tree3d2a77481bc112e58a7618ef3b8de20a9e415811 /src/llr/llr.c
parentc2cbe2ef1cb0237efd14fbe87469ee991ad3daa1 (diff)
Consolidate LLR into a single file.
Diffstat (limited to 'src/llr/llr.c')
-rw-r--r--src/llr/llr.c117
1 files changed, 114 insertions, 3 deletions
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
13static const int IRRADIANCE_MAP_WIDTH = 1024; 13static const int IRRADIANCE_MAP_WIDTH = 1024;
14static const int IRRADIANCE_MAP_HEIGHT = 1024; 14static const int IRRADIANCE_MAP_HEIGHT = 1024;
@@ -17,6 +17,117 @@ static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128;
17static const int BRDF_INTEGRATION_MAP_WIDTH = 512; 17static const int BRDF_INTEGRATION_MAP_WIDTH = 512;
18static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; 18static const int BRDF_INTEGRATION_MAP_HEIGHT = 512;
19 19
20static 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
28Light* 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
46void 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
56static 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
66Material* 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
73void gfx_destroy_material(Material** material) { mem_free_material(material); }
74
75static 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.
103static 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
112static 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
123Mesh* gfx_make_mesh(const MeshDesc* desc) {
124 Mesh* mesh = mem_alloc_mesh();
125 mesh_make(mesh, desc);
126 return mesh;
127}
128
129void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); }
130
20/// Initialize renderer state for IBL. 131/// Initialize renderer state for IBL.
21static bool init_ibl(LLR* renderer) { 132static bool init_ibl(LLR* renderer) {
22 assert(renderer); 133 assert(renderer);