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 <array>
10
11namespace unravel
12{
13
15{
16public:
17
20 {
23 {
24 float cone_angle_bias = 0.05f;
25 int max_mip_level = 6;
26 float blur_base_sigma = 1.0f;
27 float roughness_multiplier = 2.0f;
28 };
29
32 {
33 float history_strength = 0.9f; // 0 … 1 (was blend_factor)
34 float depth_threshold = 0.01f; // clip-space 0 … ~0.03
35 float roughness_sensitivity= 0.3f; // 0 … 1
36 float motion_scale_pixels = 120.0f; // Motion scale in pixels
37 float normal_dot_threshold = 0.95f; // Normal dot threshold for motion detection
38 int max_accum_frames = 8; // Maximum accumulation frames
39 };
40
41 int max_steps = 64;
42 int max_rays = 8;
43 float depth_tolerance = 0.1f;
44 float brightness = 1.0f;
47 float fade_in_start = 0.1f;
48 float fade_in_end = 0.2f;
49 bool enable_half_res = false;
50 // Cone tracing parameters
51 bool enable_cone_tracing = false;
53
54
56 // Temporal accumulation parameters
58 };
59
65
75
77 auto init(rtti::context& ctx) -> bool;
78
80 auto run(gfx::render_view& rview, const run_params& params) -> gfx::frame_buffer::ptr;
81
84
87
90
93 const gfx::frame_buffer::ptr& ssr_curr,
94 const gfx::frame_buffer::ptr& g_buffer,
95 const camera* cam,
97
100 const gfx::frame_buffer::ptr& ssr_history,
101 const gfx::frame_buffer::ptr& ssr_curr,
102 const gfx::frame_buffer::ptr& probe_buffer,
103 const gfx::frame_buffer::ptr& g_buffer,
105
108 const gfx::texture::ptr& input_color,
109 const gfx::frame_buffer::ptr& g_buffer,
111
112
113private:
115 auto create_or_update_output_fb(gfx::render_view& rview,
116 const gfx::frame_buffer::ptr& reference,
117 const gfx::frame_buffer::ptr& output)
119
121 auto create_or_update_ssr_curr_fb(gfx::render_view& rview,
122 const gfx::frame_buffer::ptr& reference,
123 bool enable_half_res) -> gfx::frame_buffer::ptr;
124
126 auto create_or_update_ssr_history_tex(gfx::render_view& rview,
127 const gfx::frame_buffer::ptr& reference,
128 bool enable_half_res) -> gfx::texture::ptr;
129
131 auto create_or_update_ssr_history_temp_fb(gfx::render_view& rview,
132 const gfx::frame_buffer::ptr& reference,
133 bool enable_half_res) -> gfx::frame_buffer::ptr;
134
135
136 // FidelityFX SSR Pixel Shader Program
137 struct fidelityfx_pixel_program : uniforms_cache
138 {
139 gpu_program::ptr program;
140 gfx::program::uniform_ptr u_ssr_params; // x: max_steps, y: depth_tolerance, z: max_rays, w: brightness
141 gfx::program::uniform_ptr u_hiz_params; // x: buffer_width, y: buffer_height, z: num_depth_mips, w: half_res
142 gfx::program::uniform_ptr u_fade_params; // x: fade_in_start, y: fade_in_end, z: roughness_depth_tolerance, w: facing_reflections_fading
143 gfx::program::uniform_ptr u_cone_params; // x: cone_angle_bias, y: max_mip_level, z: unused, w: unused
144 gfx::program::uniform_ptr u_prev_view_proj; // Previous frame view-projection matrix
145 gfx::program::uniform_ptr s_color; // Input color texture
146 gfx::program::uniform_ptr s_normal; // Normal buffer
147 gfx::program::uniform_ptr s_depth; // Depth buffer
148 gfx::program::uniform_ptr s_hiz; // Hi-Z buffer
149 gfx::program::uniform_ptr s_color_blurred; // Pre-blurred color buffer with mip chain
150
151 void cache_uniforms()
152 {
153 // Manual uniform creation for FidelityFX SSR using std::make_shared
154 cache_uniform(program.get(), u_ssr_params, "u_ssr_params", gfx::uniform_type::Vec4);
155 cache_uniform(program.get(), u_hiz_params, "u_hiz_params", gfx::uniform_type::Vec4);
156 cache_uniform(program.get(), u_fade_params, "u_fade_params", gfx::uniform_type::Vec4);
157 cache_uniform(program.get(), u_cone_params, "u_cone_params", gfx::uniform_type::Vec4);
158 cache_uniform(program.get(), u_prev_view_proj, "u_prev_view_proj", gfx::uniform_type::Mat4);
159 cache_uniform(program.get(), s_color, "s_color", gfx::uniform_type::Sampler);
160 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
161 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
162 cache_uniform(program.get(), s_hiz, "s_hiz", gfx::uniform_type::Sampler);
163 cache_uniform(program.get(), s_color_blurred, "s_color_blurred", gfx::uniform_type::Sampler);
164 }
165
166 auto is_valid() const -> bool
167 {
168 return program && program->is_valid();
169 }
170 } fidelityfx_pixel_program_;
171
172 // Temporal resolve program for SSR temporal accumulation
173 struct temporal_resolve_program : uniforms_cache
174 {
175 gpu_program::ptr program;
176 gfx::program::uniform_ptr u_temporal_params; // x: enable_temporal, y: history_strength, z: depth_threshold, w: roughness_sensitivity
177 gfx::program::uniform_ptr u_motion_params; // x: motion_scale_pixels, y: normal_dot_threshold, z: max_accum_frames, w: unused
178 gfx::program::uniform_ptr u_fade_params; // x: fade_in_start, y: fade_in_end, z: unused, w: unused
179 gfx::program::uniform_ptr u_prev_view_proj; // Previous frame view-projection matrix
180 gfx::program::uniform_ptr s_ssr_curr; // Current frame SSR result
181 gfx::program::uniform_ptr s_ssr_history; // Previous frame SSR history
182 gfx::program::uniform_ptr s_normal; // Normal buffer
183 gfx::program::uniform_ptr s_depth; // Depth buffer
184
185 void cache_uniforms()
186 {
187 cache_uniform(program.get(), u_temporal_params, "u_temporal_params", gfx::uniform_type::Vec4);
188 cache_uniform(program.get(), u_motion_params, "u_motion_params", gfx::uniform_type::Vec4);
189 cache_uniform(program.get(), u_fade_params, "u_fade_params", gfx::uniform_type::Vec4);
190 cache_uniform(program.get(), u_prev_view_proj, "u_prev_view_proj", gfx::uniform_type::Mat4);
191 cache_uniform(program.get(), s_ssr_curr, "s_ssr_curr", gfx::uniform_type::Sampler);
192 cache_uniform(program.get(), s_ssr_history, "s_ssr_history", gfx::uniform_type::Sampler);
193 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
194 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
195 }
196
197 auto is_valid() const -> bool
198 {
199 return program && program->is_valid();
200 }
201 } temporal_resolve_program_;
202
203 // Composite program for blending SSR with reflection probes
204 struct composite_program : uniforms_cache
205 {
206 gpu_program::ptr program;
207 gfx::program::uniform_ptr s_ssr_history; // Temporally filtered SSR result
208 gfx::program::uniform_ptr s_ssr_curr; // Current frame SSR result (for confidence)
209 gfx::program::uniform_ptr s_normal; // Normal buffer
210 gfx::program::uniform_ptr s_depth; // Depth buffer
211
212 void cache_uniforms()
213 {
214 cache_uniform(program.get(), s_ssr_history, "s_ssr_history", gfx::uniform_type::Sampler);
215 cache_uniform(program.get(), s_ssr_curr, "s_ssr_curr", gfx::uniform_type::Sampler);
216 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
217 cache_uniform(program.get(), s_depth, "s_depth", gfx::uniform_type::Sampler);
218 }
219
220 auto is_valid() const -> bool
221 {
222 return program && program->is_valid();
223 }
224 } composite_program_;
225
226 // Unified blur compute program for cone tracing
227 struct blur_compute_program : uniforms_cache
228 {
229 gpu_program::ptr program;
230 gfx::program::uniform_ptr u_blur_params;
231 gfx::program::uniform_ptr s_normal; // Normal buffer for roughness sampling
232
233 void cache_uniforms()
234 {
235 cache_uniform(program.get(), u_blur_params, "u_blur_params", gfx::uniform_type::Vec4);
236 cache_uniform(program.get(), s_normal, "s_normal", gfx::uniform_type::Sampler);
237 }
238
239 auto is_valid() const -> bool
240 {
241 return program && program->is_valid();
242 }
243 };
244 blur_compute_program blur_compute_program_;
245
246};
247
248} // namespace unravel
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:35
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:329
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:226
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:219
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:305
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:505
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:11
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:207
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:425
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:24
float blur_base_sigma
Base blur sigma for mip generation (CPU-side only)
Definition ssr_pass.h:26
float roughness_multiplier
Multiplier for roughness-based blur (higher = more blur for rough surfaces)
Definition ssr_pass.h:27
bool enable_half_res
Enable half resolution for SSR buffers.
Definition ssr_pass.h:49
int max_steps
Maximum ray marching steps for hierarchical traversal.
Definition ssr_pass.h:41
float depth_tolerance
Depth tolerance for hit validation.
Definition ssr_pass.h:43
temporal_settings temporal
Temporal accumulation settings.
Definition ssr_pass.h:57
float roughness_depth_tolerance
Additional depth tolerance for rough surfaces.
Definition ssr_pass.h:46
float fade_in_end
Screen edge fade end.
Definition ssr_pass.h:48
float brightness
Reflection brightness multiplier.
Definition ssr_pass.h:44
float facing_reflections_fading
Fade factor for camera-facing reflections.
Definition ssr_pass.h:45
cone_tracing_settings cone_tracing
Cone tracing specific settings.
Definition ssr_pass.h:52
bool enable_cone_tracing
Enable cone tracing for glossy reflections.
Definition ssr_pass.h:51
int max_rays
Maximum rays for rough surfaces (future: cone tracing)
Definition ssr_pass.h:42
bool enable_temporal_accumulation
Enable temporal accumulation.
Definition ssr_pass.h:55
float fade_in_start
Screen edge fade start.
Definition ssr_pass.h:47
gfx::frame_buffer::ptr output
Optional output buffer.
Definition ssr_pass.h:68
gfx::texture::ptr previous_frame
Previous frame color for reflection sampling.
Definition ssr_pass.h:71
gfx::frame_buffer::ptr g_buffer
G-buffer containing normals.
Definition ssr_pass.h:69
gfx::texture::ptr hiz_buffer
Hi-Z buffer texture.
Definition ssr_pass.h:70
Combined SSR settings.
Definition ssr_pass.h:62
fidelityfx_ssr_settings fidelityfx
FidelityFX SSR settings.
Definition ssr_pass.h:63
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.