This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFXStage.js
176 lines (161 loc) · 5.5 KB
/
FXStage.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var FXResourceMgr = require('./FXResourceMgr');
var ScreenImage = require('./ScreenImage');
var FXStageCount = 0;
function FXStage(ctx, source, resourceMgr, fullscreenQuad) {
this.id = FXStageCount++;
this.ctx = ctx;
this.source = source || null;
this.resourceMgr = resourceMgr || new FXResourceMgr(ctx);
this.fullscreenQuad = fullscreenQuad || new ScreenImage(ctx);
this.defaultBPP = 8;
}
FXStage.prototype.reset = function() {
this.resourceMgr.markAllAsNotUsed();
return this;
};
FXStage.prototype.getOutputSize = function(width, height) {
if (width && height) {
return {
width: width,
height: height
};
}
else if (this.source && this.source.width) {
return {
width: this.source.width,
height: this.source.height
};
}
else if (this.source && this.source.getWidth) {
return {
width: this.source.getWidth(),
height: this.source.getHeight()
};
}
else {
var viewport = this.ctx.getViewport()
return {
width: viewport[2],
height: viewport[3]
};
}
};
FXStage.prototype.getRenderTarget = function(w, h, depth, bpp) {
depth = depth || false;
bpp = bpp || this.defaultBPP;
var resProps = {
w: w,
h: h,
depth: depth,
bpp: bpp
};
var res = this.resourceMgr.getResource('RenderTarget', resProps);
var ctx = this.ctx;
if (!res) {
var type = ctx.UNSIGNED_BYTE;
if (bpp == 16) type = ctx.HALF_FLOAT;
if (bpp == 32) type = ctx.FLOAT;
var colorTex = ctx.createTexture2D(null, w, h, { magFilter: ctx.LINEAR, minFilter: ctx.LINEAR, type: type });
var colorAttachments = [{
texture: colorTex
}]
var depthAttachment = null;
if (depth) {
var depthTex = ctx.createTexture2D(null, w, h, { magFilter: ctx.NEAREST, minFilter: ctx.NEAREST, format: ctx.DEPTH_COMPONENT, type: ctx.UNSIGNED_SHORT });
depthAttachment = {
texture: depthTex
}
}
var renderTarget = ctx.createFramebuffer(colorAttachments, depthAttachment);
res = this.resourceMgr.addResource('RenderTarget', renderTarget, resProps);
}
res.used = true;
return res.obj;
};
FXStage.prototype.getFXStage = function(name) {
var resProps = {};
var res = this.resourceMgr.getResource('FXStage', resProps);
if (!res) {
var fxState = new FXStage(this.ctx, null, this.resourceMgr, this.fullscreenQuad);
res = this.resourceMgr.addResource('FXStage', fxState, resProps);
}
res.used = true;
return res.obj;
};
FXStage.prototype.asFXStage = function(source, name) {
var stage = this.getFXStage(name);
stage.source = source;
stage.name = name + '_' + stage.id;
return stage;
};
FXStage.prototype.getShader = function(vert, frag) {
var resProps = { vert: vert, frag: frag };
var res = this.resourceMgr.getResource('Program', resProps);
if (!res) {
var ctx = this.ctx;
var program = ctx.createProgram(vert, frag);
res = this.resourceMgr.addResource('Program', program, resProps);
}
res.used = true;
return res.obj;
};
FXStage.prototype.getSourceTexture = function(source) {
if (source) {
if (source.source) {
if (source.source.getColorAttachment) {
return source.source.getColorAttachment(0).texture;
}
else return source.source;
}
else if (source.getColorAttachment) {
return source.getColorAttachment(0).texture;
}
else return source;
}
else if (this.source) {
if (this.source.getColorAttachment) {
return this.source.getColorAttachment(0).texture;
}
else return this.source;
}
else throw 'FXStage.getSourceTexture() No source texture!';
};
FXStage.prototype.drawFullScreenQuad = function(width, height, image, program) {
this.drawFullScreenQuadAt(0, 0, width, height, image, program);
};
FXStage.prototype.drawFullScreenQuadAt = function(x, y, width, height, image, program) {
var ctx = this.ctx;
program = program || this.fullscreenQuad.program
ctx.pushState(ctx.DEPTH_BIT | ctx.VIEWPORT_BIT | ctx.MESH_BIT | ctx.PROGRAM_BIT | ctx.TEXTURE_BIT);
ctx.setDepthTest(false);
ctx.setDepthMask(0);
ctx.setViewport(x, y, width, height);
ctx.bindMesh(this.fullscreenQuad.mesh);
ctx.bindProgram(program);
if (image) {
if (program.hasUniform('imageSize')) {
var w = image.width || image.getWidth()
var h = image.height || image.getHeight()
program.setUniform('imageSize', [w, h]); //TODO: reuse imageSize array
}
ctx.bindTexture(image, 0);
}
ctx.drawMesh();
ctx.popState(ctx.DEPTH_BIT | ctx.VIEWPORT_BIT | ctx.MESH_BIT | ctx.PROGRAM_BIT | ctx.TEXTURE_BIT);
};
FXStage.prototype.getImage = function(path) {
throw new Error('FXStage.getImage is not implemented!');
//var resProps = { path: path };
//var res = this.resourceMgr.getResource('Image', resProps);
//if (!res) {
// var image = Texture2D.load(path);
// res = this.resourceMgr.addResource('Image', image, resProps);
//}
//res.used = false;
////can be shared so no need for locking
//return res.obj;
};
FXStage.prototype.getFullScreenQuad = function() {
return this.fullscreenQuad;
};
module.exports = FXStage;