working on WebGPU support

This commit is contained in:
2025-02-23 17:12:27 +01:00
parent 3a598ab887
commit 443eb6b810
34 changed files with 859 additions and 74 deletions

View File

@@ -29,6 +29,7 @@ PulseBackendFlags VulkanCheckSupport(PulseBackendFlags candidates, PulseShaderFo
VulkanInstance instance;
if(!VulkanInitInstance(PULSE_NULL_HANDLE, &instance, PULSE_NO_DEBUG)) // Instance creation will fail if Vulkan is not supported
{
VulkanLoaderShutdown();
PulseGetLastErrorType(); // Clearing out the errors set by the failed instance creation
return PULSE_BACKEND_INVALID;
}
@@ -44,7 +45,10 @@ bool VulkanLoadBackend(PulseBackend backend, PulseDebugLevel debug_level)
VulkanDriverData* driver_data = (VulkanDriverData*)calloc(1, sizeof(VulkanDriverData));
PULSE_CHECK_ALLOCATION_RETVAL(driver_data, false);
if(!VulkanInitInstance(backend, &driver_data->instance, debug_level))
{
free(driver_data);
return false;
}
VulkanDriver.driver_data = driver_data;
return true;
}
@@ -53,6 +57,7 @@ void VulkanUnloadBackend(PulseBackend backend)
{
VulkanDestroyInstance(&VULKAN_RETRIEVE_DRIVER_DATA_AS(backend, VulkanDriverData*)->instance);
VulkanLoaderShutdown();
free(backend->driver_data);
}
const char* VulkanVerbaliseResult(VkResult res)

View File

@@ -46,6 +46,7 @@ static int32_t VulkanScorePhysicalDevice(VulkanInstance* instance, VkPhysicalDev
break;
}
}
free(props);
if(are_there_required_device_extensions == false)
return -1;
@@ -76,7 +77,6 @@ static bool VulkanIsDeviceForbidden(VkPhysicalDevice device, PulseDevice* forbid
{
if(device == VK_NULL_HANDLE)
return true;
#pragma omp parallel for
for(uint32_t i = 0; i < forbiden_devices_count; i++)
{
if(device == ((VulkanDevice*)forbiden_devices[i]->driver_data)->physical)
@@ -97,7 +97,6 @@ static VkPhysicalDevice VulkanPickPhysicalDevice(VulkanInstance* instance, Pulse
PULSE_CHECK_ALLOCATION_RETVAL(devices, VK_NULL_HANDLE);
instance->vkEnumeratePhysicalDevices(instance->instance, &device_count, devices);
#pragma omp parallel for
for(uint32_t i = 0; i < device_count; i++)
{
if(VulkanIsDeviceForbidden(devices[i], forbiden_devices, forbiden_devices_count))
@@ -120,12 +119,12 @@ PulseDevice VulkanCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
PULSE_CHECK_ALLOCATION_RETVAL(pulse_device, PULSE_NULL_HANDLE);
VulkanDevice* device = (VulkanDevice*)calloc(1, sizeof(VulkanDevice));
PULSE_CHECK_ALLOCATION_RETVAL(device, PULSE_NULLPTR);
PULSE_CHECK_ALLOCATION_RETVAL(device, PULSE_NULL_HANDLE);
VulkanInstance* instance = &VULKAN_RETRIEVE_DRIVER_DATA_AS(backend, VulkanDriverData*)->instance;
device->physical = VulkanPickPhysicalDevice(instance, forbiden_devices, forbiden_devices_count);
PULSE_CHECK_HANDLE_RETVAL(device->physical, PULSE_NULLPTR);
PULSE_CHECK_HANDLE_RETVAL(device->physical, PULSE_NULL_HANDLE);
const float queue_priority = 1.0f;
@@ -139,6 +138,8 @@ PulseDevice VulkanCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
if(!VulkanPrepareDeviceQueue(instance, device, i))
{
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
free(device);
free(pulse_device);
return PULSE_NULLPTR;
}
}
@@ -185,6 +186,8 @@ PulseDevice VulkanCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend))
PulseLogInfoFmt(backend, "(Vulkan) Could not load device functions from %s", device->properties.deviceName);
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
free(device);
free(pulse_device);
return PULSE_NULLPTR;
}
@@ -193,6 +196,8 @@ PulseDevice VulkanCreateDevice(PulseBackend backend, PulseDevice* forbiden_devic
if(!VulkanRetrieveDeviceQueue(device, (VulkanQueueType)i))
{
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
free(device);
free(pulse_device);
return PULSE_NULLPTR;
}
}

View File

@@ -2,7 +2,7 @@
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include "Pulse.h"
#include <Pulse.h>
#include "Vulkan.h"
#include "VulkanImage.h"
#include "VulkanBuffer.h"

53
Sources/Backends/WebGPU/WebGPU.c git.filemode.normal_file
View File

@@ -0,0 +1,53 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "../../PulseInternal.h"
#include "WebGPU.h"
#include "WebGPUDevice.h"
PulseBackendFlags WebGPUCheckSupport(PulseBackendFlags candidates, PulseShaderFormatsFlags shader_formats_used)
{
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)
return PULSE_BACKEND_INVALID;
WGPUInstance instance = wgpuCreateInstance(PULSE_NULLPTR);
if(instance == PULSE_NULLPTR)
return PULSE_BACKEND_INVALID;
wgpuInstanceRelease(instance);
return PULSE_BACKEND_WEBGPU;
}
bool WebGPULoadBackend(PulseBackend backend, PulseDebugLevel debug_level)
{
WebGPUDriverData* driver_data = (WebGPUDriverData*)calloc(1, sizeof(WebGPUDriverData));
PULSE_CHECK_ALLOCATION_RETVAL(driver_data, false);
driver_data->instance = wgpuCreateInstance(PULSE_NULLPTR);
if(driver_data->instance == PULSE_NULLPTR)
{
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
free(driver_data);
return false;
}
WebGPUDriver.driver_data = driver_data;
return true;
}
void WebGPUUnloadBackend(PulseBackend backend)
{
wgpuInstanceRelease(WEBGPU_RETRIEVE_DRIVER_DATA_AS(backend, WebGPUDriverData*)->instance);
free(backend->driver_data);
}
PulseBackendHandler WebGPUDriver = {
.PFN_LoadBackend = WebGPULoadBackend,
.PFN_UnloadBackend = WebGPUUnloadBackend,
.PFN_CreateDevice = WebGPUCreateDevice,
.backend = PULSE_BACKEND_WEBGPU,
.supported_shader_formats = PULSE_SHADER_FORMAT_SPIRV_BIT | PULSE_SHADER_FORMAT_WGSL_BIT,
.driver_data = PULSE_NULLPTR
};

26
Sources/Backends/WebGPU/WebGPU.h git.filemode.normal_file
View File

@@ -0,0 +1,26 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_H_
#define PULSE_WEBGPU_H_
#include <webgpu/webgpu.h>
#define WEBGPU_RETRIEVE_DRIVER_DATA_AS(handle, cast) ((cast)handle->driver_data)
typedef struct WebGPUDriverData
{
WGPUInstance instance;
} WebGPUDriverData;
PulseBackendFlags WebGPUCheckSupport(PulseBackendFlags candidates, PulseShaderFormatsFlags shader_formats_used); // Return PULSE_BACKEND_WEBGPU in case of success and PULSE_BACKEND_INVALID otherwise
#endif // PULSE_WEBGPU_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

30
Sources/Backends/WebGPU/WebGPUBuffer.c git.filemode.normal_file
View File

@@ -0,0 +1,30 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "WebGPU.h"
PulseBuffer WebGPUCreateBuffer(PulseDevice device, const PulseBufferCreateInfo* create_infos)
{
}
bool WebGPUMapBuffer(PulseBuffer buffer, void** data)
{
}
void WebGPUUnmapBuffer(PulseBuffer buffer)
{
}
bool WebGPUCopyBufferToBuffer(PulseCommandList cmd, const PulseBufferRegion* src, const PulseBufferRegion* dst)
{
}
bool WebGPUCopyBufferToImage(PulseCommandList cmd, const PulseBufferRegion* src, const PulseImageRegion* dst)
{
}
void WebGPUDestroyBuffer(PulseDevice device, PulseBuffer buffer)
{
}

28
Sources/Backends/WebGPU/WebGPUBuffer.h git.filemode.normal_file
View File

@@ -0,0 +1,28 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_BUFFER_H_
#define PULSE_WEBGPU_BUFFER_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
#include "../../PulseInternal.h"
typedef struct WebGPUBuffer
{
} WebGPUBuffer;
PulseBuffer WebGPUCreateBuffer(PulseDevice device, const PulseBufferCreateInfo* create_infos);
bool WebGPUMapBuffer(PulseBuffer buffer, void** data);
void WebGPUUnmapBuffer(PulseBuffer buffer);
bool WebGPUCopyBufferToBuffer(PulseCommandList cmd, const PulseBufferRegion* src, const PulseBufferRegion* dst);
bool WebGPUCopyBufferToImage(PulseCommandList cmd, const PulseBufferRegion* src, const PulseImageRegion* dst);
void WebGPUDestroyBuffer(PulseDevice device, PulseBuffer buffer);
#endif // PULSE_WEBGPU_BUFFER_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

18
Sources/Backends/WebGPU/WebGPUCommandList.c git.filemode.normal_file
View File

@@ -0,0 +1,18 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "WebGPU.h"
PulseCommandList WebGPURequestCommandList(PulseDevice device, PulseCommandListUsage usage)
{
}
bool WebGPUSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFence fence)
{
}
void WebGPUReleaseCommandList(PulseDevice device, PulseCommandList cmd)
{
}

25
Sources/Backends/WebGPU/WebGPUCommandList.h git.filemode.normal_file
View File

@@ -0,0 +1,25 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_COMMAND_LIST_H_
#define PULSE_WEBGPU_COMMAND_LIST_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
#include "WebGPUBuffer.h"
typedef struct WebGPUCommandList
{
} WebGPUCommandList;
PulseCommandList WebGPURequestCommandList(PulseDevice device, PulseCommandListUsage usage);
bool WebGPUSubmitCommandList(PulseDevice device, PulseCommandList cmd, PulseFence fence);
void WebGPUReleaseCommandList(PulseDevice device, PulseCommandList cmd);
#endif // PULSE_WEBGPU_COMMAND_LIST_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

42
Sources/Backends/WebGPU/WebGPUComputePass.c git.filemode.normal_file
View File

@@ -0,0 +1,42 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "WebGPU.h"
PulseComputePass WebGPUCreateComputePass(PulseDevice device, PulseCommandList cmd)
{
}
void WebGPUDestroyComputePass(PulseDevice device, PulseComputePass pass)
{
}
PulseComputePass WebGPUBeginComputePass(PulseCommandList cmd)
{
}
void WebGPUEndComputePass(PulseComputePass pass)
{
}
void WebGPUBindStorageBuffers(PulseComputePass pass, const PulseBuffer* buffers, uint32_t num_buffers)
{
}
void WebGPUBindUniformData(PulseComputePass pass, uint32_t slot, const void* data, uint32_t data_size)
{
}
void WebGPUBindStorageImages(PulseComputePass pass, const PulseImage* images, uint32_t num_images)
{
}
void WebGPUBindComputePipeline(PulseComputePass pass, PulseComputePipeline pipeline)
{
}
void WebGPUDispatchComputations(PulseComputePass pass, uint32_t groupcount_x, uint32_t groupcount_y, uint32_t groupcount_z)
{
}

33
Sources/Backends/WebGPU/WebGPUComputePass.h git.filemode.normal_file
View File

@@ -0,0 +1,33 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_COMPUTE_PASS_H_
#define PULSE_WEBGPU_COMPUTE_PASS_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
#include "WebGPUBuffer.h"
#include "WebGPUCommandList.h"
typedef struct WebGPUComputePass
{
} WebGPUComputePass;
PulseComputePass WebGPUCreateComputePass(PulseDevice device, PulseCommandList cmd);
void WebGPUDestroyComputePass(PulseDevice device, PulseComputePass pass);
PulseComputePass WebGPUBeginComputePass(PulseCommandList cmd);
void WebGPUEndComputePass(PulseComputePass pass);
void WebGPUBindStorageBuffers(PulseComputePass pass, const PulseBuffer* buffers, uint32_t num_buffers);
void WebGPUBindUniformData(PulseComputePass pass, uint32_t slot, const void* data, uint32_t data_size);
void WebGPUBindStorageImages(PulseComputePass pass, const PulseImage* images, uint32_t num_images);
void WebGPUBindComputePipeline(PulseComputePass pass, PulseComputePipeline pipeline);
void WebGPUDispatchComputations(PulseComputePass pass, uint32_t groupcount_x, uint32_t groupcount_y, uint32_t groupcount_z);
#endif // PULSE_WEBGPU_COMPUTE_PASS_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

View File

@@ -0,0 +1,14 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "WebGPU.h"
PulseComputePipeline WebGPUCreateComputePipeline(PulseDevice device, const PulseComputePipelineCreateInfo* info)
{
}
void WebGPUDestroyComputePipeline(PulseDevice device, PulseComputePipeline pipeline)
{
}

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_COMPUTE_PIPELINE_H_
#define PULSE_WEBGPU_COMPUTE_PIPELINE_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
typedef struct WebGPUComputePipeline
{
} WebGPUComputePipeline;
PulseComputePipeline WebGPUCreateComputePipeline(PulseDevice device, const PulseComputePipelineCreateInfo* info);
void WebGPUDestroyComputePipeline(PulseDevice device, PulseComputePipeline pipeline);
#endif // PULSE_WEBGPU_COMPUTE_PIPELINE_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

207
Sources/Backends/WebGPU/WebGPUDevice.c git.filemode.normal_file
View File

@@ -0,0 +1,207 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "../../PulseInternal.h"
#include "WebGPU.h"
#include "WebGPUComputePipeline.h"
#include "WebGPUCommandList.h"
#include "WebGPUDevice.h"
#include "WebGPUFence.h"
#include "WebGPUBuffer.h"
#include "WebGPUImage.h"
#include "WebGPUComputePass.h"
#ifndef PULSE_PLAT_WASM
#include <wgpu.h>
#endif
#ifdef PULSE_PLAT_WASM
static void WebGPURequestAdapterCallback(WGPURequestAdapterStatus status, WGPUAdapter adapter, WGPUStringView message, void* userdata1, void* userdata2)
{
WebGPUDevice* device = (WebGPUDevice*)userdata1;
PulseBackend backend = (PulseBackend)userdata2;
if(status != WGPURequestAdapterStatus_Success)
{
if(PULSE_IS_BACKEND_LOW_LEVEL_DEBUG(backend))
PulseLogErrorFmt(backend, "(WebGPU) Could not load adapter %.*s", message.length, message.data);
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
pulse_device->has_error = true;
return;
}
pulse_device->has_error = false;
device->adapter = adapter;
}
#else
static uint64_t WebGPUScoreAdapter(WGPUInstance instance, WGPUAdapter adapter)
{
uint64_t score = 0;
WGPUAdapterInfo infos;
wgpuAdapterGetInfo(adapter, &infos);
WGPULimits limits;
wgpuAdapterGetLimits(adapter, &limits);
if(infos.adapterType == WGPUAdapterType_DiscreteGPU)
score += 10000;
switch(infos.backendType)
{
case WGPUBackendType_Metal:
case WGPUBackendType_D3D12:
case WGPUBackendType_WebGPU:
case WGPUBackendType_Vulkan: score += 1000;
default: break;
}
score += limits.maxComputeWorkgroupSizeX;
score += limits.maxComputeWorkgroupSizeY;
score += limits.maxComputeWorkgroupSizeZ;
score += limits.maxComputeWorkgroupStorageSize;
score += limits.maxComputeInvocationsPerWorkgroup;
score += limits.maxComputeWorkgroupsPerDimension;
score += limits.maxTextureDimension2D;
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 void WebGPURequestDeviceCallback(WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message, void* userdata1, void* userdata2)
{
WebGPUDevice* pulse_device = (WebGPUDevice*)userdata1;
PulseBackend backend = (PulseBackend)userdata2;
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);
PulseSetInternalError(PULSE_ERROR_INITIALIZATION_FAILED);
pulse_device->has_error = true;
return;
}
pulse_device->has_error = false;
pulse_device->device = device;
}
PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count)
{
PULSE_CHECK_HANDLE_RETVAL(backend, PULSE_NULLPTR);
PulseDevice pulse_device = (PulseDeviceHandler*)calloc(1, sizeof(PulseDeviceHandler));
PULSE_CHECK_ALLOCATION_RETVAL(pulse_device, PULSE_NULL_HANDLE);
WebGPUDevice* device = (WebGPUDevice*)calloc(1, sizeof(WebGPUDevice));
PULSE_CHECK_ALLOCATION_RETVAL(device, PULSE_NULL_HANDLE);
WGPUInstance instance = WEBGPU_RETRIEVE_DRIVER_DATA_AS(backend, WebGPUDriverData*)->instance;
#ifdef PULSE_PLAT_WASM
WGPURequestAdapterOptions adapter_options = { 0 };
adapter_options.powerPreference = WGPUPowerPreference_HighPerformance;
adapter_options.compatibleSurface = PULSE_NULLPTR;
adapter_options.backendType = WGPUBackendType_WebGPU;
WGPURequestAdapterCallbackInfo adapter_callback = { 0 };
adapter_callback.callback = WebGPURequestAdapterCallback;
adapter_callback.mode = WGPUCallbackMode_AllowProcessEvents;
adapter_callback.userdata1 = device;
adapter_callback.userdata2 = backend;
wgpuInstanceRequestAdapter(instance, &adapter_options, adapter_callback);
while(device->adapter == PULSE_NULLPTR && !device->has_error) // Wait for adapter request
PulseSleep(100);
#else
WGPUInstanceEnumerateAdapterOptions adapter_options = { 0 };
adapter_options.nextInChain = PULSE_NULLPTR;
adapter_options.backends = WGPUInstanceBackend_All;
size_t adapter_count = wgpuInstanceEnumerateAdapters(instance, &adapter_options, PULSE_NULLPTR);
WGPUAdapter* adapters = (WGPUAdapter*)calloc(adapter_count, sizeof(WGPUAdapter));
wgpuInstanceEnumerateAdapters(instance, &adapter_options, adapters);
uint64_t best_device_score = 0;
for(uint32_t i = 0; i < adapter_count; i++)
{
if(WebGPUIsDeviceForbidden(adapters[i], forbiden_devices, forbiden_devices_count))
continue;
uint64_t current_device_score = WebGPUScoreAdapter(instance, adapters[i]);
if(current_device_score > best_device_score)
{
best_device_score = current_device_score;
device->adapter = adapters[i];
}
}
#endif
if(device->adapter == PULSE_NULLPTR)
{
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;
}
wgpuAdapterGetLimits(device->adapter, &device->limits);
wgpuAdapterGetInfo(device->adapter, &device->infos);
WGPUDeviceDescriptor descriptor = { 0 };
descriptor.requiredLimits = &device->limits;
WGPURequestDeviceCallbackInfo device_callback = { 0 };
device_callback.callback = WebGPURequestDeviceCallback;
device_callback.mode = WGPUCallbackMode_AllowProcessEvents;
device_callback.userdata1 = device;
device_callback.userdata2 = backend;
wgpuAdapterRequestDevice(device->adapter, &descriptor, device_callback);
while(device->device == PULSE_NULLPTR && !device->has_error) // Wait for device request
PulseSleep(100);
if(device->has_error)
{
free(pulse_device);
free(device);
return PULSE_NULL_HANDLE;
}
pulse_device->driver_data = device;
pulse_device->backend = backend;
PULSE_LOAD_DRIVER_DEVICE(WebGPU);
if(PULSE_IS_BACKEND_HIGH_LEVEL_DEBUG(backend))
{
const char* backends[] = {
"Undefined",
"Null",
"WebGPU",
"D3D11",
"D3D12",
"Metal",
"Vulkan",
"OpenGL",
"OpenGLES",
};
PulseLogInfoFmt(backend, "(WebGPU) created device from %.*s using backend %s", device->infos.device.length, device->infos.device.data, backends[device->infos.backendType]);
}
return pulse_device;
}
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;
free(webgpu_device);
free(device);
}

29
Sources/Backends/WebGPU/WebGPUDevice.h git.filemode.normal_file
View File

@@ -0,0 +1,29 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_DEVICE_H_
#define PULSE_WEBGPU_DEVICE_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
typedef struct WebGPUDevice
{
WGPUAdapterInfo infos;
WGPULimits limits;
WGPUAdapter adapter;
WGPUDevice device;
bool has_error;
} WebGPUDevice;
PulseDevice WebGPUCreateDevice(PulseBackend backend, PulseDevice* forbiden_devices, uint32_t forbiden_devices_count);
void WebGPUDestroyDevice(PulseDevice device);
#endif // PULSE_WEBGPU_DEVICE_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

22
Sources/Backends/WebGPU/WebGPUFence.c git.filemode.normal_file
View File

@@ -0,0 +1,22 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "WebGPU.h"
PulseFence WebGPUCreateFence(PulseDevice device)
{
}
void WebGPUDestroyFence(PulseDevice device, PulseFence fence)
{
}
bool WebGPUIsFenceReady(PulseDevice device, PulseFence fence)
{
}
bool WebGPUWaitForFences(PulseDevice device, const PulseFence* fences, uint32_t fences_count, bool wait_for_all)
{
}

21
Sources/Backends/WebGPU/WebGPUFence.h git.filemode.normal_file
View File

@@ -0,0 +1,21 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_FENCE_H_
#define PULSE_WEBGPU_FENCE_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
PulseFence WebGPUCreateFence(PulseDevice device);
void WebGPUDestroyFence(PulseDevice device, PulseFence fence);
bool WebGPUIsFenceReady(PulseDevice device, PulseFence fence);
bool WebGPUWaitForFences(PulseDevice device, const PulseFence* fences, uint32_t fences_count, bool wait_for_all);
#endif // PULSE_WEBGPU_FENCE_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

26
Sources/Backends/WebGPU/WebGPUImage.c git.filemode.normal_file
View File

@@ -0,0 +1,26 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include <Pulse.h>
#include "WebGPU.h"
PulseImage WebGPUCreateImage(PulseDevice device, const PulseImageCreateInfo* create_infos)
{
}
bool WebGPUIsImageFormatValid(PulseDevice device, PulseImageFormat format, PulseImageType type, PulseImageUsageFlags usage)
{
}
bool WebGPUCopyImageToBuffer(PulseCommandList cmd, const PulseImageRegion* src, const PulseBufferRegion* dst)
{
}
bool WebGPUBlitImage(PulseCommandList cmd, const PulseImageRegion* src, const PulseImageRegion* dst)
{
}
void WebGPUDestroyImage(PulseDevice device, PulseImage image)
{
}

27
Sources/Backends/WebGPU/WebGPUImage.h git.filemode.normal_file
View File

@@ -0,0 +1,27 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#ifndef PULSE_WEBGPU_IMAGE_H_
#define PULSE_WEBGPU_IMAGE_H_
#include <webgpu/webgpu.h>
#include <Pulse.h>
#include "../../PulseInternal.h"
typedef struct WebGPUImage
{
} WebGPUImage;
PulseImage WebGPUCreateImage(PulseDevice device, const PulseImageCreateInfo* create_infos);
bool WebGPUIsImageFormatValid(PulseDevice device, PulseImageFormat format, PulseImageType type, PulseImageUsageFlags usage);
bool WebGPUCopyImageToBuffer(PulseCommandList cmd, const PulseImageRegion* src, const PulseBufferRegion* dst);
bool WebGPUBlitImage(PulseCommandList cmd, const PulseImageRegion* src, const PulseImageRegion* dst);
void WebGPUDestroyImage(PulseDevice device, PulseImage image);
#endif // PULSE_WEBGPU_IMAGE_H_
#endif // PULSE_ENABLE_WEBGPU_BACKEND

View File

@@ -14,6 +14,9 @@
#ifdef PULSE_ENABLE_METAL_BACKEND
#include "Backends/Metal/Metal.h"
#endif
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
#include "Backends/WebGPU/WebGPU.h"
#endif
// Ordered by default preference
static const PulseCheckBackendSupportPFN backends_supports[] = {
@@ -23,6 +26,9 @@ static const PulseCheckBackendSupportPFN backends_supports[] = {
#ifdef PULSE_ENABLE_METAL_BACKEND
MetalCheckSupport,
#endif
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
WebGPUCheckSupport,
#endif
PULSE_NULLPTR
};
@@ -90,6 +96,9 @@ static PulseBackend PulseGetBackendFromFlag(PulseBackendBits flag)
#ifdef PULSE_ENABLE_METAL_BACKEND
case PULSE_BACKEND_METAL: return &MetalDriver;
#endif
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
case PULSE_BACKEND_WEBGPU: return &WebGPUDriver;
#endif
default: break;
}

19
Sources/PulseInternal.c git.filemode.normal_file
View File

@@ -0,0 +1,19 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include "PulseInternal.h"
#include <threads.h>
PulseThreadID PulseGetThreadID()
{
return (PulseThreadID)thrd_current();
}
void PulseSleep(int32_t ms)
{
if(ms <= 0)
return;
thrd_sleep(&(struct timespec){ .tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000 }, PULSE_NULLPTR);
}

View File

@@ -155,6 +155,7 @@ typedef struct PulseComputePassHandler
} PulseComputePassHandler;
PulseThreadID PulseGetThreadID();
void PulseSleep(int32_t ms);
void PulseSetInternalError(PulseErrorType error);
@@ -174,5 +175,8 @@ void PulseLogBackend(PulseBackend backend, PulseDebugMessageSeverity type, const
#ifdef PULSE_ENABLE_METAL_BACKEND
extern PulseBackendHandler MetalDriver;
#endif // PULSE_ENABLE_METAL_BACKEND
#ifdef PULSE_ENABLE_WEBGPU_BACKEND
extern PulseBackendHandler WebGPUDriver;
#endif // PULSE_ENABLE_WEBGPU_BACKEND
#endif // PULSE_INTERNAL_H_

View File

@@ -1,15 +0,0 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include "PulseInternal.h"
#include "PulsePosix.h"
#ifdef PULSE_PLAT_POSIX
PulseThreadID PulseGetThreadID()
{
return (PulseThreadID)pthread_self();
}
#endif // PULSE_PLAT_POSIX

View File

@@ -1,17 +0,0 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifndef PULSE_POSIX_H_
#define PULSE_POSIX_H_
#include <PulseProfile.h>
#include "PulseInternal.h"
#ifdef PULSE_PLAT_POSIX
#include <pthread.h>
#endif // PULSE_PLAT_WINDOWS
#endif // PULSE_POSIX_H_

View File

@@ -1,15 +0,0 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#include "PulseInternal.h"
#include "PulseWindows.h"
#ifdef PULSE_PLAT_WINDOWS
PulseThreadID PulseGetThreadID()
{
return (PulseThreadID)GetCurrentThreadId();
}
#endif // PULSE_PLAT_WINDOWS

View File

@@ -1,18 +0,0 @@
// Copyright (C) 2025 kanel
// This file is part of "Pulse"
// For conditions of distribution and use, see copyright notice in LICENSE
#ifndef PULSE_WINDOWS_H_
#define PULSE_WINDOWS_H_
#include <PulseProfile.h>
#include "PulseInternal.h"
#ifdef PULSE_PLAT_WINDOWS
#define NOMINMAX
#include <windows.h>
#endif // PULSE_PLAT_WINDOWS
#endif // PULSE_WINDOWS_H_