-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shader-ast-stdlib): add indexTo*() and readIndex*() fns
- Loading branch information
1 parent
a0af395
commit a804c28
Showing
4 changed files
with
94 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) | ||
) | ||
) | ||
] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters