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
10using namespace Cogs;
11
12namespace
13{
14 struct UpscaleParameters
15 {
16 uint32_t width, height;
17 };
18}
19
20void Cogs::Core::TransparencyUpscaleTask::initialize(RenderTaskContext * context)
21{
22 auto device = context->renderer->getDevice();
23 auto buffers = device->getBuffers();
24
25 EffectDescription desc = {};
26 desc.vs = "Engine/FullscreenV3T2VS.hlsl";
27 desc.ps = "Engine/TransparencyUpscalePS.hlsl";
28 effect = context->renderer->getEffectCache().loadEffect(context, desc);
29 if (!HandleIsValid(effect->handle)) return;
30
31 parameterHandle = device->getBuffers()->loadBuffer(nullptr, sizeof(UpscaleParameters), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
32 buffers->annotate(parameterHandle, "UpscaleParameters");
33}
34
35void Cogs::Core::TransparencyUpscaleTask::cleanup(RenderTaskContext * context)
36{
37 auto device = context->renderer->getDevice();
38 auto buffers = device->getBuffers();
39 context->renderer->getEffectCache().release(context, effect);
40 buffers->releaseBuffer(parameterHandle);
41}
42
43void Cogs::Core::TransparencyUpscaleTask::apply(RenderTaskContext * taskContext)
44{
45 RenderInstrumentationScope(taskContext->device->getImmediateContext(), SCOPE_RENDERING, "TransparencyUpscaleTask::apply");
46
47 uint32_t frame = taskContext->context->time->getFrame();
48
49 RenderTexture *color = (frame%2==0) ? input.resources[0].renderTexure : input.resources[1].renderTexure;
50 auto deferredRenderTarget = input.get(RenderResourceType::RenderTarget)->renderTarget;
51 auto renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
52
53 auto device = taskContext->renderer->getDevice();
54 auto deviceContext = device->getImmediateContext();
55
56 if (!HandleIsValid(effect->handle)) return;
57 deviceContext->setEffect(effect->handle);
58
59 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
60 deviceContext->setViewport(0, 0, static_cast<float>(renderTarget->width), static_cast<float>(renderTarget->height));
61
62 deviceContext->setDepthStencilState(taskContext->states->noTestDepthStencilStateHandle);
63 deviceContext->setRasterizerState(taskContext->states->defaultRasterizerStateHandle);
64
65 deviceContext->setTexture("oitTexture", 0, color->textureHandle);
66 deviceContext->setTexture("colorTexture", 0, deferredRenderTarget->textures[0]->textureHandle);
67 deviceContext->setTexture("depthTexture", 1, deferredRenderTarget->depth->textureHandle);
68
69 {
70 MappedBuffer<UpscaleParameters> parameters(deviceContext, parameterHandle, MapMode::WriteDiscard);
71
72 if (parameters) {
73 parameters->width = taskContext->context->variables->get("renderer.oit.UpscaleWidth", 1);
74 parameters->height = taskContext->context->variables->get("renderer.oit.UpscaleHeight", 1);
75 }
76 }
77
78 deviceContext->setConstantBuffer("UpscaleParameters", parameterHandle);
79
80 deviceContext->setVertexBuffers(nullptr, 0, nullptr, nullptr);
81 deviceContext->setIndexBuffer(IndexBufferHandle::NoHandle);
82 deviceContext->setInputLayout(InputLayoutHandle::NoHandle);
83 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
84}
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.
Definition: FieldSetter.h:23
@ 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
Contains an effect description used to load a single effect.
Definition: IEffects.h:55
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:77
@ 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
@ TriangleList
List of triangles.
Definition: Common.h:116
@ Dynamic
Buffer will be loaded and modified with some frequency.
Definition: Flags.h:30