22#include "gpu_shader_codegen_lib.glsl"
29 float near_dist = view.
near();
67 const bool allow_self_intersection,
71 if (view.
winmat[3][3] == 0.0f) {
79 if (!allow_self_intersection && ssray.
max_time < 1.1f) {
93 float prev_delta = 0.0f, prev_time = 0.0f;
95 float delta = depth_sample - ssray.
origin.z;
100 float prev_ray_z = ssray.
origin.z;
103 float t = 1.001f, time = 1.001f;
105 constexpr int max_steps = 255;
106 for (
int iter = 1; !hit && (time < ssray.
max_time) && (iter < max_steps); iter++) {
107 float stride = 1.0f +
float(iter) * rt_data.quality;
108 float lod =
log2(stride) * lod_fac;
113 time =
min(t + stride * stride_rand, ssray.
max_time);
117 depth_sample =
textureLod(hiz_tx, ss_ray_P.xy * hiz_data.uv_scale,
floor(lod)).r;
120 float sample_ndc_min_thickness = rt_data.ray_thickness.pixel_depth_thickness_at(sample_view_Z);
122 delta = depth_sample - ss_ray_P.
z;
124 depth_sample, time, sample_ndc_min_thickness, ss_ray_P.
z, prev_ray_z);
125 prev_ray_z = ss_ray_P.
z;
128 hit = hit && (depth_sample != 1.0f);
131 time =
mix(prev_time, time,
saturate(prev_delta / (prev_delta - delta)));
136 const float backface_epsilon = -1e-7f;
137 result.hit_backface = prev_delta < backface_epsilon;
143 result.valid = hit &&
144 (
textureLod(hiz_tx, result.ss_hit_P.xy * hiz_data.uv_scale, 0.0f).r != 1.0f);
150 RayTraceData rt_data,
152 PlanarProbeData planar,
157 if (view.
winmat[3][3] == 0.0f) {
165 float prev_delta = 0.0f, prev_time = 0.0f;
168 float delta = depth_sample - ssray.
origin.z;
170 float t = 0.0f, time = 0.0f;
172 constexpr int max_steps = 32;
173 for (
int iter = 1; !hit && (time < ssray.
max_time) && (iter < max_steps); iter++) {
174 float stride = 1.0f +
float(iter) * rt_data.quality;
179 time =
min(t + stride * stride_rand, ssray.
max_time);
186 delta = depth_sample - ss_ray.
z;
188 hit = (delta < 0.0f);
191 hit = hit && (depth_sample != 1.0f);
194 time =
mix(prev_time, time,
saturate(prev_delta / (prev_delta - delta)));
201 textureLod(planar_depth_tx,
float3(result.ss_hit_P.xy, planar.layer_id), 0.0f).r != 0.0;
221 case CLOSURE_BSDF_MICROFACET_GGX_REFLECTION_ID:
232 const float max_distance,
233 const float4 frustum_planes[6])
236 float max_t = max_distance;
238 for (
int i = 0; i < 6; i++) {
241 const float3 plane_normal = -frustum_planes[i].xyz;
242 const float plane_distance = frustum_planes[i].
w;
244 const float NoR =
dot(plane_normal, direction);
245 const float NoS =
dot(plane_normal, start);
247 if (
abs(NoR) < 1e-6f) {
249 if (NoS > plane_distance) {
256 const float plane_t = (plane_distance - NoS) / NoR;
260 max_t =
min(max_t, plane_t);
264 min_t =
max(min_t, plane_t);
268 end = start + direction * max_t;
269 start = start + direction * min_t;
271 return max_t > min_t;
287 const float3 vs_direction,
289 const RayTraceData rt_data,
293 const uint object_id,
304 const float2 total_pixel_delta =
abs(start.xy - end.xy) * extent;
306 int steps = int(
max(total_pixel_delta.
x, total_pixel_delta.
y)) + 1;
308 steps =
min(steps, max_steps);
311 const float3 delta = (end - start) /
float(steps);
313 const float max_t =
max(steps - 1, 1);
314 float previous_step_z = start.
z;
319 for (
int i = 1; i < steps || i == 1; i++) {
321 const float step_t =
min(
float(i) + jitter, max_t);
325 if (object_id != 0 && object_id !=
texelFetch(ob_id_tx,
int2(texel), 0).r) {
326 thickness_estimator.
thickness(1.0f, step_t, 0.0f);
327 previous_step_z =
step.z;
336 const float2 gather_uv =
round(texel) * texel_to_uv;
337 const float2 bilinear_coords =
fract(texel - 0.5f);
339 const float hit_depth_point =
mix(
mix(depth4.
w, depth4.
z, bilinear_coords.
x > 0.5f),
340 mix(depth4.
x, depth4.
y, bilinear_coords.
x > 0.5f),
341 bilinear_coords.
y > 0.5f);
342 const float hit_depth_linear =
mix(
mix(depth4.
w, depth4.
z, bilinear_coords.
x),
343 mix(depth4.
x, depth4.
y, bilinear_coords.
x),
345 const float hit_min_z =
min(hit_depth_point, hit_depth_linear);
346 const float hit_max_z =
max(hit_depth_point, hit_depth_linear);
349 const float sample_ndc_min_thickness = rt_data.ray_thickness.pixel_depth_thickness_at(
353 float sample_thickness = thickness_estimator.
thickness(
354 hit_depth_point, step_t, sample_ndc_min_thickness);
358 sample_thickness +=
abs(
step.z - previous_step_z);
360 const float sample_min = hit_max_z;
361 const float sample_max = hit_min_z + sample_thickness;
362 const bool hit =
step.z >= sample_min &&
step.z <= sample_max;
364 previous_step_z =
step.z;
371 return dot(vs_hit_point - vs_origin, vs_direction);
385 RayTraceData raytrace,
392 float shading_cone_tangent =
saturate(roughness * 0.05);
393 float vs_footprint = shading_cone_tangent * hit.
time;
396 float ndc_footprint = (vs_footprint * view.
winmat[0][0]) / hs_hit_P.
w;
397 float pixel_footprint = ndc_footprint * raytrace.full_resolution.x;
401 if (pixel_footprint < 1.0f) {
402 radiance =
textureLod(radiance_tx, ss_hit_P, 0.0f).rgb;
405 float kernel_radius =
saturate(pixel_footprint - 1.0f);
406 float4 ofs =
float2(kernel_radius, -kernel_radius).xxyy * raytrace.full_resolution_inv.xyxy;
410 radiance =
log2(1.0f +
textureLod(radiance_tx, ss_hit_P + ofs.xy, 0.0f).rgb);
411 radiance +=
log2(1.0f +
textureLod(radiance_tx, ss_hit_P + ofs.xw, 0.0f).rgb);
412 radiance +=
log2(1.0f +
textureLod(radiance_tx, ss_hit_P + ofs.zy, 0.0f).rgb);
413 radiance +=
log2(1.0f +
textureLod(radiance_tx, ss_hit_P + ofs.zw, 0.0f).rgb);
415 radiance =
exp2(radiance) - 1.0f;
426 RayTraceData raytrace,
434 float shading_cone_tangent =
saturate(roughness * 0.05);
435 float vs_footprint = shading_cone_tangent * hit.
time;
438 float ndc_footprint = (vs_footprint * view.
winmat[0][0]) / hs_hit_P.
w;
439 float pixel_footprint = ndc_footprint * raytrace.full_resolution.x;
443 if (pixel_footprint < 1.0f) {
447 float kernel_radius =
saturate(pixel_footprint - 1.0f);
448 float4 ofs =
float2(kernel_radius, -kernel_radius).xxyy * raytrace.full_resolution_inv.xyxy;
457 radiance =
exp2(radiance) - 1.0f;
Ray bxdf_translucent_ray_amend(ClosureUndetermined cl, float3, Ray ray, Thickness thickness)
Ray bxdf_ggx_ray_amend_transmission(ClosureUndetermined cl, float3 V, Ray ray, Thickness thickness)
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)
bool clip_ray(float3 &start, float3 &end, const float3 direction, const float max_distance, const float4 frustum_planes[6])
float raytrace_screen_2(ViewMatrices view, const float3 vs_origin, const float3 vs_end, const float3 vs_direction, sampler2D depth_tx, const RayTraceData rt_data, const int max_steps, const float jitter, usampler2D ob_id_tx, const uint object_id, float2 &r_hit_uv)
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)
void raytrace_clip_ray_to_near_plane(ViewMatrices view, Ray &ray)
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
SamplerBase< float, 2, false, true, false, true > sampler2DArrayDepth
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureGather(T, FltCoord, int=0) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2, false, true > sampler2DArray
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
SamplerBase< uint, 2 > usampler2D
@ CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID
@ CLOSURE_BSDF_DIFFUSE_ID
@ CLOSURE_BSSRDF_BURLEY_ID
@ CLOSURE_BSDF_THIN_GLASS_TRANSMISSION_ID
@ CLOSURE_BSDF_TRANSLUCENT_ID
VecBase< float, 3 > float3
T read(T depth_buffer_value)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static ScreenSpaceRay create(ViewMatrices view, Ray ray, float2 pixel_size)
float3 screen_position_at(float t)
static ScreenThicknessEstimator init(float start_z)
float thickness(float sample_ss_z, float sample_ss_t, float sample_min_thickness)
bool intersect(float sample_ss_z, float sample_ss_t, float sample_min_thickness, float ray_ss_z, float ray_ss_z_prev)
float depth_screen_to_view(float ss_depth) const
float3 point_screen_to_world(float3 ssP) const
T screen_to_ndc(T ss_P) const
float depth_view_to_screen(float v_depth) const
float3 point_view_to_screen(float3 vP) const
float3 point_screen_to_view(float3 ssP) const
CCL_NAMESPACE_BEGIN struct Window V