Cogs.Core
EngineBindGroups.cpp
1#include "EngineBindGroups.h"
2
3#include "Context.h"
4#include "Renderer.h"
5#include "RenderTarget.h"
6
7#include "Rendering/IContext.h"
8#include "Systems/Core/LightSystem.h"
9#include "Systems/Core/EnvironmentSystem.h"
10#include "Tasks/RenderTask.h"
11
12#include "RenderResources.h"
13#include "RenderTexture.h"
14
15#include "Resources/TextureManager.h"
16#include "Resources/ShaderBuilderHelpersWebGPU.h"
17
18#include "Rendering/IBindGroups.h"
19
20void Cogs::Core::initializeEngineBindGroups(IBindGroups * bindGroups, EngineBindGroups & engineBindGroups)
21{
22 if (!bindGroups) return;
23
24 const BindGroupSetDescription & desc = getEngineBindGroupDescription();
25 const BindGroupDescription & lightTextures = desc.groups[static_cast<size_t>(BindGroup::LightTextures)];
26 if (lightTextures.numEntries == 0) return;
27
28 engineBindGroups.lightTextures = bindGroups->createBindGroup(&lightTextures);
29 if (!HandleIsValid(engineBindGroups.lightTextures)) return;
30
31 BindGroupHandle handle = engineBindGroups.lightTextures;
32 engineBindGroups.skyBinding = bindGroups->getTextureBinding(handle, "environmentSky");
33 engineBindGroups.radianceBinding = bindGroups->getTextureBinding(handle, "environmentRadiance");
34 engineBindGroups.irradianceBinding = bindGroups->getTextureBinding(handle, "environmentIrradiance");
35 engineBindGroups.ambientIrradianceBinding = bindGroups->getTextureBinding(handle, "ambientIrradiance");
36 engineBindGroups.brdfLUTBinding = bindGroups->getTextureBinding(handle, "brdfLUT");
37 engineBindGroups.shadowArrayBinding = bindGroups->getTextureBinding(handle, "cascadedShadowMap");
38 engineBindGroups.shadowCubeArrayBinding = bindGroups->getTextureBinding(handle, "cubeShadowMap");
39
40 engineBindGroups.linearSamplerBinding = bindGroups->getSamplerBinding(handle, "linearSampler");
41 engineBindGroups.linearClampSampler = bindGroups->getSamplerBinding(handle, "linearClampSampler");
42 engineBindGroups.pointSampler = bindGroups->getSamplerBinding(handle, "pointSampler");
43 engineBindGroups.pointClampSampler = bindGroups->getSamplerBinding(handle, "pointClampSampler");
44 engineBindGroups.shadowSamplerBinding = bindGroups->getSamplerBinding(handle, "shadowSampler");
45}
46
47void Cogs::Core::updateLightTexturesBindGroup(RenderTaskContext* taskContext)
48{
49 IGraphicsDevice* device = taskContext->renderer->getDevice();
50 IBindGroups* bindGroups = device->getBindGroups();
51 IContext* deviceContext = device->getImmediateContext();
52 if (!bindGroups) return;
53
54 EngineBindGroups& engineBindGroups = taskContext->renderer->getEngineBindGroups();
55 if (!HandleIsValid(engineBindGroups.lightTextures)) return;
56
57 BindGroupHandle handle = engineBindGroups.lightTextures;
58 RenderResources& renderResources = taskContext->renderer->getRenderResources();
59 TextureManager* textureManager = taskContext->context->textureManager;
60
61 // Every entry in the bind group must be bound to something, so unresolved environment/shadow
62 // slots fall back to the same dummy white/whiteDepth textures used previously by the per-effect
63 // WebGPU dummy-texture path in updateEnvironmentBindings.
67 Cogs::TextureHandle ambientIrradianceTexture = Cogs::TextureHandle::NoHandle;
69
70 const EnvironmentComponent* environmentComponent = taskContext->context->environmentSystem->getGlobalEnvironment();
71 if (environmentComponent) {
72 if (environmentComponent->isSubmerged(taskContext->cameraData)) {
73 if (environmentComponent->subseaRadiance) {
74 RenderTexture* subseaRadianceTexture = renderResources.getRenderTexture(environmentComponent->subseaRadiance);
75 if (subseaRadianceTexture && subseaRadianceTexture->description.target == ResourceDimensions::TextureCube) {
76 skyTexture = subseaRadianceTexture->textureHandle;
77 radianceTexture = subseaRadianceTexture->textureHandle;
78 irradianceTexture = subseaRadianceTexture->textureHandle;
79 ambientIrradianceTexture = subseaRadianceTexture->textureHandle;
80 }
81 }
82 } else {
83 if (environmentComponent->skyDome) {
84 RenderTexture* tex = renderResources.getRenderTexture(environmentComponent->skyDome);
85 if (tex && tex->description.target == ResourceDimensions::TextureCube) skyTexture = tex->textureHandle;
86 }
87 if (environmentComponent->radiance) {
88 RenderTexture* tex = renderResources.getRenderTexture(environmentComponent->radiance);
89 if (tex && tex->description.target == ResourceDimensions::TextureCube) radianceTexture = tex->textureHandle;
90 }
91 if (environmentComponent->irradiance) {
92 RenderTexture* tex = renderResources.getRenderTexture(environmentComponent->irradiance);
93 if (tex && tex->description.target == ResourceDimensions::TextureCube) irradianceTexture = tex->textureHandle;
94 }
95 if (environmentComponent->ambientIrradiance) {
96 RenderTexture* tex = renderResources.getRenderTexture(environmentComponent->ambientIrradiance);
97 if (tex && tex->description.target == ResourceDimensions::TextureCube) ambientIrradianceTexture = tex->textureHandle;
98 }
99 if (environmentComponent->brdfLUT) {
100 RenderTexture* tex = renderResources.getRenderTexture(environmentComponent->brdfLUT);
101 if (tex && tex->description.target == ResourceDimensions::Texture2D) brdfLUTTexture = tex->textureHandle;
102 }
103 }
104 }
105
106 if (!HandleIsValid(skyTexture)) {
107 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->whiteCube)) skyTexture = tex->textureHandle;
108 }
109 if (!HandleIsValid(radianceTexture)) {
110 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->whiteCube)) radianceTexture = tex->textureHandle;
111 }
112 if (!HandleIsValid(irradianceTexture)) {
113 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->whiteCube)) irradianceTexture = tex->textureHandle;
114 }
115 if (!HandleIsValid(ambientIrradianceTexture)) {
116 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->whiteCube)) ambientIrradianceTexture = tex->textureHandle;
117 }
118 if (!HandleIsValid(brdfLUTTexture)) {
119 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->white)) brdfLUTTexture = tex->textureHandle;
120 }
121
123 if (RenderTexture* tex = renderResources.getRenderTexture(taskContext->context->lightSystem->cascadeArray)) shadowArrayTexture = tex->textureHandle;
124 if (!HandleIsValid(shadowArrayTexture)) {
125 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->whiteDepthArray)) shadowArrayTexture = tex->textureHandle;
126 }
127
128 Cogs::TextureHandle shadowCubeArrayTexture = Cogs::TextureHandle::NoHandle;
129 if (RenderTexture* tex = renderResources.getRenderTexture(taskContext->context->lightSystem->cubeArray)) shadowCubeArrayTexture = tex->textureHandle;
130 if (!HandleIsValid(shadowCubeArrayTexture)) {
131 if (RenderTexture* tex = renderResources.getRenderTexture(textureManager->whiteDepthCube)) shadowCubeArrayTexture = tex->textureHandle;
132 }
133 bindGroups->setTexture(handle, engineBindGroups.skyBinding, skyTexture);
134 bindGroups->setTexture(handle, engineBindGroups.radianceBinding, radianceTexture);
135 bindGroups->setTexture(handle, engineBindGroups.irradianceBinding, irradianceTexture);
136 bindGroups->setTexture(handle, engineBindGroups.ambientIrradianceBinding, ambientIrradianceTexture);
137 bindGroups->setTexture(handle, engineBindGroups.brdfLUTBinding, brdfLUTTexture);
138 bindGroups->setTexture(handle, engineBindGroups.shadowArrayBinding, shadowArrayTexture);
139 bindGroups->setTexture(handle, engineBindGroups.shadowCubeArrayBinding, shadowCubeArrayTexture);
140
141 bindGroups->setSamplerState(handle, engineBindGroups.linearSamplerBinding, taskContext->states->defaultSampler);
142 bindGroups->setSamplerState(handle, engineBindGroups.linearClampSampler, taskContext->states->commonSamplerStates[1]);
143 bindGroups->setSamplerState(handle, engineBindGroups.pointSampler, taskContext->states->commonSamplerStates[2]);
144 bindGroups->setSamplerState(handle, engineBindGroups.pointClampSampler, taskContext->states->commonSamplerStates[3]);
145 bindGroups->setSamplerState(handle, engineBindGroups.shadowSamplerBinding, taskContext->states->shadowSampler);
146
147 deviceContext->setBindGroup(engineBindGroups.lightTextures, static_cast<uint32_t>(BindGroup::LightTextures));
148
149}
150
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78