Cogs.Core
RenderStateUpdater.cpp
1#include "Foundation/Logging/Logger.h"
2
3#include "Rendering/ICapabilities.h"
4
5#include "Context.h"
6
7#include "Systems/Core/LightSystem.h"
8#include "Systems/Core/EnvironmentSystem.h"
9
10#include "Resources/Mesh.h"
11#include "Resources/Skeleton.h"
12#include "Resources/Animation.h"
13#include "Resources/BasicBlueNoiseManager.h"
14#include "RenderStateUpdater.h"
15#include "RenderList.h"
16
17#include "Tasks/RenderTask.h"
18#include "Tasks/GenerateListTask.h"
19
20#include "Resources/TextureManager.h"
21
22namespace
23{
24 Cogs::Logging::Log logger = Cogs::Logging::getLogger("RenderStateUpdater");
25}
26
27
28void Cogs::Core::applyMaterialProperties(const DrawContext * drawContext,
29 const Material * /*material*/,
30 const ConstantBufferBindingHandle & bufferBinding,
31 const BufferHandle & constantBuffer)
32{
33 auto deviceContext = drawContext->deviceContext;
34 if (HandleIsValid(bufferBinding)) {
35 deviceContext->setConstantBuffer(bufferBinding, constantBuffer);
36 }
37}
38
39void Cogs::Core::updateSceneBindings(const DrawContext * drawContext, const CameraData * /*cameraData*/, const EffectBinding * bindings)
40{
41 assert(drawContext->taskContext && "Invalid rendering context.");
42
43 updateSceneBindings(drawContext->taskContext, bindings);
44}
45
46void Cogs::Core::updateSceneBindings(const RenderTaskContext * taskContext, const EffectBinding * bindings)
47{
48 const EngineBuffers* engineBuffers = &taskContext->renderer->getEngineBuffers();
49 IGraphicsDevice* device = taskContext->renderer->getDevice();
50 IContext* deviceContext = device->getImmediateContext();
51
52 if (HandleIsValid(bindings->sceneBufferBinding)) {
53 deviceContext->setConstantBuffer(bindings->sceneBufferBinding, engineBuffers->sceneBufferHandle);
54 }
55 if (HandleIsValid(bindings->viewBufferBinding)) {
56 deviceContext->setConstantBuffer(bindings->viewBufferBinding, engineBuffers->viewBufferHandle);
57 }
58
59 if (HandleIsValid(bindings->blueNoise)) {
60 auto& blueNoiseManager = taskContext->context->blueNoiseManager;
61 blueNoiseManager->enable();
62 TextureHandle blueNoise = blueNoiseManager->getBlueNoiseHandle(false)->texture;
63 RenderTexture * blueNoiseTexture = taskContext->renderer->getRenderResources().getRenderTexture(blueNoise);
64 if (blueNoiseTexture) {
65 deviceContext->setTexture(bindings->blueNoise, blueNoiseTexture->textureHandle);
66 }
67 }
68 if (HandleIsValid(bindings->blueNoiseStable)) {
69 auto& blueNoiseManager = taskContext->context->blueNoiseManager;
70 blueNoiseManager->enable();
71 TextureHandle blueNoise = blueNoiseManager->getBlueNoiseHandle(true)->texture;
72 RenderTexture * blueNoiseTexture = taskContext->renderer->getRenderResources().getRenderTexture(blueNoise);
73 if (blueNoiseTexture) {
74 deviceContext->setTexture(bindings->blueNoiseStable, blueNoiseTexture->textureHandle);
75 }
76 }
77}
78
79void Cogs::Core::updateEnvironmentBindings(const DrawContext * drawContext, const EffectBinding * bindings)
80{
81 auto deviceContext = drawContext->deviceContext;
82 auto lightSystem = drawContext->context->lightSystem;
83
84 if (HandleIsValid(bindings->lightBufferBinding)) {
85 deviceContext->setConstantBuffer(bindings->lightBufferBinding,
86 drawContext->renderer->getEngineBuffers().lightBufferHandle);
87 }
88
89 if (!drawContext->permutation->isShadowPass() && HandleIsValid(bindings->shadowBufferBinding)) {
90 if (HandleIsValid(bindings->shadowBufferBinding)) {
91 deviceContext->setConstantBuffer(bindings->shadowBufferBinding, drawContext->engineBuffers->shadowBufferHandle);
92
93 if (HandleIsValid(bindings->shadowArrayBinding)) {
94 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cascadeArray);
95
96 if (shadowTexture) {
97 deviceContext->setTexture(bindings->shadowArrayBinding, shadowTexture->textureHandle);
98 deviceContext->setSamplerState("", 1, drawContext->taskContext->states->shadowSampler);
99 }
100 }
101 if (HandleIsValid(bindings->shadowArrayBinding_1)) {
102 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cascadeArray);
103
104 if (shadowTexture) {
105 deviceContext->setTexture(bindings->shadowArrayBinding_1, shadowTexture->textureHandle);
106 deviceContext->setSamplerState("", 1, drawContext->taskContext->states->shadowSampler);
107 }
108 }
109
110 if (HandleIsValid(bindings->shadowCubeArrayBinding)) {
111 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cubeArray);
112
113 if (shadowTexture) {
114 deviceContext->setTexture(bindings->shadowCubeArrayBinding, shadowTexture->textureHandle);
115 deviceContext->setSamplerState("", 2, drawContext->taskContext->states->shadowSampler);
116 }
117 }
118 if (HandleIsValid(bindings->shadowCubeArrayBinding_1)) {
119 auto shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cubeArray);
120
121 if (shadowTexture) {
122 deviceContext->setTexture(bindings->shadowCubeArrayBinding_1, shadowTexture->textureHandle);
123 deviceContext->setSamplerState("", 2, drawContext->taskContext->states->shadowSampler);
124 }
125 }
126 }
127
128 if (HandleIsValid(bindings->shadowSamplerBinding)) {
129 deviceContext->setSamplerState(bindings->shadowSamplerBinding, drawContext->taskContext->states->shadowSampler);
130 }
131 if (HandleIsValid(bindings->shadowArraySamplerBinding)) {
132 deviceContext->setSamplerState(bindings->shadowArraySamplerBinding, drawContext->taskContext->states->shadowSampler);
133 }
134 if (HandleIsValid(bindings->shadowCubeArraySamplerBinding)) {
135 deviceContext->setSamplerState(bindings->shadowCubeArraySamplerBinding, drawContext->taskContext->states->shadowSampler);
136 }
137 }
138 else {
139 if (HandleIsValid(bindings->shadowSamplerBinding)) {
140 deviceContext->setSamplerState(bindings->shadowSamplerBinding, drawContext->taskContext->states->shadowSampler);
141 }
142 }
143
144 auto environmentComponent = drawContext->cameraData->environment.resolveComponent<EnvironmentComponent>();
145
146 if (environmentComponent) {
147 if (environmentComponent->isSubmerged(drawContext->cameraData)) {
148
149 if (environmentComponent->subseaRadiance) {
150 RenderTexture * subseaRadianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->subseaRadiance);
151 if (subseaRadianceTexture) {
152 if (subseaRadianceTexture->description.target != ResourceDimensions::TextureCube) {
153 static int count = 0;
154 if (count < 10) {
155 LOG_ERROR(logger, "environment.subseaRadiance is not a cube texture.");
156 count++;
157 }
158 }
159 else {
160 if (HandleIsValid(bindings->skyBinding)) {
161 deviceContext->setTexture(bindings->skyBinding, subseaRadianceTexture->textureHandle);
162 }
163 if (HandleIsValid(bindings->radianceBinding)) {
164 deviceContext->setTexture(bindings->radianceBinding, subseaRadianceTexture->textureHandle);
165 }
166 if (HandleIsValid(bindings->irradianceBinding)) {
167 deviceContext->setTexture(bindings->irradianceBinding, subseaRadianceTexture->textureHandle);
168 }
169 if (HandleIsValid(bindings->ambientIrradianceBinding)) {
170 deviceContext->setTexture(bindings->ambientIrradianceBinding, subseaRadianceTexture->textureHandle);
171 }
172 }
173 }
174 }
175 } else {
176 if (environmentComponent->skyDome && HandleIsValid(bindings->skyBinding)) {
177
178 if (RenderTexture * skyTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->skyDome); skyTexture) {
179
180 if (skyTexture->description.target != ResourceDimensions::TextureCube) {
181 static int warningCount = 0;
182 if (warningCount < 10) {
183 LOG_ERROR(logger, "environment.skyDome is not a cube texture.");
184 warningCount++;
185 }
186 }
187 else {
188 deviceContext->setTexture(bindings->skyBinding, skyTexture->textureHandle);
189 }
190 }
191
192 }
193
194 if (environmentComponent->radiance && HandleIsValid(bindings->radianceBinding)) {
195
196 if (RenderTexture * radianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->radiance); radianceTexture) {
197
198 if (radianceTexture->description.target != ResourceDimensions::TextureCube) {
199 static int warningCount = 0;
200 if (warningCount < 10) {
201 LOG_ERROR(logger, "environment.radiance is not a cube texture.");
202 warningCount++;
203 }
204 }
205 else {
206 deviceContext->setTexture(bindings->radianceBinding, radianceTexture->textureHandle);
207 }
208 }
209
210 }
211
212 if (environmentComponent->irradiance && HandleIsValid(bindings->irradianceBinding)) {
213
214 if (RenderTexture * irradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->irradiance); irradianceTexture) {
215
216 if (irradianceTexture->description.target != ResourceDimensions::TextureCube) {
217 static int warningCount = 0;
218 if (warningCount < 10) {
219 LOG_ERROR(logger, "environment.irradiance is not a cube texture.");
220 warningCount++;
221 }
222 }
223 else {
224 deviceContext->setTexture(bindings->irradianceBinding, irradianceTexture->textureHandle);
225 }
226 }
227 }
228
229 if (environmentComponent->ambientIrradiance && HandleIsValid(bindings->ambientIrradianceBinding)) {
230 RenderTexture * ambientIrradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->ambientIrradiance);
231 if (ambientIrradianceTexture) {
232
233 if (ambientIrradianceTexture->description.target != ResourceDimensions::TextureCube) {
234 static int warningCount = 0;
235 if (warningCount < 10) {
236 LOG_ERROR(logger, "environment.ambientIrradiance is not a cube texture.");
237 warningCount++;
238 }
239 }
240 else {
241 deviceContext->setTexture(bindings->ambientIrradianceBinding, ambientIrradianceTexture->textureHandle);
242 }
243 }
244 }
245 if (environmentComponent->brdfLUT && HandleIsValid(bindings->brdfLUTBinding)) {
246 RenderTexture* brdfLUTTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->brdfLUT);
247 if (brdfLUTTexture) {
248
249 if (brdfLUTTexture->description.target != ResourceDimensions::Texture2D) {
250 static int warningCount = 0;
251 if (warningCount < 10) {
252 LOG_ERROR(logger, "environment.brdfLUT is not a 2D texture.");
253 warningCount++;
254 }
255 }
256 else {
257 deviceContext->setTexture(bindings->brdfLUTBinding, brdfLUTTexture->textureHandle);
258 }
259 }
260 } else if (HandleIsValid(bindings->brdfLUTBinding)) {
261 RenderTexture * white = drawContext->renderer->getRenderResources().getRenderTexture(drawContext->context->textureManager->white);
262 deviceContext->setTexture(bindings->brdfLUTBinding, white->textureHandle);
263 }
264
265 }
266 }
267
268 SamplerState linearFiltering = {
269 SamplerState::AddressMode::Clamp,
270 SamplerState::AddressMode::Clamp,
271 SamplerState::AddressMode::Clamp,
272 SamplerState::FilterMode::MinMagMipLinear,
273 SamplerState::ComparisonFunction::Never,
274 1,
275 { 0, 0, 0, 1 }
276 };
277
278 if (HandleIsValid(bindings->skySamplerBinding)) {
279 deviceContext->setSamplerState(bindings->skySamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
280 }
281
282
283 if (HandleIsValid(bindings->radianceSamplerBinding)) {
284 deviceContext->setSamplerState(bindings->radianceSamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
285 }
286 if (HandleIsValid(bindings->irradianceSamplerBinding)) {
287 deviceContext->setSamplerState(bindings->irradianceSamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
288 }
289 if (HandleIsValid(bindings->ambientIrradianceSamplerBinding)) {
290 deviceContext->setSamplerState(bindings->ambientIrradianceSamplerBinding, drawContext->taskContext->states->getSamplerState(SamplerState::DefaultState()));
291 }
292 if (HandleIsValid(bindings->brdfLUTSamplerBinding)) {
293 SamplerState lutSamplerState = {
294 SamplerState::AddressMode::Clamp,
295 SamplerState::AddressMode::Clamp,
296 SamplerState::AddressMode::Clamp,
297 SamplerState::FilterMode::MinMagMipLinear,
298 SamplerState::ComparisonFunction::Never,
299 1,
300 { 0, 0, 0, 1 }
301 };
302 deviceContext->setSamplerState(bindings->brdfLUTSamplerBinding, drawContext->taskContext->states->getSamplerState(lutSamplerState));
303 }
304}
305
306void Cogs::Core::updateMaterialBindings(const DrawContext * drawContext, const RenderMaterialInstance * renderMaterialInstance, const EffectBinding * bindings, bool perInstance)
307{
308 auto deviceContext = drawContext->deviceContext;
309
310 auto materialInstance = renderMaterialInstance->getResource();
311 auto material = materialInstance->material;
312
313 for (auto & buffer : material->constantBuffers.buffers) {
314 if (buffer.isPerInstance != perInstance) continue;
315
316 auto & bufferBinding = bindings->bufferBindings[buffer.index];
317
318 if (perInstance) {
319 applyMaterialProperties(drawContext, material, bufferBinding, renderMaterialInstance->buffers[buffer.index].handle);
320 }
321 else {
322 applyMaterialProperties(drawContext, material, bufferBinding, bindings->buffers[buffer.index].handle);
323 }
324 }
325
326 if (perInstance) {
327 for (const TextureValue& textureProperty : materialInstance->textureVariables) {
328 if (textureProperty.property->isPerInstance) {
329 const Cogs::TextureBindingHandle& textureBinding = bindings->textureBindings[textureProperty.key];
330 const Cogs::SamplerStateBindingHandle& samplerBinding = bindings->samplerBindings[textureProperty.key];
331
332 RenderTexture* renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.texture.handle);
333
334 if (renderTexture) {
335 deviceContext->setTexture(textureBinding, renderTexture->textureHandle);
336 }
337 else {
338 if (HandleIsValid(textureProperty.property->texture.handle)) {
339 // Render texture is not ready, fall back to default value if available.
340 renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.property->texture.handle);
341 deviceContext->setTexture(textureBinding, renderTexture ? renderTexture->textureHandle : Cogs::TextureHandle::NoHandle);
342 }
343 else {
344 deviceContext->setTexture(textureBinding, Cogs::TextureHandle::NoHandle);
345 }
346 }
347 if (HandleIsValid(renderMaterialInstance->samplerStates[textureProperty.key])) {
348 deviceContext->setSamplerState(samplerBinding, renderMaterialInstance->samplerStates[textureProperty.key]);
349 }
350 else {
351 deviceContext->setSamplerState(samplerBinding, Cogs::SamplerStateHandle::NoHandle);
352 }
353 }
354 }
355 }
356 else {
357 RenderMaterial* renderMaterial = renderMaterialInstance->renderMaterial;
358
359 for (size_t i = 0, N = material->textureProperties.size(); i < N; i++) {
360 const TextureProperty& textureProperty = material->textureProperties[i];
361 if (!textureProperty.isPerInstance) {
362
363 RenderTexture* renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.texture.handle);
364 if (renderTexture) {
365 deviceContext->setTexture(bindings->textureBindings[textureProperty.key], renderTexture->textureHandle);
366 }
367 else {
368 deviceContext->setTexture(bindings->textureBindings[textureProperty.key], Cogs::TextureHandle::NoHandle);
369 }
370
371 const RenderMaterial::TexturePropertyState& state = renderMaterial->texturePropertyStates[i];
372 if (HandleIsValid(state.samplerState)) {
373 deviceContext->setSamplerState(bindings->samplerBindings[textureProperty.key], state.samplerState);
374 }
375 else {
376 deviceContext->setSamplerState(bindings->samplerBindings[textureProperty.key], Cogs::SamplerStateHandle::NoHandle);
377 }
378
379 }
380 }
381 }
382}
383
384void Cogs::Core::applyMaterialPermutation(RenderTaskContext * taskContext, const DrawContext * drawContext, const EffectBinding * bindings, const RenderMaterialInstance * renderMaterialInstance)
385{
386 drawContext->deviceContext->setEffect(bindings->renderEffect->effectHandle);
387 drawContext->deviceContext->setInputLayout(bindings->renderEffect->inputHandle);
388 updateSceneBindings(taskContext, bindings);
389 updateMaterialBindings(drawContext, renderMaterialInstance, bindings, false);
390}
391
392void Cogs::Core::applyMaterialInstance(const DrawContext * drawContext, const EffectBinding * bindings, const RenderMaterialInstance * renderMaterialInstance)
393{
394 updateMaterialBindings(drawContext, renderMaterialInstance, bindings, true);
395}
396
397size_t Cogs::Core::populateObjectBuffer(const DrawContext * drawContext, const RenderItem* items, size_t o, size_t n)
398{
399 auto deviceContext = drawContext->deviceContext;
400 auto & engineBuffers = *drawContext->engineBuffers;
401 assert(engineBuffers.objectBatch.count != 0);
402
403 const auto stride = engineBuffers.objectBatch.stride;
404 auto m = std::min(n - o, size_t(engineBuffers.objectBatch.count));
405 if (m) {
406 MappedBuffer<char> objectBatch(deviceContext, engineBuffers.objectBatch.bufferHandle, MapMode::WriteDiscard);
407
408 if (objectBatch) {
409 for (size_t i = 0; i < m; i++) {
410 if (items[o + i].isCustom()) continue;
411 auto * obj = (ObjectBuffer*)(objectBatch.get() + stride * i);
412 obj->worldMatrix = *items[o + i].worldMatrix;
413 obj->objectId = items[o + i].objectId;
414 }
415 }
416 }
417 return o + m;
418}
419
420void Cogs::Core::applyMaterialPerObjectBatched(const DrawContext * drawContext, const RenderMaterialInstance * /*renderMaterialInstance*/, const RenderItem & renderItem, size_t o)
421{
422 auto deviceContext = drawContext->deviceContext;
423 auto & engineBuffers = *drawContext->engineBuffers;
424 auto & batch = engineBuffers.objectBatch;
425
426 const auto bindings = renderItem.binding;
427
428 if (HandleIsValid(bindings->objectBufferBinding)) {
429 deviceContext->setConstantBuffer(bindings->objectBufferBinding,
430 batch.bufferHandle,
431 batch.stride * uint32_t(o),
432 batch.stride);
433 }
434
435 if (renderItem.poseData) {
436 if (HandleIsValid(bindings->animationBufferBinding)) {
437 deviceContext->updateBuffer(engineBuffers.animationBuffer, renderItem.poseData->transforms, sizeof(glm::mat4) * renderItem.poseData->numPoses);
438 deviceContext->setConstantBuffer(bindings->animationBufferBinding, engineBuffers.animationBuffer);
439 }
440 }
441}
442
443
444void Cogs::Core::applyMaterialPerObject(const DrawContext * drawContext, const RenderMaterialInstance * /*renderMaterialInstance*/, const RenderItem & renderItem)
445{
446 auto deviceContext = drawContext->deviceContext;
447 auto & engineBuffers = *drawContext->engineBuffers;
448
449 const auto bindings = renderItem.binding;
450
451 if (HandleIsValid(bindings->objectBufferBinding)) {
452 ListObjectBuffer* unifiedObjectBuffer = drawContext->listObjectBuffer;
453
454 if (unifiedObjectBuffer && renderItem.hasObjectBufferSlot() && HandleIsValid(unifiedObjectBuffer->gpu)) {
455 deviceContext->setConstantBuffer(bindings->objectBufferBinding,
456 unifiedObjectBuffer->gpu,
457 unifiedObjectBuffer->stride * renderItem.objectBufferSlot,
458 unifiedObjectBuffer->stride);
459 }
460 else {
461 MappedBuffer<ObjectBuffer> objectBuffer(deviceContext, engineBuffers.objectBufferHandle, MapMode::WriteDiscard);
462 if (objectBuffer) {
463 objectBuffer->worldMatrix = *renderItem.worldMatrix;
464 objectBuffer->objectId = renderItem.objectId;
465 }
466 deviceContext->setConstantBuffer(bindings->objectBufferBinding, engineBuffers.objectBufferHandle);
467 }
468 }
469
470 if (renderItem.poseData) {
471 if (HandleIsValid(bindings->animationBufferBinding)) {
472 deviceContext->updateBuffer(engineBuffers.animationBuffer, renderItem.poseData->transforms, sizeof(glm::mat4) * renderItem.poseData->numPoses);
473 deviceContext->setConstantBuffer(bindings->animationBufferBinding, engineBuffers.animationBuffer);
474 }
475 }
476}
Log implementation class.
Definition: LogManager.h:140
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:181
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78