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