- 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
import { BatchGeometryFactory, BatchGeometry, BufferPool } from 'pixi-batch-renderer';
import { BufferInvalidationQueue } from './utils/BufferInvalidationQueue';
import { DiffBuffer } from './DiffBuffer';
import { DiffGeometry } from './DiffGeometry';
import { ViewableBuffer } from '@pixi/core';
import type { BatchRenderer } from 'pixi-batch-renderer';
declare interface HackedUint32Array extends Uint32Array {
viewableBuffer: ViewableBuffer;
}
// ViewableBuffer.constructor(ArrayBuffer) is introduced in the Compressed Textures PR in pixi.js, will be released ion 5.4.0
// Cringe hack till then:
function hackViewableBuffer(buffer: Uint32Array): ViewableBuffer
{
const hackedBuffer = buffer as HackedUint32Array;
if (hackedBuffer.viewableBuffer)
{
return hackedBuffer.viewableBuffer;
}
const viewableBuffer = new ViewableBuffer(0);
viewableBuffer.rawBinaryData = buffer.buffer;
viewableBuffer.uint32View = buffer;
viewableBuffer.float32View = new Float32Array(buffer.buffer);
hackedBuffer.viewableBuffer = viewableBuffer;
return viewableBuffer;
}
export class DiffGeometryFactory extends BatchGeometryFactory
{
// typings from BGF
_vertexSize: number;
protected _geometryCache: DiffGeometry[];
protected _geometryPipeline: DiffGeometry[];
attribPool: BufferPool<Uint32Array>;
indexPool: BufferPool<Uint16Array>;
constructor(renderer: BatchRenderer)
{
super(renderer);
/**
* Cache of the geometries drawn in the last frame.
*/
this._geometryCache = [];
/**
* The geometries already drawn this frame.
*/
this._geometryPipeline = [];
this.attribPool = new BufferPool(Uint32Array);
this.indexPool = new BufferPool(Uint16Array);
renderer.renderer.on('postrender', this.postrender, this);
}
build(): DiffGeometry
{
const cache: DiffGeometry = this._geometryCache.shift();
const geom = cache || new DiffGeometry(
this._attribRedirects,
true,
this._texIDAttrib,
this._texturesPerObject,
this._inBatchIDAttrib,
this._uniformIDAttrib,
this._masterIDAttrib,
);
if (!cache)
{
geom.attribBuffer.update(this._targetCompositeAttributeBuffer.uint32View);
geom.indexBuffer.update(this._targetCompositeIndexBuffer);
}
else
{
this.updateCache(cache, 'attribBuffer', this._targetCompositeAttributeBuffer.uint32View);
this.updateCache(cache, 'indexBuffer', this._targetCompositeIndexBuffer);
}
return geom;
}
/**
* {@code DiffGeometryFactory}
*
* @override
* @param geom
*/
release(geom: DiffGeometry): void
{
this._geometryPipeline.push(geom);
}
protected updateCache(
geometry: BatchGeometry,
type: 'attribBuffer' | 'indexBuffer',
data: Uint32Array | Uint16Array,
): void
{
const cachedBuffer = geometry[type].data as ArrayLike<number>;
const buffer = geometry[type] as DiffBuffer;
const bufferPool = this[type === 'attribBuffer' ? 'attribPool' : 'indexPool'];
const bufferLength = type === 'attribBuffer' ? this._aIndex / 4 : this._iIndex;
if (cachedBuffer.length < data.length)
{
buffer.update(data);
bufferPool.releaseBuffer(cachedBuffer as any);
return;
}
this.diffCache(buffer.data as ArrayLike<number>, data, bufferLength, buffer.updateQueue);
bufferPool.releaseBuffer(data as any);
}
/**
* Calculates the regions different in the cached & updated versions of a buffer. The cache is expected to not be smaller
* than the updated data.
*
* @param cache
* @param data
*/
protected diffCache(
cache: ArrayLike<number>,
data: ArrayLike<number>,
length: number,
diffQueue: BufferInvalidationQueue): void
{
diffQueue.clear();
// Flags whether the loop is inside an invalidated interval
let inDiff = false;
// Stores the offset of the current invalidated interval, if inDiff
let diffOffset = 0;
// Fill diffQueue
for (let i = 0; i < length; i++)
{
const cachedElement = cache[i];
const dataElement = data[i];
if (cachedElement !== dataElement)
{
(cache as number[])[i] = dataElement;
}
if (cachedElement !== dataElement && !inDiff)
{
inDiff = true;
diffOffset = i;
}
else if (cachedElement === dataElement && inDiff)
{
inDiff = false;
diffQueue.append(diffOffset, i - diffOffset);
}
}
if (inDiff)
{
diffQueue.append(diffOffset, length - diffOffset);
}
}
/**
* Release the geometry cache
*/
protected releaseCache(): void
{
this._geometryPool.push(...this._geometryCache);
// Swap geometryCache & geometryPipeline arrays.
const lastCache = this._geometryCache;
this._geometryCache = this._geometryPipeline;
this._geometryPipeline = lastCache;
this._geometryPipeline.length = 0;
}
postrender(): void
{
this.releaseCache();
}
getAttributeBuffer(size: number): ViewableBuffer
{
const buffer = this.attribPool.allocateBuffer(size * this._vertexSize);
const vbuf = hackViewableBuffer(buffer);
return vbuf;
}
getIndexBuffer(size: number): Uint16Array
{
return this.indexPool.allocateBuffer(size);
}
}