From 42c59e0892507c577611b7af7dde90775dfe7aa0 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 27 May 2023 19:28:22 -0700 Subject: Lazy load of IBL state in renderer. --- gfx/src/renderer/renderer.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/gfx/src/renderer/renderer.c b/gfx/src/renderer/renderer.c index 5d88ae6..1e96873 100644 --- a/gfx/src/renderer/renderer.c +++ b/gfx/src/renderer/renderer.c @@ -32,19 +32,6 @@ bool renderer_make(Renderer* renderer, RenderBackend* render_backend) { renderer->render_backend = render_backend; - // TODO: Load the IBL stuff lazily. - if (!(renderer->ibl = gfx_make_ibl(render_backend))) { - renderer_destroy(renderer); - return false; - } - - if (!(renderer->brdf_integration_map = gfx_make_brdf_integration_map( - renderer->ibl, render_backend, BRDF_INTEGRATION_MAP_WIDTH, - BRDF_INTEGRATION_MAP_HEIGHT))) { - renderer_destroy(renderer); - return false; - } - return true; } @@ -58,6 +45,25 @@ void renderer_destroy(Renderer* renderer) { } } +/// Initialize renderer state for IBL if not already initialized. +static bool init_ibl(Renderer* renderer) { + assert(renderer); + + if (!renderer->ibl && + !(renderer->ibl = gfx_make_ibl(renderer->render_backend))) { + return false; + } + + if (!renderer->brdf_integration_map && + !(renderer->brdf_integration_map = gfx_make_brdf_integration_map( + renderer->ibl, renderer->render_backend, BRDF_INTEGRATION_MAP_WIDTH, + BRDF_INTEGRATION_MAP_HEIGHT))) { + return false; + } + + return true; +} + // static void log_matrix(const mat4* m) { // for (int row = 0; row < 4; ++row) { // LOGI("[ %5.2f, %5.2f, %5.2f, %5.2f ]", m->val[0][row], m->val[1][row], @@ -72,7 +78,10 @@ static bool setup_environment_light( EnvironmentLight* light) { assert(renderer); assert(light); - assert(renderer->ibl); + + if (!init_ibl(renderer)) { + return false; + } if (light->irradiance_map) { assert(light->prefiltered_environment_map); @@ -217,6 +226,7 @@ static void draw_recursively( // Apply lights. if (state->environment_light) { const EnvironmentLight* light = &state->environment_light->environment; + assert(light->environment_map); assert(light->irradiance_map); assert(light->prefiltered_environment_map); assert(state->renderer->brdf_integration_map); -- cgit v1.2.3