- 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
import type { UniformGroup, IUniformData, Renderer, IRenderingContext } from '@pixi/core';
// cv = CachedValue
// v = value
// ud = uniformData
// uv = uniformValue
// l = location
/* eslint-disable max-len, @typescript-eslint/explicit-module-boundary-types */
const GLSL_TO_SINGLE_SETTERS = {
float(gl: IRenderingContext, location: WebGLUniformLocation, cv: any, v: number): void
{
if (cv !== v)
{
cv.v = v;
gl.uniform1f(location, v);
}
},
vec2(gl: IRenderingContext, location: WebGLUniformLocation, cv: number[], v: number[]): void
{
if (cv[0] !== v[0] || cv[1] !== v[1])
{
cv[0] = v[0];
cv[1] = v[1];
gl.uniform2f(location, v[0], v[1]);
}
},
vec3(gl: IRenderingContext, location: WebGLUniformLocation, cv: number[], v: number[]): void
{
if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2])
{
cv[0] = v[0];
cv[1] = v[1];
cv[2] = v[2];
gl.uniform3f(location, v[0], v[1], v[2]);
}
},
int(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number): void { gl.uniform1i(location, value); },
ivec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform2i(location, value[0], value[1]); },
ivec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform3i(location, value[0], value[1], value[2]); },
ivec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform4i(location, value[0], value[1], value[2], value[3]); },
uint(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number): void { gl.uniform1ui(location, value); },
uvec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform2ui(location, value[0], value[1]); },
uvec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform3ui(location, value[0], value[1], value[2]); },
uvec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform4ui(location, value[0], value[1], value[2], value[3]); },
bool(gl: IRenderingContext, location: WebGLUniformLocation, cv: any, v: boolean): void
{
if (cv !== v)
{
cv.v = v;
gl.uniform1i(location, Number(v));
}
},
bvec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform2i(location, value[0], value[1]); },
bvec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform3i(location, value[0], value[1], value[2]); },
bvec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniform4i(location, value[0], value[1], value[2], value[3]); },
mat2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniformMatrix2fv(location, false, value); },
mat3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniformMatrix3fv(location, false, value); },
mat4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number[]): void { gl.uniformMatrix4fv(location, false, value); },
sampler2D(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number): void { gl.uniform1i(location, value); },
samplerCube(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number): void { gl.uniform1i(location, value); },
sampler2DArray(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: number): void { gl.uniform1i(location, value); },
};
const GLSL_TO_ARRAY_SETTERS = {
float(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Float32List): void { gl.uniform1fv(location, value); },
vec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Float32List): void { gl.uniform2fv(location, value); },
vec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Float32List): void { gl.uniform3fv(location, value); },
vec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Float32List): void { gl.uniform4fv(location, value); },
int(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform1iv(location, value); },
ivec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform2iv(location, value); },
ivec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform3iv(location, value); },
ivec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform4iv(location, value); },
uint(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Uint32List): void { gl.uniform1uiv(location, value); },
uvec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Uint32List): void { gl.uniform2uiv(location, value); },
uvec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Uint32List): void { gl.uniform3uiv(location, value); },
uvec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Uint32List): void { gl.uniform4uiv(location, value); },
bool(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform1iv(location, value); },
bvec2(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform2iv(location, value); },
bvec3(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform3iv(location, value); },
bvec4(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform4iv(location, value); },
sampler2D(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform1iv(location, value); },
samplerCube(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform1iv(location, value); },
sampler2DArray(gl: IRenderingContext, location: WebGLUniformLocation, _cv: any, value: Int32List): void { gl.uniform1iv(location, value); },
};
/* eslint-disable max-len */
export function syncUniforms(group: UniformGroup, uniformData: {[x: string]: IUniformData}, ud: any, uv: any, renderer: Renderer): void
{
let textureCount = 0;
let v = null;
let cv = null;
const gl = renderer.gl;
for (const i in group.uniforms)
{
const data = uniformData[i];
const uvi = uv[i];
const udi = ud[i];
const gu = group.uniforms[i];
if (!data)
{
if (gu.group)
{
renderer.shader.syncUniformGroup(uvi);
}
continue;
}
if (data.type === 'float' && data.size === 1)
{
if (uvi !== udi.value)
{
udi.value = uvi;
gl.uniform1f(udi.location, uvi);
}
}
/* eslint-disable max-len */
else if ((data.type === 'sampler2D' || data.type === 'samplerCube' || data.type === 'sampler2DArray') && data.size === 1 && !data.isArray)
/* eslint-disable max-len */
{
renderer.texture.bind(uvi, textureCount);
if (udi.value !== textureCount)
{
udi.value = textureCount;
gl.uniform1i(udi.location, textureCount);
}
textureCount++;
}
else if (data.type === 'mat3' && data.size === 1)
{
if (gu.a !== undefined)
{
gl.uniformMatrix3fv(udi.location, false, uvi.toArray(true));
}
else
{
gl.uniformMatrix3fv(udi.location, false, uvi);
}
}
else if (data.type === 'vec2' && data.size === 1)
{
if (gu.x !== undefined)
{
cv = udi.value;
v = uvi;
if (cv[0] !== v.x || cv[1] !== v.y)
{
cv[0] = v.x;
cv[1] = v.y;
gl.uniform2f(udi.location, v.x, v.y);
}
}
else
{
cv = udi.value;
v = uvi;
if (cv[0] !== v[0] || cv[1] !== v[1])
{
cv[0] = v[0];
cv[1] = v[1];
gl.uniform2f(udi.location, v[0], v[1]);
}
}
}
else if (data.type === 'vec4' && data.size === 1)
{
if (gu.width !== undefined)
{
cv = udi.value;
v = uvi;
if (cv[0] !== v.x || cv[1] !== v.y || cv[2] !== v.width || cv[3] !== v.height)
{
cv[0] = v.x;
cv[1] = v.y;
cv[2] = v.width;
cv[3] = v.height;
gl.uniform4f(udi.location, v.x, v.y, v.width, v.height);
}
}
else
{
cv = udi.value;
v = uvi;
if (cv[0] !== v[0] || cv[1] !== v[1] || cv[2] !== v[2] || cv[3] !== v[3])
{
cv[0] = v[0];
cv[1] = v[1];
cv[2] = v[2];
cv[3] = v[3];
gl.uniform4f(udi.location, v[0], v[1], v[2], v[3]);
}
}
}
else
{
const funcArray = (data.size === 1) ? GLSL_TO_SINGLE_SETTERS : GLSL_TO_ARRAY_SETTERS;
(funcArray as any)[data.type].call(null, gl, udi.location, udi.value, uvi);
}
}
}