react-native-wgpu 0.2.9 → 0.3.0

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 (248) hide show
  1. package/README.md +155 -69
  2. package/android/CMakeLists.txt +4 -5
  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/RNFJSIConverter.h +17 -59
  18. package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
  19. package/cpp/rnwgpu/api/GPU.cpp +51 -26
  20. package/cpp/rnwgpu/api/GPU.h +5 -18
  21. package/cpp/rnwgpu/api/GPUAdapter.cpp +75 -54
  22. package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
  23. package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
  24. package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
  25. package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
  26. package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
  27. package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
  28. package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
  29. package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
  30. package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
  31. package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
  32. package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
  33. package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
  34. package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
  35. package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
  36. package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
  37. package/cpp/rnwgpu/api/GPUDevice.h +22 -21
  38. package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
  39. package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
  40. package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
  41. package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
  42. package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
  43. package/cpp/rnwgpu/api/GPUQueue.h +7 -6
  44. package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
  45. package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
  46. package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
  47. package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
  48. package/cpp/rnwgpu/api/GPUSampler.h +1 -3
  49. package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
  50. package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
  51. package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
  52. package/cpp/rnwgpu/api/GPUTexture.h +1 -3
  53. package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
  54. package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
  55. package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
  56. package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
  57. package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
  58. package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
  59. package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
  60. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
  61. package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
  62. package/cpp/webgpu/webgpu.h +5 -4827
  63. package/cpp/webgpu/webgpu_cpp.h +5 -10140
  64. package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
  65. package/lib/commonjs/Canvas.js +6 -66
  66. package/lib/commonjs/Canvas.js.map +1 -1
  67. package/lib/commonjs/hooks.js +6 -42
  68. package/lib/commonjs/hooks.js.map +1 -1
  69. package/lib/module/Canvas.js +7 -67
  70. package/lib/module/Canvas.js.map +1 -1
  71. package/lib/module/hooks.js +5 -40
  72. package/lib/module/hooks.js.map +1 -1
  73. package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
  74. package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
  75. package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
  76. package/lib/typescript/lib/module/hooks.d.ts +1 -5
  77. package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
  78. package/lib/typescript/src/Canvas.d.ts +0 -1
  79. package/lib/typescript/src/Canvas.d.ts.map +1 -1
  80. package/lib/typescript/src/hooks.d.ts +2 -7
  81. package/lib/typescript/src/hooks.d.ts.map +1 -1
  82. package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
  83. package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
  84. package/libs/android/x86/libwebgpu_dawn.so +0 -0
  85. package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
  86. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
  87. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  88. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  89. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  90. package/package.json +4 -3
  91. package/react-native-wgpu.podspec +12 -16
  92. package/src/Canvas.tsx +8 -69
  93. package/src/hooks.tsx +14 -48
  94. package/android/cpp/platform/ThreadUtils.cpp +0 -41
  95. package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
  96. package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
  97. package/apple/WebGPUViewManager.mm +0 -24
  98. package/apple/platform/ThreadUtils.cpp +0 -34
  99. package/cpp/dawn/dawn_proc.h +0 -50
  100. package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
  101. package/cpp/dawn/native/D3D11Backend.h +0 -77
  102. package/cpp/dawn/native/D3D12Backend.h +0 -68
  103. package/cpp/dawn/native/D3DBackend.h +0 -56
  104. package/cpp/dawn/native/MetalBackend.h +0 -56
  105. package/cpp/dawn/platform/DawnPlatform.h +0 -167
  106. package/cpp/dawn/platform/dawn_platform_export.h +0 -49
  107. package/cpp/platform/ThreadUtils.h +0 -30
  108. package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
  109. package/cpp/threading/CallInvokerDispatcher.h +0 -37
  110. package/cpp/threading/Dispatcher.cpp +0 -55
  111. package/cpp/threading/Dispatcher.h +0 -93
  112. package/cpp/threading/ThreadPool.cpp +0 -88
  113. package/cpp/threading/ThreadPool.h +0 -53
  114. package/cpp/webgpu/webgpu_glfw.h +0 -88
  115. package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
  116. package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
  117. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
  118. package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
  119. package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
  120. package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
  121. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
  122. package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
  123. package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
  124. package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
  125. package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
  126. package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
  127. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
  128. package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
  129. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
  130. package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
  131. package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
  132. package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
  133. package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
  134. package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
  135. package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
  136. package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
  137. package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
  138. package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
  139. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
  140. package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
  141. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
  142. package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
  143. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
  144. package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
  145. package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
  146. package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
  147. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
  148. package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
  149. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
  150. package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
  151. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
  152. package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
  153. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
  154. package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
  155. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
  156. package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
  157. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
  158. package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
  159. package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
  160. package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
  161. package/lib/typescript/src/__tests__/config.d.ts +0 -3
  162. package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
  163. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
  164. package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
  165. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
  166. package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
  167. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
  168. package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
  169. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
  170. package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
  171. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
  172. package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
  173. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
  174. package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
  175. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
  176. package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
  177. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
  178. package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
  179. package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
  180. package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
  181. package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
  182. package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
  183. package/lib/typescript/src/__tests__/setup.d.ts +0 -63
  184. package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
  185. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  186. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  187. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  188. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  189. package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
  190. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
  191. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
  192. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  193. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  194. package/libs/dawn.json +0 -4670
  195. package/src/__tests__/Alpha.spec.ts +0 -28
  196. package/src/__tests__/ArrayBuffer.spec.ts +0 -76
  197. package/src/__tests__/Buffer.spec.ts +0 -357
  198. package/src/__tests__/ComputeShader.spec.ts +0 -375
  199. package/src/__tests__/Constants.spec.ts +0 -89
  200. package/src/__tests__/Device.spec.ts +0 -84
  201. package/src/__tests__/ErrorScope.spec.ts +0 -92
  202. package/src/__tests__/ExternalTexture.spec.ts +0 -284
  203. package/src/__tests__/GPU.spec.ts +0 -272
  204. package/src/__tests__/ImageData.spec.ts +0 -26
  205. package/src/__tests__/Shaders.spec.ts +0 -232
  206. package/src/__tests__/Texture.spec.ts +0 -197
  207. package/src/__tests__/assets/Di-3d.png +0 -0
  208. package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
  209. package/src/__tests__/components/Wireframe/models.ts +0 -113
  210. package/src/__tests__/components/Wireframe/utils.ts +0 -22
  211. package/src/__tests__/components/cube.ts +0 -51
  212. package/src/__tests__/components/meshes/mesh.ts +0 -96
  213. package/src/__tests__/components/meshes/sphere.ts +0 -103
  214. package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
  215. package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
  216. package/src/__tests__/components/meshes/teapot.ts +0 -13
  217. package/src/__tests__/components/meshes/utils.ts +0 -235
  218. package/src/__tests__/components/triangle.ts +0 -17
  219. package/src/__tests__/config.ts +0 -2
  220. package/src/__tests__/demos/ABuffer.spec.ts +0 -890
  221. package/src/__tests__/demos/Blur.spec.ts +0 -398
  222. package/src/__tests__/demos/Cube.spec.ts +0 -929
  223. package/src/__tests__/demos/FractalCube.spec.ts +0 -240
  224. package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
  225. package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
  226. package/src/__tests__/demos/Triangle.spec.ts +0 -266
  227. package/src/__tests__/demos/Wireframe.spec.ts +0 -188
  228. package/src/__tests__/globalSetup.ts +0 -45
  229. package/src/__tests__/globalTeardown.ts +0 -11
  230. package/src/__tests__/setup.ts +0 -423
  231. package/src/__tests__/snapshots/abuffer.png +0 -0
  232. package/src/__tests__/snapshots/asteroid.png +0 -0
  233. package/src/__tests__/snapshots/blur.png +0 -0
  234. package/src/__tests__/snapshots/buffer.png +0 -0
  235. package/src/__tests__/snapshots/constant-triangle.png +0 -0
  236. package/src/__tests__/snapshots/cube.png +0 -0
  237. package/src/__tests__/snapshots/f.png +0 -0
  238. package/src/__tests__/snapshots/f2.png +0 -0
  239. package/src/__tests__/snapshots/fractal-cubes.png +0 -0
  240. package/src/__tests__/snapshots/instanced-cubes.png +0 -0
  241. package/src/__tests__/snapshots/occlusion-query.png +0 -0
  242. package/src/__tests__/snapshots/ref.png +0 -0
  243. package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
  244. package/src/__tests__/snapshots/texture.png +0 -0
  245. package/src/__tests__/snapshots/textured-cube.png +0 -0
  246. package/src/__tests__/snapshots/triangle-msaa.png +0 -0
  247. package/src/__tests__/snapshots/triangle.png +0 -0
  248. package/src/__tests__/snapshots/two-cube.png +0 -0
@@ -1,398 +0,0 @@
1
- import { checkImage, client, encodeImage } from "../setup";
2
-
3
- const blur = /*wgsl*/ `struct Params {
4
- filterDim : i32,
5
- blockDim : u32,
6
- }
7
-
8
- @group(0) @binding(0) var samp : sampler;
9
- @group(0) @binding(1) var<uniform> params : Params;
10
- @group(1) @binding(1) var inputTex : texture_2d<f32>;
11
- @group(1) @binding(2) var outputTex : texture_storage_2d<rgba8unorm, write>;
12
-
13
- struct Flip {
14
- value : u32,
15
- }
16
- @group(1) @binding(3) var<uniform> flip : Flip;
17
-
18
- // This shader blurs the input texture in one direction, depending on whether
19
- // |flip.value| is 0 or 1.
20
- // It does so by running (128 / 4) threads per workgroup to load 128
21
- // texels into 4 rows of shared memory. Each thread loads a
22
- // 4 x 4 block of texels to take advantage of the texture sampling
23
- // hardware.
24
- // Then, each thread computes the blur result by averaging the adjacent texel values
25
- // in shared memory.
26
- // Because we're operating on a subset of the texture, we cannot compute all of the
27
- // results since not all of the neighbors are available in shared memory.
28
- // Specifically, with 128 x 128 tiles, we can only compute and write out
29
- // square blocks of size 128 - (filterSize - 1). We compute the number of blocks
30
- // needed in Javascript and dispatch that amount.
31
-
32
- var<workgroup> tile : array<array<vec3f, 128>, 4>;
33
-
34
- @compute @workgroup_size(32, 1, 1)
35
- fn main(
36
- @builtin(workgroup_id) WorkGroupID : vec3u,
37
- @builtin(local_invocation_id) LocalInvocationID : vec3u
38
- ) {
39
- let filterOffset = (params.filterDim - 1) / 2;
40
- let dims = vec2i(textureDimensions(inputTex, 0));
41
- let baseIndex = vec2i(WorkGroupID.xy * vec2(params.blockDim, 4) +
42
- LocalInvocationID.xy * vec2(4, 1))
43
- - vec2(filterOffset, 0);
44
-
45
- for (var r = 0; r < 4; r++) {
46
- for (var c = 0; c < 4; c++) {
47
- var loadIndex = baseIndex + vec2(c, r);
48
- if (flip.value != 0u) {
49
- loadIndex = loadIndex.yx;
50
- }
51
-
52
- tile[r][4 * LocalInvocationID.x + u32(c)] = textureSampleLevel(
53
- inputTex,
54
- samp,
55
- (vec2f(loadIndex) + vec2f(0.25, 0.25)) / vec2f(dims),
56
- 0.0
57
- ).rgb;
58
- }
59
- }
60
-
61
- workgroupBarrier();
62
-
63
- for (var r = 0; r < 4; r++) {
64
- for (var c = 0; c < 4; c++) {
65
- var writeIndex = baseIndex + vec2(c, r);
66
- if (flip.value != 0) {
67
- writeIndex = writeIndex.yx;
68
- }
69
-
70
- let center = i32(4 * LocalInvocationID.x) + c;
71
- if (center >= filterOffset &&
72
- center < 128 - filterOffset &&
73
- all(writeIndex < dims)) {
74
- var acc = vec3(0.0, 0.0, 0.0);
75
- for (var f = 0; f < params.filterDim; f++) {
76
- var i = center + f - filterOffset;
77
- acc = acc + (1.0 / f32(params.filterDim)) * tile[r][i];
78
- }
79
- textureStore(outputTex, writeIndex, vec4(acc, 1.0));
80
- }
81
- }
82
- }
83
- }
84
- `;
85
-
86
- const fullscreenTexturedQuad = /*wgsl*/ `@group(0) @binding(0) var mySampler : sampler;
87
- @group(0) @binding(1) var myTexture : texture_2d<f32>;
88
-
89
- struct VertexOutput {
90
- @builtin(position) Position : vec4f,
91
- @location(0) fragUV : vec2f,
92
- }
93
-
94
- @vertex
95
- fn vert_main(@builtin(vertex_index) VertexIndex : u32) -> VertexOutput {
96
- const pos = array(
97
- vec2( 1.0, 1.0),
98
- vec2( 1.0, -1.0),
99
- vec2(-1.0, -1.0),
100
- vec2( 1.0, 1.0),
101
- vec2(-1.0, -1.0),
102
- vec2(-1.0, 1.0),
103
- );
104
-
105
- const uv = array(
106
- vec2(1.0, 0.0),
107
- vec2(1.0, 1.0),
108
- vec2(0.0, 1.0),
109
- vec2(1.0, 0.0),
110
- vec2(0.0, 1.0),
111
- vec2(0.0, 0.0),
112
- );
113
-
114
- var output : VertexOutput;
115
- output.Position = vec4(pos[VertexIndex], 0.0, 1.0);
116
- output.fragUV = uv[VertexIndex];
117
- return output;
118
- }
119
-
120
- @fragment
121
- fn frag_main(@location(0) fragUV : vec2f) -> @location(0) vec4f {
122
- return textureSample(myTexture, mySampler, fragUV);
123
- }
124
- `;
125
-
126
- describe("Blur", () => {
127
- it("draw scene", async () => {
128
- const result = await client.eval(
129
- ({
130
- ctx,
131
- device,
132
- gpu,
133
- assets: { di3D: imageBitmap },
134
- fullscreenTexturedQuadWGSL,
135
- blurWGSL,
136
- canvas,
137
- }) => {
138
- const tileDim = 128;
139
- const batch = [4, 4];
140
- const presentationFormat = gpu.getPreferredCanvasFormat();
141
- const blurPipeline = device.createComputePipeline({
142
- layout: "auto",
143
- compute: {
144
- module: device.createShaderModule({
145
- code: blurWGSL,
146
- }),
147
- },
148
- });
149
-
150
- const fullscreenQuadPipeline = device.createRenderPipeline({
151
- layout: "auto",
152
- vertex: {
153
- module: device.createShaderModule({
154
- code: fullscreenTexturedQuadWGSL,
155
- }),
156
- },
157
- fragment: {
158
- module: device.createShaderModule({
159
- code: fullscreenTexturedQuadWGSL,
160
- }),
161
- targets: [
162
- {
163
- format: presentationFormat,
164
- },
165
- ],
166
- },
167
- primitive: {
168
- topology: "triangle-list",
169
- },
170
- });
171
-
172
- const sampler = device.createSampler({
173
- magFilter: "linear",
174
- minFilter: "linear",
175
- });
176
-
177
- const [srcWidth, srcHeight] = [imageBitmap.width, imageBitmap.height];
178
- const imageTexture = device.createTexture({
179
- size: [srcWidth, srcHeight, 1],
180
- format: "rgba8unorm",
181
- usage:
182
- GPUTextureUsage.TEXTURE_BINDING |
183
- GPUTextureUsage.COPY_DST |
184
- GPUTextureUsage.RENDER_ATTACHMENT,
185
- });
186
- device.queue.copyExternalImageToTexture(
187
- { source: imageBitmap },
188
- { texture: imageTexture },
189
- [imageBitmap.width, imageBitmap.height],
190
- );
191
- const textures = [0, 1].map(() => {
192
- return device.createTexture({
193
- size: {
194
- width: srcWidth,
195
- height: srcHeight,
196
- },
197
- format: "rgba8unorm",
198
- usage:
199
- GPUTextureUsage.COPY_DST |
200
- GPUTextureUsage.STORAGE_BINDING |
201
- GPUTextureUsage.TEXTURE_BINDING,
202
- });
203
- });
204
-
205
- const buffer0 = (() => {
206
- const buffer = device.createBuffer({
207
- size: 4,
208
- mappedAtCreation: true,
209
- usage: GPUBufferUsage.UNIFORM,
210
- });
211
- new Uint32Array(buffer.getMappedRange())[0] = 0;
212
- buffer.unmap();
213
- return buffer;
214
- })();
215
-
216
- const buffer1 = (() => {
217
- const buffer = device.createBuffer({
218
- size: 4,
219
- mappedAtCreation: true,
220
- usage: GPUBufferUsage.UNIFORM,
221
- });
222
- new Uint32Array(buffer.getMappedRange())[0] = 1;
223
- buffer.unmap();
224
- return buffer;
225
- })();
226
-
227
- const blurParamsBuffer = device.createBuffer({
228
- size: 8,
229
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM,
230
- });
231
-
232
- const computeConstants = device.createBindGroup({
233
- layout: blurPipeline.getBindGroupLayout(0),
234
- entries: [
235
- {
236
- binding: 0,
237
- resource: sampler,
238
- },
239
- {
240
- binding: 1,
241
- resource: {
242
- buffer: blurParamsBuffer,
243
- },
244
- },
245
- ],
246
- });
247
-
248
- const computeBindGroup0 = device.createBindGroup({
249
- layout: blurPipeline.getBindGroupLayout(1),
250
- entries: [
251
- {
252
- binding: 1,
253
- resource: imageTexture.createView(),
254
- },
255
- {
256
- binding: 2,
257
- resource: textures[0].createView(),
258
- },
259
- {
260
- binding: 3,
261
- resource: {
262
- buffer: buffer0,
263
- },
264
- },
265
- ],
266
- });
267
-
268
- const computeBindGroup1 = device.createBindGroup({
269
- layout: blurPipeline.getBindGroupLayout(1),
270
- entries: [
271
- {
272
- binding: 1,
273
- resource: textures[0].createView(),
274
- },
275
- {
276
- binding: 2,
277
- resource: textures[1].createView(),
278
- },
279
- {
280
- binding: 3,
281
- resource: {
282
- buffer: buffer1,
283
- },
284
- },
285
- ],
286
- });
287
-
288
- const computeBindGroup2 = device.createBindGroup({
289
- layout: blurPipeline.getBindGroupLayout(1),
290
- entries: [
291
- {
292
- binding: 1,
293
- resource: textures[1].createView(),
294
- },
295
- {
296
- binding: 2,
297
- resource: textures[0].createView(),
298
- },
299
- {
300
- binding: 3,
301
- resource: {
302
- buffer: buffer0,
303
- },
304
- },
305
- ],
306
- });
307
-
308
- const showResultBindGroup = device.createBindGroup({
309
- layout: fullscreenQuadPipeline.getBindGroupLayout(0),
310
- entries: [
311
- {
312
- binding: 0,
313
- resource: sampler,
314
- },
315
- {
316
- binding: 1,
317
- resource: textures[1].createView(),
318
- },
319
- ],
320
- });
321
-
322
- const settings = {
323
- filterSize: 15,
324
- iterations: 2,
325
- };
326
-
327
- let blockDim: number;
328
- const updateSettings = () => {
329
- blockDim = tileDim - (settings.filterSize - 1);
330
- device.queue.writeBuffer(
331
- blurParamsBuffer,
332
- 0,
333
- new Uint32Array([settings.filterSize, blockDim]),
334
- );
335
- };
336
- updateSettings();
337
-
338
- function frame() {
339
- const commandEncoder = device.createCommandEncoder();
340
-
341
- const computePass = commandEncoder.beginComputePass();
342
- computePass.setPipeline(blurPipeline);
343
- computePass.setBindGroup(0, computeConstants);
344
-
345
- computePass.setBindGroup(1, computeBindGroup0);
346
- computePass.dispatchWorkgroups(
347
- Math.ceil(srcWidth / blockDim),
348
- Math.ceil(srcHeight / batch[1]),
349
- );
350
-
351
- computePass.setBindGroup(1, computeBindGroup1);
352
- computePass.dispatchWorkgroups(
353
- Math.ceil(srcHeight / blockDim),
354
- Math.ceil(srcWidth / batch[1]),
355
- );
356
-
357
- for (let i = 0; i < settings.iterations - 1; ++i) {
358
- computePass.setBindGroup(1, computeBindGroup2);
359
- computePass.dispatchWorkgroups(
360
- Math.ceil(srcWidth / blockDim),
361
- Math.ceil(srcHeight / batch[1]),
362
- );
363
-
364
- computePass.setBindGroup(1, computeBindGroup1);
365
- computePass.dispatchWorkgroups(
366
- Math.ceil(srcHeight / blockDim),
367
- Math.ceil(srcWidth / batch[1]),
368
- );
369
- }
370
-
371
- computePass.end();
372
-
373
- const passEncoder = commandEncoder.beginRenderPass({
374
- colorAttachments: [
375
- {
376
- view: ctx.getCurrentTexture().createView(),
377
- clearValue: [0, 0, 0, 1],
378
- loadOp: "clear",
379
- storeOp: "store",
380
- },
381
- ],
382
- });
383
-
384
- passEncoder.setPipeline(fullscreenQuadPipeline);
385
- passEncoder.setBindGroup(0, showResultBindGroup);
386
- passEncoder.draw(6);
387
- passEncoder.end();
388
- device.queue.submit([commandEncoder.finish()]);
389
- }
390
- frame();
391
- return canvas.getImageData();
392
- },
393
- { blurWGSL: blur, fullscreenTexturedQuadWGSL: fullscreenTexturedQuad },
394
- );
395
- const image = encodeImage(result);
396
- checkImage(image, "snapshots/blur.png");
397
- });
398
- });