Skip to content

Commit 214badd

Browse files
committed
Add 16-bit fallback to MDI.
1 parent f819c2c commit 214badd

5 files changed

Lines changed: 31 additions & 6 deletions

File tree

assets/shaders/decode/meshlet_decode.comp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ layout(constant_id = 0) const uint NUM_U32_STREAMS = MESHLET_PAYLOAD_MAX_STREAMS
1010
layout(constant_id = 1) const bool UNROLLED_MESH = false;
1111
layout(constant_id = 2) const uint TARGET_MESH_STYLE = 0;
1212
layout(constant_id = 3) const bool RUNTIME_MESH = false;
13+
layout(constant_id = 4) const bool INDEX16 = false;
1314

1415
#include "../inc/global_bindings.h"
1516
#undef BINDING_GLOBAL_SCENE_MESHLET_STREAM_HEADER_BUFFER
@@ -29,6 +30,11 @@ layout(set = 0, binding = 2, scalar) writeonly buffer OutputIndices
2930
uvec3 data[];
3031
} output_indices32;
3132

33+
layout(set = 0, binding = 2, scalar) writeonly buffer OutputIndices16
34+
{
35+
u16vec3 data[];
36+
} output_indices16;
37+
3238
layout(set = 0, binding = 2, scalar) writeonly buffer OutputIndices8
3339
{
3440
u8vec3 data[];
@@ -117,6 +123,8 @@ void main()
117123

118124
if (UNROLLED_MESH)
119125
output_indices32.data[offsets.primitive_output_offset + lane_index] = indices;
126+
else if (INDEX16)
127+
output_indices16.data[offsets.primitive_output_offset + lane_index] = u16vec3(indices);
120128
else
121129
output_indices8.data[offsets.primitive_output_offset + lane_index] = u8vec3(indices);
122130
}

renderer/renderer.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,15 @@ void Renderer::render_mesh_assets(Vulkan::CommandBuffer &cmd, const RenderContex
674674
auto *pos = manager.get_position_buffer();
675675
auto *attr = manager.get_attribute_buffer();
676676

677-
cmd.set_index_buffer(*ibo, 0,
678-
encoding == ResourceManager::MeshEncoding::Classic
679-
? VK_INDEX_TYPE_UINT32
680-
: VK_INDEX_TYPE_UINT8);
677+
VkIndexType index_type;
678+
if (encoding == ResourceManager::MeshEncoding::Classic)
679+
index_type = VK_INDEX_TYPE_UINT32;
680+
else if (device->get_device_features().vk14_features.indexTypeUint8)
681+
index_type = VK_INDEX_TYPE_UINT8;
682+
else
683+
index_type = VK_INDEX_TYPE_UINT16;
684+
685+
cmd.set_index_buffer(*ibo, 0, index_type);
681686

682687
cmd.set_vertex_binding(0, *pos, 0, 12);
683688
cmd.set_vertex_binding(1, *attr, 0, 16);

vulkan/managers/resource_manager.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,15 @@ void ResourceManager::init_mesh_assets()
143143

144144
if (mesh_encoding != MeshEncoding::MeshletEncoded)
145145
{
146-
unsigned index_size = mesh_encoding == MeshEncoding::Classic ? sizeof(uint32_t) : sizeof(uint8_t);
146+
unsigned index_size;
147+
148+
if (mesh_encoding == MeshEncoding::Classic)
149+
index_size = sizeof(uint32_t);
150+
else if (device->get_device_features().vk14_features.indexTypeUint8)
151+
index_size = sizeof(uint8_t);
152+
else
153+
index_size = sizeof(uint16_t);
154+
147155
index_buffer_allocator.set_element_size(0, 3 * index_size); // 8-bit or 32-bit indices.
148156
attribute_buffer_allocator.set_soa_count(3);
149157
attribute_buffer_allocator.set_element_size(0, sizeof(float) * 3);
@@ -547,6 +555,8 @@ void ResourceManager::instantiate_asset_mesh(Granite::AssetManager &manager_,
547555
info.target_style = view.format_header->style;
548556
if (mesh_encoding == MeshEncoding::Classic)
549557
info.flags |= Meshlet::DECODE_MODE_UNROLLED_MESH;
558+
else if (!device->get_device_features().vk14_features.indexTypeUint8)
559+
info.flags |= Meshlet::DECODE_MODE_INDEX_16;
550560
info.ibo = index_buffer_allocator.get_buffer(0, 0);
551561

552562
for (unsigned i = 0; i < 3; i++)

vulkan/mesh/meshlet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,12 @@ bool decode_mesh(CommandBuffer &cmd, const DecodeInfo &info, const MeshView &vie
196196
cmd.set_storage_buffer(0, 1, *info.payload);
197197
cmd.set_storage_buffer(0, 2, *info.ibo);
198198

199-
cmd.set_specialization_constant_mask(0xf);
199+
cmd.set_specialization_constant_mask(0x1f);
200200
cmd.set_specialization_constant(0, view.format_header->stream_count);
201201
cmd.set_specialization_constant(1, (info.flags & DECODE_MODE_UNROLLED_MESH) != 0);
202202
cmd.set_specialization_constant(2, uint32_t(info.target_style));
203203
cmd.set_specialization_constant(3, uint32_t(meshlet_runtime));
204+
cmd.set_specialization_constant(4, (info.flags & DECODE_MODE_INDEX_16) != 0);
204205

205206
for (unsigned i = 0; i < 3; i++)
206207
cmd.set_storage_buffer(0, 3 + i, info.streams[i] ? *info.streams[i] : *info.streams[0]);

vulkan/mesh/meshlet.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ MeshView create_mesh_view(const Granite::FileMapping &mapping);
130130
enum DecodeModeFlagBits : uint32_t
131131
{
132132
DECODE_MODE_UNROLLED_MESH = 1 << 0,
133+
DECODE_MODE_INDEX_16 = 1 << 1,
133134
};
134135
using DecodeModeFlags = uint32_t;
135136

0 commit comments

Comments
 (0)