Skip to content

Commit

Permalink
CL/Vulkan: Update map interface for CLMemoryVk
Browse files Browse the repository at this point in the history
Clean up the map interface of the CLMemoryVk to account for any offsets.

Bug: angleproject:369724757
Change-Id: I12c7bf2f7f94cd7fbf57a8ee10748d2275a2ba70
Signed-off-by: Gowtham Tammana <[email protected]>
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5893956
Reviewed-by: Charlie Lao <[email protected]>
Reviewed-by: Shahbaz Youssefi <[email protected]>
  • Loading branch information
gowtham-sarc authored and Angle LUCI CQ committed Oct 3, 2024
1 parent 0a45269 commit b5d548b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
83 changes: 37 additions & 46 deletions src/libANGLE/renderer/vulkan/CLMemoryVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// CLMemoryVk.cpp: Implements the class methods for CLMemoryVk.

#include "libANGLE/renderer/vulkan/CLMemoryVk.h"
#include <cstdint>
#include "libANGLE/Error.h"
#include "libANGLE/renderer/vulkan/CLContextVk.h"
#include "libANGLE/renderer/vulkan/vk_renderer.h"

Expand All @@ -21,7 +23,7 @@ CLMemoryVk::CLMemoryVk(const cl::Memory &memory)
: CLMemoryImpl(memory),
mContext(&memory.getContext().getImpl<CLContextVk>()),
mRenderer(mContext->getRenderer()),
mMapPtr(nullptr),
mMappedMemory(nullptr),
mMapCount(0),
mParent(nullptr)
{}
Expand All @@ -40,16 +42,6 @@ angle::Result CLMemoryVk::createSubBuffer(const cl::Buffer &buffer,
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}

angle::Result CLMemoryVk::getMapPtr(uint8_t **mapPtrOut)
{
if (mMapPtr == nullptr && mapPtrOut != nullptr)
{
ANGLE_TRY(map());
*mapPtrOut = mMapPtr;
}
return angle::Result::Continue;
}

VkBufferUsageFlags CLMemoryVk::getVkUsageFlags()
{
VkBufferUsageFlags usageFlags =
Expand Down Expand Up @@ -85,33 +77,40 @@ VkMemoryPropertyFlags CLMemoryVk::getVkMemPropertyFlags()
return propFlags;
}

angle::Result CLMemoryVk::map(uint8_t *&ptrOut, size_t offset)
{
if (!isMapped())
{
ANGLE_TRY(mapImpl());
}
ptrOut = mMappedMemory + offset;
return angle::Result::Continue;
}

angle::Result CLMemoryVk::copyTo(void *dst, size_t srcOffset, size_t size)
{
ANGLE_TRY(map());
void *src = reinterpret_cast<void *>(mMapPtr + srcOffset);
uint8_t *src = nullptr;
ANGLE_TRY(map(src, srcOffset));
std::memcpy(dst, src, size);
ANGLE_TRY(unmap());
unmap();
return angle::Result::Continue;
}

angle::Result CLMemoryVk::copyTo(CLMemoryVk *dst, size_t srcOffset, size_t dstOffset, size_t size)
{
ANGLE_TRY(map());
ANGLE_TRY(dst->map());
uint8_t *ptr = nullptr;
ANGLE_TRY(dst->getMapPtr(&ptr));
ANGLE_TRY(copyTo(reinterpret_cast<void *>(ptr + dstOffset), srcOffset, size));
ANGLE_TRY(unmap());
ANGLE_TRY(dst->unmap());
uint8_t *dstPtr = nullptr;
ANGLE_TRY(dst->map(dstPtr, dstOffset));
ANGLE_TRY(copyTo(dstPtr, srcOffset, size));
dst->unmap();
return angle::Result::Continue;
}

angle::Result CLMemoryVk::copyFrom(const void *src, size_t srcOffset, size_t size)
{
ANGLE_TRY(map());
void *dst = reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(mMapPtr) + srcOffset);
uint8_t *dst = nullptr;
ANGLE_TRY(map(dst, srcOffset));
std::memcpy(dst, src, size);
ANGLE_TRY(unmap());
unmap();
return angle::Result::Continue;
}

Expand All @@ -130,7 +129,10 @@ CLBufferVk::CLBufferVk(const cl::Buffer &buffer) : CLMemoryVk(buffer)

CLBufferVk::~CLBufferVk()
{
ANGLE_CL_IMPL_TRY(unmap());
if (isMapped())
{
unmap();
}
mBuffer.destroy(mRenderer);
}

Expand Down Expand Up @@ -164,37 +166,26 @@ angle::Result CLBufferVk::create(void *hostPtr)
return angle::Result::Continue;
}

angle::Result CLBufferVk::map()
angle::Result CLBufferVk::mapImpl()
{
if (mParent != nullptr)
{
ANGLE_TRY(mParent->getMapPtr(&mMapPtr));
if (mMapPtr != nullptr)
{
mMapPtr += mBuffer.getOffset();
return angle::Result::Continue;
}
ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
}
ASSERT(!isMapped());

if (!IsError(mBuffer.map(mContext, &mMapPtr)))
if (isSubBuffer())
{
ANGLE_TRY(mParent->map(mMappedMemory, static_cast<size_t>(mBuffer.getOffset())));
return angle::Result::Continue;
}

ANGLE_CL_RETURN_ERROR(CL_OUT_OF_RESOURCES);
ANGLE_TRY(mBuffer.map(mContext, &mMappedMemory));
return angle::Result::Continue;
}

angle::Result CLBufferVk::unmap()
void CLBufferVk::unmapImpl()
{
if (mParent != nullptr)
if (!isSubBuffer())
{
ANGLE_CL_IMPL_TRY_ERROR(mParent->unmap(), CL_OUT_OF_RESOURCES);
return angle::Result::Continue;
mBuffer.unmap(mRenderer);
}
mBuffer.unmap(mRenderer);
mMapPtr = nullptr;
return angle::Result::Continue;
mMappedMemory = nullptr;
}

angle::Result CLBufferVk::setDataImpl(const uint8_t *data, size_t size, size_t offset)
Expand Down
21 changes: 12 additions & 9 deletions src/libANGLE/renderer/vulkan/CLMemoryVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ class CLMemoryVk : public CLMemoryImpl
CLMemoryImpl::Ptr *subBufferOut) override = 0;

virtual vk::BufferHelper &getBuffer() = 0;
angle::Result getMapPtr(uint8_t **mapPtrOut);

angle::Result map(uint8_t *&ptrOut, size_t offset = 0);
void unmap() { unmapImpl(); }

VkBufferUsageFlags getVkUsageFlags();
VkMemoryPropertyFlags getVkMemPropertyFlags();
size_t getSize() const { return mMemory.getSize(); }

virtual angle::Result map() = 0;
virtual angle::Result unmap() = 0;
angle::Result copyTo(void *ptr, size_t offset, size_t size);
angle::Result copyTo(CLMemoryVk *dst, size_t srcOffset, size_t dstOffset, size_t size);
angle::Result copyFrom(const void *ptr, size_t offset, size_t size);
Expand All @@ -51,16 +52,19 @@ class CLMemoryVk : public CLMemoryImpl
}

virtual bool isCurrentlyInUse() const = 0;
virtual size_t getSize() const = 0;
bool isMapped() const { return mMappedMemory != nullptr; }

protected:
CLMemoryVk(const cl::Memory &memory);

virtual angle::Result mapImpl() = 0;
virtual void unmapImpl() = 0;

CLContextVk *mContext;
vk::Renderer *mRenderer;
vk::Allocation mAllocation;
angle::SimpleMutex mMapLock;
uint8_t *mMapPtr;
uint8_t *mMappedMemory;
uint32_t mMapCount;
CLMemoryVk *mParent;
};
Expand All @@ -83,13 +87,12 @@ class CLBufferVk : public CLMemoryVk

bool isSubBuffer() const { return mParent != nullptr; }

angle::Result map() override;
angle::Result unmap() override;

bool isCurrentlyInUse() const override;
size_t getSize() const override { return mMemory.getSize(); }

private:
angle::Result mapImpl() override;
void unmapImpl() override;

angle::Result setDataImpl(const uint8_t *data, size_t size, size_t offset);

vk::BufferHelper mBuffer;
Expand Down

0 comments on commit b5d548b

Please sign in to comment.