|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { |
| 3 | + Platform, |
| 4 | + PixelRatio, |
| 5 | + StyleSheet, |
| 6 | + Switch, |
| 7 | + Text, |
| 8 | + View, |
| 9 | +} from "react-native"; |
| 10 | +import type { CanvasRef } from "react-native-wgpu"; |
| 11 | +import { Canvas } from "react-native-wgpu"; |
| 12 | + |
| 13 | +import { fullscreenTriangleVertWGSL, hdrBandFragWGSL } from "./shaders"; |
| 14 | + |
| 15 | +type ToneMapping = "standard" | "extended"; |
| 16 | + |
| 17 | +const HDR_FORMAT: GPUTextureFormat = "rgba16float"; |
| 18 | +const PEAK_MULTIPLIER = 8.0; // 8x SDR reference white. |
| 19 | + |
| 20 | +function HDRCanvas({ |
| 21 | + toneMapping, |
| 22 | + peak, |
| 23 | +}: { |
| 24 | + toneMapping: ToneMapping; |
| 25 | + peak: number; |
| 26 | +}) { |
| 27 | + const ref = useRef<CanvasRef>(null); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + let cancelled = false; |
| 31 | + (async () => { |
| 32 | + const adapter = await navigator.gpu.requestAdapter(); |
| 33 | + if (!adapter) { |
| 34 | + throw new Error("No adapter"); |
| 35 | + } |
| 36 | + const device = await adapter.requestDevice(); |
| 37 | + if (cancelled) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const context = ref.current!.getContext("webgpu")!; |
| 42 | + const canvas = context.canvas as HTMLCanvasElement; |
| 43 | + canvas.width = canvas.clientWidth * PixelRatio.get(); |
| 44 | + canvas.height = canvas.clientHeight * PixelRatio.get(); |
| 45 | + |
| 46 | + context.configure({ |
| 47 | + device, |
| 48 | + format: HDR_FORMAT, |
| 49 | + alphaMode: "opaque", |
| 50 | + toneMapping: { mode: toneMapping }, |
| 51 | + }); |
| 52 | + |
| 53 | + const pipeline = device.createRenderPipeline({ |
| 54 | + layout: "auto", |
| 55 | + vertex: { |
| 56 | + module: device.createShaderModule({ |
| 57 | + code: fullscreenTriangleVertWGSL, |
| 58 | + }), |
| 59 | + entryPoint: "main", |
| 60 | + }, |
| 61 | + fragment: { |
| 62 | + module: device.createShaderModule({ code: hdrBandFragWGSL }), |
| 63 | + entryPoint: "main", |
| 64 | + targets: [{ format: HDR_FORMAT }], |
| 65 | + }, |
| 66 | + primitive: { topology: "triangle-list" }, |
| 67 | + }); |
| 68 | + |
| 69 | + const paramsBuffer = device.createBuffer({ |
| 70 | + size: 16, |
| 71 | + usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, |
| 72 | + }); |
| 73 | + device.queue.writeBuffer( |
| 74 | + paramsBuffer, |
| 75 | + 0, |
| 76 | + new Float32Array([peak, 0, 0, 0]), |
| 77 | + ); |
| 78 | + |
| 79 | + const bindGroup = device.createBindGroup({ |
| 80 | + layout: pipeline.getBindGroupLayout(0), |
| 81 | + entries: [{ binding: 0, resource: { buffer: paramsBuffer } }], |
| 82 | + }); |
| 83 | + |
| 84 | + const encoder = device.createCommandEncoder(); |
| 85 | + const pass = encoder.beginRenderPass({ |
| 86 | + colorAttachments: [ |
| 87 | + { |
| 88 | + view: context.getCurrentTexture().createView(), |
| 89 | + clearValue: [0, 0, 0, 1], |
| 90 | + loadOp: "clear", |
| 91 | + storeOp: "store", |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + pass.setPipeline(pipeline); |
| 96 | + pass.setBindGroup(0, bindGroup); |
| 97 | + pass.draw(3); |
| 98 | + pass.end(); |
| 99 | + device.queue.submit([encoder.finish()]); |
| 100 | + context.present(); |
| 101 | + })(); |
| 102 | + return () => { |
| 103 | + cancelled = true; |
| 104 | + }; |
| 105 | + }, [toneMapping, peak]); |
| 106 | + |
| 107 | + return <Canvas ref={ref} style={StyleSheet.absoluteFill} />; |
| 108 | +} |
| 109 | + |
| 110 | +export function HDR() { |
| 111 | + const [extended, setExtended] = useState(true); |
| 112 | + const mode: ToneMapping = extended ? "extended" : "standard"; |
| 113 | + return ( |
| 114 | + <View style={styles.container}> |
| 115 | + <View style={styles.toolbar}> |
| 116 | + <Text style={styles.title}>HDR — {mode}</Text> |
| 117 | + <View style={styles.switchRow}> |
| 118 | + <Text style={styles.switchLabel}>Extended</Text> |
| 119 | + <Switch value={extended} onValueChange={setExtended} /> |
| 120 | + </View> |
| 121 | + </View> |
| 122 | + |
| 123 | + <Text style={styles.hint}> |
| 124 | + Left band: SDR white (1.0). Right band: {PEAK_MULTIPLIER}x. Toggle the |
| 125 | + switch. With "extended" on an EDR display (iPhone Pro / iPad Pro XDR / |
| 126 | + MBP XDR), the right band glows visibly brighter than the left. In |
| 127 | + "standard" both bands match. |
| 128 | + </Text> |
| 129 | + <Text style={styles.hint}> |
| 130 | + Tip: dim the display brightness; the OS allocates more EDR headroom at |
| 131 | + lower SDR brightness, so the boost is more obvious. iOS Settings, |
| 132 | + Display & Brightness, Auto-Brightness can also affect headroom. |
| 133 | + </Text> |
| 134 | + |
| 135 | + <View style={styles.canvasContainer}> |
| 136 | + {/* Force a fresh CAMetalLayer per mode: iOS won't downgrade a layer |
| 137 | + out of EDR composition once it's been promoted, so we remount the |
| 138 | + Canvas (and therefore the underlying MetalView) when the toggle |
| 139 | + flips. */} |
| 140 | + <HDRCanvas key={mode} toneMapping={mode} peak={PEAK_MULTIPLIER} /> |
| 141 | + </View> |
| 142 | + |
| 143 | + {Platform.OS !== "ios" && Platform.OS !== "macos" ? ( |
| 144 | + <Text style={styles.warning}> |
| 145 | + Note: HDR display is currently only wired for Apple platforms. |
| 146 | + </Text> |
| 147 | + ) : null} |
| 148 | + </View> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +const styles = StyleSheet.create({ |
| 153 | + container: { |
| 154 | + flex: 1, |
| 155 | + backgroundColor: "black", |
| 156 | + }, |
| 157 | + toolbar: { |
| 158 | + flexDirection: "row", |
| 159 | + alignItems: "center", |
| 160 | + justifyContent: "space-between", |
| 161 | + padding: 12, |
| 162 | + }, |
| 163 | + title: { |
| 164 | + color: "white", |
| 165 | + fontSize: 14, |
| 166 | + fontWeight: "600", |
| 167 | + flex: 1, |
| 168 | + }, |
| 169 | + switchRow: { |
| 170 | + flexDirection: "row", |
| 171 | + alignItems: "center", |
| 172 | + }, |
| 173 | + switchLabel: { |
| 174 | + color: "white", |
| 175 | + marginRight: 8, |
| 176 | + }, |
| 177 | + hint: { |
| 178 | + color: "#bbb", |
| 179 | + fontSize: 12, |
| 180 | + paddingHorizontal: 12, |
| 181 | + paddingBottom: 8, |
| 182 | + }, |
| 183 | + canvasContainer: { |
| 184 | + flex: 1, |
| 185 | + margin: 12, |
| 186 | + backgroundColor: "black", |
| 187 | + }, |
| 188 | + warning: { |
| 189 | + color: "#ffb347", |
| 190 | + fontSize: 12, |
| 191 | + padding: 12, |
| 192 | + }, |
| 193 | +}); |
0 commit comments