Cogs.Core
TransparencyUpscaleTask.cpp
1#include "TransparencyUpscaleTask.h"
2
3#include "Context.h"
4#include "Renderer/Renderer.h"
5#include "Renderer/RenderTarget.h"
6#include "Renderer/RenderStateUpdater.h"
7#include "Services/Time.h"
8#include "Services/Variables.h"
9
10#include "Rendering/IBuffers.h"
11#include "Rendering/IEffects.h"
12#include "Rendering/IGraphicsDevice.h"
13
14#include "Foundation/Logging/Logger.h"
15
16#include <cassert>
17
18using namespace Cogs;
19
20namespace
21{
22 struct UpscaleParameters
23 {
24 uint32_t width, height;
25 };
26
27 Cogs::Logging::Log logger = Cogs::Logging::getLogger("TransparencyUpscaleTask");
28}
29
30void Cogs::Core::TransparencyUpscaleTask::initialize(RenderTaskContext * context)
31{
32 IGraphicsDevice* device = context->renderer->getDevice();
33 IBuffers* buffers = device->getBuffers();
34
35 EffectDescription desc = {};
36 desc.vs = "Engine/FullscreenV3T2VS.hlsl";
37 desc.ps = "Engine/TransparencyUpscalePS.hlsl";
38 effect = context->renderer->getEffectCache().loadEffect(context, desc);
39
40 parameterHandle = device->getBuffers()->loadBuffer(nullptr, sizeof(UpscaleParameters), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
41 buffers->annotate(parameterHandle, "UpscaleParameters");
42}
43
44void Cogs::Core::TransparencyUpscaleTask::cleanup(RenderTaskContext * context)
45{
46 auto device = context->renderer->getDevice();
47 auto buffers = device->getBuffers();
48 context->renderer->getEffectCache().release(context, effect);
49 buffers->releaseBuffer(parameterHandle);
50}
51
53{
54 IEffects* effects = context->device->getEffects();
55
56 Cogs::ResourceStatus status = HandleIsValid(effect->handle) ? effects->checkEffect(effect->handle) : Cogs::ResourceStatus::Error;
57
58 if (status == Cogs::ResourceStatus::Error && effectStatus != Cogs::ResourceStatus::Error) {
59 LOG_ERROR(logger, "Invalid transparency upscale effect handle.");
60 }
61
62 effectStatus = status;
63 return effectStatus;
64}
65
66void Cogs::Core::TransparencyUpscaleTask::apply(RenderTaskContext * taskContext)
67{
68 RenderInstrumentationScope(taskContext->device->getImmediateContext(), SCOPE_RENDERING, "TransparencyUpscaleTask::apply");
69
70 assert(effectStatus == Cogs::ResourceStatus::Ready);
71
72 uint32_t frame = taskContext->context->time->getFrame();
73
74 RenderTexture *color = (frame%2==0) ? input.resources[0].renderTexure : input.resources[1].renderTexure;
75 auto deferredRenderTarget = input.get(RenderResourceType::RenderTarget)->renderTarget;
76 auto renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
77
78 auto device = taskContext->renderer->getDevice();
79 auto deviceContext = device->getImmediateContext();
80
81 deviceContext->setEffect(effect->handle);
82
83 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
84 deviceContext->setViewport(0, 0, static_cast<float>(renderTarget->width), static_cast<float>(renderTarget->height));
85
86 deviceContext->setDepthStencilState(taskContext->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
87 deviceContext->setRasterizerState(taskContext->states->defaultRasterizerStateHandle);
88
89 deviceContext->setTexture("oitTexture", 0, color->textureHandle);
90 deviceContext->setTexture("colorTexture", 0, deferredRenderTarget->textures[0]->textureHandle);
91 deviceContext->setTexture("depthTexture", 1, deferredRenderTarget->depth->textureHandle);
92
93 {
94 MappedBuffer<UpscaleParameters> parameters(deviceContext, parameterHandle, MapMode::WriteDiscard);
95
96 if (parameters) {
97 parameters->width = taskContext->context->variables->get("renderer.oit.UpscaleWidth", 1);
98 parameters->height = taskContext->context->variables->get("renderer.oit.UpscaleHeight", 1);
99 }
100 }
101
102 deviceContext->setConstantBuffer("UpscaleParameters", parameterHandle);
103
104 deviceContext->setVertexBuffers(nullptr, 0, nullptr, nullptr);
105 deviceContext->setIndexBuffer(IndexBufferHandle::NoHandle);
106 deviceContext->setInputLayout(InputLayoutHandle::NoHandle);
107 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
108}
std::unique_ptr< class Variables > variables
Variables service instance.
Definition: Context.h:180
std::unique_ptr< class Time > time
Time service instance.
Definition: Context.h:198
IGraphicsDevice * getDevice() override
Get the graphics device used by the renderer.
Definition: Renderer.h:46
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.
Definition: LogManager.h:140
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
Definition: LogManager.h:181
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ResourceStatus
Status of an asynchronously loaded resource, such as an effect or a pipeline.
Definition: Common.h:198
@ 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.
Definition: Flags.h:50
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
Definition: Flags.h:72
Cogs::ResourceStatus checkReady(RenderTaskContext *context) override
Check if the task is ready to be applied, e.g.
Contains an effect description used to load a single effect.
Definition: IEffects.h:62
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78
Provides buffer management functionality.
Definition: IBuffers.h:13
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
Definition: IBuffers.h:17
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.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.
Provides effects and shader management functionality.
Definition: IEffects.h:158
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.
Definition: Flags.h:103
Provides RAII style mapping of a buffer resource.
Definition: IBuffers.h:160
@ Dynamic
Buffer will be loaded and modified with some frequency.
Definition: Flags.h:30