Skip to content

Commit

Permalink
Rewrite CLOn12 loader in C (#179)
Browse files Browse the repository at this point in the history
* Rewrite CLOn12 loader in C

* Remove additional now-unneeded extern "C" guards for khrEnableTrace

* Fix trace message

Co-authored-by: Ben Ashbaugh <[email protected]>

Co-authored-by: Ben Ashbaugh <[email protected]>
  • Loading branch information
jenatali and bashbaug authored Sep 13, 2022
1 parent 7305673 commit 4aa6837
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 188 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ set (OPENCL_ICD_LOADER_SOURCES
loader/icd_platform.h)

if (WIN32)
enable_language (CXX)
list (APPEND OPENCL_ICD_LOADER_SOURCES
loader/windows/adapter.h
loader/windows/icd_windows.c
Expand All @@ -79,7 +78,7 @@ if (WIN32)
loader/windows/icd_windows_envvars.c
loader/windows/icd_windows_hkr.c
loader/windows/icd_windows_hkr.h
loader/windows/icd_windows_apppackage.cpp
loader/windows/icd_windows_apppackage.c
loader/windows/icd_windows_apppackage.h
loader/windows/OpenCL.def
loader/windows/OpenCL.rc)
Expand Down
8 changes: 1 addition & 7 deletions loader/icd.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,7 @@ struct KHRicdVendorRec
// the global state
extern KHRicdVendor * khrIcdVendors;

#ifdef __cplusplus
extern "C" {
#endif
extern int khrEnableTrace;
#ifdef __cplusplus
}
#endif
extern int khrEnableTrace;

#if defined(CL_ENABLE_LAYERS)
/*
Expand Down
99 changes: 99 additions & 0 deletions loader/windows/icd_windows_apppackage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2017-2019 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/

#include <icd.h>
#include "icd_windows_apppackage.h"

#ifdef OPENCL_ICD_LOADER_DISABLE_OPENCLON12

bool khrIcdOsVendorsEnumerateAppPackage(void)
{
KHR_ICD_TRACE("OpenCLOn12 is disabled\n");
return false;
}

#else

#include <AppModel.h>

bool khrIcdOsVendorsEnumerateAppPackage(void)
{
UINT32 numPackages = 0, bufferLength = 0;
PCWSTR familyName = L"Microsoft.D3DMappingLayers_8wekyb3d8bbwe";
if (ERROR_INSUFFICIENT_BUFFER != GetPackagesByPackageFamily(familyName,
&numPackages, NULL,
&bufferLength, NULL) ||
numPackages == 0 || bufferLength == 0)
{
KHR_ICD_TRACE("Failed to find mapping layers packages by family name\n");
return false;
}

bool ret = false;
WCHAR *buffer = malloc(sizeof(WCHAR) * bufferLength);
PWSTR *packages = malloc(sizeof(PWSTR) * numPackages);
if (!buffer || !packages)
{
KHR_ICD_TRACE("Failed to allocate memory for package names\n");
goto cleanup;
}

if (ERROR_SUCCESS != GetPackagesByPackageFamily(familyName,
&numPackages, packages,
&bufferLength, buffer))
{
KHR_ICD_TRACE("Failed to get mapping layers package full names\n");
goto cleanup;
}

UINT32 pathLength = 0;
WCHAR path[MAX_PATH];
if (ERROR_INSUFFICIENT_BUFFER != GetPackagePathByFullName(packages[0], &pathLength, NULL) ||
pathLength > MAX_PATH ||
ERROR_SUCCESS != GetPackagePathByFullName(packages[0], &pathLength, path))
{
KHR_ICD_TRACE("Failed to get mapping layers package path length\n");
goto cleanup;
}

#if defined(_M_AMD64)
#define PLATFORM_PATH L"x64"
#elif defined(_M_ARM)
#define PLATFORM_PATH L"arm"
#elif defined(_M_ARM64)
#define PLATFORM_PATH L"arm64"
#elif defined(_M_IX86)
#define PLATFORM_PATH L"x86"
#endif

wchar_t dllPath[MAX_PATH];
wcscpy_s(dllPath, MAX_PATH, path);
wcscat_s(dllPath, MAX_PATH, L"\\" PLATFORM_PATH L"\\OpenCLOn12.dll");

char narrowDllPath[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, dllPath, -1, narrowDllPath, MAX_PATH, NULL, NULL);

ret = adapterAdd(narrowDllPath, ZeroLuid);

cleanup:
free(buffer);
free(packages);
return ret;
}

#endif
176 changes: 0 additions & 176 deletions loader/windows/icd_windows_apppackage.cpp

This file was deleted.

3 changes: 0 additions & 3 deletions loader/windows/icd_windows_apppackage.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@
#include <stdbool.h>
#include "icd_windows.h"

#ifdef __cplusplus
extern "C"
#endif
bool khrIcdOsVendorsEnumerateAppPackage(void);

0 comments on commit 4aa6837

Please sign in to comment.