Skip to content

Commit

Permalink
vk: rt: add workaround for holes in geometry
Browse files Browse the repository at this point in the history
Once upon a time, there were some BLASes. There were the BLASes for dynamic geometry, that were to be used every frame to draw very dynamic things, like sprites and beams. They were initialized at the very begining of the renderer's lifetime, and were expected to live happily for the entire process duration.
However, an evil NewMap appeared. It didn't care abount anything or anyone, so when it came, it just cleared all the allocators, and allowed other BLASes to be allocated into the same space as dynamic BLASes were already given. This made BLASes live on top of each other, use each others toys and make them fight. They weren't happy.

So we just kill them and creat them again from scratch, when the evil NewMap comes.

The end.

Fixes #729
  • Loading branch information
w23 committed Feb 2, 2024
1 parent 67ea7af commit cba60f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ref/vk/vk_ray_accel.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static struct {
// Stores AS built data. Lifetime similar to render buffer:
// - some portion lives for entire map lifetime
// - some portion lives only for a single frame (may have several frames in flight)
// TODO: unify this with render buffer
// TODO: unify this with render buffer -- really?
// Needs: AS_STORAGE_BIT, SHADER_DEVICE_ADDRESS_BIT
vk_buffer_t accels_buffer;
struct alo_pool_s *accels_buffer_alloc;
Expand Down Expand Up @@ -330,6 +330,7 @@ vk_resource_t RT_VkAccelPrepareTlas(vk_combuf_t *combuf) {
.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR, // | VK_ACCESS_TRANSFER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR,
.buffer = g_accel.accels_buffer.buffer,
// FIXME this is completely wrong. Offset ans size are BLAS-specifig
.offset = instance_offset * sizeof(VkAccelerationStructureInstanceKHR),
.size = g_ray_model_state.frame.instances_count * sizeof(VkAccelerationStructureInstanceKHR),
}};
Expand All @@ -349,6 +350,7 @@ vk_resource_t RT_VkAccelPrepareTlas(vk_combuf_t *combuf) {
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR,
.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
// FIXME also incorrect -- here we must barrier on tlas_geom_buffer, not accels_buffer
.buffer = g_accel.accels_buffer.buffer,
.offset = 0,
.size = VK_WHOLE_SIZE,
Expand Down Expand Up @@ -430,6 +432,7 @@ void RT_VkAccelNewMap(void) {

g_accel.frame.scratch_offset = 0;

// FIXME this clears up memory before its users are deallocated (e.g. dynamic models BLASes)
if (g_accel.accels_buffer_alloc)
aloPoolDestroy(g_accel.accels_buffer_alloc);
g_accel.accels_buffer_alloc = aloPoolCreate(MAX_ACCELS_BUFFER, expected_accels, accels_alignment);
Expand Down
8 changes: 8 additions & 0 deletions ref/vk/vk_ray_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ uint32_t R_VkMaterialModeFromRenderType(vk_render_type_e render_type) {

void RT_RayModel_Clear(void) {
R_DEBuffer_Init(&g_ray_model_state.kusochki_alloc, MAX_KUSOCHKI / 2, MAX_KUSOCHKI / 2);

// FIXME
// This is a dirty workaround for sub-part memory management in this little project
// Accel backing buffer gets cleared on NewMap. Therefore, we need to recreate BLASes for dynamic
// models, even though they might have lived for the entire process lifetime.
// See #729
RT_DynamicModelShutdown();
RT_DynamicModelInit();
}

void XVK_RayModel_ClearForNextFrame( void ) {
Expand Down

0 comments on commit cba60f7

Please sign in to comment.