From 3a807d5643ec6af143a6e831212a6b400802e76a Mon Sep 17 00:00:00 2001 From: "Dunajski, Bartosz" Date: Mon, 23 Jul 2018 13:33:46 +0200 Subject: [PATCH] Render compressed buffer creation flow Change-Id: I58b7f7eb3b69afcb78c6ab3de1f6fb7524d33f13 --- runtime/mem_obj/buffer.cpp | 19 ++++++-- runtime/mem_obj/buffer.h | 1 + unit_tests/mem_obj/buffer_tests.cpp | 75 +++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/runtime/mem_obj/buffer.cpp b/runtime/mem_obj/buffer.cpp index d281ad97b7..905a01f2ac 100644 --- a/runtime/mem_obj/buffer.cpp +++ b/runtime/mem_obj/buffer.cpp @@ -25,6 +25,7 @@ #include "runtime/context/context.h" #include "runtime/device/device.h" #include "runtime/gmm_helper/gmm.h" +#include "runtime/gmm_helper/gmm_helper.h" #include "runtime/helpers/aligned_memory.h" #include "runtime/helpers/hw_info.h" #include "runtime/helpers/ptr_math.h" @@ -93,11 +94,14 @@ Buffer *Buffer::create(Context *context, bool isHostPtrSVM = false; bool allocateMemory = false; bool copyMemoryFromHostPtr = false; + GraphicsAllocation::AllocationType allocationType = GmmHelper::hwInfo->capabilityTable.ftrRenderCompressedBuffers + ? GraphicsAllocation::AllocationType::BUFFER_COMPRESSED + : GraphicsAllocation::AllocationType::BUFFER; MemoryManager *memoryManager = context->getMemoryManager(); UNRECOVERABLE_IF(!memoryManager); - checkMemory(flags, size, hostPtr, errcodeRet, zeroCopy, allocateMemory, copyMemoryFromHostPtr, memoryManager); + checkMemory(flags, size, hostPtr, errcodeRet, zeroCopy, allocateMemory, copyMemoryFromHostPtr, allocationType, memoryManager); if (hostPtr && context->isProvidingPerformanceHints()) { if (zeroCopy) { @@ -108,6 +112,7 @@ Buffer *Buffer::create(Context *context, } if (context->isSharedContext) { + allocationType = GraphicsAllocation::AllocationType::BUFFER; zeroCopy = true; copyMemoryFromHostPtr = false; allocateMemory = false; @@ -117,6 +122,7 @@ Buffer *Buffer::create(Context *context, if (flags & CL_MEM_USE_HOST_PTR) { memory = context->getSVMAllocsManager()->getSVMAlloc(hostPtr); if (memory) { + allocationType = GraphicsAllocation::AllocationType::BUFFER; zeroCopy = true; isHostPtrSVM = true; copyMemoryFromHostPtr = false; @@ -125,7 +131,7 @@ Buffer *Buffer::create(Context *context, } if (!memory) { - memory = memoryManager->allocateGraphicsMemoryInPreferredPool(zeroCopy, allocateMemory, true, false, hostPtr, static_cast(size), GraphicsAllocation::AllocationType::BUFFER); + memory = memoryManager->allocateGraphicsMemoryInPreferredPool(zeroCopy, allocateMemory, true, false, hostPtr, static_cast(size), allocationType); } if (allocateMemory) { @@ -140,7 +146,7 @@ Buffer *Buffer::create(Context *context, zeroCopy = false; copyMemoryFromHostPtr = true; allocateMemory = true; - memory = memoryManager->allocateGraphicsMemoryInPreferredPool(zeroCopy, allocateMemory, true, false, nullptr, static_cast(size), GraphicsAllocation::AllocationType::BUFFER); + memory = memoryManager->allocateGraphicsMemoryInPreferredPool(zeroCopy, allocateMemory, true, false, nullptr, static_cast(size), allocationType); } } @@ -149,7 +155,7 @@ Buffer *Buffer::create(Context *context, break; } - memory->setAllocationType(GraphicsAllocation::AllocationType::BUFFER); + memory->setAllocationType(allocationType); memory->setMemObjectsAllocationWithWritableFlags(!(flags & (CL_MEM_READ_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS))); DBG_LOG(LogMemoryObject, __FUNCTION__, "hostPtr:", hostPtr, "size:", size, "memoryStorage:", memory->getUnderlyingBuffer(), "GPU address:", std::hex, memory->getGpuAddress()); @@ -198,6 +204,7 @@ void Buffer::checkMemory(cl_mem_flags flags, bool &isZeroCopy, bool &allocateMemory, bool ©MemoryFromHostPtr, + GraphicsAllocation::AllocationType allocationType, MemoryManager *memMngr) { errcodeRet = CL_SUCCESS; isZeroCopy = false; @@ -219,6 +226,7 @@ void Buffer::checkMemory(cl_mem_flags flags, if (alignUp(hostPtr, MemoryConstants::cacheLineSize) != hostPtr || alignUp(size, MemoryConstants::cacheLineSize) != size || minAddress > reinterpret_cast(hostPtr) || + GraphicsAllocation::AllocationType::BUFFER_COMPRESSED == allocationType || DebugManager.flags.DisableZeroCopyForUseHostPtr.get() || DebugManager.flags.DisableZeroCopyForBuffers.get()) { allocateMemory = true; @@ -233,7 +241,8 @@ void Buffer::checkMemory(cl_mem_flags flags, } else { allocateMemory = true; isZeroCopy = true; - if (DebugManager.flags.DisableZeroCopyForBuffers.get()) { + if (DebugManager.flags.DisableZeroCopyForBuffers.get() || + GraphicsAllocation::AllocationType::BUFFER_COMPRESSED == allocationType) { isZeroCopy = false; } } diff --git a/runtime/mem_obj/buffer.h b/runtime/mem_obj/buffer.h index 5143bac50d..5cd95ec562 100644 --- a/runtime/mem_obj/buffer.h +++ b/runtime/mem_obj/buffer.h @@ -136,6 +136,7 @@ class Buffer : public MemObj { bool &isZeroCopy, bool &allocateMemory, bool ©MemoryFromHostPtr, + GraphicsAllocation::AllocationType allocationType, MemoryManager *memMngr); static bool isReadOnlyMemoryPermittedByFlags(cl_mem_flags flags); diff --git a/unit_tests/mem_obj/buffer_tests.cpp b/unit_tests/mem_obj/buffer_tests.cpp index 1e3949921b..821d413063 100644 --- a/unit_tests/mem_obj/buffer_tests.cpp +++ b/unit_tests/mem_obj/buffer_tests.cpp @@ -214,6 +214,81 @@ TEST(Buffer, givenNullPtrWhenBufferIsCreatedWithKernelReadOnlyFlagsThenBufferAll EXPECT_EQ(nullptr, buffer.get()); } +TEST(Buffer, givenBufferCompressedAllocationAndZeroCopyHostPtrWhenCheckingMemoryPropertiesThenForceDisableZeroCopyAndAllocateStorage) { + HardwareInfo localHwInfo = *platformDevices[0]; + localHwInfo.capabilityTable.ftrRenderCompressedBuffers = false; + std::unique_ptr device(Device::create(&localHwInfo, new ExecutionEnvironment())); + auto context = std::make_unique(device.get()); + + void *cacheAlignedHostPtr = alignedMalloc(MemoryConstants::cacheLineSize, MemoryConstants::cacheLineSize); + cl_int retVal = CL_SUCCESS; + + std::unique_ptr buffer(Buffer::create(context.get(), CL_MEM_USE_HOST_PTR, MemoryConstants::cacheLineSize, cacheAlignedHostPtr, retVal)); + EXPECT_EQ(cacheAlignedHostPtr, buffer->getGraphicsAllocation()->getUnderlyingBuffer()); + EXPECT_TRUE(buffer->isMemObjZeroCopy()); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER); + + localHwInfo.capabilityTable.ftrRenderCompressedBuffers = true; + buffer.reset(Buffer::create(context.get(), CL_MEM_USE_HOST_PTR, MemoryConstants::cacheLineSize, cacheAlignedHostPtr, retVal)); + EXPECT_NE(cacheAlignedHostPtr, buffer->getGraphicsAllocation()->getUnderlyingBuffer()); + EXPECT_FALSE(buffer->isMemObjZeroCopy()); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); + + alignedFree(cacheAlignedHostPtr); +} + +TEST(Buffer, givenBufferCompressedAllocationAndNoHostPtrWhenCheckingMemoryPropertiesThenForceDisableZeroCopy) { + HardwareInfo localHwInfo = *platformDevices[0]; + localHwInfo.capabilityTable.ftrRenderCompressedBuffers = false; + std::unique_ptr device(Device::create(&localHwInfo, new ExecutionEnvironment())); + auto context = std::make_unique(device.get()); + + cl_int retVal = CL_SUCCESS; + + std::unique_ptr buffer(Buffer::create(context.get(), 0, MemoryConstants::cacheLineSize, nullptr, retVal)); + EXPECT_TRUE(buffer->isMemObjZeroCopy()); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER); + + localHwInfo.capabilityTable.ftrRenderCompressedBuffers = true; + buffer.reset(Buffer::create(context.get(), 0, MemoryConstants::cacheLineSize, nullptr, retVal)); + EXPECT_FALSE(buffer->isMemObjZeroCopy()); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); +} + +TEST(Buffer, givenBufferCompressedAllocationWhenSharedContextIsUsedThenForceDisableCompression) { + HardwareInfo localHwInfo = *platformDevices[0]; + localHwInfo.capabilityTable.ftrRenderCompressedBuffers = true; + std::unique_ptr device(Device::create(&localHwInfo, new ExecutionEnvironment())); + auto context = std::make_unique(device.get()); + context->isSharedContext = false; + + cl_int retVal = CL_SUCCESS; + uint32_t hostPtr = 0; + + std::unique_ptr buffer(Buffer::create(context.get(), 0, sizeof(uint32_t), &hostPtr, retVal)); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); + + context->isSharedContext = true; + buffer.reset(Buffer::create(context.get(), 0, sizeof(uint32_t), &hostPtr, retVal)); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER); +} + +TEST(Buffer, givenSvmAllocationWhenCreatingBufferThenForceDisableCompression) { + HardwareInfo localHwInfo = *platformDevices[0]; + localHwInfo.capabilityTable.ftrRenderCompressedBuffers = true; + std::unique_ptr device(Device::create(&localHwInfo, new ExecutionEnvironment())); + auto context = std::make_unique(device.get()); + + auto svmAlloc = context->getSVMAllocsManager()->createSVMAlloc(sizeof(uint32_t), false); + + cl_int retVal = CL_SUCCESS; + + std::unique_ptr buffer(Buffer::create(context.get(), CL_MEM_USE_HOST_PTR, sizeof(uint32_t), svmAlloc, retVal)); + EXPECT_EQ(buffer->getGraphicsAllocation()->getAllocationType(), GraphicsAllocation::AllocationType::BUFFER); + + context->getSVMAllocsManager()->freeSVMAlloc(svmAlloc); +} + class BufferTest : public DeviceFixture, public testing::TestWithParam { public: