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 else if (p.key ==
"source") {
57 else if (p.key ==
"useVariables") {
60 else if (p.key ==
"setVariables") {
63 else if (p.key ==
"useComponentFields") {
66 else if (p.key ==
"properties") {
69 else if (p.key ==
"groups" && p.values.size() == 3) {
72 else if (p.key ==
"options") {
76 LOG_WARNING(logger,
"Unknown post process task pipeline section \"%s\"", p.key.c_str());
83 switch (graphicsDeviceType)
86 success = Cogs::Core::buildPostProcessEffectES3(context, desc,
this);
89 success = buildPostProcessEffectWebGPU(context, desc,
this);
92 success = buildPostProcessEffect(context, desc,
this);
96 LOG_ERROR(logger,
"Failed to build post processing shader source");
100 effect = context->renderer->getEffectCache().loadEffect(context, desc);
105 auto * buffers = context->device->getBuffers();
106 inputLayout = buffers->loadInputLayout(&VertexFormats::Pos4f, 1, effect->handle);
108 for (
size_t i = 0; i < 4; ++i) {
109 auto binding = effects->getSamplerStateBinding(effect->handle, names[i], 0);
112 samplerStateBindings[i] = binding;
113 samplerStates[i] = context->states->commonSamplerStates[i];
120void Cogs::Core::PostProcessTask::apply(RenderTaskContext * context)
122 if (scopeName.empty()) {
123 scopeName = std::string(
"PostProcessTask<") + name +
">::apply";
126 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING,
"PostProcessTask", scopeName.c_str());
128 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
138 auto deviceContext = context->device->getImmediateContext();
139 deviceContext->setEffect(effect->handle);
141 if(context->device->getCapabilities()->getDeviceCapabilities().RenderPass){
143 info.renderTargetHandle = renderTarget->renderTargetHandle;
144 info.depthStencilHandle = renderTarget->depthTargetHandle;
147 info.loadOp[i] = clearColor ? LoadOp::Clear : LoadOp::Load;
149 info.storeOp[i] = StoreOp::Store;
151 if (clearToDefault) {
152 color = context->renderer->getBackgroundColor();
155 color = renderTarget->getClearColor();
157 info.clearValue[i][0] = color[0];
158 info.clearValue[i][1] = color[1];
159 info.clearValue[i][2] = color[2];
160 info.clearValue[i][3] = color[3];
162 info.depthLoadOp = clearDepth ? LoadOp::Clear : LoadOp::Load;
163 info.depthStoreOp = writeDepth ? StoreOp::Store : StoreOp::Discard;
164 info.depthClearValue = context->renderer->getClearDepth();
166 deviceContext->beginRenderPass(info);
169 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
171 if (clearToDefault) {
172 deviceContext->clearRenderTarget(glm::value_ptr(context->renderer->getBackgroundColor()));
174 deviceContext->clearRenderTarget(glm::value_ptr(renderTarget->getClearColor()));
179 deviceContext->clearDepth(context->renderer->getClearDepth());
184 if (viewportFromTarget) {
185 deviceContext->setViewport(0.0f, 0.0f,
static_cast<float>(renderTarget->width),
static_cast<float>(renderTarget->height));
189 deviceContext->setViewport(
190 context->cameraData->viewportOrigin.x, context->cameraData->viewportOrigin.y,
191 context->cameraData->viewportSize.x, context->cameraData->viewportSize.y
195 if (writeDepth && depthTest) {
196 deviceContext->setDepthStencilState(context->states->leqDepthStencilStateHandle);
197 }
else if (writeDepth && !depthTest) {
198 deviceContext->setDepthStencilState(context->states->noTestDepthStencilStateHandle);
200 else if (!writeDepth && depthTest) {
201 deviceContext->setDepthStencilState(context->states->noWriteDepthStencilStateHandle);
204 deviceContext->setDepthStencilState(context->states->noDepthStencilStateHandle);
207 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
209 for (
size_t i = 0; i < 4; ++i) {
211 deviceContext->setSamplerState(samplerStateBindings[i], samplerStates[i]);
215 setProperties(context);
218 deviceContext->setBlendState(context->states->blendStates[
size_t(
BlendMode::Zero)].handle);
221 deviceContext->setVertexBuffers(&context->states->fullScreenTriangle, 1);
223 deviceContext->setInputLayout(inputLayout);
226 if(context->device->getCapabilities()->getDeviceCapabilities().RenderPass){
227 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.