VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
renderPassDofBlur.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 "renderPassDofBlur.h"
6
7#include <algorithm>
8#include <cmath>
9
13
14namespace visutwin::canvas
15{
16 namespace
17 {
18 // Concentric sample kernel equivalent to Kernel.concentric usage in the upstream engine.
19 std::vector<float> makeConcentricKernel(const int rings, const int pointsPerRing)
20 {
21 std::vector<float> out;
22 out.reserve(static_cast<size_t>(rings * pointsPerRing * 2));
23 constexpr float twoPi = 6.28318530718f;
24 for (int r = 1; r <= rings; ++r) {
25 const float radius = static_cast<float>(r) / static_cast<float>(std::max(rings, 1));
26 const int points = std::max(pointsPerRing * r, 1);
27 for (int i = 0; i < points; ++i) {
28 const float angle = (static_cast<float>(i) / static_cast<float>(points)) * twoPi;
29 out.push_back(std::cos(angle) * radius);
30 out.push_back(std::sin(angle) * radius);
31 }
32 }
33 if (out.empty()) {
34 out.push_back(0.0f);
35 out.push_back(0.0f);
36 }
37 return out;
38 }
39 }
40
41 RenderPassDofBlur::RenderPassDofBlur(const std::shared_ptr<GraphicsDevice>& device, Texture* nearTexture,
42 Texture* farTexture, Texture* cocTexture)
43 : RenderPassShaderQuad(device), _nearTexture(nearTexture), _farTexture(farTexture), _cocTexture(cocTexture)
44 {
45 rebuildKernel();
46 }
47
48 void RenderPassDofBlur::setBlurRings(const int value)
49 {
50 const int clamped = std::max(value, 1);
51 if (_blurRings != clamped) {
52 _blurRings = clamped;
53 rebuildKernel();
54 }
55 }
56
58 {
59 const int clamped = std::max(value, 1);
60 if (_blurRingPoints != clamped) {
61 _blurRingPoints = clamped;
62 rebuildKernel();
63 }
64 }
65
67 {
68 if (_kernel.empty()) {
69 rebuildKernel();
70 }
71
72 const auto gd = device();
73 if (!gd) {
75 return;
76 }
77
78 const auto rt = renderTarget();
79 if (!rt || !rt->colorBuffer()) {
81 return;
82 }
83
84 const auto width = static_cast<float>(rt->colorBuffer()->width());
85 const auto height = static_cast<float>(rt->colorBuffer()->height());
86 if (width <= 0.0f || height <= 0.0f) {
88 return;
89 }
90
91 DofBlurPassParams params;
92 params.nearTexture = _nearTexture;
93 params.farTexture = _farTexture;
94 params.cocTexture = _cocTexture;
97 params.blurRings = _blurRings;
98 params.blurRingPoints = _blurRingPoints;
99 params.invResolutionX = 1.0f / width;
100 params.invResolutionY = 1.0f / height;
101 gd->executeDofBlurPass(params);
102 }
103
104 void RenderPassDofBlur::rebuildKernel()
105 {
106 _kernel = makeConcentricKernel(_blurRings, _blurRingPoints);
107 }
108}
109
RenderPassDofBlur(const std::shared_ptr< GraphicsDevice > &device, Texture *nearTexture, Texture *farTexture, Texture *cocTexture)
std::shared_ptr< RenderTarget > renderTarget() const
Definition renderPass.h:98
std::shared_ptr< GraphicsDevice > device() const
Definition renderPass.h:124
RenderPassShaderQuad(const std::shared_ptr< GraphicsDevice > &device)
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57