1#include "DeferredLightingTask.h"
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IContext.h"
5#include "Rendering/IEffects.h"
6#include "Rendering/CommandGroupAnnotation.h"
9#include "Renderer/Renderer.h"
10#include "Renderer/RenderTarget.h"
11#include "Renderer/RenderStateUpdater.h"
12#include "Services/Variables.h"
13#include "Systems/Core/LightSystem.h"
14#include "Systems/Core/EnvironmentSystem.h"
15#include "Utilities/Parsing.h"
17#include "Foundation/Logging/Logger.h"
35 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
36 desc.ps =
"Engine/DeferredLightingPS.hlsl";
37 desc.definitions.push_back({
"COGS_FORWARD_SHADOWS_ENABLED",
"1" });
38 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_SHADOW_LIGHTS", std::to_string(context->renderer->
getActiveLights().numDirectionalShadowLights) });
39 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_LIGHTS", std::to_string(context->renderer->
getActiveLights().numDirectionalLights) });
40 desc.definitions.push_back({
"COGS_NUM_POINT_SHADOW_LIGHTS", std::to_string(context->renderer->
getActiveLights().numPointShadowLights) });
41 desc.definitions.push_back({
"COGS_NUM_POINT_LIGHTS", std::to_string(context->renderer->
getActiveLights().numPointLights) });
43 auto softShadows = parseEnum<SoftShadows>(context->context->
variables->get(
"shadows.softShadows",
"Default"));
44 switch (softShadows) {
45 case SoftShadows::High:
46 desc.definitions.push_back({
"COGS_SOFT_SHADOWS_HIGH",
"1" });
48 case SoftShadows::Low:
49 desc.definitions.push_back({
"COGS_SOFT_SHADOWS_LOW",
"1" });
54 if (
auto ec = context->context->environmentSystem->getGlobalEnvironment(); ec !=
nullptr) {
55 if (ec->subseaSupport) {
56 desc.definitions.push_back({
"COGS_SUBSEA_SUPPORT",
"1" });
58 if (ec->imageBasedLighting) {
59 desc.definitions.push_back({
"COGS_IMAGE_BASED_LIGHTING",
"1" });
62 if(context->context->
variables->get(
"renderer.reverseDepth",
false)){
63 desc.definitions.push_back({
"COGS_REVERSE_DEPTH",
"1" });
65 effect = context->renderer->
getEffectCache().loadEffect(context, desc);
71 initializeGlobalBindings(context, effect, bindings);
75 LOG_ERROR(logger,
"Invalid deferred lighting effect handle.");
79 effectStatus = status;
88void Cogs::Core::DeferredLightingTask::apply(RenderTaskContext * context)
90 RenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING,
"DeferredLightingTask::apply");
94 RenderTarget* deferredRenderTarget = input.get(RenderResourceType::RenderTarget)->renderTarget;
95 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
100 deviceContext->
setEffect(effect->handle);
107 context->renderer->getSize().x,
108 context->renderer->getSize().y);
110 deviceContext->
setDepthStencilState(context->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
114 applyGlobalBindings(context, bindings);
116 const char * textureName[] = {
124 deviceContext->
setTexture(
"colorTexture", 0, deferredRenderTarget->get(
"Color"));
125 deviceContext->
setTexture(
"emissiveTexture", 1, deferredRenderTarget->get(
"Emissive"));
126 deviceContext->
setTexture(
"specularTexture", 2, deferredRenderTarget->get(
"Specular"));
127 deviceContext->
setTexture(
"normalTexture", 3, deferredRenderTarget->get(
"Normals"));
128 deviceContext->
setTexture(
"depthTexture", 4, deferredRenderTarget->depth->textureHandle);
135 for (
size_t i = 0; i < 5; ++i) {
140void Cogs::Core::DeferredLightingTask::initializeGlobalBindings(RenderTaskContext * context, CachedEffect * effect, GlobalBinding & bindings)
142 auto effects = context->device->getEffects();
144 bindings.sceneBufferBinding = effects->getConstantBufferBinding(effect->handle,
"SceneBuffer");
146 bindings.shadowArrayBinding = effects->getTextureBinding(effect->handle,
"cascadedShadowMap", 1);
147 bindings.shadowArrayBinding_1 = effects->getTextureBinding(effect->handle,
"cascadedShadowMap_1", 1);
148 bindings.shadowCubeArrayBinding = effects->getTextureBinding(effect->handle,
"cubeShadowMap", 2);
149 bindings.shadowCubeArrayBinding_1 = effects->getTextureBinding(effect->handle,
"cubeShadowMap_1", 2);
150 bindings.shadowSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"cascadedShadowSampler", 2);
152 bindings.linearSampler = effects->getSamplerStateBinding(effect->handle,
"linearSampler", 0);
153 bindings.radianceSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"environmentRadianceSampler", 0);
154 bindings.irradianceSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"environmentIrradianceSampler", 0);
155 bindings.ambientIrradianceSamplerBinding = effects->getSamplerStateBinding(effect->handle,
"ambientIrradianceSampler", 0);
158void Cogs::Core::DeferredLightingTask::applyGlobalBindings(RenderTaskContext * context, GlobalBinding & bindings)
160 auto deviceContext = context->device->getImmediateContext();
161 auto engineBuffers = context->engineBuffers;
164 deviceContext->
setConstantBuffer(bindings.sceneBufferBinding, engineBuffers->sceneBufferHandle);
167 auto shadowsEnabled = context->context->variables->get(
"renderer.shadowsEnabled",
false);
168 auto lightSystem = context->context->lightSystem;
170 if (shadowsEnabled) {
172 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cascadeArray);
175 deviceContext->
setTexture(bindings.shadowArrayBinding, shadowTexture->textureHandle);
179 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cascadeArray);
182 deviceContext->
setTexture(bindings.shadowArrayBinding_1, shadowTexture->textureHandle);
187 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cubeArray);
190 deviceContext->
setTexture(bindings.shadowCubeArrayBinding, shadowTexture->textureHandle);
194 auto shadowTexture = context->resources->getRenderTexture(lightSystem->cubeArray);
197 deviceContext->
setTexture(bindings.shadowCubeArrayBinding_1, shadowTexture->textureHandle);
202 deviceContext->
setSamplerState(bindings.shadowSamplerBinding, context->states->shadowSampler);
206 deviceContext->
setSamplerState(bindings.radianceSamplerBinding, context->states->defaultSampler);
209 deviceContext->
setSamplerState(bindings.irradianceSamplerBinding, context->states->defaultSampler);
211 if (
HandleIsValid(bindings.ambientIrradianceSamplerBinding)) {
212 deviceContext->
setSamplerState(bindings.ambientIrradianceSamplerBinding, context->states->defaultSampler);
215 deviceContext->
setSamplerState(bindings.brdfLUTSamplerBinding, context->states->defaultSampler);
218 deviceContext->
setSamplerState(bindings.linearSampler, context->states->defaultSampler);
std::unique_ptr< class Variables > variables
Variables service instance.
ActiveLights & getActiveLights() override
Get the reference to the ActiveLights structure.
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
EffectCache & getEffectCache() override
Get the reference to the EffectCache structure.
Represents a graphics device used to manage graphics resources and issue drawing commands.
virtual IEffects * getEffects()=0
Get a pointer to the effect management interface.
virtual IContext * getImmediateContext()=0
Get a pointer to the immediate context used to issue commands to the graphics device.
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.
@ None
No blending enabled for opaque shapes, defaults to Blend for transparent shapes.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
ResourceStatus
Status of an asynchronously loaded resource, such as an effect or a pipeline.
@ Error
The resource failed to load.
@ Ready
The resource has loaded successfully and is ready for use.
@ TriangleList
List of triangles.
RAII-helper for pushCommandGroupAnnotation/pushCommandGroupAnnotation.
Cogs::ResourceStatus checkReady(RenderTaskContext *context) override
Check if the task is ready to be applied, e.g.
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 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.
Provides effects and shader management functionality.
virtual ResourceStatus checkEffect(EffectHandle effectHandle)=0
Check the load status of the effect with the given effectHandle.