45 constexpr int bitmask_len = 32;
48 uint a = uint(
floor(
float(bitmask_len) * ratio.
x));
50 uint
b = uint(
ceil(
float(bitmask_len) * (ratio.
y - ratio.
x)));
52 return (((
b < 32u) ? 1u <<
b : 0u) - 1u) << a;
57 constexpr int bitmask_len = 32;
82 constexpr int bitmask_len = 32;
83 float visibility = 0.0f;
84 for (
int bit = 0; bit < bitmask_len; bit++) {
87 if (((bitmask >> bit) & 1u) == 0u) {
96 constexpr float4 weights =
float4(0.0095061f, 0.0270951f, 0.0405571f, 0.0478421f);
97 constexpr uint4 masks =
uint4(0xF000000Fu, 0x0F0000F0u, 0x00F00F00u, 0x000FF000u);
113 float N_sin =
dot(N_proj,
T);
114 float N_cos =
dot(N_proj,
V);
152 return textureLod(screen_radiance_tx, uv, lod).rgb;
157 return textureLod(screen_normal_tx, uv, lod).rgb * 2.0f - 1.0f;
177template<
typename ResultT>
180 ScreenThicknessParameters thickness_params,
188 float search_distance,
190 const int slice_count,
191 const int sample_count,
198 if (slice_count <= 2) {
203 float weight_accum = 0.0f;
204 float occlusion_accum = 0.0f;
207#if defined(GPU_METAL)
209# pragma clang loop unroll(full)
211 for (
int slice = 0; slice < slice_count; slice++) {
212 if (slice_count > 2) {
222 uint slice_bitmask = 0u;
232 vN_angle += (
noise.z - 0.5f) * (
M_PI / 32.0f) * angle_bias;
239 for (
int side = 0; side < 2; side++) {
251#if defined(GPU_METAL) && defined(GPU_APPLE)
253# pragma clang loop unroll(full)
255 for (
int j = 0; j < sample_count; j++) {
271 if (sample_depth == 1.0f && !reversed) {
277 float sample_ndc_min_thickness = thickness_params.pixel_depth_thickness_at(sample_view_z);
279 float sample_thickness = thickness_estimator.
thickness(
280 sample_depth, time, sample_ndc_min_thickness);
283 constexpr float bias = 2.0f * 2.4e-7f;
284 const float sample_depth_front = sample_depth + (reversed ? -bias : bias);
285 const float sample_depth_back = sample_depth + (reversed ? 0.0 : sample_thickness);
294 float3 ls_P_front = (vP_sample_front - vP) / search_distance;
295 float3 ls_P_back = (vP_sample_back - vP) / search_distance;
299 ls_P_front.
z =
clamp(ls_P_front.
z, -max_dist, max_dist);
300 ls_P_back.
z =
clamp(ls_P_back.
z, -max_dist, max_dist);
301 if (ls_P_front.
z == ls_P_back.
z) {
309 float LV_front =
dot(vL_front, vV);
310 float LV_back =
dot(vL_back, vV);
316 if (sample_depth == 1.0f) {
323 theta.
y =
max(theta.
x, theta.
y);
330 theta = (side == 0) ? theta.xy : -theta.yx;
334 float lod_data = lod - uni.
raytrace_buf.fast_gi_lod_bias;
343 float facing =
dot(normal, -vL_front);
347 float facing_weight =
abs(facing);
350 float2 biased_theta = (theta - vN_angle) * angle_bias;
354 radiance *= facing_weight * weight_bitmask;
357 slice_bitmask |= sample_bitmask;
363 occlusion_accum += occlusion_slice * vN_length;
368 weight_accum += vN_length;
374 float weight_rcp =
safe_rcp(weight_accum);
377 float sphere_weight = 2.0f *
M_TAU;
386 float gi_weight_factor = 1.0f / ((3.0f / 2.0f) -
cos(angle_bias *
M_PI_2));
388 sphere_weight = weight_rcp * sphere_weight;
393 occlusion_accum *= weight_rcp;
399 ScreenThicknessParameters,
415 ScreenThicknessParameters,
450 is_processed = (normal.
w != 0.0f);
451 return views.
get(0).normal_view_to_world(normal.xyz * 2.0f - 1.0f);
462 int2 sample_offset)
const
464 int2 sample_texel_fullres = sample_texel * uni.
raytrace_buf.fast_gi_resolution_scale +
466 float sample_depth =
texelFetch(hiz_tx, sample_texel_fullres, 0).r;
483 return max(1e-6f, depth_weight * spatial_weight * normal_weight);
492 int2 sample_offset)
const
494 int2 sample_texel = center_texel + sample_offset;
495 int2 sample_texel_fullres = sample_texel * uni.
raytrace_buf.fast_gi_resolution_scale +
497 float2 sample_uv = (
float2(sample_texel_fullres) + 0.5f) *
516 float epsilon_weight = 1e-4f;
518 return max(epsilon_weight, depth_weight * normal_weight);
594 int2 texel_fullres = texel * uni.raytrace_buf.fast_gi_resolution_scale +
595 uni.raytrace_buf.fast_gi_resolution_bias;
599 texel_fullres =
min(texel_fullres, extent - 1);
616 vN =
float3(0.0f, 0.0f, 1.0f);
620 float3 vN_unit = vN * 0.5f + 0.5f;
623 srt.neigbhor_data[local_id.y][local_id.x] = vN_unit;
630 constexpr uint lod = 1u;
631 if (
all(
equal(local_id.xy & ((1u << lod) - 1u),
uint2(0)))) {
632 uint stride = (1u << (lod - 1u));
633 float3 normal_avg = (srt.neigbhor_data[local_id.y][local_id.x] +
634 srt.neigbhor_data[local_id.y + stride][local_id.x] +
635 srt.neigbhor_data[local_id.y][local_id.x + stride] +
636 srt.neigbhor_data[local_id.y + stride][local_id.x + stride]) *
639 srt.neigbhor_data[local_id.y][local_id.x] = normal_avg;
644 constexpr uint lod = 2u;
645 if (
all(
equal(local_id.xy & ((1u << lod) - 1u),
uint2(0)))) {
646 uint stride = (1u << (lod - 1u));
647 float3 normal_avg = (srt.neigbhor_data[local_id.y][local_id.x] +
648 srt.neigbhor_data[local_id.y + stride][local_id.x] +
649 srt.neigbhor_data[local_id.y][local_id.x + stride] +
650 srt.neigbhor_data[local_id.y + stride][local_id.x + stride]) *
653 srt.neigbhor_data[local_id.y][local_id.x] = normal_avg;
658 constexpr uint lod = 3u;
659 if (
all(
equal(local_id.xy & ((1u << lod) - 1u),
uint2(0)))) {
660 uint stride = (1u << (lod - 1u));
661 float3 normal_avg = (srt.neigbhor_data[local_id.y][local_id.x] +
662 srt.neigbhor_data[local_id.y + stride][local_id.x] +
663 srt.neigbhor_data[local_id.y][local_id.x + stride] +
664 srt.neigbhor_data[local_id.y + stride][local_id.x + stride]) *
682 srt.neigbhor_data[local_id.y][local_id.x] = radiance.rgb;
687 constexpr uint lod = 1u;
688 if (
all(
equal(local_id.xy & ((1u << lod) - 1u),
uint2(0)))) {
689 uint stride = (1u << (lod - 1u));
690 float3 radiance_avg = (srt.neigbhor_data[local_id.y][local_id.x] +
691 srt.neigbhor_data[local_id.y + stride][local_id.x] +
692 srt.neigbhor_data[local_id.y][local_id.x + stride] +
693 srt.neigbhor_data[local_id.y + stride][local_id.x + stride]) *
696 srt.neigbhor_data[local_id.y][local_id.x] = radiance_avg;
701 constexpr uint lod = 2u;
702 if (
all(
equal(local_id.xy & ((1u << lod) - 1u),
uint2(0)))) {
703 uint stride = (1u << (lod - 1u));
704 float3 radiance_avg = (srt.neigbhor_data[local_id.y][local_id.x] +
705 srt.neigbhor_data[local_id.y + stride][local_id.x] +
706 srt.neigbhor_data[local_id.y][local_id.x + stride] +
707 srt.neigbhor_data[local_id.y + stride][local_id.x + stride]) *
710 srt.neigbhor_data[local_id.y][local_id.x] = radiance_avg;
715 constexpr uint lod = 3u;
716 if (
all(
equal(local_id.xy & ((1u << lod) - 1u),
uint2(0)))) {
717 uint stride = (1u << (lod - 1u));
718 float3 radiance_avg = (srt.neigbhor_data[local_id.y][local_id.x] +
719 srt.neigbhor_data[local_id.y + stride][local_id.x] +
720 srt.neigbhor_data[local_id.y][local_id.x + stride] +
721 srt.neigbhor_data[local_id.y + stride][local_id.x + stride]) *
760 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
762 int2 texel =
int2(local_id.xy + tile_coord * tile_size);
764 int2 texel_fullres = texel * uni.raytrace_buf.fast_gi_resolution_scale +
765 uni.raytrace_buf.fast_gi_resolution_bias;
772 texel_fullres =
min(texel_fullres, extent - 1);
775 if (
texelFetch(reader.gbuf_header_tx,
int3(texel_fullres, 0), 0).r == 0u) {
787 float2 uv = (
float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
798 uni.raytrace_buf.fast_gi_thickness,
800 srt.screen_radiance_tx,
801 srt.screen_normal_tx,
805 uni.uniform_buf.ao.pixel_size,
806 uni.uniform_buf.ao.gi_distance,
807 uni.uniform_buf.ao.angle_bias,
808 constants.slice_count,
809 constants.step_count,
813 sh_out.write(texel, result);
833 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
835 int2 texel =
int2(local_id.xy + tile_coord * tile_size);
838 int2 texel_fullres = texel * uni.raytrace_buf.fast_gi_resolution_scale +
839 uni.raytrace_buf.fast_gi_resolution_bias;
842 float center_depth =
texelFetch(
hiz.hiz_tx, texel_fullres, 0).r;
844 float3 center_P = views.get(0).point_screen_to_world(
float3(center_uv, center_depth));
845 float3 center_N = sh_in.sample_normal_get(views, texel, is_valid);
858 float accum_weight = 0.0f;
860 for (
int y = -1;
y <= 1;
y++) {
861 for (
int x = -1;
x <= 1;
x++) {
863 int2 sample_texel = texel + sample_offset;
864 float2 sample_uv = (
float2(sample_texel) + 0.5f) * texel_size;
865 float sample_weight = sh_in.sample_weight_get(
866 uni, views,
hiz.hiz_tx, center_N, center_P, sample_texel, sample_uv, sample_offset);
869 if (sample_weight > 0.0f) {
872 accum_weight += sample_weight;
877 sh_out.write(texel, accum_sh);
904 constexpr uint tile_size = uint(RAYTRACE_GROUP_SIZE);
906 int2 texel_fullres =
int2(local_id.xy + tile_coord * tile_size);
908 int2 texel =
max(
int2(0), texel_fullres - uni.raytrace_buf.fast_gi_resolution_bias) /
909 uni.raytrace_buf.fast_gi_resolution_scale;
923 float2 center_uv = (
float2(texel_fullres) + 0.5f) * uni.raytrace_buf.full_resolution_inv;
929 if (uni.raytrace_buf.fast_gi_resolution_scale == 1) {
930 accum_sh = sh_in.load_sh(texel,
true);
933 float2 interp =
float2(texel_fullres - texel * uni.raytrace_buf.fast_gi_resolution_scale -
934 uni.raytrace_buf.fast_gi_resolution_bias) /
935 float2(uni.raytrace_buf.fast_gi_resolution_scale);
937 float4 bilinear_weight = interp4.zxzx * interp4.wwyy;
940 bilateral_weights.
x = sh_in.sample_weight_get(
941 uni, views, center_N, center_P, texel,
int2(0, 0));
942 bilateral_weights.
y = sh_in.sample_weight_get(
943 uni, views, center_N, center_P, texel,
int2(1, 0));
944 bilateral_weights.
z = sh_in.sample_weight_get(
945 uni, views, center_N, center_P, texel,
int2(0, 1));
946 bilateral_weights.
w = sh_in.sample_weight_get(
947 uni, views, center_N, center_P, texel,
int2(1, 1));
949 float4 weights = bilateral_weights * bilinear_weight;
973 float clamp_indirect = uni.uniform_buf.clamp.surface_indirect;
982 for (
int i = 0; i < 3 ; i++) [[
unroll]] {
983 if (i < closure_count) {
984 ClosureUndetermined cl = gbuf.
layer[i];
988 float mix_fac =
saturate(roughness * uni.raytrace_buf.roughness_mask_scale -
989 uni.raytrace_buf.roughness_mask_bias);
990 bool use_raytrace = mix_fac < 1.0f;
991 bool use_fast_gi = mix_fac > 0.0f;
1000 float3 radiance = radiance_with_visibility.xyz;
1008 float distant_radiance_visibility =
saturate(radiance_with_visibility.
w / 3.0f);
1010 if (closure_has_transmission(cl.type)) {
1013 distant_radiance_visibility = 1.0f;
1020 uchar layer_index = bin_indices[i];
1026 if (layer_index == 0u) {
1027 radiance_raytrace =
imageLoad(srt.closure0_img, texel_fullres);
1029 else if (layer_index == 1u) {
1030 radiance_raytrace =
imageLoad(srt.closure1_img, texel_fullres);
1032 else if (layer_index == 2u) {
1033 radiance_raytrace =
imageLoad(srt.closure2_img, texel_fullres);
1036 float4 radiance_mixed =
mix(radiance_raytrace, radiance_fast_gi, mix_fac);
1039 if (layer_index == 0u) {
1040 imageStore(srt.closure0_img, texel_fullres, radiance_mixed);
1042 else if (layer_index == 1u) {
1043 imageStore(srt.closure1_img, texel_fullres, radiance_mixed);
1045 else if (layer_index == 2u) {
1046 imageStore(srt.closure2_img, texel_fullres, radiance_mixed);
1097 if (depth == 1.0f) {
1107 float3 vN = view.normal_world_to_view(
N);
1114 uni.raytrace_buf.fast_gi_thickness,
1121 uni.uniform_buf.ao.pixel_size,
1122 uni.uniform_buf.ao.distance,
1123 uni.uniform_buf.ao.angle_bias,
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
float closure_apparent_roughness_get(ClosureUndetermined cl)
LightProbeRay bxdf_lightprobe_ray(ClosureUndetermined cl, float3, float3 V, Thickness thickness)
#define RAYTRACE_RADIANCE_FORMAT
#define UTIL_BLUE_NOISE_LAYER
#define AMBIENT_OCCLUSION_PASS_TILE_SIZE
float2 sample_circle(float rand)
#define metal_max_total_threads_per_threadgroup(count)
#define storage(slot, qualifiers)
#define image(slot, qualifiers, format)
#define specialization_constant(default_value)
#define global_invocation_id
#define local_invocation_id
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
bool all(VecOp< bool, D >) RET
VecBase< int, D > bitCount(VecOp< int, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
IMG_TEMPLATE DataVec imageLoad(const T &, IntCoord) RET
ImageBase< float, 2, true > image2DArray
ImageBase< float, 2 > image2D
IMG_TEMPLATE SizeVec imageSize(const T &) RET
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
TEX_TEMPLATE DataVec textureLod(T, FltCoord, float) RET
SamplerBase< float, 2, false, false, false, true > sampler2DDepth
SamplerBase< float, 2 > sampler2D
TEX_TEMPLATE SizeVec textureSize(T, int) RET
VecT normalize_and_get_length(VecT vector, float &out_length)
float length_squared(VecT a)
float reduce_add(float2 a)
uint2 unpackUvec2x16(uint a)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles
ccl_device_inline float interp(const float a, const float b, const float t)
float3 brightness_clamp_max(float3 color, float limit)
float bitmask_to_occlusion_uniform(uint bitmask)
void resolve(const uint3 group_id, const uint3 local_id, const Tiles &tiles, const SampleInput &sh_in, Resolve &srt, const gbuffer::Reader &reader, const Uniform &uni, const draw::View &views, const LightprobeRenderData &lightprobes)
float3 sample_radiance(sampler2D, float2, float)
float bitmask_to_occlusion_cosine(uint bitmask)
void denoise(const uint3 group_id, const uint3 local_id, SampleInput &sh_in, SampleOutput &sh_out, const HiZ &hiz, const Uniform &uni, const draw::View &views, Tiles &tiles)
uint angles_to_bitmask(float2 theta)
void projected_normal_to_plane_angle_and_length(float3 N, float3 V, float3 T, float3 B, float &N_proj_len, float &N_angle)
float bitmask_to_visibility_uniform(uint bitmask)
template float eval< float >(const Uniform &, const ViewMatrices, ScreenThicknessParameters, sampler2D, sampler2D, sampler2D, float3, float3, float4, float2, float, float, int, int, bool, bool)
void setup(const uint3 global_id, const uint3 local_id, const gbuffer::Reader &reader, const Uniform &uni, const draw::View &views, Setup &srt)
T select_result(float, SphericalHarmonicL1< float4 >)
ResultT eval(const Uniform &uni, const ViewMatrices view, ScreenThicknessParameters thickness_params, sampler2D hiz_tx, sampler2D screen_radiance_tx, sampler2D screen_normal_tx, float3 vP, float3 vN, float4 noise, float2 pixel_size, float search_distance, float angle_bias, const int slice_count, const int sample_count, const bool reversed, const bool ao_only)
void scan(const uint3 group_id, const uint3 local_id, Scan &srt, const Tiles &tiles, const Sampling &sampling, const UtilityTexture &util_tx, const HiZ &hiz, const gbuffer::Reader &reader, const Uniform &uni, const draw::View &views, SampleOutput &sh_out, Constants &constants)
float3 sample_normal(sampler2D, float2, float)
PipelineCompute ambient_occlusion_pass(occlusion_pass)
PipelineCompute fast_gi_setup(fast_gi::setup)
PipelineCompute fast_gi_resolve(fast_gi::resolve)
void occlusion_pass(const uint3 global_id, const Sampling &sampling, const UtilityTexture &util_tx, const Uniform &uni, const draw::View &views, const HiZ &hiz, AOPass &srt)
PipelineCompute fast_gi_denoise(fast_gi::denoise)
PipelineCompute fast_gi_scan(fast_gi::scan)
float gaussian_factor(float linear_distance, float standard_deviation)
float angle_weight(float3 center_N, float3 sample_N)
float gaussian_weight(float factor, float square_distance)
float planar_weight(float3 plane_N, float3 plane_P, float3 P, float scale)
T read(T depth_buffer_value)
SphericalHarmonicL1< float4 > mul(SphericalHarmonicL1< float4 > a, float b)
SphericalHarmonicL1< float4 > clamp_energy(SphericalHarmonicL1< float4 > sh, float clamp_value)
SphericalHarmonicL1< float4 > rotate(float3x3 rotation, SphericalHarmonicL1< float4 > sh)
SphericalHarmonicL1< float4 > compress(SphericalHarmonicL1< float4 > sh)
SphericalHarmonicL1< float4 > madd(SphericalHarmonicL1< float4 > a, float b, SphericalHarmonicL1< float4 > c)
SphericalHarmonicL1< float4 > decompress(SphericalHarmonicL1< float4 > sh)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
packed_float3 dominant_direction
static ScreenSpaceRay create(ViewMatrices view, Ray ray, float2 pixel_size)
static ScreenThicknessEstimator init(float start_z)
float thickness(float sample_ss_z, float sample_ss_t, float sample_min_thickness)
T evaluate_lambert(float3 N) const
spherical_harmonics::BandL1< T > L1
spherical_harmonics::BandL0< T > L0
void encode_signal_sample(float3 direction, T amplitude)
float depth_screen_to_view(float ss_depth) const
T ndc_to_screen(T ndc_P) const
float3 point_screen_to_world(float3 ssP) const
float3 view_incident_vector(float3 vP) const
float3 world_incident_vector(float3 P) const
float3 point_screen_to_view(float3 ssP) const
ViewMatrices get(uint view_id) const
const int out_ao_img_layer_index
image2DArray in_normal_img
const int in_normal_img_layer_index
SphericalHarmonicL1< float4 > volume_irradiance
void write(int2 texel, SphericalHarmonicL1< float4 > result)
const sampler2D screen_radiance_tx
const sampler2D screen_normal_tx
float3 neigbhor_data[RAYTRACE_GROUP_SIZE][RAYTRACE_GROUP_SIZE]
image2D out_radiance_mip2
image2D out_radiance_mip3
const sampler2DDepth depth_tx
const sampler2D in_radiance_tx
image2D out_radiance_mip1
image2D out_radiance_mip0
const uint(& tiles_coord_buf)[]
ClosureUndetermined layer[GBUFFER_LAYER_MAX]
CCL_NAMESPACE_BEGIN struct Window V