forked from nvpro-samples/gl_vk_meshlet_cadscene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawmeshlet_ext_basic.mesh.glsl
367 lines (286 loc) · 10.6 KB
/
drawmeshlet_ext_basic.mesh.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright (c) 2016-2022, 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) 2016-2022 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_control_flow_attributes: require
#define UNROLL_LOOP [[unroll]]
#include "config.h"
//////////////////////////////////////
#extension GL_EXT_mesh_shader : require
//////////////////////////////////////
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
//////////////////////////////////////
#include "common.h"
//////////////////////////////////////////////////
// MESH CONFIG
// see Sample::getShaderPrepend() how these are computed
const uint WORKGROUP_SIZE = EXT_MESH_SUBGROUP_COUNT * EXT_MESH_SUBGROUP_SIZE;
layout(local_size_x=WORKGROUP_SIZE) in;
layout(max_vertices=NVMESHLET_VERTEX_COUNT, max_primitives=NVMESHLET_PRIMITIVE_COUNT) out;
layout(triangles) out;
// The workgroup size of the shader may not have enough threads
// to do all the work in a unique thread.
// Therefore we might need to loop to process all the work.
// Looping can have the benefit that we can amortize some registers
// that are common to all threads. However, it may also introduce
// more registers.
const uint MESHLET_VERTEX_ITERATIONS = ((NVMESHLET_VERTEX_COUNT + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE);
const uint MESHLET_PRIMITIVE_ITERATIONS = ((NVMESHLET_PRIMITIVE_COUNT + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE);
// task shader is used in advance, doing early cluster culling
#ifndef USE_TASK_STAGE
#define USE_TASK_STAGE 0
#endif
/////////////////////////////////////
// UNIFORMS
layout(push_constant) uniform pushConstant{
// x: mesh, y: prim, z: 0, w: vertex
uvec4 geometryOffsets;
// x: meshFirst, y: meshMax
uvec4 drawRange;
};
layout(std140, binding = SCENE_UBO_VIEW, set = DSET_SCENE) uniform sceneBuffer {
SceneData scene;
};
layout(std430, binding = SCENE_SSBO_STATS, set = DSET_SCENE) buffer statsBuffer {
CullStats stats;
};
layout(std140, binding= 0, set = DSET_OBJECT) uniform objectBuffer {
ObjectData object;
};
layout(std430, binding = GEOMETRY_SSBO_MESHLETDESC, set = DSET_GEOMETRY) buffer meshletDescBuffer {
uvec4 meshletDescs[];
};
layout(std430, binding = GEOMETRY_SSBO_PRIM, set = DSET_GEOMETRY) buffer primIndexBuffer1 {
uint primIndices1[];
};
layout(std430, binding = GEOMETRY_SSBO_PRIM, set = DSET_GEOMETRY) buffer primIndexBuffer2 {
uint8_t primIndices_u8[];
};
layout(binding=GEOMETRY_TEX_VBO, set=DSET_GEOMETRY) uniform samplerBuffer texVbo;
layout(binding=GEOMETRY_TEX_ABO, set=DSET_GEOMETRY) uniform samplerBuffer texAbo;
/////////////////////////////////////////////////
#include "nvmeshlet_utils.glsl"
/////////////////////////////////////////////////
// MESH INPUT
#if USE_TASK_STAGE
struct Task {
uint baseID;
uint8_t deltaIDs[NVMESHLET_PER_TASK];
};
taskPayloadSharedEXT Task IN;
// gl_WorkGroupID.x runs from [0 .. parentTask.groupCountX - 1]
uint meshletID = IN.baseID + IN.deltaIDs[gl_WorkGroupID.x];
#else
uint meshletID = gl_WorkGroupID.x + drawRange.x;
#endif
uint laneID = gl_LocalInvocationID.x;
////////////////////////////////////////////////////////////
// INPUT
// We are using simple vertex attributes here, so
// that we can switch easily between fp32 and fp16 to
// investigate impact of vertex bandwith.
//
// In a more performance critical scenario we recommend the use
// of packed normals for CAD, like octant encoding and pack position
// and normal in a single 128-bit value.
// If you work from fixed vertex definitions and don't need dynamic
// format conversions by texture formats, or don't mind
// creating multiple shader permutations, you may want to
// use ssbos here, instead of tbos
vec3 getPosition( uint vidx ){
return texelFetch(texVbo, int(vidx)).xyz;
}
vec3 getNormal( uint vidx ){
return texelFetch(texAbo, int(vidx * VERTEX_NORMAL_STRIDE)).xyz;
}
vec4 getExtra( uint vidx, uint xtra ){
return texelFetch(texAbo, int(vidx * VERTEX_NORMAL_STRIDE + 1 + xtra));
}
////////////////////////////////////////////////////////////
// OUTPUT
#if SHOW_PRIMIDS
// nothing to output
#elif USE_BARYCENTRIC_SHADING
layout(location=0) out Interpolants {
flat uint meshletID;
} OUT[];
layout(location=1) out ManualInterpolants {
uint vidx;
} OUTBary[];
#else
layout(location=0) out Interpolants {
vec3 wPos;
vec3 wNormal;
flat uint meshletID;
#if VERTEX_EXTRAS_COUNT
vec4 xtra[VERTEX_EXTRAS_COUNT];
#endif
} OUT[];
#endif
//////////////////////////////////////////////////
// VERTEX EXECUTION
// This is the code that is normally done in the vertex-shader
// "vidx" is what gl_VertexIndex would be
//
// We split vertex-shading from attribute-shading,
// to highlight the differences between the drawmeshlet_cull.mesh.glsl
// and drawmeshlet_basic.mesh.glsl files (just use a file-diff
// program to view the two)
vec4 procVertex(const uint vert, uint vidx)
{
vec3 oPos = getPosition(vidx);
vec3 wPos = (object.worldMatrix * vec4(oPos,1)).xyz;
vec4 hPos = (scene.viewProjMatrix * vec4(wPos,1));
// only early out if we could make out-of-bounds write
if ((WORKGROUP_SIZE * MESHLET_VERTEX_ITERATIONS > NVMESHLET_VERTEX_COUNT) && vert >= NVMESHLET_VERTEX_COUNT) return hPos;
gl_MeshVerticesEXT[vert].gl_Position = hPos;
#if !SHOW_PRIMIDS
#if USE_BARYCENTRIC_SHADING
OUTBary[vert].vidx = vidx;
OUT[vert].meshletID = meshletID;
#else
OUT[vert].wPos = wPos;
OUT[vert].meshletID = meshletID;
#endif
#endif
#if USE_CLIPPING
#if IS_VULKAN
// spir-v annoyance, doesn't unroll the loop and therefore cannot derive the number of clip distances used
#if NUM_CLIPPING_PLANES > 0
gl_MeshVerticesEXT[vert].gl_ClipDistance[0] = dot(scene.wClipPlanes[0], vec4(wPos,1));
#endif
#if NUM_CLIPPING_PLANES > 1
gl_MeshVerticesEXT[vert].gl_ClipDistance[1] = dot(scene.wClipPlanes[1], vec4(wPos,1));
#endif
#if NUM_CLIPPING_PLANES > 2
gl_MeshVerticesEXT[vert].gl_ClipDistance[2] = dot(scene.wClipPlanes[2], vec4(wPos,1));
#endif
#else
for (int i = 0; i < NUM_CLIPPING_PLANES; i++){
gl_MeshVerticesEXT[vert].gl_ClipDistance[i] = dot(scene.wClipPlanes[i], vec4(wPos,1));
}
#endif
#endif
return hPos;
}
void procAttributes(const uint vert, uint vidx)
{
#if !SHOW_PRIMIDS && !USE_BARYCENTRIC_SHADING
vec3 oNormal = getNormal(vidx);
vec3 wNormal = mat3(object.worldMatrixIT) * oNormal;
// only early out if we could make out-of-bounds write
if ((WORKGROUP_SIZE * MESHLET_VERTEX_ITERATIONS > NVMESHLET_VERTEX_COUNT) && vert >= NVMESHLET_VERTEX_COUNT) return;
OUT[vert].wNormal = wNormal;
#if VERTEX_EXTRAS_COUNT
UNROLL_LOOP
for (int i = 0; i < VERTEX_EXTRAS_COUNT; i++) {
vec4 xtra = getExtra(vidx, i);
OUT[vert].xtra[i] = xtra;
}
#endif
#endif
}
//////////////////////////////////////////////////
// MESH EXECUTION
///////////////////////////////////////////////////////////////////////////////
// One can see that the primary mesh-shader code is agnostic of the vertex-shading work.
// In theory it should be possible to even automatically generate mesh-shader SPIR-V
// as combination of a template mesh-shader and a vertex-shader provided as SPIR-V
void main()
{
#if NVMESHLET_ENCODING == NVMESHLET_ENCODING_PACKBASIC
// LOAD HEADER PHASE
uvec4 desc = meshletDescs[meshletID + geometryOffsets.x];
uint vertMax;
uint primMax;
uint vidxStart;
uint vidxBits;
uint vidxDiv;
uint primStart;
uint primDiv;
decodeMeshlet(desc, vertMax, primMax, primStart, primDiv, vidxStart, vidxBits, vidxDiv);
vidxStart += geometryOffsets.y / 4;
primStart += geometryOffsets.y / 4;
uint primCount = primMax + 1;
uint vertCount = vertMax + 1;
SetMeshOutputsEXT(vertCount, primCount);
// VERTEX PROCESSING
{
UNROLL_LOOP
for (uint i = 0; i < uint(MESHLET_VERTEX_ITERATIONS); i++)
{
uint vert = laneID + i * WORKGROUP_SIZE;
uint vertLoad = min(vert, vertMax);
{
// the meshlet contains two set of indices
// - vertex indices (which can be either 16 or 32 bit)
// are loaded here. The idx is manipulated
// as one 32 bit value contains either two 16 bits
// or just a single 32 bit.
// The bit shifting handles the 16 or 32 bit decoding
//
// - primitive (triangle) indices are loaded
// later in bulk, see PRIMITIVE TOPOLOGY
uint idx = (vertLoad) >> (vidxDiv-1);
uint shift = (vertLoad) & (vidxDiv-1);
uint vidx = primIndices1[idx + vidxStart];
vidx <<= vidxBits * (1-shift);
vidx >>= vidxBits;
vidx += geometryOffsets.w;
// here we do the work typically done in the vertex-shader
procVertex(vert, vidx);
procAttributes(vert, vidx);
}
}
}
// PRIMITIVE TOPOLOGY
{
uint readBegin = primStart * 4;
UNROLL_LOOP
for (uint i = 0; i < uint(MESHLET_PRIMITIVE_ITERATIONS); i++)
{
uint prim = laneID + i * WORKGROUP_SIZE;
uint primRead = min(prim, primMax);
uvec3 indices = uvec3(primIndices_u8[readBegin + primRead * 3 + 0],
primIndices_u8[readBegin + primRead * 3 + 1],
primIndices_u8[readBegin + primRead * 3 + 2]);
if (prim <= primMax) {
gl_PrimitiveTriangleIndicesEXT[prim] = indices;
#if SHOW_PRIMIDS
// let's compute some fake unique primitiveID
gl_MeshPrimitivesEXT[prim].gl_PrimitiveID = int((meshletID + geometryOffsets.x) * NVMESHLET_PRIMITIVE_COUNT + prim);
#endif
}
}
}
#else
#error "NVMESHLET_ENCODING not supported"
#endif
////////////////////////////////////////////
if (laneID == 0) {
//atomicMax(stats.debugA[0], WORKGROUP_SIZE);
#if USE_STATS
atomicAdd(stats.meshletsOutput, 1);
atomicAdd(stats.trisOutput, primCount);
atomicAdd(stats.attrInput, vertCount);
atomicAdd(stats.attrOutput, vertCount);
#endif
}
}