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"
25#include "Foundation/Logging/Logger.h"
33 struct MergeParameters
37 uint32_t WidthDivisor;
38 uint32_t HeightDivisor;
39 float beersScaleFactor;
51 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
53 const char * pixelShaders[] = {
54 "Engine/TransparencyBlendAlphaPS.hlsl",
55 "Engine/TransparencyBlendMaxPS.hlsl",
56 "Engine/TransparencyBlendMax2PS.hlsl",
57 "Engine/TransparencyBlendDepthWeightedPS.hlsl",
58 "Engine/TransparencyBlendBeersPS.hlsl"
61 desc.ps = pixelShaders[(int)context->renderer->
getSettings().blendKernel];
63 desc.definitions.push_back({
"COGS_FORWARD_SHADOWS_ENABLED",
"1" });
64 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_SHADOW_LIGHTS", std::to_string(context->renderer->
getActiveLights().numDirectionalShadowLights) });
65 desc.definitions.push_back({
"COGS_NUM_DIRECTIONAL_LIGHTS", std::to_string(context->renderer->
getActiveLights().numDirectionalLights) });
66 desc.definitions.push_back({
"COGS_NUM_POINT_SHADOW_LIGHTS", std::to_string(context->renderer->
getActiveLights().numPointShadowLights) });
67 desc.definitions.push_back({
"COGS_NUM_POINT_LIGHTS", std::to_string(context->renderer->
getActiveLights().numPointLights) });
69 desc.definitions.push_back({
"COGS_DISABLE_DIRECTIONAL_SHADOWS",
"1" });
71 if(context->context->
variables->get(
"renderer.reverseDepth",
false)){
72 desc.definitions.push_back({
"COGS_REVERSE_DEPTH",
"1" });
75 if (
auto ec = context->context->environmentSystem->getGlobalEnvironment(); ec !=
nullptr) {
76 if (ec->subseaSupport) {
77 desc.definitions.push_back({
"COGS_SUBSEA_SUPPORT",
"1" });
79 if (ec->imageBasedLighting) {
80 desc.definitions.push_back({
"COGS_IMAGE_BASED_LIGHTING",
"1" });
83 effect = context->renderer->
getEffectCache().loadEffect(context, desc);
93 buffers->
annotate(parameterHandle,
"MergeParameters");
94 DeferredLightingTask::initializeGlobalBindings(context, effect, bindings);
98 LOG_ERROR(logger,
"Invalid transparency merge effect handle.");
102 effectStatus = status;
108 auto device = context->renderer->
getDevice();
112 buffers->releaseBuffer(parameterHandle);
116void Cogs::Core::TransparencyMergeTask::apply(RenderTaskContext * taskContext)
118 RenderInstrumentationScope(taskContext->device->getImmediateContext(), SCOPE_RENDERING,
"TransparencyMergeTask::apply");
122 RenderTarget* deferredRenderTarget = input.get(RenderResourceType::RenderTarget)->renderTarget;
123 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
125 RenderBuffer* listTarget = input.get(
"ListBuffer")->renderBuffer;
126 RenderBuffer* nodeTarget = input.get(
"NodeBuffer")->renderBuffer;
127 RenderBuffer* dataTarget = input.get(
"DataBuffer")->renderBuffer;
132 deviceContext->
setEffect(effect->handle);
134 deviceContext->
setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
138 deviceContext->
clearDepth(taskContext->context->renderer->getClearDepth());
141 deviceContext->
setViewport(0, 0,
static_cast<float>(renderTarget->width),
static_cast<float>(renderTarget->height));
143 deviceContext->
setDepthStencilState(taskContext->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
146 if(deferredRenderTarget->textures.size())
147 deviceContext->
setTexture(
"colorTexture", 0, deferredRenderTarget->textures[0]->textureHandle);
148 deviceContext->
setTexture(
"depthTexture", 1, deferredRenderTarget->depth->textureHandle);
150 DeferredLightingTask::applyGlobalBindings(taskContext, bindings);
152 uint32_t widthDivisor = 1;
153 uint32_t heightDivisor = 1;
159 parameters->alloc =
static_cast<int>(nodeTarget->bufferSize);
160 parameters->width = renderTarget->width/widthDivisor;
161 parameters->WidthDivisor = widthDivisor;
162 parameters->HeightDivisor = heightDivisor;
163 parameters->beersScaleFactor = taskContext->renderer->getSettings().beersScaleFactor;
169 deviceContext->
setBuffer(
"List", listTarget->buffer);
170 deviceContext->
setBuffer(
"Node", nodeTarget->buffer);
171 deviceContext->
setBuffer(
"Data", dataTarget->buffer);
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.
const RenderSettings & getSettings() const override
Get the settings of 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.
virtual IBuffers * getBuffers()=0
Get a pointer to the buffer management interface.
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
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.
@ 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.
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.
Provides buffer management functionality.
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
virtual BufferHandle loadBuffer(const void *data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride=0)=0
Loads a new buffer using the given data to populate the buffer.
virtual void releaseBuffer(BufferHandle bufferHandle)=0
Releases the buffer with the given bufferHandle.
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 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.
Provides effects and shader management functionality.
virtual ResourceStatus checkEffect(EffectHandle effectHandle)=0
Check the load status of the effect with the given effectHandle.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.
@ Dynamic
Buffer will be loaded and modified with some frequency.