Skip to content

Commit 8f51613

Browse files
authored
dynamically load functions for OpenCLon12 (#197)
* try without including AppModel.h * dynamically load the OpenCLOn12 functions * update README
1 parent f573575 commit 8f51613

3 files changed

Lines changed: 47 additions & 29 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ set_target_properties (OpenCL PROPERTIES VERSION "1.2" SOVERSION "1")
117117
if (WIN32)
118118
target_link_libraries (OpenCL PRIVATE cfgmgr32.lib runtimeobject.lib)
119119

120-
option (OPENCL_ICD_LOADER_DISABLE_OPENCLON12 "Disable support for OpenCLOn12. Support for OpenCLOn12 should only be disabled when building an import lib to link with, and must be enabled when building an ICD loader for distribution!" OFF)
121-
122120
# Generate a DLL without a "lib" prefix for mingw.
123121
if (MINGW OR MSYS OR CYGWIN)
124122
set_target_properties(OpenCL PROPERTIES PREFIX "")
@@ -151,7 +149,6 @@ target_compile_definitions (OpenCL
151149
PRIVATE
152150
CL_TARGET_OPENCL_VERSION=300
153151
$<$<BOOL:${ENABLE_OPENCL_LAYERS}>:CL_ENABLE_LAYERS>
154-
$<$<BOOL:${OPENCL_ICD_LOADER_DISABLE_OPENCLON12}>:OPENCL_ICD_LOADER_DISABLE_OPENCLON12>
155152
)
156153

157154
target_include_directories (OpenCL

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ The OpenCL ICD Loader requires:
3535
- It is recommended to install the headers via CMake, however a convenience shorthand is provided. Providing `OPENCL_ICD_LOADER_HEADERS_DIR` to CMake, one may specify the location of OpenCL Headers. By default, the OpenCL ICD Loader will look for OpenCL Headers in the inc directory.
3636
- The OpenCL ICD Loader uses CMake for its build system.
3737
If CMake is not provided by your build system or OS package manager, please consult the [CMake website](https://cmake.org).
38-
- The Windows OpenCL ICD Loader requires the Windows SDK to check for and enumerate the OpenCLOn12 ICD.
39-
An OpenCL ICD Loader can be built without a dependency on the Windows SDK using the CMake variable `OPENCL_ICD_LOADER_DISABLE_OPENCLON12`.
40-
This variable should only be used when building an import lib to link with, and must be enabled when building an OpenCL ICD Loader for distribution!
4138

4239
### Example Build
4340

loader/windows/icd_windows_apppackage.c

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2019 The Khronos Group Inc.
2+
* Copyright (c) 2017-2022 The Khronos Group Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,53 +19,78 @@
1919
#include <icd.h>
2020
#include "icd_windows_apppackage.h"
2121

22-
#ifdef OPENCL_ICD_LOADER_DISABLE_OPENCLON12
22+
typedef _Check_return_ LONG(WINAPI *PFN_GetPackagesByPackageFamily)(
23+
_In_ PCWSTR packageFamilyName,
24+
_Inout_ UINT32* count,
25+
_Out_writes_opt_(*count) PWSTR* packageFullNames,
26+
_Inout_ UINT32* bufferLength,
27+
_Out_writes_opt_(*bufferLength) WCHAR* buffer
28+
);
29+
30+
typedef LONG (WINAPI *PFN_GetPackagePathByFullName)(
31+
_In_ PCWSTR packageFullName,
32+
_Inout_ UINT32* pathLength,
33+
_Out_writes_opt_(*pathLength) PWSTR path
34+
);
2335

2436
bool khrIcdOsVendorsEnumerateAppPackage(void)
2537
{
26-
KHR_ICD_TRACE("OpenCLOn12 is disabled\n");
27-
return false;
28-
}
38+
bool ret = false;
39+
WCHAR *buffer = NULL;
40+
PWSTR *packages = NULL;
2941

30-
#else
42+
HMODULE h = LoadLibrary("kernel32.dll");
43+
if (h == NULL)
44+
return ret;
3145

32-
#include <AppModel.h>
46+
PFN_GetPackagesByPackageFamily pGetPackagesByPackageFamily =
47+
(PFN_GetPackagesByPackageFamily)GetProcAddress(h, "GetPackagesByPackageFamily");
48+
if (!pGetPackagesByPackageFamily)
49+
{
50+
KHR_ICD_TRACE("GetProcAddress failed for GetPackagesByPackageFamily\n");
51+
goto cleanup;
52+
}
53+
54+
PFN_GetPackagePathByFullName pGetPackagePathByFullName =
55+
(PFN_GetPackagePathByFullName)GetProcAddress(h, "GetPackagePathByFullName");
56+
if (!pGetPackagePathByFullName)
57+
{
58+
KHR_ICD_TRACE("GetProcAddress failed for GetPackagePathByFullName\n");
59+
goto cleanup;
60+
}
3361

34-
bool khrIcdOsVendorsEnumerateAppPackage(void)
35-
{
3662
UINT32 numPackages = 0, bufferLength = 0;
3763
PCWSTR familyName = L"Microsoft.D3DMappingLayers_8wekyb3d8bbwe";
38-
if (ERROR_INSUFFICIENT_BUFFER != GetPackagesByPackageFamily(familyName,
39-
&numPackages, NULL,
40-
&bufferLength, NULL) ||
64+
if (ERROR_INSUFFICIENT_BUFFER != pGetPackagesByPackageFamily(familyName,
65+
&numPackages, NULL,
66+
&bufferLength, NULL) ||
4167
numPackages == 0 || bufferLength == 0)
4268
{
4369
KHR_ICD_TRACE("Failed to find mapping layers packages by family name\n");
44-
return false;
70+
goto cleanup;
4571
}
4672

47-
bool ret = false;
48-
WCHAR *buffer = malloc(sizeof(WCHAR) * bufferLength);
49-
PWSTR *packages = malloc(sizeof(PWSTR) * numPackages);
73+
buffer = malloc(sizeof(WCHAR) * bufferLength);
74+
packages = malloc(sizeof(PWSTR) * numPackages);
5075
if (!buffer || !packages)
5176
{
5277
KHR_ICD_TRACE("Failed to allocate memory for package names\n");
5378
goto cleanup;
5479
}
5580

56-
if (ERROR_SUCCESS != GetPackagesByPackageFamily(familyName,
57-
&numPackages, packages,
58-
&bufferLength, buffer))
81+
if (ERROR_SUCCESS != pGetPackagesByPackageFamily(familyName,
82+
&numPackages, packages,
83+
&bufferLength, buffer))
5984
{
6085
KHR_ICD_TRACE("Failed to get mapping layers package full names\n");
6186
goto cleanup;
6287
}
6388

6489
UINT32 pathLength = 0;
6590
WCHAR path[MAX_PATH];
66-
if (ERROR_INSUFFICIENT_BUFFER != GetPackagePathByFullName(packages[0], &pathLength, NULL) ||
91+
if (ERROR_INSUFFICIENT_BUFFER != pGetPackagePathByFullName(packages[0], &pathLength, NULL) ||
6792
pathLength > MAX_PATH ||
68-
ERROR_SUCCESS != GetPackagePathByFullName(packages[0], &pathLength, path))
93+
ERROR_SUCCESS != pGetPackagePathByFullName(packages[0], &pathLength, path))
6994
{
7095
KHR_ICD_TRACE("Failed to get mapping layers package path length\n");
7196
goto cleanup;
@@ -91,9 +116,8 @@ bool khrIcdOsVendorsEnumerateAppPackage(void)
91116
ret = adapterAdd(narrowDllPath, ZeroLuid);
92117

93118
cleanup:
119+
FreeLibrary(h);
94120
free(buffer);
95121
free(packages);
96122
return ret;
97123
}
98-
99-
#endif

0 commit comments

Comments
 (0)