-
Notifications
You must be signed in to change notification settings - Fork 12
/
pipeline.js
97 lines (89 loc) · 2.95 KB
/
pipeline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { checkProps } from "./utils.js";
/**
* @typedef {import("./types.js").PexResource} PipelineOptions
* @property {string} [vert=null] Vertex shader code
* @property {string} [frag=null] Fragment shader code
* @property {boolean} [depthWrite=true] Depth write mask
* @property {boolean} [depthTest=false] Depth test on/off
* @property {ctx.DepthFunc} [depthFunc=ctx.DepthFunc.LessEqual] Depth test function
* @property {boolean} [blend=false] Blending on/off
* @property {ctx.BlendFactor} [blendSrcRGBFactor=ctx.BlendFactor.One] Blending source color factor
* @property {ctx.BlendFactor} [blendSrcAlphaFactor=ctx.BlendFactor.One] Blending source alpha factor
* @property {ctx.BlendFactor} [blendDstRGBFactor=ctx.BlendFactor.One] Blending destination color factor
* @property {ctx.BlendFactor} [blendDstAlphaFactor=ctx.BlendFactor.One] Blending destination alpha factor
* @property {boolean} [cullFace=false] Face culling on/off
* @property {ctx.Face} [cullFaceMode=ctx.Face.Back] Face culling mode
* @property {boolean[]} [colorMask=[true, true, true, true]] Color write mask for [r, g, b, a]
* @property {ctx.Primitive} [primitive=ctx.Primitive.Triangles] Geometry primitive
*/
const allowedProps = [
"vert",
"frag",
"program",
"depthWrite",
"depthTest",
"depthFunc",
"blend",
"blendSrcRGBFactor",
"blendSrcAlphaFactor",
"blendDstRGBFactor",
"blendDstAlphaFactor",
"cullFace",
"cullFaceMode",
"colorMask",
"primitive",
"vertexLayout",
];
function createPipeline(ctx, opts) {
checkProps(allowedProps, opts);
const pipeline = Object.assign(
{
class: "pipeline",
program: null,
depthWrite: true,
depthTest: false,
depthFunc: ctx.DepthFunc.LessEqual,
blend: false,
blendSrcRGBFactor: ctx.BlendFactor.One,
blendSrcAlphaFactor: ctx.BlendFactor.One,
blendDstRGBFactor: ctx.BlendFactor.One,
blendDstAlphaFactor: ctx.BlendFactor.One,
cullFace: false,
cullFaceMode: ctx.Face.Back,
colorMask: [true, true, true, true],
primitive: ctx.Primitive.Triangles,
_dispose() {
this.vert = null;
this.frag = null;
if (
this.program &&
--this.program.refCount === 0 &&
this.program.handle
) {
ctx.dispose(this.program);
}
this.program = null;
},
},
opts,
);
if (opts.vert && opts.frag) {
pipeline.program = ctx.program({
vert: opts.vert,
frag: opts.frag,
vertexLayout: opts.vertexLayout,
});
}
if (pipeline.program && !pipeline.vertexLayout) {
pipeline.program.refCount++;
const attributesPerLocation = pipeline.program.attributesPerLocation;
pipeline.vertexLayout = Object.keys(attributesPerLocation).map(
(location) => {
const attribute = attributesPerLocation[location];
return [attribute.name, parseInt(location, 10), attribute.size];
},
);
}
return pipeline;
}
export default createPipeline;