Cogs.Core
MipLevelsTask.cpp
1#include "MipLevelsTask.h"
2
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"
8
9#include "Renderer/RenderTarget.h"
10#include "Renderer/RenderTexture.h"
11
12#include "Resources/VertexFormats.h"
13
14#include "Utilities/Parsing.h"
15
16using namespace Cogs;
17
18namespace
19{
20 struct MipLevelsParameter
21 {
22 glm::vec2 srcTexelSize;
23 glm::vec2 dstTexelSize;
24 int srcLevel;
25 int dstLevel;
26 };
27}
28
29void Cogs::Core::MipLevelsTask::initialize(RenderTaskContext * context)
30{
31 PostProcessTask::initialize(context);
32 if (effect == nullptr || !HandleIsValid(effect->handle)) return;
33
34 auto device = context->renderer->getDevice();
35 auto effects = device->getEffects();
36 auto buffers = device->getBuffers();
37
38 parameterHandle = buffers->loadBuffer(nullptr, sizeof(MipLevelsParameter), Usage::Dynamic, AccessMode::Write, BindFlags::ConstantBuffer);
39 parameterBufferBinding = effects->getConstantBufferBinding(effect->handle, "MipLevelsParameter");
40}
41
42void Cogs::Core::MipLevelsTask::cleanup(RenderTaskContext * context)
43{
44 auto device = context->renderer->getDevice();
45 auto buffers = device->getBuffers();
46 buffers->releaseBuffer(parameterHandle);
47 PostProcessTask::cleanup(context);
48}
49
50void Cogs::Core::MipLevelsTask::apply(RenderTaskContext * context)
51{
52 DynamicRenderInstrumentationScope(context->device->getImmediateContext(), SCOPE_RENDERING, "MipLevelsTask", (std::string("MipLevelsTask<") + name + ">::apply").c_str());
53
54 if (effect == nullptr || !HandleIsValid(effect->handle)) return;
55
56 auto device = context->renderer->getDevice();
57 auto deviceContext = device->getImmediateContext();
58 auto effects = device->getEffects();
59
60 auto renderTarget = output.get(RenderResourceType::RenderTarget)->renderTarget;
61 auto targetSource = renderTarget->textures[0];
62
63 std::string targetSourceKey;
64 for (auto & p : properties) {
65 if (p.definition->type == ParsedDataType::Texture2D) {
66 auto inputSource = input.get(p.definition->value);
67 RenderTexture* tex = nullptr;
68 if (inputSource->type == RenderResourceType::RenderTexture) {
69 tex = inputSource->renderTexure;
70 }
71 else if (inputSource->type == RenderResourceType::RenderTarget) {
72 tex = inputSource->renderTarget->textures[0];
73 }
74 if (tex) {
75 targetSourceKey = p.definition->key;
76 }
77 }
78 }
79
80 size_t maxLevel = std::max(size_t(1u), renderTarget->mipLevelViews.size());
81 if (sizes.size() != maxLevel || sizes[0].x != renderTarget->width || sizes[0].y != renderTarget->height) {
82 sizes.resize(maxLevel);
83 sizes[0] = glm::ivec2(renderTarget->width, renderTarget->height);
84 for (uint32_t i = 1; i < maxLevel; i++) {
85 sizes[i] = glm::ivec2(std::max(1, sizes[i - 1].x / 2), std::max(1, sizes[i - 1].y / 2));
86 }
87 }
88
89
90
91 deviceContext->setEffect(effect->handle);
92 deviceContext->setDepthStencilState(context->states->noDepthStencilStateHandle);
93 deviceContext->setRasterizerState(context->states->defaultRasterizerStateHandle);
94
95
96 setProperties(context, targetSource);
97
99 if (!targetSourceKey.empty()) {
100 SamplerState ss;
101 std::memset(&ss, 0, sizeof(SamplerState));
106
107 targetSourceHandle = effects->getTextureBinding(effect->handle, targetSourceKey, texUnit);
108 deviceContext->setSamplerState(targetSourceKey + "Sampler", texUnit, context->states->getSamplerState(ss));
109
110 texUnit++;
111 }
112
113 if (!HandleIsValid(targetSourceHandle)) {
114 //TODO: Log warning
115 return;
116 }
117
118 deviceContext->setVertexBuffers(nullptr, 0, nullptr, nullptr);
119 deviceContext->setIndexBuffer(IndexBufferHandle::NoHandle);
120 deviceContext->setInputLayout(InputLayoutHandle::NoHandle);
121
122 size_t currentFirstLevel = std::min(maxLevel - 1u, static_cast<size_t>(firstLevel));
123 size_t currentLastLevel = std::min(maxLevel - 1u, static_cast<size_t>(lastLevel));
124 if (currentFirstLevel < currentLastLevel) {
125 for (size_t l = currentFirstLevel; l < currentLastLevel; l++) {
126 const size_t srcLevel = l;
127 const size_t dstLevel = l + 1;
128
129 {
130 MappedBuffer<MipLevelsParameter> parameters(deviceContext, parameterHandle, MapMode::WriteDiscard);
131
132 if (parameters) {
133 parameters->srcTexelSize = glm::vec2(1.f) / glm::vec2(sizes[srcLevel]);
134 parameters->dstTexelSize = glm::vec2(1.f) / glm::vec2(sizes[dstLevel]);
135 parameters->srcLevel = static_cast<int>(srcLevel);
136 parameters->dstLevel = static_cast<int>(dstLevel);
137 }
138 }
139
140 deviceContext->setConstantBuffer(parameterBufferBinding, parameterHandle);
141 deviceContext->setRenderTarget(renderTarget->mipLevelViews[dstLevel], DepthStencilHandle::NoHandle);
142
143 TextureViewDescription textureViewDesc = {};
144 textureViewDesc.texture = targetSource->textureHandle;
145 textureViewDesc.levelIndex = static_cast<uint32_t>(srcLevel);
146 textureViewDesc.numLevels = 1;
147
148 size_t code = textureViewDesc.hash();
149
150 auto found = targetSource->views.find(code);
151
152 Cogs::TextureViewHandle textureView;
153
154 if (found == targetSource->views.end()) {
155 textureView = device->getTextures()->createTextureView(textureViewDesc);
156 targetSource->views.insert({ code, textureView });
157 } else {
158 textureView = found->second;
159 }
160
161 deviceContext->setTexture(targetSourceHandle, textureView);
162
163 deviceContext->setViewport(0, 0, (float)sizes[dstLevel].x, (float)sizes[dstLevel].y);
164
165 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
166 }
167 } else if (currentLastLevel < currentFirstLevel) {
168 for (size_t l = currentFirstLevel; currentLastLevel < l; l--) {
169 const size_t srcLevel = l;
170 const size_t dstLevel = l - 1;
171
172 {
173 MappedBuffer<MipLevelsParameter> parameters(deviceContext, parameterHandle, MapMode::WriteDiscard);
174
175 if (parameters) {
176 parameters->srcTexelSize = glm::vec2(1.f) / glm::vec2(sizes[srcLevel]);
177 parameters->dstTexelSize = glm::vec2(1.f) / glm::vec2(sizes[dstLevel]);
178 parameters->srcLevel = static_cast<int>(srcLevel);
179 parameters->dstLevel = static_cast<int>(dstLevel);
180 }
181 }
182
183 deviceContext->setConstantBuffer(parameterBufferBinding, parameterHandle);
184 deviceContext->setRenderTarget(renderTarget->mipLevelViews[dstLevel], DepthStencilHandle::NoHandle);
185
186 TextureViewDescription textureViewDesc = {};
187 textureViewDesc.texture = targetSource->textureHandle;
188 textureViewDesc.levelIndex = static_cast<uint32_t>(srcLevel);
189 textureViewDesc.numLevels = 1;
190
191 size_t code = textureViewDesc.hash();
192
193 auto found = targetSource->views.find(code);
194
195 Cogs::TextureViewHandle textureView;
196
197 if (found == targetSource->views.end()) {
198 textureView = device->getTextures()->createTextureView(textureViewDesc);
199 targetSource->views.insert({ code, textureView });
200 } else {
201 textureView = found->second;
202 }
203
204 deviceContext->setTexture(targetSourceHandle, textureView);
205
206 deviceContext->setViewport(0, 0, (float)sizes[dstLevel].x, (float)sizes[dstLevel].y);
207
208 deviceContext->draw(PrimitiveType::TriangleList, 0, 3);
209 }
210 }
211}
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.
Definition: FieldSetter.h:23
@ Write
The buffer can be mapped and written to by the CPU after creation.
Definition: Flags.h:50
@ ConstantBuffer
The buffer can be bound as input to effects as a constant buffer.
Definition: Flags.h:72
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:77
@ WriteDiscard
Write access. When unmapping the graphics system will discard the old contents of the resource.
Definition: Flags.h:103
Provides RAII style mapping of a buffer resource.
Definition: IBuffers.h:160
@ TriangleList
List of triangles.
Definition: Common.h:116
Encapsulates state for texture sampling in a state object.
Definition: SamplerState.h:12
AddressMode addressModeW
Specifies the addressing mode along the W axis in texture coordinate space.
Definition: SamplerState.h:67
AddressMode addressModeS
Specifies the addressing mode along the S axis in texture coordinate space.
Definition: SamplerState.h:63
@ Clamp
Texture coordinates are clamped to the [0, 1] range.
Definition: SamplerState.h:17
@ MinMagMipLinear
Linear sampling for both minification and magnification.
Definition: SamplerState.h:35
FilterMode filter
Specifies the filter to use for texture sampling.
Definition: SamplerState.h:70
AddressMode addressModeT
Specifies the addressing mode along the T axis in texture coordinate space.
Definition: SamplerState.h:65
Describes how to fetch data from a texture in shaders.
Definition: ITextures.h:13
uint32_t numLevels
Number of mipmap levels available.
Definition: ITextures.h:23
uint32_t levelIndex
First mipmap level to fetch data from.
Definition: ITextures.h:21
TextureHandle texture
Texture.
Definition: ITextures.h:15
@ Dynamic
Buffer will be loaded and modified with some frequency.
Definition: Flags.h:30