Skip to content

Commit

Permalink
Vulkan: Invalidate host visible non-coherent buffers on mapping
Browse files Browse the repository at this point in the history
We can not trust the cache during CPU readback when the buffer
memory type is non-coherent.

Bug: b/366134076
Change-Id: I89920cfa468ee0be0feb607fea9d60bc0732191f
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5890707
Reviewed-by: Shahbaz Youssefi <[email protected]>
Commit-Queue: Shahbaz Youssefi <[email protected]>
Reviewed-by: Charlie Lao <[email protected]>
Auto-Submit: Imran Ziad <[email protected]>
  • Loading branch information
Imran Ziad authored and Angle LUCI CQ committed Oct 1, 2024
1 parent 65ece02 commit 0040cda
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/libANGLE/renderer/vulkan/BufferVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,21 @@ angle::Result BufferVk::handleDeviceLocalBufferMap(ContextVk *contextVk,
return angle::Result::Continue;
}

angle::Result BufferVk::mapHostVisibleBuffer(ContextVk *contextVk,
VkDeviceSize offset,
GLbitfield access,
uint8_t **mapPtr)
{
ANGLE_TRY(mBuffer.mapWithOffset(contextVk, mapPtr, static_cast<size_t>(offset)));

// Invalidate non-coherent for READ case.
if (!mBuffer.isCoherent() && (access & GL_MAP_READ_BIT) != 0)
{
ANGLE_TRY(mBuffer.invalidate(contextVk->getRenderer()));
}
return angle::Result::Continue;
}

angle::Result BufferVk::map(const gl::Context *context, GLenum access, void **mapPtr)
{
ASSERT(mBuffer.valid());
Expand Down Expand Up @@ -773,7 +788,7 @@ angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
{
if (hostVisible)
{
return mBuffer.mapWithOffset(contextVk, mapPtrBytes, static_cast<size_t>(offset));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
return handleDeviceLocalBufferMap(contextVk, offset, length, mapPtrBytes);
}
Expand All @@ -795,7 +810,7 @@ angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
}
if (hostVisible)
{
return mBuffer.mapWithOffset(contextVk, mapPtrBytes, static_cast<size_t>(offset));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
return handleDeviceLocalBufferMap(contextVk, offset, length, mapPtrBytes);
}
Expand All @@ -809,7 +824,7 @@ angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
// Write case, buffer not in use.
if (isExternalBuffer() || !isCurrentlyInUse(contextVk->getRenderer()))
{
return mBuffer.mapWithOffset(contextVk, mapPtrBytes, static_cast<size_t>(offset));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}

// Write case, buffer in use.
Expand All @@ -828,7 +843,7 @@ angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
{
ANGLE_TRY(acquireBufferHelper(contextVk, static_cast<size_t>(mState.getSize()),
BufferUsageType::Dynamic));
return mBuffer.mapWithOffset(contextVk, mapPtrBytes, static_cast<size_t>(offset));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}

bool smallMapRange = (length < static_cast<VkDeviceSize>(mState.getSize()) / 2);
Expand All @@ -849,7 +864,7 @@ angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
// Write case (worst case, buffer in use for write)
ANGLE_TRY(mBuffer.waitForIdle(contextVk, "GPU stall due to mapping buffer in use by the GPU",
RenderPassClosureReason::BufferInUseWhenSynchronizedMap));
return mBuffer.mapWithOffset(contextVk, mapPtrBytes, static_cast<size_t>(offset));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}

angle::Result BufferVk::unmap(const gl::Context *context, GLboolean *result)
Expand Down
4 changes: 4 additions & 0 deletions src/libANGLE/renderer/vulkan/BufferVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ class BufferVk : public BufferImpl
VkDeviceSize offset,
VkDeviceSize size,
uint8_t **mapPtr);
angle::Result mapHostVisibleBuffer(ContextVk *contextVk,
VkDeviceSize offset,
GLbitfield access,
uint8_t **mapPtr);
angle::Result setDataImpl(ContextVk *contextVk,
size_t bufferSize,
const BufferDataSource &dataSource,
Expand Down

0 comments on commit 0040cda

Please sign in to comment.