diff --git a/runtime/memory_manager/os_agnostic_memory_manager.cpp b/runtime/memory_manager/os_agnostic_memory_manager.cpp index f2dee8c796..73259f7b55 100644 --- a/runtime/memory_manager/os_agnostic_memory_manager.cpp +++ b/runtime/memory_manager/os_agnostic_memory_manager.cpp @@ -121,7 +121,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(con } GraphicsAllocation *OsAgnosticMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) { - auto graphicsAllocation = new MemoryAllocation(GraphicsAllocation::AllocationType::UNDECIDED, nullptr, reinterpret_cast(1), 1, + auto graphicsAllocation = new MemoryAllocation(properties.allocationType, nullptr, reinterpret_cast(1), 1, 4096u, static_cast(handle), MemoryPool::SystemCpuInaccessible, false, false, false); graphicsAllocation->setSharedHandle(handle); diff --git a/runtime/os_interface/windows/wddm_memory_manager.cpp b/runtime/os_interface/windows/wddm_memory_manager.cpp index 274c395d7b..8f48b2f693 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.cpp +++ b/runtime/os_interface/windows/wddm_memory_manager.cpp @@ -206,8 +206,8 @@ GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemoryImpl(const All return wddmAllocation.release(); } -GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle) { - auto allocation = std::make_unique(GraphicsAllocation::AllocationType::UNDECIDED, nullptr, 0, handle, MemoryPool::SystemCpuInaccessible, false); +GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle, GraphicsAllocation::AllocationType allocationType) { + auto allocation = std::make_unique(allocationType, nullptr, 0, handle, MemoryPool::SystemCpuInaccessible, false); bool status = ntHandle ? wddm->openNTHandle(reinterpret_cast(static_cast(handle)), allocation.get()) : wddm->openSharedHandle(handle, allocation.get()); @@ -242,11 +242,11 @@ GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handl } GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) { - return createAllocationFromHandle(handle, requireSpecificBitness, false); + return createAllocationFromHandle(handle, requireSpecificBitness, false, properties.allocationType); } GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromNTHandle(void *handle) { - return createAllocationFromHandle((osHandle)((UINT_PTR)handle), false, true); + return createAllocationFromHandle((osHandle)((UINT_PTR)handle), false, true, GraphicsAllocation::AllocationType::SHARED_IMAGE); } void WddmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) { diff --git a/runtime/os_interface/windows/wddm_memory_manager.h b/runtime/os_interface/windows/wddm_memory_manager.h index 3cef273878..fbb4f610da 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.h +++ b/runtime/os_interface/windows/wddm_memory_manager.h @@ -77,7 +77,7 @@ class WddmMemoryManager : public MemoryManager { GraphicsAllocation *allocate32BitGraphicsMemoryImpl(const AllocationData &allocationData) override; GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override; - GraphicsAllocation *createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle); + GraphicsAllocation *createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle, GraphicsAllocation::AllocationType allocationType); static bool validateAllocation(WddmAllocation *alloc); bool createWddmAllocation(WddmAllocation *allocation, void *requiredGpuPtr); bool mapGpuVirtualAddressWithRetry(WddmAllocation *graphicsAllocation, const void *preferredGpuVirtualAddress); diff --git a/unit_tests/d3d_sharing/d3d9_tests.cpp b/unit_tests/d3d_sharing/d3d9_tests.cpp index 8a3553f765..b939af1679 100644 --- a/unit_tests/d3d_sharing/d3d9_tests.cpp +++ b/unit_tests/d3d_sharing/d3d9_tests.cpp @@ -235,6 +235,18 @@ TEST_F(D3D9Tests, createSurfaceIntel) { clReleaseMemObject(memObj); } +TEST_F(D3D9Tests, givenD3DHandleWhenCreatingSharedSurfaceThenAllocationTypeImageIsSet) { + mockSharingFcns->mockTexture2dDesc.Format = (D3DFORMAT)MAKEFOURCC('Y', 'V', '1', '2'); + surfaceInfo.shared_handle = reinterpret_cast(1); + + EXPECT_CALL(*mockSharingFcns, getTexture2dDesc(_, _)).Times(1).WillOnce(SetArgPointee<0>(mockSharingFcns->mockTexture2dDesc)); + + auto sharedImg = std::unique_ptr(D3DSurface::create(context, &surfaceInfo, CL_MEM_READ_WRITE, 0, 2, nullptr)); + ASSERT_NE(nullptr, sharedImg.get()); + ASSERT_NE(nullptr, sharedImg->getGraphicsAllocation()); + EXPECT_EQ(GraphicsAllocation::AllocationType::SHARED_IMAGE, sharedImg->getGraphicsAllocation()->getAllocationType()); +} + TEST_F(D3D9Tests, givenUPlaneWhenCreateSurfaceThenChangeWidthHeightAndPitch) { mockSharingFcns->mockTexture2dDesc.Format = (D3DFORMAT)MAKEFOURCC('Y', 'V', '1', '2'); surfaceInfo.shared_handle = (HANDLE)1; diff --git a/unit_tests/d3d_sharing/d3d_tests.cpp b/unit_tests/d3d_sharing/d3d_tests.cpp index 02e66207cc..2fed408b75 100644 --- a/unit_tests/d3d_sharing/d3d_tests.cpp +++ b/unit_tests/d3d_sharing/d3d_tests.cpp @@ -1270,6 +1270,34 @@ TYPED_TEST_P(D3DTests, inForced32BitAddressingBufferCreatedHas32BitAllocation) { EXPECT_TRUE(allocation->is32BitAllocation()); } +TYPED_TEST_P(D3DTests, givenD3DTexture2dWhenOclImageIsCreatedThenSharedImageAllocationTypeIsSet) { + this->mockSharingFcns->mockTexture2dDesc.Format = DXGI_FORMAT_P016; + EXPECT_CALL(*this->mockSharingFcns, getTexture2dDesc(_, _)) + .Times(1) + .WillOnce(SetArgPointee<0>(this->mockSharingFcns->mockTexture2dDesc)); + + auto image = std::unique_ptr(D3DTexture::create2d(this->context, reinterpret_cast(&this->dummyD3DTexture), CL_MEM_READ_WRITE, 7, nullptr)); + ASSERT_NE(nullptr, image.get()); + ASSERT_NE(nullptr, image->getGraphicsAllocation()); + EXPECT_EQ(GraphicsAllocation::AllocationType::SHARED_IMAGE, image->getGraphicsAllocation()->getAllocationType()); +} + +TYPED_TEST_P(D3DTests, givenD3DTexture3dWhenOclImageIsCreatedThenSharedImageAllocationTypeIsSet) { + this->mockSharingFcns->mockTexture3dDesc.MiscFlags = D3DResourceFlags::MISC_SHARED; + + EXPECT_CALL(*this->mockSharingFcns, getTexture3dDesc(_, _)) + .Times(1) + .WillOnce(SetArgPointee<0>(this->mockSharingFcns->mockTexture3dDesc)); + EXPECT_CALL(*this->mockSharingFcns, createTexture3d(_, _, _)) + .Times(1) + .WillOnce(SetArgPointee<0>(reinterpret_cast(&this->dummyD3DTextureStaging))); + + auto image = std::unique_ptr(D3DTexture::create3d(this->context, reinterpret_cast(&this->dummyD3DTexture), CL_MEM_READ_WRITE, 1, nullptr)); + ASSERT_NE(nullptr, image.get()); + ASSERT_NE(nullptr, image->getGraphicsAllocation()); + EXPECT_EQ(GraphicsAllocation::AllocationType::SHARED_IMAGE, image->getGraphicsAllocation()->getAllocationType()); +} + REGISTER_TYPED_TEST_CASE_P(D3DTests, getDeviceIDsFromD3DWithSpecificDeviceSet, getDeviceIDsFromD3DWithSpecificDeviceSource, @@ -1313,7 +1341,9 @@ REGISTER_TYPED_TEST_CASE_P(D3DTests, givenNV12FormatAndOddPlaneWhen2dCreatedThenSetPlaneParams, givenP010FormatAndOddPlaneWhen2dCreatedThenSetPlaneParams, givenP016FormatAndOddPlaneWhen2dCreatedThenSetPlaneParams, - inForced32BitAddressingBufferCreatedHas32BitAllocation); + inForced32BitAddressingBufferCreatedHas32BitAllocation, + givenD3DTexture2dWhenOclImageIsCreatedThenSharedImageAllocationTypeIsSet, + givenD3DTexture3dWhenOclImageIsCreatedThenSharedImageAllocationTypeIsSet); typedef ::testing::Types D3DTypes; INSTANTIATE_TYPED_TEST_CASE_P(D3DSharingTests, D3DTests, D3DTypes); diff --git a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp index 172866f64d..29e56ccae2 100644 --- a/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp @@ -10,6 +10,7 @@ #include "runtime/gmm_helper/gmm.h" #include "runtime/gmm_helper/gmm_helper.h" #include "runtime/helpers/aligned_memory.h" +#include "runtime/helpers/array_count.h" #include "runtime/mem_obj/buffer.h" #include "runtime/mem_obj/image.h" #include "runtime/mem_obj/mem_obj_helper.h" @@ -223,6 +224,32 @@ TEST_F(WddmMemoryManagerSimpleTest, givenMemoryManagerWhenCreateAllocationFromHa memoryManager->freeGraphicsMemory(allocation); } +TEST_F(WddmMemoryManagerSimpleTest, givenAllocationPropertiesWhenCreateAllocationFromHandleIsCalledThenCorrectAllocationTypeIsSet) { + memoryManager.reset(new MockWddmMemoryManager(false, false, *executionEnvironment)); + auto osHandle = 1u; + gdi->getQueryResourceInfoArgOut().NumAllocations = 1; + std::unique_ptr gmm(new Gmm(nullptr, 0, false)); + + D3DDDI_OPENALLOCATIONINFO allocationInfo; + allocationInfo.pPrivateDriverData = gmm->gmmResourceInfo->peekHandle(); + allocationInfo.hAllocation = ALLOCATION_HANDLE; + allocationInfo.PrivateDriverDataSize = sizeof(GMM_RESOURCE_INFO); + + gdi->getOpenResourceArgOut().pOpenAllocationInfo = &allocationInfo; + + AllocationProperties propertiesBuffer(false, 0, GraphicsAllocation::AllocationType::SHARED_BUFFER); + AllocationProperties propertiesImage(false, 0, GraphicsAllocation::AllocationType::SHARED_IMAGE); + + AllocationProperties *properties[2] = {&propertiesBuffer, &propertiesImage}; + + for (uint32_t i = 0; i < arrayCount(properties); i++) { + auto allocation = memoryManager->createGraphicsAllocationFromSharedHandle(osHandle, *properties[i], false); + EXPECT_NE(nullptr, allocation); + EXPECT_EQ(properties[i]->allocationType, allocation->getAllocationType()); + memoryManager->freeGraphicsMemory(allocation); + } +} + TEST_F(WddmMemoryManagerSimpleTest, whenCreateAllocationFromHandleAndMapCallFailsThenFreeGraphicsMemoryIsCalled) { memoryManager.reset(new MockWddmMemoryManager(false, false, *executionEnvironment)); auto osHandle = 1u; @@ -456,6 +483,7 @@ TEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWhenCreateFromNTHandleIsCall ASSERT_NE(nullptr, gpuAllocation); EXPECT_EQ(NT_RESOURCE_HANDLE, wddmAlloc->resourceHandle); EXPECT_EQ(NT_ALLOCATION_HANDLE, wddmAlloc->getDefaultHandle()); + EXPECT_EQ(GraphicsAllocation::AllocationType::SHARED_IMAGE, wddmAlloc->getAllocationType()); memoryManager->freeGraphicsMemory(gpuAllocation); } diff --git a/unit_tests/sharings/gl/gl_create_from_texture_tests.cpp b/unit_tests/sharings/gl/gl_create_from_texture_tests.cpp index c01aaed289..a7c39e0349 100644 --- a/unit_tests/sharings/gl/gl_create_from_texture_tests.cpp +++ b/unit_tests/sharings/gl/gl_create_from_texture_tests.cpp @@ -324,4 +324,22 @@ TEST_F(CreateFromGlTextureTests, GivenGlTextureTargetAndMipLevelNonNegativeWhenC EXPECT_GE(1u, glImage->getImageDesc().num_mip_levels); EXPECT_EQ(glImage->peekBaseMipLevel(), 2); } + +TEST_F(CreateFromGlTextureTests, GivenGlTextureWhenCreateIsCalledThenAllocationTypeIsSharedImage) { + unsigned int target = GL_TEXTURE_3D; + cl_GLint miplevel = 2; + + imgDesc.image_type = GlTexture::getClMemObjectType(target); + imgDesc.image_height = 13; + imgDesc.image_width = 15; + imgDesc.image_depth = 7; + + updateImgInfoAndForceGmm(); + + auto glImage = std::unique_ptr(GlTexture::createSharedGlTexture(&clContext, 0u, target, miplevel, 0, &retVal)); + EXPECT_EQ(CL_SUCCESS, retVal); + + ASSERT_NE(nullptr, glImage->getGraphicsAllocation()); + EXPECT_EQ(GraphicsAllocation::AllocationType::SHARED_IMAGE, glImage->getGraphicsAllocation()->getAllocationType()); +} } // namespace NEO diff --git a/unit_tests/sharings/gl/gl_sharing_tests.cpp b/unit_tests/sharings/gl/gl_sharing_tests.cpp index f5f3d87668..a827502c07 100644 --- a/unit_tests/sharings/gl/gl_sharing_tests.cpp +++ b/unit_tests/sharings/gl/gl_sharing_tests.cpp @@ -1175,3 +1175,9 @@ TEST_F(glSharingTests, whenGetGlContextHandleIsCalledThenProperHandleIsReturned) sharing->GLContextHandle = 0x2c; EXPECT_EQ(0x2cU, sharing->getGLContextHandle()); } + +TEST_F(glSharingTests, givenClGLBufferWhenCreatedThenSharedBufferAllocatoinTypeIsSet) { + std::unique_ptr buffer(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId, nullptr)); + ASSERT_NE(nullptr, buffer->getGraphicsAllocation()); + EXPECT_EQ(GraphicsAllocation::AllocationType::SHARED_BUFFER, buffer->getGraphicsAllocation()->getAllocationType()); +}