mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-06 19:42:39 +08:00
43b4cb30f2
pr-test is not ready, skip it.
136 lines
4.5 KiB
Plaintext
136 lines
4.5 KiB
Plaintext
// Copyright (c) 2023 Xiamen Yaji Software Co., Ltd.
|
|
|
|
CCEffect %{
|
|
techniques:
|
|
- name: opaque
|
|
passes:
|
|
- compute: cluster-main
|
|
pass: cluster-culling-cs
|
|
}%
|
|
|
|
|
|
CCProgram cluster-main %{
|
|
precision highp float;
|
|
#define LOCAL_SIZE_Z 1
|
|
|
|
#pragma rate CCConst pass
|
|
layout(std140) uniform CCConst {
|
|
vec4 cc_nearFar;
|
|
vec4 cc_viewPort;
|
|
vec4 cc_workGroup;
|
|
mat4 cc_matView;
|
|
mat4 cc_matProjInv;
|
|
};
|
|
|
|
#pragma rate b_ccLightsBuffer pass
|
|
layout(std430) readonly buffer b_ccLightsBuffer { vec4 b_ccLights[]; };
|
|
|
|
#pragma rate b_clustersBuffer pass
|
|
layout(std430) readonly buffer b_clustersBuffer { vec4 b_clusters[]; };
|
|
|
|
#pragma rate b_clusterLightIndicesBuffer pass
|
|
layout(std430) buffer b_clusterLightIndicesBuffer { uint b_clusterLightIndices[]; };
|
|
|
|
#pragma rate b_clusterLightGridBuffer pass
|
|
layout(std430) buffer b_clusterLightGridBuffer { uvec4 b_clusterLightGrid[]; };
|
|
|
|
#pragma rate b_globalIndexBuffer pass
|
|
layout(std430) buffer b_globalIndexBuffer { uint b_globalIndex[]; };
|
|
|
|
struct CCLight {
|
|
vec4 cc_lightPos;
|
|
vec4 cc_lightColor;
|
|
vec4 cc_lightSizeRangeAngle;
|
|
vec4 cc_lightDir;
|
|
};
|
|
uint ccLightCount()
|
|
{
|
|
return uint(b_ccLights[3].w);
|
|
}
|
|
CCLight getCCLight(uint i)
|
|
{
|
|
CCLight light;
|
|
light.cc_lightPos = b_ccLights[4u * i + 0u];
|
|
light.cc_lightColor = b_ccLights[4u * i + 1u];
|
|
light.cc_lightSizeRangeAngle = b_ccLights[4u * i + 2u];
|
|
light.cc_lightDir = b_ccLights[4u * i + 3u];
|
|
return light;
|
|
}
|
|
struct Cluster {
|
|
vec3 minBounds;
|
|
vec3 maxBounds;
|
|
};
|
|
struct LightGrid {
|
|
uint offset;
|
|
uint ccLights;
|
|
};
|
|
Cluster getCluster(uint index)
|
|
{
|
|
Cluster cluster;
|
|
cluster.minBounds = b_clusters[2u * index + 0u].xyz;
|
|
cluster.maxBounds = b_clusters[2u * index + 1u].xyz;
|
|
return cluster;
|
|
}
|
|
bool ccLightIntersectsCluster(CCLight light, Cluster cluster)
|
|
{
|
|
if (light.cc_lightPos.w > 0.0) {
|
|
vec3 halfExtents = (cluster.maxBounds - cluster.minBounds) * 0.5;
|
|
vec3 center = (cluster.minBounds + cluster.maxBounds) * 0.5;
|
|
float sphereRadius = sqrt(dot(halfExtents, halfExtents));
|
|
light.cc_lightDir = ((cc_matView) * (vec4(light.cc_lightDir.xyz, 1.0)));
|
|
light.cc_lightDir.xyz = normalize((light.cc_lightDir - ((cc_matView) * (vec4(0,0,0, 1.0)))).xyz).xyz;
|
|
vec3 v = center - light.cc_lightPos.xyz;
|
|
float lenSq = dot(v, v);
|
|
float v1Len = dot(v, light.cc_lightDir.xyz);
|
|
float cosAngle = light.cc_lightSizeRangeAngle.z;
|
|
float sinAngle = sqrt(1.0 - cosAngle * cosAngle);
|
|
float distanceClosestPoint = cosAngle * sqrt(lenSq - v1Len * v1Len) - v1Len * sinAngle;
|
|
bool angleCull = distanceClosestPoint > sphereRadius;
|
|
bool frontCull = v1Len > sphereRadius + light.cc_lightSizeRangeAngle.y;
|
|
bool backCull = v1Len < -sphereRadius;
|
|
return !(angleCull || frontCull || backCull);
|
|
}
|
|
vec3 closest = max(cluster.minBounds, min(light.cc_lightPos.xyz, cluster.maxBounds));
|
|
vec3 dist = closest - light.cc_lightPos.xyz;
|
|
return dot(dist, dist) <= (light.cc_lightSizeRangeAngle.y * light.cc_lightSizeRangeAngle.y);
|
|
}
|
|
shared CCLight lights[(16 * 8 * LOCAL_SIZE_Z)];
|
|
layout(local_size_x = 16, local_size_y = 8, local_size_z = LOCAL_SIZE_Z) in;
|
|
void main()
|
|
{
|
|
uint visibleLights[100];
|
|
uint visibleCount = 0u;
|
|
uint clusterIndex = gl_GlobalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
|
|
gl_GlobalInvocationID.y * gl_WorkGroupSize.x +
|
|
gl_GlobalInvocationID.x;
|
|
|
|
Cluster cluster = getCluster(clusterIndex);
|
|
uint lightCount = ccLightCount();
|
|
uint lightOffset = 0u;
|
|
while (lightOffset < lightCount) {
|
|
uint batchSize = min((16u * 8u * uint(LOCAL_SIZE_Z)), lightCount - lightOffset);
|
|
if (uint(gl_LocalInvocationIndex) < batchSize) {
|
|
uint lightIndex = lightOffset + gl_LocalInvocationIndex;
|
|
CCLight light = getCCLight(lightIndex);
|
|
light.cc_lightPos.xyz = ((cc_matView) * (vec4(light.cc_lightPos.xyz, 1.0))).xyz;
|
|
lights[gl_LocalInvocationIndex] = light;
|
|
}
|
|
barrier();
|
|
for (uint i = 0u; i < batchSize; i++) {
|
|
if (visibleCount < 100u && ccLightIntersectsCluster(lights[i], cluster)) {
|
|
visibleLights[visibleCount] = lightOffset + i;
|
|
visibleCount++;
|
|
}
|
|
}
|
|
lightOffset += batchSize;
|
|
}
|
|
barrier();
|
|
uint offset = 0u;
|
|
offset = atomicAdd(b_globalIndex[0], visibleCount);
|
|
for (uint i = 0u; i < visibleCount; i++) {
|
|
b_clusterLightIndices[offset + i] = visibleLights[i];
|
|
}
|
|
b_clusterLightGrid[clusterIndex] = uvec4(offset, visibleCount, 0, 0);
|
|
}
|
|
}%
|