Untitled
Loading...
Searching...
No Matches
cull_draw_calls.comp
Go to the documentation of this file.
1#version 430 core
2layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
3
4
6 uint count;
8 uint first;
10 uint instanceOffset; // the amount of instances pushed this frame
11 uint pad0;
12};
13
14
15layout(std430, binding = 5) buffer DrawArraysData {
16
17 DrawArraysIndirectCommand draw_arrays_commands[];
18};
19
20
22 uint count;
27 uint instanceOffset; // the amount of instances pushed this frame
28};
29
30layout(std430, binding = 6) buffer DrawElementsData {
31
32 DrawElementsIndirectCommand draw_elements_commands[];
33};
34
35struct MeshNode
36{
37 vec4 min;
38 vec4 max;
39 vec4 center;
40 vec4 extents;
41 uint instance_count;
42 uint static_mesh;
43 uint lm_offset;
44 uint material;
45 uint first;
46 uint count;
47 uint pad0;
48 uint pad1;
49};
50
51#define MAX_MESH_NODES 1024
52layout(std430, binding = 15) buffer MeshNodeData {
53
54 float focal_distance;
55 uint pad0;
56 uint pad1;
57 uint pad2;
59
60};
61
62
63#define MAX_INSTANCES 1024
64layout(std430, binding = 4) buffer VisibleData {
65
66 uint visible_indices[];
67
68};
69
70
81
82layout(std430, binding = 7) buffer TransformData {
83
84 Transform trs[];
85
86};
87
88uniform uint uInstanceCount;
89uniform vec4 planes[6];
90uniform uint uArrayOffset;
91
93{
96};
97
98
100{
101
102 vec3 center = (mat * mn.center).xyz;
103
104 vec3 AX = (mat * vec4(1,0,0, 0.0)).xyz;
105 vec3 AY = (mat * vec4(0,1,0, 0.0)).xyz;
106 vec3 AZ = (mat * vec4(0,0,1, 0.0)).xyz;
107
108
109 vec3 new_extents;
110 new_extents.x = abs(AX.x) * mn.extents.x + abs(AY.x) * mn.extents.y + abs(AZ.x) * mn.extents.z;
111 new_extents.y = abs(AX.y) * mn.extents.x + abs(AY.y) * mn.extents.y + abs(AZ.y) * mn.extents.z;
112 new_extents.z = abs(AX.z) * mn.extents.x + abs(AY.z) * mn.extents.y + abs(AZ.z) * mn.extents.z;
113
114 AABBBounds aabb;
115
116 aabb.min.x = mn.center.x - new_extents.x;
117 aabb.min.y = mn.center.y - new_extents.y;
118 aabb.min.z = mn.center.z - new_extents.z;
119 aabb.max.x = mn.center.x + new_extents.x;
120 aabb.max.y = mn.center.y + new_extents.y;
121 aabb.max.z = mn.center.z + new_extents.z;
122
123 return aabb;
124
125}
126
127bool aabb_in_frustum (vec3 min, vec3 max)
128{
129 for (int i = 0; i < 6; i++)
130 {
131 vec3 p = vec3(
132 planes[i].x >= 0.0f ? max.x : min.x,
133 planes[i].y >= 0.0f ? max.y : min.y,
134 planes[i].z >= 0.0f ? max.z : min.z
135 );
136
137 if (dot(planes[i].xyz, p) + planes[i].w < 0.0f)
138 {
139 return false;
140 }
141 }
142
143 return true;
144}
145
146void main()
147{
148
149 uint instance_id = gl_GlobalInvocationID.x;
150 if (instance_id >= uInstanceCount) return;
151
152
153 Transform tr = trs[instance_id];
154 MeshNode mn = mns[tr.mn_id];
155 // before cull
156 if (mn.static_mesh == 1)
157 {
158 atomicAdd(draw_arrays_commands[tr.command_id].instanceOffset, 1);
159
160 }
161 else
162 {
163 atomicAdd(draw_elements_commands[tr.command_id].instanceOffset, 1);
164 }
165
166
167 // vec3 min = (tr.mat * mn.min).xyz;
168 // vec3 max = (tr.mat * mn.max).xyz;
169
170
171 // AABBBounds bounds = transform_aabb_by_model_matrix(mn, tr.mat);
172
173 // if (!aabb_in_frustum(bounds.min, bounds.max)) return;
174
175
176
177 // cull
178
179 uint offset = MAX_INSTANCES * tr.command_id;
180 if (mn.static_mesh == 1)
181 {
182 uint count = atomicAdd(draw_arrays_commands[tr.command_id].instanceCount, 1);
183
184 visible_indices[offset + count] = instance_id;
185 }
186 else
187 {
188 offset += MAX_INSTANCES * uArrayOffset;
189 uint count = atomicAdd(draw_elements_commands[tr.command_id].instanceCount, 1);
190 visible_indices[offset + count] = instance_id;
191 }
192
193
194
195
196}
197
layout(local_size_x=16, local_size_y=16) in
void main()
uniform uint uArrayOffset
bool aabb_in_frustum(vec3 min, vec3 max)
uniform vec4 planes[6]
uniform uint uInstanceCount
AABBBounds transform_aabb_by_model_matrix(MeshNode mn, mat4 mat)
uint32_t instanceOffset
This tracks how many offsets were pushed in a frame and is used in the prefix sum.
DrawArraysIndirect commands are used for non-static meshes.
#define MAX_INSTANCES
Maximum amount of TransformGPU instances to push in one frame (used for SSBO sizing).
#define MAX_MESH_NODES
Maximum MeshNodes in a scene (used for SSBO sizing).
uint32_t instanceOffset
This tracks how many offsets were pushed in a frame and is used in the prefix sum.
uint pad1
Definition coc.frag:55
uint first
Definition coc.frag:52
vec4 min
Definition coc.frag:44
uint material
Definition coc.frag:51
vec4 max
Definition coc.frag:45
uint lm_offset
Definition coc.frag:50
uvec2 directional_lights
uint pad0
Definition coc.frag:54
vec4 center
Definition coc.frag:46
uint instance_count
Definition coc.frag:48
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
size_t count
Count of vertices pushed into the MAIN_VBO.
vec4 extents
Definition coc.frag:47
DrawArraysIndirect commands are used for static meshes (eg.
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Transforms for MeshNode models.
float z
Definition types.h:13
float x
Definition types.h:13
float x
Definition types.h:12
float y
Definition types.h:13
float y
Definition types.h:12
float z
Definition types.h:12
Definition types.h:21
Definition types.h:17
Definition types.h:12
Definition types.h:13