-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw_compressed_lod.mesh.glsl
307 lines (251 loc) · 10 KB
/
draw_compressed_lod.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
/*
* Copyright (c) 2022-2023, 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) 2022-2023 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_control_flow_attributes : require
#extension GL_NV_mesh_shader : enable
#if MICRO_DECODER == MICRO_DECODER_MICROTRI_THREAD && MICRO_MTRI_USE_INTRINSIC
#extension GL_NV_displacement_micromap : require
#extension GL_EXT_ray_query : require
#endif
#extension GL_EXT_buffer_reference2 : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_8bit_storage : enable
#extension GL_EXT_shader_16bit_storage : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : 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_shader_atomic_int64 : enable
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
#extension GL_KHR_shader_subgroup_vote : require
#extension GL_KHR_shader_subgroup_arithmetic : require
#extension GL_KHR_shader_subgroup_shuffle : require
#extension GL_KHR_shader_subgroup_shuffle_relative : require
#extension GL_NV_shader_subgroup_partitioned : require
#include "common.h"
#include "common_mesh.h"
#include "common_barymap.h"
#include "common_micromesh_uncompressed.h"
#include "common_micromesh_compressed.h"
#include "micromesh_binpack_decl.glsl"
//////////////////////////////////////////////////////////////////////////
layout(local_size_x=MICRO_GROUP_SIZE) in;
layout(max_primitives = MICRO_MESHLET_PRIMITIVES, max_vertices = MICRO_MESHLET_VERTICES, triangles) out;
////////////////////////////////////////////////////////////////
// BINDINGS
layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer {
SceneData scene;
SceneData sceneLast;
};
layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer {
ShaderStats stats;
};
layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer {
MicromeshData microdata;
};
layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer {
MeshData mesh;
};
#if MICRO_DECODER == MICRO_DECODER_MICROTRI_THREAD && MICRO_MTRI_USE_INTRINSIC
layout(binding=DRAWCOMPRESSED_ACC) uniform accelerationStructureEXT sceneTlas;
#endif
layout(push_constant) uniform pushDraw {
DrawMicromeshPushData push;
};
//////////////////////////////////////////////////////////////////////////
// INPUT
taskNV in Task
{
MicroBinPack binpack;
} IN;
//////////////////////////////////////////////////////////////////////////
// OUTPUT
#if SURFACEVIS == SURFACEVIS_SHADING
layout(location = 0) out Interpolants
{
vec3 wPos;
vec3 bary;
flat uint tri;
}
OUT[];
#endif
#if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) \
|| (SURFACEVIS == SURFACEVIS_ANISOTROPY)
layout(location = 3) out PerVertex
{
#if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS)
uint vidx;
#endif
#if SURFACEVIS == SURFACEVIS_ANISOTROPY
vec3 vwPos; // Copy of wPos; aliasing with wPos is unfortunately invalid.
#endif
}
OUTvtx[];
#endif
//////////////////////////////////////////////////////////////////////////
#include "micromesh_utils.glsl"
#include "micromesh_decoder.glsl"
#include "draw_culling.glsl"
// MicroBinPack functions need to reference target variables directly via macros
#define MICROBINPACK_USE_MESHLETCOUNT 0
#define MICROBINPACK_IN IN.binpack
#include "micromesh_binpack.glsl"
//////////////////////////////////////////////////////////////////////////
void main()
{
//////////////////////////////////////
// Decoder Configuration Phase
uint wgroupID = gl_WorkGroupID.x;
uint laneID = gl_SubgroupInvocationID;
MicroDecoderConfig cfg = MicroBinPack_subgroupUnpack(wgroupID);
#if !USE_PRIMITIVE_CULLING
uint validCount = 1;
if (cfg.targetSubdiv < 3){
uvec4 validVote = subgroupBallot(cfg.packThreadID == 0 && cfg.valid);
validCount = subgroupBallotBitCount(validVote);
}
#endif
// Load microTri
MicromeshBaseTri microTri = microdata.basetriangles.d[cfg.microID];
//////////////////////////////////////
// Initial Decoding Phase
SubgroupMicromeshDecoder sdec;
smicrodec_subgroupInit(sdec, cfg, microTri, 0, 0, 0);
//////////////////////////////////////
// Mesh Preparation Phase
mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix;
mat4 worldMatrixIT = transpose(inverse(worldMatrix));
uint triLocal = smicrodec_getMeshTriangle(sdec);
uint tri = triLocal + push.firstTriangle;
uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0],
mesh.indices.d[tri * 3 + 1],
mesh.indices.d[tri * 3 + 2]) + push.firstVertex;
// Generate vertices
vec3 v0 = mesh.positions.d[triIndices.x];
vec3 v1 = mesh.positions.d[triIndices.y];
vec3 v2 = mesh.positions.d[triIndices.z];
f16vec3 d0 = mesh.dispDirections.d[triIndices.x].xyz;
f16vec3 d1 = mesh.dispDirections.d[triIndices.y].xyz;
f16vec3 d2 = mesh.dispDirections.d[triIndices.z].xyz;
#if USE_DIRECTION_BOUNDS
boundsVec2 bounds0 = mesh.dispDirectionBounds.d[triIndices.x];
boundsVec2 bounds1 = mesh.dispDirectionBounds.d[triIndices.y];
boundsVec2 bounds2 = mesh.dispDirectionBounds.d[triIndices.z];
v0 = v0 + d0 * bounds0.x;
v1 = v1 + d1 * bounds1.x;
v2 = v2 + d2 * bounds2.x;
d0 = d0 * float16_t(bounds0.y);
d1 = d1 * float16_t(bounds1.y);
d2 = d2 * float16_t(bounds2.y);
#endif
//////////////////////////////////////
// Vertex Iteration Phase
#if USE_MICROVERTEX_NORMALS
uint firstValue = microdata.attrTriangleOffsets.d[triLocal];
#endif
uint baseSubdiv = smicrodec_getBaseSubdiv(sdec);
dispVec2 scale_bias = micromesh_concatScaleBias(push.scale_bias, dispVec2(scene.disp_scale, scene.disp_bias));
for (uint vertIter = 0; vertIter < smicrodec_getIterationCount(); vertIter++)
{
MicroDecodedVertex decoded = smicrodec_subgroupGetVertex(sdec, vertIter);
uint vertOut = decoded.outIndex;
// safe to early out post shuffle
// This thread may not be valid, but a valid one before it may need to acces its data for shuffle in
// smicrodec_subgroupGetLocalVertex
if (!decoded.valid) continue;
vec4 wPos = smicrodec_getVertexPos(decoded, v0, v1, v2, d0, d1, d2,
worldMatrix,
push.instanceID,
scale_bias);
gl_MeshVerticesNV[vertOut].gl_Position = scene.viewProjMatrix * wPos;
#if SURFACEVIS == SURFACEVIS_SHADING
OUT[vertOut].wPos = wPos.xyz;
OUT[vertOut].bary = decoded.bary;
OUT[vertOut].tri = tri;
#if USE_MICROVERTEX_NORMALS
uint valueIdx = umajorUV_toLinear(subdiv_getNumVertsPerEdge(baseSubdiv), decoded.uv);
#if !SHADING_UMAJOR
valueIdx = microdata.umajor2bmap[baseSubdiv].d[valueIdx];
#endif
OUTvtx[vertOut].vidx = firstValue + valueIdx;
#endif
#elif SURFACEVIS == SURFACEVIS_ANISOTROPY
OUTvtx[vertOut].vwPos = wPos.xyz;
#endif
}
//////////////////////////////////////
// Primitive Iteration Phase
#if SURFACEVIS == SURFACEVIS_VALUERANGE
// FIXME tri index can actually be wrong here
// okay for the baking app
float valueRange = getValueRange( float(microdata.basetriangleMinMaxs.d[tri * 2 + 0]) / float (0x7FF),
float(microdata.basetriangleMinMaxs.d[tri * 2 + 1]) / float (0x7FF));
#else
float valueRange = 0;
#endif
uint numTriangles = smicrodec_getNumTriangles(sdec);
uint formatIdx = smicrodec_getFormatIdx(sdec);
uint microSubdiv = smicrodec_getMicroSubdiv(sdec);
// Generate primitives
#if USE_PRIMITIVE_CULLING
uint numTrianglesOut = 0;
#else
uint numTrianglesOut = numTriangles;
#endif
for (uint primIter = 0; primIter < smicrodec_getIterationCount(); primIter++)
{
MicroDecodedTriangle decoded = smicrodec_getTriangle(sdec, primIter);
bool visible = decoded.valid;
#if USE_PRIMITIVE_CULLING
if (visible) {
RasterVertex a = getRasterVertex(gl_MeshVerticesNV[decoded.indices.x].gl_Position);
RasterVertex b = getRasterVertex(gl_MeshVerticesNV[decoded.indices.y].gl_Position);
RasterVertex c = getRasterVertex(gl_MeshVerticesNV[decoded.indices.z].gl_Position);
visible = testTriangle(a,b,c, 1.0);
}
uvec4 voteVis = subgroupBallot(visible);
uint primOut = numTrianglesOut + subgroupBallotExclusiveBitCount(voteVis);
numTrianglesOut += subgroupBallotBitCount(voteVis);
#else
uint primOut = decoded.outIndex;
#endif
if (visible)
{
gl_PrimitiveIndicesNV[primOut * 3 + 0] = decoded.indices.x;
gl_PrimitiveIndicesNV[primOut * 3 + 1] = decoded.indices.y;
gl_PrimitiveIndicesNV[primOut * 3 + 2] = decoded.indices.z;
uint id = decoded.localIndex + cfg.partID * numTriangles;
chooseSurfaceVisOutput(gl_MeshPrimitivesNV[primOut].gl_PrimitiveID,
tri, id, formatIdx, microSubdiv - cfg.targetSubdiv, valueRange, baseSubdiv, cfg.targetSubdiv);
}
}
if (laneID == 0)
{
#if !USE_PRIMITIVE_CULLING
numTrianglesOut = validCount * numTriangles;
#endif
gl_PrimitiveCountNV = numTrianglesOut;
#if USE_STATS
atomicAdd(stats.triangles, numTrianglesOut);
#endif
}
}