VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
renderPassDof.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 "renderPassDof.h"
6
8
9namespace visutwin::canvas
10{
11 RenderPassDof::RenderPassDof(const std::shared_ptr<GraphicsDevice>& device, CameraComponent* cameraComponent,
12 Texture* sceneTexture, Texture* sceneTextureHalf, const bool highQualityValue, const bool nearBlurValue)
13 : RenderPass(device), _cameraComponent(cameraComponent), _sceneTexture(sceneTexture), _sceneTextureHalf(sceneTextureHalf),
14 highQuality(highQualityValue), nearBlur(nearBlurValue)
15 {
16 const PixelFormat sourceFormat = sceneTexture ? sceneTexture->format() : PixelFormat::PIXELFORMAT_RGBA8;
17 Texture* halfSource = sceneTextureHalf ? sceneTextureHalf : sceneTexture;
18
20 _cocTarget = createRenderTarget("CoCTexture", cocFormat, _cocTexture);
21
22 Texture* sourceTexture = sceneTexture;
23 if (_cocTarget && sourceTexture) {
24 _cocPass = std::make_shared<RenderPassCoC>(device, cameraComponent, nearBlur);
25 auto options = std::make_shared<RenderPassOptions>();
26 options->resizeSource = std::shared_ptr<Texture>(sourceTexture, [](Texture*) {});
27 _cocPass->init(_cocTarget, options);
28 const Color clearBlack(0.0f, 0.0f, 0.0f, 1.0f);
29 _cocPass->setClearColor(&clearBlack);
30 addBeforePass(_cocPass);
31 }
32
33 Texture* farSource = highQuality ? sceneTexture : halfSource;
34 if (farSource) {
35 _farTarget = createRenderTarget("FarDofTexture", farSource->format(), _farTexture);
36 if (_farTarget) {
37 RenderPassDownsample::Options downsampleOptions;
38 downsampleOptions.boxFilter = true;
39 downsampleOptions.premultiplyTexture = _cocTexture.get();
40 downsampleOptions.premultiplySrcChannel = 'r';
41 _farPass = std::make_shared<RenderPassDownsample>(device, farSource, downsampleOptions);
42 auto options = std::make_shared<RenderPassOptions>();
43 options->resizeSource = std::shared_ptr<Texture>(farSource, [](Texture*) {});
44 options->scaleX = 0.5f;
45 options->scaleY = 0.5f;
46 _farPass->init(_farTarget, options);
47 const Color clearBlack(0.0f, 0.0f, 0.0f, 1.0f);
48 _farPass->setClearColor(&clearBlack);
49 addBeforePass(_farPass);
50 }
51 }
52
53 if (halfSource) {
54 _blurTarget = createRenderTarget("DofBlurTexture", sourceFormat, _blurTexture);
55 if (_blurTarget && _farTarget) {
56 Texture* farTexture = _farTarget->colorBuffer();
57 _blurPass = std::make_shared<RenderPassDofBlur>(device, nearBlur ? halfSource : nullptr, farTexture, _cocTexture.get());
58 auto options = std::make_shared<RenderPassOptions>();
59 options->resizeSource = std::shared_ptr<Texture>(halfSource, [](Texture*) {});
60 options->scaleX = highQuality ? 2.0f : 0.5f;
61 options->scaleY = highQuality ? 2.0f : 0.5f;
62 _blurPass->init(_blurTarget, options);
63 const Color clearBlack(0.0f, 0.0f, 0.0f, 1.0f);
64 _blurPass->setClearColor(&clearBlack);
65 addBeforePass(_blurPass);
66 }
67 }
68 }
69
71 {
73
74 if (_cocPass) {
75 _cocPass->focusDistance = focusDistance;
76 _cocPass->focusRange = focusRange;
77 }
78
79 if (_blurPass) {
80 _blurPass->blurRadiusNear = blurRadius;
81 _blurPass->blurRadiusFar = blurRadius * (highQuality ? 1.0f : 0.5f);
82 _blurPass->setBlurRings(blurRings);
83 _blurPass->setBlurRingPoints(blurRingPoints);
84 }
85 }
86
87 std::shared_ptr<Texture> RenderPassDof::createTexture(const std::string& name, const PixelFormat format) const
88 {
89 TextureOptions textureOptions;
90 textureOptions.name = name;
91 textureOptions.width = 1;
92 textureOptions.height = 1;
93 textureOptions.format = format;
94 textureOptions.mipmaps = false;
95 textureOptions.minFilter = FilterMode::FILTER_LINEAR;
96 textureOptions.magFilter = FilterMode::FILTER_LINEAR;
97 auto texture = std::make_shared<Texture>(device().get(), textureOptions);
98 texture->setAddressU(AddressMode::ADDRESS_CLAMP_TO_EDGE);
99 texture->setAddressV(AddressMode::ADDRESS_CLAMP_TO_EDGE);
100 return texture;
101 }
102
103 std::shared_ptr<RenderTarget> RenderPassDof::createRenderTarget(const std::string& name, const PixelFormat format,
104 std::shared_ptr<Texture>& outColorTexture) const
105 {
106 outColorTexture = createTexture(name, format);
107 if (!outColorTexture) {
108 return nullptr;
109 }
110
111 RenderTargetOptions targetOptions;
112 targetOptions.graphicsDevice = device().get();
113 targetOptions.colorBuffer = outColorTexture.get();
114 targetOptions.depth = false;
115 targetOptions.stencil = false;
116 targetOptions.name = name;
117
118 return device()->createRenderTarget(targetOptions);
119 }
120}
void frameUpdate() const override
RenderPassDof(const std::shared_ptr< GraphicsDevice > &device, CameraComponent *cameraComponent, Texture *sceneTexture, Texture *sceneTextureHalf, bool highQuality, bool nearBlur)
virtual void frameUpdate() const
std::shared_ptr< GraphicsDevice > device() const
Definition renderPass.h:124
RenderPass(const std::shared_ptr< GraphicsDevice > &device)
Definition renderPass.h:66
void addBeforePass(const std::shared_ptr< RenderPass > &renderPass)
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
PixelFormat format() const
Definition texture.h:97
RGBA color with floating-point components in [0, 1].
Definition color.h:18