Cogs.Core
ResolveResourceTask.cpp
1#include "ResolveResourceTask.h"
2
3#include "Rendering/CommandGroupAnnotation.h"
4#include "Rendering/IGraphicsDevice.h"
5#include "Rendering/IContext.h"
6#include "Rendering/ITextures.h"
7
8#include "Renderer/Renderer.h"
9#include "Renderer/RenderTarget.h"
10#include "Renderer/RenderTexture.h"
11
12#include "Platform/Instrumentation.h"
13
14using namespace Cogs;
15
16bool Cogs::Core::ResolveResourceTask::setupEffect(RenderTaskContext * context)
17{
19 desc.vs = "Engine/FullscreenV3T2VS.hlsl";
20 desc.ps = "PostProcess/ResolveDepth.hlsl";
21 effect = context->renderer->getEffectCache().loadEffect(context, desc);
22 if(!HandleIsValid(effect->handle)) return false;
23 IEffects* effects = context->device->getEffects();
24 depthTextureBinding = effects->getTextureBinding(effect->handle, "depthTexture", 1);
25 return true;
26}
27void Cogs::Core::ResolveResourceTask::resolveDepthShader(RenderTaskContext * context,
28 RenderTexture * inputTexture,
29 RenderTexture * outputTexture)
30{
31 if(!effect && !setupEffect(context)){
32 return;
33 }
34
35 IGraphicsDevice* device = context->device;
36 IContext* deviceContext = device->getImmediateContext();
37
38 CommandGroupAnnotation commandGroup(deviceContext, "ResolveResourceTask::DepthShader");
39 if(!outputTexture->resolveTarget){
40 RenderTexture* renderTexture = outputTexture;
41 RenderTarget* resolveTarget = context->resources->createRenderTarget();
42 resolveTarget->setName(outputTexture->getName() + " RT");
43 resolveTarget->depth = renderTexture;
44 resolveTarget->width = renderTexture->description.width;
45 resolveTarget->height = renderTexture->description.height;
46 resolveTarget->samples = renderTexture->description.samples;
47 resolveTarget->update(context->renderer);
48 renderTexture->resolveTarget = resolveTarget;
49 }
50
51 assert(HandleIsValid(effect->handle));
52 deviceContext->setEffect(effect->handle);
53
54 RenderTarget* resolveTarget = outputTexture->resolveTarget;
55 assert(HandleIsValid(resolveTarget->depthTargetHandle));
56 deviceContext->setRenderTarget(RenderTargetHandle::NoHandle, resolveTarget->depthTargetHandle);
57 deviceContext->setViewport(0.0f, 0.0f, static_cast<float>(resolveTarget->width), static_cast<float>(resolveTarget->height));
58 deviceContext->clearDepth(context->renderer->getClearDepth());
59
60 deviceContext->setDepthStencilState(context->states->noTestDepthStencilStateHandle);
61 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
62 deviceContext->setBlendState(context->states->blendStates[size_t(BlendMode::Zero)].handle);
63
64 deviceContext->setTexture(depthTextureBinding, inputTexture->textureHandle);
65
66 deviceContext->setVertexBuffers(&context->states->fullScreenTriangle, 1);
69 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
70}
71
72void Cogs::Core::ResolveResourceTask::cleanup(RenderTaskContext * context)
73{
74 if(effect) context->renderer->getEffectCache().release(context, effect);
75}
76
77void Cogs::Core::ResolveResourceTask::apply(RenderTaskContext * context)
78{
79 RenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING, "ResolveResourceTask::apply");
80
81 RenderTexture* inputTexture = input.get(RenderResourceType::RenderTexture)->renderTexure;
82 RenderTexture* outputTexture = output.get(RenderResourceType::RenderTexture)->renderTexure;
83
84 IGraphicsDevice* device = context->device;
85 IContext* deviceContext = device->getImmediateContext();
86
87 if (!HandleIsValid(inputTexture->textureHandle)) return;
88
90 inputTexture->description.flags & TextureFlags::DepthBuffer){
91 resolveDepthShader(context, inputTexture, outputTexture);
92 }
93 else{
94 if (inputTexture->description.samples == 1) {
95 deviceContext->copyResource(outputTexture->textureHandle, inputTexture->textureHandle);
96 } else {
97 deviceContext->resolveResource(inputTexture->textureHandle, outputTexture->textureHandle);
98 }
99 }
100}
Represents a graphics device used to manage graphics resources and issue drawing commands.
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.
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.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
@ Direct3D11
Graphics device using the Direct3D 11 API.
@ TriangleList
List of triangles.
RAII-helper for pushCommandGroupAnnotation/pushCommandGroupAnnotation.
Contains an effect description used to load a single effect.
Definition: IEffects.h:55
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43
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 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 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.
Definition: IEffects.h:148
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.
@ DepthBuffer
The texture can be used as a depth target and have depth buffer values written into.
Definition: Flags.h:122