-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrenderbuffer.js
58 lines (47 loc) · 1.59 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
52
53
54
55
56
57
58
/**
* @typedef {import("./types.js").PexResource} RenderbufferOptions
* @property {number} width
* @property {number} height
* @property {ctx.PixelFormat} [pixelFormat=ctx.PixelFormat.DEPTH_COMPONENT16] only `PixelFormat.DEPTH_COMPONENT16` is currently supported for use as render pass depth storage (e.g. `ctx.pass({ depth: renderbuffer })`) for platforms with no `WEBGL_depth_texture` support.
*/
function createRenderbuffer(ctx, opts) {
const gl = ctx.gl;
const renderbuffer = {
class: "renderbuffer",
handle: gl.createRenderbuffer(),
target: gl.RENDERBUFFER,
width: 0,
height: 0,
_update: updateRenderbuffer,
_dispose() {
gl.deleteRenderbuffer(this.handle);
this.color = null;
this.depth = null;
},
};
updateRenderbuffer(ctx, renderbuffer, opts);
return renderbuffer;
}
function updateRenderbuffer(ctx, renderbuffer, opts) {
Object.assign(renderbuffer, opts);
const gl = ctx.gl;
const internalFormat =
opts.internalFormat ||
(Object.keys(ctx.RenderbufferFloatFormat).includes(renderbuffer.pixelFormat)
? ctx.RenderbufferFloatFormat[renderbuffer.pixelFormat]
: gl[renderbuffer.pixelFormat]);
console.assert(
internalFormat,
`Unsupported float renderable format ${renderbuffer.pixelFormat}`,
);
renderbuffer.format = internalFormat;
gl.bindRenderbuffer(renderbuffer.target, renderbuffer.handle);
gl.renderbufferStorage(
renderbuffer.target,
renderbuffer.format,
renderbuffer.width,
renderbuffer.height,
);
gl.bindRenderbuffer(renderbuffer.target, null);
}
export default createRenderbuffer;