컴파일된 프로그램 실행

컴파일된 프로그램 실행

버텍스 진입점과 프래그먼트 진입점, 그리고 엔진처럼 이를 실행하는 호스트입니다. 모듈의 기본 내보내기가 매니페스트입니다. 호스트는 프로그램 런타임에서 rt.load()로 매니페스트를 불러오고, 렌더 파이프라인을 만들어 프레임마다 텍스처에 전체 화면 삼각형을 그립니다. 유니폼은 이름으로 바인딩합니다. 마지막으로 텍스처를 읽어 온다(규칙 11.10, 11.11).

실행 tier WebGPUWebGL2

이 목록은 컴파일러가 확인합니다. tier(호출이 실행되는 백엔드)는 호스트의 실행 전체가 그 tier 하나만으로 기준값을 낼 때만 목록에 오릅니다. 컴파일러의 저니 게이트가 호스트마다 WebGPU와 WebGL2에서 실행하고, 단위 테스트가 CPU tier에서 실행합니다.

"use typeshade";
/* @example
{
"title": "Host: run the compiled program",
"blurb": "A vertex and a fragment entry, and a host that runs them as an engine does. The module's default export is its manifest; the host loads it with `rt.load()` on the program runtime, makes a render pipeline, and draws a full-screen triangle into a texture each frame, binding the uniform by name. Then it reads the texture back (Rules 11.10, 11.11).",
"renderable": true
}
*/
class Sky {
size: vec2;
time: f32;
}
declare const sky: uniform<Sky>;
class VsOut {
@builtin("position") pos: vec4;
}
class Color {
@location(0) color: vec4;
}
// Three vertices that cover the target: the host draws `{ count: 3 }` and binds no vertex buffer.
@vertex
export function vs(@builtin("vertex_index") vi: u32): VsOut {
const x = f32(i32(vi) / 2) * 4. - 1.;
const y = f32(i32(vi) % 2) * 4. - 1.;
return { pos: vec4(x, y, 0., 1.) };
}
@fragment
export function fs(@builtin("position") p: vec4): Color {
const uv = p.xy / sky.size;
return { color: vec4(uv.x, 1. - uv.y, 0.5 + 0.5 * cos(sky.time), 1.) };
}
// Run a module's compiled program on the program runtime, as an engine does (surface §69, Rules
// 11.10 and 11.11).
//
// A shader module's default export is its manifest: the WGSL, the GLSL ES 3.00 programs, and each
// binding with its layout. `typeshade/runtime` loads it on WebGPU, or on WebGL2, and makes the
// pipelines; the host records frames and binds values by name.
import { runtime } from 'typeshade/runtime';
import program from './host-program.shade.ts';
/** Draw a frame into a `width` by `height` texture at each of `times`, then read it back:
* RGBA, one byte to a channel, the top row first. */
export async function run(
width = 32,
height = 32,
times: readonly number[] = [0, 0.5, 1.25],
): Promise<Uint8Array> {
const rt = await runtime(); // the default runtime: WebGPU, or WebGL2
const pipeline = await rt.load(program).render({ targets: ['rgba8unorm'] });
const target = rt.texture({ size: [width, height], format: 'rgba8unorm' });
for (const time of times) {
const frame = rt.frame();
frame.pass({ color: [target] }, (pass) =>
pass.draw(pipeline, { sky: { size: [width, height], time } }, { count: 3 }),
);
await frame.submit();
}
const pixels = await target.read(); // the last frame
target.destroy();
return pixels;
}

두 파일 모두 커밋 91c2fe31의 컴파일러에 있는 그대로이며, 예제 디렉터리에서 읽어 왔습니다.

GitHub의 모듈GitHub의 호스트 파일

이 페이지 편집 문제 보고