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"
26 const char * names[] = {
36void Cogs::Core::PostProcessTask::initialize(RenderTaskContext* context)
38 ProcessTask::initialize(context);
41void Cogs::Core::PostProcessTask::initialize(RenderTaskContext * context,
const RenderTaskDefinition& taskDefinition)
43 ProcessTask::initialize(context, taskDefinition);
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);
111 inputLayout = buffers->
loadInputLayout(&VertexFormats::Pos4f, 1, effect->handle);
112 for (
size_t i = 0; i < 4; ++i) {
115 samplerStateBindings[i] = binding;
116 samplerStates[i] = context->states->commonSamplerStates[i];
123 effectStatus = status;
131 if (scopeName.empty()) {
132 scopeName = std::string(
"PostProcessTask<") + name +
">::apply";
135 DynamicRenderInstrumentationScope(context->device->
getImmediateContext(), SCOPE_RENDERING,
"PostProcessTask", scopeName.c_str());
137 RenderTarget* renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
144 deviceContext->
setEffect(effect->handle);
148 info.renderTargetHandle = renderTarget->renderTargetHandle;
149 info.depthStencilHandle = renderTarget->depthTargetHandle;
152 info.loadOp[i] = clearColor ? LoadOp::Clear : LoadOp::Load;
154 info.storeOp[i] = StoreOp::Store;
156 if (clearToDefault) {
160 color = renderTarget->getClearColor();
162 info.clearValue[i][0] = color[0];
163 info.clearValue[i][1] = color[1];
164 info.clearValue[i][2] = color[2];
165 info.clearValue[i][3] = color[3];
167 info.depthLoadOp = clearDepth ? LoadOp::Clear : LoadOp::Load;
168 info.depthStoreOp = writeDepth ? StoreOp::Store : StoreOp::Discard;
171 deviceContext->beginRenderPass(info);
174 deviceContext->setRenderTarget(renderTarget->renderTargetHandle, renderTarget->depthTargetHandle);
176 if (clearToDefault) {
177 deviceContext->clearRenderTarget(glm::value_ptr(context->renderer->
getBackgroundColor()));
179 deviceContext->clearRenderTarget(glm::value_ptr(renderTarget->getClearColor()));
184 deviceContext->clearDepth(context->renderer->
getClearDepth());
189 if (viewportFromTarget) {
190 deviceContext->setViewport(0.0f, 0.0f,
static_cast<float>(renderTarget->width),
static_cast<float>(renderTarget->height));
194 deviceContext->setViewport(
195 context->cameraData->viewportOrigin.x, context->cameraData->viewportOrigin.y,
196 context->cameraData->viewportSize.x, context->cameraData->viewportSize.y
200 if (writeDepth && depthTest) {
201 deviceContext->setDepthStencilState(context->states->commonDepthStates[RenderStates::leqDepthStencilState]);
202 }
else if (writeDepth && !depthTest) {
203 deviceContext->setDepthStencilState(context->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
205 else if (!writeDepth && depthTest) {
206 deviceContext->setDepthStencilState(context->states->commonDepthStates[RenderStates::noWriteDepthStencilState]);
209 deviceContext->setDepthStencilState(context->states->commonDepthStates[RenderStates::noDepthStencilState]);
212 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
214 for (
size_t i = 0; i < 4; ++i) {
216 deviceContext->setSamplerState(samplerStateBindings[i], samplerStates[i]);
220 setProperties(context);
223 deviceContext->setBlendState(context->states->blendStates[
size_t(
BlendMode::Zero)].handle);
226 deviceContext->setVertexBuffers(&context->states->fullScreenTriangle, 1);
228 deviceContext->setInputLayout(inputLayout);
232 deviceContext->endRenderPass();
glm::vec4 getBackgroundColor() const override
Get the reference to the background color.
float getClearDepth() override
Get adjusted clear depth used to render.
virtual IEffects * getEffects()=0
Get a pointer to the effect management interface.
virtual ICapabilities * getCapabilities()=0
Get a pointer to the capability management interface used to query the graphics device capability fla...
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.
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.
ResourceStatus
Status of an asynchronously loaded resource, such as an effect or a pipeline.
@ Error
The resource failed to load.
@ Ready
The resource has loaded successfully and is ready for use.
@ TriangleList
List of triangles.
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.
static const Handle_t NoHandle
Represents a handle to nothing.
Provides buffer management functionality.
virtual InputLayoutHandle loadInputLayout(const VertexFormatHandle *vertexFormats, const size_t count, EffectHandle effectHandle)=0
Loads a new input layout to map vertex flow between vertex buffers with the given vertexFormats to ef...
virtual const GraphicsDeviceCapabilities & getDeviceCapabilities() const
Gets the device capabilities in a structure.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.
Provides effects and shader management functionality.
virtual SamplerStateBindingHandle getSamplerStateBinding(EffectHandle effectHandle, const StringView &name, const unsigned int slot)=0
Get a handle to a sampler state object binding, mapping how to bind the sampler state to the given ef...
virtual ResourceStatus checkEffect(EffectHandle effectHandle)=0
Check the load status of the effect with the given effectHandle.