- 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
import { GLBuffer } from './GLBuffer';
import type { Renderer } from '../Renderer';
import type { IRenderingContext } from '../IRenderingContext';
import type { Buffer } from './Buffer';
import type { ISystem } from '../ISystem';
/**
* System plugin to the renderer to manage buffers.
*
* WebGL uses Buffers as a way to store objects to the GPU.
* This system makes working with them a lot easier.
*
* Buffers are used in three main places in WebGL
* - geometry information
* - Uniform information (via uniform buffer objects - a WebGL 2 only feature)
* - Transform feedback information. (WebGL 2 only feature)
*
* This system will handle the binding of buffers to the GPU as well as uploading
* them. With this system, you never need to work directly with GPU buffers, but instead work with
* the PIXI.Buffer class.
* @class
* @memberof PIXI
*/
export class BufferSystem implements ISystem
{
CONTEXT_UID: number;
gl: IRenderingContext;
/** Cache for all buffers by id, used in case renderer gets destroyed or for profiling */
readonly managedBuffers: {[key: number]: Buffer};
/** Cache keeping track of the base bound buffer bases */
readonly boundBufferBases: {[key: number]: Buffer};
private renderer: Renderer;
/**
* @param {PIXI.Renderer} renderer - The renderer this System works for.
*/
constructor(renderer: Renderer)
{
this.renderer = renderer;
this.managedBuffers = {};
this.boundBufferBases = {};
}
/**
* @ignore
*/
destroy(): void
{
this.renderer = null;
}
/** Sets up the renderer context and necessary buffers. */
protected contextChange(): void
{
this.disposeAll(true);
this.gl = this.renderer.gl;
// TODO fill out...
this.CONTEXT_UID = this.renderer.CONTEXT_UID;
}
/**
* This binds specified buffer. On first run, it will create the webGL buffers for the context too
* @param buffer - the buffer to bind to the renderer
*/
bind(buffer: Buffer): void
{
const { gl, CONTEXT_UID } = this;
const glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
gl.bindBuffer(buffer.type, glBuffer.buffer);
}
/**
* Binds an uniform buffer to at the given index.
*
* A cache is used so a buffer will not be bound again if already bound.
* @param buffer - the buffer to bind
* @param index - the base index to bind it to.
*/
bindBufferBase(buffer: Buffer, index: number): void
{
const { gl, CONTEXT_UID } = this;
if (this.boundBufferBases[index] !== buffer)
{
const glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
this.boundBufferBases[index] = buffer;
gl.bindBufferBase(gl.UNIFORM_BUFFER, index, glBuffer.buffer);
}
}
/**
* Binds a buffer whilst also binding its range.
* This will make the buffer start from the offset supplied rather than 0 when it is read.
* @param buffer - the buffer to bind
* @param index - the base index to bind at, defaults to 0
* @param offset - the offset to bind at (this is blocks of 256). 0 = 0, 1 = 256, 2 = 512 etc
*/
bindBufferRange(buffer: Buffer, index?: number, offset?: number): void
{
const { gl, CONTEXT_UID } = this;
offset = offset || 0;
const glBuffer = buffer._glBuffers[CONTEXT_UID] || this.createGLBuffer(buffer);
gl.bindBufferRange(gl.UNIFORM_BUFFER, index || 0, glBuffer.buffer, offset * 256, 256);
}
/**
* Will ensure the data in the buffer is uploaded to the GPU.
* @param {PIXI.Buffer} buffer - the buffer to update
*/
update(buffer: Buffer): void
{
const { gl, CONTEXT_UID } = this;
const glBuffer = buffer._glBuffers[CONTEXT_UID];
if (buffer._updateID === glBuffer.updateID)
{
return;
}
glBuffer.updateID = buffer._updateID;
gl.bindBuffer(buffer.type, glBuffer.buffer);
if (glBuffer.byteLength >= buffer.data.byteLength)
{
// offset is always zero for now!
gl.bufferSubData(buffer.type, 0, buffer.data);
}
else
{
const drawType = buffer.static ? gl.STATIC_DRAW : gl.DYNAMIC_DRAW;
glBuffer.byteLength = buffer.data.byteLength;
gl.bufferData(buffer.type, buffer.data, drawType);
}
}
/**
* Disposes buffer
* @param {PIXI.Buffer} buffer - buffer with data
* @param {boolean} [contextLost=false] - If context was lost, we suppress deleteVertexArray
*/
dispose(buffer: Buffer, contextLost?: boolean): void
{
if (!this.managedBuffers[buffer.id])
{
return;
}
delete this.managedBuffers[buffer.id];
const glBuffer = buffer._glBuffers[this.CONTEXT_UID];
const gl = this.gl;
buffer.disposeRunner.remove(this);
if (!glBuffer)
{
return;
}
if (!contextLost)
{
gl.deleteBuffer(glBuffer.buffer);
}
delete buffer._glBuffers[this.CONTEXT_UID];
}
/**
* dispose all WebGL resources of all managed buffers
* @param {boolean} [contextLost=false] - If context was lost, we suppress `gl.delete` calls
*/
disposeAll(contextLost?: boolean): void
{
const all: Array<any> = Object.keys(this.managedBuffers);
for (let i = 0; i < all.length; i++)
{
this.dispose(this.managedBuffers[all[i]], contextLost);
}
}
/**
* creates and attaches a GLBuffer object tied to the current context.
* @param buffer
* @protected
*/
protected createGLBuffer(buffer: Buffer): GLBuffer
{
const { CONTEXT_UID, gl } = this;
buffer._glBuffers[CONTEXT_UID] = new GLBuffer(gl.createBuffer());
this.managedBuffers[buffer.id] = buffer;
buffer.disposeRunner.add(this);
return buffer._glBuffers[CONTEXT_UID];
}
}