Unravel Engine C++ Reference
Loading...
Searching...
No Matches
ssr_pass.h
Go to the documentation of this file.
1#pragma once
2
6#include <graphics/texture.h>
8#include "tonemapping_pass.h"
9#include "trace_resolution.h"
10#include <array>
11
12namespace unravel
13{
14
16{
17public:
18
21 {
24 {
25 float cone_angle_bias = 0.05f;
26 int max_mip_level = 6;
27 float blur_base_sigma = 1.0f;
28 float roughness_multiplier = 2.0f;
29 };
30
33 {
34 float history_strength = 0.9f; // 0 … 1 (was blend_factor)
35 float depth_threshold = 0.01f; // clip-space 0 … ~0.03
36 float roughness_sensitivity= 0.3f; // 0 … 1
37 float motion_scale_pixels = 120.0f; // Motion scale in pixels
38 float normal_dot_threshold = 0.95f; // Normal dot threshold for motion detection
39 int max_accum_frames = 8; // Maximum accumulation frames
40 };
41
42 int max_steps = 64;
43 int max_rays = 4;
44 float depth_tolerance = 0.1f;
45 float brightness = 1.0f;
48 float fade_in_start = 0.1f;
49 float fade_in_end = 0.2f;
53 // Cone tracing parameters
54 bool enable_cone_tracing = false;
56
57
59 // Temporal accumulation parameters
61
64 {
65 float depth_sigma = 0.02f;
66 float normal_power = 64.0f;
67 float luma_sigma = 1.0f;
68 int passes = 3;
69 };
70
73 };
74
80
90
92 auto init(rtti::context& ctx) -> bool;
93
95 auto run(gfx::render_view& rview, const run_params& params) -> gfx::frame_buffer::ptr;
96
99
102
105
108
111 const gfx::frame_buffer::ptr& ssr_curr,
112 const gfx::frame_buffer::ptr& g_buffer,
113 const camera* cam,
115
118 const gfx::frame_buffer::ptr& ssr_history,
119 const gfx::frame_buffer::ptr& ssr_curr,
120 const gfx::frame_buffer::ptr& probe_buffer,
121 const gfx::frame_buffer::ptr& g_buffer,
123
126 const gfx::texture::ptr& input_color,
127 const gfx::frame_buffer::ptr& g_buffer,
129
132 const gfx::frame_buffer::ptr& ssr_curr,
133 const gfx::frame_buffer::ptr& g_buffer,
135
136private:
138 auto create_or_update_output_fb(gfx::render_view& rview,
139 const gfx::frame_buffer::ptr& reference,
140 const gfx::frame_buffer::ptr& output)
142
144 auto create_or_update_ssr_curr_fb(gfx::render_view& rview,
145 const gfx::frame_buffer::ptr& reference,
147
149 auto create_or_update_ssr_history_tex(gfx::render_view& rview,
150 const gfx::frame_buffer::ptr& reference,
152
154 auto create_or_update_ssr_history_temp_fb(gfx::render_view& rview,
155 const gfx::frame_buffer::ptr& reference,
157
159 auto create_or_update_ssr_denoise_fb(gfx::render_view& rview,
160 const std::string& name,
161 const gfx::frame_buffer::ptr& reference,
163
164
165 // FidelityFX SSR Pixel Shader Program
166 struct fidelityfx_pixel_program : uniforms_cache
167 {
168 gpu_program::ptr program;
169 gfx::program::uniform_ptr u_ssr_params; // x: max_steps, y: depth_tolerance, z: max_rays, w: brightness
170 gfx::program::uniform_ptr u_hiz_params; // x: hiz_width, y: hiz_height, z: trace_scale_x, w: trace_scale_y
171 gfx::program::uniform_ptr u_fade_params; // x: fade_in_start, y: fade_in_end, z: roughness_depth_tolerance, w: facing_reflections_fading
172 gfx::program::uniform_ptr u_cone_params; // x: cone_angle_bias, y: max_mip_level, z: unused, w: unused
173 gfx::program::uniform_ptr u_prev_view_proj; // Previous frame view-projection matrix
174 gfx::program::uniform_ptr s_color; // Input color texture
175 gfx::program::uniform_ptr s_normal; // Normal buffer
176 gfx::program::uniform_ptr s_depth; // Depth buffer
177 gfx::program::uniform_ptr s_hiz; // Hi-Z buffer
178 gfx::program::uniform_ptr s_color_blurred; // Pre-blurred color buffer with mip chain
179
180 void cache_uniforms()
181 {
182 // Manual uniform creation for FidelityFX SSR using std::make_shared
183 cache_uniform(program.get(), u_ssr_params, "u_ssr_params", gfx::uniform_type::Vec4);
184 cache_uniform(program.get(), u_hiz_params, "u_hiz_params", gfx::uniform_type::Vec4);
185 cache_uniform(program.get(), u_fade_params, "u_fade_params", gfx::uniform_type::Vec4);
186 cache_uniform(program.get(), u_cone_params, "u_cone_params", gfx::uniform_type::Vec4);
187 cache_uniform(program.get(), u_prev_view_proj, "u_prev_view_proj", gfx::uniform_type::Mat4);
188 cache_uniform(program.get(), s_color, "s_color", gfx::uniform_type::Sampler);
189 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
190 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
191 cache_uniform(program.get(), s_hiz, "s_hiz", gfx::uniform_type::Sampler);
192 cache_uniform(program.get(), s_color_blurred, "s_color_blurred", gfx::uniform_type::Sampler);
193 }
194
195 auto is_valid() const -> bool
196 {
197 return program && program->is_valid();
198 }
199 } fidelityfx_pixel_program_;
200
201 // Temporal resolve program for SSR temporal accumulation
202 struct temporal_resolve_program : uniforms_cache
203 {
204 gpu_program::ptr program;
205 gfx::program::uniform_ptr u_temporal_params; // x: enable_temporal, y: history_strength, z: depth_threshold, w: roughness_sensitivity
206 gfx::program::uniform_ptr u_motion_params; // x: motion_scale_pixels, y: normal_dot_threshold, z: max_accum_frames, w: unused
207 gfx::program::uniform_ptr u_fade_params; // x: fade_in_start, y: fade_in_end, z: trace_scale_x, w: trace_scale_y
208 gfx::program::uniform_ptr u_prev_view_proj; // Previous frame view-projection matrix
209 gfx::program::uniform_ptr s_ssr_curr; // Current frame SSR result
210 gfx::program::uniform_ptr s_ssr_history; // Previous frame SSR history
211 gfx::program::uniform_ptr s_normal; // Normal buffer
212 gfx::program::uniform_ptr s_depth; // Depth buffer
213
214 void cache_uniforms()
215 {
216 cache_uniform(program.get(), u_temporal_params, "u_temporal_params", gfx::uniform_type::Vec4);
217 cache_uniform(program.get(), u_motion_params, "u_motion_params", gfx::uniform_type::Vec4);
218 cache_uniform(program.get(), u_fade_params, "u_fade_params", gfx::uniform_type::Vec4);
219 cache_uniform(program.get(), u_prev_view_proj, "u_prev_view_proj", gfx::uniform_type::Mat4);
220 cache_uniform(program.get(), s_ssr_curr, "s_ssr_curr", gfx::uniform_type::Sampler);
221 cache_uniform(program.get(), s_ssr_history, "s_ssr_history", gfx::uniform_type::Sampler);
222 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
223 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
224 }
225
226 auto is_valid() const -> bool
227 {
228 return program && program->is_valid();
229 }
230 } temporal_resolve_program_;
231
232 // Composite program for blending SSR with reflection probes
233 struct composite_program : uniforms_cache
234 {
235 gpu_program::ptr program;
236 gfx::program::uniform_ptr s_ssr_history; // Temporally filtered SSR result
237 gfx::program::uniform_ptr s_ssr_curr; // Current frame SSR result (for confidence)
238 gfx::program::uniform_ptr s_normal; // Normal buffer
239 gfx::program::uniform_ptr s_depth; // Depth buffer
240
241 void cache_uniforms()
242 {
243 cache_uniform(program.get(), s_ssr_history, "s_ssr_history", gfx::uniform_type::Sampler);
244 cache_uniform(program.get(), s_ssr_curr, "s_ssr_curr", gfx::uniform_type::Sampler);
245 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
246 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
247 }
248
249 auto is_valid() const -> bool
250 {
251 return program && program->is_valid();
252 }
253 } composite_program_;
254
255 // Unified blur compute program for cone tracing
256 struct blur_compute_program : uniforms_cache
257 {
258 gpu_program::ptr program;
259 gfx::program::uniform_ptr u_blur_params;
260 gfx::program::uniform_ptr s_normal; // Normal buffer for roughness sampling
261
262 void cache_uniforms()
263 {
264 cache_uniform(program.get(), u_blur_params, "u_blur_params", gfx::uniform_type::Vec4);
265 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
266 }
267
268 auto is_valid() const -> bool
269 {
270 return program && program->is_valid();
271 }
272 };
273 blur_compute_program blur_compute_program_;
274
275 // Spatial denoise compute program for edge-preserving a-trous wavelet filtering
276 struct spatial_denoise_compute_program : uniforms_cache
277 {
278 gpu_program::ptr program;
279 gfx::program::uniform_ptr u_denoise_params; // x: step_size, y: depth_sigma, z: normal_power, w: luma_sigma
280 gfx::program::uniform_ptr s_ssr_input; // Input SSR result (sampler)
281 gfx::program::uniform_ptr s_normal; // Normal buffer
282 gfx::program::uniform_ptr s_depth; // Depth buffer
283
284 void cache_uniforms()
285 {
286 cache_uniform(program.get(), u_denoise_params, "u_denoise_params", gfx::uniform_type::Vec4);
287 cache_uniform(program.get(), s_ssr_input, "s_ssr_input", gfx::uniform_type::Sampler);
288 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
289 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
290 }
291
292 auto is_valid() const -> bool
293 {
294 return program && program->is_valid();
295 }
296 };
297 spatial_denoise_compute_program spatial_denoise_compute_program_;
298
299};
300
301} // namespace unravel
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:62
std::shared_ptr< gpu_program > ptr
Definition gpu_program.h:19
auto run_ssr_trace(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Executes the SSR trace pass only. Returns SSR current frame buffer.
Definition ssr_pass.cpp:413
auto generate_blurred_color_buffer(gfx::render_view &rview, const gfx::texture::ptr &input_color, const gfx::frame_buffer::ptr &g_buffer, const fidelityfx_ssr_settings &settings) -> gfx::texture::ptr
Generates blurred color buffer with mip chain for cone tracing.
Definition ssr_pass.cpp:204
auto run_fidelityfx(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Executes the FidelityFX SSR pass. Returns the actual output framebuffer.
Definition ssr_pass.cpp:197
auto run_fidelityfx_three_pass(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Executes the three-pass SSR pipeline (trace, temporal resolve, composite)
Definition ssr_pass.cpp:372
auto run_composite(gfx::render_view &rview, const gfx::frame_buffer::ptr &ssr_history, const gfx::frame_buffer::ptr &ssr_curr, const gfx::frame_buffer::ptr &probe_buffer, const gfx::frame_buffer::ptr &g_buffer, const gfx::frame_buffer::ptr &output) -> gfx::frame_buffer::ptr
Executes the composite pass. Returns final blended output.
Definition ssr_pass.cpp:610
auto init(rtti::context &ctx) -> bool
Must be called once (after bgfx::init() and after asset_manager is registered in context).
Definition ssr_pass.cpp:12
auto run(gfx::render_view &rview, const run_params &params) -> gfx::frame_buffer::ptr
Executes the SSR pass. Returns the actual output framebuffer.
Definition ssr_pass.cpp:185
auto run_spatial_denoise(gfx::render_view &rview, const gfx::frame_buffer::ptr &ssr_curr, const gfx::frame_buffer::ptr &g_buffer, const fidelityfx_ssr_settings &settings) -> gfx::frame_buffer::ptr
Executes spatial denoise on SSR result before temporal resolve.
Definition ssr_pass.cpp:315
auto run_temporal_resolve(gfx::render_view &rview, const gfx::frame_buffer::ptr &ssr_curr, const gfx::frame_buffer::ptr &g_buffer, const camera *cam, const fidelityfx_ssr_settings &settings) -> gfx::frame_buffer::ptr
Executes the temporal resolve pass. Returns updated SSR history buffer.
Definition ssr_pass.cpp:518
void release_resources(gfx::render_view &rview)
Releases all GPU resources owned by this pass from the render_view.
Definition ssr_pass.cpp:656
std::string name
Definition hub.cpp:33
std::shared_ptr< uniform > uniform_ptr
Definition program.h:19
float cone_angle_bias
Controls cone growth rate (0.1 - 0.5)
Definition ssr_pass.h:25
float blur_base_sigma
Base blur sigma for mip generation (CPU-side only)
Definition ssr_pass.h:27
float roughness_multiplier
Multiplier for roughness-based blur (higher = more blur for rough surfaces)
Definition ssr_pass.h:28
Spatial denoise parameters (a-trous wavelet filter between trace and temporal resolve)
Definition ssr_pass.h:64
int passes
Number of a-trous filter passes (step = 1, 2, 4, ... 1<<i)
Definition ssr_pass.h:68
float luma_sigma
Luminance edge-stopping threshold (0.3 sharp – 2.0 smooth)
Definition ssr_pass.h:67
float normal_power
Normal edge-stopping exponent (16 loose – 128 strict)
Definition ssr_pass.h:66
float depth_sigma
Depth edge-stopping threshold (0.005 strict – 0.05 loose)
Definition ssr_pass.h:65
int max_steps
Maximum ray marching steps for hierarchical traversal.
Definition ssr_pass.h:42
float depth_tolerance
Depth tolerance for hit validation.
Definition ssr_pass.h:44
temporal_settings temporal
Temporal accumulation settings.
Definition ssr_pass.h:60
float roughness_depth_tolerance
Additional depth tolerance for rough surfaces.
Definition ssr_pass.h:47
bool enable_spatial_denoise
Enable spatial denoising before temporal resolve.
Definition ssr_pass.h:71
float brightness
Reflection brightness multiplier.
Definition ssr_pass.h:45
float facing_reflections_fading
Fade factor for camera-facing reflections.
Definition ssr_pass.h:46
cone_tracing_settings cone_tracing
Cone tracing specific settings.
Definition ssr_pass.h:55
bool enable_cone_tracing
Enable cone tracing for glossy reflections.
Definition ssr_pass.h:54
int max_rays
Maximum rays for rough surfaces (future: cone tracing)
Definition ssr_pass.h:43
bool enable_temporal_accumulation
Enable temporal accumulation.
Definition ssr_pass.h:58
float fade_in_start
Screen edge fade start.
Definition ssr_pass.h:48
spatial_denoise_settings spatial_denoise
Spatial denoise settings.
Definition ssr_pass.h:72
gfx::frame_buffer::ptr output
Optional output buffer.
Definition ssr_pass.h:83
gfx::texture::ptr previous_frame
Previous frame color for reflection sampling.
Definition ssr_pass.h:86
gfx::frame_buffer::ptr g_buffer
G-buffer containing normals.
Definition ssr_pass.h:84
gfx::texture::ptr hiz_buffer
Hi-Z buffer texture.
Definition ssr_pass.h:85
Combined SSR settings.
Definition ssr_pass.h:77
fidelityfx_ssr_settings fidelityfx
FidelityFX SSR settings.
Definition ssr_pass.h:78
Structure for caching uniforms.
void cache_uniform(gpu_program *program, gfx::program::uniform_ptr &uniform, const hpp::string_view &name, gfx::uniform_type type, uint16_t num=1)
Caches a uniform in the GPU program.