aboutsummaryrefslogtreecommitdiff
path: root/src/render
diff options
context:
space:
mode:
Diffstat (limited to 'src/render')
-rw-r--r--src/render/imm.c14
-rw-r--r--src/render/llr.c119
-rw-r--r--src/render/llr_impl.h31
-rw-r--r--src/render/renderer.c5
4 files changed, 20 insertions, 149 deletions
diff --git a/src/render/imm.c b/src/render/imm.c
index 8a93488..7ab8d62 100644
--- a/src/render/imm.c
+++ b/src/render/imm.c
@@ -21,12 +21,14 @@ bool gfx_imm_make(Imm* renderer, GfxCore* gfxcore, LLR* llr) {
21 renderer->llr = llr; 21 renderer->llr = llr;
22 22
23 renderer->triangles = gfx_make_geometry( 23 renderer->triangles = gfx_make_geometry(
24 gfxcore, 24 gfxcore, &(GeometryDesc){
25 &(GeometryDesc){.type = Triangles, 25 .type = Triangles,
26 .buffer_usage = BufferDynamic, 26 .buffer_usage = BufferDynamic,
27 .num_verts = num_triangle_verts, 27 .num_verts = num_triangle_verts,
28 .positions3d = (BufferView3d){ 28 .positions3d = (BufferView3d){
29 .size_bytes = num_triangle_verts * sizeof(vec3)}}); 29 .size_bytes = num_triangle_verts * sizeof(vec3),
30 .count = num_triangle_verts}
31 });
30 if (!renderer->triangles) { 32 if (!renderer->triangles) {
31 goto cleanup; 33 goto cleanup;
32 } 34 }
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
diff --git a/src/render/llr_impl.h b/src/render/llr_impl.h
index 3a5455a..9d70843 100644
--- a/src/render/llr_impl.h
+++ b/src/render/llr_impl.h
@@ -10,43 +10,12 @@
10#include <stddef.h> 10#include <stddef.h>
11#include <stdint.h> 11#include <stdint.h>
12 12
13typedef struct Geometry Geometry;
14typedef struct GfxCore GfxCore; 13typedef struct GfxCore GfxCore;
15typedef struct IBL IBL; 14typedef struct IBL IBL;
16typedef struct Material Material; 15typedef struct Material Material;
17typedef struct ShaderProgram ShaderProgram; 16typedef struct ShaderProgram ShaderProgram;
18typedef struct Texture Texture; 17typedef struct Texture Texture;
19 18
20/// An environment light.
21typedef struct EnvironmentLight {
22 const Texture* environment_map;
23 const Texture* irradiance_map; // Renderer implementation.
24 const Texture* prefiltered_environment_map; // Renderer implementation.
25 int max_reflection_lod; // Mandatory when prefiltered_environment_map is
26 // given.
27} EnvironmentLight;
28
29/// A scene light.
30typedef struct Light {
31 LightType type;
32 union {
33 EnvironmentLight environment;
34 };
35} Light;
36
37typedef struct Material {
38 AlphaMode alpha_mode;
39 float alpha_cutoff;
40 int8_t num_uniforms;
41 ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL];
42} Material;
43
44typedef struct Mesh {
45 const Geometry* geometry;
46 const Material* material;
47 ShaderProgram* shader; // TODO: Move this back to Material?
48} Mesh;
49
50/// Immediate mode renderer. 19/// Immediate mode renderer.
51/// 20///
52/// The renderer caches state changes in memory and only programs the underlying 21/// The renderer caches state changes in memory and only programs the underlying
diff --git a/src/render/renderer.c b/src/render/renderer.c
index 26b63bc..a9d9bef 100644
--- a/src/render/renderer.c
+++ b/src/render/renderer.c
@@ -3,10 +3,11 @@
3#include "animation_impl.h" 3#include "animation_impl.h"
4#include "llr_impl.h" 4#include "llr_impl.h"
5#include "memory.h" 5#include "memory.h"
6#include "scene/material_impl.h"
7#include "scene/mesh_impl.h"
6#include "scene/model_impl.h" 8#include "scene/model_impl.h"
7#include "scene/node_impl.h" 9#include "scene/node_impl.h"
8#include "scene/object_impl.h" 10#include "scene/object_impl.h"
9#include "scene/scene_impl.h"
10 11
11#include <gfx/core.h> 12#include <gfx/core.h>
12#include <gfx/render/llr.h> 13#include <gfx/render/llr.h>
@@ -236,6 +237,8 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) {
236 // potentially renders the scene multiple times as needed. For example, a 237 // potentially renders the scene multiple times as needed. For example, a
237 // depth-prepass, followed by G-buffer, followed by some post-processing, 238 // depth-prepass, followed by G-buffer, followed by some post-processing,
238 // etc. Rename this renderer to scene_renderer? 239 // etc. Rename this renderer to scene_renderer?
240 // TODO: When rendering transparent geometry, we need to turn off depth
241 // writes.
239 // Opaque. 242 // Opaque.
240 state.filter = RenderOpaqueAndAlphaMasked; 243 state.filter = RenderOpaqueAndAlphaMasked;
241 draw_recursively(&state, mat4_id(), gfx_get_scene_root(scene)); 244 draw_recursively(&state, mat4_id(), gfx_get_scene_root(scene));