|
|
|
|
@@ -13,6 +13,7 @@
|
|
|
|
|
#include "WebGPUBuffer.h"
|
|
|
|
|
#include "WebGPUImage.h"
|
|
|
|
|
#include "WebGPUComputePass.h"
|
|
|
|
|
#include "webgpu.h"
|
|
|
|
|
|
|
|
|
|
#ifndef PULSE_PLAT_WASM
|
|
|
|
|
#include <wgpu.h>
|
|
|
|
|
@@ -26,12 +27,12 @@
|
|
|
|
|
if(status != WGPURequestAdapterStatus_Success)
|
|
|
|
|
{
|
|
|
|
|
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend))
|
|
|
|
|
PulseLogErrorFmt(backend, "(WebGPU) Could not load adapter %.*s", message.length, message.data);
|
|
|
|
|
PulseLogErrorFmt(backend, "(WebGPU) could not load adapter, %.*s", message.length, message.data);
|
|
|
|
|
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
|
|
|
|
|
pulse_device->has_error = true;
|
|
|
|
|
device->has_error = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pulse_device->has_error = false;
|
|
|
|
|
device->has_error = false;
|
|
|
|
|
device->adapter = adapter;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
@@ -66,20 +67,20 @@
|
|
|
|
|
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool WebGPUIsDeviceForbidden(WGPUAdapter adapter, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count)
|
|
|
|
|
{
|
|
|
|
|
if(adapter == PULSE_NULLPTR)
|
|
|
|
|
return true;
|
|
|
|
|
for(uint32_t i = 0; i < forbiden_devices_count; i++)
|
|
|
|
|
{
|
|
|
|
|
if(adapter == ((WebGPUDevice*)forbiden_devices[i]->driver_data)->adapter)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static bool WebGPUIsDeviceForbidden(WGPUAdapter adapter, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count)
|
|
|
|
|
{
|
|
|
|
|
if(adapter == PULSE_NULLPTR)
|
|
|
|
|
return true;
|
|
|
|
|
for(uint32_t i = 0; i < forbiden_devices_count; i++)
|
|
|
|
|
{
|
|
|
|
|
if(adapter == ((WebGPUDevice*)forbiden_devices[i]->driver_data)->adapter)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void WebGPURequestDeviceCallback(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, void* userdata1, void* userdata2)
|
|
|
|
|
{
|
|
|
|
|
WebGPUDevice* pulse_device = (WebGPUDevice*)userdata1;
|
|
|
|
|
@@ -87,7 +88,7 @@ static void WebGPURequestDeviceCallback(WGPURequestDeviceStatus status, WGPUDevi
|
|
|
|
|
if(status != WGPURequestDeviceStatus_Success)
|
|
|
|
|
{
|
|
|
|
|
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend))
|
|
|
|
|
PulseLogErrorFmt(backend, "(WebGPU) Could not create device from %.*s, %.*s", pulse_device->infos.device.length, pulse_device->infos.device.data, message.length, message.data);
|
|
|
|
|
PulseLogErrorFmt(backend, "(WebGPU) could not create device from %.*s, %.*s", pulse_device->infos.device.length, pulse_device->infos.device.data, message.length, message.data);
|
|
|
|
|
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
|
|
|
|
|
pulse_device->has_error = true;
|
|
|
|
|
return;
|
|
|
|
|
@@ -96,6 +97,35 @@ static void WebGPURequestDeviceCallback(WGPURequestDeviceStatus status, WGPUDevi
|
|
|
|
|
pulse_device->device = device;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void WebGPUDeviceLostCallback(const WGPUDevice* _, WGPUDeviceLostReason reason, WGPUStringView message, void* userdata1, void* userdata2)
|
|
|
|
|
{
|
|
|
|
|
WebGPUDevice* device = (WebGPUDevice*)userdata1;
|
|
|
|
|
PulseBackend backend = (PulseBackend)userdata2;
|
|
|
|
|
const char* reasons[] = {
|
|
|
|
|
"of unknown reason",
|
|
|
|
|
"device has been destroyed",
|
|
|
|
|
"instance have been dropped",
|
|
|
|
|
"creation failed",
|
|
|
|
|
};
|
|
|
|
|
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend))
|
|
|
|
|
PulseLogErrorFmt(backend, "(WebGPU) device %.*s lost because %s. %.*s", device->infos.device.length, device->infos.device.data, reasons[reason], message.length, message.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void WebGPUDeviceUncapturedErrorCallback(const WGPUDevice* _, WGPUErrorType type, WGPUStringView message, void* userdata1, void* userdata2)
|
|
|
|
|
{
|
|
|
|
|
WebGPUDevice* device = (WebGPUDevice*)userdata1;
|
|
|
|
|
PulseBackend backend = (PulseBackend)userdata2;
|
|
|
|
|
const char* types[] = {
|
|
|
|
|
"has recieved no error",
|
|
|
|
|
"has recieved a validation error",
|
|
|
|
|
"is out of memory",
|
|
|
|
|
"has recieved an internal error",
|
|
|
|
|
"has recieved an unknown error",
|
|
|
|
|
};
|
|
|
|
|
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend))
|
|
|
|
|
PulseLogErrorFmt(backend, "(WebGPU) device %.*s %s. %.*s", device->infos.device.length, device->infos.device.data, types[type], message.length, message.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count)
|
|
|
|
|
{
|
|
|
|
|
PULSE_CHECK_HANDLE_RETVAL(backend, PULSE_NULLPTR);
|
|
|
|
|
@@ -112,7 +142,7 @@ PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
|
|
|
|
|
WGPURequestAdapterOptions adapter_options = { 0 };
|
|
|
|
|
adapter_options.powerPreference = WGPUPowerPreference_HighPerformance;
|
|
|
|
|
adapter_options.compatibleSurface = PULSE_NULLPTR;
|
|
|
|
|
adapter_options.backendType = WGPUBackendType_WebGPU;
|
|
|
|
|
adapter_options.backendType = WGPUBackendType_Undefined;
|
|
|
|
|
WGPURequestAdapterCallbackInfo adapter_callback = { 0 };
|
|
|
|
|
adapter_callback.callback = WebGPURequestAdapterCallback;
|
|
|
|
|
adapter_callback.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
|
|
|
@@ -122,6 +152,16 @@ PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
|
|
|
|
|
|
|
|
|
|
while(device->adapter == PULSE_NULLPTR && !device->has_error) // Wait for adapter request
|
|
|
|
|
PulseSleep(100);
|
|
|
|
|
|
|
|
|
|
if(device->adapter != PULSE_NULLPTR && WebGPUIsDeviceForbidden(device->adapter, forbiden_devices, forbiden_devices_count))
|
|
|
|
|
{
|
|
|
|
|
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(backend))
|
|
|
|
|
PulseLogError(backend, "(WebGPU) could not find suitable adapter");
|
|
|
|
|
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
|
|
|
|
|
free(pulse_device);
|
|
|
|
|
free(device);
|
|
|
|
|
return PULSE_NULL_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
WGPUInstanceEnumerateAdapterOptions adapter_options = { 0 };
|
|
|
|
|
adapter_options.nextInChain = PULSE_NULLPTR;
|
|
|
|
|
@@ -156,8 +196,19 @@ PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
|
|
|
|
|
wgpuAdapterGetLimits(device->adapter, &device->limits);
|
|
|
|
|
wgpuAdapterGetInfo(device->adapter, &device->infos);
|
|
|
|
|
|
|
|
|
|
WGPUDeviceLostCallbackInfo lost_callback = { 0 };
|
|
|
|
|
lost_callback.callback = WebGPUDeviceLostCallback;
|
|
|
|
|
lost_callback.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
|
|
|
lost_callback.userdata1 = device;
|
|
|
|
|
lost_callback.userdata2 = backend;
|
|
|
|
|
WGPUUncapturedErrorCallbackInfo uncaptured_callback = { 0 };
|
|
|
|
|
uncaptured_callback.callback = WebGPUDeviceUncapturedErrorCallback;
|
|
|
|
|
uncaptured_callback.userdata1 = device;
|
|
|
|
|
uncaptured_callback.userdata2 = backend;
|
|
|
|
|
WGPUDeviceDescriptor descriptor = { 0 };
|
|
|
|
|
descriptor.requiredLimits = &device->limits;
|
|
|
|
|
descriptor.deviceLostCallbackInfo = lost_callback;
|
|
|
|
|
descriptor.uncapturedErrorCallbackInfo = uncaptured_callback;
|
|
|
|
|
WGPURequestDeviceCallbackInfo device_callback = { 0 };
|
|
|
|
|
device_callback.callback = WebGPURequestDeviceCallback;
|
|
|
|
|
device_callback.mode = WGPUCallbackMode_AllowProcessEvents;
|
|
|
|
|
@@ -175,6 +226,8 @@ PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
|
|
|
|
|
return PULSE_NULL_HANDLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device->queue = wgpuDeviceGetQueue(device->device);
|
|
|
|
|
|
|
|
|
|
pulse_device->driver_data = device;
|
|
|
|
|
pulse_device->backend = backend;
|
|
|
|
|
PULSE_LOAD_DRIVER_DEVICE(WebGPU);
|
|
|
|
|
@@ -192,7 +245,7 @@ PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
|
|
|
|
|
"OpenGL",
|
|
|
|
|
"OpenGLES",
|
|
|
|
|
};
|
|
|
|
|
PulseLogInfoFmt(backend, "(WebGPU) created device from %.*s using backend %s", device->infos.device.length, device->infos.device.data, backends[device->infos.backendType]);
|
|
|
|
|
PulseLogInfoFmt(backend, "(WebGPU) created device from %.*s using %s backend", device->infos.device.length, device->infos.device.data, backends[device->infos.backendType]);
|
|
|
|
|
}
|
|
|
|
|
return pulse_device;
|
|
|
|
|
}
|
|
|
|
|
@@ -202,6 +255,12 @@ void WebGPUDestroyDevice(PulseDevice device)
|
|
|
|
|
WebGPUDevice* webgpu_device = WEBGPU_RETRIEVE_DRIVER_DATA_AS(device, WebGPUDevice*);
|
|
|
|
|
if(webgpu_device == PULSE_NULLPTR || webgpu_device->device == PULSE_NULLPTR)
|
|
|
|
|
return;
|
|
|
|
|
wgpuQueueRelease(webgpu_device->queue);
|
|
|
|
|
wgpuDeviceRelease(webgpu_device->device);
|
|
|
|
|
wgpuAdapterRelease(webgpu_device->adapter);
|
|
|
|
|
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(device->backend))
|
|
|
|
|
PulseLogInfoFmt(device->backend, "(WebGPU) destroyed device created from %.*s", webgpu_device->infos.device.length, webgpu_device->infos.device.data);
|
|
|
|
|
wgpuAdapterInfoFreeMembers(webgpu_device->infos);
|
|
|
|
|
free(webgpu_device);
|
|
|
|
|
free(device);
|
|
|
|
|
}
|
|
|
|
|
|