1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2017 The Khronos Group Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Synchronization tests for resources shared with DX11 keyed mutex
22 *//*--------------------------------------------------------------------*/
24 #include "vktSynchronizationWin32KeyedMutexTests.hpp"
26 #include "vkDeviceUtil.hpp"
27 #include "vkPlatform.hpp"
28 #include "vkCmdUtil.hpp"
29 #include "vktTestCaseUtil.hpp"
30 #include "deSharedPtr.hpp"
32 #include "vktSynchronizationUtil.hpp"
33 #include "vktSynchronizationOperation.hpp"
34 #include "vktSynchronizationOperationTestData.hpp"
35 #include "vktExternalMemoryUtil.hpp"
36 #include "vktTestGroupUtil.hpp"
37 #include "vktCustomInstancesDevices.hpp"
39 #include "tcuResultCollector.hpp"
40 #include "tcuTestLog.hpp"
41 #include "tcuCommandLine.hpp"
43 #if (DE_OS == DE_OS_WIN32)
44 # define WIN32_LEAN_AND_MEAN
48 # include "VersionHelpers.h"
50 # include "d3dcompiler.h"
52 typedef HRESULT (WINAPI * LPD3DX11COMPILEFROMMEMORY)(LPCSTR,
55 CONST D3D10_SHADER_MACRO*,
61 void*, /* ID3DX11ThreadPump */
68 using namespace vkt::ExternalMemoryUtil;
73 namespace synchronization
80 static const ResourceDescription s_resourcesWin32KeyedMutex[] =
82 { RESOURCE_TYPE_BUFFER, tcu::IVec4( 0x4000, 0, 0, 0), vk::VK_IMAGE_TYPE_LAST, vk::VK_FORMAT_UNDEFINED, (vk::VkImageAspectFlags)0 }, // 16 KiB (min max UBO range)
83 { RESOURCE_TYPE_BUFFER, tcu::IVec4(0x40000, 0, 0, 0), vk::VK_IMAGE_TYPE_LAST, vk::VK_FORMAT_UNDEFINED, (vk::VkImageAspectFlags)0 }, // 256 KiB
85 { RESOURCE_TYPE_IMAGE, tcu::IVec4(128, 128, 0, 0), vk::VK_IMAGE_TYPE_2D, vk::VK_FORMAT_R8_UNORM, vk::VK_IMAGE_ASPECT_COLOR_BIT },
86 { RESOURCE_TYPE_IMAGE, tcu::IVec4(128, 128, 0, 0), vk::VK_IMAGE_TYPE_2D, vk::VK_FORMAT_R16_UINT, vk::VK_IMAGE_ASPECT_COLOR_BIT },
87 { RESOURCE_TYPE_IMAGE, tcu::IVec4(128, 128, 0, 0), vk::VK_IMAGE_TYPE_2D, vk::VK_FORMAT_R8G8B8A8_UNORM, vk::VK_IMAGE_ASPECT_COLOR_BIT },
88 { RESOURCE_TYPE_IMAGE, tcu::IVec4(128, 128, 0, 0), vk::VK_IMAGE_TYPE_2D, vk::VK_FORMAT_R16G16B16A16_UINT, vk::VK_IMAGE_ASPECT_COLOR_BIT },
89 { RESOURCE_TYPE_IMAGE, tcu::IVec4(128, 128, 0, 0), vk::VK_IMAGE_TYPE_2D, vk::VK_FORMAT_R32G32B32A32_SFLOAT, vk::VK_IMAGE_ASPECT_COLOR_BIT },
94 TestConfig (const ResourceDescription& resource_,
95 OperationName writeOp_,
96 OperationName readOp_,
97 vk::VkExternalMemoryHandleTypeFlagBits memoryHandleTypeBuffer_,
98 vk::VkExternalMemoryHandleTypeFlagBits memoryHandleTypeImage_)
99 : resource (resource_)
102 , memoryHandleTypeBuffer (memoryHandleTypeBuffer_)
103 , memoryHandleTypeImage (memoryHandleTypeImage_)
107 const ResourceDescription resource;
108 const OperationName writeOp;
109 const OperationName readOp;
110 const vk::VkExternalMemoryHandleTypeFlagBits memoryHandleTypeBuffer;
111 const vk::VkExternalMemoryHandleTypeFlagBits memoryHandleTypeImage;
114 bool checkQueueFlags (vk::VkQueueFlags availableFlags, const vk::VkQueueFlags neededFlags)
116 if ((availableFlags & (vk::VK_QUEUE_GRAPHICS_BIT | vk::VK_QUEUE_COMPUTE_BIT)) != 0)
117 availableFlags |= vk::VK_QUEUE_TRANSFER_BIT;
119 return (availableFlags & neededFlags) != 0;
122 class SimpleAllocation : public vk::Allocation
125 SimpleAllocation (const vk::DeviceInterface& vkd,
127 const vk::VkDeviceMemory memory);
128 ~SimpleAllocation (void);
131 const vk::DeviceInterface& m_vkd;
132 const vk::VkDevice m_device;
135 SimpleAllocation::SimpleAllocation (const vk::DeviceInterface& vkd,
137 const vk::VkDeviceMemory memory)
138 : Allocation (memory, 0, DE_NULL)
144 SimpleAllocation::~SimpleAllocation (void)
146 m_vkd.freeMemory(m_device, getMemory(), DE_NULL);
149 CustomInstance createTestInstance (Context& context)
151 std::vector<std::string> extensions;
152 extensions.push_back("VK_KHR_get_physical_device_properties2");
153 extensions.push_back("VK_KHR_external_memory_capabilities");
155 return createCustomInstanceWithExtensions(context, extensions);
158 vk::Move<vk::VkDevice> createTestDevice (Context& context,
159 vk::VkInstance instance,
160 const vk::InstanceInterface& vki,
161 vk::VkPhysicalDevice physicalDevice)
163 const bool validationEnabled = context.getTestContext().getCommandLine().isValidationEnabled();
164 const deUint32 apiVersion = context.getUsedApiVersion();
165 const vk::PlatformInterface& vkp = context.getPlatformInterface();
166 const float priority = 0.0f;
167 const std::vector<vk::VkQueueFamilyProperties> queueFamilyProperties = vk::getPhysicalDeviceQueueFamilyProperties(vki, physicalDevice);
168 std::vector<deUint32> queueFamilyIndices (queueFamilyProperties.size(), 0xFFFFFFFFu);
169 std::vector<const char*> extensions;
171 if (!isCoreDeviceExtension(apiVersion, "VK_KHR_external_memory"))
172 extensions.push_back("VK_KHR_external_memory");
173 if (!isCoreDeviceExtension(apiVersion, "VK_KHR_dedicated_allocation"))
174 extensions.push_back("VK_KHR_dedicated_allocation");
175 if (!isCoreDeviceExtension(apiVersion, "VK_KHR_get_memory_requirements2"))
176 extensions.push_back("VK_KHR_get_memory_requirements2");
178 extensions.push_back("VK_KHR_external_memory_win32");
179 extensions.push_back("VK_KHR_win32_keyed_mutex");
183 std::vector<vk::VkDeviceQueueCreateInfo> queues;
185 for (size_t ndx = 0; ndx < queueFamilyProperties.size(); ndx++)
187 const vk::VkDeviceQueueCreateInfo createInfo =
189 vk::VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
198 queues.push_back(createInfo);
201 const vk::VkDeviceCreateInfo createInfo =
203 vk::VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
207 (deUint32)queues.size(),
213 (deUint32)extensions.size(),
214 extensions.empty() ? DE_NULL : &extensions[0],
218 return createCustomDevice(validationEnabled, vkp, instance, vki, physicalDevice, &createInfo);
220 catch (const vk::Error& error)
222 if (error.getError() == vk::VK_ERROR_EXTENSION_NOT_PRESENT)
223 TCU_THROW(NotSupportedError, "Required extensions not supported");
229 deUint32 chooseMemoryType (deUint32 bits)
231 DE_ASSERT(bits != 0);
233 for (deUint32 memoryTypeIndex = 0; (1u << memoryTypeIndex) <= bits; memoryTypeIndex++)
235 if ((bits & (1u << memoryTypeIndex)) != 0)
236 return memoryTypeIndex;
239 DE_FATAL("No supported memory types");
243 bool isOpaqueHandleType (const vk::VkExternalMemoryHandleTypeFlagBits handleType)
247 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
248 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT:
249 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT:
251 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT:
252 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT:
253 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_HEAP_BIT:
254 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE_BIT:
255 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
256 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT:
257 case vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
260 TCU_THROW(InternalError, "Unknown handle type or multiple bits set");
264 vk::Move<vk::VkDeviceMemory> importMemory (const vk::DeviceInterface& vkd,
266 const vk::VkMemoryRequirements& requirements,
267 vk::VkExternalMemoryHandleTypeFlagBits externalType,
268 NativeHandle& handle,
269 bool requiresDedicated,
273 const vk::VkMemoryDedicatedAllocateInfo dedicatedInfo =
275 vk::VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
280 const vk::VkImportMemoryWin32HandleInfoKHR importInfo =
282 vk::VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR,
283 (requiresDedicated) ? &dedicatedInfo : DE_NULL,
285 handle.getWin32Handle(),
286 (vk::pt::Win32LPCWSTR)NULL
289 deUint32 handleCompatibleMemoryTypeBits = ~0u;
290 if(!isOpaqueHandleType(externalType))
292 vk::VkMemoryWin32HandlePropertiesKHR memoryWin32HandleProperties =
294 vk::VK_STRUCTURE_TYPE_MEMORY_WIN32_HANDLE_PROPERTIES_KHR,
298 VK_CHECK(vkd.getMemoryWin32HandlePropertiesKHR(device, externalType, handle.getWin32Handle(), &memoryWin32HandleProperties));
299 handleCompatibleMemoryTypeBits &= memoryWin32HandleProperties.memoryTypeBits;
302 const vk::VkMemoryAllocateInfo info =
304 vk::VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
307 chooseMemoryType(requirements.memoryTypeBits & handleCompatibleMemoryTypeBits)
310 vk::Move<vk::VkDeviceMemory> memory (vk::allocateMemory(vkd, device, &info));
317 de::MovePtr<vk::Allocation> importAndBindMemory (const vk::DeviceInterface& vkd,
320 NativeHandle& nativeHandle,
321 vk::VkExternalMemoryHandleTypeFlagBits externalType)
323 const vk::VkBufferMemoryRequirementsInfo2 requirementsInfo =
325 vk::VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
329 vk::VkMemoryDedicatedRequirements dedicatedRequirements =
331 vk::VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
336 vk::VkMemoryRequirements2 requirements =
338 vk::VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
339 &dedicatedRequirements,
342 vkd.getBufferMemoryRequirements2(device, &requirementsInfo, &requirements);
344 vk::Move<vk::VkDeviceMemory> memory = importMemory(vkd, device, requirements.memoryRequirements, externalType, nativeHandle, !!dedicatedRequirements.requiresDedicatedAllocation, buffer, DE_NULL);
345 VK_CHECK(vkd.bindBufferMemory(device, buffer, *memory, 0u));
347 return de::MovePtr<vk::Allocation>(new SimpleAllocation(vkd, device, memory.disown()));
350 de::MovePtr<vk::Allocation> importAndBindMemory (const vk::DeviceInterface& vkd,
353 NativeHandle& nativeHandle,
354 vk::VkExternalMemoryHandleTypeFlagBits externalType)
356 const vk::VkImageMemoryRequirementsInfo2 requirementsInfo =
358 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
362 vk::VkMemoryDedicatedRequirements dedicatedRequirements =
364 vk::VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
369 vk::VkMemoryRequirements2 requirements =
371 vk::VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
372 &dedicatedRequirements,
375 vkd.getImageMemoryRequirements2(device, &requirementsInfo, &requirements);
377 vk::Move<vk::VkDeviceMemory> memory = importMemory(vkd, device, requirements.memoryRequirements, externalType, nativeHandle, !!dedicatedRequirements.requiresDedicatedAllocation, DE_NULL, image);
378 VK_CHECK(vkd.bindImageMemory(device, image, *memory, 0u));
380 return de::MovePtr<vk::Allocation>(new SimpleAllocation(vkd, device, memory.disown()));
383 de::MovePtr<Resource> importResource (const vk::DeviceInterface& vkd,
385 const ResourceDescription& resourceDesc,
386 const std::vector<deUint32>& queueFamilyIndices,
387 const OperationSupport& readOp,
388 const OperationSupport& writeOp,
389 NativeHandle& nativeHandle,
390 vk::VkExternalMemoryHandleTypeFlagBits externalType)
392 if (resourceDesc.type == RESOURCE_TYPE_IMAGE)
394 const vk::VkExtent3D extent =
396 (deUint32)resourceDesc.size.x(),
397 de::max(1u, (deUint32)resourceDesc.size.y()),
398 de::max(1u, (deUint32)resourceDesc.size.z())
400 const vk::VkImageSubresourceRange subresourceRange =
402 resourceDesc.imageAspect,
408 const vk::VkImageSubresourceLayers subresourceLayers =
410 resourceDesc.imageAspect,
415 const vk::VkExternalMemoryImageCreateInfo externalInfo =
417 vk::VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
419 (vk::VkExternalMemoryHandleTypeFlags)externalType
421 const vk::VkImageCreateInfo createInfo =
423 vk::VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
427 resourceDesc.imageType,
428 resourceDesc.imageFormat,
432 vk::VK_SAMPLE_COUNT_1_BIT,
433 vk::VK_IMAGE_TILING_OPTIMAL,
434 readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags(),
435 vk::VK_SHARING_MODE_EXCLUSIVE,
437 (deUint32)queueFamilyIndices.size(),
438 &queueFamilyIndices[0],
439 vk::VK_IMAGE_LAYOUT_UNDEFINED
442 vk::Move<vk::VkImage> image = vk::createImage(vkd, device, &createInfo);
443 de::MovePtr<vk::Allocation> allocation = importAndBindMemory(vkd, device, *image, nativeHandle, externalType);
445 return de::MovePtr<Resource>(new Resource(image, allocation, extent, resourceDesc.imageType, resourceDesc.imageFormat, subresourceRange, subresourceLayers));
449 const vk::VkDeviceSize offset = 0u;
450 const vk::VkDeviceSize size = static_cast<vk::VkDeviceSize>(resourceDesc.size.x());
451 const vk::VkBufferUsageFlags usage = readOp.getInResourceUsageFlags() | writeOp.getOutResourceUsageFlags();
452 const vk::VkExternalMemoryBufferCreateInfo externalInfo =
454 vk::VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
456 (vk::VkExternalMemoryHandleTypeFlags)externalType
458 const vk::VkBufferCreateInfo createInfo =
460 vk::VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
466 vk::VK_SHARING_MODE_EXCLUSIVE,
467 (deUint32)queueFamilyIndices.size(),
468 &queueFamilyIndices[0]
470 vk::Move<vk::VkBuffer> buffer = vk::createBuffer(vkd, device, &createInfo);
471 de::MovePtr<vk::Allocation> allocation = importAndBindMemory(vkd, device, *buffer, nativeHandle, externalType);
473 return de::MovePtr<Resource>(new Resource(resourceDesc.type, buffer, allocation, offset, size));
477 void recordWriteBarrier (const vk::DeviceInterface& vkd,
478 vk::VkCommandBuffer commandBuffer,
479 const Resource& resource,
480 const SyncInfo& writeSync,
481 deUint32 writeQueueFamilyIndex,
482 const SyncInfo& readSync)
484 const vk::VkPipelineStageFlags srcStageMask = writeSync.stageMask;
485 const vk::VkAccessFlags srcAccessMask = writeSync.accessMask;
487 const vk::VkPipelineStageFlags dstStageMask = readSync.stageMask;
488 const vk::VkAccessFlags dstAccessMask = readSync.accessMask;
490 const vk::VkDependencyFlags dependencyFlags = 0;
492 if (resource.getType() == RESOURCE_TYPE_IMAGE)
494 const vk::VkImageMemoryBarrier barrier =
496 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
502 writeSync.imageLayout,
503 readSync.imageLayout,
505 writeQueueFamilyIndex,
506 VK_QUEUE_FAMILY_EXTERNAL,
508 resource.getImage().handle,
509 resource.getImage().subresourceRange
512 vkd.cmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, 0u, (const vk::VkMemoryBarrier*)DE_NULL, 0u, (const vk::VkBufferMemoryBarrier*)DE_NULL, 1u, (const vk::VkImageMemoryBarrier*)&barrier);
516 const vk::VkBufferMemoryBarrier barrier =
518 vk::VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
524 writeQueueFamilyIndex,
525 VK_QUEUE_FAMILY_EXTERNAL,
527 resource.getBuffer().handle,
532 vkd.cmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, 0u, (const vk::VkMemoryBarrier*)DE_NULL, 1u, (const vk::VkBufferMemoryBarrier*)&barrier, 0u, (const vk::VkImageMemoryBarrier*)DE_NULL);
536 void recordReadBarrier (const vk::DeviceInterface& vkd,
537 vk::VkCommandBuffer commandBuffer,
538 const Resource& resource,
539 const SyncInfo& writeSync,
540 const SyncInfo& readSync,
541 deUint32 readQueueFamilyIndex)
543 const vk::VkPipelineStageFlags srcStageMask = readSync.stageMask;
544 const vk::VkAccessFlags srcAccessMask = readSync.accessMask;
546 const vk::VkPipelineStageFlags dstStageMask = readSync.stageMask;
547 const vk::VkAccessFlags dstAccessMask = readSync.accessMask;
549 const vk::VkDependencyFlags dependencyFlags = 0;
551 if (resource.getType() == RESOURCE_TYPE_IMAGE)
553 const vk::VkImageMemoryBarrier barrier =
555 vk::VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
561 writeSync.imageLayout,
562 readSync.imageLayout,
564 VK_QUEUE_FAMILY_EXTERNAL,
565 readQueueFamilyIndex,
567 resource.getImage().handle,
568 resource.getImage().subresourceRange
571 vkd.cmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, 0u, (const vk::VkMemoryBarrier*)DE_NULL, 0u, (const vk::VkBufferMemoryBarrier*)DE_NULL, 1u, (const vk::VkImageMemoryBarrier*)&barrier);
575 const vk::VkBufferMemoryBarrier barrier =
577 vk::VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
583 VK_QUEUE_FAMILY_EXTERNAL,
584 readQueueFamilyIndex,
586 resource.getBuffer().handle,
591 vkd.cmdPipelineBarrier(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, 0u, (const vk::VkMemoryBarrier*)DE_NULL, 1u, (const vk::VkBufferMemoryBarrier*)&barrier, 0u, (const vk::VkImageMemoryBarrier*)DE_NULL);
595 std::vector<deUint32> getFamilyIndices (const std::vector<vk::VkQueueFamilyProperties>& properties)
597 std::vector<deUint32> indices (properties.size(), 0);
599 for (deUint32 ndx = 0; ndx < properties.size(); ndx++)
617 KEYED_MUTEX_INIT = 0,
618 KEYED_MUTEX_VK_WRITE = 1,
619 KEYED_MUTEX_DX_COPY = 2,
620 KEYED_MUTEX_VK_VERIFY = 3,
621 KEYED_MUTEX_DONE = 4,
624 #if (DE_OS == DE_OS_WIN32)
625 DX11Operation (const ResourceDescription& resourceDesc,
626 vk::VkExternalMemoryHandleTypeFlagBits memoryHandleType,
627 ID3D11Device* pDevice,
628 ID3D11DeviceContext* pContext,
629 LPD3DX11COMPILEFROMMEMORY fnD3DX11CompileFromMemory,
630 pD3DCompile fnD3DCompile)
631 : m_resourceDesc (resourceDesc)
633 , m_pDevice (pDevice)
634 , m_pContext (pContext)
635 , m_fnD3DX11CompileFromMemory (fnD3DX11CompileFromMemory)
636 , m_fnD3DCompile (fnD3DCompile)
638 , m_pRenderTargetView (0)
639 , m_pVertexShader (0)
641 , m_pVertexBuffer (0)
643 , m_pSamplerLinear (0)
648 if (memoryHandleType == vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT ||
649 memoryHandleType == vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT)
651 m_isMemNtHandle = true;
653 m_isMemNtHandle = false;
655 m_securityAttributes.lpSecurityDescriptor = 0;
657 for (UINT i = 0; i < BUFFER_COUNT; i++)
659 m_pTexture[i] = NULL;
661 m_keyedMutex[i] = NULL;
664 if (m_resourceDesc.type == RESOURCE_TYPE_BUFFER)
666 // SHARED_NTHANDLE is not supported with CreateBuffer().
667 TCU_CHECK_INTERNAL(!m_isMemNtHandle);
669 D3D11_BUFFER_DESC descBuf = { };
670 descBuf.ByteWidth = (UINT)m_resourceDesc.size.x();
671 descBuf.Usage = D3D11_USAGE_DEFAULT;
672 descBuf.BindFlags = 0;
673 descBuf.CPUAccessFlags = 0;
674 descBuf.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
675 descBuf.StructureByteStride = 0;
677 for (UINT i = 0; i < BUFFER_COUNT; ++i)
679 hr = m_pDevice->CreateBuffer(&descBuf, NULL, &m_pBuffer[i]);
681 TCU_FAIL("Failed to create a buffer");
683 m_sharedMemHandle[i] = 0;
685 IDXGIResource* tempResource = NULL;
686 hr = m_pBuffer[i]->QueryInterface(__uuidof(IDXGIResource), (void**)&tempResource);
688 TCU_FAIL("Query interface of IDXGIResource failed");
689 hr = tempResource->GetSharedHandle(&m_sharedMemHandle[i]);
690 tempResource->Release();
692 TCU_FAIL("Failed to get DX shared handle");
694 hr = m_pBuffer[i]->QueryInterface(__uuidof(IDXGIKeyedMutex), (void**)&m_keyedMutex[i]);
696 TCU_FAIL("Query interface of IDXGIKeyedMutex failed");
698 // Take ownership of the lock.
699 m_keyedMutex[i]->AcquireSync(KEYED_MUTEX_INIT, INFINITE);
702 // Release the buffer write lock for Vulkan to write into.
703 m_keyedMutex[BUFFER_VK_WRITE]->ReleaseSync(KEYED_MUTEX_VK_WRITE);
705 m_sharedMemSize = descBuf.ByteWidth;
706 m_sharedMemOffset = 0;
710 DE_ASSERT(m_resourceDesc.type == RESOURCE_TYPE_IMAGE);
712 for (UINT i = 0; i < BUFFER_COUNT; ++i)
714 D3D11_TEXTURE2D_DESC descColor = { };
715 descColor.Width = m_resourceDesc.size.x();
716 descColor.Height = m_resourceDesc.size.y();
717 descColor.MipLevels = 1;
718 descColor.ArraySize = 1;
719 descColor.Format = getDxgiFormat(m_resourceDesc.imageFormat);
720 descColor.SampleDesc.Count = 1;
721 descColor.SampleDesc.Quality = 0;
722 descColor.Usage = D3D11_USAGE_DEFAULT;
723 descColor.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
724 descColor.CPUAccessFlags = 0;
727 descColor.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE;
729 descColor.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
731 hr = m_pDevice->CreateTexture2D(&descColor, NULL, &m_pTexture[i]);
733 TCU_FAIL("Unable to create DX11 texture");
735 m_sharedMemHandle[i] = 0;
739 IDXGIResource1* tempResource1 = NULL;
740 hr = m_pTexture[i]->QueryInterface(__uuidof(IDXGIResource1), (void**)&tempResource1);
742 TCU_FAIL("Unable to query IDXGIResource1 interface");
744 hr = tempResource1->CreateSharedHandle(getSecurityAttributes(), DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, /*lpName*/NULL, &m_sharedMemHandle[i]);
745 tempResource1->Release();
747 TCU_FAIL("Enable to get DX shared handle");
751 IDXGIResource* tempResource = NULL;
752 hr = m_pTexture[i]->QueryInterface(__uuidof(IDXGIResource), (void**)&tempResource);
754 TCU_FAIL("Query interface of IDXGIResource failed");
755 hr = tempResource->GetSharedHandle(&m_sharedMemHandle[i]);
756 tempResource->Release();
758 TCU_FAIL("Failed to get DX shared handle");
761 hr = m_pTexture[i]->QueryInterface(__uuidof(IDXGIKeyedMutex), (void**)&m_keyedMutex[i]);
763 TCU_FAIL("Unable to query DX11 keyed mutex interface");
765 // Take ownership of the lock.
766 m_keyedMutex[i]->AcquireSync(KEYED_MUTEX_INIT, INFINITE);
770 m_sharedMemOffset = 0;
772 hr = m_pDevice->CreateRenderTargetView(m_pTexture[BUFFER_VK_READ], NULL, &m_pRenderTargetView);
774 TCU_FAIL("Unable to create DX11 render target view");
776 m_pContext->OMSetRenderTargets(1, &m_pRenderTargetView, NULL);
778 // Setup the viewport
780 vp.Width = (FLOAT)m_resourceDesc.size.x();
781 vp.Height = (FLOAT)m_resourceDesc.size.y();
786 m_pContext->RSSetViewports(1, &vp);
788 // Compile the vertex shader
790 "Texture2D txDiffuse : register(t0);\n"
791 "SamplerState samLinear : register(s0);\n"
794 " float4 Pos : POSITION;\n"
795 " float2 Tex : TEXCOORD0;\n"
799 " float4 Pos : SV_POSITION;\n"
800 " float2 Tex : TEXCOORD0;\n"
802 "PS_INPUT VS(VS_INPUT input)\n"
804 " PS_INPUT output = (PS_INPUT)0;\n"
805 " output.Pos = input.Pos;\n"
806 " output.Tex = input.Tex;\n"
810 "float4 PS(PS_INPUT input) : SV_Target\n"
812 " return txDiffuse.Sample(samLinear, input.Tex);\n"
815 // Define the input layout
816 D3D11_INPUT_ELEMENT_DESC layout[] =
818 { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
819 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
822 createShaders(shader, "VS", "vs_4_0", ARRAYSIZE(layout), layout, &m_pVertexShader, "PS", "ps_4_0", &m_pPixelShader);
830 SimpleVertex vertices[] =
832 { { -1.f, -1.f, 0.0f }, { 0.0f, 1.0f } },
833 { { -1.f, 1.f, 0.0f }, { 0.0f, 0.0f } },
834 { { 1.f, -1.f, 0.0f }, { 1.0f, 1.0f } },
835 { { 1.f, 1.f, 0.0f }, { 1.0f, 0.0f } },
838 D3D11_BUFFER_DESC bd = { };
839 bd.Usage = D3D11_USAGE_DEFAULT;
840 bd.ByteWidth = sizeof (vertices);
841 bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
842 bd.CPUAccessFlags = 0;
843 D3D11_SUBRESOURCE_DATA InitData = { };
844 InitData.pSysMem = vertices;
845 hr = m_pDevice->CreateBuffer(&bd, &InitData, &m_pVertexBuffer);
847 TCU_FAIL("Failed to create DX11 vertex buffer");
850 UINT stride = sizeof (SimpleVertex);
852 m_pContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
854 // Set primitive topology
855 m_pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
859 D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = { };
860 SRVDesc.Format = getDxgiFormat(m_resourceDesc.imageFormat);
861 SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
862 SRVDesc.Texture2D.MipLevels = 1;
864 hr = m_pDevice->CreateShaderResourceView(m_pTexture[BUFFER_VK_WRITE], &SRVDesc, &m_pTextureRV);
866 TCU_FAIL("Failed to create DX11 resource view");
868 // Create the sample state
869 D3D11_SAMPLER_DESC sampDesc = { };
870 sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
871 sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
872 sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
873 sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
874 sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
876 sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
877 hr = m_pDevice->CreateSamplerState(&sampDesc, &m_pSamplerLinear);
879 TCU_FAIL("Failed to create DX11 sampler state");
881 // Release the lock for VK to write into the texture.
882 m_keyedMutex[BUFFER_VK_WRITE]->ReleaseSync(KEYED_MUTEX_VK_WRITE);
890 #endif // #if (DE_OS == DE_OS_WIN32)
892 NativeHandle getNativeHandle (Buffer buffer)
894 #if (DE_OS == DE_OS_WIN32)
895 return NativeHandle((m_isMemNtHandle) ? NativeHandle::WIN32HANDLETYPE_NT : NativeHandle::WIN32HANDLETYPE_KMT, vk::pt::Win32Handle(m_sharedMemHandle[buffer]));
898 return NativeHandle();
904 #if (DE_OS == DE_OS_WIN32)
905 m_keyedMutex[BUFFER_VK_WRITE]->AcquireSync(KEYED_MUTEX_DX_COPY, INFINITE);
907 if (m_resourceDesc.type == RESOURCE_TYPE_BUFFER) {
908 m_pContext->CopySubresourceRegion(m_pBuffer[BUFFER_VK_READ], 0, 0, 0, 0, m_pBuffer[BUFFER_VK_WRITE], 0, NULL);
910 m_pContext->OMSetRenderTargets(1, &m_pRenderTargetView, NULL);
912 const FLOAT gray[] = { 0.f, 0.f, 1.f, 1.f };
913 m_pContext->ClearRenderTargetView(m_pRenderTargetView, gray);
915 m_pContext->VSSetShader(m_pVertexShader, NULL, 0);
916 m_pContext->PSSetShader(m_pPixelShader, NULL, 0);
917 m_pContext->PSSetShaderResources(0, 1, &m_pTextureRV);
918 m_pContext->PSSetSamplers(0, 1, &m_pSamplerLinear);
919 m_pContext->Draw(4, 0);
922 m_keyedMutex[BUFFER_VK_WRITE]->ReleaseSync(KEYED_MUTEX_DONE);
923 m_keyedMutex[BUFFER_VK_READ]->ReleaseSync(KEYED_MUTEX_VK_VERIFY);
924 #endif // #if (DE_OS == DE_OS_WIN32)
927 #if (DE_OS == DE_OS_WIN32)
928 void d3dx11CompileShader (const char* shaderCode, const char * entryPoint, const char* shaderModel, ID3D10Blob** ppBlobOut)
932 ID3D10Blob* pErrorBlob;
933 hr = m_fnD3DX11CompileFromMemory (shaderCode,
947 pErrorBlob->Release();
950 TCU_FAIL("D3DX11CompileFromMemory failed to compile shader");
953 void d3dCompileShader (const char* shaderCode, const char * entryPoint, const char* shaderModel, ID3DBlob** ppBlobOut)
957 ID3DBlob* pErrorBlob;
958 hr = m_fnD3DCompile (shaderCode,
970 pErrorBlob->Release();
973 TCU_FAIL("D3DCompile failed to compile shader");
976 void createShaders (const char* shaderSrc,
977 const char* vsEntryPoint,
978 const char* vsShaderModel,
980 D3D11_INPUT_ELEMENT_DESC* pLayoutDesc,
981 ID3D11VertexShader** pVertexShader,
982 const char* psEntryPoint,
983 const char* psShaderModel,
984 ID3D11PixelShader** pPixelShader)
988 if (m_fnD3DX11CompileFromMemory) {
991 d3dx11CompileShader(shaderSrc, vsEntryPoint, vsShaderModel, &pVSBlob);
993 hr = m_pDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, pVertexShader);
995 TCU_FAIL("Failed to create DX11 vertex shader");
997 ID3D11InputLayout *pVertexLayout;
998 hr = m_pDevice->CreateInputLayout(pLayoutDesc, numLayoutDesc, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &pVertexLayout);
1000 TCU_FAIL("Failed to create vertex input layout");
1002 m_pContext->IASetInputLayout(pVertexLayout);
1003 pVertexLayout->Release();
1007 ID3D10Blob* pPSBlob;
1008 d3dx11CompileShader(shaderSrc, psEntryPoint, psShaderModel, &pPSBlob);
1010 hr = m_pDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, pPixelShader);
1012 TCU_FAIL("Failed to create DX11 pixel shader");
1016 d3dCompileShader(shaderSrc, vsEntryPoint, vsShaderModel, &pVSBlob);
1018 hr = m_pDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, pVertexShader);
1020 TCU_FAIL("Failed to create DX11 vertex shader");
1022 ID3D11InputLayout *pVertexLayout;
1023 hr = m_pDevice->CreateInputLayout(pLayoutDesc, numLayoutDesc, pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &pVertexLayout);
1025 TCU_FAIL("Failed to create vertex input layout");
1027 m_pContext->IASetInputLayout(pVertexLayout);
1028 pVertexLayout->Release();
1033 d3dCompileShader(shaderSrc, psEntryPoint, psShaderModel, &pPSBlob);
1035 hr = m_pDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, pPixelShader);
1037 TCU_FAIL("Failed to create DX11 pixel shader");
1040 #endif // #if (DE_OS == DE_OS_WIN32)
1043 #if (DE_OS == DE_OS_WIN32)
1046 if (m_securityAttributes.lpSecurityDescriptor)
1048 freeSecurityDescriptor(m_securityAttributes.lpSecurityDescriptor);
1049 m_securityAttributes.lpSecurityDescriptor = NULL;
1053 m_pContext->ClearState();
1055 if (m_pRenderTargetView)
1057 m_pRenderTargetView->Release();
1058 m_pRenderTargetView = NULL;
1061 if (m_pSamplerLinear)
1063 m_pSamplerLinear->Release();
1064 m_pSamplerLinear = NULL;
1069 m_pTextureRV->Release();
1070 m_pTextureRV = NULL;
1073 if (m_pVertexBuffer)
1075 m_pVertexBuffer->Release();
1076 m_pVertexBuffer = NULL;
1079 if (m_pVertexShader)
1081 m_pVertexShader->Release();
1082 m_pVertexShader = NULL;
1087 m_pPixelShader->Release();
1088 m_pPixelShader = NULL;
1091 for (int i = 0; i < BUFFER_COUNT; i++)
1093 if (m_keyedMutex[i])
1095 m_keyedMutex[i]->AcquireSync(KEYED_MUTEX_DONE, INFINITE);
1096 m_keyedMutex[i]->Release();
1097 m_keyedMutex[i] = NULL;
1100 if (m_isMemNtHandle && m_sharedMemHandle[i]) {
1101 CloseHandle(m_sharedMemHandle[i]);
1102 m_sharedMemHandle[i] = 0;
1106 m_pBuffer[i]->Release();
1107 m_pBuffer[i] = NULL;
1110 if (m_pTexture[i]) {
1111 m_pTexture[i]->Release();
1112 m_pTexture[i] = NULL;
1117 static void* getSecurityDescriptor ()
1119 PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR)deCalloc(SECURITY_DESCRIPTOR_MIN_LENGTH + 2 * sizeof (void**));
1123 PSID* ppEveryoneSID = (PSID*)((PBYTE)pSD + SECURITY_DESCRIPTOR_MIN_LENGTH);
1124 PACL* ppACL = (PACL*)((PBYTE)ppEveryoneSID + sizeof(PSID*));
1126 InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);
1128 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
1129 AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, ppEveryoneSID);
1131 EXPLICIT_ACCESS ea = { };
1132 ea.grfAccessPermissions = STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
1133 ea.grfAccessMode = SET_ACCESS;
1134 ea.grfInheritance = INHERIT_ONLY;
1135 ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
1136 ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
1137 ea.Trustee.ptstrName = (LPTSTR)*ppEveryoneSID;
1139 SetEntriesInAcl(1, &ea, NULL, ppACL);
1141 SetSecurityDescriptorDacl(pSD, TRUE, *ppACL, FALSE);
1147 static void freeSecurityDescriptor (void* pSD)
1151 PSID* ppEveryoneSID = (PSID*)((PBYTE)pSD + SECURITY_DESCRIPTOR_MIN_LENGTH);
1152 PACL* ppACL = (PACL*)((PBYTE)ppEveryoneSID + sizeof(PSID*));
1155 FreeSid(*ppEveryoneSID);
1164 static DXGI_FORMAT getDxgiFormat (vk::VkFormat format)
1168 case vk::VK_FORMAT_R8_UNORM:
1169 return DXGI_FORMAT_R8_UNORM;
1170 case vk::VK_FORMAT_R16_UINT:
1171 return DXGI_FORMAT_R16_UINT;
1172 case vk::VK_FORMAT_R8G8B8A8_UNORM:
1173 return DXGI_FORMAT_R8G8B8A8_UNORM;
1174 case vk::VK_FORMAT_R16G16B16A16_UINT:
1175 return DXGI_FORMAT_R16G16B16A16_UINT;
1176 case vk::VK_FORMAT_R32G32B32A32_SFLOAT:
1177 return DXGI_FORMAT_R32G32B32A32_FLOAT;
1178 case vk::VK_FORMAT_D16_UNORM:
1179 return DXGI_FORMAT_D16_UNORM;
1180 case vk::VK_FORMAT_D32_SFLOAT:
1181 return DXGI_FORMAT_D32_FLOAT;
1183 TCU_CHECK_INTERNAL(!"Unsupported DXGI format");
1184 return DXGI_FORMAT_UNKNOWN;
1188 ResourceDescription m_resourceDesc;
1190 deUint64 m_sharedMemSize;
1191 deUint64 m_sharedMemOffset;
1192 HANDLE m_sharedMemHandle[BUFFER_COUNT];
1193 bool m_isMemNtHandle;
1195 ID3D11Device* m_pDevice;
1196 ID3D11DeviceContext* m_pContext;
1197 LPD3DX11COMPILEFROMMEMORY m_fnD3DX11CompileFromMemory;
1198 pD3DCompile m_fnD3DCompile;
1200 ID3D11RenderTargetView* m_pRenderTargetView;
1201 ID3D11VertexShader* m_pVertexShader;
1202 ID3D11PixelShader* m_pPixelShader;
1203 ID3D11Buffer* m_pVertexBuffer;
1204 ID3D11ShaderResourceView* m_pTextureRV;
1205 ID3D11SamplerState* m_pSamplerLinear;
1207 ID3D11Texture2D* m_pTexture[BUFFER_COUNT];
1208 ID3D11Buffer* m_pBuffer[BUFFER_COUNT];
1209 IDXGIKeyedMutex* m_keyedMutex[BUFFER_COUNT];
1211 SECURITY_ATTRIBUTES m_securityAttributes;
1213 SECURITY_ATTRIBUTES* getSecurityAttributes ()
1215 m_securityAttributes.nLength = sizeof (SECURITY_ATTRIBUTES);
1216 m_securityAttributes.bInheritHandle = TRUE;
1217 if (!m_securityAttributes.lpSecurityDescriptor)
1218 m_securityAttributes.lpSecurityDescriptor = getSecurityDescriptor();
1220 return &m_securityAttributes;
1222 #endif // #if (DE_OS == DE_OS_WIN32)
1225 class DX11OperationSupport
1228 DX11OperationSupport (const vk::InstanceInterface& vki,
1229 vk::VkPhysicalDevice physicalDevice)
1230 #if (DE_OS == DE_OS_WIN32)
1233 , m_hD3DCompilerLib (0)
1235 , m_fnD3D11CreateDevice (0)
1236 , m_fnD3DX11CompileFromMemory (0)
1237 , m_fnD3DCompile (0)
1240 #if (DE_OS == DE_OS_WIN32)
1243 vk::VkPhysicalDeviceIDProperties propertiesId = { vk::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
1244 vk::VkPhysicalDeviceProperties2 properties = { vk::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
1246 properties.pNext = &propertiesId;
1248 vki.getPhysicalDeviceProperties2(physicalDevice, &properties);
1249 if (!propertiesId.deviceLUIDValid)
1250 TCU_FAIL("Physical device deviceLUIDValid is not valid");
1253 m_hD3D11Lib = LoadLibrary("d3d11.dll");
1255 TCU_FAIL("Failed to load d3d11.dll");
1258 m_fnD3D11CreateDevice = (LPD3D11CREATEDEVICE) GetProcAddress(m_hD3D11Lib, "D3D11CreateDevice");
1259 if (!m_fnD3D11CreateDevice)
1260 TCU_FAIL("Unable to find D3D11CreateDevice() function");
1262 m_hD3DX11Lib = LoadLibrary("d3dx11_42.dll");
1264 m_fnD3DX11CompileFromMemory = (LPD3DX11COMPILEFROMMEMORY) GetProcAddress(m_hD3DX11Lib, "D3DX11CompileFromMemory");
1267 m_hD3DCompilerLib = LoadLibrary("d3dcompiler_43.dll");
1268 if (!m_hD3DCompilerLib)
1269 m_hD3DCompilerLib = LoadLibrary("d3dcompiler_47.dll");
1270 if (!m_hD3DCompilerLib)
1271 TCU_FAIL("Unable to load DX11 d3dcompiler_43.dll or d3dcompiler_47.dll");
1273 m_fnD3DCompile = (pD3DCompile)GetProcAddress(m_hD3DCompilerLib, "D3DCompile");
1274 if (!m_fnD3DCompile)
1275 TCU_FAIL("Unable to load find D3DCompile");
1278 m_hDxgiLib = LoadLibrary("dxgi.dll");
1280 TCU_FAIL("Unable to load DX11 dxgi.dll");
1282 typedef HRESULT (WINAPI *LPCREATEDXGIFACTORY1)(REFIID riid, void** ppFactory);
1283 LPCREATEDXGIFACTORY1 CreateDXGIFactory1 = (LPCREATEDXGIFACTORY1)GetProcAddress(m_hDxgiLib, "CreateDXGIFactory1");
1284 if (!CreateDXGIFactory1)
1285 TCU_FAIL("Unable to load find CreateDXGIFactory1");
1287 IDXGIFactory1* pFactory = NULL;
1288 hr = CreateDXGIFactory1(__uuidof(IDXGIFactory), (void**)&pFactory);
1290 TCU_FAIL("Unable to create IDXGIFactory interface");
1292 IDXGIAdapter *pAdapter = NULL;
1293 for (UINT i = 0; pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++i)
1295 DXGI_ADAPTER_DESC desc;
1296 pAdapter->GetDesc(&desc);
1298 if (deMemCmp(&desc.AdapterLuid, propertiesId.deviceLUID, VK_LUID_SIZE_KHR) == 0)
1301 pFactory->Release();
1303 D3D_FEATURE_LEVEL fLevel[] = {D3D_FEATURE_LEVEL_11_0};
1304 UINT devflags = D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS | // no separate D3D11 worker thread
1306 D3D11_CREATE_DEVICE_DEBUG | // useful for diagnosing DX failures
1308 D3D11_CREATE_DEVICE_SINGLETHREADED;
1310 hr = m_fnD3D11CreateDevice (pAdapter,
1311 pAdapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
1315 DE_LENGTH_OF_ARRAY(fLevel),
1322 pAdapter->Release();
1326 TCU_FAIL("Failed to created DX11 device");
1328 TCU_FAIL("Failed to created DX11 context");
1331 DE_UNREF(physicalDevice);
1332 TCU_THROW(NotSupportedError, "OS not supported");
1336 ~DX11OperationSupport ()
1338 #if (DE_OS == DE_OS_WIN32)
1343 #if (DE_OS == DE_OS_WIN32)
1347 m_pContext->Release();
1352 m_pDevice->Release();
1358 FreeLibrary(m_hDxgiLib);
1362 if (m_hD3DCompilerLib)
1364 FreeLibrary(m_hD3DCompilerLib);
1365 m_hD3DCompilerLib = 0;
1370 FreeLibrary(m_hD3DX11Lib);
1376 FreeLibrary(m_hD3D11Lib);
1383 virtual de::MovePtr<DX11Operation> build (const ResourceDescription& resourceDesc, vk::VkExternalMemoryHandleTypeFlagBits memoryHandleType) const
1385 #if (DE_OS == DE_OS_WIN32)
1386 return de::MovePtr<DX11Operation>(new DX11Operation(resourceDesc, memoryHandleType, m_pDevice, m_pContext, m_fnD3DX11CompileFromMemory, m_fnD3DCompile));
1388 DE_UNREF(resourceDesc);
1389 DE_UNREF(memoryHandleType);
1390 TCU_THROW(NotSupportedError, "OS not supported");
1396 #if (DE_OS == DE_OS_WIN32)
1397 typedef HRESULT (WINAPI *LPD3D11CREATEDEVICE)(IDXGIAdapter*,
1401 const D3D_FEATURE_LEVEL*,
1406 ID3D11DeviceContext**);
1408 HMODULE m_hD3D11Lib;
1409 HMODULE m_hD3DX11Lib;
1410 HMODULE m_hD3DCompilerLib;
1412 LPD3D11CREATEDEVICE m_fnD3D11CreateDevice;
1413 LPD3DX11COMPILEFROMMEMORY m_fnD3DX11CompileFromMemory;
1414 pD3DCompile m_fnD3DCompile;
1415 ID3D11Device* m_pDevice;
1416 ID3D11DeviceContext* m_pContext;
1420 // Class to wrap a singleton instance and device
1421 class InstanceAndDevice
1423 InstanceAndDevice (Context& context)
1424 : m_instance (createTestInstance(context))
1425 , m_vki (m_instance.getDriver())
1426 , m_physicalDevice (vk::chooseDevice(m_vki, m_instance, context.getTestContext().getCommandLine()))
1427 , m_logicalDevice (createTestDevice(context, m_instance, m_vki, m_physicalDevice))
1428 , m_supportDX11 (new DX11OperationSupport(m_vki, m_physicalDevice))
1434 static vk::VkInstance getInstance(Context& context)
1436 if (!m_instanceAndDevice)
1437 m_instanceAndDevice = SharedPtr<InstanceAndDevice>(new InstanceAndDevice(context));
1439 return m_instanceAndDevice->m_instance;
1441 static const vk::InstanceDriver& getDriver()
1443 DE_ASSERT(m_instanceAndDevice);
1444 return m_instanceAndDevice->m_instance.getDriver();
1446 static vk::VkPhysicalDevice getPhysicalDevice()
1448 DE_ASSERT(m_instanceAndDevice);
1449 return m_instanceAndDevice->m_physicalDevice;
1451 static const Unique<vk::VkDevice>& getDevice()
1453 DE_ASSERT(m_instanceAndDevice);
1454 return m_instanceAndDevice->m_logicalDevice;
1456 static const de::UniquePtr<DX11OperationSupport>& getSupportDX11()
1458 DE_ASSERT(m_instanceAndDevice);
1459 return m_instanceAndDevice->m_supportDX11;
1461 static void collectMessages()
1463 DE_ASSERT(m_instanceAndDevice);
1464 m_instanceAndDevice->m_instance.collectMessages();
1467 static void destroy()
1469 m_instanceAndDevice.clear();
1473 CustomInstance m_instance;
1474 const vk::InstanceDriver& m_vki;
1475 const vk::VkPhysicalDevice m_physicalDevice;
1476 const Unique<vk::VkDevice> m_logicalDevice;
1477 const de::UniquePtr<DX11OperationSupport> m_supportDX11;
1479 static SharedPtr<InstanceAndDevice> m_instanceAndDevice;
1481 SharedPtr<InstanceAndDevice> InstanceAndDevice::m_instanceAndDevice;
1484 class Win32KeyedMutexTestInstance : public TestInstance
1487 Win32KeyedMutexTestInstance (Context& context,
1490 virtual tcu::TestStatus iterate (void);
1493 const TestConfig m_config;
1494 const de::UniquePtr<OperationSupport> m_supportWriteOp;
1495 const de::UniquePtr<OperationSupport> m_supportReadOp;
1497 const vk::VkInstance m_instance;
1499 const vk::InstanceDriver& m_vki;
1500 const vk::VkPhysicalDevice m_physicalDevice;
1501 const std::vector<vk::VkQueueFamilyProperties> m_queueFamilies;
1502 const std::vector<deUint32> m_queueFamilyIndices;
1503 const vk::Unique<vk::VkDevice>& m_device;
1504 const vk::DeviceDriver m_vkd;
1506 const vk::VkExternalMemoryHandleTypeFlagBits m_memoryHandleType;
1508 // \todo Should this be moved to the group same way as in the other tests?
1509 PipelineCacheData m_pipelineCacheData;
1510 tcu::ResultCollector m_resultCollector;
1513 bool m_useDedicatedAllocation;
1516 Win32KeyedMutexTestInstance::Win32KeyedMutexTestInstance (Context& context,
1518 : TestInstance (context)
1520 , m_supportWriteOp (makeOperationSupport(config.writeOp, config.resource))
1521 , m_supportReadOp (makeOperationSupport(config.readOp, config.resource))
1523 , m_instance (InstanceAndDevice::getInstance(context))
1525 , m_vki (InstanceAndDevice::getDriver())
1526 , m_physicalDevice (InstanceAndDevice::getPhysicalDevice())
1527 , m_queueFamilies (vk::getPhysicalDeviceQueueFamilyProperties(m_vki, m_physicalDevice))
1528 , m_queueFamilyIndices (getFamilyIndices(m_queueFamilies))
1529 , m_device (InstanceAndDevice::getDevice())
1530 , m_vkd (context.getPlatformInterface(), m_instance, *m_device)
1532 , m_memoryHandleType ((m_config.resource.type == RESOURCE_TYPE_IMAGE) ? m_config.memoryHandleTypeImage : m_config.memoryHandleTypeBuffer)
1534 , m_resultCollector (context.getTestContext().getLog())
1537 , m_useDedicatedAllocation (false)
1539 #if (DE_OS == DE_OS_WIN32)
1540 TestLog& log = m_context.getTestContext().getLog();
1542 // Check resource support
1543 if (m_config.resource.type == RESOURCE_TYPE_IMAGE)
1545 if (m_memoryHandleType == vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT && !IsWindows8OrGreater())
1546 TCU_THROW(NotSupportedError, "Memory handle type not supported by this OS");
1548 const vk::VkPhysicalDeviceExternalImageFormatInfo externalInfo =
1550 vk::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
1554 const vk::VkPhysicalDeviceImageFormatInfo2 imageFormatInfo =
1556 vk::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1558 m_config.resource.imageFormat,
1559 m_config.resource.imageType,
1560 vk::VK_IMAGE_TILING_OPTIMAL,
1561 m_supportReadOp->getInResourceUsageFlags() | m_supportWriteOp->getOutResourceUsageFlags(),
1564 vk::VkExternalImageFormatProperties externalProperties =
1566 vk::VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES,
1570 vk::VkImageFormatProperties2 formatProperties =
1572 vk::VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
1573 &externalProperties,
1582 const vk::VkResult res = m_vki.getPhysicalDeviceImageFormatProperties2(m_physicalDevice, &imageFormatInfo, &formatProperties);
1583 if (res == vk::VK_ERROR_FORMAT_NOT_SUPPORTED)
1584 TCU_THROW(NotSupportedError, "Handle type is not compatible");
1587 // \todo How to log this nicely?
1588 log << TestLog::Message << "External image format properties: " << imageFormatInfo << "\n"<< externalProperties << TestLog::EndMessage;
1590 if ((externalProperties.externalMemoryProperties.externalMemoryFeatures & vk::VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT) == 0)
1591 TCU_THROW(NotSupportedError, "Importing image resource not supported");
1593 if (externalProperties.externalMemoryProperties.externalMemoryFeatures & vk::VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT)
1594 m_useDedicatedAllocation = true;
1598 if (m_memoryHandleType == vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT && !IsWindows8OrGreater())
1599 TCU_THROW(NotSupportedError, "Memory handle type not supported by this OS");
1601 const vk::VkPhysicalDeviceExternalBufferInfo info =
1603 vk::VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_BUFFER_INFO,
1607 m_supportReadOp->getInResourceUsageFlags() | m_supportWriteOp->getOutResourceUsageFlags(),
1610 vk::VkExternalBufferProperties properties =
1612 vk::VK_STRUCTURE_TYPE_EXTERNAL_BUFFER_PROPERTIES,
1616 m_vki.getPhysicalDeviceExternalBufferProperties(m_physicalDevice, &info, &properties);
1618 log << TestLog::Message << "External buffer properties: " << info << "\n" << properties << TestLog::EndMessage;
1620 if ((properties.externalMemoryProperties.externalMemoryFeatures & vk::VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT) == 0)
1621 TCU_THROW(NotSupportedError, "Importing memory type not supported");
1623 if (properties.externalMemoryProperties.externalMemoryFeatures & vk::VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT)
1624 m_useDedicatedAllocation = true;
1627 DE_UNREF(m_useDedicatedAllocation);
1628 TCU_THROW(NotSupportedError, "OS not supported");
1632 tcu::TestStatus Win32KeyedMutexTestInstance::iterate (void)
1634 TestLog& log (m_context.getTestContext().getLog());
1638 const deUint32 queueFamily = (deUint32)m_queueNdx;
1640 const tcu::ScopedLogSection queuePairSection (log, "Queue-" + de::toString(queueFamily), "Queue-" + de::toString(queueFamily));
1642 const vk::VkQueue queue (getDeviceQueue(m_vkd, *m_device, queueFamily, 0u));
1643 const vk::Unique<vk::VkCommandPool> commandPool (createCommandPool(m_vkd, *m_device, 0u, queueFamily));
1644 const vk::Unique<vk::VkCommandBuffer> commandBufferWrite (allocateCommandBuffer(m_vkd, *m_device, *commandPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY));
1645 const vk::Unique<vk::VkCommandBuffer> commandBufferRead (allocateCommandBuffer(m_vkd, *m_device, *commandPool, vk::VK_COMMAND_BUFFER_LEVEL_PRIMARY));
1646 vk::SimpleAllocator allocator (m_vkd, *m_device, vk::getPhysicalDeviceMemoryProperties(m_vki, m_physicalDevice));
1647 OperationContext operationContext (m_context, m_vki, m_vkd, m_physicalDevice, *m_device, allocator, m_context.getBinaryCollection(), m_pipelineCacheData);
1649 if (!checkQueueFlags(m_queueFamilies[m_queueNdx].queueFlags, vk::VK_QUEUE_GRAPHICS_BIT))
1650 TCU_THROW(NotSupportedError, "Operation not supported by the source queue");
1652 const de::UniquePtr<DX11Operation> dx11Op (InstanceAndDevice::getSupportDX11()->build(m_config.resource, m_memoryHandleType));
1654 NativeHandle nativeHandleWrite = dx11Op->getNativeHandle(DX11Operation::BUFFER_VK_WRITE);
1655 const de::UniquePtr<Resource> resourceWrite (importResource(m_vkd, *m_device, m_config.resource, m_queueFamilyIndices, *m_supportReadOp, *m_supportWriteOp, nativeHandleWrite, m_memoryHandleType));
1657 NativeHandle nativeHandleRead = dx11Op->getNativeHandle(DX11Operation::BUFFER_VK_READ);
1658 const de::UniquePtr<Resource> resourceRead (importResource(m_vkd, *m_device, m_config.resource, m_queueFamilyIndices, *m_supportReadOp, *m_supportWriteOp, nativeHandleRead, m_memoryHandleType));
1660 const de::UniquePtr<Operation> writeOp (m_supportWriteOp->build(operationContext, *resourceWrite));
1661 const de::UniquePtr<Operation> readOp (m_supportReadOp->build(operationContext, *resourceRead));
1663 const SyncInfo writeSync = writeOp->getOutSyncInfo();
1664 const SyncInfo readSync = readOp->getInSyncInfo();
1666 beginCommandBuffer(m_vkd, *commandBufferWrite);
1667 writeOp->recordCommands(*commandBufferWrite);
1668 recordWriteBarrier(m_vkd, *commandBufferWrite, *resourceWrite, writeSync, queueFamily, readSync);
1669 endCommandBuffer(m_vkd, *commandBufferWrite);
1671 beginCommandBuffer(m_vkd, *commandBufferRead);
1672 recordReadBarrier(m_vkd, *commandBufferRead, *resourceRead, writeSync, readSync, queueFamily);
1673 readOp->recordCommands(*commandBufferRead);
1674 endCommandBuffer(m_vkd, *commandBufferRead);
1677 vk::VkDeviceMemory memory = resourceWrite->getMemory();
1678 deUint64 keyInit = DX11Operation::KEYED_MUTEX_VK_WRITE;
1679 deUint32 timeout = 0xFFFFFFFF; // INFINITE
1680 deUint64 keyExternal = DX11Operation::KEYED_MUTEX_DX_COPY;
1681 vk::VkWin32KeyedMutexAcquireReleaseInfoKHR keyedMutexInfo =
1683 vk::VK_STRUCTURE_TYPE_WIN32_KEYED_MUTEX_ACQUIRE_RELEASE_INFO_KHR,
1696 const vk::VkCommandBuffer commandBuffer = *commandBufferWrite;
1697 const vk::VkSubmitInfo submitInfo =
1699 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,
1712 VK_CHECK(m_vkd.queueSubmit(queue, 1u, &submitInfo, DE_NULL));
1715 dx11Op->copyMemory();
1718 vk::VkDeviceMemory memory = resourceRead->getMemory();
1719 deUint64 keyInternal = DX11Operation::KEYED_MUTEX_VK_VERIFY;
1720 deUint32 timeout = 0xFFFFFFFF; // INFINITE
1721 deUint64 keyExternal = DX11Operation::KEYED_MUTEX_DONE;
1722 vk::VkWin32KeyedMutexAcquireReleaseInfoKHR keyedMutexInfo =
1724 vk::VK_STRUCTURE_TYPE_WIN32_KEYED_MUTEX_ACQUIRE_RELEASE_INFO_KHR,
1737 const vk::VkCommandBuffer commandBuffer = *commandBufferRead;
1738 const vk::VkSubmitInfo submitInfo =
1740 vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,
1753 VK_CHECK(m_vkd.queueSubmit(queue, 1u, &submitInfo, DE_NULL));
1756 VK_CHECK(m_vkd.queueWaitIdle(queue));
1759 const Data expected = writeOp->getData();
1760 const Data actual = readOp->getData();
1762 DE_ASSERT(expected.size == actual.size);
1764 if (0 != deMemCmp(expected.data, actual.data, expected.size))
1766 const size_t maxBytesLogged = 256;
1767 std::ostringstream expectedData;
1768 std::ostringstream actualData;
1771 // Find first byte difference
1772 for (; actual.data[byteNdx] == expected.data[byteNdx]; byteNdx++)
1777 log << TestLog::Message << "First different byte at offset: " << byteNdx << TestLog::EndMessage;
1779 // Log 8 previous bytes before the first incorrect byte
1782 expectedData << "... ";
1783 actualData << "... ";
1790 for (size_t i = 0; i < maxBytesLogged && byteNdx < expected.size; i++, byteNdx++)
1792 expectedData << (i > 0 ? ", " : "") << (deUint32)expected.data[byteNdx];
1793 actualData << (i > 0 ? ", " : "") << (deUint32)actual.data[byteNdx];
1796 if (expected.size > byteNdx)
1798 expectedData << "...";
1799 actualData << "...";
1802 log << TestLog::Message << "Expected data: (" << expectedData.str() << ")" << TestLog::EndMessage;
1803 log << TestLog::Message << "Actual data: (" << actualData.str() << ")" << TestLog::EndMessage;
1805 m_resultCollector.fail("Memory contents don't match");
1809 catch (const tcu::NotSupportedError& error)
1811 log << TestLog::Message << "Not supported: " << error.getMessage() << TestLog::EndMessage;
1813 catch (const tcu::TestError& error)
1815 m_resultCollector.fail(std::string("Exception: ") + error.getMessage());
1818 // Collect possible validation errors.
1819 InstanceAndDevice::collectMessages();
1821 // Move to next queue
1825 if (m_queueNdx >= m_queueFamilies.size())
1827 return tcu::TestStatus(m_resultCollector.getResult(), m_resultCollector.getMessage());
1831 return tcu::TestStatus::incomplete();
1838 void init (vk::SourceCollections& dst, TestConfig config) const
1840 const de::UniquePtr<OperationSupport> readOp (makeOperationSupport(config.readOp, config.resource));
1841 const de::UniquePtr<OperationSupport> writeOp (makeOperationSupport(config.writeOp, config.resource));
1843 readOp->initPrograms(dst);
1844 writeOp->initPrograms(dst);
1850 static void createTests (tcu::TestCaseGroup* group)
1852 tcu::TestContext& testCtx = group->getTestContext();
1855 vk::VkExternalMemoryHandleTypeFlagBits memoryHandleTypeBuffer;
1856 vk::VkExternalMemoryHandleTypeFlagBits memoryHandleTypeImage;
1857 const char* nameSuffix;
1861 (vk::VkExternalMemoryHandleTypeFlagBits)0u, // DX11 doesn't support buffers with an NT handle
1862 vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT,
1866 vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT_BIT,
1867 vk::VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_KMT_BIT,
1872 for (size_t writeOpNdx = 0; writeOpNdx < DE_LENGTH_OF_ARRAY(s_writeOps); ++writeOpNdx)
1873 for (size_t readOpNdx = 0; readOpNdx < DE_LENGTH_OF_ARRAY(s_readOps); ++readOpNdx)
1875 const OperationName writeOp = s_writeOps[writeOpNdx];
1876 const OperationName readOp = s_readOps[readOpNdx];
1877 const std::string opGroupName = getOperationName(writeOp) + "_" + getOperationName(readOp);
1880 de::MovePtr<tcu::TestCaseGroup> opGroup (new tcu::TestCaseGroup(testCtx, opGroupName.c_str(), ""));
1882 for (size_t resourceNdx = 0; resourceNdx < DE_LENGTH_OF_ARRAY(s_resourcesWin32KeyedMutex); ++resourceNdx)
1884 const ResourceDescription& resource = s_resourcesWin32KeyedMutex[resourceNdx];
1886 for (size_t caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); caseNdx++)
1888 if (resource.type == RESOURCE_TYPE_BUFFER && !cases[caseNdx].memoryHandleTypeBuffer)
1891 if (resource.type == RESOURCE_TYPE_IMAGE && !cases[caseNdx].memoryHandleTypeImage)
1894 std::string name = getResourceName(resource) + cases[caseNdx].nameSuffix;
1896 if (isResourceSupported(writeOp, resource) && isResourceSupported(readOp, resource))
1898 const TestConfig config (resource, writeOp, readOp, cases[caseNdx].memoryHandleTypeBuffer, cases[caseNdx].memoryHandleTypeImage);
1900 opGroup->addChild(new InstanceFactory1<Win32KeyedMutexTestInstance, TestConfig, Progs>(testCtx, tcu::NODETYPE_SELF_VALIDATE, name, "", Progs(), config));
1907 group->addChild(opGroup.release());
1911 static void cleanupGroup (tcu::TestCaseGroup* group)
1914 // Destroy singleton object
1915 InstanceAndDevice::destroy();
1918 tcu::TestCaseGroup* createWin32KeyedMutexTest (tcu::TestContext& testCtx)
1920 return createTestGroup(testCtx, "win32_keyed_mutex", "", createTests, cleanupGroup);
1923 } // synchronization