Untitled
Loading...
Searching...
No Matches
cull_clusters.comp
Go to the documentation of this file.
1
2#version 430 core
3layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
4
5layout(std140, binding = 0) uniform CameraData {
6 vec4 cam_pos;
7 vec4 cam_vel;
9 mat4 unjittered_projection;
10 mat4 view;
11 mat4 inv_projection;
12 mat4 unjittered_inv_projection;
13 mat4 inv_view;
14};
15layout(std140, binding = 1) uniform RenderingData {
16 ivec4 resolution;
17};
18layout(std140, binding = 2) uniform LightCameraData {
19 vec4 light_cam_pos;
20 vec4 light_cam_vel;
21 mat4 light_projection;
22 mat4 light_unjittered_projection;
23 mat4 light_view;
24 mat4 light_inv_projection;
25 mat4 light_unjittered_inv_projection;
26 mat4 light_inv_view;
27};
28
29layout(std140, binding = 4) uniform PrevCameraData {
30 vec4 prev_cam_pos;
31 vec4 prev_cam_vel;
32 mat4 prev_projection;
33 mat4 prev_unjittered_projection;
34 mat4 prev_view;
35 mat4 prev_inv_projection;
36 mat4 prev_unjittered_inv_projection;
37 mat4 prev_inv_view;
38};
39
40
41struct Light
42{
43
44 vec3 pos;
45 uint entity_id;
46
47 vec3 dir;
48 float outer_cos;
49
50 vec3 color;
51 float cos_diff;
52
53 float intensity;
54 uint light_type;
55 float inner_angle;
56 float outer_angle;
57
58 float radius;
59 uint layer;
60 uint mesh;
61 uint tri;
62
63 float area;
64 uint pad0;
65 uint pad1;
66 uint pad2;
67
68};
69layout(std430, binding = 0) buffer LightData {
70
71 Light lights[];
72
73};
74
75struct AABB
76{
79};
80
81layout(std430, binding = 12) buffer ClusterAABBData {
82 AABB cluster_AABBs[];
83};
84layout(std430, binding = 13) buffer GlobalLightIndexData {
85 uint global_light_indices[];
86};
87struct Cluster
88{
89
90 uint offset;
91 uint count;
92};
93
94layout(std430, binding = 14) buffer ClusterData {
95 Cluster clusters[];
96};
97
98
99
101uniform float uNear;
102uniform float uFar;
103
104
106uniform uint uLightCount;
107
108bool test_sphere_aabb(vec3 light_pos, float light_radius, vec4 min, vec4 max)
109{
110
111 vec3 closest_point = clamp(light_pos, min.xyz, max.xyz);
112
113 vec3 delta = light_pos - closest_point;
114
115 return dot(delta, delta) <= (light_radius * light_radius);
116}
117void main()
118{
119
120 uvec3 coord = gl_GlobalInvocationID.xyz;
121 if (coord.x >= uGridSize.x || coord.y >= uGridSize.y || coord.z >= uGridSize.z) return;
122
123 uint index = coord.x + (coord.y * uGridSize.x) + (coord.z * uGridSize.x * uGridSize.y);
124
125 AABB cluster_aabb = cluster_AABBs[index];
126
127 uint offset = index * uMaxLightsPerCluster;
128 uint visible_count = 0;
129
130 for (int i = 0; i < uLightCount; i++)
131 {
132 Light l = lights[i];
133 if (l.light_type != 0) continue;
134
135 vec4 view_space_light = view * vec4(l.pos, 1.0);
136 vec3 light_pos_view_space = view_space_light.xyz;
137
138 if (test_sphere_aabb(light_pos_view_space, l.radius, cluster_aabb.min, cluster_aabb.max))
139 {
140 global_light_indices[offset + visible_count] = i;
141 visible_count++;
142 if (visible_count >= uMaxLightsPerCluster) break;
143 }
144 }
145
146 clusters[index].offset = offset;
147 clusters[index].count = visible_count;
148
149
150}
layout(local_size_x=16, local_size_y=16) in
void main()
bool test_sphere_aabb(vec3 light_pos, float light_radius, vec4 min, vec4 max)
uniform float uFar
uniform uvec3 uGridSize
uniform float uNear
uniform uint uMaxLightsPerCluster
float intensity
The intensity of the Light.
vec3 pos
Position of light.
uint32_t layer
Which layer a DIRECTIONAL_LIGHT corresponds to in the SHADOW_MAP_FBO.
LightType light_type
The type of Light.
vec3 dir
Direction for lights that are not POINT_LIGHTs.
float inner_angle
Starting angle for SPOT_LIGHTs.
int tri
The MeshFace this corresponds to.
float outer_angle
Outer angle for SPOT_LIGHTs.
float outer_cos
float radius
The radius of the Light.
int mesh
The MeshNode this corresponds to.
AABB area
The physical area of AREA_LIGHTs.
uint32_t entity_id
Entity ID into ENTITIES.
GPU-side Clusters for clustered forward.
The CPU side structure for Lights.
uniform int uLightCount
float clamp(float a, float b, float c)
Definition mathf.h:379
uniform mat4 projection
Definition shadow.vert:15
uniform mat4 view
Definition shadow.vert:16
uint32_t x
Definition types.h:18
uint32_t z
Definition types.h:18
uint32_t y
Definition types.h:18
Definition types.h:16
Definition types.h:21
Definition types.h:18
Definition types.h:12
Definition types.h:13