1#include "PostProcessTask.h"
2#include "Resources/VertexFormats.h"
5#include "Renderer/Renderer.h"
6#include "Renderer/RenderTarget.h"
7#include "Systems/Core/CameraSystem.h"
9#include "Rendering/IBuffers.h"
10#include "Rendering/ICapabilities.h"
11#include "Rendering/IContext.h"
12#include "Rendering/IGraphicsDevice.h"
14#include "Resources/ShaderBuilderPostProcess.h"
16#include <glm/gtc/type_ptr.hpp>
18#include "Foundation/Logging/Logger.h"
24 const char * names[] = {
34void Cogs::Core::PostProcessTask::initialize(RenderTaskContext* context)
36 ProcessTask::initialize(context);
39void Cogs::Core::PostProcessTask::initialize(RenderTaskContext * context,
const RenderTaskDefinition& taskDefinition)
41 ProcessTask::initialize(context, taskDefinition);
43 auto effects = context->device->getEffects();
46 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
48 for (
auto & p : effectParameter.values) {
49 if (p.key ==
"definitions") {
50 for (
auto & d : p.values) {
51 desc.definitions.push_back({ d.key, d.value });
54 if (p.key ==
"source") {
62 switch (graphicsDeviceType)
65 success = Cogs::Core::buildPostProcessEffectES3(context, desc,
this);
68 success = buildPostProcessEffectWebGPU(context, desc,
this);
71 success = buildPostProcessEffect(context, desc,
this);
75 LOG_ERROR(logger,
"Failed to build post processing shader source");
79 effect = context->renderer->getEffectCache().loadEffect(context, desc);
84 auto * buffers = context->device->getBuffers();
85 inputLayout = buffers->loadInputLayout(&VertexFormats::Pos4f, 1, effect->handle);
87 for (
size_t i = 0; i < 4; ++i) {
88 auto binding = effects->getSamplerStateBinding(effect->handle, names[i], 0);
91 samplerStateBindings[i] = binding;
92 samplerStates[i] = context->states->commonSamplerStates[i];
99void Cogs::Core::PostProcessTask::apply(RenderTaskContext * context)
101 if (scopeName.empty()) {
102 scopeName = std::string(
"PostProcessTask<") + name +
">::apply";
105 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING,
"PostProcessTask", scopeName.c_str());
107 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
117 auto deviceContext = context->device->getImmediateContext();
118 deviceContext->setEffect(effect->handle);
120 if(context->device->getCapabilities()->getDeviceCapabilities().RenderPass){
122 info.renderTargetHandle = renderTarget->renderTargetHandle;
123 info.depthStencilHandle = renderTarget->depthTargetHandle;
126 info.loadOp[i] = clearColor ? LoadOp::Clear : LoadOp::Load;
128 info.storeOp[i] = StoreOp::Store;
130 if (clearToDefault) {
131 color = context->renderer->getBackgroundColor();
134 color = renderTarget->getClearColor();
136 info.clearValue[i][0] = color[0];
137 info.clearValue[i][1] = color[1];
138 info.clearValue[i][2] = color[2];
139 info.clearValue[i][3] = color[3];
141 info.depthLoadOp = clearDepth ? LoadOp::Clear : LoadOp::Load;
142 info.depthStoreOp = writeDepth ? StoreOp::Store : StoreOp::Discard;
143 info.depthClearValue = context->renderer->getClearDepth();
145 deviceContext->beginRenderPass(info);
148 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
150 if (clearToDefault) {
151 deviceContext->clearRenderTarget(glm::value_ptr(context->renderer->getBackgroundColor()));
153 deviceContext->clearRenderTarget(glm::value_ptr(renderTarget->getClearColor()));
158 deviceContext->clearDepth(context->renderer->getClearDepth());
163 if (viewportFromTarget) {
164 deviceContext->setViewport(0.0f, 0.0f,
static_cast<float>(renderTarget->width),
static_cast<float>(renderTarget->height));
168 deviceContext->setViewport(
169 context->cameraData->viewportOrigin.x, context->cameraData->viewportOrigin.y,
170 context->cameraData->viewportSize.x, context->cameraData->viewportSize.y
174 if (writeDepth && depthTest) {
175 deviceContext->setDepthStencilState(context->states->leqDepthStencilStateHandle);
176 }
else if (writeDepth && !depthTest) {
177 deviceContext->setDepthStencilState(context->states->noTestDepthStencilStateHandle);
179 else if (!writeDepth && depthTest) {
180 deviceContext->setDepthStencilState(context->states->noWriteDepthStencilStateHandle);
183 deviceContext->setDepthStencilState(context->states->noDepthStencilStateHandle);
186 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
188 for (
size_t i = 0; i < 4; ++i) {
190 deviceContext->setSamplerState(samplerStateBindings[i], samplerStates[i]);
194 setProperties(context);
197 deviceContext->setBlendState(context->states->blendStates[
size_t(
BlendMode::Zero)].handle);
200 deviceContext->setVertexBuffers(&context->states->fullScreenTriangle, 1);
202 deviceContext->setInputLayout(inputLayout);
205 if(context->device->getCapabilities()->getDeviceCapabilities().RenderPass){
206 deviceContext->endRenderPass();
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.
@ Zero
Disable all color writes.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
GraphicsDeviceType
Contains types of graphics devices that may be supported.
@ OpenGLES30
Graphics device using the OpenGLES 3.0 API.
@ WebGPU
Graphics device using the WebGPU API Backend.
@ TriangleList
List of triangles.
Contains an effect description used to load a single effect.
static const Handle_t NoHandle
Represents a handle to nothing.