Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add indexTo*() and readIndex*() fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 6, 2019
1 parent a0af395 commit a804c28
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 27 deletions.
2 changes: 2 additions & 0 deletions packages/shader-ast-stdlib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ export * from "./sdf/torus";
export * from "./sdf/tri";
export * from "./sdf/union";

export * from "./tex/index-coord";
export * from "./tex/index-uv";
export * from "./tex/read-index";
30 changes: 30 additions & 0 deletions packages/shader-ast-stdlib/src/tex/index-coord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
$x,
$y,
div,
madd,
modi,
UintTerm,
uvec2
} from "@thi.ng/shader-ast";
import { UVec2Term } from "@thi.ng/shader-ast/api";

/**
* Inline function. Similar to `indexToUV()`, but returns uvec2 in pixel
* coords. Not compatible with WebGL1.
*
* @param i
* @param width
*/
export const indexToCoord = (i: UintTerm, width: UintTerm) =>
uvec2(modi(i, width), div(i, width));

/**
* Inline function. Reverse op to `indexToCoord()`. Not compatible with
* WebGL1.
*
* @param coord
* @param width
*/
export const coordToIndex = (coord: UVec2Term, width: UintTerm) =>
madd($y(coord), width, $x(coord));
55 changes: 55 additions & 0 deletions packages/shader-ast-stdlib/src/tex/index-uv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
$x,
$y,
add,
defn,
div,
float,
int,
modi,
mul,
ret,
vec2
} from "@thi.ng/shader-ast";

/**
* Converts linearized 2D index `i` into a vec2 UV coord, based on given
* texture `size` (in pixels).
*
* @param i
* @param size
*/
export const indexToUV = defn(
"vec2",
"indexToUV",
[["int", "i", { prec: "highp" }], ["ivec2"]],
(i, size) => [
ret(
vec2(
div(float(modi(i, $x(size))), float($x(size))),
div(float(div(i, $x(size))), float($y(size)))
)
)
]
);

/**
* Inverse operation of `indexToUV()`. Converts vec2 UV coord into
* linearized 2D index, based on given texture `width` (in pixels).
*
* @param i
* @param width
*/
export const uvToIndex = defn(
"int",
"uvToIndex",
[["vec2"], ["int", "width", { prec: "highp" }]],
(uv, width) => [
ret(
add(
int(mul($x(uv), float(width))),
int(mul($y(uv), float(mul(width, width))))
)
)
]
);
34 changes: 7 additions & 27 deletions packages/shader-ast-stdlib/src/tex/read-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@ import {
$x,
$xy,
$xyz,
$y,
defn,
div,
float,
IntTerm,
IVec2Term,
modi,
ret,
Sampler2DSym,
texture,
vec2
texture
} from "@thi.ng/shader-ast";

export const indexToUV = defn(
"vec2",
"indexToUV",
[["int"], ["ivec2"]],
(i, size) => [
ret(
vec2(
div(float(modi(i, $x(size))), float($x(size))),
div(float(div(i, $x(size))), float($y(size)))
)
)
]
);
import { Sampler2DTerm } from "@thi.ng/shader-ast/api";
import { indexToUV } from "./index-uv";

/**
* Inline function. Returns x component at index `i` in `tex`.
Expand All @@ -36,7 +16,7 @@ export const indexToUV = defn(
* @param i
* @param size
*/
export const readIndex1 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
export const readIndex1 = (tex: Sampler2DTerm, i: IntTerm, size: IVec2Term) =>
$x(texture(tex, indexToUV(i, size)));

/**
Expand All @@ -46,7 +26,7 @@ export const readIndex1 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
* @param i
* @param size
*/
export const readIndex2 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
export const readIndex2 = (tex: Sampler2DTerm, i: IntTerm, size: IVec2Term) =>
$xy(texture(tex, indexToUV(i, size)));

/**
Expand All @@ -56,7 +36,7 @@ export const readIndex2 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
* @param i
* @param size
*/
export const readIndex3 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
export const readIndex3 = (tex: Sampler2DTerm, i: IntTerm, size: IVec2Term) =>
$xyz(texture(tex, indexToUV(i, size)));

/**
Expand All @@ -66,5 +46,5 @@ export const readIndex3 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
* @param i
* @param size
*/
export const readIndex4 = (tex: Sampler2DSym, i: IntTerm, size: IVec2Term) =>
export const readIndex4 = (tex: Sampler2DTerm, i: IntTerm, size: IVec2Term) =>
texture(tex, indexToUV(i, size));

0 comments on commit a804c28

Please sign in to comment.