@next2d/builder 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +11 -0
  2. package/dist/index.js +247 -3
  3. package/package.json +11 -9
  4. package/templates/xbox/CMakeLists.txt +121 -0
  5. package/templates/xbox/MicrosoftGame.config +36 -0
  6. package/templates/xbox/README.md +319 -0
  7. package/templates/xbox/cmake/FetchDawn.cmake +42 -0
  8. package/templates/xbox/cmake/FindV8.cmake +62 -0
  9. package/templates/xbox/js/bootstrap.js +143 -0
  10. package/templates/xbox/js/selftest.js +515 -0
  11. package/templates/xbox/src/AssetLoader.cpp +106 -0
  12. package/templates/xbox/src/AssetLoader.h +35 -0
  13. package/templates/xbox/src/EventLoop.cpp +162 -0
  14. package/templates/xbox/src/EventLoop.h +73 -0
  15. package/templates/xbox/src/HostContext.h +58 -0
  16. package/templates/xbox/src/bindings/Audio.cpp +320 -0
  17. package/templates/xbox/src/bindings/Bindings.h +75 -0
  18. package/templates/xbox/src/bindings/Canvas.cpp +171 -0
  19. package/templates/xbox/src/bindings/Canvas2D.cpp +462 -0
  20. package/templates/xbox/src/bindings/Console.cpp +52 -0
  21. package/templates/xbox/src/bindings/DomShims.cpp +221 -0
  22. package/templates/xbox/src/bindings/EventTarget.cpp +127 -0
  23. package/templates/xbox/src/bindings/EventTarget.h +17 -0
  24. package/templates/xbox/src/bindings/Fetch.cpp +149 -0
  25. package/templates/xbox/src/bindings/Gamepad.cpp +79 -0
  26. package/templates/xbox/src/bindings/Image.cpp +160 -0
  27. package/templates/xbox/src/bindings/ImageSource.h +26 -0
  28. package/templates/xbox/src/bindings/Network.cpp +303 -0
  29. package/templates/xbox/src/bindings/RasterCore.h +265 -0
  30. package/templates/xbox/src/bindings/Timers.cpp +87 -0
  31. package/templates/xbox/src/bindings/Video.cpp +235 -0
  32. package/templates/xbox/src/bindings/webgpu/WebGPU.cpp +1318 -0
  33. package/templates/xbox/src/bindings/webgpu/WebGPUCommon.h +119 -0
  34. package/templates/xbox/src/bindings/webgpu/WebGPUEnums.h +245 -0
  35. package/templates/xbox/src/gpu/DawnContext.cpp +170 -0
  36. package/templates/xbox/src/gpu/DawnContext.h +64 -0
  37. package/templates/xbox/src/main.cpp +368 -0
  38. package/templates/xbox/src/platform/AudioEngine.cpp +184 -0
  39. package/templates/xbox/src/platform/AudioEngine.h +65 -0
  40. package/templates/xbox/src/platform/GamepadManager.cpp +83 -0
  41. package/templates/xbox/src/platform/GamepadManager.h +40 -0
  42. package/templates/xbox/src/platform/ImageTypes.h +17 -0
  43. package/templates/xbox/src/platform/TextRasterizer.cpp +183 -0
  44. package/templates/xbox/src/platform/TextRasterizer.h +35 -0
  45. package/templates/xbox/src/platform/WicDecoder.cpp +63 -0
  46. package/templates/xbox/src/platform/WicDecoder.h +16 -0
  47. package/templates/xbox/src/v8/V8Runtime.cpp +284 -0
  48. package/templates/xbox/src/v8/V8Runtime.h +68 -0
  49. package/templates/xbox/src/v8/V8Util.h +56 -0
  50. package/templates/xbox/src/worker/WorkerRuntime.cpp +453 -0
  51. package/templates/xbox/src/worker/WorkerRuntime.h +108 -0
  52. package/templates/xbox/tests/check_local.sh +56 -0
  53. package/templates/xbox/tests/raster_test.bin +0 -0
  54. package/templates/xbox/tests/raster_test.cpp +279 -0
  55. package/templates/xbox/tests/windows_platform_test.cpp +187 -0
@@ -0,0 +1,119 @@
1
+ // WebGPU バインディング共通基盤。
2
+ //
3
+ // wgpu:: (Dawn C++) ハンドルを V8 オブジェクトへラップする汎用機構と、
4
+ // JS ディスクリプタ(プレーンオブジェクト)を wgpu 構造体へ変換するヘルパーを提供する。
5
+ //
6
+ // 【重要・実装範囲について】
7
+ // WebGPU の IDL 全面(約40型・数百メソッド・全ディスクリプタ)を手書きするのは膨大なため、
8
+ // 本バインディングは Next2D の WebGPU レンダラが使用するコア経路
9
+ // (adapter/device/queue/buffer/texture/sampler/shader/bindGroup(Layout)/
10
+ // pipelineLayout/renderPipeline/commandEncoder/renderPass/canvasContext) を実装する。
11
+ // 追加のメソッド/ディスクリプタ項目は各 Create* 付近の «EXTEND» コメントに沿って拡張する。
12
+ #pragma once
13
+
14
+ #include <webgpu/webgpu_cpp.h>
15
+ #include <v8.h>
16
+
17
+ #include "v8/V8Util.h"
18
+
19
+ #include <memory>
20
+ #include <string>
21
+
22
+ namespace next2d::webgpu {
23
+
24
+ // wgpu ハンドルの寿命を JS オブジェクトに紐づけるためのホルダ。
25
+ template <typename T>
26
+ struct Holder {
27
+ T handle;
28
+ };
29
+
30
+ template <typename T>
31
+ void ReleaseHolder(const v8::WeakCallbackInfo<Holder<T>>& info)
32
+ {
33
+ delete info.GetParameter();
34
+ }
35
+
36
+ // wgpu ハンドル T を、指定 ObjectTemplate から生成したオブジェクトにラップする。
37
+ template <typename T>
38
+ v8::Local<v8::Object> Wrap(v8::Isolate* isolate,
39
+ v8::Local<v8::ObjectTemplate> tmpl,
40
+ T handle)
41
+ {
42
+ v8::Local<v8::Context> ctx = isolate->GetCurrentContext();
43
+ v8::Local<v8::Object> obj = tmpl->NewInstance(ctx).ToLocalChecked();
44
+ auto* holder = new Holder<T>{std::move(handle)};
45
+ obj->SetInternalField(0, v8::External::New(isolate, holder));
46
+ auto* global = new v8::Global<v8::Object>(isolate, obj);
47
+ global->SetWeak(holder, ReleaseHolder<T>, v8::WeakCallbackType::kParameter);
48
+ return obj;
49
+ }
50
+
51
+ // ラップされたオブジェクトから wgpu ハンドルを取り出す。
52
+ template <typename T>
53
+ T& Unwrap(v8::Local<v8::Object> obj)
54
+ {
55
+ auto* holder = static_cast<Holder<T>*>(
56
+ obj->GetInternalField(0).As<v8::External>()->Value());
57
+ return holder->handle;
58
+ }
59
+
60
+ // 内部フィールド 1 個の ObjectTemplate を作る。
61
+ inline v8::Local<v8::ObjectTemplate> HandleTemplate(v8::Isolate* isolate)
62
+ {
63
+ v8::Local<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(isolate);
64
+ tmpl->SetInternalFieldCount(1);
65
+ return tmpl;
66
+ }
67
+
68
+ // --- JS オブジェクト読み取りヘルパー ------------------------------------
69
+ inline v8::Local<v8::Value> Prop(v8::Isolate* isolate, v8::Local<v8::Object> obj,
70
+ const char* key)
71
+ {
72
+ v8::Local<v8::Context> ctx = isolate->GetCurrentContext();
73
+ v8::Local<v8::Value> v;
74
+ if (obj->Get(ctx, v8util::Str(isolate, key)).ToLocal(&v)) {
75
+ return v;
76
+ }
77
+ return v8::Undefined(isolate);
78
+ }
79
+
80
+ inline bool HasProp(v8::Isolate* isolate, v8::Local<v8::Object> obj, const char* key)
81
+ {
82
+ v8::Local<v8::Value> v = Prop(isolate, obj, key);
83
+ return !v->IsUndefined() && !v->IsNull();
84
+ }
85
+
86
+ inline uint32_t U32(v8::Isolate* isolate, v8::Local<v8::Object> obj,
87
+ const char* key, uint32_t fallback = 0)
88
+ {
89
+ v8::Local<v8::Value> v = Prop(isolate, obj, key);
90
+ return v->IsNumber() ? static_cast<uint32_t>(v.As<v8::Number>()->Value()) : fallback;
91
+ }
92
+
93
+ inline uint64_t U64(v8::Isolate* isolate, v8::Local<v8::Object> obj,
94
+ const char* key, uint64_t fallback = 0)
95
+ {
96
+ v8::Local<v8::Value> v = Prop(isolate, obj, key);
97
+ return v->IsNumber() ? static_cast<uint64_t>(v.As<v8::Number>()->Value()) : fallback;
98
+ }
99
+
100
+ inline double F64(v8::Isolate* isolate, v8::Local<v8::Object> obj,
101
+ const char* key, double fallback = 0.0)
102
+ {
103
+ v8::Local<v8::Value> v = Prop(isolate, obj, key);
104
+ return v->IsNumber() ? v.As<v8::Number>()->Value() : fallback;
105
+ }
106
+
107
+ inline bool Bool(v8::Isolate* isolate, v8::Local<v8::Object> obj,
108
+ const char* key, bool fallback = false)
109
+ {
110
+ v8::Local<v8::Value> v = Prop(isolate, obj, key);
111
+ return v->IsBoolean() ? v.As<v8::Boolean>()->Value() : fallback;
112
+ }
113
+
114
+ inline std::string Str(v8::Isolate* isolate, v8::Local<v8::Object> obj, const char* key)
115
+ {
116
+ return v8util::ToStdString(isolate, Prop(isolate, obj, key));
117
+ }
118
+
119
+ } // namespace next2d::webgpu
@@ -0,0 +1,245 @@
1
+ // WebGPU の文字列 enum を wgpu:: enum へ変換するヘルパー。
2
+ // 2D レンダラ(Next2D)が使用する頻出値を網羅する。未知値は既定へフォールバックする。
3
+ // «EXTEND» 追加フォーマット/enum が必要な場合はここに追記する。
4
+ #pragma once
5
+
6
+ #include <webgpu/webgpu_cpp.h>
7
+ #include <string>
8
+ #include <string_view>
9
+
10
+ namespace next2d::webgpu {
11
+
12
+ inline wgpu::TextureFormat ToTextureFormat(std::string_view s)
13
+ {
14
+ using F = wgpu::TextureFormat;
15
+ if (s == "rgba8unorm") return F::RGBA8Unorm;
16
+ if (s == "rgba8unorm-srgb") return F::RGBA8UnormSrgb;
17
+ if (s == "bgra8unorm") return F::BGRA8Unorm;
18
+ if (s == "bgra8unorm-srgb") return F::BGRA8UnormSrgb;
19
+ if (s == "rgba8snorm") return F::RGBA8Snorm;
20
+ if (s == "r8unorm") return F::R8Unorm;
21
+ if (s == "rg8unorm") return F::RG8Unorm;
22
+ if (s == "r16float") return F::R16Float;
23
+ if (s == "rg16float") return F::RG16Float;
24
+ if (s == "rgba16float") return F::RGBA16Float;
25
+ if (s == "r32float") return F::R32Float;
26
+ if (s == "rg32float") return F::RG32Float;
27
+ if (s == "rgba32float") return F::RGBA32Float;
28
+ if (s == "depth16unorm") return F::Depth16Unorm;
29
+ if (s == "depth24plus") return F::Depth24Plus;
30
+ if (s == "depth24plus-stencil8") return F::Depth24PlusStencil8;
31
+ if (s == "depth32float") return F::Depth32Float;
32
+ if (s == "stencil8") return F::Stencil8;
33
+ return F::RGBA8Unorm;
34
+ }
35
+
36
+ inline wgpu::VertexFormat ToVertexFormat(std::string_view s)
37
+ {
38
+ using V = wgpu::VertexFormat;
39
+ if (s == "float32") return V::Float32;
40
+ if (s == "float32x2") return V::Float32x2;
41
+ if (s == "float32x3") return V::Float32x3;
42
+ if (s == "float32x4") return V::Float32x4;
43
+ if (s == "uint32") return V::Uint32;
44
+ if (s == "uint32x2") return V::Uint32x2;
45
+ if (s == "uint32x3") return V::Uint32x3;
46
+ if (s == "uint32x4") return V::Uint32x4;
47
+ if (s == "sint32") return V::Sint32;
48
+ if (s == "sint32x2") return V::Sint32x2;
49
+ if (s == "uint8x2") return V::Uint8x2;
50
+ if (s == "uint8x4") return V::Uint8x4;
51
+ if (s == "unorm8x2") return V::Unorm8x2;
52
+ if (s == "unorm8x4") return V::Unorm8x4;
53
+ if (s == "uint16x2") return V::Uint16x2;
54
+ if (s == "uint16x4") return V::Uint16x4;
55
+ if (s == "unorm16x2") return V::Unorm16x2;
56
+ if (s == "unorm16x4") return V::Unorm16x4;
57
+ if (s == "float16x2") return V::Float16x2;
58
+ if (s == "float16x4") return V::Float16x4;
59
+ return V::Float32;
60
+ }
61
+
62
+ inline wgpu::PrimitiveTopology ToTopology(std::string_view s)
63
+ {
64
+ using T = wgpu::PrimitiveTopology;
65
+ if (s == "point-list") return T::PointList;
66
+ if (s == "line-list") return T::LineList;
67
+ if (s == "line-strip") return T::LineStrip;
68
+ if (s == "triangle-list") return T::TriangleList;
69
+ if (s == "triangle-strip") return T::TriangleStrip;
70
+ return T::TriangleList;
71
+ }
72
+
73
+ inline wgpu::IndexFormat ToIndexFormat(std::string_view s)
74
+ {
75
+ if (s == "uint16") return wgpu::IndexFormat::Uint16;
76
+ if (s == "uint32") return wgpu::IndexFormat::Uint32;
77
+ return wgpu::IndexFormat::Undefined;
78
+ }
79
+
80
+ inline wgpu::BlendFactor ToBlendFactor(std::string_view s)
81
+ {
82
+ using B = wgpu::BlendFactor;
83
+ if (s == "zero") return B::Zero;
84
+ if (s == "one") return B::One;
85
+ if (s == "src") return B::Src;
86
+ if (s == "one-minus-src") return B::OneMinusSrc;
87
+ if (s == "src-alpha") return B::SrcAlpha;
88
+ if (s == "one-minus-src-alpha") return B::OneMinusSrcAlpha;
89
+ if (s == "dst") return B::Dst;
90
+ if (s == "one-minus-dst") return B::OneMinusDst;
91
+ if (s == "dst-alpha") return B::DstAlpha;
92
+ if (s == "one-minus-dst-alpha") return B::OneMinusDstAlpha;
93
+ if (s == "src-alpha-saturated") return B::SrcAlphaSaturated;
94
+ if (s == "constant") return B::Constant;
95
+ if (s == "one-minus-constant") return B::OneMinusConstant;
96
+ return B::One;
97
+ }
98
+
99
+ inline wgpu::BlendOperation ToBlendOperation(std::string_view s)
100
+ {
101
+ using O = wgpu::BlendOperation;
102
+ if (s == "add") return O::Add;
103
+ if (s == "subtract") return O::Subtract;
104
+ if (s == "reverse-subtract") return O::ReverseSubtract;
105
+ if (s == "min") return O::Min;
106
+ if (s == "max") return O::Max;
107
+ return O::Add;
108
+ }
109
+
110
+ inline wgpu::LoadOp ToLoadOp(std::string_view s)
111
+ {
112
+ if (s == "load") return wgpu::LoadOp::Load;
113
+ if (s == "clear") return wgpu::LoadOp::Clear;
114
+ return wgpu::LoadOp::Clear;
115
+ }
116
+
117
+ inline wgpu::StoreOp ToStoreOp(std::string_view s)
118
+ {
119
+ if (s == "store") return wgpu::StoreOp::Store;
120
+ if (s == "discard") return wgpu::StoreOp::Discard;
121
+ return wgpu::StoreOp::Store;
122
+ }
123
+
124
+ inline wgpu::CompareFunction ToCompareFunction(std::string_view s)
125
+ {
126
+ using C = wgpu::CompareFunction;
127
+ if (s == "never") return C::Never;
128
+ if (s == "less") return C::Less;
129
+ if (s == "equal") return C::Equal;
130
+ if (s == "less-equal") return C::LessEqual;
131
+ if (s == "greater") return C::Greater;
132
+ if (s == "not-equal") return C::NotEqual;
133
+ if (s == "greater-equal") return C::GreaterEqual;
134
+ if (s == "always") return C::Always;
135
+ return C::Always;
136
+ }
137
+
138
+ inline wgpu::StencilOperation ToStencilOperation(std::string_view s)
139
+ {
140
+ using O = wgpu::StencilOperation;
141
+ if (s == "keep") return O::Keep;
142
+ if (s == "zero") return O::Zero;
143
+ if (s == "replace") return O::Replace;
144
+ if (s == "invert") return O::Invert;
145
+ if (s == "increment-clamp") return O::IncrementClamp;
146
+ if (s == "decrement-clamp") return O::DecrementClamp;
147
+ if (s == "increment-wrap") return O::IncrementWrap;
148
+ if (s == "decrement-wrap") return O::DecrementWrap;
149
+ return O::Keep;
150
+ }
151
+
152
+ inline wgpu::CullMode ToCullMode(std::string_view s)
153
+ {
154
+ if (s == "none") return wgpu::CullMode::None;
155
+ if (s == "front") return wgpu::CullMode::Front;
156
+ if (s == "back") return wgpu::CullMode::Back;
157
+ return wgpu::CullMode::None;
158
+ }
159
+
160
+ inline wgpu::FrontFace ToFrontFace(std::string_view s)
161
+ {
162
+ if (s == "ccw") return wgpu::FrontFace::CCW;
163
+ if (s == "cw") return wgpu::FrontFace::CW;
164
+ return wgpu::FrontFace::CCW;
165
+ }
166
+
167
+ inline wgpu::AddressMode ToAddressMode(std::string_view s)
168
+ {
169
+ if (s == "clamp-to-edge") return wgpu::AddressMode::ClampToEdge;
170
+ if (s == "repeat") return wgpu::AddressMode::Repeat;
171
+ if (s == "mirror-repeat") return wgpu::AddressMode::MirrorRepeat;
172
+ return wgpu::AddressMode::ClampToEdge;
173
+ }
174
+
175
+ inline wgpu::FilterMode ToFilterMode(std::string_view s)
176
+ {
177
+ if (s == "nearest") return wgpu::FilterMode::Nearest;
178
+ if (s == "linear") return wgpu::FilterMode::Linear;
179
+ return wgpu::FilterMode::Nearest;
180
+ }
181
+
182
+ inline wgpu::MipmapFilterMode ToMipmapFilterMode(std::string_view s)
183
+ {
184
+ if (s == "nearest") return wgpu::MipmapFilterMode::Nearest;
185
+ if (s == "linear") return wgpu::MipmapFilterMode::Linear;
186
+ return wgpu::MipmapFilterMode::Nearest;
187
+ }
188
+
189
+ inline wgpu::VertexStepMode ToStepMode(std::string_view s)
190
+ {
191
+ if (s == "vertex") return wgpu::VertexStepMode::Vertex;
192
+ if (s == "instance") return wgpu::VertexStepMode::Instance;
193
+ return wgpu::VertexStepMode::Vertex;
194
+ }
195
+
196
+ inline wgpu::TextureDimension ToTextureDimension(std::string_view s)
197
+ {
198
+ if (s == "1d") return wgpu::TextureDimension::e1D;
199
+ if (s == "2d") return wgpu::TextureDimension::e2D;
200
+ if (s == "3d") return wgpu::TextureDimension::e3D;
201
+ return wgpu::TextureDimension::e2D;
202
+ }
203
+
204
+ inline wgpu::TextureViewDimension ToTextureViewDimension(std::string_view s)
205
+ {
206
+ using D = wgpu::TextureViewDimension;
207
+ if (s == "1d") return D::e1D;
208
+ if (s == "2d") return D::e2D;
209
+ if (s == "2d-array") return D::e2DArray;
210
+ if (s == "cube") return D::Cube;
211
+ if (s == "cube-array") return D::CubeArray;
212
+ if (s == "3d") return D::e3D;
213
+ return D::Undefined;
214
+ }
215
+
216
+ inline wgpu::BufferBindingType ToBufferBindingType(std::string_view s)
217
+ {
218
+ using B = wgpu::BufferBindingType;
219
+ if (s == "uniform") return B::Uniform;
220
+ if (s == "storage") return B::Storage;
221
+ if (s == "read-only-storage") return B::ReadOnlyStorage;
222
+ return B::Uniform;
223
+ }
224
+
225
+ inline wgpu::SamplerBindingType ToSamplerBindingType(std::string_view s)
226
+ {
227
+ using S = wgpu::SamplerBindingType;
228
+ if (s == "filtering") return S::Filtering;
229
+ if (s == "non-filtering") return S::NonFiltering;
230
+ if (s == "comparison") return S::Comparison;
231
+ return S::Filtering;
232
+ }
233
+
234
+ inline wgpu::TextureSampleType ToTextureSampleType(std::string_view s)
235
+ {
236
+ using T = wgpu::TextureSampleType;
237
+ if (s == "float") return T::Float;
238
+ if (s == "unfilterable-float") return T::UnfilterableFloat;
239
+ if (s == "depth") return T::Depth;
240
+ if (s == "sint") return T::Sint;
241
+ if (s == "uint") return T::Uint;
242
+ return T::Float;
243
+ }
244
+
245
+ } // namespace next2d::webgpu
@@ -0,0 +1,170 @@
1
+ #include "DawnContext.h"
2
+
3
+ #include <Windows.h>
4
+ #include <dawn/native/DawnNative.h>
5
+ #include <iostream>
6
+
7
+ namespace next2d {
8
+
9
+ namespace {
10
+
11
+ void HandleDeviceLost(const wgpu::Device&, wgpu::DeviceLostReason reason, wgpu::StringView message)
12
+ {
13
+ std::cerr << "[Dawn] Device lost (" << static_cast<int>(reason) << "): "
14
+ << std::string(message.data, message.length) << std::endl;
15
+ }
16
+
17
+ void HandleUncapturedError(const wgpu::Device&, wgpu::ErrorType type, wgpu::StringView message)
18
+ {
19
+ std::cerr << "[Dawn] Uncaptured error (" << static_cast<int>(type) << "): "
20
+ << std::string(message.data, message.length) << std::endl;
21
+ }
22
+
23
+ } // namespace
24
+
25
+ DawnContext::~DawnContext() = default;
26
+
27
+ bool DawnContext::Initialize(HWND hwnd, uint32_t width, uint32_t height)
28
+ {
29
+ hwnd_ = hwnd;
30
+ width_ = width;
31
+ height_ = height;
32
+
33
+ // タイムドウェイト非依存のコールバックを扱えるよう ProcessEvents を有効化
34
+ wgpu::InstanceDescriptor instance_desc = {};
35
+ static const wgpu::InstanceFeatureName kFeatures[] = {
36
+ wgpu::InstanceFeatureName::TimedWaitAny,
37
+ };
38
+ instance_desc.requiredFeatureCount = 1;
39
+ instance_desc.requiredFeatures = kFeatures;
40
+
41
+ instance_ = wgpu::CreateInstance(&instance_desc);
42
+ if (!instance_) {
43
+ std::cerr << "[Dawn] Failed to create instance" << std::endl;
44
+ return false;
45
+ }
46
+
47
+ // HWND から surface を生成 (D3D12)
48
+ wgpu::SurfaceSourceWindowsHWND hwnd_source = {};
49
+ hwnd_source.hinstance = GetModuleHandle(nullptr);
50
+ hwnd_source.hwnd = hwnd_;
51
+
52
+ wgpu::SurfaceDescriptor surface_desc = {};
53
+ surface_desc.nextInChain = &hwnd_source;
54
+
55
+ surface_ = instance_.CreateSurface(&surface_desc);
56
+ if (!surface_) {
57
+ std::cerr << "[Dawn] Failed to create surface" << std::endl;
58
+ return false;
59
+ }
60
+
61
+ return true;
62
+ }
63
+
64
+ bool DawnContext::AcquireDevice()
65
+ {
66
+ if (device_) {
67
+ return true;
68
+ }
69
+
70
+ // Adapter を要求 (D3D12 / HighPerformance)
71
+ wgpu::RequestAdapterOptions adapter_opts = {};
72
+ adapter_opts.backendType = wgpu::BackendType::D3D12;
73
+ adapter_opts.powerPreference = wgpu::PowerPreference::HighPerformance;
74
+ adapter_opts.compatibleSurface = surface_;
75
+
76
+ wgpu::Future adapter_future = instance_.RequestAdapter(
77
+ &adapter_opts,
78
+ wgpu::CallbackMode::AllowProcessEvents,
79
+ [this](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter,
80
+ wgpu::StringView message) {
81
+ if (status != wgpu::RequestAdapterStatus::Success) {
82
+ std::cerr << "[Dawn] RequestAdapter failed: "
83
+ << std::string(message.data, message.length) << std::endl;
84
+ return;
85
+ }
86
+ adapter_ = std::move(adapter);
87
+ });
88
+
89
+ instance_.WaitAny(adapter_future, UINT64_MAX);
90
+ if (!adapter_) {
91
+ return false;
92
+ }
93
+
94
+ // Device を要求
95
+ wgpu::DeviceDescriptor device_desc = {};
96
+ device_desc.SetDeviceLostCallback(wgpu::CallbackMode::AllowProcessEvents, HandleDeviceLost);
97
+ device_desc.SetUncapturedErrorCallback(HandleUncapturedError);
98
+
99
+ wgpu::Future device_future = adapter_.RequestDevice(
100
+ &device_desc,
101
+ wgpu::CallbackMode::AllowProcessEvents,
102
+ [this](wgpu::RequestDeviceStatus status, wgpu::Device device,
103
+ wgpu::StringView message) {
104
+ if (status != wgpu::RequestDeviceStatus::Success) {
105
+ std::cerr << "[Dawn] RequestDevice failed: "
106
+ << std::string(message.data, message.length) << std::endl;
107
+ return;
108
+ }
109
+ device_ = std::move(device);
110
+ });
111
+
112
+ instance_.WaitAny(device_future, UINT64_MAX);
113
+ if (!device_) {
114
+ return false;
115
+ }
116
+
117
+ // surface の優先フォーマットを取得
118
+ wgpu::SurfaceCapabilities caps = {};
119
+ surface_.GetCapabilities(adapter_, &caps);
120
+ if (caps.formatCount > 0) {
121
+ format_ = caps.formats[0];
122
+ }
123
+
124
+ Configure(width_, height_);
125
+ return true;
126
+ }
127
+
128
+ void DawnContext::Configure(uint32_t width, uint32_t height)
129
+ {
130
+ if (!device_ || width == 0 || height == 0) {
131
+ return;
132
+ }
133
+
134
+ width_ = width;
135
+ height_ = height;
136
+
137
+ wgpu::SurfaceConfiguration config = {};
138
+ config.device = device_;
139
+ config.format = format_;
140
+ config.usage = wgpu::TextureUsage::RenderAttachment;
141
+ config.width = width_;
142
+ config.height = height_;
143
+ config.presentMode = wgpu::PresentMode::Fifo;
144
+ config.alphaMode = wgpu::CompositeAlphaMode::Opaque;
145
+
146
+ surface_.Configure(&config);
147
+ }
148
+
149
+ wgpu::Texture DawnContext::GetCurrentTexture()
150
+ {
151
+ wgpu::SurfaceTexture surface_texture = {};
152
+ surface_.GetCurrentTexture(&surface_texture);
153
+ return surface_texture.texture;
154
+ }
155
+
156
+ void DawnContext::Present()
157
+ {
158
+ if (surface_) {
159
+ surface_.Present();
160
+ }
161
+ }
162
+
163
+ void DawnContext::Tick()
164
+ {
165
+ if (instance_) {
166
+ instance_.ProcessEvents();
167
+ }
168
+ }
169
+
170
+ } // namespace next2d
@@ -0,0 +1,64 @@
1
+ // DawnContext: Dawn(WebGPU) の Instance / Adapter / Device / Surface を管理する。
2
+ //
3
+ // GDK ウィンドウ(HWND) から D3D12 バックエンドの surface を生成し、
4
+ // スワップチェーンとして提示する。JS 側の navigator.gpu / canvas.getContext('webgpu')
5
+ // はこのコンテキストが保持する Device / Surface を参照する。
6
+ #pragma once
7
+
8
+ #include <webgpu/webgpu_cpp.h>
9
+
10
+ #include <cstdint>
11
+ #include <functional>
12
+
13
+ struct HWND__;
14
+ using HWND = HWND__*;
15
+
16
+ namespace next2d {
17
+
18
+ class DawnContext {
19
+ public:
20
+ DawnContext() = default;
21
+ ~DawnContext();
22
+
23
+ // Instance と HWND 由来 surface を生成する。
24
+ bool Initialize(HWND hwnd, uint32_t width, uint32_t height);
25
+
26
+ // Adapter/Device を同期的に取得する (内部で ProcessEvents をポンプして待つ)。
27
+ // navigator.gpu.requestAdapter/requestDevice からも利用する。
28
+ bool AcquireDevice();
29
+
30
+ // surface をデバイスに紐付けて構成する (canvas サイズ変更時に再呼び出し)。
31
+ void Configure(uint32_t width, uint32_t height);
32
+
33
+ // 今フレームのバックバッファテクスチャを取得する。
34
+ wgpu::Texture GetCurrentTexture();
35
+
36
+ // 提示する。
37
+ void Present();
38
+
39
+ // Dawn の非同期イベント(コールバック)を進める。毎フレーム呼ぶ。
40
+ void Tick();
41
+
42
+ const wgpu::Instance& instance() const { return instance_; }
43
+ const wgpu::Adapter& adapter() const { return adapter_; }
44
+ const wgpu::Device& device() const { return device_; }
45
+ const wgpu::Surface& surface() const { return surface_; }
46
+ wgpu::TextureFormat format() const { return format_; }
47
+
48
+ uint32_t width() const { return width_; }
49
+ uint32_t height() const { return height_; }
50
+ bool ready() const { return device_ != nullptr; }
51
+
52
+ private:
53
+ wgpu::Instance instance_ = nullptr;
54
+ wgpu::Adapter adapter_ = nullptr;
55
+ wgpu::Device device_ = nullptr;
56
+ wgpu::Surface surface_ = nullptr;
57
+ wgpu::TextureFormat format_ = wgpu::TextureFormat::BGRA8Unorm;
58
+
59
+ HWND hwnd_ = nullptr;
60
+ uint32_t width_ = 0;
61
+ uint32_t height_ = 0;
62
+ };
63
+
64
+ } // namespace next2d