Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "renderer/PostprocManager.h"
21 :
22 : #include "graphics/Color.h"
23 : #include "graphics/LightEnv.h"
24 : #include "graphics/ShaderDefines.h"
25 : #include "graphics/ShaderManager.h"
26 : #include "graphics/ShaderTechnique.h"
27 : #include "lib/debug.h"
28 : #include "lib/file/vfs/vfs_path.h"
29 : #include "lib/file/vfs/vfs_util.h"
30 : #include "lib/lib.h"
31 : #include "lib/path.h"
32 : #include "ps/CLogger.h"
33 : #include "ps/CStrIntern.h"
34 : #include "ps/CStrInternStatic.h"
35 : #include "ps/ConfigDB.h"
36 : #include "ps/Filesystem.h"
37 : #include "ps/Profile.h"
38 : #include "ps/World.h"
39 : #include "renderer/Renderer.h"
40 : #include "renderer/RenderingOptions.h"
41 : #include "renderer/backend/Backend.h"
42 : #include "renderer/backend/Format.h"
43 : #include "renderer/backend/IDevice.h"
44 : #include "renderer/backend/IDeviceCommandContext.h"
45 : #include "renderer/backend/IShaderProgram.h"
46 : #include "renderer/backend/Sampler.h"
47 : #include "tools/atlas/GameInterface/GameLoop.h"
48 :
49 : #include <algorithm>
50 : #include <iterator>
51 : #include <sstream>
52 : #include <string>
53 : #include <string_view>
54 :
55 : namespace
56 : {
57 :
58 0 : void DrawFullscreenQuad(
59 : Renderer::Backend::IVertexInputLayout* vertexInputLayout,
60 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
61 : {
62 0 : float quadVerts[] =
63 : {
64 : 1.0f, 1.0f,
65 : -1.0f, 1.0f,
66 : -1.0f, -1.0f,
67 :
68 : -1.0f, -1.0f,
69 : 1.0f, -1.0f,
70 : 1.0f, 1.0f
71 : };
72 0 : const bool flip =
73 0 : deviceCommandContext->GetDevice()->GetBackend() == Renderer::Backend::Backend::VULKAN;
74 0 : const float bottomV = flip ? 1.0 : 0.0f;
75 0 : const float topV = flip ? 0.0f : 1.0f;
76 0 : float quadTex[] =
77 : {
78 : 1.0f, topV,
79 : 0.0f, topV,
80 : 0.0f, bottomV,
81 :
82 : 0.0f, bottomV,
83 : 1.0f, bottomV,
84 : 1.0f, topV
85 0 : };
86 :
87 0 : deviceCommandContext->SetVertexInputLayout(vertexInputLayout);
88 :
89 0 : deviceCommandContext->SetVertexBufferData(
90 : 0, quadVerts, std::size(quadVerts) * sizeof(quadVerts[0]));
91 0 : deviceCommandContext->SetVertexBufferData(
92 : 1, quadTex, std::size(quadTex) * sizeof(quadTex[0]));
93 :
94 0 : deviceCommandContext->Draw(0, 6);
95 0 : }
96 :
97 : } // anonymous namespace
98 :
99 10 : CPostprocManager::CPostprocManager(Renderer::Backend::IDevice* device)
100 10 : : m_Device(device), m_IsInitialized(false), m_PostProcEffect(L"default"),
101 10 : m_WhichBuffer(true), m_Sharpness(0.3f), m_UsingMultisampleBuffer(false),
102 10 : m_MultisampleCount(0)
103 : {
104 10 : }
105 :
106 30 : bool CPostprocManager::IsEnabled() const
107 : {
108 30 : const bool isDepthStencilFormatPresent =
109 30 : m_Device->GetPreferredDepthStencilFormat(
110 : Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT, true, true)
111 30 : != Renderer::Backend::Format::UNDEFINED;
112 30 : return
113 30 : g_RenderingOptions.GetPostProc() && isDepthStencilFormatPresent;
114 : }
115 :
116 0 : void CPostprocManager::Cleanup()
117 : {
118 0 : if (!m_IsInitialized) // Only cleanup if previously used
119 : return;
120 :
121 0 : m_CaptureFramebuffer.reset();
122 :
123 0 : m_PingFramebuffer.reset();
124 0 : m_PongFramebuffer.reset();
125 :
126 0 : m_PBROutputTexture.reset();
127 0 : m_ColorTex1.reset();
128 0 : m_ColorTex2.reset();
129 0 : m_DepthTex.reset();
130 :
131 0 : for (BlurScale& scale : m_BlurScales)
132 : {
133 0 : for (BlurScale::Step& step : scale.steps)
134 : {
135 0 : step.framebuffer.reset();
136 0 : step.texture.reset();
137 : }
138 : }
139 : }
140 :
141 0 : void CPostprocManager::Initialize(const uint32_t width, const uint32_t height)
142 : {
143 0 : if (m_IsInitialized)
144 0 : return;
145 :
146 0 : if (g_ConfigDB.Get("pbr", false))
147 0 : InitializePBR();
148 :
149 0 : const std::array<Renderer::Backend::SVertexAttributeFormat, 2> attributes{{
150 : {Renderer::Backend::VertexAttributeStream::POSITION,
151 : Renderer::Backend::Format::R32G32_SFLOAT, 0, sizeof(float) * 2,
152 : Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0},
153 : {Renderer::Backend::VertexAttributeStream::UV0,
154 : Renderer::Backend::Format::R32G32_SFLOAT, 0, sizeof(float) * 2,
155 : Renderer::Backend::VertexAttributeRate::PER_VERTEX, 1},
156 : }};
157 0 : m_VertexInputLayout = g_Renderer.GetVertexInputLayout(attributes);
158 :
159 0 : const uint32_t maxSamples = m_Device->GetCapabilities().maxSampleCount;
160 0 : const uint32_t possibleSampleCounts[] = {2, 4, 8, 16};
161 0 : std::copy_if(
162 : std::begin(possibleSampleCounts), std::end(possibleSampleCounts),
163 0 : std::back_inserter(m_AllowedSampleCounts),
164 0 : [maxSamples](const uint32_t sampleCount) { return sampleCount <= maxSamples; } );
165 :
166 : // The screen size starts out correct and then must be updated with
167 : // Create/DestroyViewSizeDependentObjects()
168 0 : RecalculateSize(width, height);
169 :
170 0 : RecreateBuffers();
171 0 : m_IsInitialized = true;
172 :
173 : // Once we have initialised the buffers, we can update the techniques.
174 0 : UpdateAntiAliasingTechnique();
175 0 : UpdateSharpeningTechnique();
176 0 : UpdateSharpnessFactor();
177 0 : SetUpscaleTechnique(g_ConfigDB.Get("renderer.upscale.technique", std::string{}));
178 :
179 : // This might happen after the map is loaded and the effect chosen
180 0 : SetPostEffect(m_PostProcEffect);
181 :
182 0 : if (m_Device->GetCapabilities().computeShaders)
183 0 : m_DownscaleComputeTech = g_Renderer.GetShaderManager().LoadEffect(CStrIntern("compute_downscale"));
184 : }
185 :
186 0 : void CPostprocManager::InitializePBR()
187 : {
188 0 : const bool PBRSupported{
189 0 : m_Device->IsFramebufferFormatSupported(Renderer::Backend::Format::R16G16B16A16_SFLOAT)};
190 0 : if (!PBRSupported)
191 : {
192 0 : m_PBREnabled = false;
193 0 : LOGWARNING("PBR is unsupported for the current renderer backend");
194 0 : return;
195 : }
196 :
197 0 : m_FramebufferColorFormat = Renderer::Backend::Format::R16G16B16A16_SFLOAT;
198 0 : const std::string framebufferFormatName{g_ConfigDB.Get("pbr.framebufferformat", std::string{})};
199 0 : if (framebufferFormatName == "rgba32")
200 : {
201 0 : if (m_Device->IsFramebufferFormatSupported(Renderer::Backend::Format::R32G32B32A32_SFLOAT))
202 0 : m_FramebufferColorFormat = Renderer::Backend::Format::R32G32B32A32_SFLOAT;
203 : else
204 0 : LOGWARNING("{} is unsupported", framebufferFormatName);
205 : }
206 0 : else if (framebufferFormatName == "r11g11b10")
207 : {
208 0 : if (m_Device->IsFramebufferFormatSupported(Renderer::Backend::Format::B10G11R11_UFLOAT))
209 0 : m_FramebufferColorFormat = Renderer::Backend::Format::B10G11R11_UFLOAT;
210 : else
211 0 : LOGWARNING("{} is unsupported", framebufferFormatName);
212 : }
213 :
214 0 : if (m_Device->GetCapabilities().computeShaders)
215 : {
216 0 : m_ResolvePBRComputeTech = g_Renderer.GetShaderManager().LoadEffect(str_compute_resolve_pbr);
217 0 : if (m_ResolvePBRComputeTech)
218 0 : m_PBREnabled = true;
219 : }
220 : else
221 : {
222 0 : m_ResolvePBRTech = g_Renderer.GetShaderManager().LoadEffect(str_resolve_pbr);
223 0 : if (m_ResolvePBRTech)
224 0 : m_PBREnabled = true;
225 : }
226 0 : }
227 :
228 0 : void CPostprocManager::Recreate()
229 : {
230 0 : DestroyViewSizeDependentObjects();
231 0 : CreateViewSizeDependentObjects(m_UnscaledWidth, m_UnscaledHeight);
232 0 : }
233 :
234 0 : void CPostprocManager::CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height)
235 : {
236 0 : RecalculateSize(width, height);
237 :
238 : // If the buffers were intialized, recreate them to the new size.
239 0 : if (m_IsInitialized)
240 0 : RecreateBuffers();
241 0 : }
242 :
243 0 : void CPostprocManager::DestroyViewSizeDependentObjects()
244 : {
245 0 : Cleanup();
246 0 : DestroyMultisampleBuffer();
247 0 : }
248 :
249 0 : void CPostprocManager::RecreateBuffers()
250 : {
251 0 : Cleanup();
252 :
253 0 : const auto defaultSampler{
254 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
255 : Renderer::Backend::Sampler::Filter::LINEAR,
256 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE)};
257 0 : const uint32_t colorTextureUsage{
258 : Renderer::Backend::ITexture::Usage::SAMPLED |
259 : Renderer::Backend::ITexture::Usage::COLOR_ATTACHMENT |
260 : Renderer::Backend::ITexture::Usage::TRANSFER_SRC |
261 : Renderer::Backend::ITexture::Usage::TRANSFER_DST};
262 :
263 0 : if (m_PBREnabled)
264 : {
265 0 : m_PBROutputTexture = m_Device->CreateTexture2D(
266 : "PostProcPBROutput", colorTextureUsage,
267 : m_FramebufferColorFormat,
268 0 : m_Width, m_Height, defaultSampler);
269 : }
270 :
271 : // Two fullscreen ping-pong textures.
272 0 : m_ColorTex1 = m_Device->CreateTexture2D(
273 0 : "PostProcColor1", colorTextureUsage | (m_PBREnabled ? Renderer::Backend::ITexture::Usage::STORAGE : 0),
274 : Renderer::Backend::Format::R8G8B8A8_UNORM,
275 0 : m_Width, m_Height, defaultSampler);
276 0 : m_ColorTex2 = m_Device->CreateTexture2D(
277 0 : "PostProcColor2", colorTextureUsage | (m_PBREnabled ? Renderer::Backend::ITexture::Usage::STORAGE : 0),
278 : Renderer::Backend::Format::R8G8B8A8_UNORM,
279 0 : m_Width, m_Height, defaultSampler);
280 :
281 0 : if (m_UnscaledWidth != m_Width && m_Device->GetCapabilities().computeShaders)
282 : {
283 0 : const uint32_t usage =
284 : Renderer::Backend::ITexture::Usage::TRANSFER_SRC |
285 : Renderer::Backend::ITexture::Usage::COLOR_ATTACHMENT |
286 : Renderer::Backend::ITexture::Usage::SAMPLED |
287 : Renderer::Backend::ITexture::Usage::STORAGE;
288 0 : m_UnscaledTexture1 = m_Device->CreateTexture2D(
289 : "PostProcUnscaledTexture1", usage,
290 : Renderer::Backend::Format::R8G8B8A8_UNORM,
291 : m_UnscaledWidth, m_UnscaledHeight,
292 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
293 : Renderer::Backend::Sampler::Filter::LINEAR,
294 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE));
295 :
296 0 : m_UnscaledTexture2 = m_Device->CreateTexture2D(
297 : "PostProcUnscaledTexture2", usage,
298 : Renderer::Backend::Format::R8G8B8A8_UNORM, m_UnscaledWidth, m_UnscaledHeight,
299 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
300 : Renderer::Backend::Sampler::Filter::LINEAR,
301 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE));
302 :
303 0 : Renderer::Backend::SColorAttachment colorAttachment{};
304 0 : colorAttachment.clearColor = CColor{0.0f, 0.0f, 0.0f, 0.0f};
305 0 : colorAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::LOAD;
306 0 : colorAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
307 :
308 0 : colorAttachment.texture = m_UnscaledTexture1.get();
309 0 : m_UnscaledFramebuffer1 = m_Device->CreateFramebuffer("PostprocUnscaledFramebuffer1",
310 0 : &colorAttachment, nullptr);
311 :
312 0 : colorAttachment.texture = m_UnscaledTexture2.get();
313 0 : m_UnscaledFramebuffer2 = m_Device->CreateFramebuffer("PostprocUnscaledFramebuffer2",
314 0 : &colorAttachment, nullptr);
315 : }
316 :
317 : // Textures for several blur sizes. It would be possible to reuse
318 : // m_BlurTex2b, thus avoiding the need for m_BlurTex4b and m_BlurTex8b, though given
319 : // that these are fairly small it's probably not worth complicating the coordinates passed
320 : // to the blur helper functions.
321 0 : uint32_t width = m_Width / 2, height = m_Height / 2;
322 0 : for (BlurScale& scale : m_BlurScales)
323 : {
324 0 : for (BlurScale::Step& step : scale.steps)
325 : {
326 0 : step.texture = m_Device->CreateTexture2D(
327 : "PostProcBlurStep", colorTextureUsage,
328 : Renderer::Backend::Format::R8G8B8A8_UNORM,
329 0 : width, height, defaultSampler);
330 0 : Renderer::Backend::SColorAttachment colorAttachment{};
331 0 : colorAttachment.texture = step.texture.get();
332 0 : colorAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::LOAD;
333 0 : colorAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
334 0 : colorAttachment.clearColor = CColor{0.0f, 0.0f, 0.0f, 0.0f};
335 0 : step.framebuffer = m_Device->CreateFramebuffer(
336 0 : "BlurScaleStepFramebuffer", &colorAttachment, nullptr);
337 : }
338 0 : width = std::max(1u, width / 2);
339 0 : height = std::max(1u, height / 2);
340 : }
341 :
342 : // Allocate the Depth/Stencil texture.
343 0 : m_DepthTex = m_Device->CreateTexture2D("PostProcDepthTexture",
344 : Renderer::Backend::ITexture::Usage::SAMPLED |
345 : Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT,
346 0 : m_Device->GetPreferredDepthStencilFormat(
347 : Renderer::Backend::ITexture::Usage::SAMPLED |
348 : Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT,
349 : true, true),
350 : m_Width, m_Height,
351 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
352 : Renderer::Backend::Sampler::Filter::LINEAR,
353 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE));
354 :
355 0 : RecreateCaptureFramebuffer();
356 :
357 : // Set up the framebuffers with some initial textures.
358 0 : Renderer::Backend::SColorAttachment colorAttachment{};
359 0 : colorAttachment.texture = m_ColorTex1.get();
360 0 : colorAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::LOAD;
361 0 : colorAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
362 0 : colorAttachment.clearColor = CColor{0.0f, 0.0f, 0.0f, 0.0f};
363 0 : m_PingFramebuffer = m_Device->CreateFramebuffer("PostprocPingFramebuffer",
364 0 : &colorAttachment, nullptr);
365 :
366 0 : colorAttachment.texture = m_ColorTex2.get();
367 0 : m_PongFramebuffer = m_Device->CreateFramebuffer("PostprocPongFramebuffer",
368 0 : &colorAttachment, nullptr);
369 :
370 0 : if (!m_PingFramebuffer || !m_PongFramebuffer)
371 : {
372 0 : LOGWARNING("Failed to create postproc framebuffers");
373 0 : g_RenderingOptions.SetPostProc(false);
374 : }
375 :
376 0 : if (m_UsingMultisampleBuffer)
377 : {
378 0 : DestroyMultisampleBuffer();
379 0 : CreateMultisampleBuffer();
380 : }
381 0 : }
382 :
383 0 : void CPostprocManager::RecreateCaptureFramebuffer()
384 : {
385 0 : Renderer::Backend::SColorAttachment colorAttachment{};
386 0 : colorAttachment.texture = m_PBREnabled ? m_PBROutputTexture.get() : m_ColorTex1.get();
387 0 : colorAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::DONT_CARE;
388 0 : colorAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
389 0 : colorAttachment.clearColor = CColor{0.0f, 0.0f, 0.0f, 0.0f};
390 :
391 0 : Renderer::Backend::SDepthStencilAttachment depthStencilAttachment{};
392 0 : depthStencilAttachment.texture = m_DepthTex.get();
393 0 : depthStencilAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::CLEAR;
394 0 : depthStencilAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
395 :
396 0 : m_CaptureFramebuffer = m_Device->CreateFramebuffer("PostprocCaptureFramebuffer",
397 0 : &colorAttachment, m_UsingMultisampleBuffer ? nullptr : &depthStencilAttachment);
398 :
399 0 : if (!m_CaptureFramebuffer)
400 : {
401 0 : LOGWARNING("Failed to create postproc capture framebuffer");
402 0 : g_RenderingOptions.SetPostProc(false);
403 : }
404 0 : }
405 :
406 0 : void CPostprocManager::ApplyBlurDownscale2x(
407 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
408 : Renderer::Backend::IFramebuffer* framebuffer,
409 : Renderer::Backend::ITexture* inTex, int inWidth, int inHeight)
410 : {
411 0 : deviceCommandContext->BeginFramebufferPass(framebuffer);
412 :
413 0 : Renderer::Backend::IDeviceCommandContext::Rect viewportRect{};
414 0 : viewportRect.width = inWidth / 2;
415 0 : viewportRect.height = inHeight / 2;
416 0 : deviceCommandContext->SetViewports(1, &viewportRect);
417 :
418 : // Get bloom shader with instructions to simply copy texels.
419 0 : CShaderDefines defines;
420 0 : defines.Add(str_BLOOM_NOP, str_1);
421 0 : CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_bloom, defines);
422 :
423 0 : deviceCommandContext->SetGraphicsPipelineState(
424 : tech->GetGraphicsPipelineState());
425 0 : deviceCommandContext->BeginPass();
426 0 : Renderer::Backend::IShaderProgram* shader = tech->GetShader();
427 :
428 0 : deviceCommandContext->SetTexture(
429 0 : shader->GetBindingSlot(str_renderedTex), inTex);
430 :
431 0 : DrawFullscreenQuad(m_VertexInputLayout, deviceCommandContext);
432 :
433 0 : deviceCommandContext->EndPass();
434 0 : deviceCommandContext->EndFramebufferPass();
435 0 : }
436 :
437 0 : void CPostprocManager::ApplyBlurGauss(
438 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
439 : Renderer::Backend::ITexture* inTex,
440 : Renderer::Backend::ITexture* tempTex,
441 : Renderer::Backend::IFramebuffer* tempFramebuffer,
442 : Renderer::Backend::IFramebuffer* outFramebuffer,
443 : int inWidth, int inHeight)
444 : {
445 0 : deviceCommandContext->BeginFramebufferPass(tempFramebuffer);
446 :
447 0 : Renderer::Backend::IDeviceCommandContext::Rect viewportRect{};
448 0 : viewportRect.width = inWidth;
449 0 : viewportRect.height = inHeight;
450 0 : deviceCommandContext->SetViewports(1, &viewportRect);
451 :
452 : // Get bloom shader, for a horizontal Gaussian blur pass.
453 0 : CShaderDefines defines2;
454 0 : defines2.Add(str_BLOOM_PASS_H, str_1);
455 0 : CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_bloom, defines2);
456 :
457 0 : deviceCommandContext->SetGraphicsPipelineState(
458 : tech->GetGraphicsPipelineState());
459 0 : deviceCommandContext->BeginPass();
460 0 : Renderer::Backend::IShaderProgram* shader = tech->GetShader();
461 0 : deviceCommandContext->SetTexture(
462 0 : shader->GetBindingSlot(str_renderedTex), inTex);
463 0 : deviceCommandContext->SetUniform(
464 0 : shader->GetBindingSlot(str_texSize), inWidth, inHeight);
465 :
466 0 : DrawFullscreenQuad(m_VertexInputLayout, deviceCommandContext);
467 :
468 0 : deviceCommandContext->EndPass();
469 0 : deviceCommandContext->EndFramebufferPass();
470 :
471 0 : deviceCommandContext->BeginFramebufferPass(outFramebuffer);
472 :
473 0 : deviceCommandContext->SetViewports(1, &viewportRect);
474 :
475 : // Get bloom shader, for a vertical Gaussian blur pass.
476 0 : CShaderDefines defines3;
477 0 : defines3.Add(str_BLOOM_PASS_V, str_1);
478 0 : tech = g_Renderer.GetShaderManager().LoadEffect(str_bloom, defines3);
479 0 : deviceCommandContext->SetGraphicsPipelineState(
480 : tech->GetGraphicsPipelineState());
481 :
482 0 : deviceCommandContext->BeginPass();
483 0 : shader = tech->GetShader();
484 :
485 : // Our input texture to the shader is the output of the horizontal pass.
486 0 : deviceCommandContext->SetTexture(
487 0 : shader->GetBindingSlot(str_renderedTex), tempTex);
488 0 : deviceCommandContext->SetUniform(
489 0 : shader->GetBindingSlot(str_texSize), inWidth, inHeight);
490 :
491 0 : DrawFullscreenQuad(m_VertexInputLayout, deviceCommandContext);
492 :
493 0 : deviceCommandContext->EndPass();
494 0 : deviceCommandContext->EndFramebufferPass();
495 0 : }
496 :
497 0 : void CPostprocManager::ApplyBlur(
498 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
499 : {
500 0 : uint32_t width = m_Width, height = m_Height;
501 0 : Renderer::Backend::ITexture* previousTexture =
502 0 : (m_WhichBuffer ? m_ColorTex1 : m_ColorTex2).get();
503 :
504 0 : for (BlurScale& scale : m_BlurScales)
505 : {
506 0 : ApplyBlurDownscale2x(deviceCommandContext, scale.steps[0].framebuffer.get(), previousTexture, width, height);
507 0 : width /= 2;
508 0 : height /= 2;
509 0 : ApplyBlurGauss(deviceCommandContext, scale.steps[0].texture.get(),
510 0 : scale.steps[1].texture.get(), scale.steps[1].framebuffer.get(),
511 0 : scale.steps[0].framebuffer.get(), width, height);
512 : }
513 0 : }
514 :
515 0 : Renderer::Backend::IFramebuffer* CPostprocManager::PrepareAndGetOutputFramebuffer()
516 : {
517 0 : ENSURE(m_IsInitialized);
518 :
519 : // Leaves m_PingFramebuffer selected for rendering; m_WhichBuffer stays true at this point.
520 :
521 0 : m_WhichBuffer = true;
522 :
523 0 : return m_UsingMultisampleBuffer ? m_MultisampleFramebuffer.get() : m_CaptureFramebuffer.get();
524 : }
525 :
526 0 : void CPostprocManager::UpscaleTextureByCompute(
527 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
528 : CShaderTechnique* shaderTechnique,
529 : Renderer::Backend::ITexture* source,
530 : Renderer::Backend::ITexture* destination)
531 : {
532 0 : Renderer::Backend::IShaderProgram* shaderProgram = shaderTechnique->GetShader();
533 :
534 0 : const std::array<float, 4> screenSize{{
535 0 : static_cast<float>(m_Width), static_cast<float>(m_Height),
536 0 : static_cast<float>(m_UnscaledWidth), static_cast<float>(m_UnscaledHeight)}};
537 :
538 0 : constexpr uint32_t threadGroupWorkRegionDim = 16;
539 0 : const uint32_t dispatchGroupCountX = DivideRoundUp(m_UnscaledWidth, threadGroupWorkRegionDim);
540 0 : const uint32_t dispatchGroupCountY = DivideRoundUp(m_UnscaledHeight, threadGroupWorkRegionDim);
541 :
542 0 : deviceCommandContext->BeginComputePass();
543 0 : deviceCommandContext->SetComputePipelineState(
544 : shaderTechnique->GetComputePipelineState());
545 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_screenSize), screenSize);
546 0 : deviceCommandContext->SetTexture(shaderProgram->GetBindingSlot(str_inTex), source);
547 0 : deviceCommandContext->SetStorageTexture(shaderProgram->GetBindingSlot(str_outTex), destination);
548 0 : deviceCommandContext->Dispatch(dispatchGroupCountX, dispatchGroupCountY, 1);
549 0 : deviceCommandContext->EndComputePass();
550 0 : }
551 :
552 0 : void CPostprocManager::UpscaleTextureByFullscreenQuad(
553 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
554 : CShaderTechnique* shaderTechnique,
555 : Renderer::Backend::ITexture* source,
556 : Renderer::Backend::IFramebuffer* destination)
557 : {
558 0 : Renderer::Backend::IShaderProgram* shaderProgram = shaderTechnique->GetShader();
559 :
560 0 : const std::array<float, 4> screenSize{{
561 0 : static_cast<float>(m_Width), static_cast<float>(m_Height),
562 0 : static_cast<float>(m_UnscaledWidth), static_cast<float>(m_UnscaledHeight)}};
563 :
564 0 : deviceCommandContext->BeginFramebufferPass(destination);
565 :
566 0 : Renderer::Backend::IDeviceCommandContext::Rect viewportRect{};
567 0 : viewportRect.width = destination->GetWidth();
568 0 : viewportRect.height = destination->GetHeight();
569 0 : deviceCommandContext->SetViewports(1, &viewportRect);
570 :
571 0 : deviceCommandContext->SetGraphicsPipelineState(
572 : shaderTechnique->GetGraphicsPipelineState());
573 0 : deviceCommandContext->BeginPass();
574 :
575 0 : deviceCommandContext->SetTexture(
576 0 : shaderProgram->GetBindingSlot(str_inTex), source);
577 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_screenSize), screenSize);
578 :
579 0 : DrawFullscreenQuad(m_VertexInputLayout, deviceCommandContext);
580 :
581 0 : deviceCommandContext->EndPass();
582 0 : deviceCommandContext->EndFramebufferPass();
583 0 : }
584 :
585 0 : void CPostprocManager::ApplySharpnessAfterScale(
586 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
587 : CShaderTechnique* shaderTechnique,
588 : Renderer::Backend::ITexture* source,
589 : Renderer::Backend::ITexture* destination)
590 : {
591 0 : Renderer::Backend::IShaderProgram* shaderProgram = shaderTechnique->GetShader();
592 :
593 : // Recommended sharpness for RCAS.
594 0 : constexpr float sharpness = 0.2f;
595 :
596 0 : const std::array<float, 4> screenSize{ {
597 0 : static_cast<float>(m_Width), static_cast<float>(m_Height),
598 0 : static_cast<float>(m_UnscaledWidth), static_cast<float>(m_UnscaledHeight)} };
599 :
600 0 : constexpr uint32_t threadGroupWorkRegionDim = 16;
601 0 : const uint32_t dispatchGroupCountX = DivideRoundUp(m_UnscaledWidth, threadGroupWorkRegionDim);
602 0 : const uint32_t dispatchGroupCountY = DivideRoundUp(m_UnscaledHeight, threadGroupWorkRegionDim);
603 :
604 0 : deviceCommandContext->BeginComputePass();
605 0 : deviceCommandContext->SetComputePipelineState(
606 : shaderTechnique->GetComputePipelineState());
607 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_sharpness), sharpness);
608 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_screenSize), screenSize);
609 0 : deviceCommandContext->SetTexture(shaderProgram->GetBindingSlot(str_inTex), source);
610 0 : deviceCommandContext->SetStorageTexture(
611 0 : shaderProgram->GetBindingSlot(str_outTex), destination);
612 0 : deviceCommandContext->Dispatch(dispatchGroupCountX, dispatchGroupCountY, 1);
613 0 : deviceCommandContext->EndComputePass();
614 0 : }
615 :
616 0 : void CPostprocManager::DownscaleTextureByCompute(
617 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
618 : CShaderTechnique* shaderTechnique,
619 : Renderer::Backend::ITexture* source,
620 : Renderer::Backend::ITexture* destination)
621 : {
622 0 : Renderer::Backend::IShaderProgram* shaderProgram = shaderTechnique->GetShader();
623 :
624 0 : const std::array<float, 4> screenSize{{
625 0 : static_cast<float>(m_Width), static_cast<float>(m_Height),
626 0 : static_cast<float>(m_UnscaledWidth), static_cast<float>(m_UnscaledHeight)}};
627 :
628 0 : constexpr uint32_t threadGroupWorkRegionDim = 8;
629 0 : const uint32_t dispatchGroupCountX = DivideRoundUp(m_UnscaledWidth, threadGroupWorkRegionDim);
630 0 : const uint32_t dispatchGroupCountY = DivideRoundUp(m_UnscaledHeight, threadGroupWorkRegionDim);
631 :
632 0 : deviceCommandContext->BeginComputePass();
633 0 : deviceCommandContext->SetComputePipelineState(
634 : shaderTechnique->GetComputePipelineState());
635 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_screenSize), screenSize);
636 0 : deviceCommandContext->SetTexture(shaderProgram->GetBindingSlot(str_inTex), source);
637 0 : deviceCommandContext->SetStorageTexture(shaderProgram->GetBindingSlot(str_outTex), destination);
638 0 : deviceCommandContext->Dispatch(dispatchGroupCountX, dispatchGroupCountY, 1);
639 0 : deviceCommandContext->EndComputePass();
640 0 : }
641 :
642 0 : void CPostprocManager::BlitOutputFramebuffer(
643 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
644 : Renderer::Backend::IFramebuffer* destination)
645 : {
646 0 : ENSURE(m_IsInitialized);
647 :
648 0 : GPU_SCOPED_LABEL(deviceCommandContext, "Copy postproc to backbuffer");
649 :
650 0 : Renderer::Backend::ITexture* previousTexture =
651 0 : (m_WhichBuffer ? m_ColorTex1 : m_ColorTex2).get();
652 :
653 0 : if (ShouldUpscale())
654 : {
655 0 : if (m_UpscaleComputeTech)
656 : {
657 0 : Renderer::Backend::ITexture* unscaledTexture = m_RCASComputeTech ? m_UnscaledTexture1.get() : m_UnscaledTexture2.get();
658 0 : UpscaleTextureByCompute(deviceCommandContext, m_UpscaleComputeTech.get(), previousTexture, unscaledTexture);
659 0 : if (m_RCASComputeTech)
660 0 : ApplySharpnessAfterScale(deviceCommandContext, m_RCASComputeTech.get(), m_UnscaledTexture1.get(), m_UnscaledTexture2.get());
661 :
662 0 : Renderer::Backend::IDeviceCommandContext::Rect sourceRegion{}, destinationRegion{};
663 0 : sourceRegion.width = m_UnscaledTexture2->GetWidth();
664 0 : sourceRegion.height = m_UnscaledTexture2->GetHeight();
665 0 : destinationRegion.width = destination->GetWidth();
666 0 : destinationRegion.height = destination->GetHeight();
667 0 : deviceCommandContext->BlitFramebuffer(
668 : m_UnscaledFramebuffer2.get(), destination, sourceRegion, destinationRegion,
669 : Renderer::Backend::Sampler::Filter::NEAREST);
670 : }
671 : else
672 : {
673 0 : UpscaleTextureByFullscreenQuad(deviceCommandContext, m_UpscaleTech.get(), previousTexture, destination);
674 : }
675 : }
676 0 : else if (ShouldDownscale())
677 : {
678 0 : Renderer::Backend::IDeviceCommandContext::Rect sourceRegion{};
679 0 : Renderer::Backend::Sampler::Filter samplerFilter{
680 : Renderer::Backend::Sampler::Filter::NEAREST};
681 0 : Renderer::Backend::IFramebuffer* source{nullptr};
682 :
683 0 : if (m_DownscaleComputeTech)
684 : {
685 0 : DownscaleTextureByCompute(deviceCommandContext, m_DownscaleComputeTech.get(), previousTexture, m_UnscaledTexture1.get());
686 :
687 0 : source = m_UnscaledFramebuffer1.get();
688 0 : sourceRegion.width = m_UnscaledTexture1->GetWidth();
689 0 : sourceRegion.height = m_UnscaledTexture1->GetHeight();
690 : }
691 : else
692 : {
693 0 : source = (m_WhichBuffer ? m_PingFramebuffer : m_PongFramebuffer).get();
694 0 : sourceRegion.width = source->GetWidth();
695 0 : sourceRegion.height = source->GetHeight();
696 0 : samplerFilter = Renderer::Backend::Sampler::Filter::LINEAR;
697 : }
698 :
699 0 : Renderer::Backend::IDeviceCommandContext::Rect destinationRegion{};
700 0 : destinationRegion.width = destination->GetWidth();
701 0 : destinationRegion.height = destination->GetHeight();
702 0 : deviceCommandContext->BlitFramebuffer(
703 : source, destination, sourceRegion, destinationRegion, samplerFilter);
704 : }
705 : else
706 : {
707 0 : Renderer::Backend::IFramebuffer* source =
708 0 : (m_WhichBuffer ? m_PingFramebuffer : m_PongFramebuffer).get();
709 :
710 : // We blit to the backbuffer from the previous active buffer.
711 0 : Renderer::Backend::IDeviceCommandContext::Rect region{};
712 0 : region.width = std::min(source->GetWidth(), destination->GetWidth());
713 0 : region.height = std::min(source->GetHeight(), destination->GetHeight());
714 0 : deviceCommandContext->BlitFramebuffer(
715 : source, destination, region, region,
716 : Renderer::Backend::Sampler::Filter::NEAREST);
717 : }
718 0 : }
719 :
720 0 : void CPostprocManager::ApplyEffect(
721 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
722 : const CShaderTechniquePtr& shaderTech, int pass)
723 : {
724 : // Select the other framebuffer for rendering.
725 0 : Renderer::Backend::IFramebuffer* framebuffer =
726 0 : (m_WhichBuffer ? m_PongFramebuffer : m_PingFramebuffer).get();
727 0 : deviceCommandContext->BeginFramebufferPass(framebuffer);
728 :
729 0 : Renderer::Backend::IDeviceCommandContext::Rect viewportRect{};
730 0 : viewportRect.width = framebuffer->GetWidth();
731 0 : viewportRect.height = framebuffer->GetHeight();
732 0 : deviceCommandContext->SetViewports(1, &viewportRect);
733 :
734 0 : deviceCommandContext->SetGraphicsPipelineState(
735 : shaderTech->GetGraphicsPipelineState(pass));
736 0 : deviceCommandContext->BeginPass();
737 0 : Renderer::Backend::IShaderProgram* shader = shaderTech->GetShader(pass);
738 :
739 : // Use the textures from the current framebuffer as input to the shader.
740 : // We also bind a bunch of other textures and parameters, but since
741 : // this only happens once per frame the overhead is negligible.
742 0 : deviceCommandContext->SetTexture(
743 0 : shader->GetBindingSlot(str_renderedTex),
744 0 : m_WhichBuffer ? m_ColorTex1.get() : m_ColorTex2.get());
745 0 : deviceCommandContext->SetTexture(
746 0 : shader->GetBindingSlot(str_depthTex), m_DepthTex.get());
747 :
748 0 : deviceCommandContext->SetTexture(
749 0 : shader->GetBindingSlot(str_blurTex2), m_BlurScales[0].steps[0].texture.get());
750 0 : deviceCommandContext->SetTexture(
751 0 : shader->GetBindingSlot(str_blurTex4), m_BlurScales[1].steps[0].texture.get());
752 0 : deviceCommandContext->SetTexture(
753 0 : shader->GetBindingSlot(str_blurTex8), m_BlurScales[2].steps[0].texture.get());
754 :
755 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_width), m_Width);
756 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_height), m_Height);
757 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_zNear), m_NearPlane);
758 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_zFar), m_FarPlane);
759 :
760 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_sharpness), m_Sharpness);
761 :
762 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_brightness), g_LightEnv.m_Brightness);
763 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_hdr), g_LightEnv.m_Contrast);
764 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_saturation), g_LightEnv.m_Saturation);
765 0 : deviceCommandContext->SetUniform(shader->GetBindingSlot(str_bloom), g_LightEnv.m_Bloom);
766 :
767 0 : DrawFullscreenQuad(m_VertexInputLayout, deviceCommandContext);
768 :
769 0 : deviceCommandContext->EndPass();
770 0 : deviceCommandContext->EndFramebufferPass();
771 :
772 0 : m_WhichBuffer = !m_WhichBuffer;
773 0 : }
774 :
775 0 : void CPostprocManager::ApplyPostproc(
776 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
777 : {
778 0 : ENSURE(m_IsInitialized);
779 :
780 0 : PROFILE3_GPU(deviceCommandContext, "Render postproc");
781 0 : GPU_SCOPED_LABEL(deviceCommandContext, "Render postproc");
782 :
783 0 : if (m_PBREnabled)
784 : {
785 0 : ResolvePBR(deviceCommandContext);
786 : }
787 :
788 0 : if (!m_PBREnabled && m_PostProcEffect != L"default")
789 : {
790 : // First render blur textures. Note that this only happens ONLY ONCE, before any effects are applied!
791 : // (This may need to change depending on future usage, however that will have a fps hit)
792 0 : ApplyBlur(deviceCommandContext);
793 0 : for (int pass = 0; pass < m_PostProcTech->GetNumPasses(); ++pass)
794 0 : ApplyEffect(deviceCommandContext, m_PostProcTech, pass);
795 : }
796 :
797 0 : if (m_AATech)
798 : {
799 0 : for (int pass = 0; pass < m_AATech->GetNumPasses(); ++pass)
800 0 : ApplyEffect(deviceCommandContext, m_AATech, pass);
801 : }
802 :
803 0 : if (m_SharpTech && !ShouldUpscale())
804 : {
805 0 : for (int pass = 0; pass < m_SharpTech->GetNumPasses(); ++pass)
806 0 : ApplyEffect(deviceCommandContext, m_SharpTech, pass);
807 : }
808 0 : }
809 :
810 :
811 : // Generate list of available effect-sets
812 0 : std::vector<CStrW> CPostprocManager::GetPostEffects()
813 : {
814 0 : std::vector<CStrW> effects;
815 :
816 0 : const VfsPath folder(L"shaders/effects/postproc/");
817 :
818 0 : VfsPaths pathnames;
819 0 : if (vfs::GetPathnames(g_VFS, folder, 0, pathnames) < 0)
820 0 : LOGERROR("Error finding Post effects in '{}'", folder.string8());
821 :
822 0 : for (const VfsPath& path : pathnames)
823 0 : if (path.Extension() == L".xml")
824 0 : effects.push_back(path.Basename().string());
825 :
826 : // Add the default "null" effect to the list.
827 0 : effects.push_back(L"default");
828 :
829 0 : sort(effects.begin(), effects.end());
830 :
831 0 : return effects;
832 0 : }
833 :
834 0 : void CPostprocManager::SetPostEffect(const CStrW& name)
835 : {
836 0 : if (m_IsInitialized)
837 : {
838 0 : if (name != L"default")
839 : {
840 0 : CStrW n = L"postproc/" + name;
841 0 : m_PostProcTech = g_Renderer.GetShaderManager().LoadEffect(CStrIntern(n.ToUTF8()));
842 0 : }
843 : }
844 :
845 0 : m_PostProcEffect = name;
846 0 : }
847 :
848 0 : void CPostprocManager::UpdateAntiAliasingTechnique()
849 : {
850 0 : if (!m_IsInitialized)
851 0 : return;
852 :
853 0 : const std::string newAAName{g_ConfigDB.Get("antialiasing", std::string{})};
854 0 : if (m_AAName == newAAName)
855 : return;
856 0 : m_AAName = newAAName;
857 0 : m_AATech.reset();
858 :
859 0 : if (m_UsingMultisampleBuffer)
860 : {
861 0 : m_UsingMultisampleBuffer = false;
862 0 : DestroyMultisampleBuffer();
863 : }
864 :
865 : // We have to hardcode names in the engine, because anti-aliasing
866 : // techinques strongly depend on the graphics pipeline.
867 : // We might use enums in future though.
868 0 : constexpr std::string_view msaaPrefix{"msaa"};
869 0 : if (m_AAName == str_fxaa.string())
870 : {
871 0 : m_AATech = g_Renderer.GetShaderManager().LoadEffect(str_fxaa);
872 : }
873 0 : else if (m_AAName.size() > msaaPrefix.size() &&
874 0 : std::string_view{m_AAName}.substr(0, msaaPrefix.size()) == msaaPrefix)
875 : {
876 : // We don't want to enable MSAA in Atlas, because it uses wxWidgets and its canvas.
877 0 : if (g_AtlasGameLoop && g_AtlasGameLoop->running)
878 0 : return;
879 0 : if (!m_Device->GetCapabilities().multisampling || m_AllowedSampleCounts.empty())
880 : {
881 0 : LOGWARNING("MSAA is unsupported.");
882 0 : return;
883 : }
884 0 : std::stringstream ss(m_AAName.substr(msaaPrefix.size()));
885 0 : ss >> m_MultisampleCount;
886 0 : if (std::find(std::begin(m_AllowedSampleCounts), std::end(m_AllowedSampleCounts), m_MultisampleCount) ==
887 0 : std::end(m_AllowedSampleCounts))
888 : {
889 0 : m_MultisampleCount = std::min(4u, m_Device->GetCapabilities().maxSampleCount);
890 0 : LOGWARNING("Wrong MSAA sample count: {}.", m_AAName.EscapeToPrintableASCII());
891 : }
892 0 : m_UsingMultisampleBuffer = true;
893 0 : CreateMultisampleBuffer();
894 0 : }
895 :
896 0 : RecreateCaptureFramebuffer();
897 0 : }
898 :
899 0 : void CPostprocManager::UpdateSharpeningTechnique()
900 : {
901 0 : if (!m_IsInitialized)
902 0 : return;
903 :
904 0 : const std::string newSharpName{g_ConfigDB.Get("sharpening", std::string{})};
905 0 : if (m_SharpName == newSharpName)
906 0 : return;
907 0 : m_SharpName = newSharpName;
908 0 : m_SharpTech.reset();
909 :
910 0 : if (m_SharpName == "cas")
911 : {
912 0 : m_SharpTech = g_Renderer.GetShaderManager().LoadEffect(CStrIntern(m_SharpName));
913 : }
914 0 : }
915 :
916 0 : void CPostprocManager::UpdateSharpnessFactor()
917 : {
918 0 : m_Sharpness = g_ConfigDB.Get("sharpness", m_Sharpness);
919 0 : }
920 :
921 0 : void CPostprocManager::SetUpscaleTechnique(const CStr& upscaleName)
922 : {
923 0 : m_UpscaleTech.reset();
924 0 : m_UpscaleComputeTech.reset();
925 0 : m_RCASComputeTech.reset();
926 0 : if (m_Device->GetCapabilities().computeShaders && upscaleName == "fsr")
927 : {
928 0 : m_UpscaleComputeTech = g_Renderer.GetShaderManager().LoadEffect(str_compute_upscale_fsr);
929 0 : m_RCASComputeTech = g_Renderer.GetShaderManager().LoadEffect(str_compute_rcas);
930 : }
931 0 : else if (upscaleName == "pixelated")
932 : {
933 0 : m_UpscaleTech = g_Renderer.GetShaderManager().LoadEffect(str_upscale_nearest);
934 : }
935 : else
936 : {
937 0 : m_UpscaleTech = g_Renderer.GetShaderManager().LoadEffect(str_upscale_bilinear);
938 : }
939 0 : }
940 :
941 0 : void CPostprocManager::SetDepthBufferClipPlanes(float nearPlane, float farPlane)
942 : {
943 0 : m_NearPlane = nearPlane;
944 0 : m_FarPlane = farPlane;
945 0 : }
946 :
947 0 : void CPostprocManager::CreateMultisampleBuffer()
948 : {
949 0 : m_MultisampleColorTex = m_Device->CreateTexture("PostProcColorMS",
950 : Renderer::Backend::ITexture::Type::TEXTURE_2D_MULTISAMPLE,
951 : Renderer::Backend::ITexture::Usage::COLOR_ATTACHMENT |
952 : Renderer::Backend::ITexture::Usage::TRANSFER_SRC,
953 : m_FramebufferColorFormat, m_Width, m_Height,
954 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
955 : Renderer::Backend::Sampler::Filter::LINEAR,
956 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE), 1, m_MultisampleCount);
957 :
958 : // Allocate the Depth/Stencil texture.
959 0 : m_MultisampleDepthTex = m_Device->CreateTexture("PostProcDepthMS",
960 : Renderer::Backend::ITexture::Type::TEXTURE_2D_MULTISAMPLE,
961 : Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT |
962 : Renderer::Backend::ITexture::Usage::TRANSFER_SRC,
963 0 : m_Device->GetPreferredDepthStencilFormat(
964 : Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT |
965 : Renderer::Backend::ITexture::Usage::TRANSFER_SRC,
966 : true, true),
967 : m_Width, m_Height,
968 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
969 : Renderer::Backend::Sampler::Filter::LINEAR,
970 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE), 1, m_MultisampleCount);
971 :
972 : // Set up the framebuffers with some initial textures.
973 0 : Renderer::Backend::SColorAttachment colorAttachment{};
974 0 : colorAttachment.texture = m_MultisampleColorTex.get();
975 0 : colorAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::DONT_CARE;
976 0 : colorAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
977 0 : colorAttachment.clearColor = CColor{0.0f, 0.0f, 0.0f, 0.0f};
978 :
979 0 : Renderer::Backend::SDepthStencilAttachment depthStencilAttachment{};
980 0 : depthStencilAttachment.texture = m_MultisampleDepthTex.get();
981 0 : depthStencilAttachment.loadOp = Renderer::Backend::AttachmentLoadOp::CLEAR;
982 0 : depthStencilAttachment.storeOp = Renderer::Backend::AttachmentStoreOp::STORE;
983 :
984 0 : m_MultisampleFramebuffer = m_Device->CreateFramebuffer(
985 0 : "PostprocMultisampleFramebuffer", &colorAttachment, &depthStencilAttachment);
986 :
987 0 : if (!m_MultisampleFramebuffer)
988 : {
989 0 : LOGERROR("Failed to create postproc multisample framebuffer");
990 0 : m_UsingMultisampleBuffer = false;
991 0 : DestroyMultisampleBuffer();
992 : }
993 0 : }
994 :
995 0 : void CPostprocManager::DestroyMultisampleBuffer()
996 : {
997 0 : m_MultisampleFramebuffer.reset();
998 0 : m_MultisampleColorTex.reset();
999 0 : m_MultisampleDepthTex.reset();
1000 0 : }
1001 :
1002 0 : bool CPostprocManager::IsMultisampleEnabled() const
1003 : {
1004 0 : return m_UsingMultisampleBuffer;
1005 : }
1006 :
1007 0 : void CPostprocManager::ResolveMultisampleFramebuffer(
1008 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
1009 : {
1010 0 : if (!m_UsingMultisampleBuffer)
1011 0 : return;
1012 :
1013 0 : GPU_SCOPED_LABEL(deviceCommandContext, "Resolve postproc multisample");
1014 0 : deviceCommandContext->ResolveFramebuffer(
1015 : m_MultisampleFramebuffer.get(),
1016 0 : m_PBREnabled ? m_CaptureFramebuffer.get() : m_PingFramebuffer.get());
1017 0 : }
1018 :
1019 0 : void CPostprocManager::ResolvePBR(
1020 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
1021 : {
1022 0 : ENSURE(m_PBREnabled);
1023 :
1024 0 : GPU_SCOPED_LABEL(deviceCommandContext, "Resolve PBR");
1025 :
1026 0 : const std::array<float, 4> screenSize{{
1027 0 : static_cast<float>(m_Width), static_cast<float>(m_Height),
1028 0 : 1.0f / static_cast<float>(m_Width), 1.0f / static_cast<float>(m_Height)}};
1029 : // Mapping [0.0, 1.0] to [-2.0, 2.0].
1030 0 : const float exposure{
1031 0 : std::exp2f((g_RenderingOptions.GetPBRBrightness() - 0.5f) * 4.0f)};
1032 :
1033 0 : Renderer::Backend::ITexture* source{m_PBROutputTexture.get()};
1034 :
1035 0 : if (m_ResolvePBRComputeTech)
1036 : {
1037 0 : Renderer::Backend::IShaderProgram* shaderProgram{m_ResolvePBRComputeTech->GetShader()};
1038 :
1039 0 : Renderer::Backend::ITexture* destination{m_ColorTex1.get()};
1040 :
1041 0 : constexpr uint32_t threadGroupWorkRegionDim{8};
1042 0 : const uint32_t dispatchGroupCountX{DivideRoundUp(m_Width, threadGroupWorkRegionDim)};
1043 0 : const uint32_t dispatchGroupCountY{DivideRoundUp(m_Height, threadGroupWorkRegionDim)};
1044 :
1045 0 : deviceCommandContext->BeginComputePass();
1046 0 : deviceCommandContext->SetComputePipelineState(
1047 0 : m_ResolvePBRComputeTech->GetComputePipelineState());
1048 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_screenSize), screenSize);
1049 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_exposure), exposure);
1050 0 : deviceCommandContext->SetTexture(shaderProgram->GetBindingSlot(str_inTex), source);
1051 0 : deviceCommandContext->SetStorageTexture(shaderProgram->GetBindingSlot(str_outTex), destination);
1052 0 : deviceCommandContext->Dispatch(dispatchGroupCountX, dispatchGroupCountY, 1);
1053 0 : deviceCommandContext->EndComputePass();
1054 : }
1055 : else
1056 : {
1057 0 : ENSURE(m_ResolvePBRTech);
1058 0 : Renderer::Backend::IShaderProgram* shaderProgram{m_ResolvePBRTech->GetShader()};
1059 :
1060 0 : Renderer::Backend::IFramebuffer* framebuffer{m_PingFramebuffer.get()};
1061 0 : deviceCommandContext->BeginFramebufferPass(framebuffer);
1062 :
1063 0 : Renderer::Backend::IDeviceCommandContext::Rect viewportRect{};
1064 0 : viewportRect.width = framebuffer->GetWidth();
1065 0 : viewportRect.height = framebuffer->GetHeight();
1066 0 : deviceCommandContext->SetViewports(1, &viewportRect);
1067 :
1068 0 : deviceCommandContext->SetGraphicsPipelineState(
1069 0 : m_ResolvePBRTech->GetGraphicsPipelineState());
1070 0 : deviceCommandContext->BeginPass();
1071 :
1072 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_screenSize), screenSize);
1073 0 : deviceCommandContext->SetUniform(shaderProgram->GetBindingSlot(str_exposure), exposure);
1074 0 : deviceCommandContext->SetTexture(shaderProgram->GetBindingSlot(str_inTex), source);
1075 :
1076 0 : DrawFullscreenQuad(m_VertexInputLayout, deviceCommandContext);
1077 :
1078 0 : deviceCommandContext->EndPass();
1079 0 : deviceCommandContext->EndFramebufferPass();
1080 : }
1081 0 : }
1082 :
1083 0 : void CPostprocManager::RecalculateSize(const uint32_t width, const uint32_t height)
1084 : {
1085 0 : m_Scale = g_ConfigDB.Get("renderer.scale", m_Scale);
1086 0 : if (m_Scale < 0.25f || m_Scale > 2.0f)
1087 : {
1088 0 : LOGWARNING("Invalid renderer scale: {:0.2f}", m_Scale);
1089 0 : m_Scale = 1.0f;
1090 : }
1091 0 : m_UnscaledWidth = width;
1092 0 : m_UnscaledHeight = height;
1093 0 : m_Width = m_UnscaledWidth * m_Scale;
1094 0 : m_Height = m_UnscaledHeight * m_Scale;
1095 0 : }
1096 :
1097 0 : bool CPostprocManager::ShouldUpscale() const
1098 : {
1099 0 : return m_Width < m_UnscaledWidth;
1100 : }
1101 :
1102 0 : bool CPostprocManager::ShouldDownscale() const
1103 : {
1104 0 : return m_Width > m_UnscaledWidth;
1105 : }
|