From b89085510fd5bce075b51ae773e24f649ef0e384 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Sat, 13 Jan 2024 14:46:07 +0100 Subject: [PATCH] Handle arbitrary size in encoded path as well. --- assets/shaders/inc/meshlet_primitive_cull.h | 6 ++-- tests/assets/shaders/meshlet_debug.mesh | 36 ++++++++++--------- tests/assets/shaders/meshlet_debug_plain.mesh | 23 ++++++------ 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/assets/shaders/inc/meshlet_primitive_cull.h b/assets/shaders/inc/meshlet_primitive_cull.h index f931d62e..8b338204 100644 --- a/assets/shaders/inc/meshlet_primitive_cull.h +++ b/assets/shaders/inc/meshlet_primitive_cull.h @@ -168,12 +168,12 @@ uint meshlet_get_meshlet_index() return gl_WorkGroupSize.y == 8 ? gl_WorkGroupID.x : gl_WorkGroupID.y; } -uint meshlet_get_sublet_index(uint meshlet_index, uint sublet_index) +uint meshlet_get_sublet_index(uint sublet_index) { if (gl_WorkGroupSize.y == 8) - return 8u * meshlet_index + sublet_index; + return sublet_index; else - return 8u * meshlet_index + gl_WorkGroupSize.y * gl_WorkGroupID.x + sublet_index; + return gl_WorkGroupSize.y * gl_WorkGroupID.x + sublet_index; } void meshlet_emit_primitive(uvec3 prim, vec4 clip_pos, vec4 viewport) diff --git a/tests/assets/shaders/meshlet_debug.mesh b/tests/assets/shaders/meshlet_debug.mesh index 3bbebab4..3910a57b 100644 --- a/tests/assets/shaders/meshlet_debug.mesh +++ b/tests/assets/shaders/meshlet_debug.mesh @@ -79,10 +79,11 @@ layout(set = 0, binding = 10) buffer Stats uint vert; } stats; +const uint STRIDE = 4; // TODO: Spec constant or push constant + void main() { uint compacted_meshlet_index = meshlet_get_meshlet_index() + 0x8000 * gl_DrawIDARB; - uint linear_index = gl_LocalInvocationIndex; #if defined(MESHLET_RENDER_TASK_HIERARCHICAL) && !MESHLET_RENDER_TASK_HIERARCHICAL CompactedDrawInfo task = mesh_payload.info; @@ -91,35 +92,38 @@ void main() CompactedDrawInfo task = mesh_payload.infos[compacted_meshlet_index]; #endif - const uint STRIDE = 4; - MeshletMetaRuntime meta = meshlet_metas_runtime.data[task.meshlet_index]; - meta.stream_offset += STRIDE * gl_WorkGroupID.x; - mat4 M = transforms.data[task.node_offset]; - uint lane_index; - + uint linear_index, sublet_index; if (gl_SubgroupSize == 32) - lane_index = gl_SubgroupInvocationID; + { + linear_index = gl_SubgroupInvocationID; + sublet_index = gl_SubgroupID; + } else - lane_index = gl_LocalInvocationID.x; + { + linear_index = gl_LocalInvocationID.x; + sublet_index = gl_LocalInvocationID.y; + } - meshlet_setup_local_invocation(uvec2(lane_index, 0)); + meshlet_setup_local_invocation(uvec2(linear_index, sublet_index)); + MeshletMetaRuntime meta = meshlet_metas_runtime.data[task.meshlet_index]; + meta.stream_offset += STRIDE * meshlet_get_sublet_index(sublet_index); MeshletInfo info = meshlet_get_meshlet_info(meta.stream_offset); uvec3 decoded_index_buffer = uvec3(0); vec3 world_pos; vec4 clip_pos = vec4(-1.0); - if (lane_index < info.primitive_count) - decoded_index_buffer = meshlet_decode_index_buffer(meta.stream_offset, lane_index); + if (linear_index < info.primitive_count) + decoded_index_buffer = meshlet_decode_index_buffer(meta.stream_offset, linear_index); - if (lane_index < info.vertex_count) + if (linear_index < info.vertex_count) { int exponent; i16vec3 ipos = meshlet_decode_snorm_scaled_i16x3( meta.stream_offset + MESHLET_STREAM_TYPE_POSITION, - lane_index, exponent); + linear_index, exponent); vec3 pos = ldexp(vec3(ipos), ivec3(exponent)); world_pos = (M * vec4(pos, 1.0)).xyz; clip_pos = VP * vec4(world_pos, 1.0); @@ -135,10 +139,10 @@ void main() bool t_sign; u8vec4 nt = meshlet_decode_normal_tangent_oct8( meta.stream_offset + MESHLET_STREAM_TYPE_NORMAL_TANGENT_OCT8, - lane_index, t_sign); + linear_index, t_sign); i16vec2 uv = meshlet_decode_snorm_scaled_i16x2( meta.stream_offset + MESHLET_STREAM_TYPE_UV, - lane_index, exponent); + linear_index, exponent); #ifdef MESHLET_PRIMITIVE_CULL_SHARED_INDEX shared_clip_pos[out_vert_index] = clip_pos; diff --git a/tests/assets/shaders/meshlet_debug_plain.mesh b/tests/assets/shaders/meshlet_debug_plain.mesh index c5c26c42..9b2136c9 100644 --- a/tests/assets/shaders/meshlet_debug_plain.mesh +++ b/tests/assets/shaders/meshlet_debug_plain.mesh @@ -119,15 +119,20 @@ void main() CompactedDrawInfo task = mesh_payload.infos[compacted_meshlet_index]; #endif -#if defined(MESHLET_PRIMITIVE_CULL_WAVE32) && MESHLET_PRIMITIVE_CULL_WAVE32 - uint linear_index = gl_SubgroupInvocationID; - uint sublet_index = gl_SubgroupID; -#else - uint linear_index = gl_LocalInvocationID.x; - uint sublet_index = gl_LocalInvocationID.y; -#endif + uint linear_index, sublet_index; + if (gl_SubgroupSize == 32) + { + linear_index = gl_SubgroupInvocationID; + sublet_index = gl_SubgroupID; + } + else + { + linear_index = gl_LocalInvocationID.x; + sublet_index = gl_LocalInvocationID.y; + } - sublet_index = meshlet_get_sublet_index(task.meshlet_index, sublet_index); + meshlet_setup_local_invocation(uvec2(linear_index, sublet_index)); + sublet_index = 8u * task.meshlet_index + meshlet_get_sublet_index(sublet_index); IndirectDrawMesh meshlet = indirect_commands_mesh.draws[sublet_index]; mat4 M = transforms.data[task.node_offset]; @@ -137,8 +142,6 @@ void main() vec3 world_pos = (M * vec4(pos, 1.0)).xyz; vec4 clip_pos = VP * vec4(world_pos, 1.0); - meshlet_setup_local_invocation(gl_LocalInvocationID.xy); - uvec3 prim = uvec3(0); if (linear_index < meshlet.primitive_count) prim = uvec3(ibo.data[meshlet.primitive_offset + linear_index]);