Blender V5.3
eevee_raytrace.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "GPU_debug.hh"
12
13#include "eevee_instance.hh"
14
15#include "eevee_raytrace.hh"
16
17namespace blender::eevee {
18
19/* -------------------------------------------------------------------- */
23
29{
30 x &= 0x55555555;
31 x = (x ^ (x >> 1)) & 0x33333333;
32 x = (x ^ (x >> 2)) & 0x0f0f0f0f;
33 x = (x ^ (x >> 4)) & 0x00ff00ff;
34 x = (x ^ (x >> 8)) & 0x0000ffff;
35 return x;
36}
37
39{
40 return uint2(compact_bits(code >> 0), compact_bits(code >> 1));
41}
42
43/* Return a pseudo random sequence visiting all pixels in a tile before wrapping.
44 * `tile_size` needs to be a power of 2. */
45static uint2 random_in_tile(uint sample_id, uint tile_size)
46{
47 if (tile_size == 1) {
48 return uint2(0);
49 }
50
51 uint hash = sample_id;
52 hash = hash ^ (hash >> 16);
53 hash *= 0x85ebca6b;
54 hash = hash ^ (hash >> 13);
55 hash *= 0xc2b2ae35;
56 hash = hash ^ (hash >> 16);
57
58 return morton2d_to_coordinate(hash % square(tile_size));
59}
60
62{
63 const SceneEEVEE &sce_eevee = inst_.scene->eevee;
64
65 ray_tracing_options_ = sce_eevee.ray_tracing_options;
66 if ((sce_eevee.flag & SCE_EEVEE_FAST_GI_ENABLED) == 0) {
67 ray_tracing_options_.trace_max_roughness = 1.0f;
68 }
69 /* Always initialize thickness, for the ray-cast node. */
70 data_.thickness = ray_tracing_options_.screen_trace_thickness;
71
72 tracing_method_ = RaytraceEEVEE_Method(sce_eevee.ray_tracing_method);
73 fast_gi_ray_count_ = sce_eevee.fast_gi_ray_count;
74 fast_gi_step_count_ = sce_eevee.fast_gi_step_count;
75 fast_gi_ao_only_ = (sce_eevee.fast_gi_method == FAST_GI_AO_ONLY);
76
77 use_raytracing_ = (sce_eevee.flag & SCE_EEVEE_SSR_ENABLED) != 0;
78
79 /* TODO: Support raytracing for panoramic camera. */
80 if (use_raytracing_ && inst_.camera.is_panoramic()) {
81 use_raytracing_ = false;
82 inst_.info_append_i18n("Raytracing is not supported with panoramic camera yet");
83 }
84
85 float4 data(0.0f);
86 radiance_dummy_black_tx_.ensure_2d(
87 gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, int2(1), GPU_TEXTURE_USAGE_SHADER_READ, data);
88}
89
91{
92 Texture &depth_tx = inst_.render_buffers.depth_tx;
93
94 if (!use_raytracing_) {
95 /* Do not request raytracing shaders if not needed. */
96 return;
97 }
98
99#define PASS_VARIATION(_pass_name, _index, _suffix) \
100 ((_index == 0) ? _pass_name##reflect##_suffix : \
101 (_index == 1) ? _pass_name##refract##_suffix : \
102 _pass_name##diffuse##_suffix)
103
104 /* Setup. */
105 {
106 PassSimple &pass = tile_classify_ps_;
107 pass.init();
108 pass.shader_set(inst_.shaders.static_shader_get(RAY_TILE_CLASSIFY));
109 pass.bind_image("tile_raytrace_denoise_img", &tile_raytrace_denoise_tx_);
110 pass.bind_image("tile_raytrace_tracing_img", &tile_raytrace_tracing_tx_);
111 pass.bind_image("tile_fast_gi_denoise_img", &tile_fast_gi_denoise_tx_);
112 pass.bind_image("tile_fast_gi_tracing_img", &tile_fast_gi_tracing_tx_);
113 pass.bind_resources(inst_.uniform_data);
114 pass.bind_resources(inst_.gbuffer);
115 pass.dispatch(&tile_classify_dispatch_size_);
117 }
118 {
119 PassSimple &pass = tile_compact_ps_;
120 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_TILE_COMPACT);
121 pass.init();
122 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
123 pass.specialize_constant(sh, "resolution_scale", &data_.trace_pixel_scale);
124 pass.shader_set(sh);
125 pass.bind_image("tile_raytrace_denoise_img", &tile_raytrace_denoise_tx_);
126 pass.bind_image("tile_raytrace_tracing_img", &tile_raytrace_tracing_tx_);
127 pass.bind_ssbo("raytrace_tracing_dispatch_buf", &raytrace_tracing_dispatch_buf_);
128 pass.bind_ssbo("raytrace_denoise_dispatch_buf", &raytrace_denoise_dispatch_buf_);
129 pass.bind_ssbo("raytrace_tracing_tiles_buf", &raytrace_tracing_tiles_buf_);
130 pass.bind_ssbo("raytrace_denoise_tiles_buf", &raytrace_denoise_tiles_buf_);
131 pass.bind_resources(inst_.uniform_data);
132 pass.dispatch(&tile_compact_dispatch_size_);
134 }
135 {
136 PassSimple &pass = generate_ps_;
137 pass.init();
138 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_GENERATE);
139 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
140 pass.shader_set(sh);
141 pass.bind_texture(RBUFS_UTILITY_TEX_SLOT, inst_.pipelines.utility_tx);
142 pass.bind_image("out_ray_data_img", &ray_data_tx_);
143 pass.bind_ssbo("tiles_coord_buf", &raytrace_tracing_tiles_buf_);
144 pass.bind_resources(inst_.uniform_data);
145 pass.bind_resources(inst_.sampling);
146 pass.bind_resources(inst_.gbuffer);
147 pass.dispatch(raytrace_tracing_dispatch_buf_);
150 }
151 /* Tracing. */
152 {
153 PassSimple &pass = trace_planar_ps_;
154 pass.init();
155 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_TRACE_PLANAR);
156 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
157 pass.shader_set(sh);
158 pass.bind_ssbo("tiles_coord_buf", &raytrace_tracing_tiles_buf_);
159 pass.bind_image("ray_data_img", &ray_data_tx_);
160 pass.bind_image("ray_time_img", &ray_time_tx_);
161 pass.bind_image("ray_radiance_img", &ray_radiance_tx_);
162 pass.bind_texture("depth_tx", &depth_tx);
163 pass.bind_resources(inst_.uniform_data);
164 pass.bind_resources(inst_.sampling);
165 pass.bind_resources(inst_.planar_probes);
166 pass.bind_resources(inst_.volume_probes);
167 pass.bind_resources(inst_.sphere_probes);
168 pass.bind_resources(inst_.gbuffer);
169 /* TODO(@fclem): Use another dispatch with only tiles that touches planar captures. */
170 pass.dispatch(raytrace_tracing_dispatch_buf_);
172 }
173 {
174 PassSimple &pass = trace_screen_ps_;
175 pass.init();
176 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_TRACE_SCREEN);
178 sh, "trace_refraction", reinterpret_cast<bool *>(&data_.trace_refraction));
179 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
180 pass.shader_set(sh);
181 pass.bind_ssbo("tiles_coord_buf", &raytrace_tracing_tiles_buf_);
182 pass.bind_image("ray_data_img", &ray_data_tx_);
183 pass.bind_image("ray_time_img", &ray_time_tx_);
184 pass.bind_texture("radiance_front_tx", &screen_radiance_front_tx_);
185 pass.bind_texture("radiance_back_tx", &screen_radiance_back_tx_);
186 pass.bind_texture("hiz_front_tx", &inst_.hiz_buffer.front.ref_tx_);
187 pass.bind_texture("hiz_back_tx", &inst_.hiz_buffer.back.ref_tx_);
188 /* Still bind front to hiz_tx for validation layers. */
189 pass.bind_resources(inst_.hiz_buffer.front);
190 pass.bind_texture("depth_tx", &depth_tx);
191 pass.bind_image("ray_radiance_img", &ray_radiance_tx_);
192 pass.bind_resources(inst_.uniform_data);
193 pass.bind_resources(inst_.sampling);
194 pass.bind_resources(inst_.volume_probes);
195 pass.bind_resources(inst_.sphere_probes);
196 pass.bind_resources(inst_.gbuffer);
197 pass.dispatch(raytrace_tracing_dispatch_buf_);
199 }
200 {
201 PassSimple &pass = trace_fallback_ps_;
202 pass.init();
203 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_TRACE_FALLBACK);
204 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
205 pass.shader_set(sh);
206 pass.bind_ssbo("tiles_coord_buf", &raytrace_tracing_tiles_buf_);
207 pass.bind_image("ray_data_img", &ray_data_tx_);
208 pass.bind_image("ray_time_img", &ray_time_tx_);
209 pass.bind_image("ray_radiance_img", &ray_radiance_tx_);
210 pass.bind_texture("depth_tx", &depth_tx);
211 pass.bind_resources(inst_.uniform_data);
212 pass.bind_resources(inst_.volume_probes);
213 pass.bind_resources(inst_.sphere_probes);
214 pass.bind_resources(inst_.sampling);
215 pass.bind_resources(inst_.gbuffer);
216 pass.dispatch(raytrace_tracing_dispatch_buf_);
218 }
219 /* Denoise. */
220 {
221 PassSimple &pass = denoise_spatial_ps_;
222 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_DENOISE_SPATIAL);
223 pass.init();
224 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
225 pass.specialize_constant(sh, "raytrace_resolution_scale", &data_.trace_pixel_scale);
226 pass.specialize_constant(sh, "skip_denoise", reinterpret_cast<bool *>(&data_.skip_denoise));
227 pass.shader_set(sh);
228 pass.bind_ssbo("tiles_coord_buf", &raytrace_denoise_tiles_buf_);
229 pass.bind_texture(RBUFS_UTILITY_TEX_SLOT, inst_.pipelines.utility_tx);
230 pass.bind_texture("depth_tx", &depth_tx);
231 pass.bind_image("ray_data_img", &ray_data_tx_);
232 pass.bind_image("ray_time_img", &ray_time_tx_);
233 pass.bind_image("ray_radiance_img", &ray_radiance_tx_);
234 pass.bind_image("out_radiance_img", &denoised_spatial_tx_);
235 pass.bind_image("out_variance_img", &hit_variance_tx_);
236 pass.bind_image("out_hit_depth_img", &hit_depth_tx_);
237 pass.bind_image("tile_mask_img", &tile_raytrace_denoise_tx_);
238 pass.bind_resources(inst_.uniform_data);
239 pass.bind_resources(inst_.sampling);
240 pass.bind_resources(inst_.gbuffer);
241 pass.dispatch(raytrace_denoise_dispatch_buf_);
242 /* Can either be loaded by next denoise pass as image or by combined pass as texture if this is
243 * the lass stage. */
245 }
246 {
247 PassSimple &pass = denoise_temporal_ps_;
248 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_DENOISE_TEMPORAL);
249 pass.init();
250 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
251 pass.shader_set(sh);
252 pass.bind_resources(inst_.uniform_data);
253 pass.bind_texture("radiance_history_tx", &radiance_history_tx_);
254 pass.bind_texture("variance_history_tx", &variance_history_tx_);
255 pass.bind_texture("tilemask_history_tx", &tilemask_history_tx_);
256 pass.bind_texture("depth_tx", &depth_tx);
257 pass.bind_image("hit_depth_img", &hit_depth_tx_);
258 pass.bind_image("in_radiance_img", &denoised_spatial_tx_);
259 pass.bind_image("out_radiance_img", &denoised_temporal_tx_);
260 pass.bind_image("in_variance_img", &hit_variance_tx_);
261 pass.bind_image("out_variance_img", &denoise_variance_tx_);
262 pass.bind_image("tile_mask_img", &tile_raytrace_denoise_tx_);
263 pass.bind_ssbo("tiles_coord_buf", &raytrace_denoise_tiles_buf_);
264 pass.bind_resources(inst_.sampling);
265 pass.dispatch(raytrace_denoise_dispatch_buf_);
266 /* Can either be loaded by next denoise pass as image or by combined pass as texture if this is
267 * the lass stage. */
269 }
270 {
271 PassSimple &pass = denoise_bilateral_ps_;
272 pass.init();
273 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_DENOISE_BILATERAL);
274 pass.specialize_constant(sh, "closure_index", &data_.closure_index);
275 pass.shader_set(sh);
276 pass.bind_texture("depth_tx", &depth_tx);
277 pass.bind_image("in_radiance_img", &denoised_temporal_tx_);
278 pass.bind_image("out_radiance_img", &denoised_bilateral_tx_);
279 pass.bind_image("in_variance_img", &denoise_variance_tx_);
280 pass.bind_image("tile_mask_img", &tile_raytrace_denoise_tx_);
281 pass.bind_ssbo("tiles_coord_buf", &raytrace_denoise_tiles_buf_);
282 pass.bind_resources(inst_.uniform_data);
283 pass.bind_resources(inst_.sampling);
284 pass.bind_resources(inst_.gbuffer);
285 pass.dispatch(raytrace_denoise_dispatch_buf_);
286 /* Can either be loaded and written by horizon scan as image or by combined pass as texture. */
288 }
289 {
290 PassSimple &pass = fast_gi_schedule_ps_;
291 /* Reuse tile compaction shader but feed it with horizon scan specific buffers. */
292 gpu::Shader *sh = inst_.shaders.static_shader_get(RAY_TILE_COMPACT);
293 pass.init();
294 pass.specialize_constant(sh, "closure_index", 0);
295 pass.specialize_constant(sh, "resolution_scale", &data_.fast_gi_resolution_scale);
296 pass.shader_set(sh);
297 pass.bind_image("tile_raytrace_denoise_img", &tile_fast_gi_denoise_tx_);
298 pass.bind_image("tile_raytrace_tracing_img", &tile_fast_gi_tracing_tx_);
299 pass.bind_ssbo("raytrace_tracing_dispatch_buf", &fast_gi_tracing_dispatch_buf_);
300 pass.bind_ssbo("raytrace_denoise_dispatch_buf", &fast_gi_denoise_dispatch_buf_);
301 pass.bind_ssbo("raytrace_tracing_tiles_buf", &fast_gi_tracing_tiles_buf_);
302 pass.bind_ssbo("raytrace_denoise_tiles_buf", &fast_gi_denoise_tiles_buf_);
303 pass.bind_resources(inst_.uniform_data);
304 pass.dispatch(&fast_gi_schedule_dispatch_size_);
306 }
307 {
308 PassSimple &pass = fast_gi_setup_ps_;
309 pass.init();
310 pass.shader_set(inst_.shaders.static_shader_get(FAST_GI_SETUP));
311 pass.bind_resources(inst_.uniform_data);
312 pass.bind_texture("depth_tx", &depth_tx);
313 pass.bind_texture(
314 "in_radiance_tx", &screen_radiance_front_tx_, GPUSamplerState::default_sampler());
315 pass.bind_image("out_radiance_mip0", &downsampled_in_radiance_tx_ptr_[0]);
316 pass.bind_image("out_radiance_mip1", &downsampled_in_radiance_tx_ptr_[1]);
317 pass.bind_image("out_radiance_mip2", &downsampled_in_radiance_tx_ptr_[2]);
318 pass.bind_image("out_radiance_mip3", &downsampled_in_radiance_tx_ptr_[3]);
319 pass.bind_image("out_normal_mip0", &downsampled_in_normal_tx_ptr_[0]);
320 pass.bind_image("out_normal_mip1", &downsampled_in_normal_tx_ptr_[1]);
321 pass.bind_image("out_normal_mip2", &downsampled_in_normal_tx_ptr_[2]);
322 pass.bind_image("out_normal_mip3", &downsampled_in_normal_tx_ptr_[3]);
323 pass.bind_resources(inst_.uniform_data);
324 pass.bind_resources(inst_.gbuffer);
325 pass.dispatch(&fast_gi_tracing_dispatch_size_);
326 /* Result loaded by the next stage using samplers. */
328 }
329 {
330 const GPUSamplerState mipmap_linear = {GPU_SAMPLER_FILTERING_MIPMAP |
332 PassSimple &pass = fast_gi_scan_ps_;
333 pass.init();
334 gpu::Shader *sh = inst_.shaders.static_shader_get(FAST_GI_SCAN);
335 pass.specialize_constant(sh, "slice_count", fast_gi_ray_count_);
336 pass.specialize_constant(sh, "step_count", fast_gi_step_count_);
337 pass.specialize_constant(sh, "ao_only", fast_gi_ao_only_);
338 pass.shader_set(sh);
339 pass.bind_texture("screen_radiance_tx", &downsampled_in_radiance_tx_, mipmap_linear);
340 pass.bind_texture("screen_normal_tx", &downsampled_in_normal_tx_, mipmap_linear);
341 pass.bind_image("sh_0_img", &fast_gi_radiance_tx_[0]);
342 pass.bind_image("sh_1_img", &fast_gi_radiance_tx_[1]);
343 pass.bind_image("sh_2_img", &fast_gi_radiance_tx_[2]);
344 pass.bind_image("sh_3_img", &fast_gi_radiance_tx_[3]);
345 pass.bind_ssbo("tiles_coord_buf", &fast_gi_tracing_tiles_buf_);
346 pass.bind_texture(RBUFS_UTILITY_TEX_SLOT, inst_.pipelines.utility_tx);
347 pass.bind_resources(inst_.uniform_data);
348 pass.bind_resources(inst_.hiz_buffer.front);
349 pass.bind_resources(inst_.sampling);
350 pass.bind_resources(inst_.gbuffer);
351 pass.dispatch(fast_gi_tracing_dispatch_buf_);
352 /* Result loaded by the next stage using samplers. */
354 }
355 {
356 PassSimple &pass = fast_gi_denoise_ps_;
357 pass.init();
358 gpu::Shader *sh = inst_.shaders.static_shader_get(FAST_GI_DENOISE);
359 pass.shader_set(sh);
360 pass.bind_texture("depth_tx", &depth_tx);
361 pass.bind_texture("fast_gi_radiance_0_tx", &fast_gi_radiance_tx_[0]);
362 pass.bind_texture("fast_gi_radiance_1_tx", &fast_gi_radiance_tx_[1]);
363 pass.bind_texture("fast_gi_radiance_2_tx", &fast_gi_radiance_tx_[2]);
364 pass.bind_texture("fast_gi_radiance_3_tx", &fast_gi_radiance_tx_[3]);
365 pass.bind_texture("screen_normal_tx", &downsampled_in_normal_tx_);
366 pass.bind_image("sh_0_img", &fast_gi_radiance_denoised_tx_[0]);
367 pass.bind_image("sh_1_img", &fast_gi_radiance_denoised_tx_[1]);
368 pass.bind_image("sh_2_img", &fast_gi_radiance_denoised_tx_[2]);
369 pass.bind_image("sh_3_img", &fast_gi_radiance_denoised_tx_[3]);
370 pass.bind_ssbo("tiles_coord_buf", &fast_gi_tracing_tiles_buf_);
371 pass.bind_resources(inst_.uniform_data);
372 pass.bind_resources(inst_.sampling);
373 pass.bind_resources(inst_.hiz_buffer.front);
374 pass.dispatch(fast_gi_tracing_dispatch_buf_);
375 /* Result loaded by the next stage using samplers. */
377 }
378 {
379 PassSimple &pass = fast_gi_resolve_ps_;
380 pass.init();
381 gpu::Shader *sh = inst_.shaders.static_shader_get(FAST_GI_RESOLVE);
382 pass.shader_set(sh);
383 pass.bind_texture("depth_tx", &depth_tx);
384 pass.bind_texture("fast_gi_radiance_0_tx", &fast_gi_radiance_denoised_tx_[0]);
385 pass.bind_texture("fast_gi_radiance_1_tx", &fast_gi_radiance_denoised_tx_[1]);
386 pass.bind_texture("fast_gi_radiance_2_tx", &fast_gi_radiance_denoised_tx_[2]);
387 pass.bind_texture("fast_gi_radiance_3_tx", &fast_gi_radiance_denoised_tx_[3]);
388 pass.bind_texture("screen_normal_tx", &downsampled_in_normal_tx_);
389 pass.bind_image("closure0_img", &fast_gi_scan_output_tx_[0]);
390 pass.bind_image("closure1_img", &fast_gi_scan_output_tx_[1]);
391 pass.bind_image("closure2_img", &fast_gi_scan_output_tx_[2]);
392 pass.bind_ssbo("tiles_coord_buf", &fast_gi_denoise_tiles_buf_);
393 pass.bind_resources(inst_.uniform_data);
394 pass.bind_resources(inst_.sampling);
395 pass.bind_resources(inst_.gbuffer);
396 pass.bind_resources(inst_.volume_probes);
397 pass.bind_resources(inst_.sphere_probes);
398 pass.dispatch(fast_gi_denoise_dispatch_buf_);
399 /* Can either be loaded by another denoising stage or by combined pass as texture. */
401 }
402
403 for (int i : IndexRange(3)) {
404 const bool use_denoise = (ray_tracing_options_.flag & RAYTRACE_EEVEE_USE_DENOISE);
405 const bool use_spatial_denoise = (ray_tracing_options_.denoise_stages &
407 use_denoise;
408 const bool use_temporal_denoise = (ray_tracing_options_.denoise_stages &
410 use_spatial_denoise;
411 const bool use_bilateral_denoise = (ray_tracing_options_.denoise_stages &
413 use_temporal_denoise;
414
415 data_.closure_index = i;
416 data_.trace_pixel_scale = max_ii(1, power_of_2_max_i(ray_tracing_options_.resolution_scale));
417 data_.skip_denoise = !use_spatial_denoise;
418 inst_.manager->warm_shader_specialization(tile_classify_ps_);
419 inst_.manager->warm_shader_specialization(tile_compact_ps_);
420 inst_.manager->warm_shader_specialization(generate_ps_);
421 if (tracing_method_ == RAYTRACE_EEVEE_METHOD_SCREEN) {
422 if (inst_.planar_probes.enabled()) {
423 inst_.manager->warm_shader_specialization(trace_planar_ps_);
424 }
425 for (int j : IndexRange(2)) {
426 data_.trace_refraction = bool(j);
427 inst_.manager->warm_shader_specialization(trace_screen_ps_);
428 }
429 }
430 else {
431 inst_.manager->warm_shader_specialization(trace_fallback_ps_);
432 }
433 if (use_spatial_denoise) {
434 inst_.manager->warm_shader_specialization(denoise_spatial_ps_);
435 }
436 if (use_temporal_denoise) {
437 inst_.manager->warm_shader_specialization(denoise_temporal_ps_);
438 }
439 if (use_bilateral_denoise) {
440 inst_.manager->warm_shader_specialization(denoise_bilateral_ps_);
441 }
442 bool use_fast_gi_scan = ray_tracing_options_.trace_max_roughness < 1.0f;
443 if (use_fast_gi_scan) {
444 inst_.manager->warm_shader_specialization(fast_gi_schedule_ps_);
445 inst_.manager->warm_shader_specialization(fast_gi_setup_ps_);
446 inst_.manager->warm_shader_specialization(fast_gi_scan_ps_);
447 inst_.manager->warm_shader_specialization(fast_gi_denoise_ps_);
448 inst_.manager->warm_shader_specialization(fast_gi_resolve_ps_);
449 }
450 }
451}
452
454
455void RayTraceModule::debug_draw(View & /*view*/, gpu::FrameBuffer * /*view_fb*/) {}
456
458{
459 bool is_persp = winmat[3][3] == 0.0f;
460
461 float left, right, bottom, top, near, far;
462 projmat_dimensions(winmat.ptr(), &left, &right, &bottom, &top, &near, &far);
463
464 float avg_pixel_radius_far = length(abs(float2(left - right, bottom - top)) / float2(extent));
465
466 /* Average pixel radius at unit Z plane from the camera. */
467 const float avg_pixel_radius_unit = is_persp ? avg_pixel_radius_far / near :
468 avg_pixel_radius_far;
469
470 data_.ray_thickness = ScreenThicknessParameters::build(
471 winmat, avg_pixel_radius_unit, 3.0f, data_.thickness);
472
473 data_.fast_gi_thickness = ScreenThicknessParameters::build(
474 winmat,
475 avg_pixel_radius_unit,
476 /* Eyeballed for 64 samples and scaled for lower step count. */
477 4.0f * sqrtf(64.0f / float(fast_gi_step_count_)),
478 inst_.uniform_data.data.ao.thickness_near);
479}
480
482 gpu::Texture *screen_radiance_back_tx,
483 eClosureBits active_closures,
484 View &render_view)
485{
486 using namespace blender::math;
487 BLI_assert(use_raytracing_);
488
489 screen_radiance_front_tx_ = rt_buffer.radiance_feedback_tx.is_valid() ?
490 rt_buffer.radiance_feedback_tx :
491 radiance_dummy_black_tx_;
492 screen_radiance_back_tx_ = screen_radiance_back_tx ? screen_radiance_back_tx :
493 screen_radiance_front_tx_;
494
495 RaytraceEEVEE options = ray_tracing_options_;
496
497 bool use_fast_gi_scan = options.trace_max_roughness < 1.0f;
498
499 const int resolution_scale = max_ii(1, power_of_2_max_i(options.resolution_scale));
500 const int fast_gi_resolution_scale = max_ii(
501 1, power_of_2_max_i(inst_.scene->eevee.fast_gi_resolution));
502
503 const int2 group_size(RAYTRACE_GROUP_SIZE);
504 const int2 extent = inst_.film.render_extent_get();
505 const int2 tracing_res = math::divide_ceil(extent, int2(resolution_scale));
506 /* Just like HiZ, the FastGI needs a multiple of its dispatch size in order to use MipMaps. */
507 const int2 tracing_res_fast_gi = math::ceil_to_multiple(
508 math::divide_ceil(extent, int2(fast_gi_resolution_scale)), group_size);
509
510 const int2 denoise_tiles = divide_ceil(extent, group_size);
511 const int2 raytrace_tiles = divide_ceil(tracing_res, group_size);
512 const int2 raytrace_tiles_fast_gi = divide_ceil(tracing_res_fast_gi, group_size);
513 const int denoise_tile_count = denoise_tiles.x * denoise_tiles.y;
514 const int raytrace_tile_count = raytrace_tiles.x * raytrace_tiles.y;
515 const int raytrace_tile_count_fast_gi = raytrace_tiles_fast_gi.x * raytrace_tiles_fast_gi.y;
516 tile_classify_dispatch_size_ = int3(denoise_tiles, 1);
517 fast_gi_schedule_dispatch_size_ = int3(divide_ceil(raytrace_tiles_fast_gi, group_size), 1);
518 tile_compact_dispatch_size_ = int3(divide_ceil(raytrace_tiles, group_size), 1);
519 tracing_dispatch_size_ = int3(raytrace_tiles, 1);
520 fast_gi_tracing_dispatch_size_ = int3(raytrace_tiles_fast_gi, 1);
521
522 /* TODO(fclem): Use real max closure count from shader. */
523 const int closure_count = 3;
524 gpu::TextureFormat format = gpu::TextureFormat::RAYTRACE_TILEMASK_FORMAT;
526 tile_raytrace_denoise_tx_.ensure_2d_array(format, denoise_tiles, closure_count, usage_rw);
527 tile_raytrace_tracing_tx_.ensure_2d_array(format, raytrace_tiles, closure_count, usage_rw);
528 /* Kept as 2D array for compatibility with the tile compaction shader. */
529 tile_fast_gi_denoise_tx_.ensure_2d_array(format, denoise_tiles, 1, usage_rw);
530 tile_fast_gi_tracing_tx_.ensure_2d_array(format, raytrace_tiles_fast_gi, 1, usage_rw);
531
532 tile_raytrace_denoise_tx_.clear(uint4(0u));
533 tile_raytrace_tracing_tx_.clear(uint4(0u));
534 tile_fast_gi_denoise_tx_.clear(uint4(0u));
535 tile_fast_gi_tracing_tx_.clear(uint4(0u));
536
537 fast_gi_tracing_tiles_buf_.resize(ceil_to_multiple_u(raytrace_tile_count_fast_gi, 512));
538 fast_gi_denoise_tiles_buf_.resize(ceil_to_multiple_u(denoise_tile_count, 512));
539 raytrace_tracing_tiles_buf_.resize(ceil_to_multiple_u(raytrace_tile_count, 512));
540 raytrace_denoise_tiles_buf_.resize(ceil_to_multiple_u(denoise_tile_count, 512));
541
542 data_.use_backface_hit = (options.flag & RAYTRACE_EEVEE_USE_BACKFACE) != 0;
543 data_.backface_hit_scale = data_.use_backface_hit ? options.backface_radiance_scale : 0.0f;
544
545 /* Data for tile classification. */
546 float roughness_mask_start = options.trace_max_roughness;
547 float roughness_mask_fade = 0.2f;
548 data_.roughness_mask_scale = 1.0 / roughness_mask_fade;
549 data_.roughness_mask_bias = data_.roughness_mask_scale * roughness_mask_start;
550
551 /* Data for the radiance setup. */
552 data_.trace_pixel_scale = resolution_scale;
553 data_.trace_pixel_offset = int2(random_in_tile(inst_.sampling.sample_index(), resolution_scale));
554 data_.history_persmat = rt_buffer.history_persmat;
555 data_.full_resolution = extent;
556 data_.full_resolution_inv = 1.0f / float2(extent);
557
558 data_.fast_gi_uv_scale = float2(extent) / float2(tracing_res_fast_gi * fast_gi_resolution_scale);
559 data_.fast_gi_lod_bias = log2_floor_u(resolution_scale);
560 data_.fast_gi_resolution_scale = fast_gi_resolution_scale;
561 data_.fast_gi_resolution_bias = int2(
562 random_in_tile(inst_.sampling.sample_index(), fast_gi_resolution_scale));
563
564 /* TODO(fclem): Eventually all uniform data is setup here. */
565
566 inst_.uniform_data.raytrace.push_update();
567
569
570 GPU_debug_group_begin("Raytracing");
571
572 const bool has_active_closure = active_closures != CLOSURE_NONE;
573
574 if (has_active_closure) {
575 inst_.manager->submit(tile_classify_ps_);
576 }
577
578 data_.trace_refraction = screen_radiance_back_tx != nullptr;
579
580 for (int i = 0; i < 3; i++) {
581 result.closures[i] = trace(i, (closure_count > i), options, rt_buffer, render_view);
582 }
583
584 if (has_active_closure) {
585 if (use_fast_gi_scan) {
586 GPU_debug_group_begin("Fast GI");
587
588 downsampled_in_radiance_tx_.ensure_2d(
589 gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, tracing_res_fast_gi, usage_rw, nullptr, 4);
590 downsampled_in_normal_tx_.ensure_2d(
591 gpu::TextureFormat::UNORM_10_10_10_2, tracing_res_fast_gi, usage_rw, nullptr, 4);
592
593 downsampled_in_radiance_tx_.ensure_mip_views();
594 downsampled_in_normal_tx_.ensure_mip_views();
595
596 for (int i : IndexRange(4)) {
597 downsampled_in_radiance_tx_ptr_[i] = downsampled_in_radiance_tx_.mip_view(i);
598 downsampled_in_normal_tx_ptr_[i] = downsampled_in_normal_tx_.mip_view(i);
599 }
600
601 fast_gi_radiance_tx_[0].acquire_2d(
602 tracing_res_fast_gi, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_rw);
603 fast_gi_radiance_denoised_tx_[0].acquire_2d(
604 tracing_res_fast_gi, gpu::TextureFormat::SFLOAT_16_16_16_16, usage_rw);
605 for (int i : IndexRange(1, 3)) {
606 fast_gi_radiance_tx_[i].acquire_2d(
607 tracing_res_fast_gi, gpu::TextureFormat::UNORM_8_8_8_8, usage_rw);
608 fast_gi_radiance_denoised_tx_[i].acquire_2d(
609 tracing_res_fast_gi, gpu::TextureFormat::UNORM_8_8_8_8, usage_rw);
610 }
611 for (int i : IndexRange(3)) {
612 fast_gi_scan_output_tx_[i] = result.closures[i];
613 }
614
615 fast_gi_tracing_dispatch_buf_.clear_to_zero();
616 fast_gi_denoise_dispatch_buf_.clear_to_zero();
617 inst_.manager->submit(fast_gi_schedule_ps_);
618
619 inst_.manager->submit(fast_gi_setup_ps_, render_view);
620 inst_.manager->submit(fast_gi_scan_ps_, render_view);
621 inst_.manager->submit(fast_gi_denoise_ps_, render_view);
622 inst_.manager->submit(fast_gi_resolve_ps_, render_view);
623
624 for (int i : IndexRange(4)) {
625 fast_gi_radiance_tx_[i].release();
626 fast_gi_radiance_denoised_tx_[i].release();
627 }
628
630 }
631 }
632
634
635 rt_buffer.history_persmat = render_view.persmat();
636
637 return result;
638}
639
640RayTraceResultTexture RayTraceModule::trace(int closure_index,
641 bool active_layer,
643 RayTraceBuffer &rt_buffer,
644 View &render_view)
645{
646 RayTraceBuffer::DenoiseBuffer *denoise_buf = &rt_buffer.closures[closure_index];
647
648 if (!active_layer) {
649 /* Early out. Release persistent buffers. Still acquire one dummy resource for validation. */
650 denoise_buf->denoised_spatial_tx.acquire_2d(int2(1),
651 gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT);
652 denoise_buf->radiance_history_tx.release();
653 denoise_buf->variance_history_tx.release();
654 denoise_buf->tilemask_history_tx.free();
655 return {denoise_buf->denoised_spatial_tx};
656 }
657
658 const int resolution_scale = max_ii(1, power_of_2_max_i(options.resolution_scale));
659
660 const int2 extent = inst_.film.render_extent_get();
661 const int2 tracing_res = math::divide_ceil(extent, int2(resolution_scale));
662
663 renderbuf_depth_view_ = inst_.render_buffers.depth_tx;
664
665 const bool use_denoise = (options.flag & RAYTRACE_EEVEE_USE_DENOISE);
666 const bool use_spatial_denoise = (options.denoise_stages & RAYTRACE_EEVEE_DENOISE_SPATIAL) &&
667 use_denoise;
668 const bool use_temporal_denoise = (options.denoise_stages & RAYTRACE_EEVEE_DENOISE_TEMPORAL) &&
669 use_spatial_denoise;
670 const bool use_bilateral_denoise = (options.denoise_stages & RAYTRACE_EEVEE_DENOISE_BILATERAL) &&
671 use_temporal_denoise;
672
674
675 GPU_debug_group_begin("Raytracing");
676
677 data_.thickness = options.screen_trace_thickness;
678 data_.quality = 1.0f - 0.95f * options.screen_trace_quality;
679
680 float roughness_mask_start = options.trace_max_roughness;
681 float roughness_mask_fade = 0.2f;
682 data_.roughness_mask_scale = 1.0 / roughness_mask_fade;
683 data_.roughness_mask_bias = data_.roughness_mask_scale * roughness_mask_start;
684
685 data_.trace_pixel_scale = resolution_scale;
686 data_.trace_pixel_offset = int2(random_in_tile(inst_.sampling.sample_index(), resolution_scale));
687 data_.denoise_history_persmat = denoise_buf->history_persmat;
688 data_.full_resolution = extent;
689 data_.full_resolution_inv = 1.0f / float2(extent);
690 data_.skip_denoise = !use_spatial_denoise;
691 data_.closure_index = closure_index;
693
694 /* Ray setup. */
695 raytrace_tracing_dispatch_buf_.clear_to_zero();
696 raytrace_denoise_dispatch_buf_.clear_to_zero();
697 inst_.manager->submit(tile_compact_ps_);
698
699 {
700 /* Tracing rays. */
701 ray_data_tx_.acquire_2d(tracing_res, gpu::TextureFormat::SFLOAT_16_16_16_16);
702 ray_time_tx_.acquire_2d(tracing_res, gpu::TextureFormat::RAYTRACE_RAYTIME_FORMAT);
703 ray_radiance_tx_.acquire_2d(tracing_res, gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT);
704
705 inst_.manager->submit(generate_ps_, render_view);
706 if (tracing_method_ == RAYTRACE_EEVEE_METHOD_SCREEN) {
707 if (inst_.planar_probes.enabled()) {
708 inst_.manager->submit(trace_planar_ps_, render_view);
709 }
710 inst_.manager->submit(trace_screen_ps_, render_view);
711 }
712 else {
713 inst_.manager->submit(trace_fallback_ps_, render_view);
714 }
715 }
716
717 RayTraceResultTexture result;
718
719 /* Spatial denoise pass is required to resolve at least one ray per pixel. */
720 {
721 denoise_buf->denoised_spatial_tx.acquire_2d(extent,
722 gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT);
723 hit_variance_tx_.acquire_2d(use_temporal_denoise ? extent : int2(1),
724 gpu::TextureFormat::RAYTRACE_VARIANCE_FORMAT);
725 hit_depth_tx_.acquire_2d(use_temporal_denoise ? extent : int2(1),
726 gpu::TextureFormat::SFLOAT_32);
727 denoised_spatial_tx_ = denoise_buf->denoised_spatial_tx;
728
729 inst_.manager->submit(denoise_spatial_ps_, render_view);
730
731 result = {denoise_buf->denoised_spatial_tx};
732 }
733
734 ray_data_tx_.release();
735 ray_time_tx_.release();
736 ray_radiance_tx_.release();
737
738 if (use_temporal_denoise) {
739 denoise_buf->denoised_temporal_tx.acquire_2d(
740 extent, gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, usage_rw);
741
742 int2 variance_size = use_bilateral_denoise ? extent : int2(1);
743 gpu::TextureFormat variance_format = gpu::TextureFormat::RAYTRACE_VARIANCE_FORMAT;
744
745 denoise_variance_tx_.acquire_2d(variance_size, variance_format, usage_rw);
746 denoise_buf->variance_history_tx.acquire_2d(variance_size, variance_format, usage_rw);
747
748 denoise_buf->tilemask_history_tx.ensure_2d_array(gpu::TextureFormat::RAYTRACE_TILEMASK_FORMAT,
749 tile_raytrace_denoise_tx_.size().xy(),
750 tile_raytrace_denoise_tx_.size().z,
751 usage_rw);
752
753 if (denoise_buf->radiance_history_tx.acquire_2d(
754 extent, gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, usage_rw) ||
755 denoise_buf->valid_history == false)
756 {
757 /* If viewport resolution changes, do not try to use history. */
758 denoise_buf->tilemask_history_tx.clear(uint4(0u));
759 }
760
761 radiance_history_tx_ = denoise_buf->radiance_history_tx;
762 variance_history_tx_ = denoise_buf->variance_history_tx;
763 tilemask_history_tx_ = denoise_buf->tilemask_history_tx;
764 denoised_temporal_tx_ = denoise_buf->denoised_temporal_tx;
765
766 inst_.manager->submit(denoise_temporal_ps_, render_view);
767
768 /* Save view-projection matrix for next reprojection. */
769 denoise_buf->history_persmat = render_view.persmat();
770 /* Radiance will be swapped with history in #RayTraceResult::release().
771 * Variance is swapped with history after bilateral denoise.
772 * It keeps data-flow easier to follow. */
773 result = {denoise_buf->denoised_temporal_tx, denoise_buf->radiance_history_tx};
774 /* Not referenced by result anymore. */
775 denoise_buf->denoised_spatial_tx.release();
776
777 GPU_texture_copy(denoise_buf->tilemask_history_tx, tile_raytrace_denoise_tx_);
778 }
779
780 /* Only use history buffer for the next frame if temporal denoise was used by the current one. */
781 denoise_buf->valid_history = use_temporal_denoise;
782
783 hit_variance_tx_.release();
784 hit_depth_tx_.release();
785
786 if (use_bilateral_denoise) {
788 extent, gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, usage_rw);
789 denoised_bilateral_tx_ = denoise_buf->denoised_bilateral_tx;
790
791 inst_.manager->submit(denoise_bilateral_ps_, render_view);
792
793 /* Swap after last use, retain history buffers until next cycle. */
795 TextureFromPool::swap(denoise_variance_tx_, denoise_buf->variance_history_tx);
796 denoise_buf->radiance_history_tx.retain();
797 denoise_buf->variance_history_tx.retain();
798
799 result = {denoise_buf->denoised_bilateral_tx};
800 /* Not referenced by result anymore. */
801 denoise_buf->denoised_temporal_tx.release();
802 }
803 else if (use_temporal_denoise) {
804 /* Not referenced by result anymore. */
805 denoise_buf->variance_history_tx.retain();
806 }
807
808 denoise_variance_tx_.release();
809
811
812 return result;
813}
814
816{
817 const int2 extent = inst_.film.render_extent_get();
819
821 for (int i = 0; i < 3; i++) {
822 RayTraceBuffer::DenoiseBuffer *denoise_buf = &rt_buffer.closures[i];
824 extent, gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, usage_rw);
825 result.closures[i] = {denoise_buf->denoised_bilateral_tx};
826 }
827 return result;
828}
829
831{
833
835 for (int i = 0; i < 3; i++) {
836 RayTraceBuffer::DenoiseBuffer *denoise_buf = &rt_buffer.closures[i];
838 int2(1), gpu::TextureFormat::RAYTRACE_RADIANCE_FORMAT, usage_rw);
839 result.closures[i] = {denoise_buf->denoised_bilateral_tx};
840 }
841 return result;
842}
843
844
845} // namespace blender::eevee
constexpr IndexRange()=default
void submit(PassSimple &pass, View &view)
static void swap(TextureFromPool &a, TextureFromPool &b)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
void clear(float4 values)
bool ensure_2d_array(gpu::TextureFormat format, int2 extent, int layers, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
const float4x4 persmat(int view_id=0) const
Definition draw_view.hh:161
void bind_resources(U &resources)
Definition draw_pass.hh:459
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void specialize_constant(gpu::Shader *shader, const char *name, const float &data)
void bind_image(const char *name, gpu::Texture *image)
void dispatch(int group_len)
void barrier(GPUBarrier type)
void bind_ssbo(const char *name, gpu::StorageBuf *buffer)
int2 render_extent_get() const
PlanarProbeModule planar_probes
UniformDataModule uniform_data
RayTraceResult render(RayTraceBuffer &rt_buffer, gpu::Texture *screen_radiance_back_tx, eClosureBits active_closures, View &render_view)
RayTraceResult alloc_only(RayTraceBuffer &rt_buffer)
void thickness_parameters_setup(const float4x4 &winmat, const int2 extent)
void debug_draw(View &view, gpu::FrameBuffer *view_fb)
RayTraceResult alloc_dummy(RayTraceBuffer &rt_buffer)
uint64_t sample_index() const
CCL_NAMESPACE_BEGIN struct Options options
#define RBUFS_UTILITY_TEX_SLOT
#define abs
float length(VecOp< float, D >) RET
float square(float v)
int divide_ceil(int a, int b)
detail::Pass< command::DrawCommandBuf > PassSimple
static uint2 morton2d_to_coordinate(uint code)
static uint compact_bits(uint x)
static uint2 random_in_tile(uint sample_id, uint tile_size)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, Size > ceil_to_multiple(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< uint32_t, 2 > uint2
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ RAYTRACE_EEVEE_USE_DENOISE
@ RAYTRACE_EEVEE_USE_BACKFACE
unsigned int uint
Definition BKE_text.h:55
void projmat_dimensions(const float winmat[4][4], float *r_left, float *r_right, float *r_bottom, float *r_top, float *r_near, float *r_far)
VecBase< uint32_t, 4 > uint4
void GPU_texture_copy(gpu::Texture *dst, gpu::Texture *src)
MINLINE int power_of_2_max_i(int n)
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
MINLINE int max_ii(int a, int b)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
@ SCE_EEVEE_FAST_GI_ENABLED
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_SHADER_WRITE
BMesh const char void * data
@ RAYTRACE_EEVEE_METHOD_SCREEN
@ GPU_BARRIER_TEXTURE_FETCH
Definition GPU_state.hh:40
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
@ GPU_BARRIER_SHADER_IMAGE_ACCESS
Definition GPU_state.hh:38
void GPU_debug_group_end()
Definition gpu_debug.cc:34
MINLINE unsigned int log2_floor_u(unsigned int x)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
@ RAYTRACE_EEVEE_DENOISE_SPATIAL
@ RAYTRACE_EEVEE_DENOISE_BILATERAL
@ RAYTRACE_EEVEE_DENOISE_TEMPORAL
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
#define hash
Definition noise_c.cc:156
#define sqrtf
static constexpr GPUSamplerState default_sampler()
const c_style_mat & ptr() const
RaytraceEEVEE_Method ray_tracing_method
FastGI_Method fast_gi_method
struct RaytraceEEVEE ray_tracing_options
eSceneEEVEE_Flag flag
static ScreenThicknessParameters build(float4x4 winmat, float avg_pixel_radius_unit, float min_pixel_thickness, float min_constant_thickness)