123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- import Cartesian2 from "../Core/Cartesian2.js";
- import Cartesian3 from "../Core/Cartesian3.js";
- import Cartesian4 from "../Core/Cartesian4.js";
- import Color from "../Core/Color.js";
- import defined from "../Core/defined.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import Matrix2 from "../Core/Matrix2.js";
- import Matrix3 from "../Core/Matrix3.js";
- import Matrix4 from "../Core/Matrix4.js";
- import RuntimeError from "../Core/RuntimeError.js";
- /**
- * @private
- * @constructor
- */
- function createUniformArray(gl, activeUniform, uniformName, locations) {
- switch (activeUniform.type) {
- case gl.FLOAT:
- return new UniformArrayFloat(gl, activeUniform, uniformName, locations);
- case gl.FLOAT_VEC2:
- return new UniformArrayFloatVec2(
- gl,
- activeUniform,
- uniformName,
- locations
- );
- case gl.FLOAT_VEC3:
- return new UniformArrayFloatVec3(
- gl,
- activeUniform,
- uniformName,
- locations
- );
- case gl.FLOAT_VEC4:
- return new UniformArrayFloatVec4(
- gl,
- activeUniform,
- uniformName,
- locations
- );
- case gl.SAMPLER_2D:
- case gl.SAMPLER_CUBE:
- return new UniformArraySampler(gl, activeUniform, uniformName, locations);
- case gl.INT:
- case gl.BOOL:
- return new UniformArrayInt(gl, activeUniform, uniformName, locations);
- case gl.INT_VEC2:
- case gl.BOOL_VEC2:
- return new UniformArrayIntVec2(gl, activeUniform, uniformName, locations);
- case gl.INT_VEC3:
- case gl.BOOL_VEC3:
- return new UniformArrayIntVec3(gl, activeUniform, uniformName, locations);
- case gl.INT_VEC4:
- case gl.BOOL_VEC4:
- return new UniformArrayIntVec4(gl, activeUniform, uniformName, locations);
- case gl.FLOAT_MAT2:
- return new UniformArrayMat2(gl, activeUniform, uniformName, locations);
- case gl.FLOAT_MAT3:
- return new UniformArrayMat3(gl, activeUniform, uniformName, locations);
- case gl.FLOAT_MAT4:
- return new UniformArrayMat4(gl, activeUniform, uniformName, locations);
- default:
- throw new RuntimeError(
- `Unrecognized uniform type: ${activeUniform.type} for uniform "${uniformName}".`
- );
- }
- }
- /**
- * @private
- * @constructor
- */
- function UniformArrayFloat(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayFloat.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (v !== arraybuffer[i]) {
- arraybuffer[i] = v;
- changed = true;
- }
- }
- if (changed) {
- this._gl.uniform1fv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayFloatVec2(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length * 2);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayFloatVec2.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Cartesian2.equalsArray(v, arraybuffer, j)) {
- Cartesian2.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 2;
- }
- if (changed) {
- this._gl.uniform2fv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayFloatVec3(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length * 3);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayFloatVec3.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (defined(v.red)) {
- if (
- v.red !== arraybuffer[j] ||
- v.green !== arraybuffer[j + 1] ||
- v.blue !== arraybuffer[j + 2]
- ) {
- arraybuffer[j] = v.red;
- arraybuffer[j + 1] = v.green;
- arraybuffer[j + 2] = v.blue;
- changed = true;
- }
- } else if (defined(v.x)) {
- if (!Cartesian3.equalsArray(v, arraybuffer, j)) {
- Cartesian3.pack(v, arraybuffer, j);
- changed = true;
- }
- } else {
- //>>includeStart('debug', pragmas.debug);
- throw new DeveloperError("Invalid vec3 value.");
- //>>includeEnd('debug');
- }
- j += 3;
- }
- if (changed) {
- this._gl.uniform3fv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayFloatVec4(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length * 4);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayFloatVec4.prototype.set = function () {
- // PERFORMANCE_IDEA: if it is a common case that only a few elements
- // in a uniform array change, we could use heuristics to determine
- // when it is better to call uniform4f for each element that changed
- // vs. call uniform4fv once to set the entire array. This applies
- // to all uniform array types, not just vec4. We might not care
- // once we have uniform buffers since that will be the fast path.
- // PERFORMANCE_IDEA: Micro-optimization (I bet it works though):
- // As soon as changed is true, break into a separate loop that
- // does the copy without the equals check.
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (defined(v.red)) {
- if (!Color.equalsArray(v, arraybuffer, j)) {
- Color.pack(v, arraybuffer, j);
- changed = true;
- }
- } else if (defined(v.x)) {
- if (!Cartesian4.equalsArray(v, arraybuffer, j)) {
- Cartesian4.pack(v, arraybuffer, j);
- changed = true;
- }
- } else {
- //>>includeStart('debug', pragmas.debug);
- throw new DeveloperError("Invalid vec4 value.");
- //>>includeEnd('debug');
- }
- j += 4;
- }
- if (changed) {
- this._gl.uniform4fv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArraySampler(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length);
- this._gl = gl;
- this._locations = locations;
- this.textureUnitIndex = undefined;
- }
- UniformArraySampler.prototype.set = function () {
- const gl = this._gl;
- const textureUnitIndex = gl.TEXTURE0 + this.textureUnitIndex;
- const value = this.value;
- const length = value.length;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- gl.activeTexture(textureUnitIndex + i);
- gl.bindTexture(v._target, v._texture);
- }
- };
- UniformArraySampler.prototype._setSampler = function (textureUnitIndex) {
- this.textureUnitIndex = textureUnitIndex;
- const locations = this._locations;
- const length = locations.length;
- for (let i = 0; i < length; ++i) {
- const index = textureUnitIndex + i;
- this._gl.uniform1i(locations[i], index);
- }
- return textureUnitIndex + length;
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayInt(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Int32Array(length);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayInt.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (v !== arraybuffer[i]) {
- arraybuffer[i] = v;
- changed = true;
- }
- }
- if (changed) {
- this._gl.uniform1iv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayIntVec2(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Int32Array(length * 2);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayIntVec2.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Cartesian2.equalsArray(v, arraybuffer, j)) {
- Cartesian2.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 2;
- }
- if (changed) {
- this._gl.uniform2iv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayIntVec3(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Int32Array(length * 3);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayIntVec3.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Cartesian3.equalsArray(v, arraybuffer, j)) {
- Cartesian3.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 3;
- }
- if (changed) {
- this._gl.uniform3iv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayIntVec4(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Int32Array(length * 4);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayIntVec4.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Cartesian4.equalsArray(v, arraybuffer, j)) {
- Cartesian4.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 4;
- }
- if (changed) {
- this._gl.uniform4iv(this._location, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayMat2(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length * 4);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayMat2.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Matrix2.equalsArray(v, arraybuffer, j)) {
- Matrix2.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 4;
- }
- if (changed) {
- this._gl.uniformMatrix2fv(this._location, false, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayMat3(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length * 9);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayMat3.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Matrix3.equalsArray(v, arraybuffer, j)) {
- Matrix3.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 9;
- }
- if (changed) {
- this._gl.uniformMatrix3fv(this._location, false, arraybuffer);
- }
- };
- ///////////////////////////////////////////////////////////////////////////
- /**
- * @private
- * @constructor
- */
- function UniformArrayMat4(gl, activeUniform, uniformName, locations) {
- const length = locations.length;
- /**
- * @type {String}
- * @readonly
- */
- this.name = uniformName;
- this.value = new Array(length);
- this._value = new Float32Array(length * 16);
- this._gl = gl;
- this._location = locations[0];
- }
- UniformArrayMat4.prototype.set = function () {
- const value = this.value;
- const length = value.length;
- const arraybuffer = this._value;
- let changed = false;
- let j = 0;
- for (let i = 0; i < length; ++i) {
- const v = value[i];
- if (!Matrix4.equalsArray(v, arraybuffer, j)) {
- Matrix4.pack(v, arraybuffer, j);
- changed = true;
- }
- j += 16;
- }
- if (changed) {
- this._gl.uniformMatrix4fv(this._location, false, arraybuffer);
- }
- };
- export default createUniformArray;
|