react-native-wgpu 0.2.10 → 0.3.1

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 (258) hide show
  1. package/README.md +155 -69
  2. package/android/CMakeLists.txt +5 -7
  3. package/android/build.gradle +7 -18
  4. package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +34 -10
  5. package/apple/MetalView.mm +0 -19
  6. package/apple/WebGPUModule.h +1 -9
  7. package/apple/WebGPUModule.mm +0 -3
  8. package/apple/WebGPUView.h +0 -3
  9. package/apple/WebGPUView.mm +0 -2
  10. package/cpp/WGPULogger.h +10 -0
  11. package/cpp/dawn/dawn_proc_table.h +1 -1
  12. package/cpp/dawn/webgpu.h +4855 -0
  13. package/cpp/dawn/webgpu_cpp.h +10168 -0
  14. package/cpp/dawn/wire/client/webgpu.h +354 -0
  15. package/cpp/dawn/wire/client/webgpu_cpp.h +10343 -0
  16. package/cpp/dawn/wire/client/webgpu_cpp_print.h +2715 -0
  17. package/cpp/jsi/RNFHybridObject.cpp +8 -4
  18. package/cpp/jsi/RNFHybridObject.h +7 -2
  19. package/cpp/jsi/RNFJSIConverter.h +13 -102
  20. package/cpp/jsi/RNFJSIHelper.h +5 -3
  21. package/cpp/jsi/RNFRuntimeState.cpp +18 -0
  22. package/cpp/jsi/RNFRuntimeState.h +106 -0
  23. package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
  24. package/cpp/rnwgpu/api/GPU.cpp +51 -26
  25. package/cpp/rnwgpu/api/GPU.h +5 -18
  26. package/cpp/rnwgpu/api/GPUAdapter.cpp +79 -54
  27. package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
  28. package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
  29. package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
  30. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
  31. package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
  32. package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
  33. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
  34. package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
  35. package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
  36. package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
  37. package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
  38. package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
  39. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
  40. package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
  41. package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
  42. package/cpp/rnwgpu/api/GPUDevice.h +22 -21
  43. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
  44. package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
  45. package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
  46. package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
  47. package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
  48. package/cpp/rnwgpu/api/GPUQueue.h +7 -6
  49. package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
  50. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
  51. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
  52. package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
  53. package/cpp/rnwgpu/api/GPUSampler.h +1 -3
  54. package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
  55. package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
  56. package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
  57. package/cpp/rnwgpu/api/GPUTexture.h +1 -3
  58. package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
  59. package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
  60. package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
  61. package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
  62. package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
  63. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
  64. package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
  65. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
  66. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
  67. package/cpp/webgpu/webgpu.h +5 -4827
  68. package/cpp/webgpu/webgpu_cpp.h +5 -10140
  69. package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
  70. package/lib/commonjs/Canvas.js +6 -66
  71. package/lib/commonjs/Canvas.js.map +1 -1
  72. package/lib/commonjs/hooks.js +6 -42
  73. package/lib/commonjs/hooks.js.map +1 -1
  74. package/lib/module/Canvas.js +7 -67
  75. package/lib/module/Canvas.js.map +1 -1
  76. package/lib/module/hooks.js +5 -40
  77. package/lib/module/hooks.js.map +1 -1
  78. package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
  79. package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
  80. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  81. package/lib/typescript/lib/module/hooks.d.ts +1 -5
  82. package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
  83. package/lib/typescript/src/Canvas.d.ts +0 -1
  84. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  85. package/lib/typescript/src/hooks.d.ts +2 -7
  86. package/lib/typescript/src/hooks.d.ts.map +1 -1
  87. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  88. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  89. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  90. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  91. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
  92. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  93. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  94. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  95. package/package.json +4 -3
  96. package/react-native-wgpu.podspec +12 -16
  97. package/src/Canvas.tsx +8 -69
  98. package/src/hooks.tsx +14 -48
  99. package/android/cpp/platform/ThreadUtils.cpp +0 -41
  100. package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
  101. package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
  102. package/apple/WebGPUViewManager.mm +0 -24
  103. package/apple/platform/ThreadUtils.cpp +0 -34
  104. package/cpp/dawn/dawn_proc.h +0 -50
  105. package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
  106. package/cpp/dawn/native/D3D11Backend.h +0 -77
  107. package/cpp/dawn/native/D3D12Backend.h +0 -68
  108. package/cpp/dawn/native/D3DBackend.h +0 -56
  109. package/cpp/dawn/native/MetalBackend.h +0 -56
  110. package/cpp/dawn/platform/DawnPlatform.h +0 -167
  111. package/cpp/dawn/platform/dawn_platform_export.h +0 -49
  112. package/cpp/jsi/RNFRuntimeCache.cpp +0 -57
  113. package/cpp/jsi/RNFRuntimeCache.h +0 -79
  114. package/cpp/jsi/RNFWorkletRuntimeCollector.h +0 -43
  115. package/cpp/jsi/RNFWorkletRuntimeRegistry.cpp +0 -12
  116. package/cpp/jsi/RNFWorkletRuntimeRegistry.h +0 -44
  117. package/cpp/platform/ThreadUtils.h +0 -30
  118. package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
  119. package/cpp/threading/CallInvokerDispatcher.h +0 -37
  120. package/cpp/threading/Dispatcher.cpp +0 -55
  121. package/cpp/threading/Dispatcher.h +0 -93
  122. package/cpp/threading/ThreadPool.cpp +0 -88
  123. package/cpp/threading/ThreadPool.h +0 -53
  124. package/cpp/webgpu/webgpu_glfw.h +0 -88
  125. package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
  126. package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
  127. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
  128. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
  129. package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
  130. package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
  131. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
  132. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
  133. package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
  134. package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
  135. package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
  136. package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
  137. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
  138. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
  139. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
  140. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
  141. package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
  142. package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
  143. package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
  144. package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
  145. package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
  146. package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
  147. package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
  148. package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
  149. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
  150. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
  151. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
  152. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
  153. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
  154. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
  155. package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
  156. package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
  157. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
  158. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
  159. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
  160. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
  161. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
  162. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
  163. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
  164. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
  165. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
  166. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
  167. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
  168. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
  169. package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
  170. package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
  171. package/lib/typescript/src/__tests__/config.d.ts +0 -3
  172. package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
  173. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
  174. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
  175. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
  176. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
  177. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
  178. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
  179. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
  180. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
  181. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
  182. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
  183. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
  184. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
  185. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
  186. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
  187. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
  188. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
  189. package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
  190. package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
  191. package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
  192. package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
  193. package/lib/typescript/src/__tests__/setup.d.ts +0 -63
  194. package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
  195. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  196. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  197. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  198. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  199. package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
  200. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
  201. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
  202. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  203. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  204. package/libs/dawn.json +0 -4670
  205. package/src/__tests__/Alpha.spec.ts +0 -28
  206. package/src/__tests__/ArrayBuffer.spec.ts +0 -76
  207. package/src/__tests__/Buffer.spec.ts +0 -357
  208. package/src/__tests__/ComputeShader.spec.ts +0 -375
  209. package/src/__tests__/Constants.spec.ts +0 -89
  210. package/src/__tests__/Device.spec.ts +0 -84
  211. package/src/__tests__/ErrorScope.spec.ts +0 -92
  212. package/src/__tests__/ExternalTexture.spec.ts +0 -284
  213. package/src/__tests__/GPU.spec.ts +0 -272
  214. package/src/__tests__/ImageData.spec.ts +0 -26
  215. package/src/__tests__/Shaders.spec.ts +0 -232
  216. package/src/__tests__/Texture.spec.ts +0 -197
  217. package/src/__tests__/assets/Di-3d.png +0 -0
  218. package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
  219. package/src/__tests__/components/Wireframe/models.ts +0 -113
  220. package/src/__tests__/components/Wireframe/utils.ts +0 -22
  221. package/src/__tests__/components/cube.ts +0 -51
  222. package/src/__tests__/components/meshes/mesh.ts +0 -96
  223. package/src/__tests__/components/meshes/sphere.ts +0 -103
  224. package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
  225. package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
  226. package/src/__tests__/components/meshes/teapot.ts +0 -13
  227. package/src/__tests__/components/meshes/utils.ts +0 -235
  228. package/src/__tests__/components/triangle.ts +0 -17
  229. package/src/__tests__/config.ts +0 -2
  230. package/src/__tests__/demos/ABuffer.spec.ts +0 -890
  231. package/src/__tests__/demos/Blur.spec.ts +0 -398
  232. package/src/__tests__/demos/Cube.spec.ts +0 -929
  233. package/src/__tests__/demos/FractalCube.spec.ts +0 -240
  234. package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
  235. package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
  236. package/src/__tests__/demos/Triangle.spec.ts +0 -266
  237. package/src/__tests__/demos/Wireframe.spec.ts +0 -188
  238. package/src/__tests__/globalSetup.ts +0 -45
  239. package/src/__tests__/globalTeardown.ts +0 -11
  240. package/src/__tests__/setup.ts +0 -423
  241. package/src/__tests__/snapshots/abuffer.png +0 -0
  242. package/src/__tests__/snapshots/asteroid.png +0 -0
  243. package/src/__tests__/snapshots/blur.png +0 -0
  244. package/src/__tests__/snapshots/buffer.png +0 -0
  245. package/src/__tests__/snapshots/constant-triangle.png +0 -0
  246. package/src/__tests__/snapshots/cube.png +0 -0
  247. package/src/__tests__/snapshots/f.png +0 -0
  248. package/src/__tests__/snapshots/f2.png +0 -0
  249. package/src/__tests__/snapshots/fractal-cubes.png +0 -0
  250. package/src/__tests__/snapshots/instanced-cubes.png +0 -0
  251. package/src/__tests__/snapshots/occlusion-query.png +0 -0
  252. package/src/__tests__/snapshots/ref.png +0 -0
  253. package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
  254. package/src/__tests__/snapshots/texture.png +0 -0
  255. package/src/__tests__/snapshots/textured-cube.png +0 -0
  256. package/src/__tests__/snapshots/triangle-msaa.png +0 -0
  257. package/src/__tests__/snapshots/triangle.png +0 -0
  258. package/src/__tests__/snapshots/two-cube.png +0 -0
@@ -1,240 +0,0 @@
1
- import { checkImage, client, encodeImage } from "../setup";
2
-
3
- import { basicVert } from "./Cube.spec";
4
-
5
- const sampleSelf = /*wgsl*/ `@binding(1) @group(0) var mySampler: sampler;
6
- @binding(2) @group(0) var myTexture: texture_2d<f32>;
7
-
8
- @fragment
9
- fn main(
10
- @location(0) fragUV: vec2f,
11
- @location(1) fragPosition: vec4f
12
- ) -> @location(0) vec4f {
13
- let texColor = textureSample(myTexture, mySampler, fragUV * 0.8 + vec2(0.1));
14
- let f = select(1.0, 0.0, length(texColor.rgb - vec3(0.5)) < 0.01);
15
- return f * texColor + (1.0 - f) * fragPosition;
16
- }
17
- `;
18
-
19
- describe("Fractal Cube", () => {
20
- it("Fractal Cube", async () => {
21
- const result = await client.eval(
22
- ({
23
- gpu,
24
- device,
25
- assets: { cubeVertexArray },
26
- ctx,
27
- mat4,
28
- vec3,
29
- sampleSelfWGSL,
30
- basicVertWGSL,
31
- canvas,
32
- }) => {
33
- const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
34
- const cubePositionOffset = 0;
35
- const cubeUVOffset = 4 * 8;
36
- const cubeVertexCount = 36;
37
- const presentationFormat = gpu.getPreferredCanvasFormat();
38
- // Create a vertex buffer from the cube data.
39
- const verticesBuffer = device.createBuffer({
40
- size: cubeVertexArray.byteLength,
41
- usage: GPUBufferUsage.VERTEX,
42
- mappedAtCreation: true,
43
- });
44
- new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray);
45
- verticesBuffer.unmap();
46
-
47
- const pipeline = device.createRenderPipeline({
48
- layout: "auto",
49
- vertex: {
50
- module: device.createShaderModule({
51
- code: basicVertWGSL,
52
- }),
53
- buffers: [
54
- {
55
- arrayStride: cubeVertexSize,
56
- attributes: [
57
- {
58
- // position
59
- shaderLocation: 0,
60
- offset: cubePositionOffset,
61
- format: "float32x4",
62
- },
63
- {
64
- // uv
65
- shaderLocation: 1,
66
- offset: cubeUVOffset,
67
- format: "float32x2",
68
- },
69
- ],
70
- },
71
- ],
72
- },
73
- fragment: {
74
- module: device.createShaderModule({
75
- code: sampleSelfWGSL,
76
- }),
77
- targets: [
78
- {
79
- format: presentationFormat,
80
- },
81
- ],
82
- },
83
- primitive: {
84
- topology: "triangle-list",
85
-
86
- // Backface culling since the cube is solid piece of geometry.
87
- // Faces pointing away from the camera will be occluded by faces
88
- // pointing toward the camera.
89
- cullMode: "back",
90
- },
91
-
92
- // Enable depth testing so that the fragment closest to the camera
93
- // is rendered in front.
94
- depthStencil: {
95
- depthWriteEnabled: true,
96
- depthCompare: "less",
97
- format: "depth24plus",
98
- },
99
- });
100
-
101
- const depthTexture = device.createTexture({
102
- size: [ctx.canvas.width, ctx.canvas.height],
103
- format: "depth24plus",
104
- usage: GPUTextureUsage.RENDER_ATTACHMENT,
105
- });
106
-
107
- const uniformBufferSize = 4 * 16; // 4x4 matrix
108
- const uniformBuffer = device.createBuffer({
109
- size: uniformBufferSize,
110
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
111
- });
112
-
113
- // We will copy the frame's rendering results into this texture and
114
- // sample it on the next frame.
115
- const cubeTexture = device.createTexture({
116
- size: [ctx.canvas.width, ctx.canvas.height],
117
- format: presentationFormat,
118
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
119
- });
120
-
121
- // Create a sampler with linear filtering for smooth interpolation.
122
- const sampler = device.createSampler({
123
- magFilter: "linear",
124
- minFilter: "linear",
125
- });
126
-
127
- const uniformBindGroup = device.createBindGroup({
128
- layout: pipeline.getBindGroupLayout(0),
129
- entries: [
130
- {
131
- binding: 0,
132
- resource: {
133
- buffer: uniformBuffer,
134
- },
135
- },
136
- {
137
- binding: 1,
138
- resource: sampler,
139
- },
140
- {
141
- binding: 2,
142
- resource: cubeTexture.createView(),
143
- },
144
- ],
145
- });
146
-
147
- const swapChainTexture = ctx.getCurrentTexture();
148
- const renderPassDescriptor: GPURenderPassDescriptor = {
149
- colorAttachments: [
150
- {
151
- view: swapChainTexture.createView(), // Assigned later
152
-
153
- clearValue: [0.5, 0.5, 0.5, 1.0],
154
- loadOp: "clear",
155
- storeOp: "store",
156
- },
157
- ],
158
- depthStencilAttachment: {
159
- view: depthTexture.createView(),
160
-
161
- depthClearValue: 1.0,
162
- depthLoadOp: "clear",
163
- depthStoreOp: "store",
164
- },
165
- };
166
-
167
- const aspect = ctx.canvas.width / ctx.canvas.height;
168
- const projectionMatrix = mat4.perspective(
169
- (2 * Math.PI) / 5,
170
- aspect,
171
- 1,
172
- 100.0,
173
- );
174
- const modelViewProjectionMatrix = mat4.create();
175
-
176
- function getTransformationMatrix(now: number) {
177
- const viewMatrix = mat4.identity();
178
- mat4.translate(viewMatrix, vec3.fromValues(0, 0, -4), viewMatrix);
179
- mat4.rotate(
180
- viewMatrix,
181
- vec3.fromValues(Math.sin(now), Math.cos(now), 0),
182
- 1,
183
- viewMatrix,
184
- );
185
-
186
- mat4.multiply(
187
- projectionMatrix,
188
- viewMatrix,
189
- modelViewProjectionMatrix,
190
- );
191
-
192
- return modelViewProjectionMatrix;
193
- }
194
- function frame(now: number) {
195
- const transformationMatrix = getTransformationMatrix(now);
196
- device.queue.writeBuffer(
197
- uniformBuffer,
198
- 0,
199
- transformationMatrix.buffer,
200
- transformationMatrix.byteOffset,
201
- transformationMatrix.byteLength,
202
- );
203
-
204
- const commandEncoder = device.createCommandEncoder();
205
- const passEncoder =
206
- commandEncoder.beginRenderPass(renderPassDescriptor);
207
- passEncoder.setPipeline(pipeline);
208
- passEncoder.setBindGroup(0, uniformBindGroup);
209
- passEncoder.setVertexBuffer(0, verticesBuffer);
210
- passEncoder.draw(cubeVertexCount);
211
- passEncoder.end();
212
-
213
- // Copy the rendering results from the swapchain into |cubeTexture|.
214
- commandEncoder.copyTextureToTexture(
215
- {
216
- texture: swapChainTexture,
217
- },
218
- {
219
- texture: cubeTexture,
220
- },
221
- [ctx.canvas.width, ctx.canvas.height],
222
- );
223
-
224
- device.queue.submit([commandEncoder.finish()]);
225
- }
226
- const now = 1721677648;
227
- for (let i = 0; i < 10; i++) {
228
- frame(now + i * 16);
229
- }
230
- return canvas.getImageData();
231
- },
232
- {
233
- basicVertWGSL: basicVert,
234
- sampleSelfWGSL: sampleSelf,
235
- },
236
- );
237
- const image = encodeImage(result);
238
- checkImage(image, "snapshots/fractal-cubes.png");
239
- });
240
- });
@@ -1,376 +0,0 @@
1
- /* eslint-disable @typescript-eslint/ban-ts-comment */
2
- import { checkImage, client, encodeImage } from "../setup";
3
-
4
- const solidColorLit = /*wgsl*/ `struct Uniforms {
5
- worldViewProjectionMatrix: mat4x4f,
6
- worldMatrix: mat4x4f,
7
- color: vec4f,
8
- };
9
-
10
- struct Vertex {
11
- @location(0) position: vec4f,
12
- @location(1) normal: vec3f,
13
- };
14
-
15
- struct VSOut {
16
- @builtin(position) position: vec4f,
17
- @location(0) normal: vec3f,
18
- };
19
-
20
- @group(0) @binding(0) var<uniform> uni: Uniforms;
21
-
22
- @vertex fn vs(vin: Vertex) -> VSOut {
23
- var vOut: VSOut;
24
- vOut.position = uni.worldViewProjectionMatrix * vin.position;
25
- vOut.normal = (uni.worldMatrix * vec4f(vin.normal, 0)).xyz;
26
- return vOut;
27
- }
28
-
29
- @fragment fn fs(vin: VSOut) -> @location(0) vec4f {
30
- let lightDirection = normalize(vec3f(4, 10, 6));
31
- let light = dot(normalize(vin.normal), lightDirection) * 0.5 + 0.5;
32
- return vec4f(uni.color.rgb * light, uni.color.a);
33
- }
34
- `;
35
-
36
- type TypedArrayView =
37
- | Int8Array
38
- | Uint8Array
39
- | Int16Array
40
- | Uint16Array
41
- | Int32Array
42
- | Uint32Array
43
- | Float32Array
44
- | Float64Array;
45
-
46
- export type TypedArrayConstructor =
47
- | Int8ArrayConstructor
48
- | Uint8ArrayConstructor
49
- | Int16ArrayConstructor
50
- | Uint16ArrayConstructor
51
- | Int32ArrayConstructor
52
- | Uint32ArrayConstructor
53
- | Float32ArrayConstructor
54
- | Float64ArrayConstructor;
55
-
56
- describe("OcclusionQuery", () => {
57
- it("occlusionQuery", async () => {
58
- const result = await client.eval(
59
- ({ device, ctx, gpu, solidColorLitWGSL, mat4, canvas }) => {
60
- const depthFormat = "depth24plus";
61
- const presentationFormat = gpu.getPreferredCanvasFormat();
62
- const animate = true;
63
- const module = device.createShaderModule({
64
- code: solidColorLitWGSL,
65
- });
66
-
67
- const pipeline = device.createRenderPipeline({
68
- layout: "auto",
69
- vertex: {
70
- module,
71
- buffers: [
72
- {
73
- arrayStride: 6 * 4, // 3x2 floats, 4 bytes each
74
- attributes: [
75
- { shaderLocation: 0, offset: 0, format: "float32x3" }, // position
76
- { shaderLocation: 1, offset: 12, format: "float32x3" }, // normal
77
- ],
78
- },
79
- ],
80
- },
81
- fragment: {
82
- module,
83
- targets: [{ format: presentationFormat }],
84
- },
85
- primitive: {
86
- topology: "triangle-list",
87
- cullMode: "back",
88
- },
89
- depthStencil: {
90
- depthWriteEnabled: true,
91
- depthCompare: "less",
92
- format: depthFormat,
93
- },
94
- });
95
-
96
- // prettier-ignore
97
- const cubePositions = [
98
- { position: [-1, 0, 0], id: "🟥", color: [1, 0, 0, 1] },
99
- { position: [ 1, 0, 0], id: "🟨", color: [1, 1, 0, 1] },
100
- { position: [ 0, -1, 0], id: "🟩", color: [0, 0.5, 0, 1] },
101
- { position: [ 0, 1, 0], id: "🟧", color: [1, 0.6, 0, 1] },
102
- { position: [ 0, 0, -1], id: "🟦", color: [0, 0, 1, 1] },
103
- { position: [ 0, 0, 1], id: "🟪", color: [0.5, 0, 0.5, 1] },
104
- ];
105
-
106
- const objectInfos = cubePositions.map(({ position, id, color }) => {
107
- const uniformBufferSize = (2 * 16 + 3 + 1 + 4) * 4;
108
- const uniformBuffer = device.createBuffer({
109
- size: uniformBufferSize,
110
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
111
- });
112
- const uniformValues = new Float32Array(uniformBufferSize / 4);
113
- const worldViewProjection = uniformValues.subarray(0, 16);
114
- const worldInverseTranspose = uniformValues.subarray(16, 32);
115
- const colorValue = uniformValues.subarray(32, 36);
116
-
117
- colorValue.set(color);
118
-
119
- const bindGroup = device.createBindGroup({
120
- layout: pipeline.getBindGroupLayout(0),
121
- entries: [{ binding: 0, resource: { buffer: uniformBuffer } }],
122
- });
123
-
124
- return {
125
- id,
126
- position: position.map((v) => v * 10),
127
- bindGroup,
128
- uniformBuffer,
129
- uniformValues,
130
- worldInverseTranspose,
131
- worldViewProjection,
132
- };
133
- });
134
-
135
- const querySet = device.createQuerySet({
136
- type: "occlusion",
137
- count: objectInfos.length,
138
- });
139
-
140
- const resolveBuf = device.createBuffer({
141
- label: "resolveBuffer",
142
- // Query results are 64bit unsigned integers.
143
- size: objectInfos.length * BigUint64Array.BYTES_PER_ELEMENT,
144
- usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC,
145
- });
146
-
147
- const resultBuf = device.createBuffer({
148
- label: "resultBuffer",
149
- size: resolveBuf.size,
150
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
151
- });
152
-
153
- function createBufferWithData(
154
- dev: GPUDevice,
155
- data: TypedArrayView,
156
- usage: GPUBufferUsageFlags,
157
- label: string,
158
- ) {
159
- const buffer = dev.createBuffer({
160
- label,
161
- size: data.byteLength,
162
- usage,
163
- mappedAtCreation: true,
164
- });
165
- const Ctor = data.constructor as TypedArrayConstructor;
166
- const dst = new Ctor(buffer.getMappedRange());
167
- dst.set(data);
168
- buffer.unmap();
169
- return buffer;
170
- }
171
-
172
- // prettier-ignore
173
- const vertexData = new Float32Array([
174
- // position normal
175
- 1, 1, -1, 1, 0, 0,
176
- 1, 1, 1, 1, 0, 0,
177
- 1, -1, 1, 1, 0, 0,
178
- 1, -1, -1, 1, 0, 0,
179
- -1, 1, 1, -1, 0, 0,
180
- -1, 1, -1, -1, 0, 0,
181
- -1, -1, -1, -1, 0, 0,
182
- -1, -1, 1, -1, 0, 0,
183
- -1, 1, 1, 0, 1, 0,
184
- 1, 1, 1, 0, 1, 0,
185
- 1, 1, -1, 0, 1, 0,
186
- -1, 1, -1, 0, 1, 0,
187
- -1, -1, -1, 0, -1, 0,
188
- 1, -1, -1, 0, -1, 0,
189
- 1, -1, 1, 0, -1, 0,
190
- -1, -1, 1, 0, -1, 0,
191
- 1, 1, 1, 0, 0, 1,
192
- -1, 1, 1, 0, 0, 1,
193
- -1, -1, 1, 0, 0, 1,
194
- 1, -1, 1, 0, 0, 1,
195
- -1, 1, -1, 0, 0, -1,
196
- 1, 1, -1, 0, 0, -1,
197
- 1, -1, -1, 0, 0, -1,
198
- -1, -1, -1, 0, 0, -1,
199
- ]);
200
- // prettier-ignore
201
- const indices = new Uint16Array([
202
- 0, 1, 2, 0, 2, 3, // +x face
203
- 4, 5, 6, 4, 6, 7, // -x face
204
- 8, 9, 10, 8, 10, 11, // +y face
205
- 12, 13, 14, 12, 14, 15, // -y face
206
- 16, 17, 18, 16, 18, 19, // +z face
207
- 20, 21, 22, 20, 22, 23, // -z face
208
- ]);
209
-
210
- const vertexBuf = createBufferWithData(
211
- device,
212
- vertexData,
213
- GPUBufferUsage.VERTEX,
214
- "vertexBuffer",
215
- );
216
- const indicesBuf = createBufferWithData(
217
- device,
218
- indices,
219
- GPUBufferUsage.INDEX,
220
- "indexBuffer",
221
- );
222
-
223
- const renderPassDescriptor: GPURenderPassDescriptor = {
224
- // @ts-expect-error
225
- colorAttachments: [
226
- {
227
- view: undefined, // Assigned later
228
- clearValue: { r: 0.5, g: 0.5, b: 0.5, a: 1.0 },
229
- loadOp: "clear",
230
- storeOp: "store",
231
- },
232
- ],
233
- depthStencilAttachment: {
234
- // @ts-expect-error
235
- view: undefined, // Assigned later
236
- depthClearValue: 1.0,
237
- depthLoadOp: "clear",
238
- depthStoreOp: "store",
239
- },
240
- occlusionQuerySet: querySet,
241
- };
242
-
243
- const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
244
- const lerpV = (a: number[], b: number[], t: number) =>
245
- a.map((v, i) => lerp(v, b[i], t));
246
- const pingPongSine = (t: number) =>
247
- Math.sin(t * Math.PI * 2) * 0.5 + 0.5;
248
-
249
- let depthTexture: GPUTexture | undefined;
250
-
251
- let time = 0;
252
- let then = 0;
253
- let visible = "";
254
- function render(now: number) {
255
- now *= 0.001; // convert to seconds
256
- const deltaTime = now - then;
257
- then = now;
258
-
259
- if (animate) {
260
- time += deltaTime;
261
- }
262
-
263
- const projection = mat4.perspective(
264
- (30 * Math.PI) / 180,
265
- ctx.canvas.width / ctx.canvas.height,
266
- 0.5,
267
- 100,
268
- );
269
-
270
- const m = mat4.identity();
271
- mat4.rotateX(m, time, m);
272
- mat4.rotateY(m, time * 0.7, m);
273
- mat4.translate(
274
- m,
275
- lerpV([0, 0, 5], [0, 0, 40], pingPongSine(time * 0.2)),
276
- m,
277
- );
278
- const view = mat4.inverse(m);
279
- const viewProjection = mat4.multiply(projection, view);
280
-
281
- const canvasTexture = ctx.getCurrentTexture();
282
- if (
283
- !depthTexture ||
284
- depthTexture.width !== canvasTexture.width ||
285
- depthTexture.height !== canvasTexture.height
286
- ) {
287
- depthTexture?.destroy();
288
- depthTexture = device.createTexture({
289
- size: canvasTexture, // canvasTexture has width, height, and depthOrArrayLayers properties
290
- format: depthFormat,
291
- usage: GPUTextureUsage.RENDER_ATTACHMENT,
292
- });
293
- }
294
-
295
- const colorTexture = ctx.getCurrentTexture();
296
- //@ts-expect-error
297
- renderPassDescriptor.colorAttachments[0].view =
298
- colorTexture.createView();
299
- //@ts-expect-error
300
- renderPassDescriptor.depthStencilAttachment.view =
301
- depthTexture.createView();
302
-
303
- const encoder = device.createCommandEncoder();
304
- const pass = encoder.beginRenderPass(renderPassDescriptor);
305
- pass.setPipeline(pipeline);
306
- pass.setVertexBuffer(0, vertexBuf);
307
- pass.setIndexBuffer(indicesBuf, "uint16");
308
-
309
- objectInfos.forEach(
310
- (
311
- {
312
- bindGroup,
313
- uniformBuffer,
314
- uniformValues,
315
- worldViewProjection,
316
- worldInverseTranspose,
317
- position,
318
- },
319
- i,
320
- ) => {
321
- const world = mat4.translation(position);
322
- mat4.transpose(mat4.inverse(world), worldInverseTranspose);
323
- mat4.multiply(viewProjection, world, worldViewProjection);
324
-
325
- device.queue.writeBuffer(uniformBuffer, 0, uniformValues);
326
-
327
- pass.setBindGroup(0, bindGroup);
328
- pass.beginOcclusionQuery(i);
329
- pass.drawIndexed(indices.length);
330
- pass.endOcclusionQuery();
331
- },
332
- );
333
-
334
- pass.end();
335
- encoder.resolveQuerySet(
336
- querySet,
337
- 0,
338
- objectInfos.length,
339
- resolveBuf,
340
- 0,
341
- );
342
- if (resultBuf.mapState === "unmapped") {
343
- encoder.copyBufferToBuffer(
344
- resolveBuf,
345
- 0,
346
- resultBuf,
347
- 0,
348
- resultBuf.size,
349
- );
350
- }
351
-
352
- device.queue.submit([encoder.finish()]);
353
-
354
- if (resultBuf.mapState === "unmapped") {
355
- resultBuf.mapAsync(GPUMapMode.READ).then(() => {
356
- const results = new BigUint64Array(resultBuf.getMappedRange());
357
-
358
- visible = objectInfos
359
- .filter((_, i) => results[i])
360
- .map(({ id }) => id)
361
- .join("");
362
- // console.log({ visible });
363
- resultBuf.unmap();
364
- });
365
- }
366
- }
367
- render(1721766068905 + 150 * 16);
368
- return canvas.getImageData().then((image) => ({ image, visible }));
369
- },
370
- { solidColorLitWGSL: solidColorLit },
371
- );
372
- expect(result.visible).toBe("🟥🟨🟩🟧🟦🟪");
373
- const image = encodeImage(result.image);
374
- checkImage(image, "snapshots/occlusion-query.png");
375
- });
376
- });