From ed6b68edf60ee6b585e0d537d77c61558f3b2dd2 Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Tue, 12 Nov 2024 14:44:44 +0200 Subject: [PATCH 1/4] Update `useClient.ts` reference (#181) Signed-off-by: Emmanuel Ferdman --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9584a48bd..30f5e47c4 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ yarn test:ref To run the e2e test, open the example app on the e2e screen. By default, it will try to connect to a localhost test server. -If you want to run the test suite on a physical device, you can modify the address [here](/package/example/src/useClient.ts#L4). +If you want to run the test suite on a physical device, you can modify the address [here](/apps/example/src/useClient.ts#L4). ``` yarn test From 8d77a4015bb5433fcec0de51b36fc790ac444d7d Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Tue, 12 Nov 2024 14:20:03 +0100 Subject: [PATCH 2/4] chore: Update to TypeGPU 0.2 (#178) --------- Co-authored-by: William Candillon --- apps/example/package.json | 2 +- .../example/src/ComputeBoids/ComputeBoids.tsx | 129 ++++++++---------- apps/example/src/ComputeBoids/Shaders.ts | 9 +- .../src/GradientTiles/GradientTiles.tsx | 74 +++++----- yarn.lock | 10 +- 5 files changed, 108 insertions(+), 116 deletions(-) diff --git a/apps/example/package.json b/apps/example/package.json index c5a33bfdc..cf8310f01 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -28,7 +28,7 @@ "react-native-wgpu": "*", "teapot": "^1.0.0", "three": "0.168.0", - "typegpu": "^0.1.2", + "typegpu": "^0.2.0", "wgpu-matrix": "^3.0.2" }, "devDependencies": { diff --git a/apps/example/src/ComputeBoids/ComputeBoids.tsx b/apps/example/src/ComputeBoids/ComputeBoids.tsx index d93ef3d2c..75a1b6d95 100644 --- a/apps/example/src/ComputeBoids/ComputeBoids.tsx +++ b/apps/example/src/ComputeBoids/ComputeBoids.tsx @@ -18,6 +18,33 @@ type BoidsOptions = { cohesionStrength: number; }; +const Parameters = struct({ + separationDistance: f32, + separationStrength: f32, + alignmentDistance: f32, + alignmentStrength: f32, + cohesionDistance: f32, + cohesionStrength: f32, +}); + +const TriangleData = struct({ + position: vec2f, + velocity: vec2f, +}); + +const TriangleDataArray = (n: number) => arrayOf(TriangleData, n); + +const renderBindGroupLayout = tgpu.bindGroupLayout({ + trianglePos: { storage: TriangleDataArray }, + colorPalette: { uniform: vec3f }, +}); + +const computeBindGroupLayout = tgpu.bindGroupLayout({ + currentTrianglePos: { storage: TriangleDataArray }, + nextTrianglePos: { storage: TriangleDataArray, access: 'mutable' }, + params: { uniform: Parameters }, +}); + const colorPresets = { plumTree: vec3f(1.0, 2.0, 1.0), jeans: vec3f(2.0, 1.5, 1.0), @@ -69,28 +96,20 @@ export function ComputeBoids() { ); const ref = useWebGPU(({ context, device, presentationFormat }) => { + const root = tgpu.initFromDevice({ device }); + context.configure({ device, format: presentationFormat, alphaMode: "premultiplied", }); - const params = struct({ - separationDistance: f32, - separationStrength: f32, - alignmentDistance: f32, - alignmentStrength: f32, - cohesionDistance: f32, - cohesionStrength: f32, - }); - - const paramsBuffer = tgpu - .createBuffer(params, presets.default) - .$device(device) - .$usage(tgpu.Storage); + const paramsBuffer = root + .createBuffer(Parameters, presets.default) + .$usage("uniform"); const triangleSize = 0.03; - const triangleVertexBuffer = tgpu + const triangleVertexBuffer = root .createBuffer(arrayOf(f32, 6), [ 0.0, triangleSize, @@ -99,19 +118,11 @@ export function ComputeBoids() { triangleSize / 2, -triangleSize / 2, ]) - .$device(device) - .$usage(tgpu.Vertex); + .$usage("vertex"); const triangleAmount = 1000; - const triangleInfoStruct = struct({ - position: vec2f, - velocity: vec2f, - }); const trianglePosBuffers = Array.from({ length: 2 }, () => - tgpu - .createBuffer(arrayOf(triangleInfoStruct, triangleAmount)) - .$device(device) - .$usage(tgpu.Storage, tgpu.Uniform), + root.createBuffer(TriangleDataArray(triangleAmount)).$usage("storage") ); randomizePositions.current = () => { @@ -119,22 +130,21 @@ export function ComputeBoids() { position: vec2f(Math.random() * 2 - 1, Math.random() * 2 - 1), velocity: vec2f(Math.random() * 0.1 - 0.05, Math.random() * 0.1 - 0.05), })); - tgpu.write(trianglePosBuffers[0], positions); - tgpu.write(trianglePosBuffers[1], positions); + trianglePosBuffers[0].write(positions); + trianglePosBuffers[1].write(positions); }; randomizePositions.current(); - const colorPaletteBuffer = tgpu + const colorPaletteBuffer = root .createBuffer(vec3f, colorPresets.plumTree) - .$device(device) - .$usage(tgpu.Uniform); + .$usage("uniform"); updateColorPreset.current = (newColorPreset: ColorPresets) => { - tgpu.write(colorPaletteBuffer, colorPresets[newColorPreset]); + colorPaletteBuffer.write(colorPresets[newColorPreset]); }; updateParams.current = (newOptions: BoidsOptions) => { - tgpu.write(paramsBuffer, newOptions); + paramsBuffer.write(newOptions); }; const renderModule = device.createShaderModule({ @@ -146,7 +156,9 @@ export function ComputeBoids() { }); const pipeline = device.createRenderPipeline({ - layout: "auto", + layout: device.createPipelineLayout({ + bindGroupLayouts: [root.unwrap(renderBindGroupLayout)], + }), vertex: { module: renderModule, buffers: [ @@ -176,55 +188,26 @@ export function ComputeBoids() { }); const computePipeline = device.createComputePipeline({ - layout: "auto", + layout: device.createPipelineLayout({ + bindGroupLayouts: [root.unwrap(computeBindGroupLayout)], + }), compute: { module: computeModule, }, }); const renderBindGroups = [0, 1].map((idx) => - device.createBindGroup({ - layout: pipeline.getBindGroupLayout(0), - entries: [ - { - binding: 0, - resource: { - buffer: trianglePosBuffers[idx].buffer, - }, - }, - { - binding: 1, - resource: { - buffer: colorPaletteBuffer.buffer, - }, - }, - ], + renderBindGroupLayout.populate({ + trianglePos: trianglePosBuffers[idx], + colorPalette: colorPaletteBuffer, }), ); const computeBindGroups = [0, 1].map((idx) => - device.createBindGroup({ - layout: computePipeline.getBindGroupLayout(0), - entries: [ - { - binding: 0, - resource: { - buffer: trianglePosBuffers[idx].buffer, - }, - }, - { - binding: 1, - resource: { - buffer: trianglePosBuffers[1 - idx].buffer, - }, - }, - { - binding: 2, - resource: { - buffer: paramsBuffer.buffer, - }, - }, - ], + computeBindGroupLayout.populate({ + currentTrianglePos: trianglePosBuffers[idx], + nextTrianglePos: trianglePosBuffers[1 - idx], + params: paramsBuffer, }), ); @@ -251,7 +234,7 @@ export function ComputeBoids() { computePass.setPipeline(computePipeline); computePass.setBindGroup( 0, - even ? computeBindGroups[0] : computeBindGroups[1], + root.unwrap(even ? computeBindGroups[0] : computeBindGroups[1]) ); computePass.dispatchWorkgroups(triangleAmount); computePass.end(); @@ -261,7 +244,7 @@ export function ComputeBoids() { passEncoder.setVertexBuffer(0, triangleVertexBuffer.buffer); passEncoder.setBindGroup( 0, - even ? renderBindGroups[1] : renderBindGroups[0], + root.unwrap(even ? renderBindGroups[1] : renderBindGroups[0]) ); passEncoder.draw(3, triangleAmount); passEncoder.end(); diff --git a/apps/example/src/ComputeBoids/Shaders.ts b/apps/example/src/ComputeBoids/Shaders.ts index cdf01eb64..12d4882c6 100644 --- a/apps/example/src/ComputeBoids/Shaders.ts +++ b/apps/example/src/ComputeBoids/Shaders.ts @@ -1,4 +1,3 @@ -const triangleAmount = 1000; const triangleSize = 0.03; export const renderCode = /* wgsl */ ` @@ -24,7 +23,7 @@ export const renderCode = /* wgsl */ ` @location(1) color : vec4f, }; - @binding(0) @group(0) var trianglePos : array; + @binding(0) @group(0) var trianglePos : array; @binding(1) @group(0) var colorPalette : vec3f; @vertex @@ -67,9 +66,9 @@ export const computeCode = /* wgsl */ ` cohesion_strength : f32, }; - @binding(0) @group(0) var currentTrianglePos : array; + @binding(0) @group(0) var currentTrianglePos : array; @binding(1) @group(0) var nextTrianglePos : array; - @binding(2) @group(0) var params : Parameters; + @binding(2) @group(0) var params : Parameters; @compute @workgroup_size(1) fn mainCompute(@builtin(global_invocation_id) gid: vec3u) { @@ -80,7 +79,7 @@ export const computeCode = /* wgsl */ ` var alignmentCount = 0u; var cohesion = vec2(0.0, 0.0); var cohesionCount = 0u; - for (var i = 0u; i < ${triangleAmount}; i = i + 1) { + for (var i = 0u; i < arrayLength(¤tTrianglePos); i = i + 1) { if (i == index) { continue; } diff --git a/apps/example/src/GradientTiles/GradientTiles.tsx b/apps/example/src/GradientTiles/GradientTiles.tsx index 7ef11589c..701cd3159 100644 --- a/apps/example/src/GradientTiles/GradientTiles.tsx +++ b/apps/example/src/GradientTiles/GradientTiles.tsx @@ -1,16 +1,33 @@ -import { useEffect, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { Button, PixelRatio, StyleSheet, Text, View } from "react-native"; import { Canvas, useDevice, useGPUContext } from "react-native-wgpu"; import { struct, u32 } from "typegpu/data"; -import tgpu from "typegpu"; +import tgpu, { type TgpuBindGroup, type TgpuBuffer } from "typegpu"; -import { vertWGSL, fragWGSL } from "./gradientWgsl"; +import { vertWGSL, fragWGSL } from './gradientWgsl'; + +const Span = struct({ + x: u32, + y: u32, +}); + +const bindGroupLayout = tgpu.bindGroupLayout({ + span: { uniform: Span }, +}); interface RenderingState { pipeline: GPURenderPipeline; - // eslint-disable-next-line @typescript-eslint/no-explicit-any - spanBuffer: any; - bindGroup: GPUBindGroup; + spanBuffer: TgpuBuffer; + bindGroup: TgpuBindGroup<(typeof bindGroupLayout)['entries']>; +} + +function useRoot() { + const { device } = useDevice(); + + return useMemo( + () => (device ? tgpu.initFromDevice({ device }) : null), + [device] + ); } export function GradientTiles() { @@ -18,12 +35,15 @@ export function GradientTiles() { const [state, setState] = useState(null); const [spanX, setSpanX] = useState(4); const [spanY, setSpanY] = useState(4); - const { device } = useDevice(); + const root = useRoot(); + const { device = null } = root ?? {}; const { ref, context } = useGPUContext(); + useEffect(() => { - if (!device || !context || state !== null) { + if (!device || !root || !context || state !== null) { return; } + const canvas = context.canvas as HTMLCanvasElement; canvas.width = canvas.clientWidth * PixelRatio.get(); canvas.height = canvas.clientHeight * PixelRatio.get(); @@ -32,18 +52,14 @@ export function GradientTiles() { format: presentationFormat, }); - const Span = struct({ - x: u32, - y: u32, - }); - - const spanBuffer = tgpu + const spanBuffer = root .createBuffer(Span, { x: 10, y: 10 }) - .$device(device) - .$usage(tgpu.Uniform); + .$usage("uniform"); const pipeline = device.createRenderPipeline({ - layout: "auto", + layout: device.createPipelineLayout({ + bindGroupLayouts: [root.unwrap(bindGroupLayout)], + }), vertex: { module: device.createShaderModule({ code: vertWGSL, @@ -64,24 +80,18 @@ export function GradientTiles() { }, }); - const bindGroup = device.createBindGroup({ - layout: pipeline.getBindGroupLayout(0), - entries: [ - { - binding: 0, - resource: { - buffer: spanBuffer.buffer, - }, - }, - ], + const bindGroup = bindGroupLayout.populate({ + span: spanBuffer, }); + setState({ bindGroup, pipeline, spanBuffer }); - }, [context, device, presentationFormat, state]); + }, [context, device, root, presentationFormat, state]); useEffect(() => { - if (!context || !device || !state) { + if (!context || !device || !root || !state) { return; } + const { bindGroup, pipeline, spanBuffer } = state; const textureView = context.getCurrentTexture().createView(); const renderPassDescriptor: GPURenderPassDescriptor = { @@ -95,18 +105,18 @@ export function GradientTiles() { ], }; - tgpu.write(spanBuffer, { x: spanX, y: spanY }); + spanBuffer.write({ x: spanX, y: spanY }); const commandEncoder = device.createCommandEncoder(); const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); passEncoder.setPipeline(pipeline); - passEncoder.setBindGroup(0, bindGroup); + passEncoder.setBindGroup(0, root.unwrap(bindGroup)); passEncoder.draw(4); passEncoder.end(); device.queue.submit([commandEncoder.finish()]); context.present(); - }, [context, device, spanX, spanY, state]); + }, [context, device, root, spanX, spanY, state]); return ( diff --git a/yarn.lock b/yarn.lock index 5cbcab31e..897aaf550 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4301,7 +4301,7 @@ __metadata: react-test-renderer: 18.2.0 teapot: ^1.0.0 three: 0.168.0 - typegpu: ^0.1.2 + typegpu: ^0.2.0 typescript: 5.0.4 wgpu-matrix: ^3.0.2 languageName: unknown @@ -12299,12 +12299,12 @@ __metadata: languageName: node linkType: hard -"typegpu@npm:^0.1.2": - version: 0.1.2 - resolution: "typegpu@npm:0.1.2" +"typegpu@npm:^0.2.0": + version: 0.2.0 + resolution: "typegpu@npm:0.2.0" dependencies: typed-binary: ^4.0.1 - checksum: e8bb501305ad7e280fd396b178afd5a61e470358881fda3be37014ac5f2238402a63056f1bbb9bd3c191923d259b3dd799821877ca3c2adc5a5c08dac49836c6 + checksum: 219f5a06ac7313ae36ed10fbcbf712d56504300b9b53fc2485bbe4113e82ce7ce6c317ebdc23e205bcdc7f22718078da57ff100cbc6e1bb88cd63ce47e6c60ce languageName: node linkType: hard From 4a289a686949427556ea7c8893ecf494e8cd6825 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Tue, 12 Nov 2024 14:20:31 +0100 Subject: [PATCH 3/4] Refactor build scripts (#177) --- .gitignore | 1 + packages/webgpu/.gitignore | 2 +- packages/webgpu/cpp/rnwgpu/api/GPU.h | 2 +- .../cpp/rnwgpu/api/GPUCanvasContext.cpp | 4 + .../webgpu/cpp/rnwgpu/api/GPUCanvasContext.h | 16 ---- packages/webgpu/package.json | 1 + .../webgpu/scripts/build/copy-artifacts.ts | 3 +- .../scripts/build/dawn-configuration.ts | 92 +++++++++++++++++++ packages/webgpu/scripts/build/dawn.ts | 25 +---- packages/webgpu/scripts/build/util.ts | 72 --------------- 10 files changed, 107 insertions(+), 111 deletions(-) create mode 100644 packages/webgpu/scripts/build/dawn-configuration.ts diff --git a/.gitignore b/.gitignore index a278eb27b..60de60316 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +!packages/webgpu/scripts/build # Yarn .package/.yarn/* .yarn/* diff --git a/packages/webgpu/.gitignore b/packages/webgpu/.gitignore index d89f44f75..1cc9e5603 100644 --- a/packages/webgpu/.gitignore +++ b/packages/webgpu/.gitignore @@ -1,4 +1,4 @@ -!package/scripts/build +!scripts/build # OSX # .DS_Store diff --git a/packages/webgpu/cpp/rnwgpu/api/GPU.h b/packages/webgpu/cpp/rnwgpu/api/GPU.h index fe15b7c31..9b2fc8b17 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPU.h +++ b/packages/webgpu/cpp/rnwgpu/api/GPU.h @@ -58,4 +58,4 @@ class GPU : public m::HybridObject { std::shared_ptr _async; }; -} // namespace rnwgpu \ No newline at end of file +} // namespace rnwgpu diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp b/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp index 00b7ff257..6fa2afccb 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp +++ b/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.cpp @@ -2,6 +2,10 @@ #include "Convertors.h" #include "RNWebGPUManager.h" +#ifdef __APPLE__ +#include "dawn/native/MetalBackend.h" +#endif + namespace rnwgpu { void GPUCanvasContext::configure( diff --git a/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h b/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h index ab321b313..4e8f0b447 100644 --- a/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h +++ b/packages/webgpu/cpp/rnwgpu/api/GPUCanvasContext.h @@ -17,22 +17,6 @@ #include "GPUTexture.h" #include "SurfaceRegistry.h" -#ifdef __APPLE__ - -namespace dawn { -namespace native { -namespace metal { - -// See -// https://source.chromium.org/chromium/chromium/src/+/main:third_party/dawn/include/dawn/native/MetalBackend.h;l=41 -void WaitForCommandsToBeScheduled(WGPUDevice device); - -} // namespace metal -} // namespace native -} // namespace dawn - -#endif - namespace rnwgpu { namespace m = margelo; diff --git a/packages/webgpu/package.json b/packages/webgpu/package.json index b13e6be84..21b9d29a3 100644 --- a/packages/webgpu/package.json +++ b/packages/webgpu/package.json @@ -26,6 +26,7 @@ "tsc": "tsc --noEmit", "build": "bob build", "build-dawn": "ts-node scripts/build/dawn.ts", + "copy-headers": "ts-node scripts/build/copy-headers.ts", "clean-dawn": "rimraf ./libs && rimraf ../../externals/dawn/out", "clang-format": "yarn clang-format-ios && yarn clang-format-android && yarn clang-format-common", "clang-format-ios": "find apple/ -iname \"*.h\" -o -iname \"*.mm\" -o -iname \"*.cpp\" | xargs clang-format -i", diff --git a/packages/webgpu/scripts/build/copy-artifacts.ts b/packages/webgpu/scripts/build/copy-artifacts.ts index 249931dc0..f9a154545 100644 --- a/packages/webgpu/scripts/build/copy-artifacts.ts +++ b/packages/webgpu/scripts/build/copy-artifacts.ts @@ -1,4 +1,5 @@ -import { $, checkBuildArtifacts } from "./util"; +import { checkBuildArtifacts } from "./dawn-configuration"; +import { $ } from "./util"; $("cp -R ../../artifacts/libs ."); $("cp -R ../../artifacts/cpp/webgpu cpp"); diff --git a/packages/webgpu/scripts/build/dawn-configuration.ts b/packages/webgpu/scripts/build/dawn-configuration.ts new file mode 100644 index 000000000..891c9de7e --- /dev/null +++ b/packages/webgpu/scripts/build/dawn-configuration.ts @@ -0,0 +1,92 @@ +/* eslint-disable max-len */ +import { $, checkFileExists, runAsync } from "./util"; + +export const libs = ["libwebgpu_dawn"] as const; + +export const projectRoot = "packages/webgpu"; + +export const platforms = [ + "arm64", + "x86_64", + "x86", + "armeabi-v7a", + "arm64-v8a", + "universal", +] as const; + +export type OS = "apple" | "android"; +export type Platform = (typeof platforms)[number]; + +export const copyHeaders = () => { + console.log("📗 Copy headers"); + [ + `rm -rf ${projectRoot}/cpp/webgpu`, + `rm -rf ${projectRoot}/cpp/dawn`, + `cp -a externals/dawn/out/android_arm64-v8a/gen/include/webgpu ${projectRoot}/cpp`, + `cp -a externals/dawn/out/android_arm64-v8a/gen/include/dawn ${projectRoot}/cpp`, + `cp -a externals/dawn/include/webgpu/. ${projectRoot}/cpp/webgpu`, + `cp -a externals/dawn/include/dawn/. ${projectRoot}/cpp/dawn`, + `sed -i '' 's/#include "dawn\\/webgpu.h"/#include "webgpu\\/webgpu.h"/' ${projectRoot}/cpp/dawn/dawn_proc_table.h`, + `cp ${projectRoot}/cpp/dawn/webgpu.h ${projectRoot}/cpp/webgpu/webgpu.h`, + `cp ${projectRoot}/cpp/dawn/webgpu_cpp.h ${projectRoot}/cpp/webgpu/webgpu_cpp.h`, + `rm -rf ${projectRoot}/cpp/dawn/webgpu.h`, + `rm -rf ${projectRoot}/cpp/dawn/webgpu_cpp.h`, + `rm -rf ${projectRoot}/cpp/dawn/wire`, + `cp externals/dawn/src/dawn/dawn.json ${projectRoot}/libs`, + ].map((cmd) => $(cmd)); +}; + +const serializeCMakeArgs = (args: Record) => { + return Object.keys(args) + .map((key) => `-D${key}=${args[key]}`) + .join(" "); +}; + +export const build = async ( + label: string, + args: Record, + debugLabel: string, +) => { + console.log(`🔨 Building ${label}`); + $(`mkdir -p externals/dawn/out/${label}`); + process.chdir(`externals/dawn/out/${label}`); + const cmd = `cmake ../.. -G Ninja ${serializeCMakeArgs(args)}`; + await runAsync(cmd, debugLabel); + await runAsync("ninja", debugLabel); + process.chdir("../../../.."); +}; + +export const copyLib = (os: OS, platform: Platform, sdk?: string) => { + const suffix = `${platform}${sdk ? `_${sdk}` : ""}`; + const out = `${os}_${suffix}`; + const dstPath = `${projectRoot}/libs/${os}/${suffix}/`; + $(`mkdir -p ${dstPath}`); + if (os === "android") { + console.log("Strip debug symbols from libwebgpu_dawn.a..."); + $( + `$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-strip externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.so`, + ); + } + [ + `externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.${os === "apple" ? "a" : "so"}`, + ].forEach((lib) => { + const libPath = lib; + console.log(`Copying ${libPath} to ${dstPath}`); + $(`cp ${libPath} ${dstPath}`); + }); +}; + +export const checkBuildArtifacts = () => { + console.log("Check build artifacts..."); + platforms + .filter((arch) => arch !== "arm64" && arch !== "universal") + .forEach((platform) => { + libs.forEach((lib) => { + checkFileExists(`libs/android/${platform}/${lib}.so`); + }); + }); + libs.forEach((lib) => { + checkFileExists(`libs/apple/${lib}.xcframework`); + }); + checkFileExists("libs/dawn.json"); +}; diff --git a/packages/webgpu/scripts/build/dawn.ts b/packages/webgpu/scripts/build/dawn.ts index 2ecc14c61..7b1813a59 100644 --- a/packages/webgpu/scripts/build/dawn.ts +++ b/packages/webgpu/scripts/build/dawn.ts @@ -6,16 +6,16 @@ import { chdir } from "process"; import yargs from "yargs"; import { hideBin } from "yargs/helpers"; -import type { Platform } from "./util"; +import type { Platform } from "./dawn-configuration"; +import { $, mapKeys } from "./util"; import { - $, build, checkBuildArtifacts, + copyHeaders, copyLib, libs, - mapKeys, projectRoot, -} from "./util"; +} from "./dawn-configuration"; const { argv } = yargs(hideBin(process.argv)) .option("exclude", { @@ -164,22 +164,7 @@ const apple = { ); }); - console.log("Copy headers"); - $(`rm -rf ${projectRoot}/cpp/webgpu`); - $(`rm -rf ${projectRoot}/cpp/dawn`); - $( - `cp -R externals/dawn/out/android_arm64-v8a/gen/include/webgpu ${projectRoot}/cpp`, - ); - $( - `cp externals/dawn/out/android_arm64-v8a/gen/include/dawn/webgpu.h ${projectRoot}/cpp/webgpu/webgpu.h`, - ); - $( - `cp externals/dawn/out/android_arm64-v8a/gen/include/dawn/webgpu_cpp.h ${projectRoot}/cpp/webgpu/webgpu_cpp.h`, - ); - $( - `cp externals/dawn/include/webgpu/webgpu_enum_class_bitmasks.h ${projectRoot}/cpp/webgpu/`, - ); - $(`cp externals/dawn/src/dawn/dawn.json ${projectRoot}/libs`); + copyHeaders(); chdir(projectRoot); checkBuildArtifacts(); })(); diff --git a/packages/webgpu/scripts/build/util.ts b/packages/webgpu/scripts/build/util.ts index 776f81bec..acaa7a242 100644 --- a/packages/webgpu/scripts/build/util.ts +++ b/packages/webgpu/scripts/build/util.ts @@ -1,24 +1,7 @@ -/* eslint-disable max-len */ import { spawn, execSync } from "child_process"; import { existsSync } from "fs"; import { exit } from "process"; -export const libs = ["libwebgpu_dawn"] as const; - -export const projectRoot = "packages/webgpu"; - -export const platforms = [ - "arm64", - "x86_64", - "x86", - "armeabi-v7a", - "arm64-v8a", - "universal", -] as const; - -export type OS = "apple" | "android"; -export type Platform = (typeof platforms)[number]; - export const runAsync = (command: string, label: string): Promise => { return new Promise((resolve, reject) => { const [cmd, ...args] = command.split(" "); @@ -72,58 +55,3 @@ export const $ = (command: string) => { exit(1); } }; - -const serializeCMakeArgs = (args: Record) => { - return Object.keys(args) - .map((key) => `-D${key}=${args[key]}`) - .join(" "); -}; - -export const build = async ( - label: string, - args: Record, - debugLabel: string, -) => { - console.log(`🔨 Building ${label}`); - $(`mkdir -p externals/dawn/out/${label}`); - process.chdir(`externals/dawn/out/${label}`); - const cmd = `cmake ../.. -G Ninja ${serializeCMakeArgs(args)}`; - await runAsync(cmd, debugLabel); - await runAsync("ninja", debugLabel); - process.chdir("../../../.."); -}; - -export const copyLib = (os: OS, platform: Platform, sdk?: string) => { - const suffix = `${platform}${sdk ? `_${sdk}` : ""}`; - const out = `${os}_${suffix}`; - const dstPath = `${projectRoot}/libs/${os}/${suffix}/`; - $(`mkdir -p ${dstPath}`); - if (os === "android") { - console.log("Strip debug symbols from libwebgpu_dawn.a..."); - $( - `$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/llvm-strip externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.so`, - ); - } - [ - `externals/dawn/out/${out}/src/dawn/native/libwebgpu_dawn.${os === "apple" ? "a" : "so"}`, - ].forEach((lib) => { - const libPath = lib; - console.log(`Copying ${libPath} to ${dstPath}`); - $(`cp ${libPath} ${dstPath}`); - }); -}; - -export const checkBuildArtifacts = () => { - console.log("Check build artifacts..."); - platforms - .filter((arch) => arch !== "arm64" && arch !== "universal") - .forEach((platform) => { - libs.forEach((lib) => { - checkFileExists(`libs/android/${platform}/${lib}.so`); - }); - }); - libs.forEach((lib) => { - checkFileExists(`libs/apple/${lib}.xcframework`); - }); - checkFileExists("libs/dawn.json"); -}; From 678c29ef106f4ea8c569d6340a06a830c5fa939a Mon Sep 17 00:00:00 2001 From: William Candillon Date: Tue, 12 Nov 2024 14:29:28 +0100 Subject: [PATCH 4/4] Update ApplePlatformContext.mm (#176) --- packages/webgpu/android/cpp/AndroidPlatformContext.h | 2 +- packages/webgpu/apple/ApplePlatformContext.mm | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/webgpu/android/cpp/AndroidPlatformContext.h b/packages/webgpu/android/cpp/AndroidPlatformContext.h index 12a959f2f..5a1ff4b28 100644 --- a/packages/webgpu/android/cpp/AndroidPlatformContext.h +++ b/packages/webgpu/android/cpp/AndroidPlatformContext.h @@ -34,7 +34,7 @@ class AndroidPlatformContext : public PlatformContext { wgpu::Surface makeSurface(wgpu::Instance instance, void *window, int width, int height) override { - wgpu::SurfaceDescriptorFromAndroidNativeWindow androidSurfaceDesc; + wgpu::SurfaceSourceAndroidNativeWindow androidSurfaceDesc; androidSurfaceDesc.window = reinterpret_cast(window); wgpu::SurfaceDescriptor surfaceDescriptor; surfaceDescriptor.nextInChain = &androidSurfaceDesc; diff --git a/packages/webgpu/apple/ApplePlatformContext.mm b/packages/webgpu/apple/ApplePlatformContext.mm index e0813c3fa..03f2d0b5f 100644 --- a/packages/webgpu/apple/ApplePlatformContext.mm +++ b/packages/webgpu/apple/ApplePlatformContext.mm @@ -11,7 +11,7 @@ namespace rnwgpu { -void isSimulatorWithAPIValidation() { +void checkIfUsingSimulatorWithAPIValidation() { #if TARGET_OS_SIMULATOR NSDictionary *environment = [[NSProcessInfo processInfo] environment]; NSString *metalDeviceWrapperType = environment[@"METAL_DEVICE_WRAPPER_TYPE"]; @@ -25,12 +25,12 @@ void isSimulatorWithAPIValidation() { #endif } -ApplePlatformContext::ApplePlatformContext() { isSimulatorWithAPIValidation(); } +ApplePlatformContext::ApplePlatformContext() { checkIfUsingSimulatorWithAPIValidation(); } wgpu::Surface ApplePlatformContext::makeSurface(wgpu::Instance instance, void *surface, int width, int height) { - wgpu::SurfaceDescriptorFromMetalLayer metalSurfaceDesc; + wgpu::SurfaceSourceMetalLayer metalSurfaceDesc; metalSurfaceDesc.layer = surface; wgpu::SurfaceDescriptor surfaceDescriptor; surfaceDescriptor.nextInChain = &metalSurfaceDesc;