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,929 +0,0 @@
|
|
|
1
|
-
import type { Mat4 } from "wgpu-matrix";
|
|
2
|
-
|
|
3
|
-
import { checkImage, client, encodeImage } from "../setup";
|
|
4
|
-
|
|
5
|
-
export const basicVert = /*wgsl*/ `struct Uniforms {
|
|
6
|
-
modelViewProjectionMatrix : mat4x4f,
|
|
7
|
-
}
|
|
8
|
-
@binding(0) @group(0) var<uniform> uniforms : Uniforms;
|
|
9
|
-
|
|
10
|
-
struct VertexOutput {
|
|
11
|
-
@builtin(position) Position : vec4f,
|
|
12
|
-
@location(0) fragUV : vec2f,
|
|
13
|
-
@location(1) fragPosition: vec4f,
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@vertex
|
|
17
|
-
fn main(
|
|
18
|
-
@location(0) position : vec4f,
|
|
19
|
-
@location(1) uv : vec2f
|
|
20
|
-
) -> VertexOutput {
|
|
21
|
-
var output : VertexOutput;
|
|
22
|
-
output.Position = uniforms.modelViewProjectionMatrix * position;
|
|
23
|
-
output.fragUV = uv;
|
|
24
|
-
output.fragPosition = 0.5 * (position + vec4(1.0, 1.0, 1.0, 1.0));
|
|
25
|
-
return output;
|
|
26
|
-
}
|
|
27
|
-
`;
|
|
28
|
-
|
|
29
|
-
const vertexPositionColor = /*wgsl*/ `@fragment
|
|
30
|
-
fn main(
|
|
31
|
-
@location(0) fragUV: vec2f,
|
|
32
|
-
@location(1) fragPosition: vec4f
|
|
33
|
-
) -> @location(0) vec4f {
|
|
34
|
-
return fragPosition;
|
|
35
|
-
}`;
|
|
36
|
-
|
|
37
|
-
const sampleTextureMixColor = /*wgsl*/ `@group(0) @binding(1) var mySampler: sampler;
|
|
38
|
-
@group(0) @binding(2) var myTexture: texture_2d<f32>;
|
|
39
|
-
|
|
40
|
-
@fragment
|
|
41
|
-
fn main(
|
|
42
|
-
@location(0) fragUV: vec2f,
|
|
43
|
-
@location(1) fragPosition: vec4f
|
|
44
|
-
) -> @location(0) vec4f {
|
|
45
|
-
return textureSample(myTexture, mySampler, fragUV) * fragPosition;
|
|
46
|
-
}
|
|
47
|
-
`;
|
|
48
|
-
|
|
49
|
-
const instancedVert = /*wgsl*/ `struct Uniforms {
|
|
50
|
-
modelViewProjectionMatrix : array<mat4x4f, 16>,
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
@binding(0) @group(0) var<uniform> uniforms : Uniforms;
|
|
54
|
-
|
|
55
|
-
struct VertexOutput {
|
|
56
|
-
@builtin(position) Position : vec4f,
|
|
57
|
-
@location(0) fragUV : vec2f,
|
|
58
|
-
@location(1) fragPosition: vec4f,
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
@vertex
|
|
62
|
-
fn main(
|
|
63
|
-
@builtin(instance_index) instanceIdx : u32,
|
|
64
|
-
@location(0) position : vec4f,
|
|
65
|
-
@location(1) uv : vec2f
|
|
66
|
-
) -> VertexOutput {
|
|
67
|
-
var output : VertexOutput;
|
|
68
|
-
output.Position = uniforms.modelViewProjectionMatrix[instanceIdx] * position;
|
|
69
|
-
output.fragUV = uv;
|
|
70
|
-
output.fragPosition = 0.5 * (position + vec4(1.0));
|
|
71
|
-
return output;
|
|
72
|
-
}
|
|
73
|
-
`;
|
|
74
|
-
|
|
75
|
-
describe("Cube", () => {
|
|
76
|
-
it("Rotating cube", async () => {
|
|
77
|
-
const result = await client.eval(
|
|
78
|
-
({
|
|
79
|
-
gpu,
|
|
80
|
-
device,
|
|
81
|
-
assets: { cubeVertexArray },
|
|
82
|
-
ctx,
|
|
83
|
-
mat4,
|
|
84
|
-
vec3,
|
|
85
|
-
basicVertWGSL,
|
|
86
|
-
vertexPositionColorWGSL,
|
|
87
|
-
canvas,
|
|
88
|
-
}) => {
|
|
89
|
-
const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
|
|
90
|
-
const cubePositionOffset = 0;
|
|
91
|
-
const cubeUVOffset = 4 * 8;
|
|
92
|
-
const cubeVertexCount = 36;
|
|
93
|
-
|
|
94
|
-
const presentationFormat = gpu.getPreferredCanvasFormat();
|
|
95
|
-
// Create a vertex buffer from the cube data.
|
|
96
|
-
const verticesBuffer = device.createBuffer({
|
|
97
|
-
size: cubeVertexArray.byteLength,
|
|
98
|
-
usage: GPUBufferUsage.VERTEX,
|
|
99
|
-
mappedAtCreation: true,
|
|
100
|
-
});
|
|
101
|
-
new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray);
|
|
102
|
-
verticesBuffer.unmap();
|
|
103
|
-
|
|
104
|
-
const pipeline = device.createRenderPipeline({
|
|
105
|
-
label: "cube render pipeline",
|
|
106
|
-
layout: "auto",
|
|
107
|
-
vertex: {
|
|
108
|
-
module: device.createShaderModule({
|
|
109
|
-
code: basicVertWGSL,
|
|
110
|
-
}),
|
|
111
|
-
buffers: [
|
|
112
|
-
{
|
|
113
|
-
arrayStride: cubeVertexSize,
|
|
114
|
-
attributes: [
|
|
115
|
-
{
|
|
116
|
-
// position
|
|
117
|
-
shaderLocation: 0,
|
|
118
|
-
offset: cubePositionOffset,
|
|
119
|
-
format: "float32x4",
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
// uv
|
|
123
|
-
shaderLocation: 1,
|
|
124
|
-
offset: cubeUVOffset,
|
|
125
|
-
format: "float32x2",
|
|
126
|
-
},
|
|
127
|
-
],
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
},
|
|
131
|
-
fragment: {
|
|
132
|
-
module: device.createShaderModule({
|
|
133
|
-
code: vertexPositionColorWGSL,
|
|
134
|
-
}),
|
|
135
|
-
targets: [
|
|
136
|
-
{
|
|
137
|
-
format: presentationFormat,
|
|
138
|
-
},
|
|
139
|
-
],
|
|
140
|
-
},
|
|
141
|
-
primitive: {
|
|
142
|
-
topology: "triangle-list",
|
|
143
|
-
|
|
144
|
-
// Backface culling since the cube is solid piece of geometry.
|
|
145
|
-
// Faces pointing away from the camera will be occluded by faces
|
|
146
|
-
// pointing toward the camera.
|
|
147
|
-
cullMode: "back",
|
|
148
|
-
},
|
|
149
|
-
|
|
150
|
-
// Enable depth testing so that the fragment closest to the camera
|
|
151
|
-
// is rendered in front.
|
|
152
|
-
depthStencil: {
|
|
153
|
-
depthWriteEnabled: true,
|
|
154
|
-
depthCompare: "less",
|
|
155
|
-
format: "depth24plus",
|
|
156
|
-
},
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const depthTexture = device.createTexture({
|
|
160
|
-
size: [ctx.canvas.width, ctx.canvas.height],
|
|
161
|
-
format: "depth24plus",
|
|
162
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const uniformBufferSize = 4 * 16; // 4x4 matrix
|
|
166
|
-
const uniformBuffer = device.createBuffer({
|
|
167
|
-
size: uniformBufferSize,
|
|
168
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const uniformBindGroup = device.createBindGroup({
|
|
172
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
173
|
-
entries: [
|
|
174
|
-
{
|
|
175
|
-
binding: 0,
|
|
176
|
-
resource: {
|
|
177
|
-
buffer: uniformBuffer,
|
|
178
|
-
},
|
|
179
|
-
},
|
|
180
|
-
],
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
184
|
-
colorAttachments: [
|
|
185
|
-
{
|
|
186
|
-
view: ctx.getCurrentTexture().createView(), // Assigned later
|
|
187
|
-
clearValue: [0.5, 0.5, 0.5, 1.0],
|
|
188
|
-
loadOp: "clear",
|
|
189
|
-
storeOp: "store",
|
|
190
|
-
},
|
|
191
|
-
],
|
|
192
|
-
depthStencilAttachment: {
|
|
193
|
-
view: depthTexture.createView(),
|
|
194
|
-
depthClearValue: 1.0,
|
|
195
|
-
depthLoadOp: "clear",
|
|
196
|
-
depthStoreOp: "store",
|
|
197
|
-
},
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
const aspect = ctx.canvas.width / ctx.canvas.height;
|
|
201
|
-
const projectionMatrix = mat4.perspective(
|
|
202
|
-
(2 * Math.PI) / 5,
|
|
203
|
-
aspect,
|
|
204
|
-
1,
|
|
205
|
-
100.0,
|
|
206
|
-
);
|
|
207
|
-
const modelViewProjectionMatrix = mat4.create();
|
|
208
|
-
|
|
209
|
-
function getTransformationMatrix() {
|
|
210
|
-
const viewMatrix = mat4.identity();
|
|
211
|
-
mat4.translate(viewMatrix, vec3.fromValues(0, 0, -4), viewMatrix);
|
|
212
|
-
const now = 1721677648;
|
|
213
|
-
mat4.rotate(
|
|
214
|
-
viewMatrix,
|
|
215
|
-
vec3.fromValues(Math.sin(now), Math.cos(now), 0),
|
|
216
|
-
1,
|
|
217
|
-
viewMatrix,
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
mat4.multiply(
|
|
221
|
-
projectionMatrix,
|
|
222
|
-
viewMatrix,
|
|
223
|
-
modelViewProjectionMatrix,
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
return modelViewProjectionMatrix;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const transformationMatrix = getTransformationMatrix();
|
|
230
|
-
device.queue.writeBuffer(
|
|
231
|
-
uniformBuffer,
|
|
232
|
-
0,
|
|
233
|
-
transformationMatrix.buffer,
|
|
234
|
-
transformationMatrix.byteOffset,
|
|
235
|
-
transformationMatrix.byteLength,
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
const commandEncoder = device.createCommandEncoder();
|
|
239
|
-
const passEncoder =
|
|
240
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
241
|
-
passEncoder.setPipeline(pipeline);
|
|
242
|
-
passEncoder.setBindGroup(0, uniformBindGroup);
|
|
243
|
-
passEncoder.setVertexBuffer(0, verticesBuffer);
|
|
244
|
-
passEncoder.draw(cubeVertexCount);
|
|
245
|
-
passEncoder.end();
|
|
246
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
247
|
-
return canvas.getImageData();
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
basicVertWGSL: basicVert,
|
|
251
|
-
vertexPositionColorWGSL: vertexPositionColor,
|
|
252
|
-
},
|
|
253
|
-
);
|
|
254
|
-
const image = encodeImage(result);
|
|
255
|
-
checkImage(image, "snapshots/cube.png");
|
|
256
|
-
});
|
|
257
|
-
it("Two cubes", async () => {
|
|
258
|
-
const result = await client.eval(
|
|
259
|
-
({
|
|
260
|
-
gpu,
|
|
261
|
-
device,
|
|
262
|
-
assets: { cubeVertexArray },
|
|
263
|
-
ctx,
|
|
264
|
-
mat4,
|
|
265
|
-
vec3,
|
|
266
|
-
basicVertWGSL,
|
|
267
|
-
vertexPositionColorWGSL,
|
|
268
|
-
canvas,
|
|
269
|
-
}) => {
|
|
270
|
-
const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
|
|
271
|
-
const cubePositionOffset = 0;
|
|
272
|
-
const cubeUVOffset = 4 * 8;
|
|
273
|
-
const cubeVertexCount = 36;
|
|
274
|
-
const presentationFormat = gpu.getPreferredCanvasFormat();
|
|
275
|
-
// Create a vertex buffer from the cube data.
|
|
276
|
-
const verticesBuffer = device.createBuffer({
|
|
277
|
-
size: cubeVertexArray.byteLength,
|
|
278
|
-
usage: GPUBufferUsage.VERTEX,
|
|
279
|
-
mappedAtCreation: true,
|
|
280
|
-
});
|
|
281
|
-
new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray);
|
|
282
|
-
verticesBuffer.unmap();
|
|
283
|
-
|
|
284
|
-
const pipeline = device.createRenderPipeline({
|
|
285
|
-
layout: "auto",
|
|
286
|
-
vertex: {
|
|
287
|
-
module: device.createShaderModule({
|
|
288
|
-
code: basicVertWGSL,
|
|
289
|
-
}),
|
|
290
|
-
buffers: [
|
|
291
|
-
{
|
|
292
|
-
arrayStride: cubeVertexSize,
|
|
293
|
-
attributes: [
|
|
294
|
-
{
|
|
295
|
-
// position
|
|
296
|
-
shaderLocation: 0,
|
|
297
|
-
offset: cubePositionOffset,
|
|
298
|
-
format: "float32x4",
|
|
299
|
-
},
|
|
300
|
-
{
|
|
301
|
-
// uv
|
|
302
|
-
shaderLocation: 1,
|
|
303
|
-
offset: cubeUVOffset,
|
|
304
|
-
format: "float32x2",
|
|
305
|
-
},
|
|
306
|
-
],
|
|
307
|
-
},
|
|
308
|
-
],
|
|
309
|
-
},
|
|
310
|
-
fragment: {
|
|
311
|
-
module: device.createShaderModule({
|
|
312
|
-
code: vertexPositionColorWGSL,
|
|
313
|
-
}),
|
|
314
|
-
targets: [
|
|
315
|
-
{
|
|
316
|
-
format: presentationFormat,
|
|
317
|
-
},
|
|
318
|
-
],
|
|
319
|
-
},
|
|
320
|
-
primitive: {
|
|
321
|
-
topology: "triangle-list",
|
|
322
|
-
|
|
323
|
-
// Backface culling since the cube is solid piece of geometry.
|
|
324
|
-
// Faces pointing away from the camera will be occluded by faces
|
|
325
|
-
// pointing toward the camera.
|
|
326
|
-
cullMode: "back",
|
|
327
|
-
},
|
|
328
|
-
|
|
329
|
-
// Enable depth testing so that the fragment closest to the camera
|
|
330
|
-
// is rendered in front.
|
|
331
|
-
depthStencil: {
|
|
332
|
-
depthWriteEnabled: true,
|
|
333
|
-
depthCompare: "less",
|
|
334
|
-
format: "depth24plus",
|
|
335
|
-
},
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
const depthTexture = device.createTexture({
|
|
339
|
-
size: [ctx.canvas.width, ctx.canvas.height],
|
|
340
|
-
format: "depth24plus",
|
|
341
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
const matrixSize = 4 * 16; // 4x4 matrix
|
|
345
|
-
const offset = 256; // uniformBindGroup offset must be 256-byte aligned
|
|
346
|
-
const uniformBufferSize = offset + matrixSize;
|
|
347
|
-
|
|
348
|
-
const uniformBuffer = device.createBuffer({
|
|
349
|
-
size: uniformBufferSize,
|
|
350
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
const uniformBindGroup1 = device.createBindGroup({
|
|
354
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
355
|
-
entries: [
|
|
356
|
-
{
|
|
357
|
-
binding: 0,
|
|
358
|
-
resource: {
|
|
359
|
-
buffer: uniformBuffer,
|
|
360
|
-
offset: 0,
|
|
361
|
-
size: matrixSize,
|
|
362
|
-
},
|
|
363
|
-
},
|
|
364
|
-
],
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
const uniformBindGroup2 = device.createBindGroup({
|
|
368
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
369
|
-
entries: [
|
|
370
|
-
{
|
|
371
|
-
binding: 0,
|
|
372
|
-
resource: {
|
|
373
|
-
buffer: uniformBuffer,
|
|
374
|
-
offset: offset,
|
|
375
|
-
size: matrixSize,
|
|
376
|
-
},
|
|
377
|
-
},
|
|
378
|
-
],
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
382
|
-
colorAttachments: [
|
|
383
|
-
{
|
|
384
|
-
view: ctx.getCurrentTexture().createView(),
|
|
385
|
-
|
|
386
|
-
clearValue: [0.5, 0.5, 0.5, 1.0],
|
|
387
|
-
loadOp: "clear",
|
|
388
|
-
storeOp: "store",
|
|
389
|
-
},
|
|
390
|
-
],
|
|
391
|
-
depthStencilAttachment: {
|
|
392
|
-
view: depthTexture.createView(),
|
|
393
|
-
|
|
394
|
-
depthClearValue: 1.0,
|
|
395
|
-
depthLoadOp: "clear",
|
|
396
|
-
depthStoreOp: "store",
|
|
397
|
-
},
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
const aspect = ctx.canvas.width / ctx.canvas.height;
|
|
401
|
-
const projectionMatrix = mat4.perspective(
|
|
402
|
-
(2 * Math.PI) / 5,
|
|
403
|
-
aspect,
|
|
404
|
-
1,
|
|
405
|
-
100.0,
|
|
406
|
-
);
|
|
407
|
-
|
|
408
|
-
const modelMatrix1 = mat4.translation(vec3.create(-2, 0, 0));
|
|
409
|
-
const modelMatrix2 = mat4.translation(vec3.create(2, 0, 0));
|
|
410
|
-
const modelViewProjectionMatrix1 = mat4.create();
|
|
411
|
-
const modelViewProjectionMatrix2 = mat4.create();
|
|
412
|
-
const viewMatrix = mat4.translation(vec3.fromValues(0, 0, -7));
|
|
413
|
-
|
|
414
|
-
const tmpMat41 = mat4.create();
|
|
415
|
-
const tmpMat42 = mat4.create();
|
|
416
|
-
|
|
417
|
-
function updateTransformationMatrix() {
|
|
418
|
-
const now = 1721677648;
|
|
419
|
-
|
|
420
|
-
mat4.rotate(
|
|
421
|
-
modelMatrix1,
|
|
422
|
-
vec3.fromValues(Math.sin(now), Math.cos(now), 0),
|
|
423
|
-
1,
|
|
424
|
-
tmpMat41,
|
|
425
|
-
);
|
|
426
|
-
mat4.rotate(
|
|
427
|
-
modelMatrix2,
|
|
428
|
-
vec3.fromValues(Math.cos(now), Math.sin(now), 0),
|
|
429
|
-
1,
|
|
430
|
-
tmpMat42,
|
|
431
|
-
);
|
|
432
|
-
|
|
433
|
-
mat4.multiply(viewMatrix, tmpMat41, modelViewProjectionMatrix1);
|
|
434
|
-
mat4.multiply(
|
|
435
|
-
projectionMatrix,
|
|
436
|
-
modelViewProjectionMatrix1,
|
|
437
|
-
modelViewProjectionMatrix1,
|
|
438
|
-
);
|
|
439
|
-
mat4.multiply(viewMatrix, tmpMat42, modelViewProjectionMatrix2);
|
|
440
|
-
mat4.multiply(
|
|
441
|
-
projectionMatrix,
|
|
442
|
-
modelViewProjectionMatrix2,
|
|
443
|
-
modelViewProjectionMatrix2,
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
updateTransformationMatrix();
|
|
448
|
-
device.queue.writeBuffer(
|
|
449
|
-
uniformBuffer,
|
|
450
|
-
0,
|
|
451
|
-
modelViewProjectionMatrix1.buffer,
|
|
452
|
-
modelViewProjectionMatrix1.byteOffset,
|
|
453
|
-
modelViewProjectionMatrix1.byteLength,
|
|
454
|
-
);
|
|
455
|
-
device.queue.writeBuffer(
|
|
456
|
-
uniformBuffer,
|
|
457
|
-
offset,
|
|
458
|
-
modelViewProjectionMatrix2.buffer,
|
|
459
|
-
modelViewProjectionMatrix2.byteOffset,
|
|
460
|
-
modelViewProjectionMatrix2.byteLength,
|
|
461
|
-
);
|
|
462
|
-
|
|
463
|
-
const commandEncoder = device.createCommandEncoder();
|
|
464
|
-
const passEncoder =
|
|
465
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
466
|
-
passEncoder.setPipeline(pipeline);
|
|
467
|
-
passEncoder.setVertexBuffer(0, verticesBuffer);
|
|
468
|
-
|
|
469
|
-
// Bind the bind group (with the transformation matrix) for
|
|
470
|
-
// each cube, and draw.
|
|
471
|
-
passEncoder.setBindGroup(0, uniformBindGroup1);
|
|
472
|
-
passEncoder.draw(cubeVertexCount);
|
|
473
|
-
|
|
474
|
-
passEncoder.setBindGroup(0, uniformBindGroup2);
|
|
475
|
-
passEncoder.draw(cubeVertexCount);
|
|
476
|
-
|
|
477
|
-
passEncoder.end();
|
|
478
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
479
|
-
|
|
480
|
-
return canvas.getImageData();
|
|
481
|
-
},
|
|
482
|
-
{
|
|
483
|
-
basicVertWGSL: basicVert,
|
|
484
|
-
vertexPositionColorWGSL: vertexPositionColor,
|
|
485
|
-
},
|
|
486
|
-
);
|
|
487
|
-
const image = encodeImage(result);
|
|
488
|
-
checkImage(image, "snapshots/two-cube.png");
|
|
489
|
-
});
|
|
490
|
-
it("Textured cube", async () => {
|
|
491
|
-
const result = await client.eval(
|
|
492
|
-
({
|
|
493
|
-
gpu,
|
|
494
|
-
device,
|
|
495
|
-
assets: { cubeVertexArray, di3D: imageBitmap },
|
|
496
|
-
ctx,
|
|
497
|
-
mat4,
|
|
498
|
-
vec3,
|
|
499
|
-
basicVertWGSL,
|
|
500
|
-
sampleTextureMixColorWGSL,
|
|
501
|
-
canvas,
|
|
502
|
-
}) => {
|
|
503
|
-
const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
|
|
504
|
-
const cubePositionOffset = 0;
|
|
505
|
-
const cubeUVOffset = 4 * 8;
|
|
506
|
-
const cubeVertexCount = 36;
|
|
507
|
-
const presentationFormat = gpu.getPreferredCanvasFormat();
|
|
508
|
-
|
|
509
|
-
// Create a vertex buffer from the cube data.
|
|
510
|
-
const verticesBuffer = device.createBuffer({
|
|
511
|
-
size: cubeVertexArray.byteLength,
|
|
512
|
-
usage: GPUBufferUsage.VERTEX,
|
|
513
|
-
mappedAtCreation: true,
|
|
514
|
-
});
|
|
515
|
-
new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray);
|
|
516
|
-
verticesBuffer.unmap();
|
|
517
|
-
|
|
518
|
-
const pipeline = device.createRenderPipeline({
|
|
519
|
-
layout: "auto",
|
|
520
|
-
vertex: {
|
|
521
|
-
module: device.createShaderModule({
|
|
522
|
-
code: basicVertWGSL,
|
|
523
|
-
}),
|
|
524
|
-
buffers: [
|
|
525
|
-
{
|
|
526
|
-
arrayStride: cubeVertexSize,
|
|
527
|
-
attributes: [
|
|
528
|
-
{
|
|
529
|
-
// position
|
|
530
|
-
shaderLocation: 0,
|
|
531
|
-
offset: cubePositionOffset,
|
|
532
|
-
format: "float32x4",
|
|
533
|
-
},
|
|
534
|
-
{
|
|
535
|
-
// uv
|
|
536
|
-
shaderLocation: 1,
|
|
537
|
-
offset: cubeUVOffset,
|
|
538
|
-
format: "float32x2",
|
|
539
|
-
},
|
|
540
|
-
],
|
|
541
|
-
},
|
|
542
|
-
],
|
|
543
|
-
},
|
|
544
|
-
fragment: {
|
|
545
|
-
module: device.createShaderModule({
|
|
546
|
-
code: sampleTextureMixColorWGSL,
|
|
547
|
-
}),
|
|
548
|
-
targets: [
|
|
549
|
-
{
|
|
550
|
-
format: presentationFormat,
|
|
551
|
-
},
|
|
552
|
-
],
|
|
553
|
-
},
|
|
554
|
-
primitive: {
|
|
555
|
-
topology: "triangle-list",
|
|
556
|
-
|
|
557
|
-
// Backface culling since the cube is solid piece of geometry.
|
|
558
|
-
// Faces pointing away from the camera will be occluded by faces
|
|
559
|
-
// pointing toward the camera.
|
|
560
|
-
cullMode: "back",
|
|
561
|
-
},
|
|
562
|
-
|
|
563
|
-
// Enable depth testing so that the fragment closest to the camera
|
|
564
|
-
// is rendered in front.
|
|
565
|
-
depthStencil: {
|
|
566
|
-
depthWriteEnabled: true,
|
|
567
|
-
depthCompare: "less",
|
|
568
|
-
format: "depth24plus",
|
|
569
|
-
},
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
const depthTexture = device.createTexture({
|
|
573
|
-
size: [ctx.canvas.width, ctx.canvas.height],
|
|
574
|
-
format: "depth24plus",
|
|
575
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
576
|
-
});
|
|
577
|
-
|
|
578
|
-
const uniformBufferSize = 4 * 16; // 4x4 matrix
|
|
579
|
-
const uniformBuffer = device.createBuffer({
|
|
580
|
-
size: uniformBufferSize,
|
|
581
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
582
|
-
});
|
|
583
|
-
|
|
584
|
-
const cubeTexture = device.createTexture({
|
|
585
|
-
size: [imageBitmap.width, imageBitmap.height, 1],
|
|
586
|
-
format: "rgba8unorm",
|
|
587
|
-
usage:
|
|
588
|
-
GPUTextureUsage.TEXTURE_BINDING |
|
|
589
|
-
GPUTextureUsage.COPY_DST |
|
|
590
|
-
GPUTextureUsage.RENDER_ATTACHMENT,
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
device.queue.copyExternalImageToTexture(
|
|
594
|
-
{ source: imageBitmap },
|
|
595
|
-
{ texture: cubeTexture },
|
|
596
|
-
[imageBitmap.width, imageBitmap.height],
|
|
597
|
-
);
|
|
598
|
-
|
|
599
|
-
// Create a sampler with linear filtering for smooth interpolation.
|
|
600
|
-
const sampler = device.createSampler({
|
|
601
|
-
magFilter: "linear",
|
|
602
|
-
minFilter: "linear",
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
const uniformBindGroup = device.createBindGroup({
|
|
606
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
607
|
-
entries: [
|
|
608
|
-
{
|
|
609
|
-
binding: 0,
|
|
610
|
-
resource: {
|
|
611
|
-
buffer: uniformBuffer,
|
|
612
|
-
},
|
|
613
|
-
},
|
|
614
|
-
{
|
|
615
|
-
binding: 1,
|
|
616
|
-
resource: sampler,
|
|
617
|
-
},
|
|
618
|
-
{
|
|
619
|
-
binding: 2,
|
|
620
|
-
resource: cubeTexture.createView(),
|
|
621
|
-
},
|
|
622
|
-
],
|
|
623
|
-
});
|
|
624
|
-
|
|
625
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
626
|
-
colorAttachments: [
|
|
627
|
-
{
|
|
628
|
-
view: ctx.getCurrentTexture().createView(), // Assigned later
|
|
629
|
-
|
|
630
|
-
clearValue: [0.5, 0.5, 0.5, 1.0],
|
|
631
|
-
loadOp: "clear",
|
|
632
|
-
storeOp: "store",
|
|
633
|
-
},
|
|
634
|
-
],
|
|
635
|
-
depthStencilAttachment: {
|
|
636
|
-
view: depthTexture.createView(),
|
|
637
|
-
|
|
638
|
-
depthClearValue: 1.0,
|
|
639
|
-
depthLoadOp: "clear",
|
|
640
|
-
depthStoreOp: "store",
|
|
641
|
-
},
|
|
642
|
-
};
|
|
643
|
-
|
|
644
|
-
const aspect = ctx.canvas.width / ctx.canvas.height;
|
|
645
|
-
const projectionMatrix = mat4.perspective(
|
|
646
|
-
(2 * Math.PI) / 5,
|
|
647
|
-
aspect,
|
|
648
|
-
1,
|
|
649
|
-
100.0,
|
|
650
|
-
);
|
|
651
|
-
const modelViewProjectionMatrix = mat4.create();
|
|
652
|
-
|
|
653
|
-
function getTransformationMatrix() {
|
|
654
|
-
const viewMatrix = mat4.identity();
|
|
655
|
-
mat4.translate(viewMatrix, vec3.fromValues(0, 0, -4), viewMatrix);
|
|
656
|
-
const now = 1721677648;
|
|
657
|
-
mat4.rotate(
|
|
658
|
-
viewMatrix,
|
|
659
|
-
vec3.fromValues(Math.sin(now), Math.cos(now), 0),
|
|
660
|
-
1,
|
|
661
|
-
viewMatrix,
|
|
662
|
-
);
|
|
663
|
-
|
|
664
|
-
mat4.multiply(
|
|
665
|
-
projectionMatrix,
|
|
666
|
-
viewMatrix,
|
|
667
|
-
modelViewProjectionMatrix,
|
|
668
|
-
);
|
|
669
|
-
|
|
670
|
-
return modelViewProjectionMatrix;
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
const transformationMatrix = getTransformationMatrix();
|
|
674
|
-
device.queue.writeBuffer(
|
|
675
|
-
uniformBuffer,
|
|
676
|
-
0,
|
|
677
|
-
transformationMatrix.buffer,
|
|
678
|
-
transformationMatrix.byteOffset,
|
|
679
|
-
transformationMatrix.byteLength,
|
|
680
|
-
);
|
|
681
|
-
|
|
682
|
-
const commandEncoder = device.createCommandEncoder();
|
|
683
|
-
const passEncoder =
|
|
684
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
685
|
-
passEncoder.setPipeline(pipeline);
|
|
686
|
-
passEncoder.setBindGroup(0, uniformBindGroup);
|
|
687
|
-
passEncoder.setVertexBuffer(0, verticesBuffer);
|
|
688
|
-
passEncoder.draw(cubeVertexCount);
|
|
689
|
-
passEncoder.end();
|
|
690
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
691
|
-
|
|
692
|
-
return canvas.getImageData();
|
|
693
|
-
},
|
|
694
|
-
{
|
|
695
|
-
basicVertWGSL: basicVert,
|
|
696
|
-
vertexPositionColorWGSL: vertexPositionColor,
|
|
697
|
-
sampleTextureMixColorWGSL: sampleTextureMixColor,
|
|
698
|
-
},
|
|
699
|
-
);
|
|
700
|
-
const image = encodeImage(result);
|
|
701
|
-
checkImage(image, "snapshots/textured-cube.png");
|
|
702
|
-
});
|
|
703
|
-
it("Instanced cubes", async () => {
|
|
704
|
-
const result = await client.eval(
|
|
705
|
-
({
|
|
706
|
-
gpu,
|
|
707
|
-
device,
|
|
708
|
-
assets: { cubeVertexArray },
|
|
709
|
-
ctx,
|
|
710
|
-
mat4,
|
|
711
|
-
vec3,
|
|
712
|
-
vertexPositionColorWGSL,
|
|
713
|
-
instancedVertWGSL,
|
|
714
|
-
canvas,
|
|
715
|
-
}) => {
|
|
716
|
-
const cubeVertexSize = 4 * 10; // Byte size of one cube vertex.
|
|
717
|
-
const cubePositionOffset = 0;
|
|
718
|
-
const cubeUVOffset = 4 * 8;
|
|
719
|
-
const cubeVertexCount = 36;
|
|
720
|
-
const presentationFormat = gpu.getPreferredCanvasFormat();
|
|
721
|
-
// Create a vertex buffer from the cube data.
|
|
722
|
-
const verticesBuffer = device.createBuffer({
|
|
723
|
-
size: cubeVertexArray.byteLength,
|
|
724
|
-
usage: GPUBufferUsage.VERTEX,
|
|
725
|
-
mappedAtCreation: true,
|
|
726
|
-
});
|
|
727
|
-
new Float32Array(verticesBuffer.getMappedRange()).set(cubeVertexArray);
|
|
728
|
-
verticesBuffer.unmap();
|
|
729
|
-
|
|
730
|
-
const pipeline = device.createRenderPipeline({
|
|
731
|
-
layout: "auto",
|
|
732
|
-
vertex: {
|
|
733
|
-
module: device.createShaderModule({
|
|
734
|
-
code: instancedVertWGSL,
|
|
735
|
-
}),
|
|
736
|
-
buffers: [
|
|
737
|
-
{
|
|
738
|
-
arrayStride: cubeVertexSize,
|
|
739
|
-
attributes: [
|
|
740
|
-
{
|
|
741
|
-
// position
|
|
742
|
-
shaderLocation: 0,
|
|
743
|
-
offset: cubePositionOffset,
|
|
744
|
-
format: "float32x4",
|
|
745
|
-
},
|
|
746
|
-
{
|
|
747
|
-
// uv
|
|
748
|
-
shaderLocation: 1,
|
|
749
|
-
offset: cubeUVOffset,
|
|
750
|
-
format: "float32x2",
|
|
751
|
-
},
|
|
752
|
-
],
|
|
753
|
-
},
|
|
754
|
-
],
|
|
755
|
-
},
|
|
756
|
-
fragment: {
|
|
757
|
-
module: device.createShaderModule({
|
|
758
|
-
code: vertexPositionColorWGSL,
|
|
759
|
-
}),
|
|
760
|
-
targets: [
|
|
761
|
-
{
|
|
762
|
-
format: presentationFormat,
|
|
763
|
-
},
|
|
764
|
-
],
|
|
765
|
-
},
|
|
766
|
-
primitive: {
|
|
767
|
-
topology: "triangle-list",
|
|
768
|
-
|
|
769
|
-
// Backface culling since the cube is solid piece of geometry.
|
|
770
|
-
// Faces pointing away from the camera will be occluded by faces
|
|
771
|
-
// pointing toward the camera.
|
|
772
|
-
cullMode: "back",
|
|
773
|
-
},
|
|
774
|
-
|
|
775
|
-
// Enable depth testing so that the fragment closest to the camera
|
|
776
|
-
// is rendered in front.
|
|
777
|
-
depthStencil: {
|
|
778
|
-
depthWriteEnabled: true,
|
|
779
|
-
depthCompare: "less",
|
|
780
|
-
format: "depth24plus",
|
|
781
|
-
},
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
const depthTexture = device.createTexture({
|
|
785
|
-
size: [ctx.canvas.width, ctx.canvas.height],
|
|
786
|
-
format: "depth24plus",
|
|
787
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
788
|
-
});
|
|
789
|
-
|
|
790
|
-
const xCount = 4;
|
|
791
|
-
const yCount = 4;
|
|
792
|
-
const numInstances = xCount * yCount;
|
|
793
|
-
const matrixFloatCount = 16; // 4x4 matrix
|
|
794
|
-
const matrixSize = 4 * matrixFloatCount;
|
|
795
|
-
const uniformBufferSize = numInstances * matrixSize;
|
|
796
|
-
|
|
797
|
-
// Allocate a buffer large enough to hold transforms for every
|
|
798
|
-
// instance.
|
|
799
|
-
const uniformBuffer = device.createBuffer({
|
|
800
|
-
size: uniformBufferSize,
|
|
801
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
802
|
-
});
|
|
803
|
-
|
|
804
|
-
const uniformBindGroup = device.createBindGroup({
|
|
805
|
-
layout: pipeline.getBindGroupLayout(0),
|
|
806
|
-
entries: [
|
|
807
|
-
{
|
|
808
|
-
binding: 0,
|
|
809
|
-
resource: {
|
|
810
|
-
buffer: uniformBuffer,
|
|
811
|
-
},
|
|
812
|
-
},
|
|
813
|
-
],
|
|
814
|
-
});
|
|
815
|
-
|
|
816
|
-
const aspect = ctx.canvas.width / ctx.canvas.height;
|
|
817
|
-
const projectionMatrix = mat4.perspective(
|
|
818
|
-
(2 * Math.PI) / 5,
|
|
819
|
-
aspect,
|
|
820
|
-
1,
|
|
821
|
-
100.0,
|
|
822
|
-
);
|
|
823
|
-
|
|
824
|
-
const modelMatrices = new Array<Mat4>(numInstances);
|
|
825
|
-
const mvpMatricesData = new Float32Array(
|
|
826
|
-
matrixFloatCount * numInstances,
|
|
827
|
-
);
|
|
828
|
-
|
|
829
|
-
const step = 4.0;
|
|
830
|
-
|
|
831
|
-
// Initialize the matrix data for every instance.
|
|
832
|
-
let m = 0;
|
|
833
|
-
for (let x = 0; x < xCount; x++) {
|
|
834
|
-
for (let y = 0; y < yCount; y++) {
|
|
835
|
-
modelMatrices[m] = mat4.translation(
|
|
836
|
-
vec3.fromValues(
|
|
837
|
-
step * (x - xCount / 2 + 0.5),
|
|
838
|
-
step * (y - yCount / 2 + 0.5),
|
|
839
|
-
0,
|
|
840
|
-
),
|
|
841
|
-
);
|
|
842
|
-
m++;
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
const viewMatrix = mat4.translation(vec3.fromValues(0, 0, -12));
|
|
847
|
-
|
|
848
|
-
const tmpMat4 = mat4.create();
|
|
849
|
-
|
|
850
|
-
// Update the transformation matrix data for each instance.
|
|
851
|
-
function updateTransformationMatrix() {
|
|
852
|
-
const now = 1721677648;
|
|
853
|
-
|
|
854
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
855
|
-
let m = 0,
|
|
856
|
-
i = 0;
|
|
857
|
-
for (let x = 0; x < xCount; x++) {
|
|
858
|
-
for (let y = 0; y < yCount; y++) {
|
|
859
|
-
mat4.rotate(
|
|
860
|
-
modelMatrices[i],
|
|
861
|
-
vec3.fromValues(
|
|
862
|
-
Math.sin((x + 0.5) * now),
|
|
863
|
-
Math.cos((y + 0.5) * now),
|
|
864
|
-
0,
|
|
865
|
-
),
|
|
866
|
-
1,
|
|
867
|
-
tmpMat4,
|
|
868
|
-
);
|
|
869
|
-
|
|
870
|
-
mat4.multiply(viewMatrix, tmpMat4, tmpMat4);
|
|
871
|
-
mat4.multiply(projectionMatrix, tmpMat4, tmpMat4);
|
|
872
|
-
|
|
873
|
-
mvpMatricesData.set(tmpMat4, m);
|
|
874
|
-
|
|
875
|
-
i++;
|
|
876
|
-
m += matrixFloatCount;
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
const renderPassDescriptor: GPURenderPassDescriptor = {
|
|
882
|
-
colorAttachments: [
|
|
883
|
-
{
|
|
884
|
-
view: ctx.getCurrentTexture().createView(), // Assigned later
|
|
885
|
-
|
|
886
|
-
clearValue: [0.5, 0.5, 0.5, 1.0],
|
|
887
|
-
loadOp: "clear",
|
|
888
|
-
storeOp: "store",
|
|
889
|
-
},
|
|
890
|
-
],
|
|
891
|
-
depthStencilAttachment: {
|
|
892
|
-
view: depthTexture.createView(),
|
|
893
|
-
|
|
894
|
-
depthClearValue: 1.0,
|
|
895
|
-
depthLoadOp: "clear",
|
|
896
|
-
depthStoreOp: "store",
|
|
897
|
-
},
|
|
898
|
-
};
|
|
899
|
-
|
|
900
|
-
// Update the matrix data.
|
|
901
|
-
updateTransformationMatrix();
|
|
902
|
-
device.queue.writeBuffer(
|
|
903
|
-
uniformBuffer,
|
|
904
|
-
0,
|
|
905
|
-
mvpMatricesData.buffer,
|
|
906
|
-
mvpMatricesData.byteOffset,
|
|
907
|
-
mvpMatricesData.byteLength,
|
|
908
|
-
);
|
|
909
|
-
|
|
910
|
-
const commandEncoder = device.createCommandEncoder();
|
|
911
|
-
const passEncoder =
|
|
912
|
-
commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
913
|
-
passEncoder.setPipeline(pipeline);
|
|
914
|
-
passEncoder.setBindGroup(0, uniformBindGroup);
|
|
915
|
-
passEncoder.setVertexBuffer(0, verticesBuffer);
|
|
916
|
-
passEncoder.draw(cubeVertexCount, numInstances, 0, 0);
|
|
917
|
-
passEncoder.end();
|
|
918
|
-
device.queue.submit([commandEncoder.finish()]);
|
|
919
|
-
return canvas.getImageData();
|
|
920
|
-
},
|
|
921
|
-
{
|
|
922
|
-
vertexPositionColorWGSL: vertexPositionColor,
|
|
923
|
-
instancedVertWGSL: instancedVert,
|
|
924
|
-
},
|
|
925
|
-
);
|
|
926
|
-
const image = encodeImage(result);
|
|
927
|
-
checkImage(image, "snapshots/instanced-cubes.png");
|
|
928
|
-
});
|
|
929
|
-
});
|