1#include "Foundation/Logging/Logger.h"
3#include "Rendering/ICapabilities.h"
7#include "Systems/Core/LightSystem.h"
8#include "Systems/Core/EnvironmentSystem.h"
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"
17#include "Tasks/RenderTask.h"
18#include "Tasks/GenerateListTask.h"
20#include "Resources/TextureManager.h"
28void Cogs::Core::applyMaterialProperties(
const DrawContext * drawContext,
30 const ConstantBufferBindingHandle & bufferBinding,
31 const BufferHandle & constantBuffer)
33 auto deviceContext = drawContext->deviceContext;
35 deviceContext->setConstantBuffer(bufferBinding, constantBuffer);
39void Cogs::Core::updateSceneBindings(
const DrawContext * drawContext,
const CameraData * ,
const EffectBinding * bindings)
41 assert(drawContext->taskContext &&
"Invalid rendering context.");
43 updateSceneBindings(drawContext->taskContext, bindings);
46void Cogs::Core::updateSceneBindings(
const RenderTaskContext * taskContext,
const EffectBinding * bindings)
48 const EngineBuffers* engineBuffers = &taskContext->renderer->getEngineBuffers();
49 IGraphicsDevice* device = taskContext->renderer->getDevice();
50 IContext* deviceContext = device->getImmediateContext();
53 deviceContext->setConstantBuffer(bindings->sceneBufferBinding, engineBuffers->sceneBufferHandle);
56 deviceContext->setConstantBuffer(bindings->viewBufferBinding, engineBuffers->viewBufferHandle);
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);
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);
79void Cogs::Core::updateEnvironmentBindings(
const DrawContext * drawContext,
const EffectBinding * bindings)
81 IContext *deviceContext = drawContext->deviceContext;
82 LightSystem *lightSystem = drawContext->context->lightSystem;
84 bool needSkyBinding =
true;
85 bool needRadianceBinding =
true;
86 bool needIrradianceBinding =
true;
87 bool needAmbientIrradianceBinding =
true;
89 if (!drawContext->permutation->isShadowPass()) {
91 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cascadeArray);
94 deviceContext->setTexture(bindings->shadowArrayBinding, shadowTexture->textureHandle);
95 deviceContext->setSamplerState(
"", 1, drawContext->taskContext->states->shadowSampler);
99 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cascadeArray);
102 deviceContext->setTexture(bindings->shadowArrayBinding_1, shadowTexture->textureHandle);
103 deviceContext->setSamplerState(
"", 1, drawContext->taskContext->states->shadowSampler);
108 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cubeArray);
111 deviceContext->setTexture(bindings->shadowCubeArrayBinding, shadowTexture->textureHandle);
112 deviceContext->setSamplerState(
"", 2, drawContext->taskContext->states->shadowSampler);
116 auto shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cubeArray);
119 deviceContext->setTexture(bindings->shadowCubeArrayBinding_1, shadowTexture->textureHandle);
120 deviceContext->setSamplerState(
"", 2, drawContext->taskContext->states->shadowSampler);
125 deviceContext->setSamplerState(bindings->shadowSamplerBinding, drawContext->taskContext->states->shadowSampler);
128 deviceContext->setSamplerState(bindings->shadowArraySamplerBinding, drawContext->taskContext->states->shadowSampler);
130 if (
HandleIsValid(bindings->shadowCubeArraySamplerBinding)) {
131 deviceContext->setSamplerState(bindings->shadowCubeArraySamplerBinding, drawContext->taskContext->states->shadowSampler);
136 deviceContext->setSamplerState(bindings->shadowSamplerBinding, drawContext->taskContext->states->shadowSampler);
140 auto environmentComponent = drawContext->cameraData->environment.resolveComponent<EnvironmentComponent>();
142 if (environmentComponent) {
143 if (environmentComponent->isSubmerged(drawContext->cameraData)) {
145 if (environmentComponent->subseaRadiance) {
146 RenderTexture * subseaRadianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->subseaRadiance);
147 if (subseaRadianceTexture) {
148 if (subseaRadianceTexture->description.target != ResourceDimensions::TextureCube) {
149 static int count = 0;
151 LOG_ERROR(logger,
"environment.subseaRadiance is not a cube texture.");
157 deviceContext->setTexture(bindings->skyBinding, subseaRadianceTexture->textureHandle);
158 needSkyBinding =
false;
161 deviceContext->setTexture(bindings->radianceBinding, subseaRadianceTexture->textureHandle);
162 needRadianceBinding =
false;
165 deviceContext->setTexture(bindings->irradianceBinding, subseaRadianceTexture->textureHandle);
166 needIrradianceBinding =
false;
169 deviceContext->setTexture(bindings->ambientIrradianceBinding, subseaRadianceTexture->textureHandle);
170 needAmbientIrradianceBinding =
false;
176 if (environmentComponent->skyDome &&
HandleIsValid(bindings->skyBinding)) {
178 if (RenderTexture * skyTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->skyDome); skyTexture) {
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.");
188 deviceContext->setTexture(bindings->skyBinding, skyTexture->textureHandle);
189 needSkyBinding =
false;
194 if (environmentComponent->radiance &&
HandleIsValid(bindings->radianceBinding)) {
196 if (RenderTexture * radianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->radiance); radianceTexture) {
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.");
206 deviceContext->setTexture(bindings->radianceBinding, radianceTexture->textureHandle);
207 needRadianceBinding =
false;
212 if (environmentComponent->irradiance &&
HandleIsValid(bindings->irradianceBinding)) {
214 if (RenderTexture * irradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->irradiance); irradianceTexture) {
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.");
224 deviceContext->setTexture(bindings->irradianceBinding, irradianceTexture->textureHandle);
225 needIrradianceBinding =
false;
230 if (environmentComponent->ambientIrradiance &&
HandleIsValid(bindings->ambientIrradianceBinding)) {
231 RenderTexture * ambientIrradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->ambientIrradiance);
232 if (ambientIrradianceTexture) {
234 if (ambientIrradianceTexture->description.target != ResourceDimensions::TextureCube) {
235 static int warningCount = 0;
236 if (warningCount < 10) {
237 LOG_ERROR(logger,
"environment.ambientIrradiance is not a cube texture.");
242 deviceContext->setTexture(bindings->ambientIrradianceBinding, ambientIrradianceTexture->textureHandle);
243 needAmbientIrradianceBinding =
false;
248 if (environmentComponent->brdfLUT &&
HandleIsValid(bindings->brdfLUTBinding)) {
249 RenderTexture* brdfLUTTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->brdfLUT);
250 if (brdfLUTTexture) {
252 if (brdfLUTTexture->description.target != ResourceDimensions::Texture2D) {
253 static int warningCount = 0;
254 if (warningCount < 10) {
255 LOG_ERROR(logger,
"environment.brdfLUT is not a 2D texture.");
260 deviceContext->setTexture(bindings->brdfLUTBinding, brdfLUTTexture->textureHandle);
264 RenderTexture * white = drawContext->renderer->getRenderResources().getRenderTexture(drawContext->context->textureManager->white);
265 deviceContext->setTexture(bindings->brdfLUTBinding, white->textureHandle);
271 if (drawContext->device->getType() == GraphicsDeviceType::WebGPU){
276 TextureHandle whiteCube = drawContext->context->textureManager->whiteCube;
277 RenderTexture* skyTexture = drawContext->renderer->getRenderResources().getRenderTexture(whiteCube);
279 LOG_INFO_ONCE(logger,
"Setting dummy environment.skyDome.");
280 deviceContext->setTexture(bindings->skyBinding, skyTexture->textureHandle);
285 if(
HandleIsValid(bindings->radianceBinding) && needRadianceBinding) {
286 TextureHandle whiteCube = drawContext->context->textureManager->whiteCube;
287 RenderTexture* radianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(whiteCube);
288 if (radianceTexture) {
289 LOG_INFO_ONCE(logger,
"Setting dummy environment.radiance.");
290 deviceContext->setTexture(bindings->radianceBinding, radianceTexture->textureHandle);
295 if(
HandleIsValid(bindings->irradianceBinding) && needIrradianceBinding) {
296 TextureHandle whiteCube = drawContext->context->textureManager->whiteCube;
297 RenderTexture* irradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(whiteCube);
298 if (irradianceTexture) {
299 LOG_INFO_ONCE(logger,
"Setting dummy environment.irradiance.");
300 deviceContext->setTexture(bindings->irradianceBinding, irradianceTexture->textureHandle);
305 if(
HandleIsValid(bindings->ambientIrradianceBinding) && needAmbientIrradianceBinding) {
306 TextureHandle whiteCube = drawContext->context->textureManager->whiteCube;
307 RenderTexture* ambientIrradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(whiteCube);
308 if (ambientIrradianceTexture) {
309 LOG_INFO_ONCE(logger,
"Setting dummy environment.ambientIrradiance.");
310 deviceContext->setTexture(bindings->ambientIrradianceBinding, ambientIrradianceTexture->textureHandle);
315 SamplerState linearFiltering = {
316 SamplerState::AddressMode::Clamp,
317 SamplerState::AddressMode::Clamp,
318 SamplerState::AddressMode::Clamp,
319 SamplerState::FilterMode::MinMagMipLinear,
320 SamplerState::ComparisonFunction::Never,
326 deviceContext->setSamplerState(bindings->skySamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
331 deviceContext->setSamplerState(bindings->radianceSamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
334 deviceContext->setSamplerState(bindings->irradianceSamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
336 if (
HandleIsValid(bindings->ambientIrradianceSamplerBinding)) {
337 deviceContext->setSamplerState(bindings->ambientIrradianceSamplerBinding, drawContext->taskContext->states->getSamplerState(SamplerState::DefaultState()));
340 SamplerState lutSamplerState = {
341 SamplerState::AddressMode::Clamp,
342 SamplerState::AddressMode::Clamp,
343 SamplerState::AddressMode::Clamp,
344 SamplerState::FilterMode::MinMagMipLinear,
345 SamplerState::ComparisonFunction::Never,
349 deviceContext->setSamplerState(bindings->brdfLUTSamplerBinding, drawContext->taskContext->states->getSamplerState(lutSamplerState));
353void Cogs::Core::updateMaterialBindings(
const DrawContext * drawContext,
const RenderMaterialInstance * renderMaterialInstance,
const EffectBinding * bindings,
bool perInstance)
355 auto deviceContext = drawContext->deviceContext;
357 auto materialInstance = renderMaterialInstance->getResource();
358 auto material = materialInstance->material;
360 for (
auto & buffer : material->constantBuffers.buffers) {
361 if (buffer.isPerInstance != perInstance)
continue;
363 auto & bufferBinding = bindings->bufferBindings[buffer.index];
366 applyMaterialProperties(drawContext, material, bufferBinding, renderMaterialInstance->buffers[buffer.index].handle);
369 applyMaterialProperties(drawContext, material, bufferBinding, bindings->buffers[buffer.index].handle);
374 for (
const TextureValue& textureProperty : materialInstance->textureVariables) {
375 if (textureProperty.property->isPerInstance) {
379 RenderTexture* renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.texture.handle);
382 deviceContext->setTexture(textureBinding, renderTexture->textureHandle);
385 if (
HandleIsValid(textureProperty.property->texture.handle)) {
387 renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.property->texture.handle);
394 if (
HandleIsValid(renderMaterialInstance->samplerStates[textureProperty.key])) {
395 deviceContext->setSamplerState(samplerBinding, renderMaterialInstance->samplerStates[textureProperty.key]);
404 RenderMaterial* renderMaterial = renderMaterialInstance->renderMaterial;
406 for (
size_t i = 0, N = material->textureProperties.size(); i < N; i++) {
407 const TextureProperty& textureProperty = material->textureProperties[i];
408 if (!textureProperty.isPerInstance) {
410 RenderTexture* renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.texture.handle);
412 deviceContext->setTexture(bindings->textureBindings[textureProperty.key], renderTexture->textureHandle);
418 const RenderMaterial::TexturePropertyState& state = renderMaterial->texturePropertyStates[i];
420 deviceContext->setSamplerState(bindings->samplerBindings[textureProperty.key], state.samplerState);
431void Cogs::Core::applyMaterialPermutation(RenderTaskContext * taskContext,
const DrawContext * drawContext,
const EffectBinding * bindings,
const RenderMaterialInstance * renderMaterialInstance)
433 drawContext->deviceContext->setEffect(bindings->renderEffect->effectHandle);
434 drawContext->deviceContext->setInputLayout(bindings->renderEffect->inputHandle);
435 updateSceneBindings(taskContext, bindings);
436 updateMaterialBindings(drawContext, renderMaterialInstance, bindings,
false);
439void Cogs::Core::applyMaterialInstance(
const DrawContext * drawContext,
const EffectBinding * bindings,
const RenderMaterialInstance * renderMaterialInstance)
441 updateMaterialBindings(drawContext, renderMaterialInstance, bindings,
true);
444size_t Cogs::Core::populateObjectBuffer(
const DrawContext * drawContext,
const RenderItem* items,
size_t o,
size_t n)
446 auto deviceContext = drawContext->deviceContext;
447 auto & engineBuffers = *drawContext->engineBuffers;
448 assert(engineBuffers.objectBatch.count != 0);
450 const auto stride = engineBuffers.objectBatch.stride;
451 auto m = std::min(n - o,
size_t(engineBuffers.objectBatch.count));
453 MappedBuffer<char> objectBatch(deviceContext, engineBuffers.objectBatch.bufferHandle, MapMode::WriteDiscard);
456 for (
size_t i = 0; i < m; i++) {
457 if (items[o + i].isCustom())
continue;
458 auto * obj = (ObjectBuffer*)(objectBatch.get() + stride * i);
459 obj->worldMatrix = *items[o + i].worldMatrix;
460 obj->objectId = items[o + i].objectId;
467void Cogs::Core::applyMaterialPerObjectBatched(
const DrawContext * drawContext,
const RenderMaterialInstance * ,
const RenderItem & renderItem,
size_t o)
469 auto deviceContext = drawContext->deviceContext;
470 auto & engineBuffers = *drawContext->engineBuffers;
471 auto & batch = engineBuffers.objectBatch;
473 const auto bindings = renderItem.binding;
476 deviceContext->setConstantBuffer(bindings->objectBufferBinding,
478 batch.stride * uint32_t(o),
482 if (renderItem.poseData) {
484 deviceContext->updateBuffer(engineBuffers.animationBuffer, renderItem.poseData->transforms,
sizeof(glm::mat4) * renderItem.poseData->numPoses);
485 deviceContext->setConstantBuffer(bindings->animationBufferBinding, engineBuffers.animationBuffer);
491void Cogs::Core::applyMaterialPerObject(
const DrawContext * drawContext,
const RenderMaterialInstance * ,
const RenderItem & renderItem)
493 auto deviceContext = drawContext->deviceContext;
494 auto & engineBuffers = *drawContext->engineBuffers;
496 const auto bindings = renderItem.binding;
499 ListObjectBuffer* unifiedObjectBuffer = drawContext->listObjectBuffer;
501 if (unifiedObjectBuffer && renderItem.hasObjectBufferSlot() &&
HandleIsValid(unifiedObjectBuffer->gpu)) {
502 deviceContext->setConstantBuffer(bindings->objectBufferBinding,
503 unifiedObjectBuffer->gpu,
504 unifiedObjectBuffer->stride * renderItem.objectBufferSlot,
505 unifiedObjectBuffer->stride);
508 MappedBuffer<ObjectBuffer> objectBuffer(deviceContext, engineBuffers.objectBufferHandle, MapMode::WriteDiscard);
510 objectBuffer->worldMatrix = *renderItem.worldMatrix;
511 objectBuffer->objectId = renderItem.objectId;
513 deviceContext->setConstantBuffer(bindings->objectBufferBinding, engineBuffers.objectBufferHandle);
517 if (renderItem.poseData) {
519 deviceContext->updateBuffer(engineBuffers.animationBuffer, renderItem.poseData->transforms,
sizeof(glm::mat4) * renderItem.poseData->numPoses);
520 deviceContext->setConstantBuffer(bindings->animationBufferBinding, engineBuffers.animationBuffer);
Log implementation class.
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
static const Handle_t NoHandle
Represents a handle to nothing.