Untitled
Toggle main menu visibility
Loading...
Searching...
No Matches
cull_draw_calls.comp
Go to the documentation of this file.
1
#version 430 core
2
layout
(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
3
4
5
struct
DrawArraysIndirectCommand
{
6
uint
count
;
7
uint
instanceCount
;
8
uint
first
;
9
uint
baseInstance
;
10
uint
instanceOffset
;
// the amount of instances pushed this frame
11
uint
pad0
;
12
};
13
14
15
layout
(std430, binding = 5) buffer DrawArraysData {
16
17
DrawArraysIndirectCommand
draw_arrays_commands[];
18
};
19
20
21
struct
DrawElementsIndirectCommand
{
22
uint
count
;
23
uint
instanceCount
;
24
uint
firstIndex
;
25
uint
baseVertex
;
26
uint
baseInstance
;
27
uint
instanceOffset
;
// the amount of instances pushed this frame
28
};
29
30
layout
(std430, binding = 6) buffer DrawElementsData {
31
32
DrawElementsIndirectCommand
draw_elements_commands[];
33
};
34
35
struct
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
52
layout
(std430, binding = 15) buffer MeshNodeData {
53
54
float
focal_distance;
55
uint pad0;
56
uint pad1;
57
uint pad2;
58
MeshNode
mns[
MAX_MESH_NODES
];
59
60
};
61
62
63
#define MAX_INSTANCES 1024
64
layout
(std430, binding = 4) buffer VisibleData {
65
66
uint visible_indices[];
67
68
};
69
70
71
struct
Transform
72
{
73
uint
mn_id
;
74
uint
command_id
;
75
uvec2
directional_lights
;
76
uvec2
spot_lights
;
77
uint
skin_offset
;
78
uint
material
;
79
mat4
mat
;
80
};
81
82
layout
(std430, binding = 7) buffer TransformData {
83
84
Transform
trs[];
85
86
};
87
88
uniform uint
uInstanceCount
;
89
uniform
vec4
planes
[6];
90
uniform uint
uArrayOffset
;
91
92
struct
AABBBounds
93
{
94
vec4
min
;
95
vec4
max
;
96
};
97
98
99
AABBBounds
transform_aabb_by_model_matrix
(
MeshNode
mn,
mat4
mat)
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
127
bool
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
146
void
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
layout(local_size_x=16, local_size_y=16) in
main
void main()
Definition
audio_probe_bake.comp:465
uArrayOffset
uniform uint uArrayOffset
Definition
cull_draw_calls.comp:90
aabb_in_frustum
bool aabb_in_frustum(vec3 min, vec3 max)
Definition
cull_draw_calls.comp:127
planes
uniform vec4 planes[6]
Definition
cull_draw_calls.comp:89
AABBBounds::min
vec4 min
Definition
cull_draw_calls.comp:94
uInstanceCount
uniform uint uInstanceCount
Definition
cull_draw_calls.comp:88
AABBBounds::max
vec4 max
Definition
cull_draw_calls.comp:95
transform_aabb_by_model_matrix
AABBBounds transform_aabb_by_model_matrix(MeshNode mn, mat4 mat)
Definition
cull_draw_calls.comp:99
AABBBounds
Definition
cull_draw_calls.comp:93
DrawElementsIndirectCommand::instanceCount
uint32_t instanceCount
Definition
engine_types.h:860
DrawElementsIndirectCommand::count
uint32_t count
Definition
engine_types.h:859
DrawElementsIndirectCommand::firstIndex
uint32_t firstIndex
Definition
engine_types.h:861
DrawElementsIndirectCommand::baseInstance
uint32_t baseInstance
Definition
engine_types.h:863
DrawElementsIndirectCommand::instanceOffset
uint32_t instanceOffset
This tracks how many offsets were pushed in a frame and is used in the prefix sum.
Definition
engine_types.h:864
DrawElementsIndirectCommand::baseVertex
uint32_t baseVertex
Definition
engine_types.h:862
DrawElementsIndirectCommand
DrawArraysIndirect commands are used for non-static meshes.
Definition
engine_types.h:857
MAX_INSTANCES
#define MAX_INSTANCES
Maximum amount of TransformGPU instances to push in one frame (used for SSBO sizing).
Definition
engine_types.h:111
MAX_MESH_NODES
#define MAX_MESH_NODES
Maximum MeshNodes in a scene (used for SSBO sizing).
Definition
engine_types.h:108
DrawArraysIndirectCommand::first
uint32_t first
Definition
engine_types.h:846
Transform::mat
mat4 mat
Definition
cull_draw_calls.comp:79
DrawArraysIndirectCommand::pad0
uint32_t pad0
Definition
engine_types.h:849
DrawArraysIndirectCommand::instanceOffset
uint32_t instanceOffset
This tracks how many offsets were pushed in a frame and is used in the prefix sum.
Definition
engine_types.h:848
MeshNode::pad1
uint pad1
Definition
coc.frag:55
MeshNode::first
uint first
Definition
coc.frag:52
MeshNode::min
vec4 min
Definition
coc.frag:44
Transform::skin_offset
uint skin_offset
Definition
cull_draw_calls.comp:77
MeshNode::material
uint material
Definition
coc.frag:51
MeshNode::max
vec4 max
Definition
coc.frag:45
MeshNode::lm_offset
uint lm_offset
Definition
coc.frag:50
Transform::directional_lights
uvec2 directional_lights
Definition
cull_draw_calls.comp:75
Transform::mn_id
uint mn_id
Definition
cull_draw_calls.comp:73
Transform::material
uint material
Definition
cull_draw_calls.comp:78
Transform::command_id
uint command_id
Definition
cull_draw_calls.comp:74
DrawArraysIndirectCommand::instanceCount
uint32_t instanceCount
Definition
engine_types.h:845
DrawArraysIndirectCommand::count
uint32_t count
Definition
engine_types.h:844
DrawArraysIndirectCommand::baseInstance
uint32_t baseInstance
Definition
engine_types.h:847
MeshNode::pad0
uint pad0
Definition
coc.frag:54
MeshNode::center
vec4 center
Definition
coc.frag:46
MeshNode::instance_count
uint instance_count
Definition
coc.frag:48
MeshNode::static_mesh
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
Definition
engine_types.h:2399
MeshNode::count
size_t count
Count of vertices pushed into the MAIN_VBO.
Definition
engine_types.h:2402
MeshNode::extents
vec4 extents
Definition
coc.frag:47
Transform::spot_lights
uvec2 spot_lights
Definition
cull_draw_calls.comp:76
DrawArraysIndirectCommand
DrawArraysIndirect commands are used for static meshes (eg.
Definition
engine_types.h:842
MeshNode
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Definition
audio_probe_bake.comp:85
Transform
Transforms for MeshNode models.
Definition
cull_draw_calls.comp:72
vec4::z
float z
Definition
types.h:13
vec4::x
float x
Definition
types.h:13
vec3::x
float x
Definition
types.h:12
vec4::y
float y
Definition
types.h:13
vec3::y
float y
Definition
types.h:12
vec3::z
float z
Definition
types.h:12
mat4
Definition
types.h:21
uvec2
Definition
types.h:17
vec3
Definition
types.h:12
vec4
Definition
types.h:13
src
render
cull_draw_calls.comp
Generated by
1.18.0