载入中...
搜索中...
未找到
InitFlow.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5#include <algorithm>
6#include <atomic>
7#include <cstdio>
8#include <cstring>
9
10#if defined(__EMSCRIPTEN__)
11#include <emscripten/emscripten.h>
12#endif
13
14namespace eve::graphics::webgpu {
15
17 wgpu::Instance instance;
18#if defined(__EMSCRIPTEN__)
19 instance = wgpu::CreateInstance();
20#else
21 // Native (Dawn) requires the TimedWaitAny instance feature so the
22 // ProcessEvents busy-wait below can drive the request callbacks.
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));
28#endif
29 if (!instance) throw Exception("WebGPU: wgpuCreateInstance failed");
30 return InstanceDone{std::move(instance)};
31}
32
33AdapterDone InitFlow::requestAdapter(InstanceDone&& prev, const wgpu::Surface& compatibleSurface) {
34 wgpu::Instance& instance = prev.instance;
35
36 struct Result {
37 wgpu::Adapter adapter;
38 std::string error;
39 std::atomic<bool> received{false};
40 } result;
41
42 WGPURequestAdapterOptions opts{};
43 opts.compatibleSurface = compatibleSurface.Get();
44 opts.powerPreference = WGPUPowerPreference_HighPerformance;
45
46 WGPURequestAdapterCallbackInfo cbInfo{};
47 cbInfo.nextInChain = nullptr;
48 cbInfo.mode = WGPUCallbackMode_AllowProcessEvents;
49 cbInfo.callback = [](WGPURequestAdapterStatus status, WGPUAdapter a, WGPUStringView msg, void* userdata1,
50 void* /*userdata2*/) {
51 auto* r = static_cast<Result*>(userdata1);
52 if (status == WGPURequestAdapterStatus_Success && a) {
53 r->adapter = wgpu::Adapter(a);
54 } else {
55 r->error = msg.data ? std::string(msg.data, msg.length) : "unknown adapter error";
56 }
57 r->received.store(true);
58 };
59 cbInfo.userdata1 = &result;
60 cbInfo.userdata2 = nullptr;
61
62 wgpuInstanceRequestAdapter(instance.Get(), &opts, cbInfo);
63 while (!result.received.load()) {
64#if defined(__EMSCRIPTEN__)
65 // Yield to the browser event loop so the JS requestAdapter promise can
66 // resolve and fire the callback, then flush it with ProcessEvents.
67 emscripten_sleep(0);
68#endif
69 wgpuInstanceProcessEvents(instance.Get());
70 }
71 if (!result.adapter) {
72 throw Exception("WebGPU: no adapter found (%s)", result.error.c_str());
73 }
74 return AdapterDone{std::move(instance), std::move(result.adapter)};
75}
76
77DeviceDone InitFlow::requestDevice(AdapterDone&& prev, uint32_t colorAttachmentByteBudget) {
78 wgpu::Instance& instance = prev.instance;
79 wgpu::Adapter& adapter = prev.adapter;
80
81 struct Result {
82 wgpu::Device device;
83 std::string error;
84 std::atomic<bool> received{false};
85 } result;
86
87 WGPUDeviceDescriptor devDesc{};
88 devDesc.label = sv("eve_device");
89 // Fixed pipelines fit the baseline 16-texture limit. Extended PBR variants
90 // are checked against the captured device limit before their layouts are made.
91 constexpr std::uint32_t baselineSampledTextures = 16;
92 constexpr std::uint32_t extendedSampledTextures = 17;
93 // The five-target GBuffer needs 40 bytes/sample. Request that budget when
94 // available, but keep the baseline 32-byte device usable for forward rendering.
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);
108 // Leave maxColorAttachments at the WGPU default (8); only raise bytes/sample.
109 devDesc.requiredLimits = &requiredLimits;
110 devDesc.uncapturedErrorCallbackInfo.callback = [](WGPUDevice const*, WGPUErrorType type, WGPUStringView message,
111 void*, void*) {
112 std::fprintf(stderr, "[webgpu] uncaptured error type=%d: %.*s\n", int(type), int(message.length),
113 message.data ? message.data : "");
114 };
115 devDesc.deviceLostCallbackInfo.mode = WGPUCallbackMode_AllowSpontaneous;
116 devDesc.deviceLostCallbackInfo.callback = [](WGPUDevice const*, WGPUDeviceLostReason reason, WGPUStringView message,
117 void*, void*) {
118 std::fprintf(stderr, "[webgpu] device lost reason=%d: %.*s\n", int(reason), int(message.length),
119 message.data ? message.data : "");
120 };
121
122 WGPURequestDeviceCallbackInfo cbInfo{};
123 cbInfo.nextInChain = nullptr;
124 cbInfo.mode = WGPUCallbackMode_AllowProcessEvents;
125 cbInfo.callback = [](WGPURequestDeviceStatus status, WGPUDevice d, WGPUStringView msg, void* userdata1,
126 void* /*userdata2*/) {
127 auto* r = static_cast<Result*>(userdata1);
128 if (status == WGPURequestDeviceStatus_Success && d) {
129 r->device = wgpu::Device(d);
130 } else {
131 r->error = msg.data ? std::string(msg.data, msg.length) : "unknown device error";
132 }
133 r->received.store(true);
134 };
135 cbInfo.userdata1 = &result;
136 cbInfo.userdata2 = nullptr;
137
138 wgpuAdapterRequestDevice(adapter.Get(), &devDesc, cbInfo);
139 while (!result.received.load()) {
140#if defined(__EMSCRIPTEN__)
141 emscripten_sleep(0);
142#endif
143 wgpuInstanceProcessEvents(instance.Get());
144 }
145 if (!result.device) {
146 throw Exception("WebGPU: device request failed (%s)", result.error.c_str());
147 }
148
149 wgpu::Queue queue = result.device.GetQueue();
151 caps.capture(adapter, result.device);
152 return DeviceDone{std::move(instance), std::move(adapter), std::move(result.device), std::move(queue),
153 std::move(caps)};
154}
155
156} // namespace eve::graphics::webgpu
std::string message
vkb::Device & device
wgpu::PopErrorScopeStatus status
double r
Poco::JSON::Object::Ptr result
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string error
Definition Package.cpp:174
float d
std::string_view adapter
int caps
Definition TreeMesh.cpp:164
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Read-only snapshot of WebGPU adapter/device limits and surface capabilities, queried once after devic...
static InstanceDone createInstance()
Create a wgpu Instance (native requests the TimedWaitAny feature).
Definition InitFlow.cpp:16
static AdapterDone requestAdapter(InstanceDone &&prev, const wgpu::Surface &compatibleSurface)
Blocking adapter request. Drives ProcessEvents until the callback fires; throws eve::graphics::Except...
Definition InitFlow.cpp:33
static DeviceDone requestDevice(AdapterDone &&prev, uint32_t colorAttachmentByteBudget=40)
Blocking device request (plus queue + capability capture). Throws eve::graphics::Exception on failure...
Definition InitFlow.cpp:77
WGPUStringView sv(const char *s)
Build a WGPUStringView from a C string (null-safe, length auto-computed).
Definition wgpu_types.h:20
Instance + adapter resolved.
Definition InitFlow.h:25
Instance + adapter + device + queue + capability snapshot.
Definition InitFlow.h:31
Instance created, no adapter yet.
Definition InitFlow.h:20