react-native-wgpu 0.2.10 → 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.
- package/README.md +155 -69
- package/android/CMakeLists.txt +4 -5
- package/android/build.gradle +7 -18
- package/android/src/main/java/com/webgpu/WebGPUViewPackage.java +34 -10
- package/apple/MetalView.mm +0 -19
- package/apple/WebGPUModule.h +1 -9
- package/apple/WebGPUModule.mm +0 -3
- package/apple/WebGPUView.h +0 -3
- package/apple/WebGPUView.mm +0 -2
- package/cpp/WGPULogger.h +10 -0
- package/cpp/dawn/dawn_proc_table.h +1 -1
- package/cpp/dawn/webgpu.h +4855 -0
- package/cpp/dawn/webgpu_cpp.h +10168 -0
- package/cpp/dawn/wire/client/webgpu.h +354 -0
- package/cpp/dawn/wire/client/webgpu_cpp.h +10343 -0
- package/cpp/dawn/wire/client/webgpu_cpp_print.h +2715 -0
- package/cpp/jsi/RNFJSIConverter.h +17 -59
- package/cpp/rnwgpu/RNWebGPUManager.cpp +1 -9
- package/cpp/rnwgpu/api/GPU.cpp +51 -26
- package/cpp/rnwgpu/api/GPU.h +5 -18
- package/cpp/rnwgpu/api/GPUAdapter.cpp +75 -54
- package/cpp/rnwgpu/api/GPUAdapter.h +6 -6
- package/cpp/rnwgpu/api/GPUAdapterInfo.h +0 -1
- package/cpp/rnwgpu/api/GPUBindGroup.h +1 -3
- package/cpp/rnwgpu/api/GPUBindGroupLayout.h +1 -3
- package/cpp/rnwgpu/api/GPUBuffer.cpp +35 -32
- package/cpp/rnwgpu/api/GPUBuffer.h +9 -7
- package/cpp/rnwgpu/api/GPUCanvasContext.cpp +5 -1
- package/cpp/rnwgpu/api/GPUCanvasContext.h +0 -2
- package/cpp/rnwgpu/api/GPUCommandBuffer.h +1 -3
- package/cpp/rnwgpu/api/GPUCommandEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPUCompilationInfo.h +0 -2
- package/cpp/rnwgpu/api/GPUCompilationMessage.h +1 -3
- package/cpp/rnwgpu/api/GPUComputePassEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPUComputePipeline.h +1 -3
- package/cpp/rnwgpu/api/GPUDevice.cpp +183 -128
- package/cpp/rnwgpu/api/GPUDevice.h +22 -21
- package/cpp/rnwgpu/api/GPUDeviceLostInfo.h +1 -3
- package/cpp/rnwgpu/api/GPUExternalTexture.h +1 -3
- package/cpp/rnwgpu/api/GPUPipelineLayout.h +1 -3
- package/cpp/rnwgpu/api/GPUQuerySet.h +1 -3
- package/cpp/rnwgpu/api/GPUQueue.cpp +19 -8
- package/cpp/rnwgpu/api/GPUQueue.h +7 -6
- package/cpp/rnwgpu/api/GPURenderBundle.h +1 -3
- package/cpp/rnwgpu/api/GPURenderBundleEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPURenderPassEncoder.h +1 -3
- package/cpp/rnwgpu/api/GPURenderPipeline.h +1 -3
- package/cpp/rnwgpu/api/GPUSampler.h +1 -3
- package/cpp/rnwgpu/api/GPUShaderModule.cpp +42 -28
- package/cpp/rnwgpu/api/GPUShaderModule.h +6 -6
- package/cpp/rnwgpu/api/GPUSupportedLimits.h +1 -3
- package/cpp/rnwgpu/api/GPUTexture.h +1 -3
- package/cpp/rnwgpu/api/GPUTextureView.h +1 -3
- package/cpp/rnwgpu/api/RNWebGPU.h +1 -7
- package/cpp/rnwgpu/async/AsyncDispatcher.h +28 -0
- package/cpp/rnwgpu/async/AsyncRunner.cpp +215 -0
- package/cpp/rnwgpu/async/AsyncRunner.h +53 -0
- package/cpp/rnwgpu/async/AsyncTaskHandle.cpp +181 -0
- package/cpp/rnwgpu/async/AsyncTaskHandle.h +55 -0
- package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp +23 -0
- package/cpp/rnwgpu/async/JSIMicrotaskDispatcher.h +22 -0
- package/cpp/webgpu/webgpu.h +5 -4827
- package/cpp/webgpu/webgpu_cpp.h +5 -10140
- package/cpp/{dawn/native/WebGPUBackend.h → webgpu/webgpu_cpp_print.h} +4 -20
- package/lib/commonjs/Canvas.js +6 -66
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/commonjs/hooks.js +6 -42
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/module/Canvas.js +7 -67
- package/lib/module/Canvas.js.map +1 -1
- package/lib/module/hooks.js +5 -40
- package/lib/module/hooks.js.map +1 -1
- package/lib/typescript/lib/commonjs/hooks.d.ts +1 -5
- package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/hooks.d.ts +1 -5
- package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +0 -1
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/hooks.d.ts +2 -7
- package/lib/typescript/src/hooks.d.ts.map +1 -1
- package/libs/android/arm64-v8a/libwebgpu_dawn.so +0 -0
- package/libs/android/armeabi-v7a/libwebgpu_dawn.so +0 -0
- package/libs/android/x86/libwebgpu_dawn.so +0 -0
- package/libs/android/x86_64/libwebgpu_dawn.so +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +5 -35
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
- package/package.json +4 -3
- package/react-native-wgpu.podspec +12 -16
- package/src/Canvas.tsx +8 -69
- package/src/hooks.tsx +14 -48
- package/android/cpp/platform/ThreadUtils.cpp +0 -41
- package/android/src/oldarch/com/webgpu/NativeWebGPUModuleSpec.java +0 -23
- package/android/src/oldarch/com/webgpu/WebGPUViewManagerSpec.java +0 -12
- package/apple/WebGPUViewManager.mm +0 -24
- package/apple/platform/ThreadUtils.cpp +0 -34
- package/cpp/dawn/dawn_proc.h +0 -50
- package/cpp/dawn/dawn_thread_dispatch_proc.h +0 -47
- package/cpp/dawn/native/D3D11Backend.h +0 -77
- package/cpp/dawn/native/D3D12Backend.h +0 -68
- package/cpp/dawn/native/D3DBackend.h +0 -56
- package/cpp/dawn/native/MetalBackend.h +0 -56
- package/cpp/dawn/platform/DawnPlatform.h +0 -167
- package/cpp/dawn/platform/dawn_platform_export.h +0 -49
- package/cpp/platform/ThreadUtils.h +0 -30
- package/cpp/rnwgpu/api/AsyncRunner.h +0 -30
- package/cpp/threading/CallInvokerDispatcher.h +0 -37
- package/cpp/threading/Dispatcher.cpp +0 -55
- package/cpp/threading/Dispatcher.h +0 -93
- package/cpp/threading/ThreadPool.cpp +0 -88
- package/cpp/threading/ThreadPool.h +0 -53
- package/cpp/webgpu/webgpu_glfw.h +0 -88
- package/lib/typescript/src/__tests__/Alpha.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Alpha.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ArrayBuffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Buffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ComputeShader.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Constants.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Constants.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Device.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Device.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ErrorScope.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ExternalTexture.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/GPU.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/GPU.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/ImageData.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Shaders.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/Texture.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/Texture.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts +0 -3
- package/lib/typescript/src/__tests__/components/Wireframe/Shaders.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts +0 -29
- package/lib/typescript/src/__tests__/components/Wireframe/models.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts +0 -5
- package/lib/typescript/src/__tests__/components/Wireframe/utils.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/cube.d.ts +0 -7
- package/lib/typescript/src/__tests__/components/cube.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts +0 -22
- package/lib/typescript/src/__tests__/components/meshes/mesh.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts +0 -12
- package/lib/typescript/src/__tests__/components/meshes/sphere.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts +0 -7
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragon.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts +0 -6
- package/lib/typescript/src/__tests__/components/meshes/stanfordDragonData.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts +0 -6
- package/lib/typescript/src/__tests__/components/meshes/teapot.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts +0 -10
- package/lib/typescript/src/__tests__/components/meshes/utils.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/components/triangle.d.ts +0 -3
- package/lib/typescript/src/__tests__/components/triangle.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/config.d.ts +0 -3
- package/lib/typescript/src/__tests__/config.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/ABuffer.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Blur.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Cube.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/FractalCube.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/OcclusionQuery.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/RenderBundles.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Triangle.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts +0 -2
- package/lib/typescript/src/__tests__/demos/Wireframe.spec.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/globalSetup.d.ts +0 -3
- package/lib/typescript/src/__tests__/globalSetup.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/globalTeardown.d.ts +0 -3
- package/lib/typescript/src/__tests__/globalTeardown.d.ts.map +0 -1
- package/lib/typescript/src/__tests__/setup.d.ts +0 -63
- package/lib/typescript/src/__tests__/setup.d.ts.map +0 -1
- package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
- package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
- package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
- package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
- package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
- package/libs/dawn.json +0 -4670
- package/src/__tests__/Alpha.spec.ts +0 -28
- package/src/__tests__/ArrayBuffer.spec.ts +0 -76
- package/src/__tests__/Buffer.spec.ts +0 -357
- package/src/__tests__/ComputeShader.spec.ts +0 -375
- package/src/__tests__/Constants.spec.ts +0 -89
- package/src/__tests__/Device.spec.ts +0 -84
- package/src/__tests__/ErrorScope.spec.ts +0 -92
- package/src/__tests__/ExternalTexture.spec.ts +0 -284
- package/src/__tests__/GPU.spec.ts +0 -272
- package/src/__tests__/ImageData.spec.ts +0 -26
- package/src/__tests__/Shaders.spec.ts +0 -232
- package/src/__tests__/Texture.spec.ts +0 -197
- package/src/__tests__/assets/Di-3d.png +0 -0
- package/src/__tests__/components/Wireframe/Shaders.ts +0 -138
- package/src/__tests__/components/Wireframe/models.ts +0 -113
- package/src/__tests__/components/Wireframe/utils.ts +0 -22
- package/src/__tests__/components/cube.ts +0 -51
- package/src/__tests__/components/meshes/mesh.ts +0 -96
- package/src/__tests__/components/meshes/sphere.ts +0 -103
- package/src/__tests__/components/meshes/stanfordDragon.ts +0 -44
- package/src/__tests__/components/meshes/stanfordDragonData.ts +0 -5
- package/src/__tests__/components/meshes/teapot.ts +0 -13
- package/src/__tests__/components/meshes/utils.ts +0 -235
- package/src/__tests__/components/triangle.ts +0 -17
- package/src/__tests__/config.ts +0 -2
- package/src/__tests__/demos/ABuffer.spec.ts +0 -890
- package/src/__tests__/demos/Blur.spec.ts +0 -398
- package/src/__tests__/demos/Cube.spec.ts +0 -929
- package/src/__tests__/demos/FractalCube.spec.ts +0 -240
- package/src/__tests__/demos/OcclusionQuery.spec.ts +0 -376
- package/src/__tests__/demos/RenderBundles.spec.ts +0 -580
- package/src/__tests__/demos/Triangle.spec.ts +0 -266
- package/src/__tests__/demos/Wireframe.spec.ts +0 -188
- package/src/__tests__/globalSetup.ts +0 -45
- package/src/__tests__/globalTeardown.ts +0 -11
- package/src/__tests__/setup.ts +0 -423
- package/src/__tests__/snapshots/abuffer.png +0 -0
- package/src/__tests__/snapshots/asteroid.png +0 -0
- package/src/__tests__/snapshots/blur.png +0 -0
- package/src/__tests__/snapshots/buffer.png +0 -0
- package/src/__tests__/snapshots/constant-triangle.png +0 -0
- package/src/__tests__/snapshots/cube.png +0 -0
- package/src/__tests__/snapshots/f.png +0 -0
- package/src/__tests__/snapshots/f2.png +0 -0
- package/src/__tests__/snapshots/fractal-cubes.png +0 -0
- package/src/__tests__/snapshots/instanced-cubes.png +0 -0
- package/src/__tests__/snapshots/occlusion-query.png +0 -0
- package/src/__tests__/snapshots/ref.png +0 -0
- package/src/__tests__/snapshots/semi-opaque-cyan.png +0 -0
- package/src/__tests__/snapshots/texture.png +0 -0
- package/src/__tests__/snapshots/textured-cube.png +0 -0
- package/src/__tests__/snapshots/triangle-msaa.png +0 -0
- package/src/__tests__/snapshots/triangle.png +0 -0
- package/src/__tests__/snapshots/two-cube.png +0 -0
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { checkImage, client, encodeImage } from "./setup";
|
|
2
|
-
|
|
3
|
-
describe("Alpha", () => {
|
|
4
|
-
it("Premultiplied Color", async () => {
|
|
5
|
-
const result = await client.eval(({ device, ctx, canvas }) => {
|
|
6
|
-
const commandEncoder = device.createCommandEncoder();
|
|
7
|
-
const textureView = ctx.getCurrentTexture().createView();
|
|
8
|
-
const alpha = 0.5;
|
|
9
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
10
|
-
colorAttachments: [
|
|
11
|
-
{
|
|
12
|
-
view: textureView,
|
|
13
|
-
clearValue: [0.3 * alpha, 0.6 * alpha, 1 * alpha, alpha],
|
|
14
|
-
loadOp: "clear",
|
|
15
|
-
storeOp: "store",
|
|
16
|
-
},
|
|
17
|
-
],
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
21
|
-
passEncoder.end();
|
|
22
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
23
|
-
return canvas.getImageData();
|
|
24
|
-
});
|
|
25
|
-
const image = encodeImage(result);
|
|
26
|
-
checkImage(image, "snapshots/semi-opaque-cyan.png");
|
|
27
|
-
});
|
|
28
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { client } from "./setup";
|
|
2
|
-
|
|
3
|
-
describe("Buffer", () => {
|
|
4
|
-
it("Array Buffer (1)", async () => {
|
|
5
|
-
const result = await client.eval(() => {
|
|
6
|
-
return Array.from(new Uint8Array([1.0, 2.0, 3.0, 4.0]));
|
|
7
|
-
});
|
|
8
|
-
expect(result.slice(0, 4)).toEqual([1, 2, 3, 4]);
|
|
9
|
-
});
|
|
10
|
-
it("Array Buffer (2)", async () => {
|
|
11
|
-
const result = await client.eval(({ device }) => {
|
|
12
|
-
const data = new Float32Array([1.0, 2.0, 3.0, 4.0]);
|
|
13
|
-
const bufferSize = data.byteLength; // 4 32-bit floats
|
|
14
|
-
const sourceBuffer = device.createBuffer({
|
|
15
|
-
size: bufferSize,
|
|
16
|
-
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Create a buffer for reading
|
|
20
|
-
const readBuffer = device.createBuffer({
|
|
21
|
-
size: bufferSize,
|
|
22
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// Create data to upload
|
|
26
|
-
|
|
27
|
-
// Write data to the source buffer
|
|
28
|
-
device.queue.writeBuffer(sourceBuffer, 0, data);
|
|
29
|
-
|
|
30
|
-
// Copy data from source buffer to read buffer
|
|
31
|
-
const encoder = device.createCommandEncoder();
|
|
32
|
-
encoder.copyBufferToBuffer(sourceBuffer, 0, readBuffer, 0, bufferSize);
|
|
33
|
-
device.queue.submit([encoder.finish()]);
|
|
34
|
-
return device.queue.onSubmittedWorkDone().then(() => {
|
|
35
|
-
// Map the read buffer for reading
|
|
36
|
-
return readBuffer.mapAsync(GPUMapMode.READ).then(() => {
|
|
37
|
-
const readData = new Float32Array(readBuffer.getMappedRange());
|
|
38
|
-
return Array.from(readData);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
expect(result).toEqual([1, 2, 3, 4]);
|
|
43
|
-
});
|
|
44
|
-
it("Array Buffer (3)", async () => {
|
|
45
|
-
const result = await client.eval(({ device }) => {
|
|
46
|
-
const data = new Float32Array([1.0, 2.0, 3.0, 4.0]);
|
|
47
|
-
const bufferSize = data.byteLength; // 4 32-bit floats
|
|
48
|
-
const sourceBuffer = device.createBuffer({
|
|
49
|
-
size: bufferSize,
|
|
50
|
-
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Create a buffer for reading
|
|
54
|
-
const readBuffer = device.createBuffer({
|
|
55
|
-
size: bufferSize,
|
|
56
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Create data to upload
|
|
60
|
-
|
|
61
|
-
// Write data to the source buffer
|
|
62
|
-
device.queue.writeBuffer(sourceBuffer, 0, data);
|
|
63
|
-
|
|
64
|
-
// Copy data from source buffer to read buffer
|
|
65
|
-
const encoder = device.createCommandEncoder();
|
|
66
|
-
encoder.copyBufferToBuffer(sourceBuffer, 0, readBuffer, 0, bufferSize);
|
|
67
|
-
device.queue.submit([encoder.finish()]);
|
|
68
|
-
// Map the read buffer for reading
|
|
69
|
-
return readBuffer.mapAsync(GPUMapMode.READ).then(() => {
|
|
70
|
-
const readData = new Float32Array(readBuffer.getMappedRange());
|
|
71
|
-
return Array.from(readData);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
expect(result).toEqual([1, 2, 3, 4]);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
import { checkImage, client, encodeImage } from "./setup";
|
|
2
|
-
|
|
3
|
-
describe("Buffer", () => {
|
|
4
|
-
it("Label", async () => {
|
|
5
|
-
const result = await client.eval(
|
|
6
|
-
({ device, assets: { cubeVertexArray } }) => {
|
|
7
|
-
const buffer1 = device.createBuffer({
|
|
8
|
-
size: cubeVertexArray.byteLength,
|
|
9
|
-
usage: GPUBufferUsage.VERTEX,
|
|
10
|
-
mappedAtCreation: true,
|
|
11
|
-
});
|
|
12
|
-
const buffer2 = device.createBuffer({
|
|
13
|
-
size: cubeVertexArray.byteLength,
|
|
14
|
-
usage: GPUBufferUsage.VERTEX,
|
|
15
|
-
mappedAtCreation: true,
|
|
16
|
-
label: "verticesBuffer",
|
|
17
|
-
});
|
|
18
|
-
return [buffer1.label, buffer2.label];
|
|
19
|
-
},
|
|
20
|
-
);
|
|
21
|
-
expect(result).toEqual(["", "verticesBuffer"]);
|
|
22
|
-
});
|
|
23
|
-
it("metadata", async () => {
|
|
24
|
-
const result = await client.eval(
|
|
25
|
-
({ device, assets: { cubeVertexArray } }) => {
|
|
26
|
-
const buffer = device.createBuffer({
|
|
27
|
-
size: cubeVertexArray.byteLength,
|
|
28
|
-
usage: GPUBufferUsage.VERTEX,
|
|
29
|
-
mappedAtCreation: true,
|
|
30
|
-
});
|
|
31
|
-
return [buffer.label, buffer.size, buffer.usage, buffer.mapState];
|
|
32
|
-
},
|
|
33
|
-
);
|
|
34
|
-
expect(result).toEqual(["", 1440, 32, "mapped"]);
|
|
35
|
-
});
|
|
36
|
-
it("metadata (1)", async () => {
|
|
37
|
-
const result = await client.eval(
|
|
38
|
-
({ device, assets: { cubeVertexArray } }) => {
|
|
39
|
-
const buffer = device.createBuffer({
|
|
40
|
-
size: cubeVertexArray.byteLength,
|
|
41
|
-
usage: GPUBufferUsage.VERTEX,
|
|
42
|
-
mappedAtCreation: false,
|
|
43
|
-
});
|
|
44
|
-
return [buffer.label, buffer.size, buffer.usage, buffer.mapState];
|
|
45
|
-
},
|
|
46
|
-
);
|
|
47
|
-
expect(result).toEqual(["", 1440, 32, "unmapped"]);
|
|
48
|
-
});
|
|
49
|
-
it("upload data (1)", async () => {
|
|
50
|
-
const result = await client.eval(
|
|
51
|
-
({ device, assets: { cubeVertexArray } }) => {
|
|
52
|
-
const verticesBuffer = device.createBuffer({
|
|
53
|
-
size: cubeVertexArray.byteLength,
|
|
54
|
-
usage: GPUBufferUsage.VERTEX,
|
|
55
|
-
mappedAtCreation: true,
|
|
56
|
-
label: "verticesBuffer",
|
|
57
|
-
});
|
|
58
|
-
new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray);
|
|
59
|
-
verticesBuffer.unmap();
|
|
60
|
-
return !!verticesBuffer;
|
|
61
|
-
},
|
|
62
|
-
);
|
|
63
|
-
expect(result).toBe(true);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("upload data (2)", async () => {
|
|
67
|
-
const result = await client.eval(
|
|
68
|
-
({ device, assets: { cubeVertexArray } }) => {
|
|
69
|
-
const verticesBuffer = device.createBuffer({
|
|
70
|
-
size: cubeVertexArray.byteLength,
|
|
71
|
-
usage: GPUBufferUsage.VERTEX,
|
|
72
|
-
mappedAtCreation: true,
|
|
73
|
-
label: "verticesBuffer",
|
|
74
|
-
});
|
|
75
|
-
new Float32Array(
|
|
76
|
-
verticesBuffer.getMappedRange(0, cubeVertexArray.byteLength),
|
|
77
|
-
).set(cubeVertexArray);
|
|
78
|
-
verticesBuffer.unmap();
|
|
79
|
-
return !!verticesBuffer;
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
expect(result).toBe(true);
|
|
83
|
-
});
|
|
84
|
-
it("writes into a buffer (1)", async () => {
|
|
85
|
-
const result = await client.eval(({ device }) => {
|
|
86
|
-
const bufferSize = 4 * 4; // 4 32-bit floats
|
|
87
|
-
const sourceBuffer = device.createBuffer({
|
|
88
|
-
size: bufferSize,
|
|
89
|
-
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// Create data to upload
|
|
93
|
-
const data = new Float32Array([1.0, 2.0, 3.0, 4.0]);
|
|
94
|
-
|
|
95
|
-
// Write data to the source buffer
|
|
96
|
-
device.queue.writeBuffer(sourceBuffer, 0, data);
|
|
97
|
-
return true;
|
|
98
|
-
});
|
|
99
|
-
expect(result).toBe(true);
|
|
100
|
-
});
|
|
101
|
-
it("read/write buffer (1)", async () => {
|
|
102
|
-
const result = await client.eval(({ device }) => {
|
|
103
|
-
const data = new Uint32Array([1.0, 2.0, 3.0, 4.0]);
|
|
104
|
-
// Create a GPU buffer and store data
|
|
105
|
-
const gpuBuffer = device.createBuffer({
|
|
106
|
-
size: data.byteLength,
|
|
107
|
-
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
|
|
108
|
-
mappedAtCreation: false,
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// Copy data to the GPU buffer
|
|
112
|
-
device.queue.writeBuffer(gpuBuffer, 0, data);
|
|
113
|
-
|
|
114
|
-
return gpuBuffer.mapAsync(GPUMapMode.READ).then(() => {
|
|
115
|
-
const arrayBuffer = gpuBuffer.getMappedRange();
|
|
116
|
-
const readData = new Uint32Array(arrayBuffer);
|
|
117
|
-
const r = Array.from(readData);
|
|
118
|
-
gpuBuffer.unmap();
|
|
119
|
-
return r;
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
expect(result).toEqual([1, 2, 3, 4]);
|
|
123
|
-
});
|
|
124
|
-
it("read/write buffer (2)", async () => {
|
|
125
|
-
const result = await client.eval(({ device }) => {
|
|
126
|
-
const data = new Uint32Array([1.0, 2.0, 3.0, 4.0]);
|
|
127
|
-
// Create a GPU buffer and store data
|
|
128
|
-
const gpuBuffer = device.createBuffer({
|
|
129
|
-
size: data.byteLength,
|
|
130
|
-
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
|
|
131
|
-
mappedAtCreation: false,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Copy data to the GPU buffer
|
|
135
|
-
device.queue.writeBuffer(gpuBuffer, 0, data.buffer, 0, data.byteLength);
|
|
136
|
-
|
|
137
|
-
return gpuBuffer
|
|
138
|
-
.mapAsync(GPUMapMode.READ, 0, data.byteLength)
|
|
139
|
-
.then(() => {
|
|
140
|
-
const arrayBuffer = gpuBuffer.getMappedRange(0, data.byteLength);
|
|
141
|
-
const readData = new Uint32Array(arrayBuffer);
|
|
142
|
-
const r = Array.from(readData);
|
|
143
|
-
gpuBuffer.unmap();
|
|
144
|
-
return r;
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
expect(result).toEqual([1, 2, 3, 4]);
|
|
148
|
-
});
|
|
149
|
-
it("read/write buffer (3)", async () => {
|
|
150
|
-
const result = await client.eval(({ device }) => {
|
|
151
|
-
const data = new Uint32Array([1.0, 2.0, 3.0, 4.0]);
|
|
152
|
-
// Create a GPU buffer and store data
|
|
153
|
-
const gpuBuffer = device.createBuffer({
|
|
154
|
-
size: data.byteLength,
|
|
155
|
-
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
|
|
156
|
-
mappedAtCreation: false,
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
// Copy data to the GPU buffer
|
|
160
|
-
device.queue.writeBuffer(gpuBuffer, 0, data.buffer, 0, data.byteLength);
|
|
161
|
-
|
|
162
|
-
return gpuBuffer
|
|
163
|
-
.mapAsync(GPUMapMode.READ, 0, data.byteLength)
|
|
164
|
-
.then(() => {
|
|
165
|
-
const arrayBuffer = gpuBuffer.getMappedRange(0, data.byteLength);
|
|
166
|
-
const readData = new Float32Array(arrayBuffer);
|
|
167
|
-
const r = Array.from(readData);
|
|
168
|
-
gpuBuffer.unmap();
|
|
169
|
-
return r;
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
const ref = new Uint32Array([1.0, 2.0, 3.0, 4.0]);
|
|
173
|
-
expect(result).toEqual(Array.from(new Float32Array(ref.buffer)));
|
|
174
|
-
});
|
|
175
|
-
it("writes into a buffer (2)", async () => {
|
|
176
|
-
const result = await client.eval(({ device }) => {
|
|
177
|
-
const bufferSize = 4 * 4; // 4 32-bit floats
|
|
178
|
-
const sourceBuffer = device.createBuffer({
|
|
179
|
-
size: bufferSize,
|
|
180
|
-
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
// Create a buffer for reading
|
|
184
|
-
const readBuffer = device.createBuffer({
|
|
185
|
-
size: bufferSize,
|
|
186
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// Create data to upload
|
|
190
|
-
const data = new Float32Array([1.0, 2.0, 3.0, 4.0]);
|
|
191
|
-
|
|
192
|
-
// Write data to the source buffer
|
|
193
|
-
device.queue.writeBuffer(sourceBuffer, 0, data);
|
|
194
|
-
|
|
195
|
-
// Copy data from source buffer to read buffer
|
|
196
|
-
const encoder = device.createCommandEncoder();
|
|
197
|
-
encoder.copyBufferToBuffer(sourceBuffer, 0, readBuffer, 0, bufferSize);
|
|
198
|
-
device.queue.submit([encoder.finish()]);
|
|
199
|
-
// Map the read buffer for reading
|
|
200
|
-
return readBuffer.mapAsync(GPUMapMode.READ).then(() => {
|
|
201
|
-
const readData = new Float32Array(readBuffer.getMappedRange());
|
|
202
|
-
return Array.from(readData);
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
expect(result).toEqual([1, 2, 3, 4]);
|
|
206
|
-
});
|
|
207
|
-
it("writes into a buffer (3)", async () => {
|
|
208
|
-
const result = await client.eval(({ device }) => {
|
|
209
|
-
const bufferSize = 4 * 4; // 4 32-bit floats
|
|
210
|
-
const sourceBuffer = device.createBuffer({
|
|
211
|
-
size: bufferSize,
|
|
212
|
-
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// Create a buffer for reading
|
|
216
|
-
const readBuffer = device.createBuffer({
|
|
217
|
-
size: bufferSize,
|
|
218
|
-
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
// Create data to upload
|
|
222
|
-
const data = new Float32Array([1.0, 2.0, 3.0, 4.0]);
|
|
223
|
-
|
|
224
|
-
// Write data to the source buffer
|
|
225
|
-
device.queue.writeBuffer(sourceBuffer, 0, data);
|
|
226
|
-
|
|
227
|
-
// Copy data from source buffer to read buffer
|
|
228
|
-
const encoder = device.createCommandEncoder();
|
|
229
|
-
encoder.copyBufferToBuffer(sourceBuffer, 0, readBuffer, 0, bufferSize);
|
|
230
|
-
device.queue.submit([encoder.finish()]);
|
|
231
|
-
// Map the read buffer for reading
|
|
232
|
-
return readBuffer.mapAsync(GPUMapMode.READ).then(() => {
|
|
233
|
-
//const readData = new Float32Array(readBuffer.getMappedRange());
|
|
234
|
-
readBuffer.unmap();
|
|
235
|
-
// // Create a buffer for writing
|
|
236
|
-
const writeBuffer = device.createBuffer({
|
|
237
|
-
size: bufferSize,
|
|
238
|
-
usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.MAP_WRITE,
|
|
239
|
-
});
|
|
240
|
-
// Map the write buffer for writing
|
|
241
|
-
return writeBuffer.mapAsync(GPUMapMode.WRITE).then(() => {
|
|
242
|
-
const writeData = new Float32Array(writeBuffer.getMappedRange());
|
|
243
|
-
writeData.set([5.0, 6.0, 7.0, 8.0]);
|
|
244
|
-
writeBuffer.unmap();
|
|
245
|
-
// // Copy data from write buffer to source buffer
|
|
246
|
-
const encoder2 = device.createCommandEncoder();
|
|
247
|
-
encoder2.copyBufferToBuffer(
|
|
248
|
-
writeBuffer,
|
|
249
|
-
0,
|
|
250
|
-
sourceBuffer,
|
|
251
|
-
0,
|
|
252
|
-
bufferSize,
|
|
253
|
-
);
|
|
254
|
-
device.queue.submit([encoder2.finish()]);
|
|
255
|
-
// // Copy data from source buffer to read buffer
|
|
256
|
-
const encoder3 = device.createCommandEncoder();
|
|
257
|
-
encoder3.copyBufferToBuffer(
|
|
258
|
-
sourceBuffer,
|
|
259
|
-
0,
|
|
260
|
-
readBuffer,
|
|
261
|
-
0,
|
|
262
|
-
bufferSize,
|
|
263
|
-
);
|
|
264
|
-
device.queue.submit([encoder3.finish()]);
|
|
265
|
-
// Map the read buffer for final reading
|
|
266
|
-
return readBuffer.mapAsync(GPUMapMode.READ).then(() => {
|
|
267
|
-
const finalReadData = new Float32Array(readBuffer.getMappedRange());
|
|
268
|
-
const res = Array.from(finalReadData);
|
|
269
|
-
readBuffer.unmap();
|
|
270
|
-
return res;
|
|
271
|
-
});
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
expect(result).toEqual([5, 6, 7, 8]);
|
|
276
|
-
});
|
|
277
|
-
it("Builds the reference result", async () => {
|
|
278
|
-
const data = await client.eval(() => {
|
|
279
|
-
const pixels = new Uint8Array(256 * 256 * 4);
|
|
280
|
-
pixels.fill(255);
|
|
281
|
-
let i = 0;
|
|
282
|
-
for (let x = 0; x < 256 * 4; x++) {
|
|
283
|
-
for (let y = 0; y < 256 * 4; y++) {
|
|
284
|
-
pixels[i++] = (x * y) % 255;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return Array.from(pixels);
|
|
288
|
-
});
|
|
289
|
-
const png = encodeImage({
|
|
290
|
-
data,
|
|
291
|
-
width: 256,
|
|
292
|
-
height: 256,
|
|
293
|
-
format: "rgba8unorm",
|
|
294
|
-
});
|
|
295
|
-
checkImage(png, "snapshots/buffer.png");
|
|
296
|
-
});
|
|
297
|
-
it("Builds the reference result (2)", async () => {
|
|
298
|
-
const data = new Uint8Array(256 * 256 * 4);
|
|
299
|
-
data.fill(255);
|
|
300
|
-
let i = 0;
|
|
301
|
-
for (let x = 0; x < 256 * 4; x++) {
|
|
302
|
-
for (let y = 0; y < 256 * 4; y++) {
|
|
303
|
-
data[i++] = (x * y) % 255;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
const result = await client.eval(
|
|
307
|
-
({ pixels }) => {
|
|
308
|
-
return Array.from(pixels);
|
|
309
|
-
},
|
|
310
|
-
{ pixels: Array.from(data) },
|
|
311
|
-
);
|
|
312
|
-
const png = encodeImage({
|
|
313
|
-
data: result,
|
|
314
|
-
width: 256,
|
|
315
|
-
height: 256,
|
|
316
|
-
format: "rgba8unorm",
|
|
317
|
-
});
|
|
318
|
-
checkImage(png, "snapshots/buffer.png");
|
|
319
|
-
});
|
|
320
|
-
it("writes an image into a buffer and reads it back", async () => {
|
|
321
|
-
const imageData = await client.eval(({ device }) => {
|
|
322
|
-
const data = new Uint8Array(256 * 256 * 4);
|
|
323
|
-
data.fill(255);
|
|
324
|
-
let i = 0;
|
|
325
|
-
for (let x = 0; x < 256 * 4; x++) {
|
|
326
|
-
for (let y = 0; y < 256 * 4; y++) {
|
|
327
|
-
data[i++] = (x * y) % 255;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
const gpuBuffer = device.createBuffer({
|
|
331
|
-
size: 256 * 256 * 4,
|
|
332
|
-
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST,
|
|
333
|
-
mappedAtCreation: false,
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
// Copy data to the GPU buffer
|
|
337
|
-
device.queue.writeBuffer(gpuBuffer, 0, data.buffer, 0, data.byteLength);
|
|
338
|
-
|
|
339
|
-
return gpuBuffer
|
|
340
|
-
.mapAsync(GPUMapMode.READ, 0, data.byteLength)
|
|
341
|
-
.then(() => {
|
|
342
|
-
const arrayBuffer = gpuBuffer.getMappedRange(0, data.byteLength);
|
|
343
|
-
const readData = new Uint8Array(arrayBuffer);
|
|
344
|
-
const r = Array.from(readData);
|
|
345
|
-
gpuBuffer.unmap();
|
|
346
|
-
return r;
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
const png = encodeImage({
|
|
350
|
-
data: imageData,
|
|
351
|
-
width: 256,
|
|
352
|
-
height: 256,
|
|
353
|
-
format: "rgba8unorm",
|
|
354
|
-
});
|
|
355
|
-
checkImage(png, "snapshots/buffer.png");
|
|
356
|
-
});
|
|
357
|
-
});
|