react-native-wgpu 0.1.5 → 0.1.7

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 (168) hide show
  1. package/README.md +43 -42
  2. package/android/cpp/AndroidPlatformContext.h +2 -1
  3. package/cpp/jsi/RNFJSIConverter.h +10 -0
  4. package/cpp/rnwgpu/SurfaceRegistry.h +1 -2
  5. package/cpp/rnwgpu/api/Canvas.h +3 -3
  6. package/cpp/rnwgpu/api/GPUBuffer.h +1 -1
  7. package/ios/WebGPUView.mm +14 -1
  8. package/lib/commonjs/Canvas.js.map +1 -1
  9. package/lib/commonjs/utils.js +2 -2
  10. package/lib/commonjs/utils.js.map +1 -1
  11. package/lib/module/Canvas.js.map +1 -1
  12. package/lib/module/utils.js +2 -2
  13. package/lib/module/utils.js.map +1 -1
  14. package/lib/typescript/lib/commonjs/utils.d.ts +1 -1
  15. package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -1
  16. package/lib/typescript/lib/module/utils.d.ts +1 -1
  17. package/lib/typescript/lib/module/utils.d.ts.map +1 -1
  18. package/lib/typescript/src/Canvas.d.ts +3 -2
  19. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  20. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +2 -0
  21. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +1 -0
  22. package/lib/typescript/src/__tests__/Buffer.spec.d.ts +2 -0
  23. package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +1 -0
  24. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +2 -0
  25. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +1 -0
  26. package/lib/typescript/src/__tests__/Constants.spec.d.ts +2 -0
  27. package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +1 -0
  28. package/lib/typescript/src/__tests__/Device.spec.d.ts +2 -0
  29. package/lib/typescript/src/__tests__/Device.spec.d.ts.map +1 -0
  30. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +2 -0
  31. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +1 -0
  32. package/lib/typescript/src/__tests__/GPU.spec.d.ts +2 -0
  33. package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +1 -0
  34. package/lib/typescript/src/__tests__/ImageData.spec.d.ts +2 -0
  35. package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +1 -0
  36. package/lib/typescript/src/__tests__/Shaders.spec.d.ts +2 -0
  37. package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +1 -0
  38. package/lib/typescript/src/__tests__/Texture.spec.d.ts +2 -0
  39. package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +1 -0
  40. package/lib/typescript/src/__tests__/components/DrawingContext.d.ts +12 -0
  41. package/lib/typescript/src/__tests__/components/DrawingContext.d.ts.map +1 -0
  42. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +3 -0
  43. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +1 -0
  44. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +29 -0
  45. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +1 -0
  46. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +5 -0
  47. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +1 -0
  48. package/lib/typescript/src/__tests__/components/cube.d.ts +7 -0
  49. package/lib/typescript/src/__tests__/components/cube.d.ts.map +1 -0
  50. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +22 -0
  51. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +1 -0
  52. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +12 -0
  53. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +1 -0
  54. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +7 -0
  55. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +1 -0
  56. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +6 -0
  57. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +1 -0
  58. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +6 -0
  59. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +1 -0
  60. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +10 -0
  61. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +1 -0
  62. package/lib/typescript/src/__tests__/components/triangle.d.ts +3 -0
  63. package/lib/typescript/src/__tests__/components/triangle.d.ts.map +1 -0
  64. package/lib/typescript/src/__tests__/config.d.ts +3 -0
  65. package/lib/typescript/src/__tests__/config.d.ts.map +1 -0
  66. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +2 -0
  67. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +1 -0
  68. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +2 -0
  69. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +1 -0
  70. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +2 -0
  71. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +1 -0
  72. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +2 -0
  73. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +1 -0
  74. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +2 -0
  75. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +1 -0
  76. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +2 -0
  77. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +1 -0
  78. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +2 -0
  79. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +1 -0
  80. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +2 -0
  81. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +1 -0
  82. package/lib/typescript/src/__tests__/globalSetup.d.ts +10 -0
  83. package/lib/typescript/src/__tests__/globalSetup.d.ts.map +1 -0
  84. package/lib/typescript/src/__tests__/globalTeardown.d.ts +3 -0
  85. package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +1 -0
  86. package/lib/typescript/src/__tests__/setup.d.ts +62 -0
  87. package/lib/typescript/src/__tests__/setup.d.ts.map +1 -0
  88. package/lib/typescript/src/utils.d.ts +2 -1
  89. package/lib/typescript/src/utils.d.ts.map +1 -1
  90. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  91. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  92. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  93. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  94. package/libs/{ios/libwebgpu_dawn.xcframework/ios-arm64 → apple/arm64_iphoneos}/libwebgpu_dawn.a +0 -0
  95. package/libs/{ios → apple}/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  96. package/libs/{ios → apple}/arm64_xros/libwebgpu_dawn.a +0 -0
  97. package/libs/{ios → apple}/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  98. package/libs/{ios → apple}/libwebgpu_dawn.a +0 -0
  99. package/libs/{ios/arm64_iphoneos → apple/libwebgpu_dawn.xcframework/ios-arm64}/libwebgpu_dawn.a +0 -0
  100. package/libs/{ios → apple}/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  101. package/libs/apple/libwebgpu_dawn_macosx.xcframework/Info.plist +28 -0
  102. package/libs/apple/libwebgpu_dawn_macosx.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  103. package/libs/{ios/libwebgpu_dawn_visionos.xcframework/xros-arm64_x86_64-simulator → apple}/libwebgpu_dawn_visionos.a +0 -0
  104. package/libs/{ios → apple}/libwebgpu_dawn_visionos.xcframework/Info.plist +2 -2
  105. package/libs/{ios/arm64_xros/libwebgpu_dawn_visionos.a → apple/libwebgpu_dawn_visionos.xcframework/xros-arm64/libwebgpu_dawn.a} +0 -0
  106. package/libs/{ios → apple/libwebgpu_dawn_visionos.xcframework/xros-arm64_x86_64-simulator}/libwebgpu_dawn_visionos.a +0 -0
  107. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  108. package/libs/{ios → apple}/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  109. package/libs/{ios → apple}/x86_64_xrsimulator/libwebgpu_dawn.a +0 -0
  110. package/package.json +23 -22
  111. package/react-native-wgpu.podspec +2 -2
  112. package/src/Canvas.tsx +4 -2
  113. package/src/__tests__/ArrayBuffer.spec.ts +76 -0
  114. package/src/__tests__/Buffer.spec.ts +357 -0
  115. package/src/__tests__/ComputeShader.spec.ts +375 -0
  116. package/src/__tests__/Constants.spec.ts +91 -0
  117. package/src/__tests__/Device.spec.ts +35 -0
  118. package/src/__tests__/ExternalTexture.spec.ts +284 -0
  119. package/src/__tests__/GPU.spec.ts +229 -0
  120. package/src/__tests__/ImageData.spec.ts +26 -0
  121. package/src/__tests__/Shaders.spec.ts +232 -0
  122. package/src/__tests__/Texture.spec.ts +191 -0
  123. package/src/__tests__/assets/Di-3d.png +0 -0
  124. package/src/__tests__/components/DrawingContext.ts +11 -0
  125. package/src/__tests__/components/Wireframe/Shaders.ts +138 -0
  126. package/src/__tests__/components/Wireframe/models.ts +113 -0
  127. package/src/__tests__/components/Wireframe/utils.ts +22 -0
  128. package/src/__tests__/components/cube.ts +51 -0
  129. package/src/__tests__/components/meshes/mesh.ts +96 -0
  130. package/src/__tests__/components/meshes/sphere.ts +103 -0
  131. package/src/__tests__/components/meshes/stanfordDragon.ts +44 -0
  132. package/src/__tests__/components/meshes/stanfordDragonData.ts +5 -0
  133. package/src/__tests__/components/meshes/teapot.ts +13 -0
  134. package/src/__tests__/components/meshes/utils.ts +235 -0
  135. package/src/__tests__/components/triangle.ts +17 -0
  136. package/src/__tests__/config.ts +2 -0
  137. package/src/__tests__/demos/ABuffer.spec.ts +885 -0
  138. package/src/__tests__/demos/Blur.spec.ts +397 -0
  139. package/src/__tests__/demos/Cube.spec.ts +925 -0
  140. package/src/__tests__/demos/FractalCube.spec.ts +239 -0
  141. package/src/__tests__/demos/OcclusionQuery.spec.ts +376 -0
  142. package/src/__tests__/demos/RenderBundles.spec.ts +579 -0
  143. package/src/__tests__/demos/Triangle.spec.ts +248 -0
  144. package/src/__tests__/demos/Wireframe.spec.ts +190 -0
  145. package/src/__tests__/globalSetup.ts +54 -0
  146. package/src/__tests__/globalTeardown.ts +11 -0
  147. package/src/__tests__/setup.ts +409 -0
  148. package/src/__tests__/snapshots/abuffer.png +0 -0
  149. package/src/__tests__/snapshots/asteroid.png +0 -0
  150. package/src/__tests__/snapshots/blur.png +0 -0
  151. package/src/__tests__/snapshots/buffer.png +0 -0
  152. package/src/__tests__/snapshots/constant-triangle.png +0 -0
  153. package/src/__tests__/snapshots/cube.png +0 -0
  154. package/src/__tests__/snapshots/f.png +0 -0
  155. package/src/__tests__/snapshots/f2.png +0 -0
  156. package/src/__tests__/snapshots/fractal-cubes.png +0 -0
  157. package/src/__tests__/snapshots/instanced-cubes.png +0 -0
  158. package/src/__tests__/snapshots/occlusion-query.png +0 -0
  159. package/src/__tests__/snapshots/ref.png +0 -0
  160. package/src/__tests__/snapshots/texture.png +0 -0
  161. package/src/__tests__/snapshots/textured-cube.png +0 -0
  162. package/src/__tests__/snapshots/triangle-msaa.png +0 -0
  163. package/src/__tests__/snapshots/triangle.png +0 -0
  164. package/src/__tests__/snapshots/two-cube.png +0 -0
  165. package/src/utils.ts +3 -1
  166. package/android/gradle.properties +0 -5
  167. package/libs/ios/libwebgpu_dawn_visionos.xcframework/xros-arm64/libwebgpu_dawn_visionos.a +0 -0
  168. package/libs/{ios → apple}/libwebgpu_dawn.xcframework/Info.plist +5 -5
@@ -0,0 +1,248 @@
1
+ import { checkImage, client, encodeImage } from "../setup";
2
+
3
+ describe("Triangle", () => {
4
+ it("Simple Triangle", async () => {
5
+ const result = await client.eval(
6
+ ({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu, ctx }) => {
7
+ const pipeline = device.createRenderPipeline({
8
+ layout: "auto",
9
+ vertex: {
10
+ module: device.createShaderModule({
11
+ code: triangleVertWGSL,
12
+ }),
13
+ },
14
+ fragment: {
15
+ module: device.createShaderModule({
16
+ code: redFragWGSL,
17
+ }),
18
+ targets: [
19
+ {
20
+ format: gpu.getPreferredCanvasFormat(),
21
+ },
22
+ ],
23
+ },
24
+ primitive: {
25
+ topology: "triangle-list",
26
+ },
27
+ });
28
+
29
+ const commandEncoder = device.createCommandEncoder();
30
+ const textureView = ctx.getCurrentTexture().createView();
31
+
32
+ const renderPassDescriptor: GPURenderPassDescriptor = {
33
+ colorAttachments: [
34
+ {
35
+ view: textureView,
36
+ clearValue: [0.3, 0.6, 1, 1],
37
+ loadOp: "clear",
38
+ storeOp: "store",
39
+ },
40
+ ],
41
+ };
42
+
43
+ const passEncoder =
44
+ commandEncoder.beginRenderPass(renderPassDescriptor);
45
+ passEncoder.setPipeline(pipeline);
46
+ passEncoder.draw(3);
47
+ passEncoder.end();
48
+ device.queue.submit([commandEncoder.finish()]);
49
+ return ctx.getImageData();
50
+ },
51
+ );
52
+ const image = encodeImage(result);
53
+ checkImage(image, "snapshots/triangle.png");
54
+ });
55
+ it("Async Simple Triangle", async () => {
56
+ const result = await client.eval(
57
+ ({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu, ctx }) => {
58
+ return device
59
+ .createRenderPipelineAsync({
60
+ layout: "auto",
61
+ vertex: {
62
+ module: device.createShaderModule({
63
+ code: triangleVertWGSL,
64
+ }),
65
+ },
66
+ fragment: {
67
+ module: device.createShaderModule({
68
+ code: redFragWGSL,
69
+ }),
70
+ targets: [
71
+ {
72
+ format: gpu.getPreferredCanvasFormat(),
73
+ },
74
+ ],
75
+ },
76
+ primitive: {
77
+ topology: "triangle-list",
78
+ },
79
+ })
80
+ .then((pipeline) => {
81
+ const commandEncoder = device.createCommandEncoder();
82
+ const textureView = ctx.getCurrentTexture().createView();
83
+
84
+ const renderPassDescriptor: GPURenderPassDescriptor = {
85
+ colorAttachments: [
86
+ {
87
+ view: textureView,
88
+ clearValue: [0.3, 0.6, 1, 1],
89
+ loadOp: "clear",
90
+ storeOp: "store",
91
+ },
92
+ ],
93
+ };
94
+
95
+ const passEncoder =
96
+ commandEncoder.beginRenderPass(renderPassDescriptor);
97
+ passEncoder.setPipeline(pipeline);
98
+ passEncoder.draw(3);
99
+ passEncoder.end();
100
+ device.queue.submit([commandEncoder.finish()]);
101
+ return ctx.getImageData();
102
+ });
103
+ },
104
+ );
105
+ const image = encodeImage(result);
106
+ checkImage(image, "snapshots/triangle.png");
107
+ });
108
+ it("Triangle MSAA", async () => {
109
+ const result = await client.eval(
110
+ ({ device, shaders: { triangleVertWGSL, redFragWGSL }, gpu, ctx }) => {
111
+ const sampleCount = 4;
112
+ const presentationFormat = gpu.getPreferredCanvasFormat();
113
+ const pipeline = device.createRenderPipeline({
114
+ layout: "auto",
115
+ vertex: {
116
+ module: device.createShaderModule({
117
+ code: triangleVertWGSL,
118
+ }),
119
+ },
120
+ fragment: {
121
+ module: device.createShaderModule({
122
+ code: redFragWGSL,
123
+ }),
124
+ targets: [
125
+ {
126
+ format: presentationFormat,
127
+ },
128
+ ],
129
+ },
130
+ primitive: {
131
+ topology: "triangle-list",
132
+ },
133
+ multisample: {
134
+ count: sampleCount,
135
+ },
136
+ });
137
+
138
+ const texture = device.createTexture({
139
+ size: [ctx.width, ctx.height],
140
+ sampleCount,
141
+ format: presentationFormat,
142
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
143
+ });
144
+ const view = texture.createView();
145
+
146
+ const commandEncoder = device.createCommandEncoder();
147
+
148
+ const renderPassDescriptor: GPURenderPassDescriptor = {
149
+ colorAttachments: [
150
+ {
151
+ view,
152
+ resolveTarget: ctx.getCurrentTexture().createView(),
153
+ clearValue: [0, 0, 0, 1],
154
+ loadOp: "clear",
155
+ storeOp: "discard",
156
+ },
157
+ ],
158
+ };
159
+
160
+ const passEncoder =
161
+ commandEncoder.beginRenderPass(renderPassDescriptor);
162
+ passEncoder.setPipeline(pipeline);
163
+ passEncoder.draw(3);
164
+ passEncoder.end();
165
+ device.queue.submit([commandEncoder.finish()]);
166
+ return ctx.getImageData();
167
+ },
168
+ );
169
+ const image = encodeImage(result);
170
+ checkImage(image, "snapshots/triangle-msaa.png");
171
+ });
172
+ it("Triangle with constants", async () => {
173
+ const result = await client.eval(
174
+ ({ device, shaders: { triangleVertWGSL }, gpu, ctx }) => {
175
+ const pipeline = device.createRenderPipeline({
176
+ layout: "auto",
177
+ vertex: {
178
+ module: device.createShaderModule({
179
+ code: triangleVertWGSL,
180
+ }),
181
+ },
182
+ fragment: {
183
+ module: device.createShaderModule({
184
+ code: `
185
+ @vertex fn vs(
186
+ @builtin(vertex_index) vertexIndex : u32
187
+ ) -> @builtin(position) vec4f {
188
+ let pos = array(
189
+ vec2f( 0.0, 0.5), // top center
190
+ vec2f(-0.5, -0.5), // bottom left
191
+ vec2f( 0.5, -0.5) // bottom right
192
+ );
193
+
194
+ return vec4f(pos[vertexIndex], 0.0, 1.0);
195
+ }
196
+
197
+ override red = 0.0;
198
+ override green = 0.0;
199
+ override blue = 0.0;
200
+
201
+ @fragment fn fs() -> @location(0) vec4f {
202
+ return vec4f(red, green, blue, 1.0);
203
+ }
204
+ `,
205
+ }),
206
+ targets: [
207
+ {
208
+ format: gpu.getPreferredCanvasFormat(),
209
+ },
210
+ ],
211
+ constants: {
212
+ red: 1,
213
+ green: 0.5,
214
+ blue: 1,
215
+ },
216
+ },
217
+ primitive: {
218
+ topology: "triangle-list",
219
+ },
220
+ });
221
+
222
+ const commandEncoder = device.createCommandEncoder();
223
+ const textureView = ctx.getCurrentTexture().createView();
224
+
225
+ const renderPassDescriptor: GPURenderPassDescriptor = {
226
+ colorAttachments: [
227
+ {
228
+ view: textureView,
229
+ clearValue: [0.3, 0.6, 1, 1],
230
+ loadOp: "clear",
231
+ storeOp: "store",
232
+ },
233
+ ],
234
+ };
235
+
236
+ const passEncoder =
237
+ commandEncoder.beginRenderPass(renderPassDescriptor);
238
+ passEncoder.setPipeline(pipeline);
239
+ passEncoder.draw(3);
240
+ passEncoder.end();
241
+ device.queue.submit([commandEncoder.finish()]);
242
+ return ctx.getImageData();
243
+ },
244
+ );
245
+ const image = encodeImage(result);
246
+ checkImage(image, "snapshots/constant-triangle.png");
247
+ });
248
+ });
@@ -0,0 +1,190 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ import { modelData as model } from "../components/Wireframe/models";
3
+ import {
4
+ solidColorLitWGSL as solidColorLit,
5
+ wireframeWGSL as wireframe,
6
+ } from "../components/Wireframe/Shaders";
7
+ import { client } from "../setup";
8
+
9
+ type TypedArrayView = Float32Array | Uint32Array;
10
+
11
+ type Model = {
12
+ vertexBuffer: GPUBuffer;
13
+ indexBuffer: GPUBuffer;
14
+ indexFormat: GPUIndexFormat;
15
+ vertexCount: number;
16
+ };
17
+
18
+ type ModelData = Record<
19
+ string,
20
+ { vertices: Float32Array; indices: Uint32Array }
21
+ >;
22
+
23
+ type SerializedModelData = Record<
24
+ string,
25
+ { vertices: number[]; indices: number[] }
26
+ >;
27
+
28
+ const serializeModelData = (m: ModelData): SerializedModelData => {
29
+ const serialized: SerializedModelData = {};
30
+ for (const [key, value] of Object.entries(m)) {
31
+ serialized[key] = {
32
+ vertices: Array.from(value.vertices),
33
+ indices: Array.from(value.indices),
34
+ };
35
+ }
36
+ return serialized;
37
+ };
38
+
39
+ describe("Wireframe", () => {
40
+ it("draw scene", async () => {
41
+ const result = await client.eval(
42
+ ({ device, gpu, serializedData, solidColorLitWGSL, wireframeWGSL }) => {
43
+ const parseModelData = (m: SerializedModelData): ModelData => {
44
+ const parsed: ModelData = {};
45
+ for (const entry of Object.entries(m)) {
46
+ parsed[entry[0]] = {
47
+ vertices: new Float32Array(entry[1].vertices),
48
+ indices: new Uint32Array(entry[1].indices),
49
+ };
50
+ }
51
+ return parsed;
52
+ };
53
+
54
+ const modelData = parseModelData(serializedData);
55
+ const settings = {
56
+ barycentricCoordinatesBased: false,
57
+ thickness: 2,
58
+ alphaThreshold: 0.5,
59
+ animate: true,
60
+ lines: true,
61
+ depthBias: 1,
62
+ depthBiasSlopeScale: 0.5,
63
+ models: true,
64
+ };
65
+ function createBufferWithData(
66
+ // eslint-disable-next-line @typescript-eslint/no-shadow
67
+ device: GPUDevice,
68
+ data: TypedArrayView,
69
+ usage: GPUBufferUsageFlags,
70
+ ) {
71
+ const buffer = device.createBuffer({
72
+ size: data.byteLength,
73
+ usage,
74
+ });
75
+ device.queue.writeBuffer(buffer, 0, data);
76
+ return buffer;
77
+ }
78
+
79
+ function createVertexAndIndexBuffer(
80
+ // eslint-disable-next-line @typescript-eslint/no-shadow
81
+ device: GPUDevice,
82
+ {
83
+ vertices,
84
+ indices,
85
+ }: { vertices: Float32Array; indices: Uint32Array },
86
+ ): Model {
87
+ const vertexBuffer = createBufferWithData(
88
+ device,
89
+ vertices,
90
+ GPUBufferUsage.VERTEX |
91
+ GPUBufferUsage.STORAGE |
92
+ GPUBufferUsage.COPY_DST,
93
+ );
94
+ const indexBuffer = createBufferWithData(
95
+ device,
96
+ indices,
97
+ GPUBufferUsage.INDEX |
98
+ GPUBufferUsage.STORAGE |
99
+ GPUBufferUsage.COPY_DST,
100
+ );
101
+ return {
102
+ vertexBuffer,
103
+ indexBuffer,
104
+ indexFormat: "uint32",
105
+ vertexCount: indices.length,
106
+ };
107
+ }
108
+
109
+ const presentationFormat = gpu.getPreferredCanvasFormat();
110
+ const depthFormat = "depth24plus";
111
+ // @ts-ignore
112
+ const models = Object.values(modelData).map((data) =>
113
+ createVertexAndIndexBuffer(device, data),
114
+ );
115
+ const litModule = device.createShaderModule({
116
+ code: solidColorLitWGSL,
117
+ });
118
+ // @ts-ignore
119
+ const wireframeModule = device.createShaderModule({
120
+ code: wireframeWGSL,
121
+ });
122
+ const litBindGroupLayout = device.createBindGroupLayout({
123
+ label: "lit bind group layout",
124
+ entries: [
125
+ {
126
+ binding: 0,
127
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
128
+ buffer: {},
129
+ },
130
+ ],
131
+ });
132
+ const litPipeline = device.createRenderPipeline({
133
+ label: "lit pipeline",
134
+ layout: device.createPipelineLayout({
135
+ bindGroupLayouts: [litBindGroupLayout],
136
+ }),
137
+ vertex: {
138
+ module: litModule,
139
+ buffers: [
140
+ {
141
+ arrayStride: 6 * 4, // position, normal
142
+ attributes: [
143
+ {
144
+ // position
145
+ shaderLocation: 0,
146
+ offset: 0,
147
+ format: "float32x3",
148
+ },
149
+ {
150
+ // normal
151
+ shaderLocation: 1,
152
+ offset: 3 * 4,
153
+ format: "float32x3",
154
+ },
155
+ ],
156
+ },
157
+ ],
158
+ },
159
+ fragment: {
160
+ module: litModule,
161
+ targets: [{ format: presentationFormat }],
162
+ },
163
+ primitive: {
164
+ cullMode: "back",
165
+ },
166
+ depthStencil: {
167
+ depthWriteEnabled: true,
168
+ depthCompare: "less",
169
+ // Applying a depth bias can prevent aliasing from z-fighting with the
170
+ // wireframe lines. The depth bias has to be applied to the lit meshes
171
+ // rather that the wireframe because depthBias isn't considered when
172
+ // drawing line or point primitives.
173
+ depthBias: settings.depthBias,
174
+ depthBiasSlopeScale: settings.depthBiasSlopeScale,
175
+ format: depthFormat,
176
+ },
177
+ });
178
+ return litPipeline.label;
179
+ },
180
+ {
181
+ serializedData: serializeModelData(model),
182
+ solidColorLitWGSL: solidColorLit,
183
+ wireframeWGSL: wireframe,
184
+ },
185
+ );
186
+ expect(result).toBe("lit pipeline");
187
+ // const image = encodeImage(result);
188
+ // checkImage(image, "snapshots/wireframe.png");
189
+ });
190
+ });
@@ -0,0 +1,54 @@
1
+ /* eslint-disable no-var */
2
+ import type { Server, WebSocket } from "ws";
3
+ import { WebSocketServer } from "ws";
4
+
5
+ import { REFERENCE } from "./config";
6
+
7
+ declare global {
8
+ var testServer: Server;
9
+ var testClient: WebSocket;
10
+ var testOS: "ios" | "android" | "web" | "node";
11
+ var testArch: "paper" | "fabric";
12
+ }
13
+
14
+ const isOS = (os: string): os is "android" | "ios" | "web" => {
15
+ return ["ios", "android", "web"].indexOf(os) !== -1;
16
+ };
17
+
18
+ const isArch = (arc: string): arc is "paper" | "fabric" => {
19
+ return ["paper", "fabric"].indexOf(arc) !== -1;
20
+ };
21
+
22
+ const globalSetup = () => {
23
+ return new Promise<void>((resolve) => {
24
+ if (REFERENCE) {
25
+ resolve();
26
+ return;
27
+ }
28
+ const port = 4242;
29
+ global.testServer = new WebSocketServer({ port });
30
+ console.log(
31
+ `\n\nTest server listening on port ${port} (waiting for the example app to open on E2E tests screen)`,
32
+ );
33
+ global.testServer.on("connection", (client) => {
34
+ global.testClient = client;
35
+ client.once("message", (msg) => {
36
+ const obj = JSON.parse(msg.toString("utf8"));
37
+ const { OS, arch } = obj;
38
+ if (!isOS(OS)) {
39
+ throw new Error("Unknown testing platform: " + OS);
40
+ }
41
+ if (!isArch(arch)) {
42
+ throw new Error("Unknown testing architecture: " + arch);
43
+ }
44
+ global.testOS = OS;
45
+ global.testArch = arch;
46
+ console.log(`${OS} device connected (${arch})`);
47
+ resolve();
48
+ });
49
+ });
50
+ });
51
+ };
52
+
53
+ // eslint-disable-next-line import/no-default-export
54
+ export default globalSetup;
@@ -0,0 +1,11 @@
1
+ const globalTeardown = () => {
2
+ if (global.testClient) {
3
+ global.testClient.close();
4
+ }
5
+ if (global.testServer) {
6
+ global.testServer.close();
7
+ }
8
+ };
9
+
10
+ // eslint-disable-next-line import/no-default-export
11
+ export default globalTeardown;