17 wgpu::Instance instance;
18#if defined(__EMSCRIPTEN__)
19 instance = wgpu::CreateInstance();
23 const WGPUInstanceFeatureName requiredFeatures[] = {WGPUInstanceFeatureName_TimedWaitAny};
24 WGPUInstanceDescriptor instanceDesc{};
25 instanceDesc.requiredFeatureCount = 1;
26 instanceDesc.requiredFeatures = requiredFeatures;
27 instance = wgpu::CreateInstance(
reinterpret_cast<const wgpu::InstanceDescriptor*
>(&instanceDesc));
29 if (!instance)
throw Exception(
"WebGPU: wgpuCreateInstance failed");
34 wgpu::Instance& instance = prev.
instance;
39 std::atomic<bool> received{
false};
42 WGPURequestAdapterOptions opts{};
43 opts.compatibleSurface = compatibleSurface.Get();
44 opts.powerPreference = WGPUPowerPreference_HighPerformance;
46 WGPURequestAdapterCallbackInfo cbInfo{};
47 cbInfo.nextInChain =
nullptr;
48 cbInfo.mode = WGPUCallbackMode_AllowProcessEvents;
49 cbInfo.callback = [](WGPURequestAdapterStatus
status, WGPUAdapter
a, WGPUStringView msg,
void* userdata1,
51 auto*
r =
static_cast<Result*
>(userdata1);
52 if (
status == WGPURequestAdapterStatus_Success &&
a) {
53 r->adapter = wgpu::Adapter(
a);
55 r->error = msg.data ? std::string(msg.data, msg.length) :
"unknown adapter error";
57 r->received.store(
true);
59 cbInfo.userdata1 = &
result;
60 cbInfo.userdata2 =
nullptr;
62 wgpuInstanceRequestAdapter(instance.Get(), &opts, cbInfo);
63 while (!
result.received.load()) {
64#if defined(__EMSCRIPTEN__)
69 wgpuInstanceProcessEvents(instance.Get());
78 wgpu::Instance& instance = prev.
instance;
79 wgpu::Adapter&
adapter = prev.adapter;
84 std::atomic<bool> received{
false};
87 WGPUDeviceDescriptor devDesc{};
88 devDesc.label =
sv(
"eve_device");
91 constexpr std::uint32_t baselineSampledTextures = 16;
92 constexpr std::uint32_t extendedSampledTextures = 17;
95 if (colorAttachmentByteBudget < 32)
96 throw Exception(
"WebGPU: requested color attachment budget (%u) is below the 32-byte baseline",
97 colorAttachmentByteBudget);
98 wgpu::Limits adapterLimits{};
99 if (
adapter.GetLimits(&adapterLimits) != wgpu::Status::Success ||
100 adapterLimits.maxSampledTexturesPerShaderStage < baselineSampledTextures)
101 throw Exception(
"WebGPU: adapter exposes fewer than %u sampled textures per shader stage",
102 baselineSampledTextures);
103 WGPULimits requiredLimits = WGPU_LIMITS_INIT;
104 requiredLimits.maxSampledTexturesPerShaderStage =
105 std::min(adapterLimits.maxSampledTexturesPerShaderStage, extendedSampledTextures);
106 requiredLimits.maxColorAttachmentBytesPerSample =
107 std::min(adapterLimits.maxColorAttachmentBytesPerSample, colorAttachmentByteBudget);
109 devDesc.requiredLimits = &requiredLimits;
110 devDesc.uncapturedErrorCallbackInfo.callback = [](WGPUDevice
const*, WGPUErrorType
type, WGPUStringView
message,
112 std::fprintf(stderr,
"[webgpu] uncaptured error type=%d: %.*s\n",
int(
type),
int(
message.length),
115 devDesc.deviceLostCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
116 devDesc.deviceLostCallbackInfo.callback = [](WGPUDevice
const*, WGPUDeviceLostReason reason, WGPUStringView
message,
118 std::fprintf(stderr,
"[webgpu] device lost reason=%d: %.*s\n",
int(reason),
int(
message.length),
122 WGPURequestDeviceCallbackInfo cbInfo{};
123 cbInfo.nextInChain =
nullptr;
124 cbInfo.mode = WGPUCallbackMode_AllowProcessEvents;
125 cbInfo.callback = [](WGPURequestDeviceStatus
status, WGPUDevice
d, WGPUStringView msg,
void* userdata1,
127 auto*
r =
static_cast<Result*
>(userdata1);
128 if (
status == WGPURequestDeviceStatus_Success &&
d) {
129 r->device = wgpu::Device(
d);
131 r->error = msg.data ? std::string(msg.data, msg.length) :
"unknown device error";
133 r->received.store(
true);
135 cbInfo.userdata1 = &
result;
136 cbInfo.userdata2 =
nullptr;
138 wgpuAdapterRequestDevice(
adapter.Get(), &devDesc, cbInfo);
139 while (!
result.received.load()) {
140#if defined(__EMSCRIPTEN__)
143 wgpuInstanceProcessEvents(instance.Get());
146 throw Exception(
"WebGPU: device request failed (%s)",
result.error.c_str());
149 wgpu::Queue queue =
result.device.GetQueue();
wgpu::PopErrorScopeStatus status