From 65d448aad0e6c792b1adba1272efef73b31c4885 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 24 Oct 2025 18:33:36 -0700 Subject: Consolidate renderers --- src/llr/llr.c | 527 ----------------------------------------------------- src/llr/llr_impl.h | 117 ------------ 2 files changed, 644 deletions(-) delete mode 100644 src/llr/llr.c delete mode 100644 src/llr/llr_impl.h (limited to 'src/llr') diff --git a/src/llr/llr.c b/src/llr/llr.c deleted file mode 100644 index a1b37be..0000000 --- a/src/llr/llr.c +++ /dev/null @@ -1,527 +0,0 @@ -#include "llr_impl.h" - -#include "memory.h" -#include "scene/animation_impl.h" -#include "scene/node_impl.h" - -#include -#include - -#include -#include - -static const int IRRADIANCE_MAP_WIDTH = 1024; -static const int IRRADIANCE_MAP_HEIGHT = 1024; -static const int PREFILTERED_ENVIRONMENT_MAP_WIDTH = 128; -static const int PREFILTERED_ENVIRONMENT_MAP_HEIGHT = 128; -static const int BRDF_INTEGRATION_MAP_WIDTH = 512; -static const int BRDF_INTEGRATION_MAP_HEIGHT = 512; - -static void make_environment_light( - Light* light, const EnvironmentLightDesc* desc) { - assert(light); - assert(desc); - light->type = EnvironmentLightType; - light->environment.environment_map = desc->environment_map; -} - -Light* gfx_make_light(const LightDesc* desc) { - assert(desc); - - Light* light = mem_alloc_light(); - - switch (desc->type) { - case EnvironmentLightType: - make_environment_light(light, &desc->light.environment); - break; - default: - log_error("Unhandled light type"); - gfx_destroy_light(&light); - return 0; - } - - return light; -} - -void gfx_destroy_light(Light** light) { - assert(light); - if (*light) { - if ((*light)->parent.val) { - gfx_del_node((*light)->parent); - } - mem_free_light(light); - } -} - -static void material_make(Material* material, const MaterialDesc* desc) { - assert(material); - assert(desc); - assert(desc->num_uniforms < GFX_MAX_UNIFORMS_PER_MATERIAL); - material->num_uniforms = desc->num_uniforms; - for (int i = 0; i < desc->num_uniforms; ++i) { - material->uniforms[i] = desc->uniforms[i]; - } -} - -Material* gfx_make_material(const MaterialDesc* desc) { - assert(desc); - Material* material = mem_alloc_material(); - material_make(material, desc); - return material; -} - -void gfx_destroy_material(Material** material) { mem_free_material(material); } - -static void set_uniform(ShaderProgram* prog, const ShaderUniform* uniform) { - switch (uniform->type) { - case UniformTexture: - gfx_set_texture_uniform(prog, uniform->name.str, uniform->value.texture); - break; - case UniformMat4: - gfx_set_mat4_uniform(prog, uniform->name.str, &uniform->value.mat4); - break; - case UniformVec3: - gfx_set_vec3_uniform(prog, uniform->name.str, uniform->value.vec3); - break; - case UniformVec4: - gfx_set_vec4_uniform(prog, uniform->name.str, uniform->value.vec4); - break; - case UniformFloat: - gfx_set_float_uniform(prog, uniform->name.str, uniform->value.scalar); - break; - case UniformMat4Array: - gfx_set_mat4_array_uniform( - prog, uniform->name.str, uniform->value.array.values, - uniform->value.array.count); - break; - } -} - -/// Activate the material. -/// -/// This configures the shader uniforms that are specific to the material. -static void gfx_material_activate( - ShaderProgram* shader, const Material* material) { - assert(material); - for (int i = 0; i < material->num_uniforms; ++i) { - const ShaderUniform* uniform = &material->uniforms[i]; - set_uniform(shader, uniform); - } -} - -static void mesh_make(Mesh* mesh, const MeshDesc* desc) { - assert(mesh); - assert(desc); - assert(desc->geometry); - assert(desc->material); - assert(desc->shader); - mesh->geometry = desc->geometry; - mesh->material = desc->material; - mesh->shader = desc->shader; -} - -Mesh* gfx_make_mesh(const MeshDesc* desc) { - Mesh* mesh = mem_alloc_mesh(); - mesh_make(mesh, desc); - return mesh; -} - -void gfx_destroy_mesh(Mesh** mesh) { mem_free_mesh(mesh); } - -/// Initialize renderer state for IBL. -static bool init_ibl(LLR* renderer) { - assert(renderer); - assert(!renderer->ibl); - assert(!renderer->brdf_integration_map); - - if (!((renderer->ibl = gfx_make_ibl(renderer->gfxcore)))) { - return false; - } - - if (!((renderer->brdf_integration_map = gfx_make_brdf_integration_map( - renderer->ibl, renderer->gfxcore, BRDF_INTEGRATION_MAP_WIDTH, - BRDF_INTEGRATION_MAP_HEIGHT)))) { - return false; - } - - return true; -} - -/// Compute irradiance and prefiltered environment maps for the light if they -/// have not been already computed. -/// -/// This is done lazily here, and not when the light is created, because we -/// need an IBL instance to do this and it is more convenient for the public -/// API to create lights without worrying about those details. It also makes the -/// public API cheaper, since the maps are only computed when they are actually -/// needed. -static bool set_up_environment_light(LLR* renderer, EnvironmentLight* light) { - assert(renderer); - assert(light); - assert(renderer->ibl); - assert(renderer->brdf_integration_map); - - if (light->irradiance_map) { - assert(light->prefiltered_environment_map); - return true; - } - - // For convenience. - GfxCore* gfxcore = renderer->gfxcore; - - Texture* irradiance_map = 0; - Texture* prefiltered_environment_map = 0; - - if (!((irradiance_map = gfx_make_irradiance_map( - renderer->ibl, gfxcore, light->environment_map, - IRRADIANCE_MAP_WIDTH, IRRADIANCE_MAP_HEIGHT)))) { - goto cleanup; - } - - int max_mip_level = 0; - if (!((prefiltered_environment_map = gfx_make_prefiltered_environment_map( - renderer->ibl, gfxcore, light->environment_map, - PREFILTERED_ENVIRONMENT_MAP_WIDTH, - PREFILTERED_ENVIRONMENT_MAP_HEIGHT, &max_mip_level)))) { - goto cleanup; - } - - light->irradiance_map = irradiance_map; - light->prefiltered_environment_map = prefiltered_environment_map; - light->max_reflection_lod = max_mip_level; - - return true; - -cleanup: - if (irradiance_map) { - gfx_destroy_texture(gfxcore, &irradiance_map); - } - if (prefiltered_environment_map) { - gfx_destroy_texture(gfxcore, &prefiltered_environment_map); - } - return false; -} - -static void configure_light(LLR* renderer, Light* light) { - assert(renderer); - assert(light); - - // For convenience. - ShaderProgram* const shader = renderer->shader; - - switch (light->type) { - case EnvironmentLightType: { - EnvironmentLight* env = &light->environment; - - const bool initialized = set_up_environment_light(renderer, env); - ASSERT(initialized); - assert(env->environment_map); - assert(env->irradiance_map); - assert(env->prefiltered_environment_map); - assert(renderer->brdf_integration_map); - - gfx_set_texture_uniform( - shader, "BRDFIntegrationMap", renderer->brdf_integration_map); - gfx_set_texture_uniform(shader, "Sky", env->environment_map); - gfx_set_texture_uniform(shader, "IrradianceMap", env->irradiance_map); - gfx_set_texture_uniform( - shader, "PrefilteredEnvironmentMap", env->prefiltered_environment_map); - gfx_set_float_uniform( - shader, "MaxReflectionLOD", (float)env->max_reflection_lod); - - break; - } - default: - assert(false); // TODO: Implement other light types. - break; - } -} - -static void configure_state(LLR* renderer) { - assert(renderer); - - // Check if anything changed first so that we don't call gfx_apply_uniforms() - // unnecessarily. - const bool nothing_changed = (renderer->changed_flags == 0); - if (nothing_changed) { - return; - } - // Setting a null shader is also allowed, in which case there is nothing to - // configure. - if (renderer->shader == 0) { - renderer->shader_changed = false; - return; - } - - // For convenience. - ShaderProgram* const shader = renderer->shader; - const mat4* const model = &renderer->matrix_stack[renderer->stack_pointer]; - - // TODO: Check to see which ones the shader actually uses and avoid - // computing the unnecessary matrices. - - if (renderer->matrix_changed || renderer->shader_changed) { - renderer->matrix_changed = false; - - gfx_set_mat4_uniform(shader, "Model", model); - gfx_set_mat4_uniform(shader, "ModelMatrix", model); - } - - // TODO: camera_changed is not set anywhere. Need to think how imm primitive - // rendering and imm mesh rendering work together. We could treat imm - // primitive calls like setting a new shader. - if (renderer->camera_changed || renderer->shader_changed) { - renderer->camera_changed = false; - - // Set all supported camera-related uniforms. Shaders can choose which ones - // to use. - const mat4 modelview = mat4_mul(renderer->view, *model); - const mat4 view_proj = mat4_mul(renderer->projection, renderer->view); - const mat4 mvp = mat4_mul(renderer->projection, modelview); - - gfx_set_mat4_uniform(shader, "Modelview", &modelview); - gfx_set_mat4_uniform(shader, "View", &renderer->view); - gfx_set_mat4_uniform(shader, "Projection", &renderer->projection); - gfx_set_mat4_uniform(shader, "ViewProjection", &view_proj); - gfx_set_mat4_uniform(shader, "MVP", &mvp); - gfx_set_vec3_uniform(shader, "CameraPosition", renderer->camera_position); - gfx_set_mat4_uniform(shader, "CameraRotation", &renderer->camera_rotation); - gfx_set_float_uniform(shader, "Fovy", renderer->fovy); - gfx_set_float_uniform(shader, "Aspect", renderer->aspect); - } - - if (renderer->lights_changed || renderer->shader_changed) { - renderer->lights_changed = false; - - // TODO: Could do better by only setting the lights that have actually - // changed. - // TODO: Will also need to pass the number of lights to the shader once the - // other light types are implemented. - for (int i = 0; i < renderer->num_lights; ++i) { - configure_light(renderer, renderer->lights[i]); - } - } - - if (renderer->skeleton_changed || renderer->shader_changed) { - renderer->skeleton_changed = false; - - if (renderer->num_joints > 0) { - gfx_set_mat4_array_uniform( - shader, "JointMatrices", renderer->joint_matrices, - renderer->num_joints); - } - } - - if (renderer->material_changed || renderer->shader_changed) { - renderer->material_changed = false; - - // Geometry may be rendered without a material. - if (renderer->material) { - gfx_material_activate(renderer->shader, renderer->material); - } - } - - if (renderer->shader_changed) { - renderer->shader_changed = false; - gfx_activate_shader_program(renderer->shader); - } - - // TODO: At present, this results in many redundant calls to - // glGetUniformLocation() and glUniformXyz(). Look at the trace. - // - // TODO: Could add to qapitrace functionality to detect redundant calls and - // other inefficiencies. Maybe ask in the Github first if there would be - // interest in this. - // - // Must be called after activating the program. - gfx_apply_uniforms(renderer->shader); -} - -bool gfx_llr_make(LLR* renderer, GfxCore* gfxcore) { - assert(renderer); - assert(gfxcore); - - renderer->gfxcore = gfxcore; - if (!init_ibl(renderer)) { - goto cleanup; - } - gfx_llr_load_identity(renderer); - renderer->view = mat4_id(); - renderer->projection = mat4_id(); - renderer->camera_rotation = mat4_id(); - return true; - -cleanup: - gfx_llr_destroy(renderer); - return false; -} - -void gfx_llr_destroy(LLR* renderer) { - assert(renderer); - assert(renderer->gfxcore); - - if (renderer->brdf_integration_map) { - gfx_destroy_texture(renderer->gfxcore, &renderer->brdf_integration_map); - } - - // TODO: Do this once the IBL from the scene renderer is gone. - if (renderer->ibl) { - // gfx_destroy_ibl(renderer->gfxcore, &renderer->ibl); - } -} - -void gfx_llr_set_shader(LLR* renderer, ShaderProgram* shader) { - assert(renderer); - // null shader is allowed, so do not assert it. - - // It's important to not set shader_changed unnecessarily, since that would - // re-trigger the setting of uniforms. - if (renderer->shader != shader) { - renderer->shader = shader; - renderer->shader_changed = true; - } -} - -void gfx_llr_push_light(LLR* renderer, Light* light) { - assert(renderer); - assert(light); - assert(renderer->num_lights >= 0); - ASSERT(renderer->num_lights < GFX_LLR_MAX_NUM_LIGHTS); - - renderer->lights[renderer->num_lights++] = light; - renderer->lights_changed = true; -} - -void gfx_llr_pop_light(LLR* renderer) { - assert(renderer); - ASSERT(renderer->num_lights > 0); - - renderer->lights[--renderer->num_lights] = 0; - renderer->lights_changed = true; -} - -void gfx_llr_set_skeleton( - LLR* renderer, const Anima* anima, const Skeleton* skeleton) { - assert(renderer); - assert(anima); - assert(skeleton); - assert(skeleton->num_joints <= GFX_MAX_NUM_JOINTS); - - for (size_t i = 0; i < skeleton->num_joints; ++i) { - const joint_idx joint_index = skeleton->joints[i]; - const Joint* joint = &anima->joints[joint_index]; - renderer->joint_matrices[i] = joint->joint_matrix; - } - renderer->num_joints = skeleton->num_joints; - renderer->skeleton_changed = true; -} - -void gfx_llr_clear_skeleton(LLR* renderer) { - assert(renderer); - - renderer->num_joints = 0; - renderer->skeleton_changed = true; -} - -void gfx_llr_set_material(LLR* renderer, const Material* material) { - assert(renderer); - assert(material); - - renderer->material = material; - renderer->material_changed = true; -} - -void gfx_llr_set_camera(LLR* renderer, const Camera* camera) { - assert(renderer); - - renderer->camera_position = camera->spatial.p; - renderer->camera_rotation = - mat4_rotation(spatial3_transform(&camera->spatial)); - renderer->view = spatial3_inverse_transform(&camera->spatial); - renderer->projection = camera->projection; - // Assuming a perspective matrix. - renderer->fovy = (R)atan(1.0 / (mat4_at(camera->projection, 1, 1))) * 2; - renderer->camera_changed = true; -} - -void gfx_llr_set_projection_matrix(LLR* renderer, const mat4* projection) { - assert(renderer); - - renderer->projection = *projection; - renderer->camera_changed = true; -} - -void gfx_llr_set_aspect(LLR* renderer, float aspect) { - assert(renderer); - - renderer->aspect = aspect; - renderer->camera_changed = true; -} - -void gfx_llr_render_geometry(LLR* renderer, const Geometry* geometry) { - assert(renderer); - assert(geometry); - - configure_state(renderer); - gfx_render_geometry(geometry); -} - -void gfx_llr_render_mesh(LLR* renderer, const Mesh* mesh) { - assert(renderer); - assert(mesh); - assert(mesh->geometry); - assert(mesh->material); - assert(mesh->shader); - - gfx_llr_set_material(renderer, mesh->material); - gfx_llr_set_shader(renderer, mesh->shader); - gfx_llr_render_geometry(renderer, mesh->geometry); -} - -void gfx_llr_load_identity(LLR* renderer) { - assert(renderer); - - renderer->matrix_stack[0] = mat4_id(); - renderer->stack_pointer = 0; - renderer->matrix_changed = true; -} - -void gfx_llr_push_matrix(LLR* renderer, const mat4* matrix) { - assert(renderer); - assert(matrix); - assert(renderer->stack_pointer >= 0); - ASSERT(renderer->stack_pointer < GFX_LLR_MAX_NUM_MATRICES); - - renderer->stack_pointer += 1; - renderer->matrix_stack[renderer->stack_pointer] = - mat4_mul(*matrix, renderer->matrix_stack[renderer->stack_pointer - 1]); - renderer->matrix_changed = true; -} - -void gfx_llr_pop_matrix(LLR* renderer) { - assert(renderer); - ASSERT(renderer->stack_pointer > 0); - - // For debugging, zero out the matrix stack as matrices are popped out. - memset( - &renderer->matrix_stack[renderer->stack_pointer], 0, - sizeof(renderer->matrix_stack[0])); - renderer->stack_pointer -= 1; - renderer->matrix_changed = true; -} - -void gfx_llr_translate(LLR* renderer, vec3 offset) { - assert(renderer); - - const mat4 mat = mat4_translate(offset); - gfx_llr_push_matrix(renderer, &mat); -} - -void gfx_llr_set_model_matrix(LLR* renderer, const mat4* model) { - assert(renderer); - assert(model); - - renderer->matrix_stack[0] = *model; - renderer->stack_pointer = 0; - renderer->matrix_changed = true; -} diff --git a/src/llr/llr_impl.h b/src/llr/llr_impl.h deleted file mode 100644 index 6318ee0..0000000 --- a/src/llr/llr_impl.h +++ /dev/null @@ -1,117 +0,0 @@ -#pragma once - -#include -#include - -#include "scene/types.h" - -#include -#include - -#include -#include -#include - -typedef struct Geometry Geometry; -typedef struct GfxCore GfxCore; -typedef struct IBL IBL; -typedef struct Material Material; -typedef struct ShaderProgram ShaderProgram; -typedef struct Texture Texture; - -/// An environment light. -typedef struct EnvironmentLight { - const Texture* environment_map; - const Texture* irradiance_map; // Renderer implementation. - const Texture* prefiltered_environment_map; // Renderer implementation. - int max_reflection_lod; // Mandatory when prefiltered_environment_map is - // given. -} EnvironmentLight; - -/// A scene light. -typedef struct Light { - LightType type; - union { - EnvironmentLight environment; - }; - node_idx parent; // Parent SceneNode. -} Light; - -typedef struct Material { - ShaderUniform uniforms[GFX_MAX_UNIFORMS_PER_MATERIAL]; - int num_uniforms; -} Material; - -typedef struct Mesh { - const Geometry* geometry; - const Material* material; - ShaderProgram* shader; // TODO: Move this back to Material? -} Mesh; - -/// Immediate mode renderer. -/// -/// The renderer caches state changes in memory and only programs the underlying -/// shader program when a draw call is issued and if anything has changed. This -/// keeps the number of graphics API calls to a minimum, but requires tracking -/// state changes. The 'changed' booleans below fulfill this purpose, and -/// indicate whether a given state has changed since the last draw call. -/// -/// The renderer must combine state changes accordingly. For example, if only -/// the lights have changed, then it is sufficient to update light uniforms in -/// the current shader program. On the other hand, if the shader program has -/// changed, then the renderer must reconfigure it from scratch and set light -/// uniforms, camera uniforms, etc. -/// -/// Note that the shader program API has its own level of caching as well, so -/// reconfiguration at the level of the renderer does not result in the -/// worst-case set of graphics API calls. -typedef struct LLR { - GfxCore* gfxcore; - - union { - struct { - bool shader_changed : 1; // Whether the shader has changed. - bool camera_changed : 1; // Whether the camera parameters have changed. - bool lights_changed : 1; // Whether the lights have changed. - bool skeleton_changed : 1; // Whether the skeleton has changed. - bool material_changed : 1; // Whether the material has changed. - bool matrix_changed : 1; // Whether the matrix stack has changed. - }; - uint8_t changed_flags; - }; - - IBL* ibl; - Texture* brdf_integration_map; - - ShaderProgram* shader; // Active shader. Not owned. - - const Material* material; // Active material. Not owned. - - vec3 camera_position; - mat4 camera_rotation; - mat4 view; // Camera view matrix. - mat4 projection; // Camera projection matrix. - R fovy; // Camera vertical field of view. - R aspect; // Aspect ratio. - - // Lights are not const because environment lights store lazily-computed - // irradiance maps. - Light* lights[GFX_LLR_MAX_NUM_LIGHTS]; // Lights stack. - int num_lights; // Number of lights enabled at a given point in time. It - // points to one past the top of the stack. - - size_t num_joints; - mat4 joint_matrices[GFX_MAX_NUM_JOINTS]; - - // The matrix stack contains pre-multiplied matrices. - // It is also never empty. The top of the stack is an identity matrix when the - // stack is "empty" from the user's perspective. - mat4 matrix_stack[GFX_LLR_MAX_NUM_MATRICES]; - int stack_pointer; // Points to the top of the stack. -} LLR; - -/// Create a new immediate mode renderer. -bool gfx_llr_make(LLR*, GfxCore*); - -/// Destroy the immediate mode renderer. -void gfx_llr_destroy(LLR*); -- cgit v1.2.3