-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrenderbuffer.js
51 lines (51 loc) · 1.53 KB
/
renderbuffer.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
import { isWebgl2 } from './types';
let _UID = 0;
const RENDERBUFFER = 0x8d41;
class RenderBuffer {
constructor(gl, format, samples = 0) {
this.samples = 0;
this._uid = _UID++;
this.gl = gl;
this.id = gl.createRenderbuffer();
if (samples > 0 && isWebgl2(gl)) {
const maxSamples = gl.getParameter(gl.MAX_SAMPLES);
this.samples = (samples > maxSamples) ? maxSamples : samples;
}
this.width = 0;
this.height = 0;
this.format = format || gl.DEPTH_COMPONENT16;
this._valid = false;
this._storage();
}
resize(w, h) {
if (this.width !== w || this.height !== h) {
this.width = w;
this.height = h;
this._valid = false;
}
}
allocate() {
if (!this._valid && this.width > 0 && this.height > 0) {
this._storage();
this._valid = true;
}
}
bind() {
this.gl.bindRenderbuffer(RENDERBUFFER, this.id);
}
dispose() {
this.gl.deleteRenderbuffer(this.id);
}
_storage() {
const gl = this.gl;
gl.bindRenderbuffer(RENDERBUFFER, this.id);
if (this.samples > 0 && isWebgl2(gl)) {
gl.renderbufferStorageMultisample(RENDERBUFFER, this.samples, this.format, this.width, this.height);
}
else {
gl.renderbufferStorage(RENDERBUFFER, this.format, this.width, this.height);
}
gl.bindRenderbuffer(RENDERBUFFER, null);
}
}
export default RenderBuffer;