From 0cc10e47cc257b6f5b150fcc7c4a73d5f4c35381 Mon Sep 17 00:00:00 2001 From: "Zdunowski, Piotr" Date: Mon, 11 Jun 2018 14:58:35 +0200 Subject: [PATCH] Use device instead of context when programing surface state. Change-Id: I67615036d373cf905762a43a92562bf3d84854a5 --- runtime/command_queue/command_queue.cpp | 2 +- runtime/kernel/kernel.cpp | 18 ++++++++--------- runtime/mem_obj/buffer.cpp | 23 ++++++++++++++++++++-- runtime/mem_obj/buffer.h | 12 ++++++++++- runtime/program/printf_handler.cpp | 4 ++-- unit_tests/kernel/kernel_arg_svm_tests.cpp | 6 ++++-- unit_tests/mem_obj/buffer_tests.cpp | 17 ++++++++-------- unit_tests/mocks/mock_buffer.h | 13 ++++++------ 8 files changed, 64 insertions(+), 31 deletions(-) diff --git a/runtime/command_queue/command_queue.cpp b/runtime/command_queue/command_queue.cpp index c1f27c4c32..9a93ec2904 100644 --- a/runtime/command_queue/command_queue.cpp +++ b/runtime/command_queue/command_queue.cpp @@ -578,7 +578,7 @@ bool CommandQueue::setupDebugSurface(Kernel *kernel) { kernel->getKernelInfo().patchInfo.pAllocateSystemThreadSurface->Offset); void *addressToPatch = reinterpret_cast(debugSurface->getGpuAddress()); size_t sizeToPatch = debugSurface->getUnderlyingBufferSize(); - Buffer::setSurfaceState(context, surfaceState, sizeToPatch, addressToPatch, debugSurface); + Buffer::setSurfaceState(device, surfaceState, sizeToPatch, addressToPatch, debugSurface); return true; } diff --git a/runtime/kernel/kernel.cpp b/runtime/kernel/kernel.cpp index b89dd239be..90b82db582 100644 --- a/runtime/kernel/kernel.cpp +++ b/runtime/kernel/kernel.cpp @@ -171,7 +171,7 @@ void Kernel::patchWithImplicitSurface(void *ptrToPatchInCrossThreadData, Graphic auto surfaceState = ptrOffset(ssh, sshOffset); void *addressToPatch = reinterpret_cast(allocation.getUnderlyingBuffer()); size_t sizeToPatch = allocation.getUnderlyingBufferSize(); - Buffer::setSurfaceState(&getContext(), surfaceState, sizeToPatch, addressToPatch, &allocation); + Buffer::setSurfaceState(&getDevice(), surfaceState, sizeToPatch, addressToPatch, &allocation); } } @@ -295,7 +295,7 @@ cl_int Kernel::initialize() { if (requiresSshForBuffers()) { auto surfaceState = ptrOffset(reinterpret_cast(getSurfaceStateHeap()), patchInfo.pAllocateStatelessEventPoolSurface->SurfaceStateHeapOffset); - Buffer::setSurfaceState(&getContext(), surfaceState, 0, nullptr); + Buffer::setSurfaceState(&getDevice(), surfaceState, 0, nullptr); } } @@ -304,7 +304,7 @@ cl_int Kernel::initialize() { if (requiresSshForBuffers()) { auto surfaceState = ptrOffset(reinterpret_cast(getSurfaceStateHeap()), patchInfo.pAllocateStatelessDefaultDeviceQueueSurface->SurfaceStateHeapOffset); - Buffer::setSurfaceState(&getContext(), surfaceState, 0, nullptr); + Buffer::setSurfaceState(&getDevice(), surfaceState, 0, nullptr); } } @@ -841,7 +841,7 @@ cl_int Kernel::setArgSvm(uint32_t argIndex, size_t svmAllocSize, void *svmPtr, G if (requiresSshForBuffers()) { const auto &kernelArgInfo = kernelInfo.kernelArgInfo[argIndex]; auto surfaceState = ptrOffset(getSurfaceStateHeap(), kernelArgInfo.offsetHeap); - Buffer::setSurfaceState(&getContext(), surfaceState, svmAllocSize + ptrDiff(svmPtr, ptrToPatch), ptrToPatch, svmAlloc, svmFlags); + Buffer::setSurfaceState(&getDevice(), surfaceState, svmAllocSize + ptrDiff(svmPtr, ptrToPatch), ptrToPatch, svmAlloc, svmFlags); } if (!kernelArguments[argIndex].isPatched) { patchedArgumentsNum++; @@ -875,7 +875,7 @@ cl_int Kernel::setArgSvmAlloc(uint32_t argIndex, void *svmPtr, GraphicsAllocatio size_t offset = ptrDiff(ptrToPatch, svmAlloc->getUnderlyingBuffer()); allocSize -= offset; } - Buffer::setSurfaceState(&getContext(), surfaceState, allocSize, ptrToPatch, nullptr); + Buffer::setSurfaceState(&getDevice(), surfaceState, allocSize, ptrToPatch, nullptr); } if (!kernelArguments[argIndex].isPatched) { @@ -1141,7 +1141,7 @@ cl_int Kernel::setArgBuffer(uint32_t argIndex, if (requiresSshForBuffers()) { auto surfaceState = ptrOffset(getSurfaceStateHeap(), kernelArgInfo.offsetHeap); - Buffer::setSurfaceState(&getContext(), surfaceState, 0, nullptr); + Buffer::setSurfaceState(&getDevice(), surfaceState, 0, nullptr); } return CL_SUCCESS; @@ -1188,7 +1188,7 @@ cl_int Kernel::setArgPipe(uint32_t argIndex, if (requiresSshForBuffers()) { auto surfaceState = ptrOffset(getSurfaceStateHeap(), kernelArgInfo.offsetHeap); - Buffer::setSurfaceState(&getContext(), surfaceState, + Buffer::setSurfaceState(&getDevice(), surfaceState, pipe->getSize(), pipe->getCpuAddress(), pipe->getGraphicsAllocation()); } @@ -2016,7 +2016,7 @@ void Kernel::patchDefaultDeviceQueue(DeviceQueue *devQueue) { if (requiresSshForBuffers()) { auto surfaceState = ptrOffset(reinterpret_cast(getSurfaceStateHeap()), patchInfo.pAllocateStatelessDefaultDeviceQueueSurface->SurfaceStateHeapOffset); - Buffer::setSurfaceState(&getContext(), surfaceState, devQueue->getQueueBuffer()->getUnderlyingBufferSize(), (void *)devQueue->getQueueBuffer()->getGpuAddress(), devQueue->getQueueBuffer()); + Buffer::setSurfaceState(&getDevice(), surfaceState, devQueue->getQueueBuffer()->getUnderlyingBufferSize(), (void *)devQueue->getQueueBuffer()->getGpuAddress(), devQueue->getQueueBuffer()); } } } @@ -2036,7 +2036,7 @@ void Kernel::patchEventPool(DeviceQueue *devQueue) { if (requiresSshForBuffers()) { auto surfaceState = ptrOffset(reinterpret_cast(getSurfaceStateHeap()), patchInfo.pAllocateStatelessEventPoolSurface->SurfaceStateHeapOffset); - Buffer::setSurfaceState(&getContext(), surfaceState, devQueue->getEventPoolBuffer()->getUnderlyingBufferSize(), (void *)devQueue->getEventPoolBuffer()->getGpuAddress(), devQueue->getEventPoolBuffer()); + Buffer::setSurfaceState(&getDevice(), surfaceState, devQueue->getEventPoolBuffer()->getUnderlyingBufferSize(), (void *)devQueue->getEventPoolBuffer()->getGpuAddress(), devQueue->getEventPoolBuffer()); } } } diff --git a/runtime/mem_obj/buffer.cpp b/runtime/mem_obj/buffer.cpp index 194adf4d65..c112eff31a 100644 --- a/runtime/mem_obj/buffer.cpp +++ b/runtime/mem_obj/buffer.cpp @@ -374,13 +374,32 @@ Buffer *Buffer::createBufferHw(Context *context, return pBuffer; } -void Buffer::setSurfaceState(Context *context, +Buffer *Buffer::createBufferHwFromDevice(const Device *device, + cl_mem_flags flags, + size_t size, + void *memoryStorage, + void *hostPtr, + GraphicsAllocation *gfxAllocation, + bool zeroCopy, + bool isHostPtrSVM, + bool isImageRedescribed) { + + const auto &hwInfo = device->getHardwareInfo(); + + auto funcCreate = bufferFactory[hwInfo.pPlatform->eRenderCoreFamily].createBufferFunction; + DEBUG_BREAK_IF(nullptr == funcCreate); + auto pBuffer = funcCreate(nullptr, flags, size, memoryStorage, hostPtr, gfxAllocation, + zeroCopy, isHostPtrSVM, isImageRedescribed); + return pBuffer; +} + +void Buffer::setSurfaceState(const Device *device, void *surfaceState, size_t svmSize, void *svmPtr, GraphicsAllocation *gfxAlloc, cl_mem_flags flags) { - auto buffer = Buffer::createBufferHw(context, flags, svmSize, svmPtr, svmPtr, gfxAlloc, false, false, false); + auto buffer = Buffer::createBufferHwFromDevice(device, flags, svmSize, svmPtr, svmPtr, gfxAlloc, false, false, false); buffer->setArgStateful(surfaceState); buffer->graphicsAllocation = nullptr; delete buffer; diff --git a/runtime/mem_obj/buffer.h b/runtime/mem_obj/buffer.h index 4bc5529e8a..5143bac50d 100644 --- a/runtime/mem_obj/buffer.h +++ b/runtime/mem_obj/buffer.h @@ -75,11 +75,21 @@ class Buffer : public MemObj { bool isHostPtrSVM, bool isImageRedescribed); + static Buffer *createBufferHwFromDevice(const Device *device, + cl_mem_flags flags, + size_t size, + void *memoryStorage, + void *hostPtr, + GraphicsAllocation *gfxAllocation, + bool zeroCopy, + bool isHostPtrSVM, + bool isImageRedescribed); + Buffer *createSubBuffer(cl_mem_flags flags, const cl_buffer_region *region, cl_int &errcodeRet); - static void setSurfaceState(Context *context, + static void setSurfaceState(const Device *device, void *surfaceState, size_t svmSize, void *svmPtr, diff --git a/runtime/program/printf_handler.cpp b/runtime/program/printf_handler.cpp index f9dcb66b28..cc4edefb1b 100644 --- a/runtime/program/printf_handler.cpp +++ b/runtime/program/printf_handler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (c) 2017 - 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -66,7 +66,7 @@ void PrintfHandler::prepareDispatch(const MultiDispatchInfo &multiDispatchInfo) kernel->getKernelInfo().patchInfo.pAllocateStatelessPrintfSurface->SurfaceStateHeapOffset); void *addressToPatch = printfSurface->getUnderlyingBuffer(); size_t sizeToPatch = printfSurface->getUnderlyingBufferSize(); - Buffer::setSurfaceState(&kernel->getContext(), surfaceState, sizeToPatch, addressToPatch, printfSurface); + Buffer::setSurfaceState(&device, surfaceState, sizeToPatch, addressToPatch, printfSurface); } } diff --git a/unit_tests/kernel/kernel_arg_svm_tests.cpp b/unit_tests/kernel/kernel_arg_svm_tests.cpp index 00f3cbb343..1be0c69422 100644 --- a/unit_tests/kernel/kernel_arg_svm_tests.cpp +++ b/unit_tests/kernel/kernel_arg_svm_tests.cpp @@ -263,7 +263,7 @@ HWTEST_F(KernelArgSvmTest, PatchWithImplicitSurface) { { void *addressToPatch = svmAlloc.getUnderlyingBuffer(); size_t sizeToPatch = svmAlloc.getUnderlyingBufferSize(); - Buffer::setSurfaceState(pContext, &expectedSurfaceState, sizeToPatch, addressToPatch, &svmAlloc); + Buffer::setSurfaceState(pDevice, &expectedSurfaceState, sizeToPatch, addressToPatch, &svmAlloc); } // verify ssh was properly patched @@ -369,6 +369,8 @@ HWTEST_TYPED_TEST(KernelArgSvmTestTyped, GivenBufferKernelArgWhenBufferOffsetIsN using RENDER_SURFACE_STATE = typename FamilyType::RENDER_SURFACE_STATE; constexpr size_t rendSurfSize = sizeof(RENDER_SURFACE_STATE); + std::unique_ptr device(Device::create(*platformDevices)); + uint32_t svmSize = MemoryConstants::pageSize; char *svmPtr = reinterpret_cast(alignedMalloc(svmSize, MemoryConstants::pageSize)); @@ -416,7 +418,7 @@ HWTEST_TYPED_TEST(KernelArgSvmTestTyped, GivenBufferKernelArgWhenBufferOffsetIsN EXPECT_EQ(0U, *expectedOffsetPatchPtr); } - Buffer::setSurfaceState(this->pContext, &expectedSurfaceState, svmAlloc.getUnderlyingBufferSize(), svmAlloc.getUnderlyingBuffer(), &svmAlloc); + Buffer::setSurfaceState(device.get(), &expectedSurfaceState, svmAlloc.getUnderlyingBufferSize(), svmAlloc.getUnderlyingBuffer(), &svmAlloc); // verify ssh was properly patched int32_t cmpResult = memcmp(&expectedSurfaceState, surfState, rendSurfSize); diff --git a/unit_tests/mem_obj/buffer_tests.cpp b/unit_tests/mem_obj/buffer_tests.cpp index 2eca2491ab..115c3f4d16 100644 --- a/unit_tests/mem_obj/buffer_tests.cpp +++ b/unit_tests/mem_obj/buffer_tests.cpp @@ -765,10 +765,11 @@ TEST(SharedBuffersTest, whenBuffersIsCreatedWithSharingHandlerThenItIsSharedBuff class BufferTests : public ::testing::Test { protected: void SetUp() override { + device.reset(Device::create(*platformDevices)); } void TearDown() override { } - MockContext context; + std::unique_ptr device; }; typedef BufferTests BufferSetSurfaceTests; @@ -782,7 +783,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemoryPtrAndSizeIsAlign RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, size, ptr); @@ -804,7 +805,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemoryPtrIsUnalignedToC RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, size, offsetedPtr); @@ -826,7 +827,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemorySizeIsUnalignedTo RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, offsetedSize, ptr); @@ -848,7 +849,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemoryIsUnalignedToCach RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, offsetedSize, ptr, @@ -872,7 +873,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemorySizeIsUnalignedTh RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, offsetedSize, ptr); @@ -892,7 +893,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemoryPtrIsNotNullThenB RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, size, ptr); @@ -909,7 +910,7 @@ HWTEST_F(BufferSetSurfaceTests, givenBufferSetSurfaceThatMemoryPtrIsNullThenNull RENDER_SURFACE_STATE surfaceState = {}; Buffer::setSurfaceState( - &context, + device.get(), &surfaceState, 0, nullptr); diff --git a/unit_tests/mocks/mock_buffer.h b/unit_tests/mocks/mock_buffer.h index 0fbbee5d51..b854c20f35 100644 --- a/unit_tests/mocks/mock_buffer.h +++ b/unit_tests/mocks/mock_buffer.h @@ -25,6 +25,7 @@ #include "runtime/mem_obj/buffer.h" #include "unit_tests/mocks/mock_graphics_allocation.h" #include "unit_tests/mocks/mock_context.h" +#include "unit_tests/mocks/mock_device.h" using namespace OCLRT; @@ -56,9 +57,9 @@ class MockBuffer : public MockBufferStorage, public Buffer { } } void setArgStateful(void *memory) override { - Buffer::setSurfaceState(&context, memory, getSize(), getCpuAddress(), (externalAlloc != nullptr) ? externalAlloc : &mockGfxAllocation); + Buffer::setSurfaceState(device.get(), memory, getSize(), getCpuAddress(), (externalAlloc != nullptr) ? externalAlloc : &mockGfxAllocation); } - MockContext context; + std::unique_ptr device = std::unique_ptr(OCLRT::Device::create(nullptr)); GraphicsAllocation *externalAlloc = nullptr; }; @@ -69,9 +70,9 @@ class AlignedBuffer : public MockBufferStorage, public Buffer { AlignedBuffer(GraphicsAllocation *gfxAllocation) : MockBufferStorage(), Buffer(nullptr, CL_MEM_USE_HOST_PTR, sizeof(data) / 2, alignUp(&data, 64), alignUp(&data, 64), gfxAllocation, true, false, false) { } void setArgStateful(void *memory) override { - Buffer::setSurfaceState(&context, memory, getSize(), getCpuAddress(), &mockGfxAllocation); + Buffer::setSurfaceState(device.get(), memory, getSize(), getCpuAddress(), &mockGfxAllocation); } - MockContext context; + std::unique_ptr device = std::unique_ptr(OCLRT::Device::create(nullptr)); }; class UnalignedBuffer : public MockBufferStorage, public Buffer { @@ -81,7 +82,7 @@ class UnalignedBuffer : public MockBufferStorage, public Buffer { UnalignedBuffer(GraphicsAllocation *gfxAllocation) : MockBufferStorage(true), Buffer(nullptr, CL_MEM_USE_HOST_PTR, sizeof(data) / 2, alignUp(&data, 4), alignUp(&data, 4), gfxAllocation, false, false, false) { } void setArgStateful(void *memory) override { - Buffer::setSurfaceState(&context, memory, getSize(), getCpuAddress(), &mockGfxAllocation); + Buffer::setSurfaceState(device.get(), memory, getSize(), getCpuAddress(), &mockGfxAllocation); } - MockContext context; + std::unique_ptr device = std::unique_ptr(OCLRT::Device::create(nullptr)); };