VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
renderPassTAA.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4//
5#include "renderPassTAA.h"
6
7#include <cassert>
8#include <string>
9
12#include "scene/camera.h"
13
14namespace visutwin::canvas
15{
16 RenderPassTAA::RenderPassTAA(const std::shared_ptr<GraphicsDevice>& device, Texture* sourceTexture,
17 CameraComponent* cameraComponent)
18 : RenderPassShaderQuad(device), _sourceTexture(sourceTexture), _cameraComponent(cameraComponent)
19 {
20 setup();
21 }
22
23 void RenderPassTAA::setup()
24 {
25 const PixelFormat historyFormat = _sourceTexture ? _sourceTexture->format() : PixelFormat::PIXELFORMAT_RGBA8;
26 for (int i = 0; i < 2; ++i) {
27 TextureOptions textureOptions;
28 textureOptions.name = "TAA-History-" + std::to_string(i);
29 textureOptions.width = 4;
30 textureOptions.height = 4;
31 textureOptions.format = historyFormat;
32 textureOptions.mipmaps = false;
33 textureOptions.minFilter = FilterMode::FILTER_LINEAR;
34 textureOptions.magFilter = FilterMode::FILTER_LINEAR;
35 _historyTextures[i] = std::make_shared<Texture>(device().get(), textureOptions);
36 _historyTextures[i]->setAddressU(AddressMode::ADDRESS_CLAMP_TO_EDGE);
37 _historyTextures[i]->setAddressV(AddressMode::ADDRESS_CLAMP_TO_EDGE);
38
39 RenderTargetOptions targetOptions;
40 targetOptions.graphicsDevice = device().get();
41 targetOptions.colorBuffer = _historyTextures[i].get();
42 targetOptions.depth = false;
43 targetOptions.stencil = false;
44 targetOptions.name = "TaaHistoryTarget-" + std::to_string(i);
45 _historyRenderTargets[i] = device()->createRenderTarget(targetOptions);
46 }
47
48 _historyTexture = _historyTextures[0];
49
50 auto options = std::make_shared<RenderPassOptions>();
51 if (_sourceTexture) {
52 options->resizeSource = std::shared_ptr<Texture>(_sourceTexture, [](Texture*) {});
53 }
54 init(_historyRenderTargets[0], options);
55 }
56
58 {
59 if (!_sourceTexture) {
60 return;
61 }
62
63 // Keep RT sizing in sync if source changed.
64 auto options = std::make_shared<RenderPassOptions>();
65 options->resizeSource = std::shared_ptr<Texture>(_sourceTexture, [](Texture*) {});
66 setOptions(options);
67 }
68
70 {
71 const auto gd = device();
72 if (!gd || !_sourceTexture || !_cameraComponent || !_cameraComponent->camera()) {
73 return;
74 }
75
76 auto* camera = _cameraComponent->camera();
77 if (!camera) {
78 return;
79 }
80
81 Texture* sceneDepth = _depthTexture ? _depthTexture : gd->sceneDepthMap();
82 if (!sceneDepth || sceneDepth->width() != _sourceTexture->width() || sceneDepth->height() != _sourceTexture->height()) {
83 assert(false && "RenderPassTAA strict parity requires valid matching scene depth texture.");
84 return;
85 }
86
87 std::array<float, 4> cameraParams = {
88 camera->farClip() > 0.0f ? (1.0f / camera->farClip()) : 0.0f,
89 camera->farClip(),
90 camera->nearClip(),
91 camera->projection() == ProjectionType::Orthographic ? 1.0f : 0.0f
92 };
93
94 gd->executeTAAPass(_sourceTexture,
95 _historyTextures[1 - _historyIndex].get(),
96 sceneDepth,
97 camera->viewProjectionPrevious(),
98 camera->viewProjectionInverse(),
99 camera->jitters(),
100 cameraParams,
101 _highQuality,
102 _historyValid);
103 _historyValid = true;
104 }
105
110
111 std::shared_ptr<Texture> RenderPassTAA::update()
112 {
113 _historyIndex = 1 - _historyIndex;
114 _historyTexture = _historyTextures[_historyIndex];
115
116 auto options = std::make_shared<RenderPassOptions>();
117 if (_sourceTexture) {
118 options->resizeSource = std::shared_ptr<Texture>(_sourceTexture, [](Texture*) {});
119 }
120 init(_historyRenderTargets[_historyIndex], options);
121
122 return _historyTexture;
123 }
124}
virtual void init(const std::shared_ptr< RenderTarget > &renderTarget=nullptr, const std::shared_ptr< RenderPassOptions > &options=nullptr)
virtual void frameUpdate() const
std::shared_ptr< GraphicsDevice > device() const
Definition renderPass.h:124
void setOptions(const std::shared_ptr< RenderPassOptions > &value)
RenderPassShaderQuad(const std::shared_ptr< GraphicsDevice > &device)
void frameUpdate() const override
RenderPassTAA(const std::shared_ptr< GraphicsDevice > &device, Texture *sourceTexture, CameraComponent *cameraComponent)
std::shared_ptr< Texture > update()
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
uint32_t width() const
Definition texture.h:63
PixelFormat format() const
Definition texture.h:97
uint32_t height() const
Definition texture.h:65