diff options
| author | lucy96chen <47800040+lucy96chen@users.noreply.github.com> | 2021-09-15 20:22:45 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-15 20:22:45 -0700 |
| commit | b1f04c8544c650de3947955ca68f679535d249aa (patch) | |
| tree | 06fa1ac43fe10f45a9835851dc8e0101aacb03e7 | |
| parent | 2f7b9f5ae8be21c6c1d75ae9caefbc7b3f8986a9 (diff) | |
Allow D3D12Device to use an existing device handle (#1940)
* Added a new field for an existing device handle to IDevice::Desc; Modified D3D12Device::initialize to set the device stored in desc if it already exists instead of creating a new one
* Turned existingDeviceHandle into a struct containing an array of two elements; Updated D3D12Device::initialize to match changes to existingDeviceHandle; Updated comments
* Fixed style error for ExistingDeviceHandles struct
| -rw-r--r-- | slang-gfx.h | 7 | ||||
| -rw-r--r-- | tools/gfx/d3d12/render-d3d12.cpp | 44 |
2 files changed, 33 insertions, 18 deletions
diff --git a/slang-gfx.h b/slang-gfx.h index f4a70d25c..40f0985b9 100644 --- a/slang-gfx.h +++ b/slang-gfx.h @@ -1424,6 +1424,13 @@ public: DeviceType deviceType = DeviceType::Default; // Name to identify the adapter to use const char* adapter = nullptr; + // Device handles (if they already exist) + struct ExistingDeviceHandles + { + // For D3D12, this only contains a single value for the ID3D12Device. + // For Vulkan, the first value is the VkInstance and the second is the VkDevice. + uint64_t values[2] = { 0 }; + } existingDeviceHandles; // Number of required features. int requiredFeatureCount = 0; // Array of required feature names, whose size is `requiredFeatureCount`. diff --git a/tools/gfx/d3d12/render-d3d12.cpp b/tools/gfx/d3d12/render-d3d12.cpp index feacc624d..d0423044f 100644 --- a/tools/gfx/d3d12/render-d3d12.cpp +++ b/tools/gfx/d3d12/render-d3d12.cpp @@ -4287,32 +4287,40 @@ Result D3D12Device::initialize(const Desc& desc) return SLANG_FAIL; } - FlagCombiner combiner; - // TODO: we should probably provide a command-line option - // to override UseDebug of default rather than leave it - // up to each back-end to specify. + if (desc.existingDeviceHandles.values[0] == 0) + { + FlagCombiner combiner; + // TODO: we should probably provide a command-line option + // to override UseDebug of default rather than leave it + // up to each back-end to specify. #if ENABLE_DEBUG_LAYER - combiner.add(DeviceCheckFlag::UseDebug, ChangeType::OnOff); ///< First try debug then non debug + combiner.add(DeviceCheckFlag::UseDebug, ChangeType::OnOff); ///< First try debug then non debug #else - combiner.add(DeviceCheckFlag::UseDebug, ChangeType::Off); ///< Don't bother with debug + combiner.add(DeviceCheckFlag::UseDebug, ChangeType::Off); ///< Don't bother with debug #endif - combiner.add(DeviceCheckFlag::UseHardwareDevice, ChangeType::OnOff); ///< First try hardware, then reference - - const D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0; + combiner.add(DeviceCheckFlag::UseHardwareDevice, ChangeType::OnOff); ///< First try hardware, then reference - const int numCombinations = combiner.getNumCombinations(); - for (int i = 0; i < numCombinations; ++i) - { - if (SLANG_SUCCEEDED(_createDevice(combiner.getCombination(i), UnownedStringSlice(desc.adapter), featureLevel, m_deviceInfo))) + const D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0; + + const int numCombinations = combiner.getNumCombinations(); + for (int i = 0; i < numCombinations; ++i) { - break; + if (SLANG_SUCCEEDED(_createDevice(combiner.getCombination(i), UnownedStringSlice(desc.adapter), featureLevel, m_deviceInfo))) + { + break; + } } - } - if (!m_deviceInfo.m_adapter) + if (!m_deviceInfo.m_adapter) + { + // Couldn't find an adapter + return SLANG_FAIL; + } + } + else { - // Couldn't find an adapter - return SLANG_FAIL; + // Store the existing device handle in desc in m_deviceInfo + m_deviceInfo.m_device = (ID3D12Device*)desc.existingDeviceHandles.values[0]; } // Set the device |
