Skip to content

Commit

Permalink
WebGPU: Add uncapturedErrorCallbackInfo to requestDevice descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed Jun 4, 2024
1 parent 28c10a1 commit 72207ea
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/generated_struct_info32.json
Original file line number Diff line number Diff line change
Expand Up @@ -961,15 +961,16 @@
"stencilWriteMask": 52
},
"WGPUDeviceDescriptor": {
"__size__": 36,
"__size__": 48,
"defaultQueue": 20,
"deviceLostCallback": 28,
"deviceLostUserdata": 32,
"label": 4,
"nextInChain": 0,
"requiredFeatureCount": 8,
"requiredFeatures": 12,
"requiredLimits": 16
"requiredLimits": 16,
"uncapturedErrorCallbackInfo": 36
},
"WGPUExtent3D": {
"__size__": 12,
Expand Down Expand Up @@ -1355,6 +1356,12 @@
"mipLevelCount": 20,
"nextInChain": 0
},
"WGPUUncapturedErrorCallbackInfo": {
"__size__": 12,
"callback": 4,
"nextInChain": 0,
"userdata": 8
},
"WGPUVertexAttribute": {
"__size__": 24,
"format": 0,
Expand Down
11 changes: 9 additions & 2 deletions src/generated_struct_info64.json
Original file line number Diff line number Diff line change
Expand Up @@ -961,15 +961,16 @@
"stencilWriteMask": 56
},
"WGPUDeviceDescriptor": {
"__size__": 72,
"__size__": 96,
"defaultQueue": 40,
"deviceLostCallback": 56,
"deviceLostUserdata": 64,
"label": 8,
"nextInChain": 0,
"requiredFeatureCount": 16,
"requiredFeatures": 24,
"requiredLimits": 32
"requiredLimits": 32,
"uncapturedErrorCallbackInfo": 72
},
"WGPUExtent3D": {
"__size__": 12,
Expand Down Expand Up @@ -1355,6 +1356,12 @@
"mipLevelCount": 28,
"nextInChain": 0
},
"WGPUUncapturedErrorCallbackInfo": {
"__size__": 24,
"callback": 8,
"nextInChain": 0,
"userdata": 16
},
"WGPUVertexAttribute": {
"__size__": 24,
"format": 0,
Expand Down
22 changes: 22 additions & 0 deletions src/library_webgpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2639,6 +2639,8 @@ var LibraryWebGPU = {

var deviceLostCallbackPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUDeviceDescriptor.deviceLostCallback, '*') }}};
var deviceLostUserdataPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUDeviceDescriptor.deviceLostUserdata, '*') }}};
var uncapturedErrorCallbackPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUDeviceDescriptor.uncapturedErrorCallbackInfo + C_STRUCTS.WGPUUncapturedErrorCallbackInfo.callback, '*') }}};
var uncapturedErrorUserDataPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUDeviceDescriptor.uncapturedErrorCallbackInfo + C_STRUCTS.WGPUUncapturedErrorCallbackInfo.userdata, '*') }}};

var labelPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUDeviceDescriptor.label, '*') }}};
if (labelPtr) desc["label"] = UTF8ToString(labelPtr);
Expand All @@ -2656,6 +2658,26 @@ var LibraryWebGPU = {
WebGPU.Int_DeviceLostReason[info.reason], info.message, deviceLostUserdataPtr));
});
}
if (uncapturedErrorCallbackPtr) {
device.onuncapturederror = function(ev) {
// This will skip the callback if the runtime is no longer alive.
callUserCallback(() => {
// WGPUErrorType type, const char* message, void* userdata
var Validation = 0x00000001;
var OutOfMemory = 0x00000002;
var type;
#if ASSERTIONS
assert(typeof GPUValidationError != 'undefined');
assert(typeof GPUOutOfMemoryError != 'undefined');
#endif
if (ev.error instanceof GPUValidationError) type = Validation;
else if (ev.error instanceof GPUOutOfMemoryError) type = OutOfMemory;
// TODO: Implement GPUInternalError

WebGPU.errorCallback(uncapturedErrorCallbackPtr, type, ev.error.message, uncapturedErrorUserDataPtr);
});
};
}
{{{ makeDynCall('vippp', 'callback') }}}({{{ gpu.RequestDeviceStatus.Success }}}, deviceId, 0, userdata);
});
}, function(ex) {
Expand Down
8 changes: 7 additions & 1 deletion src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,11 @@
"arrayLayerCount",
"aspect"
],
"WGPUUncapturedErrorCallbackInfo": [
"nextInChain",
"callback",
"userdata"
],
"WGPUVertexAttribute": [
"format",
"offset",
Expand Down Expand Up @@ -1693,7 +1698,8 @@
"requiredLimits",
"defaultQueue",
"deviceLostCallback",
"deviceLostUserdata"
"deviceLostUserdata",
"uncapturedErrorCallbackInfo"
],
"WGPURenderPassDescriptor": [
"nextInChain",
Expand Down
8 changes: 8 additions & 0 deletions system/include/webgpu/webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ struct WGPUTextureBindingLayout;
struct WGPUTextureBindingViewDimensionDescriptor;
struct WGPUTextureDataLayout;
struct WGPUTextureViewDescriptor;
struct WGPUUncapturedErrorCallbackInfo;
struct WGPUVertexAttribute;
struct WGPUBindGroupDescriptor;
struct WGPUBindGroupLayoutEntry;
Expand Down Expand Up @@ -1174,6 +1175,12 @@ typedef struct WGPUTextureViewDescriptor {
WGPUTextureAspect aspect;
} WGPUTextureViewDescriptor WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUUncapturedErrorCallbackInfo {
WGPUChainedStruct const * nextInChain;
WGPUErrorCallback callback;
void * userdata;
} WGPUUncapturedErrorCallbackInfo WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPUVertexAttribute {
WGPUVertexFormat format;
uint64_t offset;
Expand Down Expand Up @@ -1331,6 +1338,7 @@ typedef struct WGPUDeviceDescriptor {
WGPUQueueDescriptor defaultQueue;
WGPUDeviceLostCallback deviceLostCallback;
void * deviceLostUserdata;
WGPUUncapturedErrorCallbackInfo uncapturedErrorCallbackInfo;
} WGPUDeviceDescriptor WGPU_STRUCTURE_ATTRIBUTE;

typedef struct WGPURenderPassDescriptor {
Expand Down
8 changes: 8 additions & 0 deletions system/include/webgpu/webgpu_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ namespace wgpu {
struct TextureBindingViewDimensionDescriptor;
struct TextureDataLayout;
struct TextureViewDescriptor;
struct UncapturedErrorCallbackInfo;
struct VertexAttribute;
struct BindGroupDescriptor;
struct BindGroupLayoutEntry;
Expand Down Expand Up @@ -1631,6 +1632,12 @@ namespace wgpu {
TextureAspect aspect = TextureAspect::All;
};

struct UncapturedErrorCallbackInfo {
ChainedStruct const * nextInChain = nullptr;
ErrorCallback callback = nullptr;
void * userdata = nullptr;
};

struct VertexAttribute {
VertexFormat format;
uint64_t offset;
Expand Down Expand Up @@ -1788,6 +1795,7 @@ namespace wgpu {
QueueDescriptor defaultQueue;
DeviceLostCallback deviceLostCallback = nullptr;
void * deviceLostUserdata = nullptr;
UncapturedErrorCallbackInfo uncapturedErrorCallbackInfo;
};

struct RenderPassDescriptor {
Expand Down
14 changes: 14 additions & 0 deletions system/lib/webgpu/webgpu_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,18 @@ template <typename T>
static_assert(offsetof(TextureViewDescriptor, aspect) == offsetof(WGPUTextureViewDescriptor, aspect),
"offsetof mismatch for TextureViewDescriptor::aspect");

// UncapturedErrorCallbackInfo

static_assert(sizeof(UncapturedErrorCallbackInfo) == sizeof(WGPUUncapturedErrorCallbackInfo), "sizeof mismatch for UncapturedErrorCallbackInfo");
static_assert(alignof(UncapturedErrorCallbackInfo) == alignof(WGPUUncapturedErrorCallbackInfo), "alignof mismatch for UncapturedErrorCallbackInfo");

static_assert(offsetof(UncapturedErrorCallbackInfo, nextInChain) == offsetof(WGPUUncapturedErrorCallbackInfo, nextInChain),
"offsetof mismatch for UncapturedErrorCallbackInfo::nextInChain");
static_assert(offsetof(UncapturedErrorCallbackInfo, callback) == offsetof(WGPUUncapturedErrorCallbackInfo, callback),
"offsetof mismatch for UncapturedErrorCallbackInfo::callback");
static_assert(offsetof(UncapturedErrorCallbackInfo, userdata) == offsetof(WGPUUncapturedErrorCallbackInfo, userdata),
"offsetof mismatch for UncapturedErrorCallbackInfo::userdata");

// VertexAttribute

static_assert(sizeof(VertexAttribute) == sizeof(WGPUVertexAttribute), "sizeof mismatch for VertexAttribute");
Expand Down Expand Up @@ -1841,6 +1853,8 @@ template <typename T>
"offsetof mismatch for DeviceDescriptor::deviceLostCallback");
static_assert(offsetof(DeviceDescriptor, deviceLostUserdata) == offsetof(WGPUDeviceDescriptor, deviceLostUserdata),
"offsetof mismatch for DeviceDescriptor::deviceLostUserdata");
static_assert(offsetof(DeviceDescriptor, uncapturedErrorCallbackInfo) == offsetof(WGPUDeviceDescriptor, uncapturedErrorCallbackInfo),
"offsetof mismatch for DeviceDescriptor::uncapturedErrorCallbackInfo");

// RenderPassDescriptor

Expand Down
11 changes: 5 additions & 6 deletions test/webgpu_basic_rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ void GetAdapter(void (*callback)(wgpu::Adapter)) {
}

void GetDevice(void (*callback)(wgpu::Device)) {
adapter.RequestDevice(nullptr, [](WGPURequestDeviceStatus status, WGPUDevice cDevice, const char* message, void* userdata) {
wgpu::DeviceDescriptor desc{};
desc.uncapturedErrorCallbackInfo = {nullptr, [](WGPUErrorType type, char const* message, void*) {
printf("%d: %s\n", type, message);
}, nullptr};
adapter.RequestDevice(&desc, [](WGPURequestDeviceStatus status, WGPUDevice cDevice, const char* message, void* userdata) {
if (message) {
printf("RequestDevice: %s\n", message);
}
Expand All @@ -66,11 +70,6 @@ void GetDevice(void (*callback)(wgpu::Device)) {
}

void init() {
device.SetUncapturedErrorCallback(
[](WGPUErrorType errorType, const char* message, void*) {
printf("%d: %s\n", errorType, message);
}, nullptr);

queue = device.GetQueue();

wgpu::ShaderModule shaderModule{};
Expand Down

0 comments on commit 72207ea

Please sign in to comment.