diff --git a/apps/example/src/App.tsx b/apps/example/src/App.tsx index 821b30d6e..6071fac00 100644 --- a/apps/example/src/App.tsx +++ b/apps/example/src/App.tsx @@ -43,6 +43,7 @@ import { ViewFormatsUseAfterFree } from "./Diagnostics/ViewFormatsUseAfterFree"; import { RenderAfterUnmount } from "./Diagnostics/RenderAfterUnmount"; import { BackgroundDetach } from "./Diagnostics/BackgroundDetach"; import { SurfaceChurn } from "./Diagnostics/SurfaceChurn"; +import { TransparencyMode } from "./Diagnostics/TransparencyMode"; import { StorageBufferVertices } from "./StorageBufferVertices"; import { SharedTextureMemory } from "./SharedTextureMemory"; import { ImportExternalTexture } from "./ImportExternalTexture"; @@ -126,6 +127,7 @@ function App() { /> + { diff --git a/apps/example/src/Diagnostics/TransparencyMode.tsx b/apps/example/src/Diagnostics/TransparencyMode.tsx new file mode 100644 index 000000000..034dbad10 --- /dev/null +++ b/apps/example/src/Diagnostics/TransparencyMode.tsx @@ -0,0 +1,112 @@ +import React, { useEffect, useRef, useState } from "react"; +import { Button, Pressable, StyleSheet, Text, View } from "react-native"; +import type { CanvasRef } from "react-native-webgpu"; +import { Canvas } from "react-native-webgpu"; + +type Mode = "default" | "texture" | "surface-overlay"; + +const ClearCanvas = ({ mode }: { mode: Mode }) => { + const ref = useRef(null); + + useEffect(() => { + let live = true; + (async () => { + const adapter = await navigator.gpu.requestAdapter(); + const device = await adapter!.requestDevice(); + const context = ref.current?.getContext("webgpu"); + if (!context || !live) { + return; + } + context.configure({ + device, + format: navigator.gpu.getPreferredCanvasFormat(), + alphaMode: "premultiplied", + }); + const frame = () => { + if (!live) { + return; + } + const encoder = device.createCommandEncoder(); + const pass = encoder.beginRenderPass({ + colorAttachments: [ + { + view: context.getCurrentTexture().createView(), + clearValue: [0.5, 0, 0, 0.5], + loadOp: "clear", + storeOp: "store", + }, + ], + }); + pass.end(); + device.queue.submit([encoder.finish()]); + context.present(); + setTimeout(frame, 200); + }; + frame(); + })().catch((error) => { + console.error(`[transparency] setup failed: ${error}`); + }); + return () => { + live = false; + }; + }, [mode]); + + return ( + + ); +}; + +export const TransparencyMode = () => { + const [mode, setMode] = useState("default"); + const [taps, setTaps] = useState(0); + + return ( + + + Half-transparent red canvas over a blue backdrop. In "texture" + mode the yellow overlay below stays visible on top of the canvas. In + "surface-overlay" mode the canvas gets its own layer and + covers the overlay. + + +