diff --git a/src/xrt/state_trackers/oxr/CMakeLists.txt b/src/xrt/state_trackers/oxr/CMakeLists.txt index 5d0bc54b8..115f6bc4f 100644 --- a/src/xrt/state_trackers/oxr/CMakeLists.txt +++ b/src/xrt/state_trackers/oxr/CMakeLists.txt @@ -69,10 +69,14 @@ if(XRT_HAVE_EGL) target_sources(st_oxr PRIVATE oxr_session_gfx_egl.c) endif() +if(XRT_HAVE_D3D11 OR XRT_HAVE_D3D12) + target_sources(st_oxr PRIVATE oxr_d3d.cpp) + target_link_libraries(st_oxr PRIVATE aux_d3d) +endif() + if(XRT_HAVE_D3D11) target_compile_definitions(st_oxr PRIVATE XR_USE_GRAPHICS_API_D3D11) target_sources(st_oxr PRIVATE oxr_session_gfx_d3d11.c oxr_swapchain_d3d11.c oxr_d3d11.cpp) - target_link_libraries(st_oxr PRIVATE aux_d3d) endif() if(ANDROID) diff --git a/src/xrt/state_trackers/oxr/oxr_d3d.cpp b/src/xrt/state_trackers/oxr/oxr_d3d.cpp new file mode 100644 index 000000000..243e1bd2d --- /dev/null +++ b/src/xrt/state_trackers/oxr/oxr_d3d.cpp @@ -0,0 +1,92 @@ +// Copyright 2021-2022, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief D3D 11 and 12 shared routines + * @author Ryan Pavlik + * @ingroup oxr_main + */ + +#include "util/u_misc.h" +#include "util/u_debug.h" +#include "d3d/d3d_helpers.hpp" + +#include "oxr_objects.h" +#include "oxr_logger.h" + +#include +#include +#include + +#include +#include +#include + +#define DEFAULT_CATCH(MSG) \ + catch (wil::ResultException const &e) \ + { \ + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, MSG ": %s", e.what()); \ + } \ + catch (std::exception const &e) \ + { \ + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, MSG ": %s", e.what()); \ + } \ + catch (...) \ + { \ + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, MSG); \ + } + +using namespace xrt::auxiliary::d3d; + +XrResult +oxr_d3d_get_requirements(struct oxr_logger *log, + struct oxr_system *sys, + LUID *adapter_luid, + D3D_FEATURE_LEVEL *min_feature_level) +{ + try { + + if (sys->xsysc->info.client_d3d_deviceLUID_valid) { + sys->suggested_d3d_luid = + reinterpret_cast(sys->xsysc->info.client_d3d_deviceLUID); + if (nullptr == getAdapterByLUID(sys->xsysc->info.client_d3d_deviceLUID, U_LOGGING_INFO)) { + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, + " failure enumerating adapter for LUID specified for use."); + } + } else { + auto adapter = getAdapterByIndex(0, U_LOGGING_INFO); + if (adapter == nullptr) { + return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, " failure enumerating adapter LUIDs."); + } + DXGI_ADAPTER_DESC desc{}; + THROW_IF_FAILED(adapter->GetDesc(&desc)); + sys->suggested_d3d_luid = desc.AdapterLuid; + } + sys->suggested_d3d_luid_valid = true; + *adapter_luid = sys->suggested_d3d_luid; + //! @todo implement better? + *min_feature_level = D3D_FEATURE_LEVEL_11_0; + + return XR_SUCCESS; + } + DEFAULT_CATCH(" failure determining adapter LUID") +} + +XrResult +oxr_d3d_check_device(struct oxr_logger *log, struct oxr_system *sys, IDXGIDevice *device) +{ + try { + wil::com_ptr adapter; + THROW_IF_FAILED(device->GetAdapter(adapter.put())); + DXGI_ADAPTER_DESC desc{}; + adapter->GetDesc(&desc); + if (desc.AdapterLuid.HighPart != sys->suggested_d3d_luid.HighPart || + desc.AdapterLuid.LowPart != sys->suggested_d3d_luid.LowPart) { + + return oxr_error(log, XR_ERROR_GRAPHICS_DEVICE_INVALID, + " supplied device does not match required LUID."); + } + return XR_SUCCESS; + } + DEFAULT_CATCH(" failure checking adapter LUID") +} diff --git a/src/xrt/state_trackers/oxr/oxr_d3d11.cpp b/src/xrt/state_trackers/oxr/oxr_d3d11.cpp index c5d8a1b13..1a49cd6fe 100644 --- a/src/xrt/state_trackers/oxr/oxr_d3d11.cpp +++ b/src/xrt/state_trackers/oxr/oxr_d3d11.cpp @@ -1,4 +1,4 @@ -// Copyright 2021, Collabora, Ltd. +// Copyright 2021-2022, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -7,12 +7,6 @@ * @ingroup oxr_main */ -#include "xrt/xrt_gfx_d3d11.h" - -#include "util/u_misc.h" -#include "util/u_debug.h" -#include "d3d/d3d_helpers.hpp" - #include "oxr_objects.h" #include "oxr_logger.h" @@ -39,39 +33,13 @@ return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, MSG); \ } -using namespace xrt::auxiliary::d3d; - XrResult oxr_d3d11_get_requirements(struct oxr_logger *log, struct oxr_system *sys, XrGraphicsRequirementsD3D11KHR *graphicsRequirements) { - try { - - if (sys->xsysc->info.client_d3d_deviceLUID_valid) { - sys->suggested_d3d_luid = - reinterpret_cast(sys->xsysc->info.client_d3d_deviceLUID); - if (nullptr == getAdapterByLUID(sys->xsysc->info.client_d3d_deviceLUID, U_LOGGING_INFO)) { - return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, - " failure enumerating adapter for LUID specified for use."); - } - } else { - auto adapter = getAdapterByIndex(0, U_LOGGING_INFO); - if (adapter == nullptr) { - return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, " failure enumerating adapter LUIDs."); - } - DXGI_ADAPTER_DESC desc{}; - THROW_IF_FAILED(adapter->GetDesc(&desc)); - sys->suggested_d3d_luid = desc.AdapterLuid; - } - sys->suggested_d3d_luid_valid = true; - graphicsRequirements->adapterLuid = sys->suggested_d3d_luid; - //! @todo implement better? - graphicsRequirements->minFeatureLevel = D3D_FEATURE_LEVEL_11_0; - - return XR_SUCCESS; - } - DEFAULT_CATCH(" failure determining adapter LUID") + return oxr_d3d_get_requirements(log, sys, &graphicsRequirements->adapterLuid, + &graphicsRequirements->minFeatureLevel); } XrResult @@ -80,17 +48,7 @@ oxr_d3d11_check_device(struct oxr_logger *log, struct oxr_system *sys, ID3D11Dev try { wil::com_ptr dxgiDevice; THROW_IF_FAILED(device->QueryInterface(dxgiDevice.put())); - wil::com_ptr adapter; - THROW_IF_FAILED(dxgiDevice->GetAdapter(adapter.put())); - DXGI_ADAPTER_DESC desc{}; - adapter->GetDesc(&desc); - if (desc.AdapterLuid.HighPart != sys->suggested_d3d_luid.HighPart || - desc.AdapterLuid.LowPart != sys->suggested_d3d_luid.LowPart) { - - return oxr_error(log, XR_ERROR_GRAPHICS_DEVICE_INVALID, - " supplied device does not match required LUID."); - } - return XR_SUCCESS; + return oxr_d3d_check_device(log, sys, dxgiDevice.get()); } DEFAULT_CATCH(" failure checking adapter LUID") } diff --git a/src/xrt/state_trackers/oxr/oxr_objects.h b/src/xrt/state_trackers/oxr/oxr_objects.h index e5fc0d391..7a86571ff 100644 --- a/src/xrt/state_trackers/oxr/oxr_objects.h +++ b/src/xrt/state_trackers/oxr/oxr_objects.h @@ -16,6 +16,7 @@ #include "xrt/xrt_vulkan_includes.h" #include "xrt/xrt_openxr_includes.h" #include "xrt/xrt_config_os.h" +#include "xrt/xrt_config_have.h" #include "os/os_threading.h" @@ -27,6 +28,11 @@ #include "oxr_extension_support.h" #include "oxr_subaction.h" +#if defined(XRT_HAVE_D3D11) || defined(XRT_HAVE_D3D12) +#include +#include +#endif + #ifdef XRT_FEATURE_RENDERDOC #include "renderdoc_app.h" #ifndef XRT_OS_WINDOWS @@ -1109,6 +1115,22 @@ oxr_session_populate_egl(struct oxr_logger *log, #endif +/* + * + * D3D version independent routines, located in oxr_d3d.cpp + * + */ + +#if defined(XRT_HAVE_D3D11) || defined(XRT_HAVE_D3D12) || defined(XRT_DOXYGEN) +XrResult +oxr_d3d_get_requirements(struct oxr_logger *log, + struct oxr_system *sys, + LUID *adapter_luid, + D3D_FEATURE_LEVEL *min_feature_level); + +XrResult +oxr_d3d_check_device(struct oxr_logger *log, struct oxr_system *sys, IDXGIDevice *device); +#endif /* *