mirror of
https://github.com/Kbz-8/Pulse.git
synced 2026-01-11 15:33:34 +00:00
working on WebGPU command lists
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
// This file is part of "Pulse"
|
||||
// For conditions of distribution and use, see copyright notice in LICENSE
|
||||
|
||||
#include "Pulse.h"
|
||||
#include "Vulkan.h"
|
||||
#include "VulkanCommandList.h"
|
||||
#include "VulkanCommandPool.h"
|
||||
@@ -98,8 +97,6 @@ PulseCommandList VulkanRequestCommandList(PulseDevice device, PulseCommandListUs
|
||||
|
||||
bool VulkanSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFence fence)
|
||||
{
|
||||
PULSE_CHECK_HANDLE_RETVAL(cmd, false);
|
||||
|
||||
VulkanDevice* vulkan_device = VULKAN_RETRIEVE_DRIVER_DATA_AS(device, VulkanDevice*);
|
||||
VulkanCommandList* vulkan_cmd = VULKAN_RETRIEVE_DRIVER_DATA_AS(cmd, VulkanCommandList*);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ PulseBackendFlags WebGPUCheckSupport(PulseBackendFlags candidates, PulseShaderFo
|
||||
{
|
||||
if(candidates != PULSE_BACKEND_ANY && (candidates & PULSE_BACKEND_WEBGPU) == 0)
|
||||
return PULSE_BACKEND_INVALID;
|
||||
if((shader_formats_used & PULSE_SHADER_FORMAT_SPIRV_BIT) == 0 && (shader_formats_used & PULSE_SHADER_FORMAT_WGSL_BIT) == 0)
|
||||
if((shader_formats_used & PULSE_SHADER_FORMAT_WGSL_BIT) == 0)
|
||||
return PULSE_BACKEND_INVALID;
|
||||
|
||||
WGPUInstance instance = wgpuCreateInstance(PULSE_NULLPTR);
|
||||
@@ -48,6 +48,6 @@ PulseBackendHandler WebGPUDriver = {
|
||||
.PFN_UnloadBackend = WebGPUUnloadBackend,
|
||||
.PFN_CreateDevice = WebGPUCreateDevice,
|
||||
.backend = PULSE_BACKEND_WEBGPU,
|
||||
.supported_shader_formats = PULSE_SHADER_FORMAT_SPIRV_BIT | PULSE_SHADER_FORMAT_WGSL_BIT,
|
||||
.supported_shader_formats = PULSE_SHADER_FORMAT_WGSL_BIT,
|
||||
.driver_data = PULSE_NULLPTR
|
||||
};
|
||||
|
||||
@@ -4,15 +4,62 @@
|
||||
|
||||
#include <Pulse.h>
|
||||
#include "WebGPU.h"
|
||||
#include "WebGPUDevice.h"
|
||||
#include "WebGPUCommandList.h"
|
||||
#include "WebGPUComputePass.h"
|
||||
#include "../../PulseInternal.h"
|
||||
|
||||
PulseCommandList WebGPURequestCommandList(PulseDevice device, PulseCommandListUsage usage)
|
||||
{
|
||||
PULSE_CHECK_HANDLE_RETVAL(device, PULSE_NULL_HANDLE);
|
||||
|
||||
PULSE_UNUSED(usage);
|
||||
|
||||
WebGPUDevice* webgpu_device = WEBGPU_RETRIEVE_DRIVER_DATA_AS(device, WebGPUDevice*);
|
||||
|
||||
PulseCommandList cmd = (PulseCommandList)calloc(1, sizeof(PulseCommandListHandler));
|
||||
PULSE_CHECK_ALLOCATION_RETVAL(cmd, PULSE_NULL_HANDLE);
|
||||
|
||||
WebGPUCommandList* webgpu_cmd = (WebGPUCommandList*)calloc(1, sizeof(WebGPUCommandList));
|
||||
PULSE_CHECK_ALLOCATION_RETVAL(webgpu_cmd, PULSE_NULL_HANDLE);
|
||||
|
||||
WGPUCommandEncoderDescriptor encoder_descriptor = { 0 };
|
||||
webgpu_cmd->encoder = wgpuDeviceCreateCommandEncoder(webgpu_device->device, &encoder_descriptor);
|
||||
|
||||
cmd->usage = usage;
|
||||
cmd->device = device;
|
||||
cmd->driver_data = webgpu_cmd;
|
||||
cmd->thread_id = PulseGetThreadID();
|
||||
|
||||
cmd->pass = WebGPUCreateComputePass(device, cmd);
|
||||
cmd->state = PULSE_COMMAND_LIST_STATE_RECORDING;
|
||||
cmd->is_available = false;
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
bool WebGPUSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFence fence)
|
||||
{
|
||||
WebGPUDevice* webgpu_device = WEBGPU_RETRIEVE_DRIVER_DATA_AS(device, WebGPUDevice*);
|
||||
WebGPUCommandList* webgpu_cmd = WEBGPU_RETRIEVE_DRIVER_DATA_AS(cmd, WebGPUCommandList*);
|
||||
|
||||
WGPUCommandBufferDescriptor command_buffer_descriptor = { 0 };
|
||||
WGPUCommandBuffer command_buffer = wgpuCommandEncoderFinish(webgpu_cmd->encoder, &command_buffer_descriptor);
|
||||
|
||||
wgpuQueueSubmit(webgpu_device->queue, 1, &command_buffer);
|
||||
|
||||
wgpuCommandBufferRelease(command_buffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
void WebGPUReleaseCommandList(PulseDevice device, PulseCommandList cmd)
|
||||
{
|
||||
PULSE_CHECK_HANDLE(device);
|
||||
|
||||
WebGPUCommandList* webgpu_cmd = WEBGPU_RETRIEVE_DRIVER_DATA_AS(cmd, WebGPUCommandList*);
|
||||
|
||||
wgpuCommandEncoderRelease(webgpu_cmd->encoder);
|
||||
|
||||
free(webgpu_cmd);
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
typedef struct WebGPUCommandList
|
||||
{
|
||||
WGPUCommandEncoder encoder;
|
||||
} WebGPUCommandList;
|
||||
|
||||
PulseCommandList WebGPURequestCommandList(PulseDevice device, PulseCommandListUsage usage);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ typedef struct WebGPUDevice
|
||||
WGPULimits limits;
|
||||
WGPUAdapter adapter;
|
||||
WGPUDevice device;
|
||||
WGPUQueue queue;
|
||||
|
||||
bool has_error;
|
||||
} WebGPUDevice;
|
||||
|
||||
Reference in New Issue
Block a user