The open source OpenXR runtime
at main 3.2 kB view raw
1// Copyright 2022, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief Direct3D 12 tests. 6 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 7 */ 8 9 10#include "mock/mock_compositor.h" 11#include "client/comp_d3d12_client.h" 12 13#include "catch_amalgamated.hpp" 14#include "util/u_handles.h" 15 16#include <d3d/d3d_dxgi_helpers.hpp> 17#include <d3d/d3d_d3d12_helpers.hpp> 18#include <stdint.h> 19#include <util/u_win32_com_guard.hpp> 20 21#include <d3d11_4.h> 22 23 24using namespace xrt::auxiliary::d3d; 25using namespace xrt::auxiliary::d3d::d3d12; 26using namespace xrt::auxiliary::util; 27 28TEST_CASE("d3d12_client_compositor", "[.][needgpu]") 29{ 30 xrt_compositor_native *xcn = mock_create_native_compositor(); 31 struct mock_compositor *mc = mock_compositor(&(xcn->base)); 32 33 ComGuard comGuard; 34 35 wil::com_ptr<ID3D12Device> device = createDevice(); 36 wil::com_ptr<ID3D12CommandQueue> queue; 37 D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_NORMAL, 38 D3D12_COMMAND_QUEUE_FLAG_NONE, 0}; 39 device->CreateCommandQueue(&desc, IID_PPV_ARGS(queue.put())); 40 struct xrt_compositor_d3d12 *xcd3d = client_d3d12_compositor_create(xcn, device.get(), queue.get()); 41 struct xrt_compositor *xc = &xcd3d->base; 42 43 SECTION("Swapchain create and import") 44 { 45 struct Data 46 { 47 bool nativeCreateCalled = false; 48 49 bool nativeImportCalled = false; 50 } data; 51 mc->userdata = &data; 52 mc->compositor_hooks.create_swapchain = 53 [](struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, 54 const struct xrt_swapchain_create_info *info, struct xrt_swapchain **out_xsc) { 55 auto *data = static_cast<Data *>(mc->userdata); 56 data->nativeCreateCalled = true; 57 return XRT_SUCCESS; 58 }; 59 mc->compositor_hooks.import_swapchain = 60 [](struct mock_compositor *mc, struct mock_compositor_swapchain *mcsc, 61 const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images, 62 uint32_t image_count, struct xrt_swapchain **out_xscc) { 63 auto *data = static_cast<Data *>(mc->userdata); 64 data->nativeImportCalled = true; 65 // need to release the native handles to avoid leaks 66 for (uint32_t i = 0; i < image_count; ++i) { 67 if (!native_images[i].is_dxgi_handle) { 68 u_graphics_buffer_unref(&native_images[i].handle); 69 } 70 } 71 return XRT_SUCCESS; 72 }; 73 xrt_swapchain_create_info xsci{}; 74 xsci.format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB; 75 xsci.bits = (xrt_swapchain_usage_bits)(XRT_SWAPCHAIN_USAGE_COLOR | XRT_SWAPCHAIN_USAGE_SAMPLED); 76 xsci.sample_count = 1; 77 xsci.width = 800; 78 xsci.height = 600; 79 xsci.face_count = 1; 80 xsci.array_size = 1; 81 xsci.mip_count = 1; 82 SECTION("Swapchain Create") 83 { 84 struct xrt_swapchain *xsc = nullptr; 85 // This will fail because the mock compositor doesn't actually import, but it will get far 86 // enough to trigger our hook and update the flag. 87 xrt_comp_create_swapchain(xc, &xsci, &xsc); 88 // D3D always imports into the native compositor 89 CHECK(data.nativeImportCalled); 90 CHECK_FALSE(data.nativeCreateCalled); 91 xrt_swapchain_reference(&xsc, nullptr); 92 } 93 } 94 95 xrt_comp_destroy(&xc); 96 xrt_comp_native_destroy(&xcn); 97}