Cogs.Core
TransparencyTemporalUpscaleTask.cpp
1#include "TransparencyTemporalUpscaleTask.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 TemporalParameters
15 {
16 glm::mat4 inverseClipToWorldMatrix;
17 glm::mat4 prevWorldToClipMatrix;
18 uint32_t width;
19 uint32_t height;
20 uint32_t off_x;
21 uint32_t off_y;
22 uint32_t size_x;
23 uint32_t size_y;
24 };
25}
26
27void Cogs::Core::TransparencyTemporalUpscaleTask::initialize(RenderTaskContext * context)
28{
29 auto device = context->renderer->getDevice();
30 auto buffers = device->getBuffers();
31 auto textures = device->getTextures();
32
33 EffectDescription desc = {};
34 desc.vs = "Engine/FullscreenV3T2VS.hlsl";
35 desc.ps = "Engine/TransparencyTemporalUpscalePS.hlsl";
36 effect = context->renderer->getEffectCache().loadEffect(context, desc);
37 if (!HandleIsValid(effect->handle)) return;
38
39 parameterHandle = device->getBuffers()->loadBuffer(nullptr, sizeof(TemporalParameters), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
40 buffers->annotate(parameterHandle, "TemporalParameters");
41
42 SamplerState samplerState{};
43 samplerState.addressModeS = SamplerState::Clamp;
44 samplerState.addressModeT = SamplerState::Clamp;
45 samplerState.addressModeW = SamplerState::Clamp;
46 samplerState.filter = SamplerState::MinMagMipLinear;
47 for (unsigned i = 0; i < 4; i++)
48 samplerState.borderColor[i] = 0.f;
49 sampler = textures->loadSamplerState(samplerState);
50}
51
52void Cogs::Core::TransparencyTemporalUpscaleTask::cleanup(RenderTaskContext * context)
53{
54 auto device = context->renderer->getDevice();
55 auto buffers = device->getBuffers();
56 auto textures = device->getTextures();
57 context->renderer->getEffectCache().release(context, effect);
58 buffers->releaseBuffer(parameterHandle);
59 textures->releaseSamplerState(sampler);
60}
61
62void Cogs::Core::TransparencyTemporalUpscaleTask::apply(RenderTaskContext * taskContext)
63{
64 RenderInstrumentationScope(taskContext->device->getImmediateContext(), SCOPE_RENDERING, "TransparencyTemporalUpscaleTask::apply");
65
66 uint32_t frame = taskContext->context->time->getFrame();
67 uint32_t widthDivisor = taskContext->context->variables->get("renderer.oit.TemporalUpscaleWidth", 1);
68 uint32_t heightDivisor = taskContext->context->variables->get("renderer.oit.TemporalUpscaleHeight", 1);
69
70 auto device = taskContext->renderer->getDevice();
71 auto deviceContext = device->getImmediateContext();
72
73 auto depthTarget = input.resources[0].renderTarget;
74 auto oitTarget = input.resources[1].renderTarget;
75 auto historyTarget = input.resources[(frame+1)%2+2].renderTarget;
76 auto renderTarget = output.resources[frame%2].renderTarget;
77
78 if (!HandleIsValid(effect->handle)) return;
79 deviceContext->setEffect(effect->handle);
80
81 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
82 deviceContext->setViewport(0, 0, static_cast<float>(renderTarget->width), static_cast<float>(renderTarget->height));
83
84 deviceContext->setDepthStencilState(taskContext->states->noTestDepthStencilStateHandle);
85 deviceContext->setRasterizerState(taskContext->states->defaultRasterizerStateHandle);
86
87 deviceContext->setTexture("depthTexture", 0, depthTarget->depth->textureHandle);
88 deviceContext->setTexture("oitTexture", 0, oitTarget->textures[0]->textureHandle);
89 deviceContext->setTexture("historyTexture", 0, historyTarget->textures[0]->textureHandle);
90 deviceContext->setSamplerState("historySampler", 0, sampler);
91
92 {
93 MappedBuffer<TemporalParameters> parameters(deviceContext, parameterHandle, MapMode::WriteDiscard);
94
95 if (parameters) {
96 auto& cameraData = taskContext->context->cameraSystem->getMainCameraData();
97 uint32_t i = frame % (widthDivisor * heightDivisor);
98
99 parameters->inverseClipToWorldMatrix = cameraData.inverseViewProjectionMatrix;
100 parameters->prevWorldToClipMatrix = cameraData.prevViewProjection;
101 parameters->width = widthDivisor;
102 parameters->height = heightDivisor;
103 parameters->off_x = i % widthDivisor;
104 parameters->off_y = (i / widthDivisor) % heightDivisor;
105 parameters->size_x = historyTarget->width;
106 parameters->size_y = historyTarget->height;
107 }
108 }
109 deviceContext->setConstantBuffer("TemporalParameters", parameterHandle);
110
111 deviceContext->setVertexBuffers(nullptr, 0, nullptr, nullptr);
112 deviceContext->setIndexBuffer(IndexBufferHandle::NoHandle);
113 deviceContext->setInputLayout(InputLayoutHandle::NoHandle);
114 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
115}
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
Encapsulates state for texture sampling in a state object.
Definition: SamplerState.h:12
AddressMode addressModeS
Specifies the addressing mode along the S axis in texture coordinate space.
Definition: SamplerState.h:63
@ Clamp
Texture coordinates are clamped to the [0, 1] range.
Definition: SamplerState.h:17
@ MinMagMipLinear
Linear sampling for both minification and magnification.
Definition: SamplerState.h:35
@ Dynamic
Buffer will be loaded and modified with some frequency.
Definition: Flags.h:30