Skip to content

Commit

Permalink
vk: make sure deleted textures are not referenced in staging
Browse files Browse the repository at this point in the history
This fixes -vkvalidate and fixes #464
  • Loading branch information
w23 committed Mar 4, 2023
1 parent 8ed23cb commit 784a5e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 5 additions & 5 deletions ref/vk/vk_staging.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ void R_VkStagingShutdown(void) {
R_VkCommandPoolDestroy( &g_staging.upload_pool );
}

static void flushStagingBufferSync(void) {
void R_VkStagingFlushSync( void ) {
const VkCommandBuffer cmdbuf = R_VkStagingCommit();
if (!cmdbuf)
return;

XVK_CHECK(vkEndCommandBuffer(cmdbuf));
g_staging.cmdbuf = VK_NULL_HANDLE;

gEngine.Con_Reportf(S_WARN "flushing staging buffer img count=%d\n", g_staging.images.count);
//gEngine.Con_Reportf(S_WARN "flushing staging buffer img count=%d\n", g_staging.images.count);

const VkSubmitInfo subinfo = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
Expand All @@ -84,14 +84,14 @@ static uint32_t allocateInRing(uint32_t size, uint32_t alignment) {
if (offset != ALO_ALLOC_FAILED)
return offset;

flushStagingBufferSync();
R_VkStagingFlushSync();

return R_FlippingBuffer_Alloc(&g_staging.buffer_alloc, size, alignment );
}

vk_staging_region_t R_VkStagingLockForBuffer(vk_staging_buffer_args_t args) {
if ( g_staging.buffers.count >= MAX_STAGING_ALLOCS )
flushStagingBufferSync();
R_VkStagingFlushSync();

const uint32_t offset = allocateInRing(args.size, args.alignment);
if (offset == ALO_ALLOC_FAILED)
Expand All @@ -116,7 +116,7 @@ vk_staging_region_t R_VkStagingLockForBuffer(vk_staging_buffer_args_t args) {

vk_staging_region_t R_VkStagingLockForImage(vk_staging_image_args_t args) {
if ( g_staging.images.count >= MAX_STAGING_ALLOCS )
flushStagingBufferSync();
R_VkStagingFlushSync();

const uint32_t offset = allocateInRing(args.size, args.alignment);
if (offset == ALO_ALLOC_FAILED)
Expand Down
4 changes: 4 additions & 0 deletions ref/vk/vk_staging.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ VkCommandBuffer R_VkStagingFrameEnd(void);
// Gets the current command buffer.
// WARNING: Can be invalidated by any of the Lock calls
VkCommandBuffer R_VkStagingGetCommandBuffer(void);

// Commit all staging data into current cmdbuf, submit it and wait for completion.
// Needed for CPU-GPU sync
void R_VkStagingFlushSync( void );
5 changes: 4 additions & 1 deletion ref/vk/vk_textures.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,10 @@ void VK_FreeTexture( unsigned int texnum ) {
gEngine.FS_FreeImage( tex->original );
*/

// TODO how to do this properly?
// Need to make sure that there are no references to this texture anywhere.
// It might have been added to staging and then immediately deleted, leaving references to its vkimage
// in the staging command buffer. See https://github.com/w23/xash3d-fwgs/issues/464
R_VkStagingFlushSync();
XVK_CHECK(vkDeviceWaitIdle(vk_core.device));

XVK_ImageDestroy(&tex->vk.image);
Expand Down

0 comments on commit 784a5e3

Please sign in to comment.