- 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
import { mapWebGLBlendModesToPixi } from './utils/mapWebGLBlendModesToPixi';
import { State } from './State';
import { BLEND_MODES } from '@pixi/constants';
import type { ISystem } from '../ISystem';
import type { IRenderingContext } from '../IRenderingContext';
const BLEND = 0;
const OFFSET = 1;
const CULLING = 2;
const DEPTH_TEST = 3;
const WINDING = 4;
const DEPTH_MASK = 5;
/**
* System plugin to the renderer to manage WebGL state machines.
* @memberof PIXI
*/
export class StateSystem implements ISystem
{
/**
* State ID
* @readonly
*/
public stateId: number;
/**
* Polygon offset
* @readonly
*/
public polygonOffset: number;
/**
* Blend mode
* @default PIXI.BLEND_MODES.NONE
* @readonly
*/
public blendMode: BLEND_MODES;
/** Whether current blend equation is different */
protected _blendEq: boolean;
/**
* GL context
* @member {WebGLRenderingContext}
* @readonly
*/
protected gl: IRenderingContext;
protected blendModes: number[][];
/**
* Collection of calls
* @member {Function[]}
*/
protected readonly map: Array<(value: boolean) => void>;
/**
* Collection of check calls
* @member {Function[]}
*/
protected readonly checks: Array<(system: this, state: State) => void>;
/**
* Default WebGL State
* @readonly
*/
protected defaultState: State;
constructor()
{
this.gl = null;
this.stateId = 0;
this.polygonOffset = 0;
this.blendMode = BLEND_MODES.NONE;
this._blendEq = false;
// map functions for when we set state..
this.map = [];
this.map[BLEND] = this.setBlend;
this.map[OFFSET] = this.setOffset;
this.map[CULLING] = this.setCullFace;
this.map[DEPTH_TEST] = this.setDepthTest;
this.map[WINDING] = this.setFrontFace;
this.map[DEPTH_MASK] = this.setDepthMask;
this.checks = [];
this.defaultState = new State();
this.defaultState.blend = true;
}
contextChange(gl: IRenderingContext): void
{
this.gl = gl;
this.blendModes = mapWebGLBlendModesToPixi(gl);
this.set(this.defaultState);
this.reset();
}
/**
* Sets the current state
* @param {*} state - The state to set.
*/
set(state: State): void
{
state = state || this.defaultState;
// TODO maybe to an object check? ( this.state === state )?
if (this.stateId !== state.data)
{
let diff = this.stateId ^ state.data;
let i = 0;
// order from least to most common
while (diff)
{
if (diff & 1)
{
// state change!
this.map[i].call(this, !!(state.data & (1 << i)));
}
diff = diff >> 1;
i++;
}
this.stateId = state.data;
}
// based on the above settings we check for specific modes..
// for example if blend is active we check and set the blend modes
// or of polygon offset is active we check the poly depth.
for (let i = 0; i < this.checks.length; i++)
{
this.checks[i](this, state);
}
}
/**
* Sets the state, when previous state is unknown.
* @param {*} state - The state to set
*/
forceState(state: State): void
{
state = state || this.defaultState;
for (let i = 0; i < this.map.length; i++)
{
this.map[i].call(this, !!(state.data & (1 << i)));
}
for (let i = 0; i < this.checks.length; i++)
{
this.checks[i](this, state);
}
this.stateId = state.data;
}
/**
* Sets whether to enable or disable blending.
* @param value - Turn on or off WebGl blending.
*/
setBlend(value: boolean): void
{
this.updateCheck(StateSystem.checkBlendMode, value);
this.gl[value ? 'enable' : 'disable'](this.gl.BLEND);
}
/**
* Sets whether to enable or disable polygon offset fill.
* @param value - Turn on or off webgl polygon offset testing.
*/
setOffset(value: boolean): void
{
this.updateCheck(StateSystem.checkPolygonOffset, value);
this.gl[value ? 'enable' : 'disable'](this.gl.POLYGON_OFFSET_FILL);
}
/**
* Sets whether to enable or disable depth test.
* @param value - Turn on or off webgl depth testing.
*/
setDepthTest(value: boolean): void
{
this.gl[value ? 'enable' : 'disable'](this.gl.DEPTH_TEST);
}
/**
* Sets whether to enable or disable depth mask.
* @param value - Turn on or off webgl depth mask.
*/
setDepthMask(value: boolean): void
{
this.gl.depthMask(value);
}
/**
* Sets whether to enable or disable cull face.
* @param {boolean} value - Turn on or off webgl cull face.
*/
setCullFace(value: boolean): void
{
this.gl[value ? 'enable' : 'disable'](this.gl.CULL_FACE);
}
/**
* Sets the gl front face.
* @param {boolean} value - true is clockwise and false is counter-clockwise
*/
setFrontFace(value: boolean): void
{
this.gl.frontFace(this.gl[value ? 'CW' : 'CCW']);
}
/**
* Sets the blend mode.
* @param {number} value - The blend mode to set to.
*/
setBlendMode(value: number): void
{
if (value === this.blendMode)
{
return;
}
this.blendMode = value;
const mode = this.blendModes[value];
const gl = this.gl;
if (mode.length === 2)
{
gl.blendFunc(mode[0], mode[1]);
}
else
{
gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
}
if (mode.length === 6)
{
this._blendEq = true;
gl.blendEquationSeparate(mode[4], mode[5]);
}
else if (this._blendEq)
{
this._blendEq = false;
gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
}
}
/**
* Sets the polygon offset.
* @param {number} value - the polygon offset
* @param {number} scale - the polygon offset scale
*/
setPolygonOffset(value: number, scale: number): void
{
this.gl.polygonOffset(value, scale);
}
// used
/** Resets all the logic and disables the VAOs. */
reset(): void
{
this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false);
this.forceState(this.defaultState);
this._blendEq = true;
this.blendMode = -1;
this.setBlendMode(0);
}
/**
* Checks to see which updates should be checked based on which settings have been activated.
*
* For example, if blend is enabled then we should check the blend modes each time the state is changed
* or if polygon fill is activated then we need to check if the polygon offset changes.
* The idea is that we only check what we have too.
* @param func - the checking function to add or remove
* @param value - should the check function be added or removed.
*/
updateCheck(func: (system: this, state: State) => void, value: boolean): void
{
const index = this.checks.indexOf(func);
if (value && index === -1)
{
this.checks.push(func);
}
else if (!value && index !== -1)
{
this.checks.splice(index, 1);
}
}
/**
* A private little wrapper function that we call to check the blend mode.
* @param system - the System to perform the state check on
* @param state - the state that the blendMode will pulled from
*/
private static checkBlendMode(system: StateSystem, state: State): void
{
system.setBlendMode(state.blendMode);
}
/**
* A private little wrapper function that we call to check the polygon offset.
* @param system - the System to perform the state check on
* @param state - the state that the blendMode will pulled from
*/
private static checkPolygonOffset(system: StateSystem, state: State): void
{
system.setPolygonOffset(1, state.polygonOffset);
}
/**
* @ignore
*/
destroy(): void
{
this.gl = null;
}
}