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
10#include "Rendering/IBuffers.h"
11#include "Rendering/IEffects.h"
12#include "Rendering/IGraphicsDevice.h"
13#include "Rendering/ITextures.h"
14
15#include "Foundation/Logging/Logger.h"
16
17#include <cassert>
18
19using namespace Cogs;
20
21namespace
22{
23 struct TemporalParameters
24 {
25 glm::mat4 inverseClipToWorldMatrix;
26 glm::mat4 prevWorldToClipMatrix;
27 uint32_t width;
28 uint32_t height;
29 uint32_t off_x;
30 uint32_t off_y;
31 uint32_t size_x;
32 uint32_t size_y;
33 };
34
35 Cogs::Logging::Log logger = Cogs::Logging::getLogger("TransparencyTemporalUpscaleTask");
36}
37
38void Cogs::Core::TransparencyTemporalUpscaleTask::initialize(RenderTaskContext * context)
39{
40 IGraphicsDevice* device = context->renderer->getDevice();
41 IBuffers* buffers = device->getBuffers();
42 ITextures* textures = device->getTextures();
43
44 EffectDescription desc = {};
45 desc.vs = "Engine/FullscreenV3T2VS.hlsl";
46 desc.ps = "Engine/TransparencyTemporalUpscalePS.hlsl";
47 effect = context->renderer->getEffectCache().loadEffect(context, desc);
48
49 parameterHandle = device->getBuffers()->loadBuffer(nullptr, sizeof(TemporalParameters), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
50 buffers->annotate(parameterHandle, "TemporalParameters");
51
52 SamplerState samplerState{};
53 samplerState.addressModeS = SamplerState::Clamp;
54 samplerState.addressModeT = SamplerState::Clamp;
55 samplerState.addressModeW = SamplerState::Clamp;
56 samplerState.filter = SamplerState::MinMagMipLinear;
57 for (unsigned i = 0; i < 4; i++)
58 samplerState.borderColor[i] = 0.f;
59 sampler = textures->loadSamplerState(samplerState);
60}
61
62void Cogs::Core::TransparencyTemporalUpscaleTask::cleanup(RenderTaskContext * context)
63{
64 auto device = context->renderer->getDevice();
65 auto buffers = device->getBuffers();
66 auto textures = device->getTextures();
67 context->renderer->getEffectCache().release(context, effect);
68 buffers->releaseBuffer(parameterHandle);
69 textures->releaseSamplerState(sampler);
70}
71
73{
74 IEffects* effects = context->device->getEffects();
75
76 Cogs::ResourceStatus status = HandleIsValid(effect->handle) ? effects->checkEffect(effect->handle) : Cogs::ResourceStatus::Error;
77
78 if (status == Cogs::ResourceStatus::Error && effectStatus != Cogs::ResourceStatus::Error) {
79 LOG_ERROR(logger, "Invalid transparency temporal upscale effect handle.");
80 }
81
82 effectStatus = status;
83 return effectStatus;
84}
85
86void Cogs::Core::TransparencyTemporalUpscaleTask::apply(RenderTaskContext * taskContext)
87{
88 RenderInstrumentationScope(taskContext->device->getImmediateContext(), SCOPE_RENDERING, "TransparencyTemporalUpscaleTask::apply");
89
90 assert(effectStatus == Cogs::ResourceStatus::Ready);
91
92 uint32_t frame = taskContext->context->time->getFrame();
93 uint32_t widthDivisor = taskContext->context->variables->get("renderer.oit.TemporalUpscaleWidth", 1);
94 uint32_t heightDivisor = taskContext->context->variables->get("renderer.oit.TemporalUpscaleHeight", 1);
95
96 auto device = taskContext->renderer->getDevice();
97 auto deviceContext = device->getImmediateContext();
98
99 auto depthTarget = input.resources[0].renderTarget;
100 auto oitTarget = input.resources[1].renderTarget;
101 auto historyTarget = input.resources[(frame+1)%2+2].renderTarget;
102 auto renderTarget = output.resources[frame%2].renderTarget;
103
104 deviceContext->setEffect(effect->handle);
105
106 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
107 deviceContext->setViewport(0, 0, static_cast<float>(renderTarget->width), static_cast<float>(renderTarget->height));
108
109 deviceContext->setDepthStencilState(taskContext->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
110 deviceContext->setRasterizerState(taskContext->states->defaultRasterizerStateHandle);
111
112 deviceContext->setTexture("depthTexture", 0, depthTarget->depth->textureHandle);
113 deviceContext->setTexture("oitTexture", 0, oitTarget->textures[0]->textureHandle);
114 deviceContext->setTexture("historyTexture", 0, historyTarget->textures[0]->textureHandle);
115 deviceContext->setSamplerState("historySampler", 0, sampler);
116
117 {
118 MappedBuffer<TemporalParameters> parameters(deviceContext, parameterHandle, MapMode::WriteDiscard);
119
120 if (parameters) {
121 auto& cameraData = taskContext->context->cameraSystem->getMainCameraData();
122 uint32_t i = frame % (widthDivisor * heightDivisor);
123
124 parameters->inverseClipToWorldMatrix = cameraData.inverseViewProjectionMatrix;
125 parameters->prevWorldToClipMatrix = cameraData.prevViewProjection;
126 parameters->width = widthDivisor;
127 parameters->height = heightDivisor;
128 parameters->off_x = i % widthDivisor;
129 parameters->off_y = (i / widthDivisor) % heightDivisor;
130 parameters->size_x = historyTarget->width;
131 parameters->size_y = historyTarget->height;
132 }
133 }
134 deviceContext->setConstantBuffer("TemporalParameters", parameterHandle);
135
136 deviceContext->setVertexBuffers(nullptr, 0, nullptr, nullptr);
137 deviceContext->setIndexBuffer(IndexBufferHandle::NoHandle);
138 deviceContext->setInputLayout(InputLayoutHandle::NoHandle);
139 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
140}
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 ITextures * getTextures()=0
Get a pointer to the texture 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.
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.
Provides texture management functionality.
Definition: ITextures.h:40
virtual SamplerStateHandle loadSamplerState(const SamplerState &state)=0
Load a sampler state object.
virtual void releaseSamplerState(SamplerStateHandle handle)=0
Release the sampler state with the given handle.
@ 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
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