- 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
import { AggregateUniformsBatchFactory } from './AggregateUniformsBatchFactory';
import { Shader } from '@pixi/core';
import type { BatchRenderer } from './BatchRenderer';
// This file might need a cleanup :)
// JavaScript is stupid enough not to have a replaceAll in String. This is a temporary
// solution and we should depend on an actually polyfill.
function _replaceAll(target: string, search: string, replacement: string): string
{
return target.replace(new RegExp(search, 'g'), replacement);
}
function injectTexturesPerBatch(batchRenderer: BatchRenderer): string
{
return `${batchRenderer.MAX_TEXTURES}`;
}
const INJECTORS = {
uniformsPerBatch(renderer: BatchRenderer): string
{
return `${(renderer._batchFactory as AggregateUniformsBatchFactory).MAX_UNIFORMS}`;
},
};
/**
* Exposes an easy-to-use API for generating shader-functions to use in
* the batch renderer!
*
* You are required to provide an injector map, which maps macros to functions
* that return a string value for those macros given a renderer. By default, only one
* injector is used - the textures per batch `%texturesPerBatch%` macro. This is replaced by
* the number of textures passed to the `uSamplers` textures uniform.
*
* **Built-in Injectors**:
*
* * `%texturesPerBatch%`: replaced by the max. textures allowed by WebGL context
*
* * `%uniformsPerBatch%`: replaced by the (aggregate-uniforms) batch factory's `MAX_UNIFORMS` property.
*/
export class BatchShaderFactory
{
protected _vertexShaderTemplate: string;
protected _fragmentShaderTemplate: string;
protected _uniforms: any;
protected _templateInjectors: any;
protected disableVertexShaderTemplate: boolean;
protected _cache: any;
protected _cState: any;
/**
* WARNING: Do not pass `uSamplers` in your uniforms. They
* will be added to your shader instance directly.
*
* @param {string} vertexShaderTemplate
* @param {string} fragmentShaderTemplate
* @param {UniformGroup | Map<string, object>} uniforms
* @param {Object.<String, InjectorFunction>} [templateInjectors]
* @param {boolean} [disableVertexShaderTemplate=true] - turn off (true)
* if you aren't using macros in the vertex shader
*/
constructor(
vertexShaderTemplate: string,
fragmentShaderTemplate: string,
uniforms = {},
templateInjectors: any = {},
disableVertexShaderTemplate = true,
)
{
if (!templateInjectors['%texturesPerBatch%'])
{
templateInjectors['%texturesPerBatch%'] = injectTexturesPerBatch;
}
if (!templateInjectors['%uniformsPerBatch%'])
{
templateInjectors['%uniformsPerBatch%'] = INJECTORS.uniformsPerBatch;
}
this._vertexShaderTemplate = vertexShaderTemplate;
this._fragmentShaderTemplate = fragmentShaderTemplate;
this._uniforms = uniforms;
this._templateInjectors = templateInjectors;
/**
* Disable vertex shader templates to speed up shader
* generation.
*
* @member {Boolean}
*/
this.disableVertexShaderTemplate = disableVertexShaderTemplate;
/**
* Maps the stringifed state of the batch renderer to the
* generated shader.
*
* @private
* @member {Object.<String, PIXI.Shader>}
*/
this._cache = {};
/**
* Unstringifed current state of the batch renderer.
*
* @private
* @member {Object.<String, String>}
* @see {ShaderGenerator#_generateInjectorBasedState}
*/
this._cState = null;
}
/**
* This essentially returns a function for generating the shader for a batch
* renderer.
*
* @return shader function that can be given to the batch renderer
*/
derive(): (brend: BatchRenderer) => Shader
{
return (batchRenderer: BatchRenderer): Shader =>
{
const stringState = this._generateInjectorBasedState(batchRenderer);
const cachedShader = this._cache[stringState];
if (cachedShader)
{
return cachedShader;
}
return this._generateShader(stringState, batchRenderer);
};
}
protected _generateInjectorBasedState(batchRenderer: BatchRenderer): string
{
let state = '';
const cState = this._cState = {};
for (const injectorMacro in this._templateInjectors)
{
const val = this._templateInjectors[injectorMacro](batchRenderer);
state += val;
cState[injectorMacro] = val;
}
return state;
}
protected _generateShader(stringState: string, renderer: BatchRenderer): Shader
{
let vertexShaderTemplate = this._vertexShaderTemplate.slice(0);
let fragmentShaderTemplate = this._fragmentShaderTemplate.slice(0);
for (const injectorTemplate in this._cState)
{
if (!this.disableVertexShaderTemplate)
{
vertexShaderTemplate = _replaceAll(vertexShaderTemplate,
injectorTemplate, this._cState[injectorTemplate]);
}
fragmentShaderTemplate = _replaceAll(fragmentShaderTemplate,
injectorTemplate, this._cState[injectorTemplate]);
}
const shader = Shader.from(vertexShaderTemplate,
fragmentShaderTemplate, this._uniforms);
const textures = new Array(renderer.MAX_TEXTURES);
for (let i = 0; i < textures.length; i++)
{
textures[i] = i;
}
shader.uniforms.uSamplers = textures;
this._cache[stringState] = shader;
return shader;
}
}
export default BatchShaderFactory;