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"
26void Cogs::Core::applyMaterialProperties(
const DrawContext * drawContext,
28 const ConstantBufferBindingHandle & bufferBinding,
29 const BufferHandle & constantBuffer)
31 auto deviceContext = drawContext->deviceContext;
33 deviceContext->setConstantBuffer(bufferBinding, constantBuffer);
37void Cogs::Core::updateSceneBindings(
const DrawContext * drawContext,
const CameraData * ,
const EffectBinding * bindings)
39 assert(drawContext->taskContext &&
"Invalid rendering context.");
41 updateSceneBindings(drawContext->taskContext, bindings);
44void Cogs::Core::updateSceneBindings(
const RenderTaskContext * taskContext,
const EffectBinding * bindings)
46 const EngineBuffers* engineBuffers = &taskContext->renderer->getEngineBuffers();
47 IGraphicsDevice* device = taskContext->renderer->getDevice();
48 IContext* deviceContext = device->getImmediateContext();
51 deviceContext->setConstantBuffer(bindings->sceneBufferBinding, engineBuffers->sceneBufferHandle);
54 deviceContext->setConstantBuffer(bindings->viewBufferBinding, engineBuffers->viewBufferHandle);
58 auto& blueNoiseManager = taskContext->context->blueNoiseManager;
59 blueNoiseManager->enable();
60 TextureHandle blueNoise = blueNoiseManager->getBlueNoiseHandle(
false)->texture;
61 RenderTexture * blueNoiseTexture = taskContext->renderer->getRenderResources().getRenderTexture(blueNoise);
62 if (blueNoiseTexture) {
63 deviceContext->setTexture(bindings->blueNoise, blueNoiseTexture->textureHandle);
67 auto& blueNoiseManager = taskContext->context->blueNoiseManager;
68 blueNoiseManager->enable();
69 TextureHandle blueNoise = blueNoiseManager->getBlueNoiseHandle(
true)->texture;
70 RenderTexture * blueNoiseTexture = taskContext->renderer->getRenderResources().getRenderTexture(blueNoise);
71 if (blueNoiseTexture) {
72 deviceContext->setTexture(bindings->blueNoiseStable, blueNoiseTexture->textureHandle);
77void Cogs::Core::updateEnvironmentBindings(
const DrawContext * drawContext,
const EffectBinding * bindings)
79 auto deviceContext = drawContext->deviceContext;
80 auto lightSystem = drawContext->context->lightSystem;
83 deviceContext->setConstantBuffer(bindings->lightBufferBinding,
84 drawContext->renderer->getEngineBuffers().lightBufferHandle);
87 if (!drawContext->permutation->isShadowPass() &&
HandleIsValid(bindings->shadowBufferBinding)) {
89 deviceContext->setConstantBuffer(bindings->shadowBufferBinding, drawContext->engineBuffers->shadowBufferHandle);
92 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cascadeArray);
95 deviceContext->setTexture(bindings->shadowArrayBinding, shadowTexture->textureHandle);
96 deviceContext->setSamplerState(
"", 1, drawContext->taskContext->states->shadowSampler);
100 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cascadeArray);
103 deviceContext->setTexture(bindings->shadowArrayBinding_1, shadowTexture->textureHandle);
104 deviceContext->setSamplerState(
"", 1, drawContext->taskContext->states->shadowSampler);
109 RenderTexture * shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cubeArray);
112 deviceContext->setTexture(bindings->shadowCubeArrayBinding, shadowTexture->textureHandle);
113 deviceContext->setSamplerState(
"", 2, drawContext->taskContext->states->shadowSampler);
117 auto shadowTexture = drawContext->renderer->getRenderResources().getRenderTexture(lightSystem->cubeArray);
120 deviceContext->setTexture(bindings->shadowCubeArrayBinding_1, shadowTexture->textureHandle);
121 deviceContext->setSamplerState(
"", 2, drawContext->taskContext->states->shadowSampler);
127 deviceContext->setSamplerState(bindings->shadowSamplerBinding, drawContext->taskContext->states->shadowSampler);
130 deviceContext->setSamplerState(bindings->shadowArraySamplerBinding, drawContext->taskContext->states->shadowSampler);
132 if (
HandleIsValid(bindings->shadowCubeArraySamplerBinding)) {
133 deviceContext->setSamplerState(bindings->shadowCubeArraySamplerBinding, drawContext->taskContext->states->shadowSampler);
138 deviceContext->setSamplerState(bindings->shadowSamplerBinding, drawContext->taskContext->states->shadowSampler);
142 auto environmentComponent = drawContext->cameraData->environment.resolveComponent<EnvironmentComponent>();
144 if (environmentComponent) {
145 if (environmentComponent->isSubmerged(drawContext->cameraData)) {
147 if (environmentComponent->subseaRadiance) {
148 RenderTexture * subseaRadianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->subseaRadiance);
149 if (subseaRadianceTexture) {
150 if (subseaRadianceTexture->description.target != ResourceDimensions::TextureCube) {
151 static int count = 0;
153 LOG_ERROR(logger,
"environment.subseaRadiance is not a cube texture.");
159 deviceContext->setTexture(bindings->skyBinding, subseaRadianceTexture->textureHandle);
162 deviceContext->setTexture(bindings->radianceBinding, subseaRadianceTexture->textureHandle);
165 deviceContext->setTexture(bindings->irradianceBinding, subseaRadianceTexture->textureHandle);
168 deviceContext->setTexture(bindings->ambientIrradianceBinding, subseaRadianceTexture->textureHandle);
174 if (environmentComponent->skyDome &&
HandleIsValid(bindings->skyBinding)) {
176 if (RenderTexture * skyTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->skyDome); skyTexture) {
178 if (skyTexture->description.target != ResourceDimensions::TextureCube) {
179 static int warningCount = 0;
180 if (warningCount < 10) {
181 LOG_ERROR(logger,
"environment.skyDome is not a cube texture.");
186 deviceContext->setTexture(bindings->skyBinding, skyTexture->textureHandle);
192 if (environmentComponent->radiance &&
HandleIsValid(bindings->radianceBinding)) {
194 if (RenderTexture * radianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->radiance); radianceTexture) {
196 if (radianceTexture->description.target != ResourceDimensions::TextureCube) {
197 static int warningCount = 0;
198 if (warningCount < 10) {
199 LOG_ERROR(logger,
"environment.radiance is not a cube texture.");
204 deviceContext->setTexture(bindings->radianceBinding, radianceTexture->textureHandle);
210 if (environmentComponent->irradiance &&
HandleIsValid(bindings->irradianceBinding)) {
212 if (RenderTexture * irradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->irradiance); irradianceTexture) {
214 if (irradianceTexture->description.target != ResourceDimensions::TextureCube) {
215 static int warningCount = 0;
216 if (warningCount < 10) {
217 LOG_ERROR(logger,
"environment.irradiance is not a cube texture.");
222 deviceContext->setTexture(bindings->irradianceBinding, irradianceTexture->textureHandle);
228 if (environmentComponent->ambientIrradiance &&
HandleIsValid(bindings->ambientIrradianceBinding)) {
229 RenderTexture * ambientIrradianceTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->ambientIrradiance);
230 if (ambientIrradianceTexture) {
232 if (ambientIrradianceTexture->description.target != ResourceDimensions::TextureCube) {
233 static int warningCount = 0;
234 if (warningCount < 10) {
235 LOG_ERROR(logger,
"environment.ambientIrradiance is not a cube texture.");
240 deviceContext->setTexture(bindings->ambientIrradianceBinding, ambientIrradianceTexture->textureHandle);
244 if (environmentComponent->brdfLUT &&
HandleIsValid(bindings->brdfLUTBinding)) {
245 RenderTexture* brdfLUTTexture = drawContext->renderer->getRenderResources().getRenderTexture(environmentComponent->brdfLUT);
246 if (brdfLUTTexture) {
248 if (brdfLUTTexture->description.target != ResourceDimensions::Texture2D) {
249 static int warningCount = 0;
250 if (warningCount < 10) {
251 LOG_ERROR(logger,
"environment.brdfLUT is not a 2D texture.");
256 deviceContext->setTexture(bindings->brdfLUTBinding, brdfLUTTexture->textureHandle);
263 SamplerState linearFiltering = {
264 SamplerState::AddressMode::Clamp,
265 SamplerState::AddressMode::Clamp,
266 SamplerState::AddressMode::Clamp,
267 SamplerState::FilterMode::MinMagMipLinear,
268 SamplerState::ComparisonFunction::Never,
274 deviceContext->setSamplerState(bindings->skySamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
279 deviceContext->setSamplerState(bindings->radianceSamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
282 deviceContext->setSamplerState(bindings->irradianceSamplerBinding, drawContext->taskContext->states->getSamplerState(linearFiltering));
284 if (
HandleIsValid(bindings->ambientIrradianceSamplerBinding)) {
285 deviceContext->setSamplerState(bindings->ambientIrradianceSamplerBinding, drawContext->taskContext->states->getSamplerState(SamplerState::DefaultState()));
288 SamplerState lutSamplerState = {
289 SamplerState::AddressMode::Clamp,
290 SamplerState::AddressMode::Clamp,
291 SamplerState::AddressMode::Clamp,
292 SamplerState::FilterMode::MinMagMipLinear,
293 SamplerState::ComparisonFunction::Never,
297 deviceContext->setSamplerState(bindings->brdfLUTSamplerBinding, drawContext->taskContext->states->getSamplerState(lutSamplerState));
301void Cogs::Core::updateMaterialBindings(
const DrawContext * drawContext,
const RenderMaterialInstance * renderMaterialInstance,
const EffectBinding * bindings,
bool perInstance)
303 auto deviceContext = drawContext->deviceContext;
305 auto materialInstance = renderMaterialInstance->getResource();
306 auto material = materialInstance->material;
308 for (
auto & buffer : material->constantBuffers.buffers) {
309 if (buffer.isPerInstance != perInstance)
continue;
311 auto & bufferBinding = bindings->bufferBindings[buffer.index];
314 applyMaterialProperties(drawContext, material, bufferBinding, renderMaterialInstance->buffers[buffer.index].handle);
317 applyMaterialProperties(drawContext, material, bufferBinding, bindings->buffers[buffer.index].handle);
322 for (
const TextureValue& textureProperty : materialInstance->textureVariables) {
323 if (textureProperty.property->isPerInstance) {
327 RenderTexture* renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.texture.handle);
330 deviceContext->setTexture(textureBinding, renderTexture->textureHandle);
333 if (
HandleIsValid(textureProperty.property->texture.handle)) {
335 renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.property->texture.handle);
342 if (
HandleIsValid(renderMaterialInstance->samplerStates[textureProperty.key])) {
343 deviceContext->setSamplerState(samplerBinding, renderMaterialInstance->samplerStates[textureProperty.key]);
352 RenderMaterial* renderMaterial = renderMaterialInstance->renderMaterial;
354 for (
size_t i = 0, N = material->textureProperties.size(); i < N; i++) {
355 const TextureProperty& textureProperty = material->textureProperties[i];
356 if (!textureProperty.isPerInstance) {
358 RenderTexture* renderTexture = drawContext->renderer->getRenderResources().getRenderTexture(textureProperty.texture.handle);
360 deviceContext->setTexture(bindings->textureBindings[textureProperty.key], renderTexture->textureHandle);
366 const RenderMaterial::TexturePropertyState& state = renderMaterial->texturePropertyStates[i];
368 deviceContext->setSamplerState(bindings->samplerBindings[textureProperty.key], state.samplerState);
379void Cogs::Core::applyMaterialPermutation(RenderTaskContext * taskContext,
const DrawContext * drawContext,
const EffectBinding * bindings,
const RenderMaterialInstance * renderMaterialInstance)
381 drawContext->deviceContext->setEffect(bindings->renderEffect->effectHandle);
382 drawContext->deviceContext->setInputLayout(bindings->renderEffect->inputHandle);
383 updateSceneBindings(taskContext, bindings);
384 updateMaterialBindings(drawContext, renderMaterialInstance, bindings,
false);
387void Cogs::Core::applyMaterialInstance(
const DrawContext * drawContext,
const EffectBinding * bindings,
const RenderMaterialInstance * renderMaterialInstance)
389 updateMaterialBindings(drawContext, renderMaterialInstance, bindings,
true);
392size_t Cogs::Core::populateObjectBuffer(
const DrawContext * drawContext,
const RenderItem* items,
size_t o,
size_t n)
394 auto deviceContext = drawContext->deviceContext;
395 auto & engineBuffers = *drawContext->engineBuffers;
396 assert(engineBuffers.objectBatch.count != 0);
398 const auto stride = engineBuffers.objectBatch.stride;
399 auto m = std::min(n - o,
size_t(engineBuffers.objectBatch.count));
401 MappedBuffer<char> objectBatch(deviceContext, engineBuffers.objectBatch.bufferHandle, MapMode::WriteDiscard);
404 for (
size_t i = 0; i < m; i++) {
405 if (items[o + i].isCustom())
continue;
406 auto * obj = (ObjectBuffer*)(objectBatch.get() + stride * i);
407 obj->worldMatrix = *items[o + i].worldMatrix;
408 obj->objectId = items[o + i].objectId;
415void Cogs::Core::applyMaterialPerObjectBatched(
const DrawContext * drawContext,
const RenderMaterialInstance * ,
const RenderItem & renderItem,
size_t o)
417 auto deviceContext = drawContext->deviceContext;
418 auto & engineBuffers = *drawContext->engineBuffers;
419 auto & batch = engineBuffers.objectBatch;
421 const auto bindings = renderItem.binding;
424 deviceContext->setConstantBuffer(bindings->objectBufferBinding,
426 batch.stride * uint32_t(o),
430 if (renderItem.poseData) {
432 deviceContext->updateBuffer(engineBuffers.animationBuffer, renderItem.poseData->transforms,
sizeof(glm::mat4) * renderItem.poseData->numPoses);
433 deviceContext->setConstantBuffer(bindings->animationBufferBinding, engineBuffers.animationBuffer);
439void Cogs::Core::applyMaterialPerObject(
const DrawContext * drawContext,
const RenderMaterialInstance * ,
const RenderItem & renderItem)
441 auto deviceContext = drawContext->deviceContext;
442 auto & engineBuffers = *drawContext->engineBuffers;
444 const auto bindings = renderItem.binding;
447 ListObjectBuffer* unifiedObjectBuffer = drawContext->listObjectBuffer;
449 if (unifiedObjectBuffer && renderItem.hasObjectBufferSlot() &&
HandleIsValid(unifiedObjectBuffer->gpu)) {
450 deviceContext->setConstantBuffer(bindings->objectBufferBinding,
451 unifiedObjectBuffer->gpu,
452 unifiedObjectBuffer->stride * renderItem.objectBufferSlot,
453 unifiedObjectBuffer->stride);
456 MappedBuffer<ObjectBuffer> objectBuffer(deviceContext, engineBuffers.objectBufferHandle, MapMode::WriteDiscard);
458 objectBuffer->worldMatrix = *renderItem.worldMatrix;
459 objectBuffer->objectId = renderItem.objectId;
461 deviceContext->setConstantBuffer(bindings->objectBufferBinding, engineBuffers.objectBufferHandle);
465 if (renderItem.poseData) {
467 deviceContext->updateBuffer(engineBuffers.animationBuffer, renderItem.poseData->transforms,
sizeof(glm::mat4) * renderItem.poseData->numPoses);
468 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.