40 uint l_idx = global_id.x;
41 if (l_idx >= srt.light_cull_buf.items_count) {
45 LightData
light = srt.in_light_buf[l_idx];
48 if (is_sun_light(
light.type)) {
50 bool is_world_sun_light =
light.color.r < 0.0f;
51 if (is_world_sun_light) {
52 light.color = srt.sunlight_buf[l_idx].color;
53 light.object_to_world = srt.sunlight_buf[l_idx].object_to_world;
55 LightSunData sun_data =
light.sun;
56 sun_data.direction = srt.sunlight_buf[l_idx].object_to_world.z_axis();
61 srt.out_light_buf[srt.light_cull_buf.local_lights_len + l_idx] =
light;
66 if (
light.local.local.influence_radius_max < 1e-8f) {
72 case LIGHT_SPOT_SPHERE:
73 case LIGHT_SPOT_DISK: {
74 LightSpotData spot =
light.spot;
76 if (spot.spot_tan < 10.0f) {
82 light.position() - z_axis * spot.local.influence_radius_max,
83 x_axis * spot.local.influence_radius_max * spot.spot_tan / spot.spot_size_inv.
x,
84 y_axis * spot.local.influence_radius_max * spot.spot_tan / spot.spot_size_inv.
y);
85 if (!
culling.intersect_view(pyramid, 0)) {
93 case LIGHT_OMNI_SPHERE:
96 sphere.
radius =
light.local.local.influence_radius_max;
106 if (
culling.intersect_view(sphere, 0)) {
111 srt.out_zdist_buf[
index] = z_dist;
112 srt.out_key_buf[
index] = l_idx;
139 if (srt.light_cull_buf.visible_count == 0) {
143 const uint group_size = CULLING_SORT_GROUP_SIZE;
144 uint src_index = global_id.x;
145 bool valid_thread =
true;
147 if (src_index >= srt.light_cull_buf.visible_count) {
150 src_index = srt.light_cull_buf.visible_count - 1;
151 valid_thread =
false;
154 float local_zdist = srt.in_zdist_buf[src_index];
158 uint iter =
divide_ceil(srt.light_cull_buf.visible_count, group_size);
159 for (uint i = 0u; i < iter; i++) {
160 uint
index = group_size * i + local_id.x;
163 srt.zdists_cache[local_id.x] = srt.in_zdist_buf[
index];
168 uint line_end =
min(group_size, srt.light_cull_buf.visible_count - group_size * i);
169 for (uint j = 0u; j < line_end; j++) {
170 if (srt.zdists_cache[j] < local_zdist) {
173 else if (srt.zdists_cache[j] == local_zdist) {
175 if ((group_size * i + j) < src_index) {
186 uint input_index = srt.in_key_buf[src_index];
187 srt.out_light_buf[prefix_sum] = srt.in_light_buf[input_index];
212 constexpr uint zbin_iter = uint(CULLING_ZBIN_COUNT / CULLING_ZBIN_GROUP_SIZE);
213 const uint zbin_local = local_id.x * zbin_iter;
215 for (uint i = 0u, l = zbin_local; i < zbin_iter; i++, l++) {
216 srt.zbin_max[l] = 0x0u;
217 srt.zbin_min[l] = ~0x0u;
223 uint light_iter =
divide_ceil(srt.light_cull_buf.visible_count, uint(CULLING_ZBIN_GROUP_SIZE));
224 for (uint i = 0u; i < light_iter; i++) {
225 uint
index = i * CULLING_ZBIN_GROUP_SIZE + local_id.x;
226 if (
index >= srt.light_cull_buf.visible_count) {
232 float radius =
light.local.local.influence_radius_max;
235 srt.light_cull_buf.zbin_scale, srt.light_cull_buf.zbin_bias, z_dist + radius);
237 srt.light_cull_buf.zbin_scale, srt.light_cull_buf.zbin_bias, z_dist - radius);
238 z_min =
clamp(z_min, 0, CULLING_ZBIN_COUNT - 1);
239 z_max =
clamp(z_max, 0, CULLING_ZBIN_COUNT - 1);
241 for (
int z = z_min;
z <= z_max;
z++) {
249 for (uint i = 0u, l = zbin_local; i < zbin_iter; i++, l++) {
250 srt.out_zbin_buf[l] = (srt.zbin_max[l] << 16u) | (srt.zbin_min[l] & 0xFFFFu);
267 tile.bounds = (is_persp) ? tile_bound_cone(corners[0], corners[4], corners[7], corners[3]) :
268 tile_bound_cylinder(corners[0], corners[4], corners[7], corners[3]);
297 return ::intersect(this->frustum, bbox);
302 return ::intersect(this->frustum, pyramid);
315 float angle_cosine =
dot(center, v00);
316 angle_cosine =
min(angle_cosine,
dot(center, v01));
317 angle_cosine =
min(angle_cosine,
dot(center, v10));
318 angle_cosine =
min(angle_cosine,
dot(center, v11));
321 return float4(center, angle_cosine);
328 float3 center = (v00 + v01 + v10 + v11) * 0.25f;
347 constexpr float margin = 0.02f;
348 tile_co += margin * (offset * 2.0f - 1.0f);
364 corners[1].z = corners[5].z = corners[6].z = corners[2].z = -1.0f;
365 corners[0].z = corners[4].z = corners[7].z = corners[3].z = 1.0f;
367 for (
int i = 0; i < 8; i++) [[
unroll]] {
372 bool is_persp = view.
winmat[3][3] == 0.0f;
382 uint word_idx = global_id.x % srt.light_cull_buf.tile_word_len;
383 uint tile_idx = global_id.x / srt.light_cull_buf.tile_word_len;
384 uint2 tile_co =
uint2(tile_idx % srt.light_cull_buf.tile_x_len,
385 tile_idx / srt.light_cull_buf.tile_x_len);
387 if (tile_co.
y >= srt.light_cull_buf.tile_y_len) {
396 uint l_idx = word_idx * 32u;
397 uint l_end =
min(l_idx + 32u, srt.light_cull_buf.visible_count);
399 for (; l_idx < l_end; l_idx++) {
400 LightData
light = srt.light_buf[l_idx];
404 float3 v_right = view.normal_world_to_view(
light.x_axis());
405 float3 v_up = view.normal_world_to_view(
light.y_axis());
406 float3 v_back = view.normal_world_to_view(
light.z_axis());
407 float radius =
light.local.local.influence_radius_max;
409 if (srt.light_cull_buf.view_is_flipped) {
414 bool intersect_tile =
tile.intersect(view, sphere);
416 switch (
light.type) {
417 case LIGHT_SPOT_SPHERE:
418 case LIGHT_SPOT_DISK: {
419 LightSpotData spot =
light.spot;
421 if (spot.spot_tan < 10.0f) {
424 vP - v_back * radius,
425 v_right * radius * spot.spot_tan / spot.spot_size_inv.x,
426 v_up * radius * spot.spot_tan / spot.spot_size_inv.y);
427 intersect_tile = intersect_tile &&
tile.intersect(pyramid);
434 case LIGHT_ELLIPSE: {
435 float3 v000 = vP - v_right * radius - v_up * radius;
436 float3 v100 = v000 + v_right * (radius * 2.0f);
437 float3 v010 = v000 + v_up * (radius * 2.0f);
438 float3 v001 = v000 - v_back * radius;
440 intersect_tile = intersect_tile &&
tile.intersect(bbox);
447 if (intersect_tile) {
448 word |= 1u << (l_idx % 32u);
452 srt.out_light_tile_buf[global_id.x] = word;
545 color =
float4(0.0f, 1.0f, 0.0f, 1.0f);
548 frag_out.out_debug_color_add =
float4(color.rgb, 0.0f) * 0.2f;
549 frag_out.out_debug_color_mul = color;
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
IsectFrustum isect_frustum_setup(Frustum shape)
Sphere shape_sphere(float3 center, float radius)
Cone shape_cone(float3 direction, float angle_cosine)
Frustum shape_frustum(float3 corners[8])
Box shape_box(float3 v000, float3 v100, float3 v010, float3 v001)
Pyramid shape_pyramid_non_oblique(float3 apex, float3 base_center, float3 base_center_pos_x, float3 base_center_pos_y)
Circle shape_circle(float2 center, float radius)
#define LIGHT_ATTENUATION_THRESHOLD
float light_attenuation_surface(LightData light, const bool is_directional, LightVector lv)
#define storage(slot, qualifiers)
#define global_invocation_id
#define local_invocation_id
int atomicMin(int &, int) RET
int atomicMax(int &, int) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
int atomicAdd(int &, int) RET
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
constexpr T clamp(T, U, U) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
float3 heatmap_gradient(float t)
void fullscreen_vertex(int vertex_id, float4 &out_position)
int divide_ceil(int a, int b)
float distance_squared(VecT a, VecT b)
void cull_main(Cull &srt, const draw::View &views, const draw::ViewCulling &culling, const uint3 global_id)
void debug_frag(const draw::View &views, LightRenderData &lrd, const HiZ &hiz, const float4 frag_co, const DebugVertOut &v_out, DebugFragOut &frag_out)
void debug_vert(const int vert_id, float4 &out_position, DebugVertOut &v_out)
PipelineCompute tile(tile_main)
PipelineGraphic debug(debug_vert, debug_frag)
PipelineCompute sort(sort_main)
PipelineCompute cull(cull_main)
void sort_main(Sort &srt, const uint3 global_id, const uint3 local_id)
PipelineCompute zbin(zbin_main)
void zbin_main(ZBinning &srt, const draw::View &views, const uint3 local_id)
void tile_main(const draw::View &views, Tile &srt, const uint3 global_id)
void foreach(const LightRenderData &srt, CallbackT &cb, ResourceT &res)
template void light::foreach_visible< culling::WithCullCtx, LightRenderData >(const LightRenderData &, float2, float, culling::WithCullCtx &, LightRenderData &)
int culling_z_to_zbin(float scale, float bias, float z)
void foreach_visible(const LightRenderData &srt, float2 pixel, float linear_view_z, CallbackT &cb, ResourceT &res)
template void light::foreach< culling::NoCullCtx, LightRenderData >(const LightRenderData &, culling::NoCullCtx &, LightRenderData &)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static LightVector get(LightData light, const bool is_directional, float3 P)
bool is_perspective() const
float3 point_world_to_view(float3 P) const
float depth_screen_to_view(float ss_depth) const
float3 point_screen_to_world(float3 ssP) const
const LightData(& in_light_buf)[]
LightCullingData & light_cull_buf
const LightData(& sunlight_buf)[2]
LightData(& out_light_buf)[]
bool intersect(Pyramid pyramid)
static CullingTile from_corners(bool is_persp, float3 corners[8])
bool intersect(const ViewMatrices view, Sphere sphere)
float4 out_debug_color_mul
float4 out_debug_color_add
void eval_local(LightRenderData &, uint l_idx, LightData)
void eval_directional(LightRenderData &, uint, LightData)
const LightCullingData & light_cull_buf
const float(& in_zdist_buf)[]
LightData(& out_light_buf)[]
float zdists_cache[CULLING_SORT_GROUP_SIZE]
const LightData(& in_light_buf)[]
const uint(& in_key_buf)[]
const LightCullingData & light_cull_buf
const LightData(& light_buf)[]
CullingTile tile_culling_get(const ViewMatrices view, uint2 tile_co)
uint(& out_light_tile_buf)[]
float2 tile_to_ndc(float2 tile_co, float2 offset)
void eval_local(LightRenderData &, uint l_idx, LightData light)
void eval_directional(LightRenderData &, uint, LightData)
const LightData(& light_buf)[]
const LightCullingData & light_cull_buf
uint zbin_max[CULLING_ZBIN_COUNT]
uint zbin_min[CULLING_ZBIN_COUNT]