aboutsummaryrefslogtreecommitdiff
path: root/src/render/llr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/render/llr.c')
-rw-r--r--src/render/llr.c119
1 files changed, 8 insertions, 111 deletions
diff --git a/src/render/llr.c b/src/render/llr.c
index c9c6d34..752b65b 100644
--- a/src/render/llr.c
+++ b/src/render/llr.c
@@ -1,14 +1,15 @@
1#include "llr_impl.h" 1#include "llr_impl.h"
2 2
3#include "animation_impl.h" 3#include "animation_impl.h"
4#include "memory.h" 4#include "scene/light_impl.h"
5#include "scene/material_impl.h"
6#include "scene/mesh_impl.h"
5#include "scene/node_impl.h" 7#include "scene/node_impl.h"
6 8
7#include <gfx/core.h> 9#include <gfx/core.h>
8#include <gfx/util/ibl.h> 10#include <gfx/util/ibl.h>
9 11
10#include <cassert.h> 12#include <cassert.h>
11#include <error.h>
12 13
13static const int IRRADIANCE_MAP_WIDTH = 1024; 14static const int IRRADIANCE_MAP_WIDTH = 1024;
14static const int IRRADIANCE_MAP_HEIGHT = 1024; 15static const int IRRADIANCE_MAP_HEIGHT = 1024;
@@ -17,108 +18,23 @@ static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128;
17static const int BRDF_INTEGRATION_MAP_WIDTH = 512; 18static const int BRDF_INTEGRATION_MAP_WIDTH = 512;
18static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; 19static const int BRDF_INTEGRATION_MAP_HEIGHT = 512;
19 20
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 mem_free_light(light);
50 }
51}
52
53static void material_make(Material* material, const MaterialDesc* desc) {
54 assert(material);
55 assert(desc);
56 assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL);
57 material->alpha_mode = desc->alpha_mode;
58 material->alpha_cutoff = desc->alpha_cutoff;
59 material->num_uniforms = (int8_t)desc->num_uniforms;
60 for (int i = 0; i < desc->num_uniforms; ++i) {
61 material->uniforms[i] = desc->uniforms[i];
62 }
63}
64
65Material* gfx_make_material(const MaterialDesc* desc) {
66 assert(desc);
67 Material* material = mem_alloc_material();
68 material_make(material, desc);
69 return material;
70}
71
72void gfx_destroy_material(Material** material) { mem_free_material(material); }
73
74// TODO: Move this to core/shader_program.
75static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) {
76 switch (uniform->type) {
77 case UniformInt:
78 gfx_set_int_uniform(prog, uniform->name.str, uniform->value.uniform_int);
79 break;
80 case UniformFloat:
81 gfx_set_float_uniform(
82 prog, uniform->name.str, uniform->value.uniform_float);
83 break;
84 case UniformMat4:
85 gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.uniform_mat4);
86 break;
87 case UniformVec3:
88 gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.uniform_vec3);
89 break;
90 case UniformVec4:
91 gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.uniform_vec4);
92 break;
93 case UniformTexture:
94 gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture);
95 break;
96 case UniformMat4Array:
97 gfx_set_mat4_array_uniform(
98 prog, uniform->name.str, uniform->value.array.values,
99 uniform->value.array.count);
100 break;
101 }
102}
103
104/// Activate the material. 21/// Activate the material.
105/// 22///
106/// This configures the shader uniforms that are specific to the material. 23/// This configures the shader uniforms that are specific to the material.
107static void gfx_material_activate( 24static void material_activate(ShaderProgram* shader, const Material* material) {
108 ShaderProgram* shader, const Material* material) {
109 assert(material); 25 assert(material);
110 for (int i = 0; i < material->num_uniforms; ++i) { 26 for (int i = 0; i < material->num_uniforms; ++i) {
111 const ShaderUniform* uniform = &material->uniforms[i]; 27 const ShaderUniform* uniform = &material->uniforms[i];
112 set_uniform(shader, uniform); 28 gfx_set_uniform(shader, uniform);
113 } 29 }
114 if (material->alpha_mode != Opaque) { 30 if (material->alpha_mode != Opaque) {
115 set_uniform( 31 gfx_set_uniform(
116 shader, &(ShaderUniform){.name = sstring_make("AlphaMode"), 32 shader, &(ShaderUniform){.name = sstring_make("AlphaMode"),
117 .type = UniformInt, 33 .type = UniformInt,
118 .value.uniform_int = material->alpha_mode}); 34 .value.uniform_int = material->alpha_mode});
119 } 35 }
120 if (material->alpha_mode == Mask) { 36 if (material->alpha_mode == Mask) {
121 set_uniform( 37 gfx_set_uniform(
122 shader, 38 shader,
123 &(ShaderUniform){.name = sstring_make("AlphaCutoff"), 39 &(ShaderUniform){.name = sstring_make("AlphaCutoff"),
124 .type = UniformFloat, 40 .type = UniformFloat,
@@ -126,25 +42,6 @@ static void gfx_material_activate(
126 } 42 }
127} 43}
128 44
129static void mesh_make(Mesh* mesh, const MeshDesc* desc) {
130 assert(mesh);
131 assert(desc);
132 assert(desc->geometry);
133 assert(desc->material);
134 assert(desc->shader);
135 mesh->geometry = desc->geometry;
136 mesh->material = desc->material;
137 mesh->shader = desc->shader;
138}
139
140Mesh* gfx_make_mesh(const MeshDesc* desc) {
141 Mesh* mesh = mem_alloc_mesh();
142 mesh_make(mesh, desc);
143 return mesh;
144}
145
146void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); }
147
148/// Initialize renderer state for IBL. 45/// Initialize renderer state for IBL.
149static bool init_ibl(LLR* renderer) { 46static bool init_ibl(LLR* renderer) {
150 assert(renderer); 47 assert(renderer);
@@ -334,7 +231,7 @@ static void configure_state(LLR* renderer) {
334 231
335 // Geometry may be rendered without a material. 232 // Geometry may be rendered without a material.
336 if (renderer->material) { 233 if (renderer->material) {
337 gfx_material_activate(renderer->shader, renderer->material); 234 material_activate(renderer->shader, renderer->material);
338 } 235 }
339 } 236 }
340 237