mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
wasm: handle WebGPU initialization failure case
This commit is contained in:
parent
5d617c0821
commit
8237eea4e9
1 changed files with 17 additions and 2 deletions
|
@ -43,12 +43,15 @@ static const char* NoError = "None";
|
||||||
static WGPUDevice device{};
|
static WGPUDevice device{};
|
||||||
static bool adapterRequested = false;
|
static bool adapterRequested = false;
|
||||||
static bool deviceRequested = false;
|
static bool deviceRequested = false;
|
||||||
|
static bool initializationFailed = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 0: success, 1: fail, 2: wait for async request
|
// 0: success, 1: fail, 2: wait for async request
|
||||||
int init()
|
int init()
|
||||||
{
|
{
|
||||||
#ifdef THORVG_WG_RASTER_SUPPORT
|
#ifdef THORVG_WG_RASTER_SUPPORT
|
||||||
|
if (initializationFailed) return 1;
|
||||||
|
|
||||||
//Init WebGPU
|
//Init WebGPU
|
||||||
if (!instance) instance = wgpuCreateInstance(nullptr);
|
if (!instance) instance = wgpuCreateInstance(nullptr);
|
||||||
|
|
||||||
|
@ -57,7 +60,13 @@ int init()
|
||||||
if (adapterRequested) return 2;
|
if (adapterRequested) return 2;
|
||||||
|
|
||||||
const WGPURequestAdapterOptions requestAdapterOptions { .nextInChain = nullptr, .powerPreference = WGPUPowerPreference_HighPerformance, .forceFallbackAdapter = false };
|
const WGPURequestAdapterOptions requestAdapterOptions { .nextInChain = nullptr, .powerPreference = WGPUPowerPreference_HighPerformance, .forceFallbackAdapter = false };
|
||||||
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * pUserData) { *((WGPUAdapter*)pUserData) = adapter; };
|
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const* message, void* pUserData) {
|
||||||
|
if (status != WGPURequestAdapterStatus_Success) {
|
||||||
|
initializationFailed = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*((WGPUAdapter*)pUserData) = adapter;
|
||||||
|
};
|
||||||
wgpuInstanceRequestAdapter(instance, &requestAdapterOptions, onAdapterRequestEnded, &adapter);
|
wgpuInstanceRequestAdapter(instance, &requestAdapterOptions, onAdapterRequestEnded, &adapter);
|
||||||
|
|
||||||
adapterRequested = true;
|
adapterRequested = true;
|
||||||
|
@ -71,7 +80,13 @@ int init()
|
||||||
size_t featuresCount = wgpuAdapterEnumerateFeatures(adapter, featureNames);
|
size_t featuresCount = wgpuAdapterEnumerateFeatures(adapter, featureNames);
|
||||||
if (!device) {
|
if (!device) {
|
||||||
const WGPUDeviceDescriptor deviceDesc { .nextInChain = nullptr, .label = "The device", .requiredFeatureCount = featuresCount, .requiredFeatures = featureNames };
|
const WGPUDeviceDescriptor deviceDesc { .nextInChain = nullptr, .label = "The device", .requiredFeatureCount = featuresCount, .requiredFeatures = featureNames };
|
||||||
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, char const * message, void * pUserData) { *((WGPUDevice*)pUserData) = device; };
|
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, char const* message, void* pUserData) {
|
||||||
|
if (status != WGPURequestDeviceStatus_Success) {
|
||||||
|
initializationFailed = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*((WGPUDevice*)pUserData) = device;
|
||||||
|
};
|
||||||
wgpuAdapterRequestDevice(adapter, &deviceDesc, onDeviceRequestEnded, &device);
|
wgpuAdapterRequestDevice(adapter, &deviceDesc, onDeviceRequestEnded, &device);
|
||||||
|
|
||||||
deviceRequested = true;
|
deviceRequested = true;
|
||||||
|
|
Loading…
Add table
Reference in a new issue