14
14
15
15
namespace rhi ::wgpu {
16
16
17
- static void errorCallback (WGPUErrorType type, const char * message, void * userdata)
18
- {
19
- DeviceImpl* device = static_cast <DeviceImpl*>(userdata);
20
- device->handleError (type, message);
21
- }
22
-
23
17
Context::~Context ()
24
18
{
25
19
if (device)
@@ -52,17 +46,30 @@ Result DeviceImpl::getNativeDeviceHandles(DeviceNativeHandles* outHandles)
52
46
return SLANG_E_NOT_IMPLEMENTED;
53
47
}
54
48
55
- void DeviceImpl::handleError (WGPUErrorType type, const char * message)
49
+ void DeviceImpl::reportError (const char * func, WGPUStringView message)
50
+ {
51
+ std::string msg = " WGPU error in " + std::string (func) + " : " + std::string (message.data , message.length );
52
+ m_debugCallback->handleMessage (DebugMessageType::Error, DebugMessageSource::Driver, msg.c_str ());
53
+ }
54
+
55
+ void DeviceImpl::reportDeviceLost (WGPUDeviceLostReason reason, WGPUStringView message)
56
+ {
57
+ std::string msg = " WGPU device lost: " + std::string (message.data , message.length );
58
+ m_debugCallback->handleMessage (DebugMessageType::Error, DebugMessageSource::Driver, msg.c_str ());
59
+ }
60
+
61
+ void DeviceImpl::reportUncapturedError (WGPUErrorType type, WGPUStringView message)
56
62
{
57
- fprintf (stderr, " WGPU error: %s\n " , message);
58
- this ->m_lastError = type;
63
+ std::string msg = " WGPU uncaptured error: " + std::string (message.data , message.length );
64
+ m_debugCallback->handleMessage (DebugMessageType::Error, DebugMessageSource::Driver, msg.c_str ());
65
+ this ->m_lastUncapturedError = type;
59
66
}
60
67
61
- WGPUErrorType DeviceImpl::getAndClearLastError ()
68
+ WGPUErrorType DeviceImpl::getAndClearLastUncapturedError ()
62
69
{
63
- WGPUErrorType lastError = this ->m_lastError ;
64
- this ->m_lastError = WGPUErrorType_NoError;
65
- return lastError ;
70
+ WGPUErrorType error = this ->m_lastUncapturedError ;
71
+ this ->m_lastUncapturedError = WGPUErrorType_NoError;
72
+ return error ;
66
73
}
67
74
68
75
Result DeviceImpl::initialize (const DeviceDesc& desc)
@@ -109,12 +116,12 @@ Result DeviceImpl::initialize(const DeviceDesc& desc)
109
116
options.nextInChain = &togglesDesc.chain ;
110
117
111
118
{
112
- WGPURequestAdapterStatus status = WGPURequestAdapterStatus_Unknown ;
113
- WGPURequestAdapterCallbackInfo2 callbackInfo = {};
119
+ WGPURequestAdapterStatus status = WGPURequestAdapterStatus ( 0 ) ;
120
+ WGPURequestAdapterCallbackInfo callbackInfo = {};
114
121
callbackInfo.mode = WGPUCallbackMode_WaitAnyOnly;
115
122
callbackInfo.callback = [](WGPURequestAdapterStatus status_,
116
123
WGPUAdapter adapter,
117
- const char * message,
124
+ WGPUStringView message,
118
125
void * userdata1,
119
126
void * userdata2)
120
127
{
@@ -123,7 +130,7 @@ Result DeviceImpl::initialize(const DeviceDesc& desc)
123
130
};
124
131
callbackInfo.userdata1 = &status;
125
132
callbackInfo.userdata2 = &m_ctx.adapter ;
126
- WGPUFuture future = m_ctx.api .wgpuInstanceRequestAdapter2 (m_ctx.instance , &options, callbackInfo);
133
+ WGPUFuture future = m_ctx.api .wgpuInstanceRequestAdapter (m_ctx.instance , &options, callbackInfo);
127
134
WGPUFutureWaitInfo futures[1 ] = {{future}};
128
135
uint64_t timeoutNS = UINT64_MAX;
129
136
WGPUWaitStatus waitStatus =
@@ -139,53 +146,59 @@ Result DeviceImpl::initialize(const DeviceDesc& desc)
139
146
api.wgpuAdapterGetLimits (m_ctx.adapter , &adapterLimits);
140
147
141
148
// Query adapter features.
142
- size_t adapterFeatureCount = api.wgpuAdapterEnumerateFeatures (m_ctx.adapter , nullptr );
143
- std::vector<WGPUFeatureName> adapterFeatures (adapterFeatureCount);
144
- api.wgpuAdapterEnumerateFeatures (m_ctx.adapter , adapterFeatures.data ());
149
+ WGPUSupportedFeatures adapterFeatures = {};
150
+ api.wgpuAdapterGetFeatures (m_ctx.adapter , &adapterFeatures);
145
151
146
152
// We request a device with the maximum available limits and feature set.
147
153
WGPURequiredLimits requiredLimits = {};
148
154
requiredLimits.limits = adapterLimits.limits ;
149
155
WGPUDeviceDescriptor deviceDesc = {};
150
- deviceDesc.requiredFeatures = adapterFeatures.data () ;
151
- deviceDesc.requiredFeatureCount = adapterFeatures.size () ;
156
+ deviceDesc.requiredFeatures = adapterFeatures.features ;
157
+ deviceDesc.requiredFeatureCount = adapterFeatures.featureCount ;
152
158
deviceDesc.requiredLimits = &requiredLimits;
153
- deviceDesc.uncapturedErrorCallbackInfo .callback = errorCallback;
154
- deviceDesc.uncapturedErrorCallbackInfo .userdata = this ;
159
+ deviceDesc.uncapturedErrorCallbackInfo .callback =
160
+ [](const WGPUDevice* device, WGPUErrorType type, WGPUStringView message, void * userdata1, void * userdata2)
161
+ {
162
+ DeviceImpl* deviceImpl = static_cast <DeviceImpl*>(userdata1);
163
+ deviceImpl->reportUncapturedError (type, message);
164
+ };
165
+ deviceDesc.uncapturedErrorCallbackInfo .userdata1 = this ;
155
166
deviceDesc.nextInChain = &togglesDesc.chain ;
156
167
157
168
{
158
- WGPURequestDeviceStatus status = WGPURequestDeviceStatus_Unknown ;
159
- WGPURequestDeviceCallbackInfo2 callbackInfo = {};
169
+ WGPURequestDeviceStatus status = WGPURequestDeviceStatus ( 0 ) ;
170
+ WGPURequestDeviceCallbackInfo callbackInfo = {};
160
171
callbackInfo.mode = WGPUCallbackMode_WaitAnyOnly;
161
- callbackInfo.callback =
162
- [](WGPURequestDeviceStatus status_, WGPUDevice device, const char * message, void * userdata1, void * userdata2
163
- )
172
+ callbackInfo.callback = [](WGPURequestDeviceStatus status_,
173
+ WGPUDevice device,
174
+ WGPUStringView message,
175
+ void * userdata1,
176
+ void * userdata2)
164
177
{
165
178
*(WGPURequestDeviceStatus*)userdata1 = status_;
166
179
*(WGPUDevice*)userdata2 = device;
167
180
};
168
181
callbackInfo.userdata1 = &status;
169
182
callbackInfo.userdata2 = &m_ctx.device ;
170
183
171
- WGPUDeviceLostCallbackInfo2 deviceLostCallbackInfo = {};
184
+ WGPUDeviceLostCallbackInfo deviceLostCallbackInfo = {};
172
185
deviceLostCallbackInfo.callback = [](const WGPUDevice* device,
173
186
WGPUDeviceLostReason reason,
174
- const char * message,
187
+ WGPUStringView message,
175
188
void * userdata1,
176
189
void * userdata2)
177
190
{
178
191
if (reason != WGPUDeviceLostReason_Destroyed)
179
192
{
180
193
DeviceImpl* deviceimpl = static_cast <DeviceImpl*>(userdata1);
181
- deviceimpl->handleError (WGPUErrorType_DeviceLost , message);
194
+ deviceimpl->reportDeviceLost (reason , message);
182
195
}
183
196
};
184
197
deviceLostCallbackInfo.userdata1 = this ;
185
198
deviceLostCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
186
- deviceDesc.deviceLostCallbackInfo2 = deviceLostCallbackInfo;
199
+ deviceDesc.deviceLostCallbackInfo = deviceLostCallbackInfo;
187
200
188
- WGPUFuture future = m_ctx.api .wgpuAdapterRequestDevice2 (m_ctx.adapter , &deviceDesc, callbackInfo);
201
+ WGPUFuture future = m_ctx.api .wgpuAdapterRequestDevice (m_ctx.adapter , &deviceDesc, callbackInfo);
189
202
WGPUFutureWaitInfo futures[1 ] = {{future}};
190
203
uint64_t timeoutNS = UINT64_MAX;
191
204
WGPUWaitStatus waitStatus =
@@ -204,10 +217,9 @@ Result DeviceImpl::initialize(const DeviceDesc& desc)
204
217
m_info.limits .maxComputeDispatchThreadGroups [0 ] = m_ctx.limits .maxComputeWorkgroupSizeX ;
205
218
206
219
// Query device features.
207
- size_t deviceFeatureCount = api.wgpuDeviceEnumerateFeatures (m_ctx.device , nullptr );
208
- std::vector<WGPUFeatureName> deviceFeatures (deviceFeatureCount);
209
- api.wgpuDeviceEnumerateFeatures (m_ctx.device , deviceFeatures.data ());
210
- m_ctx.features .insert (deviceFeatures.begin (), deviceFeatures.end ());
220
+ WGPUSupportedFeatures supportedFeatures = {};
221
+ api.wgpuDeviceGetFeatures (m_ctx.device , &supportedFeatures);
222
+ m_ctx.features .insert (supportedFeatures.features , supportedFeatures.features + supportedFeatures.featureCount );
211
223
212
224
// Supports ParameterBlock
213
225
m_features.push_back (" parameter-block" );
@@ -264,13 +276,13 @@ Result DeviceImpl::readBuffer(IBuffer* buffer, Offset offset, Size size, ISlangB
264
276
265
277
// Wait for the command buffer to finish executing
266
278
{
267
- WGPUQueueWorkDoneStatus status = WGPUQueueWorkDoneStatus_Unknown ;
268
- WGPUQueueWorkDoneCallbackInfo2 callbackInfo = {};
279
+ WGPUQueueWorkDoneStatus status = WGPUQueueWorkDoneStatus ( 0 ) ;
280
+ WGPUQueueWorkDoneCallbackInfo callbackInfo = {};
269
281
callbackInfo.mode = WGPUCallbackMode_WaitAnyOnly;
270
282
callbackInfo.callback = [](WGPUQueueWorkDoneStatus status_, void * userdata1, void * userdata2)
271
283
{ *(WGPUQueueWorkDoneStatus*)userdata1 = status_; };
272
284
callbackInfo.userdata1 = &status;
273
- WGPUFuture future = m_ctx.api .wgpuQueueOnSubmittedWorkDone2 (queue, callbackInfo);
285
+ WGPUFuture future = m_ctx.api .wgpuQueueOnSubmittedWorkDone (queue, callbackInfo);
274
286
WGPUFutureWaitInfo futures[1 ] = {{future}};
275
287
uint64_t timeoutNS = UINT64_MAX;
276
288
WGPUWaitStatus waitStatus =
@@ -283,13 +295,20 @@ Result DeviceImpl::readBuffer(IBuffer* buffer, Offset offset, Size size, ISlangB
283
295
284
296
// Map the staging buffer
285
297
{
286
- WGPUMapAsyncStatus status = WGPUMapAsyncStatus_Unknown ;
287
- WGPUBufferMapCallbackInfo2 callbackInfo = {};
298
+ WGPUMapAsyncStatus status = WGPUMapAsyncStatus ( 0 ) ;
299
+ WGPUBufferMapCallbackInfo callbackInfo = {};
288
300
callbackInfo.mode = WGPUCallbackMode_WaitAnyOnly;
289
- callbackInfo.callback = [](WGPUMapAsyncStatus status_, const char * message, void * userdata1, void * userdata2)
290
- { *(WGPUMapAsyncStatus*)userdata1 = status_; };
301
+ callbackInfo.callback = [](WGPUMapAsyncStatus status_, WGPUStringView message, void * userdata1, void * userdata2)
302
+ {
303
+ *(WGPUMapAsyncStatus*)userdata1 = status_;
304
+ if (status_ != WGPUMapAsyncStatus_Success)
305
+ {
306
+ static_cast <DeviceImpl*>(userdata2)->reportError (" wgpuBufferMapAsync" , message);
307
+ }
308
+ };
291
309
callbackInfo.userdata1 = &status;
292
- WGPUFuture future = m_ctx.api .wgpuBufferMapAsync2 (stagingBuffer, WGPUMapMode_Read, 0 , size, callbackInfo);
310
+ callbackInfo.userdata2 = this ;
311
+ WGPUFuture future = m_ctx.api .wgpuBufferMapAsync (stagingBuffer, WGPUMapMode_Read, 0 , size, callbackInfo);
293
312
WGPUFutureWaitInfo futures[1 ] = {{future}};
294
313
uint64_t timeoutNS = UINT64_MAX;
295
314
WGPUWaitStatus waitStatus =
0 commit comments