Blender V5.3
eevee_ray_trace.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "draw_view.bsl.hh"
16#include "eevee_uniform.bsl.hh"
18
19namespace eevee::raytrace {
20
21namespace screen {
22
39
43[[compute,
44 local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE),
47 [[resource_table]] const LightprobeRenderData &lightprobes,
48 [[resource_table]] const Uniform &uni,
49 [[resource_table]] const draw::View &views,
50 [[resource_table]] const Sampling &sampling,
51 [[resource_table]] const gbuffer::Reader &reader,
52 [[work_group_id]] const uint3 group_id,
53 [[local_invocation_id]] const uint3 local_id)
54{
55 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
56 uint2 tile_coord = unpackUvec2x16(srt.tiles_coord_buf[group_id.x]);
57 int2 texel = int2(local_id.xy + tile_coord * tile_size);
58
59 /* Check whether texel is out of bounds for all cases, so we can utilize fast
60 * texture functions and early exit if not. */
61 if (any(greaterThanEqual(texel, imageSize(srt.ray_data_img).xy)) ||
62 any(lessThan(texel, int2(0))))
63 {
64 return;
65 }
66
67 float4 ray_data_im = imageLoadFast(srt.ray_data_img, texel);
68 float ray_pdf_inv = ray_data_im.w;
69
70 if (ray_pdf_inv < 0.0f) {
71 /* Ray destined to planar trace. */
72 return;
73 }
74
75 if (ray_pdf_inv == 0.0f) {
76 /* Invalid ray or pixels without ray. Do not trace. */
77 imageStoreFast(srt.ray_time_img, texel, float4(0.0f));
78 imageStoreFast(srt.ray_radiance_img, texel, float4(0.0f));
79 return;
80 }
81
82 const ViewMatrices view = views.get(0);
83
84 int2 texel_fullres = texel * uni.raytrace_buf.trace_pixel_scale +
85 uni.raytrace_buf.trace_pixel_offset;
86
87 gbuffer::Header gbuf_header = reader.read_header(texel_fullres);
89 gbuf_header.bin_type(srt.closure_index));
90
91 float depth = reverse_z::read(texelFetch(srt.depth_tx, texel_fullres, 0).r);
92 float2 uv = (float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
93
94 float3 P = view.point_screen_to_world(float3(uv, depth));
96 Ray ray;
97 ray.origin = P;
98 ray.direction = ray_data_im.xyz;
99
100 /* Only closure 0 can be a transmission closure. */
101 if (srt.closure_index == 0) {
102 const Thickness thickness = reader.read_thickness(gbuf_header, texel_fullres);
103 if (thickness.value() != 0.0f) {
104 ClosureUndetermined cl = reader.read_bin(texel_fullres, srt.closure_index);
106 }
107 }
108
109 float3 radiance = float3(0.0f);
110 float noise_offset = sampling.rng_1D_get(SAMPLING_RAYTRACE_W);
111 float rand_trace = random::interleaved_gradient(float2(texel), 5.0f, noise_offset);
112
113 ClosureUndetermined cl = reader.read_bin(texel_fullres, srt.closure_index);
114 float roughness = closure_apparent_roughness_get(cl);
115
116 /* Transform the ray into view-space. */
117 Ray ray_view;
118 ray_view.origin = transform_point(view.viewmat, ray.origin);
119 ray_view.direction = transform_direction(view.viewmat, ray.direction);
120 /* Extend the ray to cover the whole view. */
121 ray_view.max_time = abs(view.far() - view.near());
122
124 hit.valid = false;
125 /* This huge branch is likely to be a huge issue for performance.
126 * We could split the shader but that would mean to dispatch some area twice for the same closure
127 * index. Another idea is to put both HiZ buffer in the same texture and dynamically access one
128 * or the other. But that might also impact performance. */
129 if (!closure_has_transmission(closure_type)) {
130 hit = raytrace_screen(view,
131 uni.raytrace_buf,
132 uni.uniform_buf.hiz,
133 srt.hiz_front_tx,
134 rand_trace,
135 roughness,
136 false, /* allow_self_intersection */
137 ray_view);
138
139 if (hit.valid) {
140 float3 hit_P = transform_point(view.viewinv, hit.v_hit_P);
141 /* TODO(@fclem): Split matrix multiply for precision. */
142 float2 history_ndc_hit_P = project_point(uni.raytrace_buf.history_persmat, hit_P).xy;
143 /* Make sure to tag hits that _were_ out of view as no hit. Otherwise the history is sampled
144 * with clamp to border mode, which can introduce too much energy if the border pixels are
145 * bright. */
146 hit.valid = all(lessThan(abs(history_ndc_hit_P), float2(1.0f)));
147
148 float2 history_ss_hit_P = history_ndc_hit_P * 0.5f + 0.5f;
149
150 /* Fetch radiance at hit-point. */
151 radiance = raytrace_sample_screen(
152 view, srt.radiance_front_tx, uni.raytrace_buf, hit, roughness, history_ss_hit_P);
153
154 if (hit.hit_backface) {
155 radiance *= uni.raytrace_buf.backface_hit_scale;
156
157 if (!uni.raytrace_buf.use_backface_hit) {
158 hit.valid = false;
159 }
160 }
161 }
162 }
163 else if (srt.trace_refraction) {
164 hit = raytrace_screen(view,
165 uni.raytrace_buf,
166 uni.uniform_buf.hiz,
167 srt.hiz_back_tx,
168 rand_trace,
169 roughness,
170 true, /* allow_self_intersection */
171 ray_view);
172
173 if (hit.valid) {
174 /* Fetch radiance at hit-point. */
175 radiance = raytrace_sample_screen(
176 view, srt.radiance_back_tx, uni.raytrace_buf, hit, roughness, hit.ss_hit_P.xy);
177 }
178 }
179
180 if (!hit.valid) {
181 /* Using ray direction as geometric normal to bias the sampling position.
182 * This is faster than loading the gbuffer again and averages between reflected and normal
183 * direction over many rays. */
184 float3 Ng = ray.direction;
185 /* Fall back to nearest light-probe. */
186 LightProbeSample samp = lightprobes.load(float2(texel), ray.origin, Ng, V);
187 /* Clamp SH to have parity with forward evaluation. */
188 float clamp_indirect = uni.uniform_buf.clamp.surface_indirect;
190 clamp_indirect);
191 radiance = lightprobes.eval_direction(samp, ray.origin, ray.direction, roughness);
192 /* Set point really far for correct reprojection of background. */
193 hit.time = 10000.0f;
194 }
195
196 radiance = colorspace::brightness_clamp_max(radiance, uni.uniform_buf.clamp.surface_indirect);
197
198 imageStoreFast(srt.ray_time_img, texel, float4(hit.time));
199 imageStoreFast(srt.ray_radiance_img, texel, float4(radiance, 0.0f));
200}
201
202} // namespace screen
203
204namespace planar {
205
217
224[[compute, local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE)]]
226 [[resource_table]] const LightprobeRenderData &lightprobes,
227 [[resource_table]] const LightprobePlaneRenderData &lightprobe_planes,
228 [[resource_table]] const Uniform &uni,
229 [[resource_table]] const draw::View &views,
230 [[resource_table]] const Sampling &sampling,
231 [[resource_table]] const gbuffer::Reader &reader,
232 [[work_group_id]] const uint3 group_id,
233 [[local_invocation_id]] const uint3 local_id)
234{
235 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
236 uint2 tile_coord = unpackUvec2x16(srt.tiles_coord_buf[group_id.x]);
237 int2 texel = int2(local_id.xy + tile_coord * tile_size);
238
239 /* Check if texel is out of bounds,
240 * so we can utilize fast texture functions and early-out if not. */
241 if (any(greaterThanEqual(texel, imageSize(srt.ray_time_img).xy))) {
242 return;
243 }
244
245 float4 ray_data_im = imageLoadFast(srt.ray_data_img, texel);
246 float ray_pdf_inv = ray_data_im.w;
247
248 if (ray_pdf_inv == 0.0f) {
249 /* Invalid ray or pixels without ray. Do not trace. */
250 imageStoreFast(srt.ray_time_img, texel, float4(0.0f));
251 imageStoreFast(srt.ray_radiance_img, texel, float4(0.0f));
252 return;
253 }
254
255 int2 texel_fullres = texel * uni.raytrace_buf.trace_pixel_scale +
256 uni.raytrace_buf.trace_pixel_offset;
257
258 gbuffer::Header gbuf_header = reader.read_header(texel_fullres);
260 gbuf_header.bin_type(srt.closure_index));
261
262 if (closure_has_transmission(closure_type)) {
263 /* Planar light-probes cannot trace refraction yet. */
264 return;
265 }
266
267 const ViewMatrices view = views.get(0);
268
269 ClosureUndetermined cl = reader.read_bin(texel_fullres, srt.closure_index);
270 float roughness = closure_apparent_roughness_get(cl);
271
272 float depth = reverse_z::read(texelFetch(srt.depth_tx, texel_fullres, 0).r);
273 float2 uv = (float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
274
275 float3 P = view.point_screen_to_world(float3(uv, depth));
277
278 int planar_id = lightprobe_planes.select_probe(P, ray_data_im.xyz);
279 if (planar_id == -1) {
280 return;
281 }
282
283 PlanarProbeData planar = lightprobe_planes.probe_planar_buf[planar_id];
284
285 /* Tag the ray data so that screen trace will not try to evaluate it and override the result. */
286 imageStoreFast(srt.ray_data_img, texel, float4(ray_data_im.xyz, -ray_data_im.w));
287
288 Ray ray;
289 ray.origin = P;
290 ray.direction = ray_data_im.xyz;
291
292 float3 radiance = float3(0.0f);
293 float noise_offset = sampling.rng_1D_get(SAMPLING_RAYTRACE_W);
294 float rand_trace = random::interleaved_gradient(float2(texel), 5.0f, noise_offset);
295
296 /* TODO(fclem): Take IOR into account in the roughness LOD bias. */
297 /* TODO(fclem): pdf to roughness mapping is a crude approximation. Find something better. */
298 // float roughness = saturate(ray_pdf_inv);
299
300 /* Transform the ray into planar view-space. */
301 Ray ray_view;
302 ray_view.origin = transform_point(planar.viewmat, ray.origin);
303 ray_view.direction = transform_direction(planar.viewmat, ray.direction);
304 /* Extend the ray to cover the whole view. */
305 ray_view.max_time = 1000.0f;
306
308 view, uni.raytrace_buf, lightprobe_planes.planar_depth_tx, planar, rand_trace, ray_view);
309
310 if (hit.valid) {
311 /* Evaluate radiance at hit-point. */
312 radiance = raytrace_sample_screen(view,
313 lightprobe_planes.planar_radiance_tx,
314 uni.raytrace_buf,
315 hit,
316 roughness,
317 hit.ss_hit_P.xy,
318 planar_id);
319 }
320 else {
321 /* Using ray direction as geometric normal to bias the sampling position.
322 * This is faster than loading the gbuffer again and averages between reflected and normal
323 * direction over many rays. */
324 float3 Ng = ray.direction;
325 /* Fall back to nearest light-probe. */
326 LightProbeSample samp = lightprobes.load(float2(texel), P, Ng, V);
327 radiance = lightprobes.eval_direction(samp, P, ray.direction, roughness);
328 /* Set point really far for correct reprojection of background. */
329 hit.time = 10000.0f;
330 }
331
332 radiance = colorspace::brightness_clamp_max(radiance, uni.uniform_buf.clamp.surface_indirect);
333
334 imageStoreFast(srt.ray_time_img, texel, float4(hit.time));
335 imageStoreFast(srt.ray_radiance_img, texel, float4(radiance, 0.0f));
336}
337
338} // namespace planar
339
340namespace fallback {
341
353
357[[compute, local_size(RAYTRACE_GROUP_SIZE, RAYTRACE_GROUP_SIZE)]]
359 [[resource_table]] const LightprobeRenderData &lightprobes,
360 [[resource_table]] const gbuffer::Reader &reader,
361 [[resource_table]] const Uniform &uni,
362 [[resource_table]] const draw::View &views,
363 [[work_group_id]] const uint3 group_id,
364 [[local_invocation_id]] const uint3 local_id)
365{
366 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
367 uint2 tile_coord = unpackUvec2x16(srt.tiles_coord_buf[group_id.x]);
368 int2 texel = int2(local_id.xy + tile_coord * tile_size);
369
370 int2 texel_fullres = texel * uni.raytrace_buf.trace_pixel_scale +
371 uni.raytrace_buf.trace_pixel_offset;
372
373 /* Check if texel is out of bounds,
374 * so we can utilize fast texture functions and early-out if not. */
375 if (any(greaterThanEqual(texel, imageSize(srt.ray_time_img).xy))) {
376 return;
377 }
378
379 ClosureUndetermined cl = reader.read_bin(texel_fullres, srt.closure_index);
380 float roughness = closure_apparent_roughness_get(cl);
381
382 float depth = reverse_z::read(texelFetch(srt.depth_tx, texel_fullres, 0).r);
383 float2 uv = (float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
384
385 float4 ray_data_im = imageLoadFast(srt.ray_data_img, texel);
386 float ray_pdf_inv = ray_data_im.w;
387
388 if (ray_pdf_inv == 0.0f) {
389 /* Invalid ray or pixels without ray. Do not trace. */
390 imageStoreFast(srt.ray_time_img, texel, float4(0.0f));
391 imageStoreFast(srt.ray_radiance_img, texel, float4(0.0f));
392 return;
393 }
394
395 const ViewMatrices view = views.get(0);
396
397 float3 P = view.point_screen_to_world(float3(uv, depth));
399
400 Ray ray;
401 ray.origin = P;
402 ray.direction = ray_data_im.xyz;
403
404 /* Only closure 0 can be a transmission closure. */
405 if (srt.closure_index == 0) {
406 const gbuffer::Header gbuf_header = reader.read_header(texel_fullres);
407 const Thickness thickness = reader.read_thickness(gbuf_header, texel_fullres);
408 if (thickness.value() != 0.0f) {
409 ClosureUndetermined cl = reader.read_bin(texel_fullres, srt.closure_index);
411 }
412 }
413
414 /* Using ray direction as geometric normal to bias the sampling position.
415 * This is faster than loading the gbuffer again and averages between reflected and normal
416 * direction over many rays. */
417 float3 Ng = ray.direction;
418 LightProbeSample samp = lightprobes.load(float2(texel), ray.origin, Ng, V);
419 /* Clamp SH to have parity with forward evaluation. */
420 float clamp_indirect = uni.uniform_buf.clamp.surface_indirect;
422 clamp_indirect);
423
424 float3 radiance = lightprobes.eval_direction(samp, ray.origin, ray.direction, roughness);
425 /* Set point really far for correct reprojection of background. */
426 float hit_time = 1000.0f;
427
428 radiance = colorspace::brightness_clamp_max(radiance, uni.uniform_buf.clamp.surface_indirect);
429
430 imageStoreFast(srt.ray_time_img, texel, float4(hit_time));
431 imageStoreFast(srt.ray_radiance_img, texel, float4(radiance, 0.0f));
432}
433
434} // namespace fallback
435} // namespace eevee::raytrace
436
float closure_apparent_roughness_get(ClosureUndetermined cl)
#define RAYTRACE_RAYTIME_FORMAT
#define RAYTRACE_RADIANCE_FORMAT
PipelineCompute eevee_ray_trace_screen(eevee::raytrace::screen::trace)
PipelineCompute eevee_ray_trace_fallback(eevee::raytrace::fallback::trace)
PipelineCompute eevee_ray_trace_planar(eevee::raytrace::planar::trace)
ScreenTraceHitData raytrace_screen(ViewMatrices view, RayTraceData rt_data, HiZData hiz_data, sampler2D hiz_tx, float stride_rand, float roughness, const bool allow_self_intersection, Ray ray)
Ray raytrace_thickness_ray_amend(Ray ray, ClosureUndetermined cl, float3 V, Thickness thickness)
ScreenTraceHitData raytrace_planar(ViewMatrices view, RayTraceData rt_data, sampler2DArrayDepth planar_depth_tx, PlanarProbeData planar, float stride_rand, Ray ray)
float3 raytrace_sample_screen(ViewMatrices view, sampler2D radiance_tx, RayTraceData raytrace, ScreenTraceHitData hit, float roughness, float2 ss_hit_P)
@ SFLOAT_16_16_16_16
#define metal_max_total_threads_per_threadgroup(count)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define work_group_id
#define resource_table
#define sampler(slot)
#define specialization_constant(default_value)
#define local_invocation_id
#define compute
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
#define abs
bool all(VecOp< bool, D >) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
ImageBase< float, 2 > image2D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
#define imageLoadFast
#define imageStoreFast
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
float2 project_point(float3x3 mat, float2 point)
uint2 unpackUvec2x16(uint a)
ClosureType
float3 brightness_clamp_max(float3 color, float limit)
void trace(Resources &srt, const LightprobeRenderData &lightprobes, const gbuffer::Reader &reader, const Uniform &uni, const draw::View &views, const uint3 group_id, const uint3 local_id)
void trace(Resources &srt, const LightprobeRenderData &lightprobes, const LightprobePlaneRenderData &lightprobe_planes, const Uniform &uni, const draw::View &views, const Sampling &sampling, const gbuffer::Reader &reader, const uint3 group_id, const uint3 local_id)
void trace(Resources &srt, const LightprobeRenderData &lightprobes, const Uniform &uni, const draw::View &views, const Sampling &sampling, const gbuffer::Reader &reader, const uint3 group_id, const uint3 local_id)
ClosureType mode_to_closure_type(uint mode)
float interleaved_gradient(float2 pixel, float seed, float offset)
T read(T depth_buffer_value)
SphericalHarmonicL1< float4 > clamp_energy(SphericalHarmonicL1< float4 > sh, float clamp_value)
float max_time
packed_float3 direction
packed_float3 origin
float near() const
float3 point_screen_to_world(float3 ssP) const
float far() const
float3 world_incident_vector(float3 P) const
SphericalHarmonicL1< float4 > volume_irradiance
float w
Definition sky_math.h:225
GBufferMode bin_type(uchar bin) const
CCL_NAMESPACE_BEGIN struct Window V