#include <webgpu/webgpu_cpp.h>
#include <memory>
#include <utility>
#include "dawn/common/GPUInfo.h"
#include "dawn/dawn_proc.h"
#include "dawn/native/DawnNative.h"
#if defined(DAWN_ENABLE_BACKEND_VULKAN)
#include "dawn/native/VulkanBackend.h"
#endif
#if defined(DAWN_ENABLE_BACKEND_D3D11)
#include "dawn/native/D3D11Backend.h"
#endif
#if defined(DAWN_ENABLE_BACKEND_D3D12)
#include "dawn/native/D3D12Backend.h"
#endif
#if defined(DAWN_ENABLE_BACKEND_OPENGL)
#include "dawn/native/OpenGLBackend.h"
#endif
#include <gtest/gtest.h>
namespace dawn {
namespace {
class AdapterEnumerationTests : public ::testing::Test { … };
TEST_F(AdapterEnumerationTests, OnlyFallback) { … }
TEST_F(AdapterEnumerationTests, OnlyVulkan) { … }
TEST_F(AdapterEnumerationTests, OnlyD3D11) { … }
#if defined(DAWN_ENABLE_BACKEND_D3D11)
TEST_F(AdapterEnumerationTests, MatchingDXGIAdapterD3D11) {
using Microsoft::WRL::ComPtr;
ComPtr<IDXGIFactory4> dxgiFactory;
HRESULT hr = ::CreateDXGIFactory2(0, IID_PPV_ARGS(&dxgiFactory));
ASSERT_EQ(hr, S_OK);
for (uint32_t adapterIndex = 0;; ++adapterIndex) {
ComPtr<IDXGIAdapter1> dxgiAdapter = nullptr;
if (dxgiFactory->EnumAdapters1(adapterIndex, &dxgiAdapter) == DXGI_ERROR_NOT_FOUND) {
break;
}
native::Instance instance;
DXGI_ADAPTER_DESC adapterDesc;
dxgiAdapter->GetDesc(&adapterDesc);
native::d3d::RequestAdapterOptionsLUID luidOptions = {};
luidOptions.adapterLUID = adapterDesc.AdapterLuid;
wgpu::RequestAdapterOptions adapterOptions = {};
adapterOptions.backendType = wgpu::BackendType::D3D11;
adapterOptions.nextInChain = &luidOptions;
const auto& adapters = instance.EnumerateAdapters(&adapterOptions);
if (adapters.empty()) {
continue;
}
ASSERT_EQ(adapters.size(), 1u);
wgpu::AdapterProperties properties;
adapters[0].GetProperties(&properties);
EXPECT_EQ(properties.backendType, wgpu::BackendType::D3D11);
const auto& adaptersAgain = instance.EnumerateAdapters(&adapterOptions);
ASSERT_EQ(adaptersAgain.size(), 1u);
wgpu::AdapterProperties propertiesAgain;
adaptersAgain[0].GetProperties(&propertiesAgain);
EXPECT_EQ(properties.vendorID, propertiesAgain.vendorID);
EXPECT_STREQ(properties.vendorName, propertiesAgain.vendorName);
EXPECT_STREQ(properties.architecture, propertiesAgain.architecture);
EXPECT_EQ(properties.deviceID, propertiesAgain.deviceID);
EXPECT_STREQ(properties.name, propertiesAgain.name);
EXPECT_STREQ(properties.driverDescription, propertiesAgain.driverDescription);
EXPECT_EQ(properties.adapterType, propertiesAgain.adapterType);
EXPECT_EQ(properties.backendType, propertiesAgain.backendType);
EXPECT_EQ(properties.compatibilityMode, propertiesAgain.compatibilityMode);
}
}
#endif
TEST_F(AdapterEnumerationTests, OnlyD3D12) { … }
#if defined(DAWN_ENABLE_BACKEND_D3D12)
TEST_F(AdapterEnumerationTests, MatchingDXGIAdapterD3D12) {
using Microsoft::WRL::ComPtr;
ComPtr<IDXGIFactory4> dxgiFactory;
HRESULT hr = ::CreateDXGIFactory2(0, IID_PPV_ARGS(&dxgiFactory));
ASSERT_EQ(hr, S_OK);
for (uint32_t adapterIndex = 0;; ++adapterIndex) {
ComPtr<IDXGIAdapter1> dxgiAdapter = nullptr;
if (dxgiFactory->EnumAdapters1(adapterIndex, &dxgiAdapter) == DXGI_ERROR_NOT_FOUND) {
break;
}
native::Instance instance;
DXGI_ADAPTER_DESC adapterDesc;
dxgiAdapter->GetDesc(&adapterDesc);
native::d3d::RequestAdapterOptionsLUID luidOptions = {};
luidOptions.adapterLUID = adapterDesc.AdapterLuid;
wgpu::RequestAdapterOptions adapterOptions = {};
adapterOptions.backendType = wgpu::BackendType::D3D12;
adapterOptions.nextInChain = &luidOptions;
const auto& adapters = instance.EnumerateAdapters(&adapterOptions);
if (adapters.empty()) {
continue;
}
ASSERT_EQ(adapters.size(), 1u);
wgpu::AdapterProperties properties;
adapters[0].GetProperties(&properties);
EXPECT_EQ(properties.backendType, wgpu::BackendType::D3D12);
const auto& adaptersAgain = instance.EnumerateAdapters(&adapterOptions);
ASSERT_EQ(adaptersAgain.size(), 1u);
wgpu::AdapterProperties propertiesAgain;
adaptersAgain[0].GetProperties(&propertiesAgain);
EXPECT_EQ(properties.vendorID, propertiesAgain.vendorID);
EXPECT_STREQ(properties.vendorName, propertiesAgain.vendorName);
EXPECT_STREQ(properties.architecture, propertiesAgain.architecture);
EXPECT_EQ(properties.deviceID, propertiesAgain.deviceID);
EXPECT_STREQ(properties.name, propertiesAgain.name);
EXPECT_STREQ(properties.driverDescription, propertiesAgain.driverDescription);
EXPECT_EQ(properties.adapterType, propertiesAgain.adapterType);
EXPECT_EQ(properties.backendType, propertiesAgain.backendType);
EXPECT_EQ(properties.compatibilityMode, propertiesAgain.compatibilityMode);
}
}
#endif
TEST_F(AdapterEnumerationTests, OnlyMetal) { … }
TEST_F(AdapterEnumerationTests, OneBackendThenTheOther) { … }
}
}