커널 함수 호출
커널 함수 두 개와 이를 부르는 호스트입니다. exposure는 array<f32>를 돌고, peak는 그 배열을 하나의 값으로 줄입니다. 호스트는 Float32Array를 넘겨 await exposure(1.5, pixels)를 부르고, 숫자 하나를 얻으려고 await peak(pixels)를 부릅니다. 컴파일러가 반복이 서로 독립이라고 증명한 루프는 GPU에서 반복마다 인보케이션 하나로 실행되고, 배열은 제자리에서 바뀌어 돌아온다(규칙 8.22).
실행 tier WebGPUCPU tier
이 목록은 컴파일러가 확인합니다. tier(호출이 실행되는 백엔드)는 호스트의 실행 전체가 그 tier 하나만으로 기준값을 낼 때만 목록에 오릅니다. 컴파일러의 저니 게이트가 호스트마다 WebGPU와 WebGL2에서 실행하고, 단위 테스트가 CPU tier에서 실행합니다.
"use typeshade";
/* @example{ "title": "Host: call a kernel function", "blurb": "Two kernel functions and the host that calls them. `exposure` loops over an `array<f32>` and `peak` reduces one; the host calls `await exposure(1.5, pixels)` with a `Float32Array` and `await peak(pixels)` for a number. Each loop the compiler proves independent runs on the GPU, one invocation per iteration, and the array comes back in place (Rule 8.22).", "renderable": false, "reason": "no entry point"}*/
// A kernel function: an exported function that takes an array with no size. Each iteration// writes only its own element, so the loop runs on the GPU, one invocation per element.export function exposure(gain: f32, pixels: array<f32>) { for (let i: u32 = 0; i < pixels.length; i++) { pixels[i] = 1. - exp(-pixels[i] * gain); }}
// A reduction: `m = max(m, x)` is folded by the workgroup tree on the GPU, and the return runs// on the host with what it folded.export function peak(xs: array<f32>): f32 { let m = 0.; for (const x of xs) { m = max(m, x); } return m;}// Call kernel functions with plain arguments (surface §65, Rule 8.22).//// A kernel function is called like any function the module exports, and awaited. Each loop the// compiler proves independent runs on the GPU; a typed array it writes comes back in place.import { exposure, peak } from './host-kernel.shade.ts';
/** Tone-map `n` values on the GPU, then find the brightest. */export async function run(n = 1024): Promise<{ pixels: Float32Array; peak: number }> { const pixels = Float32Array.from({ length: n }, (_, i) => (i % 64) / 16); await exposure(1.5, pixels); // `pixels` now holds the tone-mapped values const brightest = await peak(pixels); // a number, folded on the GPU return { pixels, peak: brightest };}두 파일 모두 커밋 91c2fe31의 컴파일러에 있는 그대로이며, 예제 디렉터리에서 읽어 왔습니다.