forked from KhronosGroup/OpenCL-ICD-Loader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
icd_windows_dxgk.c
145 lines (139 loc) · 6.03 KB
/
icd_windows_dxgk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2018 The Khronos Group Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software source and associated documentation files (the "Materials"),
* to deal in the Materials without restriction, including without limitation
* the rights to use, copy, modify, compile, merge, publish, distribute,
* sublicense, and/or sell copies of the Materials, and to permit persons to
* whom the Materials are furnished to do so, subject the following terms and
* conditions:
*
* All modifications to the Materials used to create a binary that is
* distributed to third parties shall be provided to Khronos with an
* unrestricted license to use for the purposes of implementing bug fixes and
* enhancements to the Materials;
*
* If the binary is used as part of an OpenCL(TM) implementation, whether binary
* is distributed together with or separately to that implementation, then
* recipient must become an OpenCL Adopter and follow the published OpenCL
* conformance process for that implementation, details at:
* http://www.khronos.org/conformance/;
*
* The above copyright notice, the OpenCL trademark license, and this permission
* notice shall be included in all copies or substantial portions of the
* Materials.
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN
* THE MATERIALS.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/
#include "icd.h"
#include <windows.h>
#include "icd_windows_dxgk.h"
#include <assert.h>
#ifndef NTSTATUS
typedef LONG NTSTATUS;
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#define STATUS_BUFFER_TOO_SMALL ((NTSTATUS)0xC0000023)
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif
#include "d3dkmthk.h"
bool khrIcdOsVendorsEnumerateDXGK(void)
{
#if defined(DXGKDDI_INTERFACE_VERSION_WDDM2_4) && (DXGKDDI_INTERFACE_VERSION >= DXGKDDI_INTERFACE_VERSION_WDDM2_4)
{
D3DKMT_ADAPTERINFO* pAdapterInfo;
D3DKMT_ENUMADAPTERS2 EnumAdapters;
NTSTATUS Status = STATUS_SUCCESS;
// Get handle to GDI Runtime
HMODULE h = LoadLibrary("gdi32.dll");
char cszLibraryName[1024] = { 0 };
EnumAdapters.NumAdapters = 0;
EnumAdapters.pAdapters = NULL;
PFND3DKMT_ENUMADAPTERS2 pEnumAdapters2 = (PFND3DKMT_ENUMADAPTERS2)GetProcAddress((HMODULE)h, "D3DKMTEnumAdapters2");
if (!pEnumAdapters2)
{
FreeLibrary(h);
return FALSE;
}
Status = pEnumAdapters2(&EnumAdapters);
if (!NT_SUCCESS(Status) && (Status != STATUS_BUFFER_TOO_SMALL))
{
FreeLibrary(h);
return FALSE;
}
pAdapterInfo = (D3DKMT_ADAPTERINFO*)malloc(sizeof(D3DKMT_ADAPTERINFO)*(EnumAdapters.NumAdapters));
EnumAdapters.pAdapters = pAdapterInfo;
Status = pEnumAdapters2(&EnumAdapters);
if (!NT_SUCCESS(Status))
{
FreeLibrary(h);
if (pAdapterInfo) free(pAdapterInfo);
return FALSE;
}
for (UINT AdapterIndex = 0; AdapterIndex < EnumAdapters.NumAdapters; AdapterIndex++)
{
D3DDDI_QUERYREGISTRY_INFO QueryArgs = {0};
D3DDDI_QUERYREGISTRY_INFO* pQueryArgs = &QueryArgs;
D3DDDI_QUERYREGISTRY_INFO* pQueryBuffer = NULL;
QueryArgs.QueryType = D3DDDI_QUERYREGISTRY_ADAPTERKEY;
QueryArgs.QueryFlags.TranslatePath = TRUE;
QueryArgs.ValueType = REG_MULTI_SZ;
#ifdef _WIN64
wcscpy_s(QueryArgs.ValueName, ARRAYSIZE(L"OpenCLDriverName"), L"OpenCLDriverName");
#else
wcscpy_s(QueryArgs.ValueName, ARRAYSIZE(L"OpenCLDriverNameWow"), L"OpenCLDriverNameWow");
#endif
D3DKMT_QUERYADAPTERINFO QueryAdapterInfo = {0};
QueryAdapterInfo.hAdapter = pAdapterInfo[AdapterIndex].hAdapter;
QueryAdapterInfo.Type = KMTQAITYPE_QUERYREGISTRY;
QueryAdapterInfo.pPrivateDriverData = &QueryArgs;
QueryAdapterInfo.PrivateDriverDataSize = sizeof(QueryArgs);
Status = D3DKMTQueryAdapterInfo(&QueryAdapterInfo);
if (!NT_SUCCESS(Status))
{
FreeLibrary(h);
if (pAdapterInfo) free(pAdapterInfo);
return FALSE;
}
if (NT_SUCCESS(Status) && pQueryArgs->Status == D3DDDI_QUERYREGISTRY_STATUS_BUFFER_OVERFLOW)
{
unsigned int QueryBufferSize = sizeof(D3DDDI_QUERYREGISTRY_INFO) + QueryArgs.OutputValueSize;
pQueryBuffer = (D3DDDI_QUERYREGISTRY_INFO*)malloc(QueryBufferSize);
memcpy(pQueryBuffer, &QueryArgs, sizeof(D3DDDI_QUERYREGISTRY_INFO));
QueryAdapterInfo.pPrivateDriverData = pQueryBuffer;
QueryAdapterInfo.PrivateDriverDataSize = QueryBufferSize;
Status = D3DKMTQueryAdapterInfo(&QueryAdapterInfo);
pQueryArgs = pQueryBuffer;
}
if (NT_SUCCESS(Status) && pQueryArgs->Status == D3DDDI_QUERYREGISTRY_STATUS_SUCCESS)
{
wchar_t* pWchar = pQueryArgs->OutputString;
unsigned int i = 0;
memset(cszLibraryName, 0, sizeof(cszLibraryName));
{
while ((*pWchar != L'\0') && (i<1023))
{
cszLibraryName[i] = (char)*pWchar;
i++;
pWchar++;
}
if (i < 1023) khrIcdVendorAdd(cszLibraryName);
}
}
if (pQueryBuffer) free(pQueryBuffer);
}
if (pAdapterInfo) free(pAdapterInfo);
FreeLibrary(h);
return TRUE;
}
#endif
return FALSE;
}