Skip to content

Commit

Permalink
Making sure to upload reorganized unique vertex buffer. Still not wor…
Browse files Browse the repository at this point in the history
…king. Not sure why
  • Loading branch information
Honeybunch committed Nov 7, 2024
1 parent 862c0e0 commit 8fcec79
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 52 deletions.
5 changes: 4 additions & 1 deletion addons/water/source/tb_ocean_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ void init_ocean_system(ecs_world_t *ecs, TbOceanSystem *sys,

uint64_t meshlets_size = 0;
uint64_t triangles_size = 0;
uint64_t verts_size = 0;
uint32_t max_meshlet_verts = 0;
uint32_t max_meshlet_tris = 0;
tb_get_cluster_sizing(&max_meshlet_verts, &max_meshlet_tris);
Expand All @@ -554,8 +555,10 @@ void init_ocean_system(ecs_world_t *ecs, TbOceanSystem *sys,
meshlets_size += max_prim_meshlets * sizeof(TbMeshlet);
triangles_size +=
max_prim_meshlets * max_meshlet_tris * sizeof(TbPackedTriangle);
verts_size += max_prim_meshlets * max_meshlet_verts * sizeof(uint32_t);
}
sys->ocean_pos_offset = index_size + meshlets_size + triangles_size;
sys->ocean_pos_offset =
index_size + meshlets_size + triangles_size + verts_size;

sys->ocean_patch_mesh2 =
tb_mesh_sys_load_gltf_mesh(ecs, data, asset_path, "ocean", 0);
Expand Down
8 changes: 8 additions & 0 deletions include/tb_common.slangh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ static_assert(sizeof(TbPackedTriangle) == sizeof(uint32_t), "TbPackedTriangle mu
#define TB_MESHLET_TRI_SET(b) \
[[vk::binding(0, b)]] \
RWBuffer<uint32_t> meshlet_prims[];
#define TB_MESHLET_VERT_SET(b) \
[[vk::binding(0, b)]] \
RWBuffer<uint32_t> meshlet_verts[];
#define TB_TEXTURE_SET(b) \
[[vk::binding(0, b)]] \
Texture2D gltf_textures[];
Expand Down Expand Up @@ -156,6 +159,11 @@ TbMeshlet tb_get_meshlet(RWBuffer<uint4> meshlets, uint32_t index) {
return TbMeshlet(m.x, m.y, m.z, m.w);
}

uint32_t tb_get_meshlet_vertex(RWBuffer<uint32_t> verts, TbMeshlet m,
uint32_t index) {
return verts[m.vert_offset + index];
}

uint3 tb_get_meshlet_primitive(RWBuffer<uint32_t> tris, TbMeshlet m,
uint32_t index) {
return tb_unpack_primitive(tris[m.prim_offset + index]);
Expand Down
1 change: 1 addition & 0 deletions include/tb_mesh_rnd_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef struct TbPrimitiveBatch {
VkDescriptorSet draw_set;
VkDescriptorSet meshlet_set;
VkDescriptorSet tri_set;
VkDescriptorSet vert_set;
VkDescriptorSet obj_set;
VkDescriptorSet tex_set;
VkDescriptorSet idx_set;
Expand Down
1 change: 1 addition & 0 deletions include/tb_mesh_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ VkDescriptorSetLayout tb_mesh_sys_get_meshlet_set_layout(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_idx_set(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_meshlet_set(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_triangles_set(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_vertices_set(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_pos_set(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_norm_set(ecs_world_t *ecs);
VkDescriptorSet tb_mesh_sys_get_tan_set(ecs_world_t *ecs);
Expand Down
12 changes: 6 additions & 6 deletions source/tb_depth_two.slangm
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ GLTF_VIEW_SET(0)
GLTF_DRAW_SET(1)
TB_MESHLET_SET(2)
TB_MESHLET_TRI_SET(3)
TB_OBJECT_SET(4)
TB_IDX_SET(5)
TB_POS_SET(6)
TB_MESHLET_VERT_SET(4)
TB_OBJECT_SET(5)
TB_IDX_SET(6)
TB_POS_SET(7)

// TODO: Can we drive these from specialization constants?
#define NUM_THREADS_X 96
Expand Down Expand Up @@ -42,9 +43,8 @@ void mesh(uint32_t gtid: SV_GroupThreadID, uint32_t gid: SV_GroupIndex,
SetMeshOutputCounts(meshlet.vert_count, meshlet.prim_count);

if (gtid < meshlet.vert_count) {
uint32_t local_idx = gtid + meshlet.vert_offset + draw.index_offset;
uint32_t idx =
tb_get_idx(local_idx, mesh_idx, idx_buffers) + draw.vertex_offset;
uint32_t idx = tb_get_meshlet_vertex(meshlet_verts[mesh_idx], meshlet, gtid + draw.index_offset) + draw.vertex_offset;

int3 local_pos =
tb_vert_get_local_pos(draw.perm, idx, mesh_idx, pos_buffers);
float3 world_pos = mul(obj_data.m, float4(local_pos, 1)).xyz;
Expand Down
23 changes: 11 additions & 12 deletions source/tb_gltf_two.slangm
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ GLTF_MATERIAL_SET(1)
GLTF_DRAW_SET(2)
TB_MESHLET_SET(3)
TB_MESHLET_TRI_SET(4)
TB_OBJECT_SET(5)
TB_TEXTURE_SET(6)
TB_IDX_SET(7)
TB_POS_SET(8)
TB_NORM_SET(9)
TB_TAN_SET(10)
TB_UV0_SET(11)
TB_JOINT_SET(12)
TB_WEIGHT_SET(13)
TB_MESHLET_VERT_SET(5)
TB_OBJECT_SET(6)
TB_TEXTURE_SET(7)
TB_IDX_SET(8)
TB_POS_SET(9)
TB_NORM_SET(10)
TB_TAN_SET(11)
TB_UV0_SET(12)
TB_JOINT_SET(13)
TB_WEIGHT_SET(14)

// TODO: Can we drive these from specialization constants?
#define NUM_THREADS_X 96
Expand Down Expand Up @@ -60,9 +61,7 @@ void mesh(uint32_t gtid: SV_GroupThreadID, uint32_t gid: SV_GroupIndex,
SetMeshOutputCounts(meshlet.vert_count, meshlet.prim_count);

if (gtid < meshlet.vert_count) {
uint32_t local_idx = gtid + meshlet.vert_offset + draw.index_offset;
uint32_t idx =
tb_get_idx(local_idx, mesh_idx, idx_buffers) + draw.vertex_offset;
uint32_t idx = tb_get_meshlet_vertex(meshlet_verts[mesh_idx], meshlet, gtid + draw.index_offset)+ draw.vertex_offset;

int3 local_pos =
tb_vert_get_local_pos(draw.perm, idx, mesh_idx, pos_buffers);
Expand Down
28 changes: 17 additions & 11 deletions source/tb_mesh_rnd_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -969,12 +969,12 @@ void prepass_meshlet_record(TracyCGPUContext *gpu_ctx, VkCommandBuffer buffer,
vkCmdSetViewport(buffer, 0, 1, &batch->viewport);
vkCmdSetScissor(buffer, 0, 1, &batch->scissor);

const uint32_t set_count = 8;
const uint32_t set_count = 9;

VkDescriptorSet sets[set_count] = {
prim_batch->view_set, prim_batch->draw_set, prim_batch->meshlet_set,
prim_batch->tri_set, prim_batch->obj_set, prim_batch->idx_set,
prim_batch->pos_set, prim_batch->norm_set};
prim_batch->tri_set, prim_batch->vert_set, prim_batch->obj_set,
prim_batch->idx_set, prim_batch->pos_set, prim_batch->norm_set};
vkCmdBindDescriptorSets(buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0,
set_count, sets, 0, NULL);

Expand Down Expand Up @@ -1076,13 +1076,14 @@ void meshlet_record_common(TracyCGPUContext *gpu_ctx, VkCommandBuffer buffer,
vkCmdSetViewport(buffer, 0, 1, &batch->viewport);
vkCmdSetScissor(buffer, 0, 1, &batch->scissor);

const uint32_t set_count = 12;
const uint32_t set_count = 13;

const VkDescriptorSet sets[set_count] = {
prim_batch->view_set, prim_batch->mat_set, prim_batch->draw_set,
prim_batch->meshlet_set, prim_batch->tri_set, prim_batch->obj_set,
prim_batch->tex_set, prim_batch->idx_set, prim_batch->pos_set,
prim_batch->norm_set, prim_batch->tan_set, prim_batch->uv0_set,
prim_batch->view_set, prim_batch->mat_set, prim_batch->draw_set,
prim_batch->meshlet_set, prim_batch->tri_set, prim_batch->vert_set,
prim_batch->obj_set, prim_batch->tex_set, prim_batch->idx_set,
prim_batch->pos_set, prim_batch->norm_set, prim_batch->tan_set,
prim_batch->uv0_set,
};
vkCmdBindDescriptorSets(buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, layout, 0,
set_count, sets, 0, NULL);
Expand Down Expand Up @@ -1236,13 +1237,14 @@ TbMeshSystem create_mesh_system_internal(ecs_world_t *ecs, TbAllocator gp_alloc,
{
VkPipelineLayoutCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 8,
.setLayoutCount = 9,
.pSetLayouts =
(VkDescriptorSetLayout[8]){
(VkDescriptorSetLayout[9]){
tb_view_sys_get_set_layout(ecs),
sys.draw_set_layout,
meshlet_set_layout,
meshlet_set_layout,
meshlet_set_layout,
tb_render_object_sys_get_set_layout(ecs),
mesh_set_layout,
mesh_set_layout,
Expand Down Expand Up @@ -1296,7 +1298,7 @@ TbMeshSystem create_mesh_system_internal(ecs_world_t *ecs, TbAllocator gp_alloc,

// Create mesh shader pipeline layouts
{
const uint32_t layout_count = 14;
const uint32_t layout_count = 15;
VkPipelineLayoutCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = layout_count,
Expand All @@ -1307,6 +1309,7 @@ TbMeshSystem create_mesh_system_internal(ecs_world_t *ecs, TbAllocator gp_alloc,
sys.draw_set_layout,
meshlet_set_layout,
meshlet_set_layout,
meshlet_set_layout,
tb_render_object_sys_get_set_layout(ecs),
tb_tex_sys_get_set_layout(ecs),
mesh_set_layout,
Expand Down Expand Up @@ -1757,6 +1760,7 @@ void mesh_draw_tick(ecs_iter_t *it) {
tb_auto idx_set = tb_mesh_sys_get_idx_set(ecs);
tb_auto meshlet_set = tb_mesh_sys_get_meshlet_set(ecs);
tb_auto tri_set = tb_mesh_sys_get_triangles_set(ecs);
tb_auto vert_set = tb_mesh_sys_get_vertices_set(ecs);
tb_auto pos_set = tb_mesh_sys_get_pos_set(ecs);
tb_auto norm_set = tb_mesh_sys_get_norm_set(ecs);
tb_auto tan_set = tb_mesh_sys_get_tan_set(ecs);
Expand Down Expand Up @@ -1800,6 +1804,7 @@ void mesh_draw_tick(ecs_iter_t *it) {
.draw_set = opaque_draw_set,
.meshlet_set = meshlet_set,
.tri_set = tri_set,
.vert_set = vert_set,
.obj_set = obj_set,
.tex_set = tex_set,
.idx_set = idx_set,
Expand Down Expand Up @@ -1848,6 +1853,7 @@ void mesh_draw_tick(ecs_iter_t *it) {
.draw_set = trans_draw_set,
.meshlet_set = meshlet_set,
.tri_set = tri_set,
.vert_set = vert_set,
.obj_set = obj_set,
.tex_set = tex_set,
.idx_set = idx_set,
Expand Down
71 changes: 65 additions & 6 deletions source/tb_mesh_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct TbMeshCtx {
VkDescriptorSetLayout meshlet_set_layout;
TbDynDescPool meshlet_desc_pool;
TbDynDescPool triangles_desc_pool;
TbDynDescPool verts_desc_pool;
VkDescriptorSetLayout set_layout;
TbDynDescPool idx_desc_pool;
TbDynDescPool pos_desc_pool;
Expand Down Expand Up @@ -68,6 +69,7 @@ typedef struct TbMeshData {
VkBufferView index_view;
VkBufferView meshlet_view;
VkBufferView tris_view;
VkBufferView verts_view;
VkBufferView attr_views[TB_INPUT_PERM_COUNT];
#endif
} TbMeshData;
Expand Down Expand Up @@ -151,6 +153,7 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
uint64_t index_size = 0;
uint64_t meshlets_size = 0;
uint64_t tris_size = 0;
uint64_t verts_size = 0;
uint64_t geom_size = 0;
uint64_t attr_size_per_type[cgltf_attribute_type_max_enum] = {0};
uint32_t max_meshlet_verts = 0;
Expand Down Expand Up @@ -184,6 +187,7 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
indices->count, max_meshlet_verts, max_meshlet_tris);
meshlets_size += (max_prim_meshlets * sizeof(TbMeshlet));
tris_size += max_prim_meshlets * max_meshlet_tris * sizeof(uint32_t);
verts_size += max_prim_meshlets * max_meshlet_verts * sizeof(uint32_t);

vertex_count = prim->attributes[0].data->count;
for (cgltf_size attr_idx = 0; attr_idx < prim->attributes_count;
Expand Down Expand Up @@ -211,12 +215,13 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
}
}

geom_size = index_size + meshlets_size + tris_size + vertex_size;
geom_size =
index_size + meshlets_size + tris_size + verts_size + vertex_size;
}

uint64_t attr_offset_per_type[cgltf_attribute_type_max_enum] = {0};
{
uint64_t offset = index_size + meshlets_size + tris_size;
uint64_t offset = index_size + meshlets_size + tris_size + verts_size;
for (uint32_t i = 0; i < cgltf_attribute_type_max_enum; ++i) {
tb_auto attr_size = attr_size_per_type[i];
if (attr_size > 0) {
Expand Down Expand Up @@ -262,6 +267,7 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
uint64_t idx_offset = 0;
uint64_t meshlet_offset = index_size;
uint64_t tris_offset = index_size + meshlets_size;
uint64_t verts_offset = index_size + meshlets_size + tris_size;
uint64_t vertex_count = 0;
cgltf_size attr_count = 0;
for (cgltf_size prim_idx = 0; prim_idx < gltf_mesh->primitives_count;
Expand Down Expand Up @@ -331,8 +337,12 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
// Done with these indices
tb_free(tb_thread_alloc, indices_data);

uint64_t tri_count = 0;
uint64_t vert_count = 0;
for (uint64_t i = 0; i < meshlet_count; ++i) {
tb_auto meshlet = TB_DYN_ARR_AT(meshlets, i);
tri_count += meshlet.triangle_count;
vert_count += meshlet.vertex_count;
meshopt_optimizeMeshlet(&meshlet_verts.data[meshlet.vertex_offset],
&meshlet_tris.data[meshlet.triangle_offset],
meshlet.triangle_count, meshlet.vertex_count);
Expand All @@ -351,11 +361,10 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
}

// Pack triangle data
tb_auto tri_count = TB_DYN_ARR_SIZE(meshlet_tris) / 3;
tb_auto packed_tris =
tb_alloc_nm_tp(tb_thread_alloc, tri_count, TbPackedTriangle);
uint32_t tri_idx = 0;
for (uint32_t i = 0; i < tri_count; i += 3) {
for (uint32_t i = 0; i < tri_count * 3; i += 3) {
packed_tris[tri_idx] = (TbPackedTriangle){
meshlet_tris.data[i + 0],
meshlet_tris.data[i + 1],
Expand All @@ -366,8 +375,7 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,

// Copy to triangles region of geometry buffer
{
const uint64_t tris_buffer_size =
sizeof(TbPackedTriangle) * tri_count;
uint64_t tris_buffer_size = tri_count * sizeof(TbPackedTriangle);

void *src = packed_tris;
void *dst = ((uint8_t *)(ptr)) + tris_offset;
Expand All @@ -377,6 +385,17 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
meshlet_count * max_meshlet_tris * sizeof(TbPackedTriangle);
}

// Copy to meshlet verts region of geometry buffer
{
uint64_t verts_buffer_size = vert_count * sizeof(uint32_t);

void *src = meshlet_verts.data;
void *dst = ((uint8_t *)(ptr)) + verts_offset;
SDL_memcpy(dst, src, verts_buffer_size);

verts_offset += meshlet_count * max_meshlet_verts * sizeof(uint32_t);
}

// Clean up arrays
tb_free(tb_thread_alloc, packed_tris);
TB_DYN_ARR_DESTROY(meshlets);
Expand Down Expand Up @@ -501,6 +520,18 @@ TbMeshData tb_load_gltf_mesh(TbRenderSystem *rnd_sys,
tb_rnd_create_buffer_view(rnd_sys, &create_info, "Mesh Triangles View",
&data.tris_view);
}
// Create one buffer view for meshlet vertices
{
VkBufferViewCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,
.buffer = data.gpu_buffer.buffer,
.offset = index_size + meshlets_size + tris_size,
.range = verts_size,
.format = VK_FORMAT_R32_UINT,
};
tb_rnd_create_buffer_view(rnd_sys, &create_info, "Mesh Vertices View",
&data.verts_view);
}

#if TB_USE_DESC_BUFFER == 1
// Set a default buffer for each primitive
Expand Down Expand Up @@ -998,6 +1029,24 @@ void tb_finalize_meshes(ecs_iter_t *it) {
tb_write_dyn_desc_pool(&ctx->triangles_desc_pool, TB_DYN_ARR_SIZE(writes),
writes.data, NULL);
}

// Write vertices descriptor
{
TB_DYN_ARR_OF(TbDynDescWrite) writes = {0};
TB_DYN_ARR_RESET(writes, rnd_tmp_alloc, it->count);

for (int32_t i = 0; i < it->count; ++i) {
tb_auto mesh = &meshes[i];
TbDynDescWrite write = {
.type = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER,
.desc.texel_buffer = mesh->verts_view,
};
TB_DYN_ARR_APPEND(writes, write);
}

tb_write_dyn_desc_pool(&ctx->verts_desc_pool, TB_DYN_ARR_SIZE(writes),
writes.data, NULL);
}
}

void tb_update_mesh_pool(ecs_iter_t *it) {
Expand All @@ -1009,6 +1058,7 @@ void tb_update_mesh_pool(ecs_iter_t *it) {
tb_tick_dyn_desc_pool(rnd_sys, &ctx->idx_desc_pool);
tb_tick_dyn_desc_pool(rnd_sys, &ctx->meshlet_desc_pool);
tb_tick_dyn_desc_pool(rnd_sys, &ctx->triangles_desc_pool);
tb_tick_dyn_desc_pool(rnd_sys, &ctx->verts_desc_pool);
tb_tick_dyn_desc_pool(rnd_sys, &ctx->pos_desc_pool);
tb_tick_dyn_desc_pool(rnd_sys, &ctx->norm_desc_pool);
tb_tick_dyn_desc_pool(rnd_sys, &ctx->tan_desc_pool);
Expand Down Expand Up @@ -1227,6 +1277,10 @@ void tb_register_mesh2_sys(TbWorld *world) {
ctx.meshlet_set_layout,
VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, desc_cap,
&ctx.triangles_desc_pool, 0);
tb_create_dyn_desc_pool(rnd_sys, "Meshlet Vertices Descriptors",
ctx.meshlet_set_layout,
VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, desc_cap,
&ctx.verts_desc_pool, 0);
tb_create_dyn_desc_pool(rnd_sys, "Mesh Index Descriptors", ctx.set_layout,
VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, desc_cap,
&ctx.idx_desc_pool, 0);
Expand Down Expand Up @@ -1315,6 +1369,11 @@ VkDescriptorSet tb_mesh_sys_get_triangles_set(ecs_world_t *ecs) {
tb_auto rnd_sys = ecs_singleton_ensure(ecs, TbRenderSystem);
return tb_dyn_desc_pool_get_set(rnd_sys, &ctx->triangles_desc_pool);
}
VkDescriptorSet tb_mesh_sys_get_vertices_set(ecs_world_t *ecs) {
tb_auto ctx = ecs_singleton_ensure(ecs, TbMeshCtx);
tb_auto rnd_sys = ecs_singleton_ensure(ecs, TbRenderSystem);
return tb_dyn_desc_pool_get_set(rnd_sys, &ctx->verts_desc_pool);
}
VkDescriptorSet tb_mesh_sys_get_pos_set(ecs_world_t *ecs) {
tb_auto ctx = ecs_singleton_ensure(ecs, TbMeshCtx);
tb_auto rnd_sys = ecs_singleton_ensure(ecs, TbRenderSystem);
Expand Down
Loading

0 comments on commit 8fcec79

Please sign in to comment.