- 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
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
import { Buffer, ObjectRenderer, Renderer, Geometry, Shader, State, ViewableBuffer } from '@pixi/core';
import { TYPES, DRAW_MODES } from '@pixi/constants';
import { log2, nextPow2 } from '@pixi/utils';
const _instanceID = 0;
let _instanceRendererID = 0;
/**
* {@code InstancedRenderer} is an object-renderer for drawing meshes/shapes/display-objects
* that have a common geometry and some "instanced" attributes.
*
* @class
* @extends PIXI.ObjectRenderer
*/
export class InstancedRenderer extends ObjectRenderer
{
public renderer: Renderer;// @pixi/core doesn't have types yet :<
public readonly instanceRendererID: string;
protected _instanceBuilder: Record<string, string>;
protected _geometry: Geometry;
protected _shader: Shader;
protected _state: State;
protected _objectBuffer: Array<{[id: string]: string}>;
protected _objectCount: number;
protected _instanceBuffer: Buffer;
protected _instanceBufferHash: number;
protected readonly _instanceAttribSizes: Record<string, number>;
protected readonly _instanceSize: number;
private _aBuffers: ViewableBuffer[] = [];
private _instanceAttribViews: Record<string, string> ;
/**
* @param {PIXI.Renderer} renderer - the WebGL renderer to attach to
* @param {PIXI.IInstancedRendererOptions} options - the pipeline description
*/
constructor(renderer: Renderer, options: IInstancedRendererOptions)
{
super(renderer);
/**
* Unique ID for this instance renderer.
*
* @protected
* @readonly
* @member {number}
*/
this.instanceRendererID = `instanceRenderer-${_instanceRendererID++}-ID`;
/**
* Maps display-object property names holding instanced attribute data to their attribute
* names.
*
* @protected
* @member {Object<string, string>}
*/
this._instanceBuilder = options.instanceBuilder;
/**
* The reference geometry specifying the "attribute style".
*
* @protected
* @member {PIXI.Geometry}
*/
this._geometry = options.geometry;
/**
* The shader used to draw all instances.
*
* @member {PIXI.Shader}
*/
this._shader = options.shader;
/**
* The WebGL state required for using the shader.
*
* @default PIXI.State.for2d()
* @member {PIXI.State}
*/
this._state = options.state || State.for2d();
/**
* Object mapping (instanced) attribute IDs to their sizes in bytes.
*
* @protected
* @readonly
* @member {Object<string, number>}
*/
this._instanceAttribSizes = {};
/**
* Object mapping (instanced) attribute IDs to their data type views (i.e. `uint32View`,
* `float32View`, `uint8View`, etc. in `PIXI.ViewableBuffer`).
*
* @protected
* @readonly
* @member {Object<string, string>}
*/
this._instanceAttribViews = {};
/**
* The bytes used per instance/display-object.
*
* @protected
* @readonly
* @member {number}
*/
this._instanceSize = this._calculateInstanceSizesAndViews();
/**
* Buffered display-objects
*
* @protected
* @member {PIXI.DisplayObject[]}
*/
this._objectBuffer = [];
/**
* The number of display-objects buffered. This is different from the buffer's capacity
* {@code this._objectBuffer.length}.
*
* @protected
* @member {number}
*/
this._objectCount = 0;
// NOTE: _initInstanceBuffer() also clones this._geometry and replaces it.
this._initInstanceBuffer();
}
/**
* @override
*/
start()
{
this._objectCount = 0;
}
/**
* @override
* @param {PIXI.DisplayObject} displayObject
*/
render(displayObject: { [id: string]: string }): void
{
this._objectBuffer[this._objectCount] = displayObject;
++this._objectCount;
}
/**
* Flushes/draws all pending display-objects.
*
* @override
*/
flush(): void
{
const instanceBuilder = this._instanceBuilder;
const instanceSize = this._instanceSize;
const instanceBuffer = this._getBuffer(this._objectCount * this._instanceSize);
// TODO: Optimize this by compiling a function that doesn't loop through each attribute
// by rolling the loop
for (let i = 0; i < this._objectCount; i++)
{
let rsize = 0;
const object = this._objectBuffer[i];
for (const attribID in this._instanceBuilder)
{
const attribute = this._geometry.attributes[attribID];
if (!attribute.instance)
{
continue;
}
const attribSize = attribute.size;
const view = instanceBuffer[this._instanceAttribViews[attribID]];
const size = this._instanceAttribSizes[attribID];
const index = (i * instanceSize + rsize) / size;
const prop = instanceBuilder[attribID];
if (attribSize === 1)
{
view[index] = object[prop];
}
else
{
for (let j = 0; j < attribSize; j++)
{
view[index + j] = object[prop][j];
}
}
rsize += size;
}
}
this._instanceBuffer.update(instanceBuffer.rawBinaryData);
const renderer = this.renderer;
renderer.shader.bind(this._shader);
renderer.geometry.bind(this._geometry);
renderer.state.set(this._state);
renderer.geometry.draw(DRAW_MODES.TRIANGLES, undefined, undefined, this._objectCount);
this._objectCount = 0;
}
/**
* Returns a (cached) buffer that can hold {@code size} bytes.
*
* @param {number} size - required capacity in bytes
* @return {ViewableBuffer} - buffer than can hold atleast `size` floats
* @private
*/
protected _getBuffer(size: number): ViewableBuffer
{
const roundedP2 = nextPow2(Math.ceil(size));
const roundedSizeIndex = log2(roundedP2);
const roundedSize = roundedP2;
if (this._aBuffers.length <= roundedSizeIndex)
{
this._aBuffers.length = roundedSizeIndex + 1;
}
let buffer = this._aBuffers[roundedSize];
if (!buffer)
{
this._aBuffers[roundedSize] = buffer = new ViewableBuffer(roundedSize);
}
return buffer;
}
/**
* Returns the no. of bytes used for each instance.
*
* @private
* @returns {number}
*/
private _calculateInstanceSizesAndViews(): number
{
let totalSize = 0;
for (const attribID in this._geometry.attributes)
{
const attribute = this._geometry.attributes[attribID];
if (!attribute.instance)
{
continue;
}
let typeSize = 0;
let view;
switch (attribute.type)
{
case TYPES.UNSIGNED_BYTE:
typeSize = 1;
view = 'uint8View';
break;
case TYPES.UNSIGNED_SHORT:
case TYPES.UNSIGNED_SHORT_5_6_5:
case TYPES.UNSIGNED_SHORT_4_4_4_4:
case TYPES.UNSIGNED_SHORT_5_5_5_1:
case TYPES.HALF_FLOAT:
typeSize = 2;
view = 'uint16View';// TODO: HALF_FLOAT will not work
break;
case TYPES.FLOAT:
typeSize = 4;
view = 'float32View';
break;
}
const byteSize = attribute.size * typeSize;
this._instanceAttribViews[attribID] = view;
this._instanceAttribSizes[attribID] = byteSize;
totalSize += byteSize;
}
return totalSize;
}
/**
* Replaces {@code this._geometry} with a new geometry such that each instanced attribute
* points to the same buffer. Uninstanced attributes refer to their original buffers.
*/
private _initInstanceBuffer(): void
{
/**
* The instance buffer holds all instanced attributes in an interleaved fashion.
*
* @member {PIXI.Buffer}
*/
this._instanceBuffer = new Buffer();
const clonedGeometry = new Geometry();
for (const attribID in this._geometry.attributes)
{
const attribute = this._geometry.attributes[attribID];
const instance = attribute.instance;
console.log(attribID);
console.log(this._geometry.buffers[attribute.buffer]);
clonedGeometry.addAttribute(
attribID,
instance ? this._instanceBuffer : this._geometry.buffers[attribute.buffer],
attribute.size,
attribute.normalized,
attribute.type,
instance ? attribute.start : undefined,
instance ? attribute.stride : undefined,
attribute.instance,
);
}
this._geometry = clonedGeometry;
}
}
export interface IInstancedRendererOptions
{
instanceBuilder: Record<string, string>;
geometry: Geometry;
shader: Shader;
state?: State;
}
/**
* This options define how display-objects are rendered by the instanced renderer.
*
* NOTE:
*
* + Make sure your instanceBuilder is in the order you want attributes to be packed
* in the same buffer. Also, make sure that floats are aligned at 4-byte boundaries and
* shorts are aligned at 2-byte boundaries.
*
* + PixiJS Bug: Make sure the first attribute is **not** instanced.
*
* @memberof PIXI
* @interface IInstancedRendererOptions
* @property {Object<string, string>} instanceBuilder - an object mapping display-object
* properties holding "instance attributes" from their attribute name.
* @property {Geometry} geometry - the geometry style used to render the display-objects
* @property {Shader} shader - the shader used to render display-objects
* @property {State}[state] - the WebGL state used to run the shader
* @example
* {
* instanceBuilder: {
* aVertexPosition: '_vertexData'
* },
* geometry: new PIXI.Geometry()
* .addAttribute('aVertexPosition', null, 2, false, TYPES.FLOAT, 0, 0, false)
* .addAttribute('aWorldTransform', null, 2, false, TYPES.FLOAT, 0, 0, true)
* shader: new PIXI.Shader(<vertexShaderSrc>, <fragmentShaderSrc>, <uniformData>),
* state: PIXI.State.for2d() // that's the default
* }
*/