From d9ec84b327fd138e375fe1fdf8ae613e95252d0a Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 4 Dec 2021 21:18:06 +0000 Subject: [PATCH] gpu: implement C wrapper for Dawn Instance.discoverAdapters Signed-off-by: Stephen Gutekanst --- gpu/src/dawn/dawn_native_mach.cpp | 39 +++++++++++++++++++++++++++---- gpu/src/dawn/dawn_native_mach.h | 14 +++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/gpu/src/dawn/dawn_native_mach.cpp b/gpu/src/dawn/dawn_native_mach.cpp index 5705cb9f..4dd7292a 100644 --- a/gpu/src/dawn/dawn_native_mach.cpp +++ b/gpu/src/dawn/dawn_native_mach.cpp @@ -1,7 +1,9 @@ #include #include #include "utils/BackendBinding.h" - +#if defined(DAWN_ENABLE_BACKEND_OPENGL) +#include +#endif #include "dawn_native_mach.h" #ifdef __cplusplus @@ -133,10 +135,37 @@ MACH_EXPORT void machDawnNativeInstance_discoverDefaultAdapters(MachDawnNativeIn dawn_native::Instance* self = reinterpret_cast(instance); self->DiscoverDefaultAdapters(); } -// TODO(dawn-native-mach): -// // Adds adapters that can be discovered with the options provided (like a getProcAddress). -// // The backend is chosen based on the type of the options used. Returns true on success. -// bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options); +MACH_EXPORT bool machDawnNativeInstance_discoverAdapters(MachDawnNativeInstance instance, WGPUBackendType backendType, const void* options) { + dawn_native::Instance* self = reinterpret_cast(instance); + switch (backendType) { + case WGPUBackendType_OpenGL: + #if defined(DAWN_ENABLE_BACKEND_DESKTOP_GL) + { + auto opt = reinterpret_cast(options); + dawn_native::opengl::AdapterDiscoveryOptions adapterOptions = dawn_native::opengl::AdapterDiscoveryOptions(); + adapterOptions.getProc = opt->getProc; + return self->DiscoverAdapters(&adapterOptions); + } + #endif + case WGPUBackendType_OpenGLES: + #if defined(DAWN_ENABLE_BACKEND_OPENGLES) + { + auto opt = reinterpret_cast(options); + dawn_native::opengl::AdapterDiscoveryOptionsES adapterOptions; + adapterOptions.getProc = opt->getProc; + return self->DiscoverAdapters(&adapterOptions); + } + #endif + case WGPUBackendType_WebGPU: + case WGPUBackendType_D3D11: + case WGPUBackendType_D3D12: + case WGPUBackendType_Metal: + case WGPUBackendType_Null: + case WGPUBackendType_Vulkan: + case WGPUBackendType_Force32: + return false; + } +} MACH_EXPORT MachDawnNativeAdapters machDawnNativeInstance_getAdapters(MachDawnNativeInstance instance) { dawn_native::Instance* self = reinterpret_cast(instance); auto cppAdapters = self->GetAdapters(); diff --git a/gpu/src/dawn/dawn_native_mach.h b/gpu/src/dawn/dawn_native_mach.h index 5f455b10..ffcc900e 100644 --- a/gpu/src/dawn/dawn_native_mach.h +++ b/gpu/src/dawn/dawn_native_mach.h @@ -81,11 +81,25 @@ typedef struct MachDawnNativeInstanceImpl* MachDawnNativeInstance; MACH_EXPORT MachDawnNativeInstance machDawnNativeInstance_init(void); MACH_EXPORT void machDawnNativeInstance_deinit(MachDawnNativeInstance); MACH_EXPORT void machDawnNativeInstance_discoverDefaultAdapters(MachDawnNativeInstance); + +// Adds adapters that can be discovered with the options provided (like a getProcAddress). +// You must specify a valid backend type corresponding to the type of MachDawnNativeAdapterDiscoveryOptions_ +// struct pointer you pass as options. +// Returns true on success. +MACH_EXPORT bool machDawnNativeInstance_discoverAdapters(MachDawnNativeInstance instance, WGPUBackendType backendType, const void* options); + MACH_EXPORT MachDawnNativeAdapters machDawnNativeInstance_getAdapters(MachDawnNativeInstance instance); // Backend-agnostic API for dawn_native MACH_EXPORT const DawnProcTable* machDawnNativeGetProcs(); +// Backend-specific options which can be passed to discoverAdapters +typedef struct MachDawnNativeAdapterDiscoveryOptions_OpenGL { + void* (*getProc)(const char*); +} MachDawnNativeAdapterDiscoveryOptions_OpenGL; +typedef struct MachDawnNativeAdapterDiscoveryOptions_OpenGLES { + void* (*getProc)(const char*); +} MachDawnNativeAdapterDiscoveryOptions_OpenGLES; // utils #include