- 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
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
import { AggregateUniformsBatch } from './AggregateUniformsBatch';
import { BaseTexture } from '@pixi/core';
import { BatchRenderer } from './BatchRenderer';
import { Matrix, Point } from '@pixi/math';
import { StdBatchFactory } from './StdBatchFactory';
import type { DisplayObject } from '@pixi/display';
import type { UniformGroup } from '@pixi/core';
/**
* Factory for producing aggregate-uniforms batches. This is useful for shaders that
* **must** use uniforms.
*/
export class AggregateUniformsBatchFactory extends StdBatchFactory
{
MAX_UNIFORMS: number;
protected uniformBuffer: { [id: string]: Array<UniformGroup> };
protected uniformMap: Array<number>;
protected uniformLength: number;
constructor(renderer: BatchRenderer)
{
super(renderer);
/**
* The max. uniforms until the batch is filled
* @member {number}
* @readonly
*/
// Max. no. of uniforms that can be passed to the batch shader. We divide by four because
// mat4d/vec4 count as four uniforms.
this.MAX_UNIFORMS = Math.floor(
Math.min(
renderer.renderer.gl.getParameter(renderer.renderer.gl.MAX_VERTEX_UNIFORM_VECTORS),
renderer.renderer.gl.getParameter(renderer.renderer.gl.MAX_FRAGMENT_UNIFORM_VECTORS))
/ (4 * renderer._uniformRedirects.length));
this.uniformBuffer = this._createUniformBuffer();
this.uniformMap = [];
this.uniformLength = 0;
}
/**
* @returns {AggregateUniformsBatch}
*/
_newBatch(): AggregateUniformsBatch
{
const batch = new AggregateUniformsBatch(this._renderer);
// All pooled batches will have a buffer
batch.uniformBuffer = this._createUniformBuffer();
batch.uniformMap = [];
return batch;
}
/**
* Stores uniforms in the current batch, if possible.
*
* If you want to override this, be sure to return beforehand if `super._put` returns
* false:
* ```
* _put(displayObject: DisplayObject): boolean
* {
* if (!super._put(displayObject))
* {
* return false;
* }
*
* // Your logic ...
* }
* ```
*
* @protected
* @returns Whether uniforms can be buffered
*/
protected _put(displayObject: DisplayObject): boolean
{
if (!this._renderer._uniformIDAttrib)
{
// No aggregation mode! If uniforms already buffered, they **must** match or batch will break.
if (this.uniformLength >= 0)
{
const id = this._matchUniforms(displayObject);
if (id >= 0)
{
return true;
}
return false;
}
}
if (this.uniformLength + 1 >= this.MAX_UNIFORMS)
{
return false;
}
if (this._renderer._uniformIDAttrib)
{
const id = this._matchUniforms(displayObject);
if (id >= 0)
{
this.uniformMap.push(id);
return true;
}
}
// Push each uniform into the buffer
for (let i = 0, j = this._renderer._uniformRedirects.length; i < j; i++)
{
const uniformRedirect = this._renderer._uniformRedirects[i];
const { source, glslIdentifer } = uniformRedirect;
const value = typeof source === 'string'
? (displayObject as any)[source]
: source(displayObject, this._renderer);
if (Array.isArray(value))
{
this.uniformBuffer[glslIdentifer].push(...value);
}
else
{
this.uniformBuffer[glslIdentifer].push(value);
}
}
this.uniformMap.push(this.uniformLength);
++this.uniformLength;
return true;
}
/**
* @protected
* @param {AggregateUniformBatch} batch
*/
_buildBatch(batch: any): void
{
super._buildBatch(batch);
const buffer = batch.uniformBuffer;
const map = batch.uniformMap;
batch.uniformBuffer = this.uniformBuffer;
batch.uniformMap = this.uniformMap;
batch.uniformLength = this.uniformLength;
// Swap & reset instead of new allocation
this.uniformBuffer = buffer;
this.uniformMap = map;
this.uniformLength = 0;
this._resetUniformBuffer(this.uniformBuffer);
this.uniformMap.length = 0;
}
/**
* Creates an array for each uniform-name in an object.
*
* @returns - the object created (the uniform buffer)
*/
private _createUniformBuffer(): { [id: string]: Array<UniformGroup> }
{
const buffer: { [id: string]: Array<UniformGroup> } = {};
for (let i = 0, j = this._renderer._uniformRedirects.length; i < j; i++)
{
const uniformRedirect = this._renderer._uniformRedirects[i];
buffer[uniformRedirect.glslIdentifer] = [];
}
return buffer;
}
/**
* Resets each array in the uniform buffer
* @param {object} buffer
*/
private _resetUniformBuffer(buffer: { [id: string]: Array<UniformGroup> }): void
{
for (let i = 0, j = this._renderer._uniformRedirects.length; i < j; i++)
{
const uniformRedirect = this._renderer._uniformRedirects[i];
buffer[uniformRedirect.glslIdentifer].length = 0;
}
}
/**
* Finds a matching set of uniforms in the buffer.
*/
private _matchUniforms(displayObject: any): number
{
const uniforms = this._renderer._uniformRedirects;
for (let i = this.uniformLength - 1; i >= 0; i--)
{
let isMatch = true;
for (let k = 0, n = uniforms.length; k < n; k++)
{
const { glslIdentifer, source } = uniforms[k];
const value = typeof source === 'string'
? displayObject[source]
: source(displayObject as DisplayObject, this._renderer);
if (!this._compareUniforms(value, this.uniformBuffer[glslIdentifer][i]))
{
isMatch = false;
break;
}
}
if (isMatch)
{
return i;
}
}
return -1;
}
// Compares two uniforms u1 & u2 for equality.
private _compareUniforms(u1: any, u2: any): boolean
{
if (u1 === u2)
{
return true;
}
// UniformGroups must have referential equality
if (u1.group || u2.group)
{
return false;
}
// Allow equals() method for custom stuff.
if (u1.equals)
{
return u1.equals(u2);
}
// Test one-depth equality for arrays
if (Array.isArray(u1) && Array.isArray(u2))
{
if (u1.length !== u2.length)
{
return false;
}
for (let i = 0, j = u1.length; i < j; i++)
{
// Referential equality for array elements
if (u1[i] !== u2[i])
{
return false;
}
}
return true;
}
if (u1 instanceof Point && u2 instanceof Point)
{
return u1.x === u2.x && u1.y === u2.y;
}
if (u1 instanceof Matrix && u2 instanceof Matrix)
{
return u1.a === u2.a && u1.b === u2.b
&& u1.c === u2.c && u1.d === u2.d
&& u1.tx === u2.tx && u1.ty === u2.ty;
}
// Unlikely for batch rendering
if (u1 instanceof BaseTexture && u2 instanceof BaseTexture)
{
return u1.uid === u2.uid;
}
return false;
}
}