-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusters_cull.comp.glsl
162 lines (121 loc) · 4.68 KB
/
clusters_cull.comp.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2024-2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
/*
Shader Description
==================
This compute shader does basic basic culling of all clusters.
Occlusion and frustum culling can be activated.
It fills `build.visibleClusters`
A single thread represents one cluster.
The sample uses a very basic approach going over all clusters,
even if we might have detected that an instance has already been
culled.
It would be better (but a bit more complex) to handle this differently
so we don't need to iterate over clusters known to be culled.
The "vk_lod_cluster" sample implements a more sophisticated
scene traversal logic.
*/
#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable
#extension GL_EXT_buffer_reference : enable
#extension GL_EXT_buffer_reference2 : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_atomic_int64 : enable
#extension GL_EXT_control_flow_attributes : require
#extension GL_KHR_shader_subgroup_ballot : require
#extension GL_KHR_shader_subgroup_shuffle : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_clustered : require
#extension GL_KHR_shader_subgroup_arithmetic : require
#include "shaderio.h"
layout(push_constant) uniform pushData
{
uint instanceID;
} push;
layout(scalar, binding = BINDINGS_FRAME_UBO, set = 0) uniform frameConstantsBuffer
{
FrameConstants view;
FrameConstants viewLast;
};
layout(scalar, binding = BINDINGS_READBACK_SSBO, set = 0) buffer readbackBuffer
{
Readback readback;
};
layout(scalar, binding = BINDINGS_RENDERINSTANCES_SSBO, set = 0) buffer renderInstancesBuffer
{
RenderInstance instances[];
};
layout(binding = BINDINGS_HIZ_TEX) uniform sampler2D texHizFar;
layout(scalar, binding = BINDINGS_SCENEBUILDING_UBO, set = 0) uniform buildBuffer
{
SceneBuilding build;
};
layout(scalar, binding = BINDINGS_SCENEBUILDING_SSBO, set = 0) buffer buildBufferRW
{
SceneBuilding buildRW;
};
layout(scalar, binding = BINDINGS_TESSTABLE_UBO, set = 0) uniform tessTableBuffer
{
TessellationTable tessTable;
};
////////////////////////////////////////////
layout(local_size_x=CLUSTERS_CULL_WORKGROUP) in;
////////////////////////////////////////////
#include "culling.glsl"
////////////////////////////////////////////
void main()
{
const uint maxEntries = MAX_VISIBLE_CLUSTERS;
//if (gl_WorkGroupID.x > 0) return;
uint clusterID = gl_GlobalInvocationID.x;
uint instanceID = push.instanceID;
RenderInstance instance = instances[instanceID];
uint instanceState = build.instanceStates.d[instanceID];
uint clusterLoad = min(clusterID, instance.numClusters -1);
BBox clusterBbox = instance.clusterBboxes.d[clusterLoad];
vec4 clipMin;
vec4 clipMax;
bool clipValid;
bool visible = clusterLoad == clusterID;
// DEBUG
//visible = visible && (clusterID == 1);
#if DO_CULLING && TARGETS_RASTERIZATION
if ((instanceState & INSTANCE_VISIBLE_BIT) == 0)
visible = false;
visible = visible && intersectFrustum(clusterBbox.lo, clusterBbox.hi, instance.worldMatrix, clipMin, clipMax, clipValid);
visible = visible && (!clipValid || (intersectSize(clipMin, clipMax) && intersectHiz(clipMin, clipMax)));
#endif
uvec4 visibleVote = subgroupBallot(visible);
uint visibleCount = subgroupBallotBitCount(visibleVote);
uint visibleOffset = subgroupBallotExclusiveBitCount(visibleVote);
uint atomicOffset = 0;
if (gl_SubgroupInvocationID == 0 && visibleCount > 0)
{
atomicOffset = atomicAdd(buildRW.visibleClusterCounter, visibleCount);
}
atomicOffset = subgroupBroadcastFirst(atomicOffset);
if (visible && (atomicOffset + visibleOffset) < maxEntries)
{
build.visibleClusters.d[atomicOffset + visibleOffset] = ClusterInfo(instanceID, clusterID);
}
}