147 uint2 tile_coord =
tiles.tile_coord(work_group.x);
151 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
152 int2 texel_fullres =
int2(local_id.xy + tile_coord * tile_size);
155 int2 texel_shifted =
max(
int2(0), texel_fullres - uni.raytrace_buf.trace_pixel_offset);
156 int2 texel_nearest = texel_shifted / srt.raytrace_resolution_scale;
159 if (srt.skip_denoise) {
160 if (srt.raytrace_resolution_scale == 1) {
163 srt.out_radiance_img, texel_fullres,
imageLoad(srt.ray_radiance_img, texel_fullres));
168 float center_depth =
texelFetch(srt.depth_tx, texel_fullres, 0).r;
169 float2 center_uv =
float2(texel_fullres) * uni.raytrace_buf.full_resolution_inv;
170 float3 center_N = reader.read_bin(texel_fullres, srt.closure_index).N;
176 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest +
int2(0, 1)),
177 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest +
int2(1, 1)),
178 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest +
int2(1, 0)),
179 srt.sample_weight_get(reader, uni, view, center_N, center_P, texel_nearest +
int2(0, 0)));
192 float4 weights = ray_validity * bilinear_weights * bilateral_weights;
202 imageStore(srt.out_radiance_img, texel_fullres, radiance);
208 for (
int x = -1;
x <= 1;
x++) {
209 for (
int y = -1;
y <= 1;
y++) {
210 if (
x == 0 &&
y == 0) {
219 int3 sample_tile =
int3(tile_coord_neighbor, srt.closure_index);
221 uint tile_mask =
imageLoadFast(srt.tile_mask_img, sample_tile).r;
222 bool tile_is_unused = !
flag_test(tile_mask, 1u << 0u);
223 if (tile_is_unused) {
224 int2 texel_fullres_neighbor = texel_fullres +
int2(
x,
y) * int(tile_size);
227 srt.invalid_pixel_write(texel_fullres_neighbor);
235 ClosureUndetermined closure = reader.read_bin(texel_fullres, srt.closure_index);
238 srt.invalid_pixel_write(texel_fullres);
245 int2 center_sample_texel = texel_nearest;
246 if (srt.raytrace_resolution_scale != 1) {
255 float2 uv = (
float2(center_sample_texel) + 0.5f) * uni.raytrace_buf.full_resolution_inv *
256 float(srt.raytrace_resolution_scale);
260 float scene_z = vs_P.
z;
272 float filter_size_factor =
saturate(apparent_roughness * 8.0f);
273 uint sample_count = 1u + uint(
floor(15.0f * filter_size_factor + 0.5f));
274 float filter_radius = 8.0f *
sqrt(filter_size_factor);
277 float max_filter_radius =
float(RAYTRACE_GROUP_SIZE - 1);
278 float min_filter_radius = 0.0f;
279 if (srt.raytrace_resolution_scale > 1) {
281 min_filter_radius = 1.5f;
282 sample_count =
max(sample_count, 5u);
287 float weight_accum = 0.0f;
288 float closest_hit_time = 1.0e10f;
292 float3 vs_N = view.normal_world_to_view(closure.N);
293 float2 pixel_uv_size = uni.raytrace_buf.full_resolution_inv *
294 float(srt.raytrace_resolution_scale);
300 dPdxy[0] = view.normal_view_to_world(dPdxy[0]);
301 dPdxy[1] = view.normal_view_to_world(dPdxy[1]);
304 float bias =
max(0.2f,
saturate((0.75f - apparent_roughness) / (0.75f - 0.3f)));
315 float aspect = 1.0f - 0.5f *
saturate(
min(apparent_roughness * 12.0f,
316 2.0f - apparent_roughness * 5.0f));
318 filter_rotation[0] *=
clamp(filter_radius, min_filter_radius, max_filter_radius);
319 filter_rotation[1] *=
clamp(filter_radius * aspect, min_filter_radius, max_filter_radius);
321 for (uint i = 0u; i < sample_count; i++) {
330 int2 sample_texel = center_sample_texel + offset;
333 float ray_time =
imageLoad(srt.ray_time_img, sample_texel).r;
336 float3 ray_direction = ray_data.xyz;
337 float ray_pdf_inv =
abs(ray_data.
w);
339 if (ray_pdf_inv == 0.0f) {
343 closest_hit_time =
min(closest_hit_time, ray_time);
355 float weight =
min(1e30f, pdf * ray_pdf_inv);
359 radiance_accum += log_radiance * weight;
360 weight_accum += weight;
363 rgb_moment +=
square(log_radiance) * weight;
365 float inv_weight =
safe_rcp(weight_accum);
366 radiance_accum *= inv_weight;
369 float3 rgb_mean = radiance_accum;
370 rgb_moment *= inv_weight;
373 float hit_variance =
reduce_max(rgb_variance);
569 uint2 tile_coord =
tiles.tile_coord(work_group.x);
573 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
574 int2 texel_fullres =
int2(local_id.xy + tile_coord * tile_size);
575 float2 uv = (
float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
579 for (
int x = -1;
x <= 1;
x++) {
580 for (
int y = -1;
y <= 1;
y++) {
581 if (
x == 0 &&
y == 0) {
590 int3 sample_tile =
int3(tile_coord_neighbor, srt.closure_index);
592 uint tile_mask =
imageLoadFast(srt.tile_mask_img, sample_tile).r;
593 bool tile_is_unused = !
flag_test(tile_mask, 1u << 0u);
594 if (tile_is_unused) {
595 int2 texel_fullres_neighbor = texel_fullres +
int2(
x,
y) * int(tile_size);
607 float in_variance =
imageLoadFast(srt.in_variance_img, texel_fullres).r;
617 LocalStatistics local = srt.local_statistics_get(texel_fullres, in_radiance);
625 float4 history_radiance = srt.radiance_history_sample(uni,
P, local);
627 float hit_depth =
imageLoadFast(srt.hit_depth_img, texel_fullres).r;
629 history_radiance += srt.radiance_history_sample(uni, P_hit, local);
631 history_radiance *=
safe_rcp(history_radiance.
w);
637 float mix_fac = (history_radiance.
w > 1e-3f) ? 0.97f : 0.0f;
639 mix_fac *=
mix(0.75f, 1.0f,
saturate(in_variance * 20.0f));
648 float2 history_variance = srt.variance_history_sample(uni, P_hit);
650 float mix_variance_fac = (history_variance.
y == 0.0f) ? 0.0f : 0.85f;
652 history_variance.
x =
clamp(history_variance.
x, 0.0f, 100.0f);
654 float out_variance =
sqrt(
693 uint2 tile_coord =
tiles.tile_coord(work_group.x);
697 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
698 int2 texel_fullres =
int2(local_id.xy + tile_coord * tile_size);
699 float2 center_uv = (
float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
704 ClosureUndetermined center_closure = reader.read_bin(texel_fullres, srt.closure_index);
712 float variance =
imageLoadFast(srt.in_variance_img, texel_fullres).r;
715 bool is_background = (center_depth == 0.0f);
716 bool is_smooth = (roughness < 0.05f);
717 bool is_low_variance = (variance < 0.1f);
720 float filter_size_factor =
saturate(roughness * 8.0f) *
saturate(variance * 2.0);
721 float filter_size =
mix(0.0f, 9.0f, filter_size_factor);
722 uint sample_count = uint(
mix(1.0f, 15.0f,
saturate(variance * 2.0)));
724 if (is_smooth || is_background || is_low_variance) {
732 noise += sampling.rng_2D_get(SAMPLING_RAYTRACE_W);
736 float accum_weight = 1.0f;
739 for (uint i = 0u; i < sample_count; i++) {
744 int2 sample_texel = texel_fullres + offset;
745 int3 sample_tile =
int3(sample_texel / RAYTRACE_GROUP_SIZE, srt.closure_index);
747 if (
imageLoad(srt.tile_mask_img, sample_tile).r == 0u) {
752 float2 sample_uv = (
float2(sample_texel) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
756 if (sample_depth == 0.0f) {
767 ClosureUndetermined sample_closure = reader.read_bin(sample_texel, srt.closure_index);
779 float weight = depth_weight * spatial_weight * normal_weight;
782 accum_weight += weight;