Skip to content

Commit

Permalink
feat(webgl): add passCopy() HOF pass gen
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 6, 2021
1 parent 4043293 commit fb6b5b7
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/webgl/src/multipass.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { assert, IObjectOf } from "@thi.ng/api";
import {
$xy,
assign,
defMain,
INT0,
ivec2,
texelFetch,
} from "@thi.ng/shader-ast";
import { assocObj, map, range, some, transduce } from "@thi.ng/transducers";
import type { ExtensionBehaviors } from "./api/ext";
import type { Multipass, MultipassOpts, PassOpts } from "./api/multipass";
Expand Down Expand Up @@ -180,3 +188,44 @@ const initBuffers = (
).map((pass) =>
defFBO(opts.gl, { tex: pass.outputs.map((id) => textures[id]) })
);

/**
* Returns a dynamically generated single pass spec ({@link PassOpts}) for use
* within a larger multipass pipeline spec, and which copies given `src`
* textures into their respective `dest` textures (e.g. for feedback purposes).
*
* @remarks
* Both arrays must have same length. The first `src` texture is written to the
* first `dest` tex, etc.
*
* WebGL2 only (uses `texelFetch()`)
*
* @param src
* @param dest
*/
export const passCopy = (src: string[], dest: string[]): PassOpts => {
assert(
src.length === dest.length,
`require same number of in/out textures`
);
return {
fs: (gl, unis, _, outs) => [
defMain(() => [
...map(
(i) =>
assign(
outs[`output${i}`],
texelFetch(
unis[`input${i}`],
ivec2($xy(gl.gl_FragCoord)),
INT0
)
),
range(src.length)
),
]),
],
inputs: src,
outputs: dest,
};
};

0 comments on commit fb6b5b7

Please sign in to comment.