Changing win dev discovery to DXCoreAdapterFactory

Second try - keeping legacy path as fallback

Signed-off-by: Jaroslaw Chodor <jaroslaw.chodor@intel.com>
This commit is contained in:
Jaroslaw Chodor
2021-05-11 17:07:20 +02:00
committed by Compute-Runtime-Automation
parent 86f63bb2ed
commit 3ae75b39fb
18 changed files with 810 additions and 54 deletions

View File

@@ -7,8 +7,10 @@
#include "shared/source/os_interface/windows/wddm/adapter_info.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/os_interface/windows/gdi_interface.h"
#include "shared/source/utilities/stackvec.h"
#include <memory>
@@ -46,4 +48,178 @@ std::wstring queryAdapterDriverStorePath(const Gdi &gdi, D3DKMT_HANDLE adapter)
return std::wstring(std::wstring(queryRegistryInfoValueDesc.OutputString, queryRegistryInfoValueDesc.OutputString + queryRegistryInfoValueDesc.OutputValueSize / sizeof(wchar_t)).c_str());
}
bool canUseAdapterBasedOnDriverDesc(const char *driverDescription) {
return (strstr(driverDescription, "Intel") != nullptr) ||
(strstr(driverDescription, "Citrix") != nullptr) ||
(strstr(driverDescription, "Virtual Render") != nullptr);
}
bool isAllowedDeviceId(uint32_t deviceId) {
if (DebugManager.flags.ForceDeviceId.get() == "unk") {
return true;
}
char *endptr = nullptr;
auto reqDeviceId = strtoul(DebugManager.flags.ForceDeviceId.get().c_str(), &endptr, 16);
return (static_cast<uint32_t>(reqDeviceId) == deviceId);
}
DxCoreAdapterFactory::DxCoreAdapterFactory(DXCoreCreateAdapterFactoryFcn createAdapterFactoryFcn) : createAdapterFactoryFcn(createAdapterFactoryFcn) {
if (nullptr == createAdapterFactoryFcn) {
return;
}
HRESULT hr = createAdapterFactoryFcn(__uuidof(IDXCoreAdapterFactory), (void **)(&adapterFactory));
if (hr != S_OK) {
adapterFactory = nullptr;
}
}
DxCoreAdapterFactory::~DxCoreAdapterFactory() {
destroyCurrentSnapshot();
if (adapterFactory) {
adapterFactory->Release();
adapterFactory = nullptr;
}
}
bool DxCoreAdapterFactory::createSnapshotOfAvailableAdapters() {
if (false == this->isSupported()) {
DEBUG_BREAK_IF(true);
return false;
}
destroyCurrentSnapshot();
GUID attributes[]{DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE};
HRESULT hr = adapterFactory->CreateAdapterList(1, attributes, __uuidof(IDXCoreAdapterList), (void **)(&adaptersInSnapshot));
if ((hr != S_OK) || (adaptersInSnapshot == nullptr)) {
DEBUG_BREAK_IF(true);
destroyCurrentSnapshot();
return false;
}
return true;
}
uint32_t DxCoreAdapterFactory::getNumAdaptersInSnapshot() {
if (nullptr == adaptersInSnapshot) {
return 0U;
}
return adaptersInSnapshot->GetAdapterCount();
}
bool DxCoreAdapterFactory::getAdapterDesc(uint32_t ordinal, AdapterDesc &outAdapter) {
if (ordinal >= getNumAdaptersInSnapshot()) {
DEBUG_BREAK_IF(true);
return false;
}
IDXCoreAdapter *adapter = nullptr;
HRESULT hr = adaptersInSnapshot->GetAdapter(ordinal, __uuidof(IDXCoreAdapter), (void **)&adapter);
if ((hr != S_OK) || (adapter == nullptr)) {
return false;
}
outAdapter = {};
bool isHardware = false;
hr = adapter->GetProperty(DXCoreAdapterProperty::IsHardware, &isHardware);
DEBUG_BREAK_IF(S_OK != hr);
outAdapter.type = isHardware ? AdapterDesc::Type::Hardware : AdapterDesc::Type::NotHardware;
static constexpr uint32_t maxDriverDescriptionStaticSize = 512;
StackVec<char, maxDriverDescriptionStaticSize> driverDescription;
size_t driverDescSize = 0;
hr = adapter->GetPropertySize(DXCoreAdapterProperty::DriverDescription, &driverDescSize);
if (S_OK == hr) {
driverDescription.resize(driverDescSize);
}
hr = adapter->GetProperty(DXCoreAdapterProperty::DriverDescription, driverDescription.size(), driverDescription.data());
if (S_OK != hr) {
adapter->Release();
DEBUG_BREAK_IF(true);
return false;
}
outAdapter.driverDescription = driverDescription.data();
DXCoreHardwareID hwId = {};
adapter->GetProperty(DXCoreAdapterProperty::HardwareID, sizeof(hwId), &hwId);
DEBUG_BREAK_IF(S_OK == hr);
outAdapter.deviceId = hwId.deviceID;
LUID luid = {};
hr = adapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &luid);
if (S_OK != hr) {
adapter->Release();
DEBUG_BREAK_IF(true);
return false;
}
outAdapter.luid = luid;
adapter->Release();
return true;
}
void DxCoreAdapterFactory::destroyCurrentSnapshot() {
if (adaptersInSnapshot) {
adaptersInSnapshot->Release();
adaptersInSnapshot = nullptr;
}
}
DxgiAdapterFactory::DxgiAdapterFactory(CreateDXGIFactoryFcn createAdapterFactoryFcn) : createAdapterFactoryFcn(createAdapterFactoryFcn) {
if (nullptr == createAdapterFactoryFcn) {
return;
}
HRESULT hr = createAdapterFactoryFcn(__uuidof(IDXGIFactory), (void **)(&adapterFactory));
if (hr != S_OK) {
adapterFactory = nullptr;
}
}
bool DxgiAdapterFactory::getAdapterDesc(uint32_t ordinal, AdapterDesc &outAdapter) {
if (ordinal >= getNumAdaptersInSnapshot()) {
DEBUG_BREAK_IF(true);
return false;
}
outAdapter = adaptersInSnapshot[ordinal];
return true;
}
bool DxgiAdapterFactory::createSnapshotOfAvailableAdapters() {
if (false == this->isSupported()) {
DEBUG_BREAK_IF(true);
return false;
}
destroyCurrentSnapshot();
uint32_t ordinal = 0;
IDXGIAdapter1 *adapter = nullptr;
while (adapterFactory->EnumAdapters1(ordinal++, &adapter) != DXGI_ERROR_NOT_FOUND) {
DXGI_ADAPTER_DESC1 adapterDesc = {{0}};
HRESULT hr = adapter->GetDesc1(&adapterDesc);
if (hr != S_OK) {
adapter->Release();
DEBUG_BREAK_IF(true);
continue;
}
adaptersInSnapshot.resize(adaptersInSnapshot.size() + 1);
auto &dstAdapterDesc = *adaptersInSnapshot.rbegin();
dstAdapterDesc.luid = adapterDesc.AdapterLuid;
dstAdapterDesc.deviceId = adapterDesc.DeviceId;
static constexpr auto driverDescMaxChars = sizeof(adapterDesc.Description) / sizeof(adapterDesc.Description[0]);
dstAdapterDesc.driverDescription.reserve(driverDescMaxChars);
for (auto wchar : std::wstring(std::wstring(adapterDesc.Description, adapterDesc.Description + driverDescMaxChars).c_str())) {
dstAdapterDesc.driverDescription.push_back(static_cast<char>(wchar));
}
adapter->Release();
}
return true;
}
} // namespace NEO

View File

@@ -7,7 +7,15 @@
#pragma once
// clang-format off
#include <initguid.h>
#include <dxcore.h>
#include <dxgi.h>
// clang-format on
#include <string>
#include <memory>
#include <vector>
typedef unsigned int D3DKMT_HANDLE;
@@ -17,4 +25,122 @@ class Gdi;
std::wstring queryAdapterDriverStorePath(const Gdi &gdi, D3DKMT_HANDLE adapter);
bool canUseAdapterBasedOnDriverDesc(const char *driverDescription);
bool isAllowedDeviceId(uint32_t deviceId);
class AdapterFactory {
public:
struct AdapterDesc {
enum class Type {
Unknown,
Hardware,
NotHardware
};
Type type = Type::Unknown;
std::string driverDescription;
uint32_t deviceId = {};
LUID luid = {};
};
virtual ~AdapterFactory() = default;
virtual bool createSnapshotOfAvailableAdapters() = 0;
virtual uint32_t getNumAdaptersInSnapshot() = 0;
virtual bool getAdapterDesc(uint32_t ordinal, AdapterDesc &outAdapter) = 0;
virtual bool isSupported() = 0;
};
class DxCoreAdapterFactory : public AdapterFactory {
public:
using DXCoreCreateAdapterFactoryFcn = HRESULT(WINAPI *)(REFIID riid, void **ppFactory);
DxCoreAdapterFactory(DXCoreCreateAdapterFactoryFcn createAdapterFactoryFcn);
~DxCoreAdapterFactory() override;
bool createSnapshotOfAvailableAdapters() override;
bool isSupported() {
return nullptr != adapterFactory;
}
uint32_t getNumAdaptersInSnapshot() override;
bool getAdapterDesc(uint32_t ordinal, AdapterDesc &outAdapter) override;
protected:
void destroyCurrentSnapshot();
DXCoreCreateAdapterFactoryFcn createAdapterFactoryFcn = nullptr;
IDXCoreAdapterFactory *adapterFactory = nullptr;
IDXCoreAdapterList *adaptersInSnapshot = nullptr;
};
class DxgiAdapterFactory : public AdapterFactory {
public:
using CreateDXGIFactoryFcn = HRESULT(WINAPI *)(REFIID riid, void **ppFactory);
DxgiAdapterFactory(CreateDXGIFactoryFcn createAdapterFactoryFcn);
~DxgiAdapterFactory() override {
destroyCurrentSnapshot();
if (adapterFactory) {
adapterFactory->Release();
}
}
bool createSnapshotOfAvailableAdapters() override;
bool isSupported() {
return nullptr != adapterFactory;
}
uint32_t getNumAdaptersInSnapshot() {
return static_cast<uint32_t>(adaptersInSnapshot.size());
}
bool getAdapterDesc(uint32_t ordinal, AdapterDesc &outAdapter) override;
protected:
void destroyCurrentSnapshot() {
adaptersInSnapshot.clear();
}
CreateDXGIFactoryFcn createAdapterFactoryFcn = nullptr;
IDXGIFactory1 *adapterFactory = nullptr;
std::vector<AdapterDesc> adaptersInSnapshot;
};
class WddmAdapterFactory : public AdapterFactory {
public:
WddmAdapterFactory(DxCoreAdapterFactory::DXCoreCreateAdapterFactoryFcn dxCoreCreateAdapterFactoryF,
DxgiAdapterFactory::CreateDXGIFactoryFcn dxgiCreateAdapterFactoryF) {
underlyingFactory = std::make_unique<DxCoreAdapterFactory>(dxCoreCreateAdapterFactoryF);
if (false == underlyingFactory->isSupported()) {
underlyingFactory = std::make_unique<DxgiAdapterFactory>(dxgiCreateAdapterFactoryF);
}
}
~WddmAdapterFactory() override = default;
bool createSnapshotOfAvailableAdapters() override {
return underlyingFactory->createSnapshotOfAvailableAdapters();
}
bool isSupported() override {
return underlyingFactory->isSupported();
}
uint32_t getNumAdaptersInSnapshot() override {
return underlyingFactory->getNumAdaptersInSnapshot();
}
bool getAdapterDesc(uint32_t ordinal, AdapterDesc &outAdapter) override {
return underlyingFactory->getAdapterDesc(ordinal, outAdapter);
}
protected:
std::unique_ptr<AdapterFactory> underlyingFactory;
};
} // namespace NEO

View File

@@ -25,6 +25,7 @@
#include "shared/source/os_interface/windows/os_context_win.h"
#include "shared/source/os_interface/windows/os_environment_win.h"
#include "shared/source/os_interface/windows/os_interface.h"
#include "shared/source/os_interface/windows/wddm/adapter_info.h"
#include "shared/source/os_interface/windows/wddm/um_km_data_translator.h"
#include "shared/source/os_interface/windows/wddm/wddm_interface.h"
#include "shared/source/os_interface/windows/wddm/wddm_residency_logger.h"
@@ -37,14 +38,20 @@
#include "gmm_client_context.h"
#include "gmm_memory.h"
// clang-format off
#include <initguid.h>
#include <dxcore.h>
#include <dxgi.h>
// clang-format on
namespace NEO {
extern Wddm::CreateDXGIFactoryFcn getCreateDxgiFactory();
extern Wddm::DXCoreCreateAdapterFactoryFcn getDXCoreCreateAdapterFactory();
extern Wddm::GetSystemInfoFcn getGetSystemInfo();
extern Wddm::VirtualAllocFcn getVirtualAlloc();
extern Wddm::VirtualFreeFcn getVirtualFree();
Wddm::DXCoreCreateAdapterFactoryFcn Wddm::dXCoreCreateAdapterFactory = getDXCoreCreateAdapterFactory();
Wddm::CreateDXGIFactoryFcn Wddm::createDxgiFactory = getCreateDxgiFactory();
Wddm::GetSystemInfoFcn Wddm::getSystemInfo = getGetSystemInfo();
Wddm::VirtualAllocFcn Wddm::virtualAllocFnc = getVirtualAlloc();
@@ -61,9 +68,6 @@ Wddm::Wddm(std::unique_ptr<HwDeviceId> hwDeviceIdIn, RootDeviceEnvironment &root
this->registryReader.reset(new RegistryReader(false, "System\\CurrentControlSet\\Control\\GraphicsDrivers\\Scheduler"));
kmDafListener = std::unique_ptr<KmDafListener>(new KmDafListener);
temporaryResources = std::make_unique<WddmResidentAllocationsContainer>(this);
if (hwDeviceIdIn && this->rootDeviceEnvironment.getGmmHelper()) {
this->rootDeviceEnvironment.getGmmClientContext()->setHandleAllocator(hwDeviceIdIn->getUmKmDataTranslator()->createGmmHandleAllocator());
}
}
Wddm::~Wddm() {
@@ -107,6 +111,7 @@ bool Wddm::init() {
auto preemptionMode = PreemptionHelper::getDefaultPreemptionMode(*hardwareInfo);
rootDeviceEnvironment.setHwInfo(hardwareInfo.get());
rootDeviceEnvironment.initGmm();
this->rootDeviceEnvironment.getGmmClientContext()->setHandleAllocator(this->hwDeviceId->getUmKmDataTranslator()->createGmmHandleAllocator());
if (WddmVersion::WDDM_2_3 == getWddmVersion()) {
wddmInterface = std::make_unique<WddmInterface23>(*this);
@@ -292,24 +297,19 @@ std::unique_ptr<HwDeviceId> createHwDeviceIdFromAdapterLuid(OsEnvironmentWin &os
}
std::vector<std::unique_ptr<HwDeviceId>> OSInterface::discoverDevices(ExecutionEnvironment &executionEnvironment) {
std::vector<std::unique_ptr<HwDeviceId>> hwDeviceIds;
auto osEnvironment = new OsEnvironmentWin();
auto gdi = osEnvironment->gdi.get();
executionEnvironment.osEnvironment.reset(osEnvironment);
if (!gdi->isInitialized()) {
return hwDeviceIds;
return {};
}
DXGI_ADAPTER_DESC1 OpenAdapterDesc = {{0}};
WddmAdapterFactory adapterFactory{Wddm::dXCoreCreateAdapterFactory, Wddm::createDxgiFactory};
IDXGIFactory1 *pFactory = nullptr;
IDXGIAdapter1 *pAdapter = nullptr;
HRESULT hr = Wddm::createDxgiFactory(__uuidof(IDXGIFactory), (void **)(&pFactory));
if ((hr != S_OK) || (pFactory == nullptr)) {
return hwDeviceIds;
if (false == adapterFactory.isSupported()) {
return {};
}
size_t numRootDevices = 0u;
@@ -317,32 +317,38 @@ std::vector<std::unique_ptr<HwDeviceId>> OSInterface::discoverDevices(ExecutionE
numRootDevices = DebugManager.flags.CreateMultipleRootDevices.get();
}
std::vector<std::unique_ptr<HwDeviceId>> hwDeviceIds;
do {
DWORD iDevNum = 0;
while (pFactory->EnumAdapters1(iDevNum++, &pAdapter) != DXGI_ERROR_NOT_FOUND) {
hr = pAdapter->GetDesc1(&OpenAdapterDesc);
if (hr == S_OK) {
bool createHwDeviceId = false;
// Check for adapters that include either "Intel" or "Citrix" (which may
// be virtualizing one of our adapters) in the description
if ((wcsstr(OpenAdapterDesc.Description, L"Intel") != 0) ||
(wcsstr(OpenAdapterDesc.Description, L"Citrix") != 0) ||
(wcsstr(OpenAdapterDesc.Description, L"Virtual Render") != 0)) {
char deviceId[16];
sprintf_s(deviceId, "%X", OpenAdapterDesc.DeviceId);
createHwDeviceId = (DebugManager.flags.ForceDeviceId.get() == "unk") || (DebugManager.flags.ForceDeviceId.get() == deviceId);
}
if (createHwDeviceId) {
auto hwDeviceId = createHwDeviceIdFromAdapterLuid(*osEnvironment, OpenAdapterDesc.AdapterLuid);
if (hwDeviceId) {
hwDeviceIds.push_back(std::move(hwDeviceId));
}
}
if (false == adapterFactory.createSnapshotOfAvailableAdapters()) {
return hwDeviceIds;
}
auto adapterCount = adapterFactory.getNumAdaptersInSnapshot();
for (uint32_t i = 0; i < adapterCount; ++i) {
AdapterFactory::AdapterDesc adapterDesc;
if (false == adapterFactory.getAdapterDesc(i, adapterDesc)) {
DEBUG_BREAK_IF(true);
continue;
}
// Release all the non-Intel adapters
pAdapter->Release();
pAdapter = nullptr;
if (!hwDeviceIds.empty() && hwDeviceIds.size() == numRootDevices) {
if (adapterDesc.type == AdapterFactory::AdapterDesc::Type::NotHardware) {
continue;
}
if (false == canUseAdapterBasedOnDriverDesc(adapterDesc.driverDescription.c_str())) {
continue;
}
if (false == isAllowedDeviceId(adapterDesc.deviceId)) {
continue;
}
auto hwDeviceId = createHwDeviceIdFromAdapterLuid(*osEnvironment, adapterDesc.luid);
if (hwDeviceId) {
hwDeviceIds.push_back(std::move(hwDeviceId));
}
if (hwDeviceIds.size() == numRootDevices) {
break;
}
}
@@ -351,10 +357,6 @@ std::vector<std::unique_ptr<HwDeviceId>> OSInterface::discoverDevices(ExecutionE
}
} while (hwDeviceIds.size() < numRootDevices);
if (pFactory != nullptr) {
pFactory->Release();
pFactory = nullptr;
}
return hwDeviceIds;
}

View File

@@ -44,6 +44,7 @@ enum class HeapIndex : uint32_t;
class Wddm {
public:
typedef HRESULT(WINAPI *CreateDXGIFactoryFcn)(REFIID riid, void **ppFactory);
typedef HRESULT(WINAPI *DXCoreCreateAdapterFactoryFcn)(REFIID riid, void **ppFactory);
typedef void(WINAPI *GetSystemInfoFcn)(SYSTEM_INFO *pSystemInfo);
typedef BOOL(WINAPI *VirtualFreeFcn)(LPVOID ptr, SIZE_T size, DWORD flags);
typedef LPVOID(WINAPI *VirtualAllocFcn)(LPVOID inPtr, SIZE_T size, DWORD flags, DWORD type);
@@ -158,6 +159,7 @@ class Wddm {
WddmVersion getWddmVersion();
static CreateDXGIFactoryFcn createDxgiFactory;
static DXCoreCreateAdapterFactoryFcn dXCoreCreateAdapterFactory;
uint32_t getRequestedEUCount() const;

View File

@@ -7,11 +7,16 @@
#include "shared/source/os_interface/windows/wddm/wddm.h"
#include <dxcore.h>
#include <dxgi.h>
namespace NEO {
Wddm::CreateDXGIFactoryFcn getCreateDxgiFactory() {
return CreateDXGIFactory;
return DXCoreCreateAdapterFactory;
}
Wddm::DXCoreCreateAdapterFactoryFcn getDXCoreCreateAdapterFactory() {
return DXCoreCreateAdapterFactory;
}
Wddm::GetSystemInfoFcn getGetSystemInfo() {