Skip to content

Commit 3f924cf

Browse files
feat(webgl): update defMultipass(), add sampler3D support
- update texture & shader init - update MultipassOpts
1 parent 73d7818 commit 3f924cf

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

packages/webgl/src/api/multipass.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { IObjectOf } from "@thi.ng/api";
22
import type { AttribPool } from "@thi.ng/vector-pools";
33
import type { IFbo, IndexBufferSpec } from "./buffers.js";
4+
import type { ExtensionBehaviors } from "./ext.js";
45
import type {
56
InstancingSpec,
67
ModelAttributeSpecs,
@@ -40,6 +41,7 @@ export interface MultipassOpts {
4041
passes: PassOpts[];
4142
width: number;
4243
height: number;
44+
depth?: number;
4345
uniforms?: Partial<PassUniforms>;
4446
uniformVals?: UniformValues;
4547
}
@@ -59,6 +61,7 @@ export interface PassOpts {
5961
replacePrelude?: boolean;
6062
generateDecls?: boolean;
6163
state?: Partial<ShaderState>;
64+
ext?: ExtensionBehaviors;
6265
}
6366

6467
export interface PassUniforms {

packages/webgl/src/multipass.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { IObjectOf } from "@thi.ng/api";
22
import { assert } from "@thi.ng/errors/assert";
3-
import { S2D, V2, V4 } from "@thi.ng/shader-ast/api/types";
3+
import { S2D, S3D, V2, V4 } from "@thi.ng/shader-ast/api/types";
44
import { assign } from "@thi.ng/shader-ast/ast/assign";
55
import { defMain } from "@thi.ng/shader-ast/ast/function";
66
import { INT0, ivec2 } from "@thi.ng/shader-ast/ast/lit";
77
import { $xy } from "@thi.ng/shader-ast/ast/swizzle";
88
import { texelFetch } from "@thi.ng/shader-ast/builtin/texture";
9+
import { mapIndexed } from "@thi.ng/transducers";
910
import { assocObj } from "@thi.ng/transducers/assoc-obj";
1011
import { map } from "@thi.ng/transducers/map";
1112
import { range } from "@thi.ng/transducers/range";
@@ -19,7 +20,12 @@ import type {
1920
ShaderUniformSpecs,
2021
UniformDecl,
2122
} from "./api/shader.js";
22-
import type { ITexture } from "./api/texture.js";
23+
import {
24+
TextureFilter,
25+
TextureRepeat,
26+
TextureTarget,
27+
type ITexture,
28+
} from "./api/texture.js";
2329
import { compileModel } from "./buffer.js";
2430
import { isFloatTexture, isGL2Context } from "./checks.js";
2531
import { draw } from "./draw.js";
@@ -110,28 +116,38 @@ const initPasses = (opts: MultipassOpts, textures: IObjectOf<ITexture>) => {
110116
});
111117
};
112118

119+
const TEX_TYPE_MAP: Record<number, string> = {
120+
[TextureTarget.TEXTURE_2D]: S2D,
121+
[TextureTarget.TEXTURE_3D]: S3D,
122+
[TextureTarget.TEXTURE_CUBE_MAP]: "samplerCube",
123+
};
124+
113125
const initShader = (
114126
gl: WebGLRenderingContext,
115127
pass: PassOpts,
116128
textures: IObjectOf<ITexture>
117129
) => {
118130
const isGL2 = isGL2Context(gl);
119-
const numIns = pass.inputs.length;
131+
// const numIns = pass.inputs.length;
120132
const numOuts = pass.outputs.length;
121-
const ext: ExtensionBehaviors = {};
133+
const ext: ExtensionBehaviors = { ...pass.ext };
122134
const spec: ShaderSpec = {
123135
vs: pass.vs || PASSTHROUGH_VS,
124136
fs: pass.fs,
125-
attribs: pass.attribs || {
126-
position: V2,
127-
},
137+
attribs: pass.attribs || { position: V2 },
128138
varying: pass.varying,
129139
uniforms: <ShaderUniformSpecs>{
130140
...pass.uniforms,
131141
...transduce(
132-
map((i) => <[string, UniformDecl]>[`input${i}`, [S2D, i]]),
142+
mapIndexed(
143+
(i, id) =>
144+
<[string, UniformDecl]>[
145+
`input${i}`,
146+
[TEX_TYPE_MAP[textures[id].target], i],
147+
]
148+
),
133149
assocObj(),
134-
range(numIns)
150+
pass.inputs
135151
),
136152
},
137153
outputs: numOuts
@@ -170,8 +186,9 @@ const initTextures = (opts: MultipassOpts) =>
170186
acc[id] = defTexture(opts.gl, {
171187
width: opts.width,
172188
height: opts.height,
173-
filter: opts.gl.NEAREST,
174-
wrap: opts.gl.CLAMP_TO_EDGE,
189+
depth: opts.depth,
190+
filter: TextureFilter.NEAREST,
191+
wrap: TextureRepeat.CLAMP,
175192
image: null,
176193
...opts.textures[id],
177194
});

0 commit comments

Comments
 (0)