1#include "DeferredLightingTask.h"
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IContext.h"
5#include "Rendering/CommandGroupAnnotation.h"
8#include "Renderer/Renderer.h"
9#include "Renderer/RenderTarget.h"
10#include "Renderer/RenderStateUpdater.h"
11#include "Services/Variables.h"
12#include "Systems/Core/LightSystem.h"
13#include "Systems/Core/EnvironmentSystem.h"
14#include "Utilities/Parsing.h"
18bool Cogs::Core::DeferredLightingTask::setupEffect(RenderTaskContext * context)
20 auto device = context->renderer->getDevice();
21 auto deviceContext = device->getImmediateContext();
25 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
26 desc.ps =
"Engine/DeferredLightingPS.hlsl";
27 desc.definitions.push_back({
"COGS_FORWARD_SHADOWS_ENABLED",
"1" });
28 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_SHADOW_LIGHTS", std::to_string(context->renderer->getActiveLights().numDirectionalShadowLights) });
29 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_LIGHTS", std::to_string(context->renderer->getActiveLights().numDirectionalLights) });
30 desc.definitions.push_back({
"COGS_NUM_POINT_SHADOW_LIGHTS", std::to_string(context->renderer->getActiveLights().numPointShadowLights) });
31 desc.definitions.push_back({
"COGS_NUM_POINT_LIGHTS", std::to_string(context->renderer->getActiveLights().numPointLights) });
33 auto softShadows = parseEnum<SoftShadows>(context->context->variables->get(
"shadows.softShadows",
"Default"));
34 switch (softShadows) {
35 case SoftShadows::High:
36 desc.definitions.push_back({
"COGS_SOFT_SHADOWS_HIGH",
"1" });
38 case SoftShadows::Low:
39 desc.definitions.push_back({
"COGS_SOFT_SHADOWS_LOW",
"1" });
44 if (
auto ec = context->context->environmentSystem->getGlobalEnvironment(); ec !=
nullptr) {
45 if (ec->subseaSupport) {
46 desc.definitions.push_back({
"COGS_SUBSEA_SUPPORT",
"1" });
48 if (ec->imageBasedLighting) {
49 desc.definitions.push_back({
"COGS_IMAGE_BASED_LIGHTING",
"1" });
52 if(context->context->variables->get(
"renderer.reverseDepth",
false)){
53 desc.definitions.push_back({
"COGS_REVERSE_DEPTH",
"1" });
55 effect = context->renderer->getEffectCache().loadEffect(context, desc);
56 if (
HandleIsValid(effect->handle) && (effectPrev != effect)) {
57 initializeGlobalBindings(context, effect, bindings);
63void Cogs::Core::DeferredLightingTask::cleanup(RenderTaskContext * context)
65 context->renderer->getEffectCache().release(context, effect);
68void Cogs::Core::DeferredLightingTask::apply(RenderTaskContext * context)
70 RenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING,
"DeferredLightingTask::apply");
72 RenderTarget* deferredRenderTarget = input.get(RenderResourceType::RenderTarget)->renderTarget;
73 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
77 if (!setupEffect(context)) {
88 context->renderer->getSize().x,
89 context->renderer->getSize().y);
97 applyGlobalBindings(context, bindings);
99 const char * textureName[] = {
107 deviceContext->
setTexture(
"colorTexture", 0, deferredRenderTarget->get(
"Color"));
108 deviceContext->
setTexture(
"emissiveTexture", 1, deferredRenderTarget->get(
"Emissive"));
109 deviceContext->
setTexture(
"specularTexture", 2, deferredRenderTarget->get(
"Specular"));
110 deviceContext->
setTexture(
"normalTexture", 3, deferredRenderTarget->get(
"Normals"));
111 deviceContext->
setTexture(
"depthTexture", 4, deferredRenderTarget->depth->textureHandle);
118 for (
size_t i = 0; i < 5; ++i) {
123void Cogs::Core::DeferredLightingTask::initializeGlobalBindings(RenderTaskContext * context, CachedEffect * effect, GlobalBinding & bindings)
125 auto effects = context->device->getEffects();
127 bindings.shadowBufferBinding = effects->getConstantBufferBinding(effect->handle,
"ShadowBuffer");
129 bindings.shadowArrayBinding = effects->getTextureBinding(effect->handle,
"cascadedShadowMap", 1);
130 bindings.shadowArrayBinding_1 = effects->getTextureBinding(effect->handle,
"cascadedShadowMap_1", 1);
131 bindings.shadowCubeArrayBinding = effects->getTextureBinding(effect->handle,
"cubeShadowMap", 2);
132 bindings.shadowCubeArrayBinding_1 = effects->getTextureBinding(effect->handle,
"cubeShadowMap_1", 2);
133 bindings.shadowSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"cascadedShadowSampler", 2);
135 bindings.linearSampler = effects->getSamplerStateBinding(effect->handle,
"linearSampler", 0);
136 bindings.radianceSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"environmentRadianceSampler", 0);
137 bindings.irradianceSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"environmentIrradianceSampler", 0);
138 bindings.ambientIrradianceSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"ambientIrradianceSampler", 0);
140 bindings.lightBufferBinding = effects->getConstantBufferBinding(effect->handle,
"LightBuffer");
141 bindings.sceneBufferBinding = effects->getConstantBufferBinding(effect->handle,
"SceneBuffer");
144void Cogs::Core::DeferredLightingTask::applyGlobalBindings(RenderTaskContext * context, GlobalBinding & bindings)
146 auto deviceContext = context->device->getImmediateContext();
147 auto engineBuffers = context->engineBuffers;
150 deviceContext->
setConstantBuffer(bindings.sceneBufferBinding, engineBuffers->sceneBufferHandle);
154 deviceContext->
setConstantBuffer(bindings.lightBufferBinding, engineBuffers->lightBufferHandle);
157 auto shadowsEnabled = context->context->variables->get(
"renderer.shadowsEnabled",
false);
158 auto lightSystem = context->context->lightSystem;
160 if (shadowsEnabled) {
162 deviceContext->
setConstantBuffer(bindings.shadowBufferBinding, engineBuffers->shadowBufferHandle);
165 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cascadeArray);
168 deviceContext->
setTexture(bindings.shadowArrayBinding, shadowTexture->textureHandle);
172 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cascadeArray);
175 deviceContext->
setTexture(bindings.shadowArrayBinding_1, shadowTexture->textureHandle);
180 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cubeArray);
183 deviceContext->
setTexture(bindings.shadowCubeArrayBinding, shadowTexture->textureHandle);
187 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cubeArray);
190 deviceContext->
setTexture(bindings.shadowCubeArrayBinding_1, shadowTexture->textureHandle);
196 deviceContext->
setSamplerState(bindings.shadowSamplerBinding, context->states->shadowSampler);
200 deviceContext->
setSamplerState(bindings.radianceSamplerBinding, context->states->defaultSampler);
203 deviceContext->
setSamplerState(bindings.irradianceSamplerBinding, context->states->defaultSampler);
205 if (
HandleIsValid(bindings.ambientIrradianceSamplerBinding)) {
206 deviceContext->
setSamplerState(bindings.ambientIrradianceSamplerBinding, context->states->defaultSampler);
209 deviceContext->
setSamplerState(bindings.brdfLUTSamplerBinding, context->states->defaultSampler);
212 deviceContext->
setSamplerState(bindings.linearSampler, context->states->defaultSampler);
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual IContext * getImmediateContext()=0
Get a pointer to the immediate context used to issue commands to the graphics device.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
@ None
No blending enabled for opaque shapes, defaults to Blend for transparent shapes.
Contains all Cogs related functionality.
RAII-helper for pushCommandGroupAnnotation/pushCommandGroupAnnotation.
Contains an effect description used to load a single effect.
static const Handle_t NoHandle
Represents a handle to nothing.
Represents a graphics device context which can receive rendering commands.
virtual void setTexture(const StringView &name, unsigned int unit, TextureHandle textureHandle)=0
Sets the texture slot given by unit with the given name to contain the given texture.
virtual void setRasterizerState(const RasterizerStateHandle handle)=0
Set the current rasterizer state.
virtual void setBlendState(const BlendStateHandle handle, const float *constant=nullptr)=0
Set the current blend state.
virtual void setInputLayout(const InputLayoutHandle inputLayoutHandle)=0
Sets the current input layout.
virtual void setIndexBuffer(IndexBufferHandle bufferHandle, uint32_t stride=4, uint32_t offset=0)=0
Sets the current index buffer.
virtual void setDepthStencilState(const DepthStencilStateHandle handle)=0
Set the current depth stencil state.
virtual void setConstantBuffer(const StringView &name, const BufferHandle bufferHandle, const uint32_t offset=0, const uint32_t size=~0u)=0
Sets a constant buffer to be bound to the given name and slot.
virtual void setViewport(const float x, const float y, const float width, const float height)=0
Sets the current viewport to the given location and dimensions.
virtual void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes)=0
Draws non-indexed, non-instanced primitives.
virtual void setSamplerState(const StringView &name, unsigned int unit, SamplerStateHandle samplerStateHandle)=0
Sets the sampler slot given by unit with the given name to contain the given sampler state.
virtual void setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets)=0
Sets the current vertex buffers.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.
virtual void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle)=0
Sets the current render target and an associated depth stencil target.
@ TriangleList
List of triangles.