1#include "TransparencyMergeTask.h"
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IBuffers.h"
5#include "Rendering/IEffects.h"
6#include "Rendering/IContext.h"
8#include "Renderer/Renderer.h"
9#include "Renderer/RenderBuffer.h"
10#include "Renderer/RenderTarget.h"
11#include "Renderer/RenderStateUpdater.h"
13#include "Resources/VertexFormats.h"
17#include "Services/Variables.h"
19#include "Systems/Core/CameraSystem.h"
20#include "Systems/Core/LightSystem.h"
21#include "Systems/Core/EnvironmentSystem.h"
23#include "DeferredLightingTask.h"
29 struct MergeParameters
33 uint32_t WidthDivisor;
34 uint32_t HeightDivisor;
35 float beersScaleFactor;
39bool Cogs::Core::TransparencyMergeTask::setupEffect(RenderTaskContext * context)
41 auto device = context->renderer->getDevice();
42 auto buffers = device->getBuffers();
45 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
47 const char * pixelShaders[] = {
48 "Engine/TransparencyBlendAlphaPS.hlsl",
49 "Engine/TransparencyBlendMaxPS.hlsl",
50 "Engine/TransparencyBlendMax2PS.hlsl",
51 "Engine/TransparencyBlendDepthWeightedPS.hlsl",
52 "Engine/TransparencyBlendBeersPS.hlsl"
55 desc.ps = pixelShaders[(int)context->renderer->getSettings().blendKernel];
57 desc.definitions.push_back({
"COGS_FORWARD_SHADOWS_ENABLED",
"1" });
58 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_SHADOW_LIGHTS", std::to_string(context->renderer->getActiveLights().numDirectionalShadowLights) });
59 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_LIGHTS", std::to_string(context->renderer->getActiveLights().numDirectionalLights) });
60 desc.definitions.push_back({
"COGS_NUM_POINT_SHADOW_LIGHTS", std::to_string(context->renderer->getActiveLights().numPointShadowLights) });
61 desc.definitions.push_back({
"COGS_NUM_POINT_LIGHTS", std::to_string(context->renderer->getActiveLights().numPointLights) });
63 desc.definitions.push_back({
"COGS_DISABLE_DIRECTIONAL_SHADOWS",
"1" });
65 if(context->context->variables->get(
"renderer.reverseDepth",
false)){
66 desc.definitions.push_back({
"COGS_REVERSE_DEPTH",
"1" });
69 if (
auto ec = context->context->environmentSystem->getGlobalEnvironment(); ec !=
nullptr) {
70 if (ec->subseaSupport) {
71 desc.definitions.push_back({
"COGS_SUBSEA_SUPPORT",
"1" });
73 if (ec->imageBasedLighting) {
74 desc.definitions.push_back({
"COGS_IMAGE_BASED_LIGHTING",
"1" });
77 effect = context->renderer->getEffectCache().loadEffect(context, desc);
78 if (
HandleIsValid(effect->handle) && (effectPrev != effect)) {
80 buffers->annotate(parameterHandle,
"MergeParameters");
81 DeferredLightingTask::initializeGlobalBindings(context, effect, bindings);
87void Cogs::Core::TransparencyMergeTask::cleanup(RenderTaskContext * context)
89 auto device = context->renderer->getDevice();
90 auto buffers = device->getBuffers();
91 context->renderer->getEffectCache().release(context, effect);
92 buffers->releaseBuffer(parameterHandle);
95void Cogs::Core::TransparencyMergeTask::apply(RenderTaskContext * taskContext)
97 RenderInstrumentationScope(taskContext->device->getImmediateContext(), SCOPE_RENDERING,
"TransparencyMergeTask::apply");
99 RenderTarget* deferredRenderTarget = input.get(RenderResourceType::RenderTarget)->renderTarget;
100 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
102 RenderBuffer* listTarget = input.get(
"ListBuffer")->renderBuffer;
103 RenderBuffer* nodeTarget = input.get(
"NodeBuffer")->renderBuffer;
104 RenderBuffer* dataTarget = input.get(
"DataBuffer")->renderBuffer;
109 if (!setupEffect(taskContext)) {
112 deviceContext->
setEffect(effect->handle);
114 deviceContext->
setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
118 deviceContext->
clearDepth(taskContext->context->renderer->getClearDepth());
121 deviceContext->
setViewport(0, 0,
static_cast<float>(renderTarget->width),
static_cast<float>(renderTarget->height));
126 if(deferredRenderTarget->textures.size())
127 deviceContext->
setTexture(
"colorTexture", 0, deferredRenderTarget->textures[0]->textureHandle);
128 deviceContext->
setTexture(
"depthTexture", 1, deferredRenderTarget->depth->textureHandle);
130 DeferredLightingTask::applyGlobalBindings(taskContext, bindings);
132 uint32_t widthDivisor = 1;
133 uint32_t heightDivisor = 1;
139 parameters->alloc =
static_cast<int>(nodeTarget->bufferSize);
140 parameters->width = renderTarget->width/widthDivisor;
141 parameters->WidthDivisor = widthDivisor;
142 parameters->HeightDivisor = heightDivisor;
143 parameters->beersScaleFactor = taskContext->renderer->getSettings().beersScaleFactor;
149 deviceContext->
setBuffer(
"List", listTarget->buffer);
150 deviceContext->
setBuffer(
"Node", nodeTarget->buffer);
151 deviceContext->
setBuffer(
"Data", dataTarget->buffer);
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.
Contains all Cogs related functionality.
@ Write
The buffer can be mapped and written to by the CPU after creation.
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
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 setInputLayout(const InputLayoutHandle inputLayoutHandle)=0
Sets the current input layout.
virtual void clearRenderTarget(const float *value)=0
Clear the currently set render target to the given value (4 component floating point RGBA).
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 clearDepth(const float depth=1.0f)=0
Clear the currently set depth/stencil target to the given depth.
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 setBuffer(const StringView &name, BufferHandle bufferHandle)=0
Sets the given buffer to the buffer binding slot with the given name.
virtual void draw(PrimitiveType::EPrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes)=0
Draws non-indexed, non-instanced primitives.
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.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.
@ TriangleList
List of triangles.
@ Dynamic
Buffer will be loaded and modified with some frequency.