Hash :
6c160cad
        
        Author :
  
        
        Date :
2025-08-04T12:32:36
        
      
CL/Vulkan: cl_khr_external_memory extension (pt.1)
  - Make this extension visible if Vulkan implementation support
    features supportsExternalMemoryFd and/or
    supportsExternalMemoryDmaBuf
  - Implemented APIs clEnqueueAcquireExternalMemObjectsKHR and
    clEnqueueReleaseExternalMemObjectsKHR
  - Updated clCreateBufferWithProperties to handle external memory
    file descriptor.
Bug: angleproject:378017028
Change-Id: Idff08808bc0227b5f98b84c2086b68541665146d
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6785087
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Commit-Queue: Austin Annestrand <a.annestrand@samsung.com>
      
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
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// dispatch.cpp: Implements a function to fetch the ANGLE OpenCL dispatch table.
#include "libOpenCL/dispatch.h"
#include "anglebase/no_destructor.h"
#include "common/debug.h"
#include "common/system_utils.h"
#include <memory>
#ifdef _WIN32
#    include <windows.h>
#endif
namespace cl
{
namespace
{
std::unique_ptr<angle::Library> &EntryPointsLib()
{
    static angle::base::NoDestructor<std::unique_ptr<angle::Library>> sEntryPointsLib;
    return *sEntryPointsLib;
}
IcdDispatch CreateDispatch()
{
    const cl_icd_dispatch *clIcdDispatch = nullptr;
    const char *error                    = nullptr;
    // Try to find ANGLE's GLESv2 library in the consistent way, which might fail
    // if the current library or a link to it is not in ANGLE's binary directory
    EntryPointsLib().reset(
        angle::OpenSharedLibrary(ANGLE_GLESV2_LIBRARY_NAME, angle::SearchType::ModuleDir));
    if (EntryPointsLib() && EntryPointsLib()->getNative() != nullptr)
    {
        EntryPointsLib()->getAs("gCLIcdDispatchTable", &clIcdDispatch);
        if (clIcdDispatch == nullptr)
        {
            INFO() << "Found system's instead of ANGLE's GLESv2 library";
        }
    }
    else
    {
        error = "Not able to find GLESv2 library";
    }
    // If not found try to find ANGLE's GLESv2 library in build path
    if (clIcdDispatch == nullptr)
    {
#ifdef _WIN32
        // On Windows the build path 'ANGLE_GLESV2_LIBRARY_PATH' is provided by the build system
        const char path[] = ANGLE_GLESV2_LIBRARY_PATH "\\" ANGLE_GLESV2_LIBRARY_NAME ".dll";
        // This function allows to load further dependent libraries from the same directory
        HMODULE handle = LoadLibraryExA(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
        if (handle != nullptr)
        {
            clIcdDispatch = reinterpret_cast<const cl_icd_dispatch *>(
                GetProcAddress(handle, "gCLIcdDispatchTable"));
            if (clIcdDispatch == nullptr)
            {
                error = "Error loading CL dispatch table.";
            }
        }
#else
        // On posix-compatible systems this will also search in the rpath, which is the build path
        EntryPointsLib().reset(
            angle::OpenSharedLibrary(ANGLE_GLESV2_LIBRARY_NAME, angle::SearchType::SystemDir));
        if (EntryPointsLib() && EntryPointsLib()->getNative() != nullptr)
        {
            EntryPointsLib()->getAs("gCLIcdDispatchTable", &clIcdDispatch);
            if (clIcdDispatch == nullptr)
            {
                INFO() << "Found system's instead of ANGLE's GLESv2 library";
            }
        }
#endif
    }
    IcdDispatch dispatch;
    if (clIcdDispatch != nullptr)
    {
        static_cast<cl_icd_dispatch &>(dispatch) = *clIcdDispatch;
        dispatch.clIcdGetPlatformIDsKHR          = reinterpret_cast<clIcdGetPlatformIDsKHR_fn>(
            clIcdDispatch->clGetExtensionFunctionAddress("clIcdGetPlatformIDsKHR"));
        // TODO: below extensions will be refactored to not to expose them publicly later
        // http://anglebug.com/378017028
        dispatch.clEnqueueAcquireExternalMemObjectsKHR =
            reinterpret_cast<clEnqueueAcquireExternalMemObjectsKHR_fn>(
                clIcdDispatch->clGetExtensionFunctionAddress(
                    "clEnqueueAcquireExternalMemObjectsKHR"));
        dispatch.clEnqueueReleaseExternalMemObjectsKHR =
            reinterpret_cast<clEnqueueReleaseExternalMemObjectsKHR_fn>(
                clIcdDispatch->clGetExtensionFunctionAddress(
                    "clEnqueueReleaseExternalMemObjectsKHR"));
    }
    else if (error != nullptr)
    {
        ERR() << error;
    }
    return dispatch;
}
}  // anonymous namespace
const IcdDispatch &GetDispatch()
{
    static const IcdDispatch sDispatch(CreateDispatch());
    return sDispatch;
}
}  // namespace cl