GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
LightBaker3D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_3D_LIGHT_BAKER_3D_HPP
5#define GREM_GRAPHICS_3D_LIGHT_BAKER_3D_HPP
6
8
12#include <GREM/core/extents.hpp>
14#include <GREM/core/math.hpp>
18
19namespace grem::graphics {
20
21class Device; // Forward declaration, to avoid including Device.hpp.
22class RenderPass; // Forward declaration, to avoid including RenderPass.hpp.
23class Camera3D; // Forward declaration, to avoid including Camera3D.hpp.
24class LightProbeVolumes3D; // Forward declaration, to avoid a circular include of LightProbeVolumes3D.hpp.
25class ReflectionProbes3D; // Forward declaration, to avoid a circular include of ReflectionProbes3D.hpp.
26class Bootstrapper; // Forward declaration.
27
37 uint32_t skyIrradianceSampleCount = 4096;
38
44 uint32_t skyReflectionSampleCount = 4096;
45
53
59 float lightProbeNearZ = 0.001f;
60
66 float lightProbeFarZ = 100.0f;
67
71 float lightProbeExposure = 1.0f;
72
79
86
93
101
107 float reflectionProbeNearZ = 0.01f;
108
114 float reflectionProbeFarZ = 1000.0f;
115
121
129};
130
135public:
149 GREM_API(graphics_3d) LightBaker3D(Device& device, Renderer3D& renderer3D, const LightBaker3DOptions& options = {});
150
164 GREM_API(graphics_3d) void bakeSkybox(Sky3D& sky, Texture newSkyTexture, const Sky3DOptions& newSkyOptions);
165
181 GREM_API(graphics_3d)
182 void bakeSkybox(Sky3D& sky, const resource::ImageView& newSkyImage, const Sky3DOptions& newSkyOptions,
183 const TextureImageUploadOptions& newSkyImageUploadOptions = {.convertToPremultipliedAlpha = false, .generateMipmap = false});
184
205 GREM_API(graphics_3d)
206 void bakeDiffuseLighting(LightProbeVolumes3D& lightProbeVolumes, FunctionView<void(RenderPass&, const Camera3D&)> drawRadiance,
207 FunctionView<void(RenderPass&, const Camera3D&)> drawDistance);
208
232 GREM_API(graphics_3d)
233 void bakeLightProbeIrradianceMap(LightProbeVolumes3D& lightProbeVolumes, uint32_t lightProbeVolumeIndex, u32vec3 lightProbeGridIndices,
234 FunctionView<void(RenderPass&, const Camera3D&)> drawRadiance);
235
253 GREM_API(graphics_3d)
254 void bakeAllLightProbeIrradianceMaps(LightProbeVolumes3D& lightProbeVolumes, FunctionView<void(RenderPass&, const Camera3D&)> drawRadiance);
255
279 GREM_API(graphics_3d)
280 void bakeLightProbeDistanceMap(LightProbeVolumes3D& lightProbeVolumes, uint32_t lightProbeVolumeIndex, u32vec3 lightProbeGridIndices,
281 FunctionView<void(RenderPass&, const Camera3D&)> drawDistance);
282
300 GREM_API(graphics_3d)
301 void bakeAllLightProbeDistanceMaps(LightProbeVolumes3D& lightProbeVolumes, FunctionView<void(RenderPass&, const Camera3D&)> drawDistance);
302
319 GREM_API(graphics_3d) void bakeSpecularReflections(ReflectionProbes3D& reflectionProbes, FunctionView<void(RenderPass&, const Camera3D&)> drawReflection);
320
342 GREM_API(graphics_3d)
343 void bakeReflectionProbeReflectionMap(ReflectionProbes3D& reflectionProbes, uint32_t reflectionProbeIndex, FunctionView<void(RenderPass&, const Camera3D&)> drawReflection);
344
362 GREM_API(graphics_3d)
363 void bakeAllReflectionProbeReflectionMaps(ReflectionProbes3D& reflectionProbes, FunctionView<void(RenderPass&, const Camera3D&)> drawReflection);
364
383 [[nodiscard]] GREM_API(graphics_3d) Texture generateCubemapFromEquirectangularMap(TextureFormat internalFormat, uint32_t resolution, const Texture& equirectangularMap,
386 .magnificationFilter = TextureFilter::LINEAR,
387 .mipmapMode = TextureMipmapMode::NONE,
388 .horizontalWrappingMode = TextureWrappingMode::CLAMP_TO_EDGE,
389 .verticalWrappingMode = TextureWrappingMode::CLAMP_TO_EDGE,
390 .maxAnisotropy = 1.0f,
391 });
392
393private:
394 friend Bootstrapper;
395
396 //==========================================================================
397
398 struct CubemapIrradianceFragmentShaderConstants {
399 uint32_t IRRADIANCE_SAMPLE_COUNT;
400 };
401
402 struct CubemapIrradianceFragmentParameters {
403 samplerCube radianceCubemapTexture;
404 };
405
406 using CubemapIrradianceFragmentParameterBuffer = UniformBuffer<CubemapIrradianceFragmentParameters, "LightBaker3DCubemapIrradianceFragmentParameters">;
407
408 using CubemapIrradianceFragmentShader = graphics::FragmentShader<Cubemap3D::Mesh, Cubemap3D::VertexShaderOutputs, CubemapIrradianceFragmentShaderConstants,
409 Cubemap3D::FragmentShaderOutputs, Cubemap3D::PerspectiveBuffer, CubemapIrradianceFragmentParameterBuffer>;
410
411 //==========================================================================
412
413 struct CubemapReflectionFragmentShaderConstants {
414 uint32_t REFLECTION_SAMPLE_COUNT;
415 };
416
417 struct CubemapReflectionFragmentParameters {
418 samplerCube radianceCubemapTexture;
419 samplerCubeShadow depthCubemapTexture;
420 float reflectionResolution;
421 float reflectionRoughness;
422 };
423
424 using CubemapReflectionFragmentParameterBuffer = UniformBuffer<CubemapReflectionFragmentParameters, "LightBaker3DCubemapReflectionFragmentParameters">;
425
426 using CubemapReflectionFragmentShader = graphics::FragmentShader<Cubemap3D::Mesh, Cubemap3D::VertexShaderOutputs, CubemapReflectionFragmentShaderConstants,
427 Cubemap3D::FragmentShaderOutputs, Cubemap3D::PerspectiveBuffer, CubemapReflectionFragmentParameterBuffer>;
428
429 //==========================================================================
430
431 struct CubemapFromEquirectangularFragmentShaderConstants {};
432
433 struct CubemapFromEquirectangularFragmentParameters {
434 sampler2D equirectangularTexture;
435 };
436
437 using CubemapFromEquirectangularFragmentParameterBuffer =
438 UniformBuffer<CubemapFromEquirectangularFragmentParameters, "LightBaker3DCubemapFromEquirectangularFragmentParameters">;
439
440 using CubemapFromEquirectangularFragmentShader = graphics::FragmentShader<Cubemap3D::Mesh, Cubemap3D::VertexShaderOutputs, CubemapFromEquirectangularFragmentShaderConstants,
441 Cubemap3D::FragmentShaderOutputs, Cubemap3D::PerspectiveBuffer, CubemapFromEquirectangularFragmentParameterBuffer>;
442
443 //==========================================================================
444
445 struct LightProbeAtlasVertex {
446 vec2 vertexPosition;
447 };
448
449 using LightProbeAtlasMesh = Mesh<LightProbeAtlasVertex>;
450
451 struct LightProbeAtlasVertexShaderConstants {};
452
453 struct LightProbeAtlasVertexShaderOutputs {
454 vec2 fragmentTextureCoordinates;
455 };
456
457 using LightProbeAtlasVertexShader = VertexShader<LightProbeAtlasMesh, LightProbeAtlasVertexShaderConstants, LightProbeAtlasVertexShaderOutputs>;
458
459 struct LightProbeAtlasIrradianceFragmentShaderConstants {
460 uint32_t IRRADIANCE_SAMPLE_COUNT;
461 };
462
463 struct LightProbeAtlasDistanceFragmentShaderConstants {
464 uint32_t DISTANCE_SAMPLE_COUNT;
465 };
466
467 struct LightProbeAtlasFragmentShaderOutputs {
468 vec4 outputColor;
469 };
470
471 struct LightProbeAtlasIrradianceFragmentParameters {
472 samplerCube radianceCubemapTexture;
473 float irradianceMapPadding;
474 };
475
476 using LightProbeAtlasIrradianceFragmentParameterBuffer = UniformBuffer<LightProbeAtlasIrradianceFragmentParameters, "LightBaker3DLightProbeAtlasIrradianceFragmentParameters">;
477
478 using LightProbeAtlasIrradianceFragmentShader = FragmentShader<LightProbeAtlasMesh, LightProbeAtlasVertexShaderOutputs, LightProbeAtlasIrradianceFragmentShaderConstants,
479 LightProbeAtlasFragmentShaderOutputs, LightProbeAtlasIrradianceFragmentParameterBuffer>;
480
481 struct LightProbeAtlasDistanceFragmentParameters {
482 samplerCube distanceCubemapTexture;
483 float distanceMapPadding;
484 float distanceSharpness;
485 float distanceMaxDistance;
486 };
487
488 using LightProbeAtlasDistanceFragmentParameterBuffer = UniformBuffer<LightProbeAtlasDistanceFragmentParameters, "LightBaker3DLightProbeAtlasDistanceFragmentParameters">;
489
490 using LightProbeAtlasDistanceFragmentShader = FragmentShader<LightProbeAtlasMesh, LightProbeAtlasVertexShaderOutputs, LightProbeAtlasDistanceFragmentShaderConstants,
491 LightProbeAtlasFragmentShaderOutputs, LightProbeAtlasDistanceFragmentParameterBuffer>;
492
493 using LightProbeAtlasShaderPipeline = ShaderPipeline<LightProbeAtlasMesh>;
494
495 //==========================================================================
496
497 struct CubemapBaker {
498 Array<Cubemap3D::PerspectiveBuffer, 6> sideCubemapPerspectiveBuffers;
499 Array<Camera3D, 6> sideCameras;
500 };
501
502 struct SkyBaker {
503 Cubemap3D::ShaderPipeline cubemapFromEquirectangularShaderPipeline;
504 Cubemap3D::ShaderPipeline cubemapIrradianceShaderPipeline;
505 Cubemap3D::ShaderPipeline cubemapReflectionShaderPipeline;
506 CubemapFromEquirectangularFragmentParameterBuffer cubemapFromEquirectangularFragmentParameterBuffer;
507 CubemapIrradianceFragmentParameterBuffer cubemapIrradianceFragmentParameterBuffer;
508 CubemapReflectionFragmentParameterBuffer cubemapReflectionFragmentParameterBuffer;
509 DrawCommandBuffer<Cubemap3D::Mesh> cubemapFromEquirectangularDrawCommandBuffer;
510 DrawCommandBuffer<Cubemap3D::Mesh> cubemapIrradianceDrawCommandBuffer;
511 DrawCommandBuffer<Cubemap3D::Mesh> cubemapReflectionDrawCommandBuffer;
512 };
513
514 struct LightProbeBaker {
515 Texture colorBuffer;
516 Texture distanceBuffer;
517 Texture depthStencilBuffer;
518 LightProbeAtlasMesh lightProbeAtlasMesh;
519 LightProbeAtlasShaderPipeline lightProbeAtlasIrradianceShaderPipeline;
520 LightProbeAtlasShaderPipeline lightProbeAtlasDistanceShaderPipeline;
521 LightProbeAtlasIrradianceFragmentParameterBuffer lightProbeAtlasIrradianceFragmentParameterBuffer;
522 LightProbeAtlasDistanceFragmentParameterBuffer lightProbeAtlasDistanceFragmentParameterBuffer;
523 DrawCommandBuffer<LightProbeAtlasMesh> lightProbeAtlasIrradianceDrawCommandBuffer;
524 DrawCommandBuffer<LightProbeAtlasMesh> lightProbeAtlasDistanceDrawCommandBuffer;
525 mat4 renderProjectionMatrix;
526 };
527
528 struct ReflectionProbeBaker {
529 Texture colorBuffer;
530 Texture depthStencilBuffer;
531 Cubemap3D::ShaderPipeline cubemapReflectionShaderPipeline;
532 CubemapReflectionFragmentParameterBuffer cubemapReflectionFragmentParameterBuffer;
533 DrawCommandBuffer<Cubemap3D::Mesh> reflectionMapsDrawCommandBuffer;
534 mat4 renderProjectionMatrix;
535 };
536
537 [[nodiscard]] GREM_API(graphics_3d) CubemapBaker& getCubemapBaker();
538 [[nodiscard]] GREM_API(graphics_3d) SkyBaker& getSkyBaker();
539 [[nodiscard]] GREM_API(graphics_3d) LightProbeBaker& getLightProbeBaker();
540 [[nodiscard]] GREM_API(graphics_3d) ReflectionProbeBaker& getReflectionProbeBaker();
541
542 Device& device;
543 Renderer3D& renderer3D;
544 LightBaker3DOptions options;
545 Optional<CubemapBaker> cubemapBakerStorage{};
546 Optional<SkyBaker> skyBakerStorage{};
547 Optional<LightProbeBaker> lightProbeBakerStorage{};
548 Optional<ReflectionProbeBaker> reflectionProbeBakerStorage{};
549};
550
551} // namespace grem::graphics
552
553#endif
Definition FunctionView.hpp:29
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Combined view-projection matrix, defining a 3D perspective to render from.
Definition Camera3D.hpp:173
graphics::Mesh< Vertex, Index > Mesh
Cubemap mesh primitive type.
Definition Cubemap3D.hpp:33
UniformBuffer< PerspectiveParameters, "Cubemap3DPerspective"> PerspectiveBuffer
Shader buffer for cubemap perspective parameters.
Definition Cubemap3D.hpp:54
graphics::ShaderPipeline< Mesh > ShaderPipeline
Shader pipeline for drawing cubemaps.
Definition Cubemap3D.hpp:60
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
void bakeReflectionProbeReflectionMap(ReflectionProbes3D &reflectionProbes, uint32_t reflectionProbeIndex, FunctionView< void(RenderPass &, const Camera3D &)> drawReflection)
Render the reflection map of a single reflection probe in a set of reflection probes.
Texture generateCubemapFromEquirectangularMap(TextureFormat internalFormat, uint32_t resolution, const Texture &equirectangularMap, Optional< TextureSamplerOptions > samplerOptions=TextureSamplerOptions{ .minificationFilter=TextureFilter::LINEAR,.magnificationFilter=TextureFilter::LINEAR,.mipmapMode=TextureMipmapMode::NONE,.horizontalWrappingMode=TextureWrappingMode::CLAMP_TO_EDGE,.verticalWrappingMode=TextureWrappingMode::CLAMP_TO_EDGE,.maxAnisotropy=1.0f, })
Generate a cubemap from a 2D equirectangular map texture.
void bakeDiffuseLighting(LightProbeVolumes3D &lightProbeVolumes, FunctionView< void(RenderPass &, const Camera3D &)> drawRadiance, FunctionView< void(RenderPass &, const Camera3D &)> drawDistance)
Render the irradiance and distance maps of a set of light probe volumes.
void bakeAllLightProbeIrradianceMaps(LightProbeVolumes3D &lightProbeVolumes, FunctionView< void(RenderPass &, const Camera3D &)> drawRadiance)
Render the irradiance maps of all light probes of all volumes in a set of light probe volumes.
void bakeSkybox(Sky3D &sky, Texture newSkyTexture, const Sky3DOptions &newSkyOptions)
Render a sky map texture onto a sky.
void bakeAllReflectionProbeReflectionMaps(ReflectionProbes3D &reflectionProbes, FunctionView< void(RenderPass &, const Camera3D &)> drawReflection)
Render all reflection maps in a set of reflection probes.
void bakeAllLightProbeDistanceMaps(LightProbeVolumes3D &lightProbeVolumes, FunctionView< void(RenderPass &, const Camera3D &)> drawDistance)
Render the distance maps of all light probes of all volumes in a set of light probe volumes.
LightBaker3D(Device &device, Renderer3D &renderer3D, const LightBaker3DOptions &options={})
Construct a light baker.
void bakeLightProbeIrradianceMap(LightProbeVolumes3D &lightProbeVolumes, uint32_t lightProbeVolumeIndex, u32vec3 lightProbeGridIndices, FunctionView< void(RenderPass &, const Camera3D &)> drawRadiance)
Render the irradiance map of a single light probe of a specific volume in a set of light probe volume...
void bakeLightProbeDistanceMap(LightProbeVolumes3D &lightProbeVolumes, uint32_t lightProbeVolumeIndex, u32vec3 lightProbeGridIndices, FunctionView< void(RenderPass &, const Camera3D &)> drawDistance)
Render the distance map of a single light probe of a specific volume in a set of light probe volumes.
void bakeSpecularReflections(ReflectionProbes3D &reflectionProbes, FunctionView< void(RenderPass &, const Camera3D &)> drawReflection)
Render the reflection maps of a set of reflection probes.
Set of light probe volumes in 3D space.
Definition LightProbeVolumes3D.hpp:89
Set of reflection probes in 3D space.
Definition ReflectionProbes3D.hpp:106
Backend-specific implementation of RenderPass.
Definition RenderPass.hpp:38
Persistent system that provides 3D rendering capabilities.
Definition Renderer3D.hpp:120
Skybox in 3D space.
Definition Sky3D.hpp:81
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition Texture.hpp:700
Definition buffer_layouts.hpp:20
@ CLAMP_TO_EDGE
Clamp to the edge of the texture when sampling outside the 0-1 range.
Definition Texture.hpp:492
TextureFormat
Description of the internal format of a Texture.
Definition Texture.hpp:267
@ NONE
Always use the most detailed mip level (level 0).
Definition Texture.hpp:482
@ LINEAR
Interpolate between texels for a smoother appearance.
Definition Texture.hpp:475
Definition Window.hpp:15
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
vec< 4, float > vec4
Definition math.hpp:440
vec< 3, uint32_t > u32vec3
Definition math.hpp:419
vec< 2, float > vec2
Definition math.hpp:438
mat< 4, 4, float > mat4
Definition math.hpp:806
Configuration options for a LightBaker3D.
Definition LightBaker3D.hpp:31
uint32_t lightProbeIrradianceSampleCount
Number of samples to use when baking the irradiance maps of light probes.
Definition LightBaker3D.hpp:78
float lightProbeNearZ
Distance to the near plane of light probes.
Definition LightBaker3D.hpp:59
float reflectionProbeNearZ
Distance to the near plane of reflection probes.
Definition LightBaker3D.hpp:107
uint32_t lightProbeDistanceSampleCount
Number of samples to use when baking the distance maps of light probes.
Definition LightBaker3D.hpp:85
uint32_t reflectionProbeRenderResolution
Width, in texels, of the source cubemaps used to render the reflection maps of reflection probes.
Definition LightBaker3D.hpp:100
uint32_t skyReflectionSampleCount
Number of samples to use when baking the reflection cubemap of skyboxes.
Definition LightBaker3D.hpp:44
uint32_t lightProbeRenderResolution
Width, in texels, of the source cubemaps used to render the irradiance and distance maps of light pro...
Definition LightBaker3D.hpp:52
float reflectionProbeFarZ
Distance to the far plane of reflection probes.
Definition LightBaker3D.hpp:114
uint32_t skyIrradianceSampleCount
Number of samples to use when baking the irradiance cubemap of skyboxes.
Definition LightBaker3D.hpp:37
float lightProbeExposure
Camera exposure to use when baking the irradiance maps of light probes.
Definition LightBaker3D.hpp:71
float lightProbeDistanceSharpness
Exponent of the cosine weight between normal and sample direction to use when baking the distance map...
Definition LightBaker3D.hpp:92
float reflectionProbeExposure
Camera exposure to use when baking the reflection maps of reflection probes.
Definition LightBaker3D.hpp:120
uint32_t reflectionProbeReflectionSampleCount
Number of samples to use when baking the reflection maps of reflection probes.
Definition LightBaker3D.hpp:128
float lightProbeFarZ
Distance to the far plane of light probes.
Definition LightBaker3D.hpp:66
Configuration options for a Sky3D.
Definition Sky3D.hpp:27
Configuration options for a texture image upload.
Definition Texture.hpp:550
bool convertToPremultipliedAlpha
Convert the uploaded image from straight to pre-multiplied alpha.
Definition Texture.hpp:565
Configuration options for the sampler of a Texture.
Definition Texture.hpp:617
TextureFilter minificationFilter
Filtering mode to use when the texture is rendered smaller than its original size.
Definition Texture.hpp:628