1#include "MipLevelsTask.h"
3#include "Platform/Instrumentation.h"
5#include "Rendering/IBuffers.h"
6#include "Rendering/ICapabilities.h"
7#include "Rendering/IContext.h"
8#include "Rendering/IGraphicsDevice.h"
9#include "Rendering/ITextures.h"
10#include "Rendering/SamplerState.h"
12#include "Renderer/Renderer.h"
13#include "Renderer/RenderTarget.h"
14#include "Renderer/RenderTexture.h"
16#include "Resources/VertexFormats.h"
18#include "Utilities/Parsing.h"
24 struct MipLevelsParameter
26 glm::vec2 srcTexelSize;
27 glm::vec2 dstTexelSize;
32void Cogs::Core::MipLevelsTask::initialize(RenderTaskContext* context)
34 PostProcessTask::initialize(context);
37void Cogs::Core::MipLevelsTask::initialize(RenderTaskContext * context,
const RenderTaskDefinition& taskDefinition)
39 PostProcessTask::initialize(context, taskDefinition);
40 if (effect ==
nullptr || !
HandleIsValid(effect->handle))
return;
42 auto device = context->renderer->getDevice();
43 auto effects = device->getEffects();
44 auto buffers = device->getBuffers();
47 parameterBufferBinding = effects->getConstantBufferBinding(effect->handle,
"MipLevelsParameter");
50void Cogs::Core::MipLevelsTask::cleanup(RenderTaskContext * context)
52 auto device = context->renderer->getDevice();
53 auto buffers = device->getBuffers();
54 buffers->releaseBuffer(parameterHandle);
55 PostProcessTask::cleanup(context);
58void Cogs::Core::MipLevelsTask::apply(RenderTaskContext * context)
60 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING,
"MipLevelsTask", (std::string(
"MipLevelsTask<") + name +
">::apply").c_str());
62 if (effect ==
nullptr || !
HandleIsValid(effect->handle))
return;
64 auto device = context->renderer->getDevice();
65 auto deviceContext = device->getImmediateContext();
66 auto effects = device->getEffects();
68 auto renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
69 auto targetSource = renderTarget->textures[0];
71 std::string targetSourceKey;
72 for (
auto & p : properties) {
73 if (p.definition->type == ParsedDataType::Texture2D) {
74 auto inputSource = input.get(p.definition->value);
76 if (inputSource->type == RenderResourceType::RenderTexture) {
77 tex = inputSource->renderTexure;
79 else if (inputSource->type == RenderResourceType::RenderTarget) {
80 tex = inputSource->renderTarget->textures[0];
83 targetSourceKey = p.definition->key;
88 size_t maxLevel = std::max(
size_t(1u), renderTarget->mipLevelViews.size());
89 if (sizes.size() != maxLevel || sizes[0].x != renderTarget->width || sizes[0].y != renderTarget->height) {
90 sizes.resize(maxLevel);
91 sizes[0] = glm::ivec2(renderTarget->width, renderTarget->height);
92 for (uint32_t i = 1; i < maxLevel; i++) {
93 sizes[i] = glm::ivec2(std::max(1, sizes[i - 1].x / 2), std::max(1, sizes[i - 1].y / 2));
98 if (!targetSourceKey.empty()) {
106 targetSourceHandle = effects->getTextureBinding(effect->handle, targetSourceKey, texUnit);
107 deviceContext->setSamplerState(targetSourceKey +
"Sampler", texUnit, context->states->getSamplerState(ss));
117 size_t currentFirstLevel = std::min(maxLevel - 1u,
static_cast<size_t>(firstLevel));
118 size_t currentLastLevel = std::min(maxLevel - 1u,
static_cast<size_t>(lastLevel));
119 if (currentFirstLevel < currentLastLevel) {
120 for (
size_t level = currentFirstLevel; level < currentLastLevel; level++) {
121 draw(context, renderTarget, targetSource, targetSourceHandle, level, level + 1);
123 }
else if (currentLastLevel < currentFirstLevel) {
124 for (
size_t level = currentFirstLevel; currentLastLevel < level; level--) {
125 draw(context, renderTarget, targetSource, targetSourceHandle, level, level - 1);
130void Cogs::Core::MipLevelsTask::draw(RenderTaskContext * context,
131 RenderTarget * renderTarget,
134 const size_t srcLevel,
const size_t dstLevel)
136 auto device = context->renderer->getDevice();
137 auto deviceContext = device->getImmediateContext();
139 if(device->getCapabilities()->getDeviceCapabilities().RenderPass){
141 info.renderTargetHandle = renderTarget->mipLevelViews[dstLevel];
145 info.loadOp[i] = clearColor ? LoadOp::Clear : LoadOp::Load;
146 info.storeOp[i] = StoreOp::Store;
148 if (clearToDefault) {
149 color = context->renderer->getBackgroundColor();
152 color = renderTarget->getClearColor();
154 info.clearValue[i][0] = color[0];
155 info.clearValue[i][1] = color[1];
156 info.clearValue[i][2] = color[2];
157 info.clearValue[i][3] = color[3];
159 info.depthLoadOp = LoadOp::Undefined;
160 info.depthStoreOp = StoreOp::Undefined;
161 info.depthClearValue = context->renderer->getClearDepth();
162 deviceContext->beginRenderPass(info);
168 deviceContext->setEffect(effect->handle);
169 deviceContext->setDepthStencilState(context->states->noDepthStencilStateHandle);
170 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
172 for (
size_t i = 0; i < 4; ++i) {
174 deviceContext->setSamplerState(samplerStateBindings[i], samplerStates[i]);
178 setProperties(context, targetSource);
184 parameters->srcTexelSize = glm::vec2(1.f) / glm::vec2(sizes[srcLevel]);
185 parameters->dstTexelSize = glm::vec2(1.f) / glm::vec2(sizes[dstLevel]);
186 parameters->srcLevel =
static_cast<int>(srcLevel);
187 parameters->dstLevel =
static_cast<int>(dstLevel);
191 deviceContext->setConstantBuffer(parameterBufferBinding, parameterHandle);
194 textureViewDesc.
texture = targetSource->textureHandle;
197 textureViewDesc.
levelIndex =
static_cast<uint32_t
>(srcLevel);
200 size_t code = textureViewDesc.hash();
202 auto found = targetSource->views.find(code);
205 if (found == targetSource->views.end()) {
206 textureView = device->getTextures()->createTextureView(textureViewDesc);
207 targetSource->views.insert({ code, textureView });
209 textureView = found->second;
212 deviceContext->setTexture(targetSourceHandle, textureView);
213 deviceContext->setViewport(0, 0, (
float)sizes[dstLevel].x, (float)sizes[dstLevel].y);
215 deviceContext->setVertexBuffers(&context->states->fullScreenTriangle, 1);
217 deviceContext->setInputLayout(inputLayout);
221 if(device->getCapabilities()->getDeviceCapabilities().RenderPass){
222 deviceContext->endRenderPass();
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
Contains all Cogs related functionality.
@ TriangleList
List of triangles.
@ Write
The buffer can be mapped and written to by the CPU after creation.
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
static const Handle_t NoHandle
Represents a handle to nothing.
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Provides RAII style mapping of a buffer resource.
Encapsulates state for texture sampling in a state object.
AddressMode addressModeW
Specifies the addressing mode along the W axis in texture coordinate space.
AddressMode addressModeS
Specifies the addressing mode along the S axis in texture coordinate space.
@ Clamp
Texture coordinates are clamped to the [0, 1] range.
@ MinMagMipLinear
Linear sampling for both minification and magnification.
FilterMode filter
Specifies the filter to use for texture sampling.
AddressMode addressModeT
Specifies the addressing mode along the T axis in texture coordinate space.
Describes how to fetch data from a texture in shaders.
uint32_t layerIndex
Index of the first layer (if array) to fetch from.
uint32_t numLayers
Number of array layers available.
uint32_t numLevels
Number of mipmap levels available.
uint32_t levelIndex
First mipmap level to fetch data from.
TextureHandle texture
Texture.
@ Dynamic
Buffer will be loaded and modified with some frequency.