The open source OpenXR runtime
at prediction-2 218 lines 6.2 kB view raw
1// Copyright 2022, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief Direct3D 11 tests. 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 */ 8#include "catch_amalgamated.hpp" 9 10#include "aux_d3d_dxgi_formats.hpp" 11 12#include <iostream> 13 14#include <xrt/xrt_config_have.h> 15#include <d3d/d3d_dxgi_helpers.hpp> 16#include <d3d/d3d_d3d11_helpers.hpp> 17#include <d3d/d3d_d3d11_allocator.hpp> 18#include <util/u_win32_com_guard.hpp> 19 20#ifdef XRT_HAVE_VULKAN 21#include "vktest_init_bundle.hpp" 22#include <vk/vk_image_allocator.h> 23#include <util/u_handles.h> 24#include <d3d/d3d_dxgi_formats.h> 25#endif 26 27#include <d3d11_4.h> 28 29using namespace xrt::auxiliary::d3d; 30using namespace xrt::auxiliary::d3d::d3d11; 31using namespace xrt::auxiliary::util; 32 33 34TEST_CASE("dxgi_adapter", "[.][needgpu]") 35{ 36 ComGuard comGuard; 37 wil::com_ptr<IDXGIAdapter> adapter; 38 39 CHECK_NOTHROW(adapter = getAdapterByIndex(0, U_LOGGING_TRACE)); 40 CHECK(adapter); 41 auto adapter1 = adapter.query<IDXGIAdapter1>(); 42 DXGI_ADAPTER_DESC1 desc{}; 43 REQUIRE(SUCCEEDED(adapter1->GetDesc1(&desc))); 44 45 xrt_luid_t luid{}; 46 memcpy(&luid, &(desc.AdapterLuid), sizeof(luid)); 47 48 49 wil::com_ptr<IDXGIAdapter> adapterFromLuid; 50 CHECK_NOTHROW(adapterFromLuid = getAdapterByLUID(luid, U_LOGGING_TRACE)); 51 CHECK(adapterFromLuid); 52} 53 54TEST_CASE("d3d11_device", "[.][needgpu]") 55{ 56 ComGuard comGuard; 57 wil::com_ptr<IDXGIAdapter> adapter; 58 59 CHECK_NOTHROW(adapter = getAdapterByIndex(0, U_LOGGING_TRACE)); 60 CHECK(adapter); 61 wil::com_ptr<ID3D11Device> device; 62 wil::com_ptr<ID3D11DeviceContext> context; 63 CHECK_NOTHROW(std::tie(device, context) = createDevice(adapter, U_LOGGING_TRACE)); 64 CHECK(device); 65 CHECK(context); 66} 67 68#ifdef XRT_HAVE_VULKAN 69 70static inline bool 71tryImport(struct vk_bundle *vk, std::vector<HANDLE> const &handles, const struct xrt_swapchain_create_info &xsci) 72{ 73 74 INFO("Testing import into Vulkan"); 75 76 static constexpr bool use_dedicated_allocation = false; 77 xrt_swapchain_create_info vk_info = xsci; 78 vk_info.format = d3d_dxgi_format_to_vk((DXGI_FORMAT)xsci.format); 79 const auto free_vk_ic = [&](struct vk_image_collection *vkic) { 80 vk_ic_destroy(vk, vkic); 81 delete vkic; 82 }; 83 84 std::shared_ptr<vk_image_collection> vkic{new vk_image_collection, free_vk_ic}; 85 86 uint32_t image_count = static_cast<uint32_t>(handles.size()); 87 // Populate for import 88 std::vector<xrt_image_native> xins; 89 xins.reserve(image_count); 90 91 // Keep this around until after successful import, then detach all. 92 std::vector<wil::unique_handle> handlesForImport; 93 handlesForImport.reserve(image_count); 94 95 for (HANDLE handle : handles) { 96 wil::unique_handle duped{u_graphics_buffer_ref(handle)}; 97 xrt_image_native xin; 98 xin.handle = duped.get(); 99 xin.size = 0; 100 xin.use_dedicated_allocation = use_dedicated_allocation; 101 102 handlesForImport.emplace_back(std::move(duped)); 103 xins.emplace_back(xin); 104 } 105 106 // Import into a vulkan image collection 107 bool result = VK_SUCCESS == vk_ic_from_natives(vk, &vk_info, xins.data(), (uint32_t)xins.size(), vkic.get()); 108 109 if (result) { 110 // The imported swapchain took ownership of them now, release them from ownership here. 111 for (auto &h : handlesForImport) { 112 h.release(); 113 } 114 } 115 return result; 116} 117#else 118 119static inline bool 120tryImport(struct vk_bundle * /* vk */, 121 std::vector<wil::unique_handle> const & /* handles */, 122 const struct xrt_swapchain_create_info & /* xsci */) 123{ 124 return true; 125} 126 127#endif 128 129TEST_CASE("d3d11_allocate", "[.][needgpu]") 130{ 131 unique_vk_bundle vk = makeVkBundle(); 132 133#ifdef XRT_HAVE_VULKAN 134 REQUIRE(vktest_init_bundle(vk.get())); 135#endif 136 137 ComGuard comGuard; 138 wil::com_ptr<ID3D11Device> device; 139 wil::com_ptr<ID3D11DeviceContext> context; 140 std::tie(device, context) = createDevice(); 141 auto device5 = device.query<ID3D11Device5>(); 142 std::vector<wil::com_ptr<ID3D11Texture2D1>> images; 143 std::vector<HANDLE> handles; 144 145 static constexpr bool kKeyedMutex = true; 146 size_t imageCount = 3; 147 148 xrt_swapchain_create_info xsci{}; 149 CAPTURE(xsci.sample_count = 1); 150 CAPTURE(xsci.width = 800); 151 CAPTURE(xsci.height = 600); 152 153 CAPTURE(xsci.mip_count = 1); 154 xsci.face_count = 1; 155 xsci.array_size = 1; 156 SECTION("create images") 157 { 158 auto nameAndFormat = GENERATE(values(namesAndFormats)); 159 DYNAMIC_SECTION("Texture format " << nameAndFormat.first) 160 { 161 auto format = nameAndFormat.second; 162 CAPTURE(isDepthStencilFormat(format)); 163 xsci.format = format; 164 if (isDepthStencilFormat(format)) { 165 xsci.bits = XRT_SWAPCHAIN_USAGE_DEPTH_STENCIL; 166 } else { 167 xsci.bits = XRT_SWAPCHAIN_USAGE_COLOR; 168 } 169 xsci.bits = (xrt_swapchain_usage_bits)(xsci.bits | XRT_SWAPCHAIN_USAGE_SAMPLED); 170 images.clear(); 171 handles.clear(); 172 SECTION("invalid array size 0") 173 { 174 CAPTURE(xsci.array_size = 0); 175 REQUIRE(XRT_SUCCESS != 176 allocateSharedImages(*device5, xsci, imageCount, kKeyedMutex, images, handles)); 177 CHECK(images.empty()); 178 CHECK(handles.empty()); 179 } 180 SECTION("not array") 181 { 182 CAPTURE(xsci.array_size); 183 REQUIRE(XRT_SUCCESS == 184 allocateSharedImages(*device5, xsci, imageCount, kKeyedMutex, images, handles)); 185 CHECK(images.size() == imageCount); 186 CHECK(handles.size() == imageCount); 187 CHECK(tryImport(vk.get(), handles, xsci)); 188 } 189 SECTION("array of 2") 190 { 191 CAPTURE(xsci.array_size = 2); 192 REQUIRE(XRT_SUCCESS == 193 allocateSharedImages(*device5, xsci, imageCount, kKeyedMutex, images, handles)); 194 CHECK(images.size() == imageCount); 195 CHECK(handles.size() == imageCount); 196 CHECK(tryImport(vk.get(), handles, xsci)); 197 } 198 SECTION("cubemaps not implemented") 199 { 200 CAPTURE(xsci.array_size); 201 CAPTURE(xsci.face_count = 6); 202 REQUIRE(XRT_ERROR_ALLOCATION == 203 allocateSharedImages(*device5, xsci, imageCount, kKeyedMutex, images, handles)); 204 CHECK(images.empty()); 205 CHECK(handles.empty()); 206 } 207 SECTION("protected content not implemented") 208 { 209 CAPTURE(xsci.array_size); 210 CAPTURE(xsci.create = XRT_SWAPCHAIN_CREATE_PROTECTED_CONTENT); 211 REQUIRE(XRT_ERROR_SWAPCHAIN_FLAG_VALID_BUT_UNSUPPORTED == 212 allocateSharedImages(*device5, xsci, imageCount, kKeyedMutex, images, handles)); 213 CHECK(images.empty()); 214 CHECK(handles.empty()); 215 } 216 } 217 } 218}