UNPKG

4.36 kBJavaScriptView Raw
1import { ExtensionType, extensions } from "@pixi/extensions";
2import { GLBuffer } from "./GLBuffer.mjs";
3class BufferSystem {
4 /**
5 * @param {PIXI.Renderer} renderer - The renderer this System works for.
6 */
7 constructor(renderer) {
8 this.renderer = renderer, this.managedBuffers = {}, this.boundBufferBases = {};
9 }
10 /**
11 * @ignore
12 */
13 destroy() {
14 this.renderer = null;
15 }
16 /** Sets up the renderer context and necessary buffers. */
17 contextChange() {
18 this.disposeAll(!0), this.gl = this.renderer.gl, this.CONTEXT_UID = this.renderer.CONTEXT_UID;
19 }
20 /**
21 * This binds specified buffer. On first run, it will create the webGL buffers for the context too
22 * @param buffer - the buffer to bind to the renderer
23 */
24 bind(buffer) {
25 const { gl, CONTEXT_UID } = this, glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
26 gl.bindBuffer(buffer.type, glBuffer.buffer);
27 }
28 unbind(type) {
29 const { gl } = this;
30 gl.bindBuffer(type, null);
31 }
32 /**
33 * Binds an uniform buffer to at the given index.
34 *
35 * A cache is used so a buffer will not be bound again if already bound.
36 * @param buffer - the buffer to bind
37 * @param index - the base index to bind it to.
38 */
39 bindBufferBase(buffer, index) {
40 const { gl, CONTEXT_UID } = this;
41 if (this.boundBufferBases[index] !== buffer) {
42 const glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
43 this.boundBufferBases[index] = buffer, gl.bindBufferBase(gl.UNIFORM_BUFFER, index, glBuffer.buffer);
44 }
45 }
46 /**
47 * Binds a buffer whilst also binding its range.
48 * This will make the buffer start from the offset supplied rather than 0 when it is read.
49 * @param buffer - the buffer to bind
50 * @param index - the base index to bind at, defaults to 0
51 * @param offset - the offset to bind at (this is blocks of 256). 0 = 0, 1 = 256, 2 = 512 etc
52 */
53 bindBufferRange(buffer, index, offset) {
54 const { gl, CONTEXT_UID } = this;
55 offset = offset || 0;
56 const glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
57 gl.bindBufferRange(gl.UNIFORM_BUFFER, index || 0, glBuffer.buffer, offset * 256, 256);
58 }
59 /**
60 * Will ensure the data in the buffer is uploaded to the GPU.
61 * @param {PIXI.Buffer} buffer - the buffer to update
62 */
63 update(buffer) {
64 const { gl, CONTEXT_UID } = this, glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
65 if (buffer._updateID !== glBuffer.updateID)
66 if (glBuffer.updateID = buffer._updateID, gl.bindBuffer(buffer.type, glBuffer.buffer), glBuffer.byteLength >= buffer.data.byteLength)
67 gl.bufferSubData(buffer.type, 0, buffer.data);
68 else {
69 const drawType = buffer.static ? gl.STATIC_DRAW : gl.DYNAMIC_DRAW;
70 glBuffer.byteLength = buffer.data.byteLength, gl.bufferData(buffer.type, buffer.data, drawType);
71 }
72 }
73 /**
74 * Disposes buffer
75 * @param {PIXI.Buffer} buffer - buffer with data
76 * @param {boolean} [contextLost=false] - If context was lost, we suppress deleteVertexArray
77 */
78 dispose(buffer, contextLost) {
79 if (!this.managedBuffers[buffer.id])
80 return;
81 delete this.managedBuffers[buffer.id];
82 const glBuffer = buffer._glBuffers[this.CONTEXT_UID], gl = this.gl;
83 buffer.disposeRunner.remove(this), glBuffer && (contextLost || gl.deleteBuffer(glBuffer.buffer), delete buffer._glBuffers[this.CONTEXT_UID]);
84 }
85 /**
86 * dispose all WebGL resources of all managed buffers
87 * @param {boolean} [contextLost=false] - If context was lost, we suppress `gl.delete` calls
88 */
89 disposeAll(contextLost) {
90 const all = Object.keys(this.managedBuffers);
91 for (let i = 0; i < all.length; i++)
92 this.dispose(this.managedBuffers[all[i]], contextLost);
93 }
94 /**
95 * creates and attaches a GLBuffer object tied to the current context.
96 * @param buffer
97 * @protected
98 */
99 createGLBuffer(buffer) {
100 const { CONTEXT_UID, gl } = this;
101 return buffer._glBuffers[CONTEXT_UID] = new GLBuffer(gl.createBuffer()), this.managedBuffers[buffer.id] = buffer, buffer.disposeRunner.add(this), buffer._glBuffers[CONTEXT_UID];
102 }
103}
104BufferSystem.extension = {
105 type: ExtensionType.RendererSystem,
106 name: "buffer"
107};
108extensions.add(BufferSystem);
109export {
110 BufferSystem
111};
112//# sourceMappingURL=BufferSystem.mjs.map