-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpost.js
227 lines (227 loc) · 7.47 KB
/
post.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Program from 'nanogl/program';
import Fbo from 'nanogl/fbo';
import GLArrayBuffer from 'nanogl/arraybuffer';
import PixelFormats from 'nanogl-pf';
import main_frag from './glsl/templates/main.frag';
import main_vert from './glsl/templates/main.vert';
import { EffectDependency } from './effects/base-effect';
import { isWebgl2 } from 'nanogl/types';
export default class Post {
constructor(gl, mipmap = false) {
this.gl = gl;
this._effects = [];
this._flags = 0;
this._shaderInvalid = true;
this.renderWidth = 1;
this.renderHeight = 1;
this.bufferWidth = 1;
this.bufferHeight = 1;
this.enabled = true;
this.mipmap = mipmap;
this.float_texture_ext = gl.getExtension('OES_texture_float');
this.halfFloat = gl.getExtension("OES_texture_half_float");
this.float_texture_ext_l = gl.getExtension("OES_texture_half_float_linear");
this.halfFloat_l = gl.getExtension("OES_texture_float_linear");
this.color_buffer_float = gl.getExtension('EXT_color_buffer_float');
this.hasDepthTexture = PixelFormats.getInstance(gl).hasDepthTexture();
this.mainFbo = this.genFbo();
this.mainColor = this.mainFbo.getColor(0);
if (this.mipmap) {
this.mainColor.bind();
gl.generateMipmap(gl.TEXTURE_2D);
const err = gl.getError();
if (err) {
this.mipmap = false;
this.mainFbo.dispose();
this.mainFbo = this.genFbo();
this.mainColor = this.mainFbo.getColor(0);
}
}
this.mainColor.setFilter(false, this.mipmap, false);
this.prg = new Program(gl);
const fsData = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
this.fsPlane = new GLArrayBuffer(gl, fsData);
this.fsPlane.attrib('aTexCoord0', 2, gl.FLOAT);
}
dispose() {
this.mainFbo.dispose();
this.fsPlane.dispose();
this.prg.dispose();
}
_needDepth() {
return (this._flags & EffectDependency.DEPTH) !== 0;
}
_needLinear() {
return (this._flags & EffectDependency.LINEAR) !== 0;
}
genFbo() {
const gl = this.gl;
const pf = PixelFormats.getInstance(gl);
const ctxAttribs = gl.getContextAttributes();
const configs = [
pf.RGB16F,
pf.RGBA16F,
pf.RGB32F,
pf.RGBA32F,
pf.RGB8
];
if (isWebgl2(gl)) {
}
const cfg = pf.getRenderableFormat(configs);
const fbo = new Fbo(gl);
fbo.bind();
fbo.attachColor(cfg.format, cfg.type, cfg.internal);
fbo.attachDepth(ctxAttribs.depth, ctxAttribs.stencil, this.hasDepthTexture);
fbo.resize(4, 4);
const color = fbo.getColor(0);
color.bind();
color.clamp();
if (this.hasDepthTexture) {
const depth = fbo.getDepth();
depth.bind();
depth.clamp();
depth.setFilter(false, false, false);
}
return fbo;
}
add(effect) {
if (this._effects.indexOf(effect) === -1) {
this._effects.push(effect);
effect._init(this);
effect.resize(this.renderWidth, this.renderHeight);
this._flags |= effect._flags;
this._shaderInvalid = true;
}
}
remove(effect) {
const i = this._effects.indexOf(effect);
if (i > -1) {
this._effects.splice(i, 1);
effect.release();
effect.post = null;
this._shaderInvalid = true;
if (effect._flags !== 0) {
this._flags = 0;
for (var j = 0; j < this._effects.length; j++) {
this._flags |= effect._flags;
}
}
}
}
resize(w, h) {
this.bufferWidth = w;
this.bufferHeight = h;
this.mainFbo.resize(this.bufferWidth, this.bufferHeight);
for (var i = 0; i < this._effects.length; i++) {
this._effects[i].resize(w, h);
}
}
preRender(w, h) {
this.renderWidth = w;
this.renderHeight = h;
if (this.enabled) {
const bufferWidth = this.mipmap ? nextPOT(w) : w;
const bufferHeight = this.mipmap ? nextPOT(h) : h;
if (this.bufferWidth !== bufferWidth || this.bufferHeight !== bufferHeight) {
this.resize(bufferWidth, bufferHeight);
}
}
}
needDepthPass() {
return this.enabled && this._needDepth() && !this.hasDepthTexture;
}
bindColor() {
const gl = this.gl;
if (this.enabled) {
this.mainFbo.bind();
}
else {
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
gl.viewport(0, 0, this.renderWidth, this.renderHeight);
this.mainFbo.clear();
}
render(toFbo) {
if (!this.enabled) {
return;
}
const gl = this.gl;
this.mainColor.bind();
if (this.mipmap) {
gl.generateMipmap(gl.TEXTURE_2D);
}
for (var i = 0; i < this._effects.length; i++) {
this._effects[i].preRender();
}
if (toFbo !== undefined) {
toFbo.resize(this.renderWidth, this.renderHeight);
toFbo.bind();
}
else {
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
gl.viewport(0, 0, this.renderWidth, this.renderHeight);
gl.clearColor(.0, .0, .0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if (this._shaderInvalid) {
this.buildProgram();
}
this.prg.use();
for (var i = 0; i < this._effects.length; i++) {
this._effects[i].setupProgram(this.prg);
}
this.prg.tInput(this.mainColor);
if (this._needDepth()) {
if (this.hasDepthTexture)
this.prg.tDepth(this.mainFbo.getDepth());
else
throw "no depth texture";
}
this.fillScreen(this.prg);
}
fillScreen(prg, fullframe = false) {
if (fullframe === true) {
prg.uViewportScale(1, 1);
}
else {
prg.uViewportScale(this.renderWidth / this.bufferWidth, this.renderHeight / this.bufferHeight);
}
this.fsPlane.attribPointer(prg);
this.fsPlane.drawTriangleStrip();
}
buildProgram() {
var codeList = [], precodeList = [];
var effects = this._effects;
for (var i = 0; i < effects.length; i++) {
effects[i].genCode(precodeList, codeList);
}
const code = codeList.join('\n');
const precode = precodeList.join('\n');
var frag = main_frag({
code: code,
precode: precode
});
var vert = main_vert();
var depthTex = this._needDepth() && this.hasDepthTexture;
var defs = '';
if (isWebgl2(this.gl)) {
defs += '#version 300 es\n';
}
defs += 'precision highp float;\n';
defs += '#define NEED_DEPTH ' + (+this._needDepth()) + '\n';
defs += '#define TEXTURE_DEPTH ' + (+depthTex) + '\n';
this.prg.compile(vert, frag, defs);
this._shaderInvalid = false;
this.mainColor.bind();
this.mainColor.setFilter(this._needLinear(), this.mipmap, false);
}
}
const MAX_POT = 4096;
function nextPOT(n) {
var p = 1;
while (p < n)
p <<= 1;
if (p > MAX_POT)
p = MAX_POT;
return p;
}