1#include "MipLevelsTask.h"
3#include "Rendering/IGraphicsDevice.h"
4#include "Rendering/IBuffers.h"
5#include "Rendering/IContext.h"
6#include "Rendering/SamplerState.h"
7#include "Rendering/ITextures.h"
9#include "Renderer/RenderTarget.h"
10#include "Renderer/RenderTexture.h"
12#include "Resources/VertexFormats.h"
14#include "Utilities/Parsing.h"
20 struct MipLevelsParameter
22 glm::vec2 srcTexelSize;
23 glm::vec2 dstTexelSize;
28void Cogs::Core::MipLevelsTask::initialize(RenderTaskContext* context)
30 PostProcessTask::initialize(context);
33void Cogs::Core::MipLevelsTask::initialize(RenderTaskContext * context,
const RenderTaskDefinition& taskDefinition)
35 PostProcessTask::initialize(context, taskDefinition);
36 if (effect ==
nullptr || !
HandleIsValid(effect->handle))
return;
38 auto device = context->renderer->getDevice();
39 auto effects = device->getEffects();
40 auto buffers = device->getBuffers();
43 parameterBufferBinding = effects->getConstantBufferBinding(effect->handle,
"MipLevelsParameter");
46void Cogs::Core::MipLevelsTask::cleanup(RenderTaskContext * context)
48 auto device = context->renderer->getDevice();
49 auto buffers = device->getBuffers();
50 buffers->releaseBuffer(parameterHandle);
51 PostProcessTask::cleanup(context);
54void Cogs::Core::MipLevelsTask::apply(RenderTaskContext * context)
56 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING,
"MipLevelsTask", (std::string(
"MipLevelsTask<") + name +
">::apply").c_str());
58 if (effect ==
nullptr || !
HandleIsValid(effect->handle))
return;
60 auto device = context->renderer->getDevice();
61 auto deviceContext = device->getImmediateContext();
62 auto effects = device->getEffects();
64 auto renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
65 auto targetSource = renderTarget->textures[0];
67 std::string targetSourceKey;
68 for (
auto & p : properties) {
69 if (p.definition->type == ParsedDataType::Texture2D) {
70 auto inputSource = input.get(p.definition->value);
72 if (inputSource->type == RenderResourceType::RenderTexture) {
73 tex = inputSource->renderTexure;
75 else if (inputSource->type == RenderResourceType::RenderTarget) {
76 tex = inputSource->renderTarget->textures[0];
79 targetSourceKey = p.definition->key;
84 size_t maxLevel = std::max(
size_t(1u), renderTarget->mipLevelViews.size());
85 if (sizes.size() != maxLevel || sizes[0].x != renderTarget->width || sizes[0].y != renderTarget->height) {
86 sizes.resize(maxLevel);
87 sizes[0] = glm::ivec2(renderTarget->width, renderTarget->height);
88 for (uint32_t i = 1; i < maxLevel; i++) {
89 sizes[i] = glm::ivec2(std::max(1, sizes[i - 1].x / 2), std::max(1, sizes[i - 1].y / 2));
93 deviceContext->setEffect(effect->handle);
94 deviceContext->setDepthStencilState(context->states->noDepthStencilStateHandle);
95 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
97 for (
size_t i = 0; i < 4; ++i) {
99 deviceContext->setSamplerState(samplerStateBindings[i], samplerStates[i]);
103 setProperties(context, targetSource);
106 if (!targetSourceKey.empty()) {
114 targetSourceHandle = effects->getTextureBinding(effect->handle, targetSourceKey, texUnit);
115 deviceContext->setSamplerState(targetSourceKey +
"Sampler", texUnit, context->states->getSamplerState(ss));
125 size_t currentFirstLevel = std::min(maxLevel - 1u,
static_cast<size_t>(firstLevel));
126 size_t currentLastLevel = std::min(maxLevel - 1u,
static_cast<size_t>(lastLevel));
127 if (currentFirstLevel < currentLastLevel) {
128 for (
size_t l = currentFirstLevel; l < currentLastLevel; l++) {
129 const size_t srcLevel = l;
130 const size_t dstLevel = l + 1;
137 parameters->srcTexelSize = glm::vec2(1.f) / glm::vec2(sizes[srcLevel]);
138 parameters->dstTexelSize = glm::vec2(1.f) / glm::vec2(sizes[dstLevel]);
139 parameters->srcLevel =
static_cast<int>(srcLevel);
140 parameters->dstLevel =
static_cast<int>(dstLevel);
144 deviceContext->setConstantBuffer(parameterBufferBinding, parameterHandle);
149 deviceContext->setVertexBuffers(&context->states->fullScreenTriangle, 1);
151 deviceContext->setInputLayout(inputLayout);
155 textureViewDesc.
texture = targetSource->textureHandle;
156 textureViewDesc.
levelIndex =
static_cast<uint32_t
>(srcLevel);
160 size_t code = textureViewDesc.hash();
162 auto found = targetSource->views.find(code);
166 if (found == targetSource->views.end()) {
167 textureView = device->getTextures()->createTextureView(textureViewDesc);
168 targetSource->views.insert({ code, textureView });
170 textureView = found->second;
173 deviceContext->setTexture(targetSourceHandle, textureView);
175 deviceContext->setViewport(0, 0, (
float)sizes[dstLevel].x, (float)sizes[dstLevel].y);
179 }
else if (currentLastLevel < currentFirstLevel) {
180 for (
size_t l = currentFirstLevel; currentLastLevel < l; l--) {
181 const size_t srcLevel = l;
182 const size_t dstLevel = l - 1;
188 parameters->srcTexelSize = glm::vec2(1.f) / glm::vec2(sizes[srcLevel]);
189 parameters->dstTexelSize = glm::vec2(1.f) / glm::vec2(sizes[dstLevel]);
190 parameters->srcLevel =
static_cast<int>(srcLevel);
191 parameters->dstLevel =
static_cast<int>(dstLevel);
195 deviceContext->setConstantBuffer(parameterBufferBinding, parameterHandle);
199 textureViewDesc.
texture = targetSource->textureHandle;
200 textureViewDesc.
levelIndex =
static_cast<uint32_t
>(srcLevel);
203 size_t code = textureViewDesc.hash();
205 auto found = targetSource->views.find(code);
209 if (found == targetSource->views.end()) {
210 textureView = device->getTextures()->createTextureView(textureViewDesc);
211 targetSource->views.insert({ code, textureView });
213 textureView = found->second;
216 deviceContext->setTexture(targetSourceHandle, textureView);
218 deviceContext->setViewport(0, 0, (
float)sizes[dstLevel].x, (float)sizes[dstLevel].y);
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 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.