Skip to content

Commit

Permalink
feat(shader-ast): add builtins, discard, add/refactor ControlFlow n…
Browse files Browse the repository at this point in the history
…ode type

- add texelFetch()
- add dFdx / dFdy / fwidth()
  • Loading branch information
postspectacular committed Jun 30, 2019
1 parent 72867ff commit 663e992
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
8 changes: 6 additions & 2 deletions packages/shader-ast/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import {
export type Tag =
| "arg"
| "assign"
| "break"
| "call"
| "call_i"
| "cont"
| "ctrl"
| "decl"
| "fn"
| "for"
Expand Down Expand Up @@ -522,6 +521,11 @@ export interface Ternary<T extends Type> extends Term<T> {
f: Term<T>;
}

export interface ControlFlow extends Term<"void"> {
tag: "ctrl";
id: string;
}

export interface FuncReturn<T extends Type> extends Term<T> {
val?: Term<any>;
}
Expand Down
19 changes: 11 additions & 8 deletions packages/shader-ast/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
BVec4Term,
Comparable,
ComparisonOperator,
ControlFlow,
Decl,
FloatTerm,
FnBody0,
Expand Down Expand Up @@ -1059,12 +1060,14 @@ export const whileLoop = (test: BoolTerm, body: Term<any>[]): WhileLoop => ({
scope: scope(body)
});

export const brk: Term<"void"> = {
tag: "break",
type: "void"
};
const ctrl = (id: string): ControlFlow => ({
tag: "ctrl",
type: "void",
id
});

export const cont: Term<"void"> = {
tag: "cont",
type: "void"
};
export const brk = ctrl("break");

export const cont = ctrl("continue");

export const discard = ctrl("discard");
26 changes: 25 additions & 1 deletion packages/shader-ast/src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Vec3Term,
Vec4Term
} from "./api";
import { builtinCall, FLOAT0 } from "./ast";
import { builtinCall, FLOAT0, INT0 } from "./ast";

const primOp1 = (name: string) => <T extends Prim>(a: Term<T>) =>
builtinCall(name, a.type, a);
Expand Down Expand Up @@ -321,3 +321,27 @@ export function textureOffset(sampler: Sym<Sampler>, uv: Term<Vec>, off: Term<IV
f.type === "float" && (f.info = "n");
return f;
}

// prettier-ignore
export function texelFetch(sampler: Sampler2DSym, uv: IVec2Term, lod?: IntTerm): FnCall<"vec4">;
// prettier-ignore
export function texelFetch(sampler: Sampler3DSym, uvw: IVec3Term, lod?: IntTerm): FnCall<"vec4">;
// prettier-ignore
export function texelFetch(sampler: Sym<Sampler>, uv: Term<IVec>, lod?: IntTerm): FnCall<"vec4"> {
return builtinCall(
"texelFetch",
"vec4",
sampler,
uv,
lod || INT0
);
}

export const dFdx = <T extends Prim>(sym: Sym<T>) =>
builtinCall("dFdx", sym.type, sym);

export const dFdy = <T extends Prim>(sym: Sym<T>) =>
builtinCall("dFdy", sym.type, sym);

export const fwidth = <T extends Prim>(sym: Sym<T>) =>
builtinCall("fwidth", sym.type, sym);

0 comments on commit 663e992

Please sign in to comment.