1#include "ResolveResourceTask.h"
3#include "ProcessTask.h"
5#include "Rendering/CommandGroupAnnotation.h"
6#include "Rendering/IBuffers.h"
7#include "Rendering/ICapabilities.h"
8#include "Rendering/IContext.h"
9#include "Rendering/IEffects.h"
10#include "Rendering/IGraphicsDevice.h"
11#include "Rendering/ITextures.h"
13#include "Resources/VertexFormats.h"
15#include "Renderer/Renderer.h"
16#include "Renderer/RenderTarget.h"
17#include "Renderer/RenderTexture.h"
19#include "Platform/Instrumentation.h"
21#include "Foundation/Logging/Logger.h"
32Cogs::ResourceStatus Cogs::Core::ResolveResourceTask::setupColorEffect(RenderTaskContext * context)
36 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
37 desc.ps =
"PostProcess/ResolveColor.hlsl";
38 colorEffect = context->renderer->getEffectCache().loadEffect(context, desc);
41 IEffects* effects = context->device->getEffects();
45 IBuffers* buffers = context->device->getBuffers();
46 colorInputLayout = buffers->
loadInputLayout(&VertexFormats::Pos4f, 1, colorEffect->handle);
47 colorTextureBinding = effects->
getTextureBinding(colorEffect->handle,
"colorTexture", 0);
51 LOG_ERROR(logger,
"Invalid resolve color effect handle.");
54 colorEffectStatus = status;
55 return colorEffectStatus;
57Cogs::ResourceStatus Cogs::Core::ResolveResourceTask::setupDepthEffect(RenderTaskContext * context)
61 desc.vs =
"Engine/FullscreenV3T2VS.hlsl";
62 desc.ps =
"PostProcess/ResolveDepth.hlsl";
63 depthEffect = context->renderer->getEffectCache().loadEffect(context, desc);
66 IEffects* effects = context->device->getEffects();
70 IBuffers* buffers = context->device->getBuffers();
71 depthInputLayout = buffers->
loadInputLayout(&VertexFormats::Pos4f, 1, depthEffect->handle);
72 depthTextureBinding = effects->
getTextureBinding(depthEffect->handle,
"depthTexture", 0);
76 LOG_ERROR(logger,
"Invalid resolve depth effect handle.");
79 depthEffectStatus = status;
80 return depthEffectStatus;
82void Cogs::Core::ResolveResourceTask::resolveColorShader(RenderTaskContext * context,
92 if(!outputTexture->resolveTarget){
94 RenderTarget* resolveTarget = context->resources->createRenderTarget();
95 resolveTarget->setName(outputTexture->getName() +
" RT");
96 resolveTarget->textures.push_back(renderTexture);
97 resolveTarget->width = renderTexture->description.width;
98 resolveTarget->height = renderTexture->description.height;
99 resolveTarget->samples = renderTexture->description.samples;
100 resolveTarget->update(context->renderer);
101 renderTexture->resolveTarget = resolveTarget;
104 deviceContext->
setEffect(colorEffect->handle);
106 RenderTarget* resolveTarget = outputTexture->resolveTarget;
110 info.renderTargetHandle = resolveTarget->renderTargetHandle;
112 info.loadOp[0] = LoadOp::Clear;
113 info.storeOp[0] = StoreOp::Store;
114 info.depthLoadOp = LoadOp::Undefined;
115 info.depthStoreOp = StoreOp::Undefined;
116 info.depthClearValue = context->renderer->getClearDepth();
117 info.depthReadOnly =
true;
126 deviceContext->
setViewport(0.0f, 0.0f,
static_cast<float>(resolveTarget->width),
static_cast<float>(resolveTarget->height));
128 deviceContext->
setDepthStencilState(context->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
132 deviceContext->
setTexture(colorTextureBinding, inputTexture->textureHandle);
143void Cogs::Core::ResolveResourceTask::resolveDepthShader(RenderTaskContext * context,
153 if(!outputTexture->resolveTarget){
155 RenderTarget* resolveTarget = context->resources->createRenderTarget();
156 resolveTarget->setName(outputTexture->getName() +
" RT");
157 resolveTarget->depth = renderTexture;
158 resolveTarget->width = renderTexture->description.width;
159 resolveTarget->height = renderTexture->description.height;
160 resolveTarget->samples = renderTexture->description.samples;
161 resolveTarget->update(context->renderer);
162 renderTexture->resolveTarget = resolveTarget;
165 deviceContext->
setEffect(depthEffect->handle);
167 RenderTarget* resolveTarget = outputTexture->resolveTarget;
172 info.depthStencilHandle = resolveTarget->depthTargetHandle;
173 info.depthLoadOp = LoadOp::Clear;
174 info.depthStoreOp = StoreOp::Store;
175 info.depthClearValue = context->renderer->getClearDepth();
176 info.depthReadOnly =
false;
181 deviceContext->
clearDepth(context->renderer->getClearDepth());
184 deviceContext->
setViewport(0.0f, 0.0f,
static_cast<float>(resolveTarget->width),
static_cast<float>(resolveTarget->height));
186 deviceContext->
setDepthStencilState(context->states->commonDepthStates[RenderStates::noTestDepthStencilState]);
190 deviceContext->
setTexture(depthTextureBinding, inputTexture->textureHandle);
202void Cogs::Core::ResolveResourceTask::cleanup(RenderTaskContext * context)
204 if(colorEffect) context->renderer->getEffectCache().release(context, colorEffect);
205 if(depthEffect) context->renderer->getEffectCache().release(context, depthEffect);
210 RenderTexture* inputTexture = input.get(RenderResourceType::RenderTexture)->renderTexure;
214 action = ResolveAction::None;
220 action = ResolveAction::DepthShader;
221 return setupDepthEffect(context);
224 if (inputTexture->description.samples == 1) {
225 action = ResolveAction::Copy;
230 action = ResolveAction::ColorShader;
231 return setupColorEffect(context);
234 action = ResolveAction::Resolve;
240 RenderInstrumentationScope(context->device->
getImmediateContext(), SCOPE_RENDERING,
"ResolveResourceTask::apply");
242 RenderTexture* inputTexture = input.get(RenderResourceType::RenderTexture)->renderTexure;
243 RenderTexture* outputTexture = output.get(RenderResourceType::RenderTexture)->renderTexure;
248 case ResolveAction::None:
250 case ResolveAction::Copy:
251 deviceContext->copyResource(outputTexture->textureHandle, inputTexture->textureHandle);
253 case ResolveAction::Resolve:
254 deviceContext->
resolveResource(inputTexture->textureHandle, outputTexture->textureHandle);
256 case ResolveAction::ColorShader:
257 resolveColorShader(context, inputTexture, outputTexture);
259 case ResolveAction::DepthShader:
260 resolveDepthShader(context, inputTexture, outputTexture);
Cogs::ResourceStatus checkReady(RenderTaskContext *context) override
Check if the task is ready to be applied, e.g.
Represents a graphics device used to manage graphics resources and issue drawing commands.
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 GraphicsDeviceType getType() const
Get the type of the graphics device.
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.
@ None
No blending enabled for opaque shapes, defaults to Blend for transparent shapes.
@ Zero
Disable all color writes.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
@ 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.
RAII-helper for pushCommandGroupAnnotation/pushCommandGroupAnnotation.
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.
Represents a graphics device context which can receive rendering commands.
virtual void setTexture(const StringView &name, unsigned int unit, TextureHandle textureHandle)=0
Sets the texture slot given by unit with the given name to contain the given texture.
virtual void setRasterizerState(const RasterizerStateHandle handle)=0
Set the current rasterizer state.
virtual void setBlendState(const BlendStateHandle handle, const float *constant=nullptr)=0
Set the current blend state.
virtual void setInputLayout(const InputLayoutHandle inputLayoutHandle)=0
Sets the current input layout.
virtual void clearRenderTarget(const float *value)=0
Clear the currently set render target to the given value (4 component floating point RGBA).
virtual void endRenderPass()=0
End a render pass.
virtual void setIndexBuffer(IndexBufferHandle bufferHandle, uint32_t stride=4, uint32_t offset=0)=0
Sets the current index buffer.
virtual void setDepthStencilState(const DepthStencilStateHandle handle)=0
Set the current depth stencil state.
virtual void resolveResource(TextureHandle source, TextureHandle destination)=0
Resolves the given source resource target into the given destination texture.
virtual void clearDepth(const float depth=1.0f)=0
Clear the currently set depth/stencil target to the given depth.
virtual void setViewport(const float x, const float y, const float width, const float height)=0
Sets the current viewport to the given location and dimensions.
virtual void draw(PrimitiveType primitiveType, const size_t startVertex, const size_t numVertexes)=0
Draws non-indexed, non-instanced primitives.
virtual void beginRenderPass(const RenderPassInfo &info)=0
Begin a render pass.
virtual void setVertexBuffers(const VertexBufferHandle *vertexBufferHandles, const size_t count, const uint32_t *strides, const uint32_t *offsets)=0
Sets the current vertex buffers.
virtual void setEffect(EffectHandle handle)=0
Set the current effect.
virtual void setRenderTarget(const RenderTargetHandle handle, const DepthStencilHandle depthStencilHandle)=0
Sets the current render target and an associated depth stencil target.
Provides effects and shader management functionality.
virtual TextureBindingHandle getTextureBinding(EffectHandle effectHandle, const StringView &name, const unsigned int slot)=0
Get a handle to a texture object binding, mapping how to bind textures to the given effect.
virtual ResourceStatus checkEffect(EffectHandle effectHandle)=0
Check the load status of the effect with the given effectHandle.
@ DepthBuffer
The texture can be used as a depth target and have depth buffer values written into.