Spaces:
Running
Running
File size: 17,986 Bytes
2b7aae2 | 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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 | /**
* Uniforms of a program.
* Those form a tree structure with a special top-level container for the root,
* which you get by calling 'new WebGLUniforms( gl, program )'.
*
*
* Properties of inner nodes including the top-level container:
*
* .seq - array of nested uniforms
* .map - nested uniforms by name
*
*
* Methods of all nodes except the top-level container:
*
* .setValue( gl, value, [textures] )
*
* uploads a uniform value(s)
* the 'textures' parameter is needed for sampler uniforms
*
*
* Static methods of the top-level container (textures factorizations):
*
* .upload( gl, seq, values, textures )
*
* sets uniforms in 'seq' to 'values[id].value'
*
* .seqWithValue( seq, values ) : filteredSeq
*
* filters 'seq' entries with corresponding entry in values
*
*
* Methods of the top-level container (textures factorizations):
*
* .setValue( gl, name, value, textures )
*
* sets uniform with name 'name' to 'value'
*
* .setOptional( gl, obj, prop )
*
* like .set for an optional property of the object
*
*/
import { CubeTexture } from '../../textures/CubeTexture.js';
import { Texture } from '../../textures/Texture.js';
import { DataTexture2DArray } from '../../textures/DataTexture2DArray.js';
import { DataTexture3D } from '../../textures/DataTexture3D.js';
const emptyTexture = new Texture();
const emptyTexture2dArray = new DataTexture2DArray();
const emptyTexture3d = new DataTexture3D();
const emptyCubeTexture = new CubeTexture();
// --- Utilities ---
// Array Caches (provide typed arrays for temporary by size)
const arrayCacheF32 = [];
const arrayCacheI32 = [];
// Float32Array caches used for uploading Matrix uniforms
const mat4array = new Float32Array(16);
const mat3array = new Float32Array(9);
const mat2array = new Float32Array(4);
// Flattening for arrays of vectors and matrices
function flatten(array, nBlocks, blockSize) {
const firstElem = array[0];
if (firstElem <= 0 || firstElem > 0) return array;
// unoptimized: ! isNaN( firstElem )
// see http://jacksondunstan.com/articles/983
const n = nBlocks * blockSize;
let r = arrayCacheF32[n];
if (r === undefined) {
r = new Float32Array(n);
arrayCacheF32[n] = r;
}
if (nBlocks !== 0) {
firstElem.toArray(r, 0);
for (let i = 1, offset = 0; i !== nBlocks; ++i) {
offset += blockSize;
array[i].toArray(r, offset);
}
}
return r;
}
function arraysEqual(a, b) {
if (a.length !== b.length) return false;
for (let i = 0, l = a.length; i < l; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
function copyArray(a, b) {
for (let i = 0, l = b.length; i < l; i++) {
a[i] = b[i];
}
}
// Texture unit allocation
function allocTexUnits(textures, n) {
let r = arrayCacheI32[n];
if (r === undefined) {
r = new Int32Array(n);
arrayCacheI32[n] = r;
}
for (let i = 0; i !== n; ++i) {
r[i] = textures.allocateTextureUnit();
}
return r;
}
// --- Setters ---
// Note: Defining these methods externally, because they come in a bunch
// and this way their names minify.
// Single scalar
function setValueV1f(gl, v) {
const cache = this.cache;
if (cache[0] === v) return;
gl.uniform1f(this.addr, v);
cache[0] = v;
}
// Single float vector (from flat array or THREE.VectorN)
function setValueV2f(gl, v) {
const cache = this.cache;
if (v.x !== undefined) {
if (cache[0] !== v.x || cache[1] !== v.y) {
gl.uniform2f(this.addr, v.x, v.y);
cache[0] = v.x;
cache[1] = v.y;
}
} else {
if (arraysEqual(cache, v)) return;
gl.uniform2fv(this.addr, v);
copyArray(cache, v);
}
}
function setValueV3f(gl, v) {
const cache = this.cache;
if (v.x !== undefined) {
if (cache[0] !== v.x || cache[1] !== v.y || cache[2] !== v.z) {
gl.uniform3f(this.addr, v.x, v.y, v.z);
cache[0] = v.x;
cache[1] = v.y;
cache[2] = v.z;
}
} else if (v.r !== undefined) {
if (cache[0] !== v.r || cache[1] !== v.g || cache[2] !== v.b) {
gl.uniform3f(this.addr, v.r, v.g, v.b);
cache[0] = v.r;
cache[1] = v.g;
cache[2] = v.b;
}
} else {
if (arraysEqual(cache, v)) return;
gl.uniform3fv(this.addr, v);
copyArray(cache, v);
}
}
function setValueV4f(gl, v) {
const cache = this.cache;
if (v.x !== undefined) {
if (cache[0] !== v.x || cache[1] !== v.y || cache[2] !== v.z || cache[3] !== v.w) {
gl.uniform4f(this.addr, v.x, v.y, v.z, v.w);
cache[0] = v.x;
cache[1] = v.y;
cache[2] = v.z;
cache[3] = v.w;
}
} else {
if (arraysEqual(cache, v)) return;
gl.uniform4fv(this.addr, v);
copyArray(cache, v);
}
}
// Single matrix (from flat array or THREE.MatrixN)
function setValueM2(gl, v) {
const cache = this.cache;
const elements = v.elements;
if (elements === undefined) {
if (arraysEqual(cache, v)) return;
gl.uniformMatrix2fv(this.addr, false, v);
copyArray(cache, v);
} else {
if (arraysEqual(cache, elements)) return;
mat2array.set(elements);
gl.uniformMatrix2fv(this.addr, false, mat2array);
copyArray(cache, elements);
}
}
function setValueM3(gl, v) {
const cache = this.cache;
const elements = v.elements;
if (elements === undefined) {
if (arraysEqual(cache, v)) return;
gl.uniformMatrix3fv(this.addr, false, v);
copyArray(cache, v);
} else {
if (arraysEqual(cache, elements)) return;
mat3array.set(elements);
gl.uniformMatrix3fv(this.addr, false, mat3array);
copyArray(cache, elements);
}
}
function setValueM4(gl, v) {
const cache = this.cache;
const elements = v.elements;
if (elements === undefined) {
if (arraysEqual(cache, v)) return;
gl.uniformMatrix4fv(this.addr, false, v);
copyArray(cache, v);
} else {
if (arraysEqual(cache, elements)) return;
mat4array.set(elements);
gl.uniformMatrix4fv(this.addr, false, mat4array);
copyArray(cache, elements);
}
}
// Single integer / boolean
function setValueV1i(gl, v) {
const cache = this.cache;
if (cache[0] === v) return;
gl.uniform1i(this.addr, v);
cache[0] = v;
}
// Single integer / boolean vector (from flat array)
function setValueV2i(gl, v) {
const cache = this.cache;
if (arraysEqual(cache, v)) return;
gl.uniform2iv(this.addr, v);
copyArray(cache, v);
}
function setValueV3i(gl, v) {
const cache = this.cache;
if (arraysEqual(cache, v)) return;
gl.uniform3iv(this.addr, v);
copyArray(cache, v);
}
function setValueV4i(gl, v) {
const cache = this.cache;
if (arraysEqual(cache, v)) return;
gl.uniform4iv(this.addr, v);
copyArray(cache, v);
}
// Single unsigned integer
function setValueV1ui(gl, v) {
const cache = this.cache;
if (cache[0] === v) return;
gl.uniform1ui(this.addr, v);
cache[0] = v;
}
// Single unsigned integer vector (from flat array)
function setValueV2ui(gl, v) {
const cache = this.cache;
if (arraysEqual(cache, v)) return;
gl.uniform2uiv(this.addr, v);
copyArray(cache, v);
}
function setValueV3ui(gl, v) {
const cache = this.cache;
if (arraysEqual(cache, v)) return;
gl.uniform3uiv(this.addr, v);
copyArray(cache, v);
}
function setValueV4ui(gl, v) {
const cache = this.cache;
if (arraysEqual(cache, v)) return;
gl.uniform4uiv(this.addr, v);
copyArray(cache, v);
}
// Single texture (2D / Cube)
function setValueT1(gl, v, textures) {
const cache = this.cache;
const unit = textures.allocateTextureUnit();
if (cache[0] !== unit) {
gl.uniform1i(this.addr, unit);
cache[0] = unit;
}
textures.safeSetTexture2D(v || emptyTexture, unit);
}
function setValueT3D1(gl, v, textures) {
const cache = this.cache;
const unit = textures.allocateTextureUnit();
if (cache[0] !== unit) {
gl.uniform1i(this.addr, unit);
cache[0] = unit;
}
textures.setTexture3D(v || emptyTexture3d, unit);
}
function setValueT6(gl, v, textures) {
const cache = this.cache;
const unit = textures.allocateTextureUnit();
if (cache[0] !== unit) {
gl.uniform1i(this.addr, unit);
cache[0] = unit;
}
textures.safeSetTextureCube(v || emptyCubeTexture, unit);
}
function setValueT2DArray1(gl, v, textures) {
const cache = this.cache;
const unit = textures.allocateTextureUnit();
if (cache[0] !== unit) {
gl.uniform1i(this.addr, unit);
cache[0] = unit;
}
textures.setTexture2DArray(v || emptyTexture2dArray, unit);
}
// Helper to pick the right setter for the singular case
function getSingularSetter(type) {
switch (type) {
case 0x1406:
return setValueV1f; // FLOAT
case 0x8b50:
return setValueV2f; // _VEC2
case 0x8b51:
return setValueV3f; // _VEC3
case 0x8b52:
return setValueV4f; // _VEC4
case 0x8b5a:
return setValueM2; // _MAT2
case 0x8b5b:
return setValueM3; // _MAT3
case 0x8b5c:
return setValueM4; // _MAT4
case 0x1404:
case 0x8b56:
return setValueV1i; // INT, BOOL
case 0x8b53:
case 0x8b57:
return setValueV2i; // _VEC2
case 0x8b54:
case 0x8b58:
return setValueV3i; // _VEC3
case 0x8b55:
case 0x8b59:
return setValueV4i; // _VEC4
case 0x1405:
return setValueV1ui; // UINT
case 0x8dc6:
return setValueV2ui; // _VEC2
case 0x8dc7:
return setValueV3ui; // _VEC3
case 0x8dc8:
return setValueV4ui; // _VEC4
case 0x8b5e: // SAMPLER_2D
case 0x8d66: // SAMPLER_EXTERNAL_OES
case 0x8dca: // INT_SAMPLER_2D
case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D
case 0x8b62: // SAMPLER_2D_SHADOW
return setValueT1;
case 0x8b5f: // SAMPLER_3D
case 0x8dcb: // INT_SAMPLER_3D
case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D
return setValueT3D1;
case 0x8b60: // SAMPLER_CUBE
case 0x8dcc: // INT_SAMPLER_CUBE
case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE
case 0x8dc5: // SAMPLER_CUBE_SHADOW
return setValueT6;
case 0x8dc1: // SAMPLER_2D_ARRAY
case 0x8dcf: // INT_SAMPLER_2D_ARRAY
case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY
case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW
return setValueT2DArray1;
}
}
// Array of scalars
function setValueV1fArray(gl, v) {
gl.uniform1fv(this.addr, v);
}
// Array of vectors (from flat array or array of THREE.VectorN)
function setValueV2fArray(gl, v) {
const data = flatten(v, this.size, 2);
gl.uniform2fv(this.addr, data);
}
function setValueV3fArray(gl, v) {
const data = flatten(v, this.size, 3);
gl.uniform3fv(this.addr, data);
}
function setValueV4fArray(gl, v) {
const data = flatten(v, this.size, 4);
gl.uniform4fv(this.addr, data);
}
// Array of matrices (from flat array or array of THREE.MatrixN)
function setValueM2Array(gl, v) {
const data = flatten(v, this.size, 4);
gl.uniformMatrix2fv(this.addr, false, data);
}
function setValueM3Array(gl, v) {
const data = flatten(v, this.size, 9);
gl.uniformMatrix3fv(this.addr, false, data);
}
function setValueM4Array(gl, v) {
const data = flatten(v, this.size, 16);
gl.uniformMatrix4fv(this.addr, false, data);
}
// Array of integer / boolean
function setValueV1iArray(gl, v) {
gl.uniform1iv(this.addr, v);
}
// Array of integer / boolean vectors (from flat array)
function setValueV2iArray(gl, v) {
gl.uniform2iv(this.addr, v);
}
function setValueV3iArray(gl, v) {
gl.uniform3iv(this.addr, v);
}
function setValueV4iArray(gl, v) {
gl.uniform4iv(this.addr, v);
}
// Array of unsigned integer
function setValueV1uiArray(gl, v) {
gl.uniform1uiv(this.addr, v);
}
// Array of unsigned integer vectors (from flat array)
function setValueV2uiArray(gl, v) {
gl.uniform2uiv(this.addr, v);
}
function setValueV3uiArray(gl, v) {
gl.uniform3uiv(this.addr, v);
}
function setValueV4uiArray(gl, v) {
gl.uniform4uiv(this.addr, v);
}
// Array of textures (2D / 3D / Cube / 2DArray)
function setValueT1Array(gl, v, textures) {
const n = v.length;
const units = allocTexUnits(textures, n);
gl.uniform1iv(this.addr, units);
for (let i = 0; i !== n; ++i) {
textures.safeSetTexture2D(v[i] || emptyTexture, units[i]);
}
}
function setValueT3DArray(gl, v, textures) {
const n = v.length;
const units = allocTexUnits(textures, n);
gl.uniform1iv(this.addr, units);
for (let i = 0; i !== n; ++i) {
textures.setTexture3D(v[i] || emptyTexture3d, units[i]);
}
}
function setValueT6Array(gl, v, textures) {
const n = v.length;
const units = allocTexUnits(textures, n);
gl.uniform1iv(this.addr, units);
for (let i = 0; i !== n; ++i) {
textures.safeSetTextureCube(v[i] || emptyCubeTexture, units[i]);
}
}
function setValueT2DArrayArray(gl, v, textures) {
const n = v.length;
const units = allocTexUnits(textures, n);
gl.uniform1iv(this.addr, units);
for (let i = 0; i !== n; ++i) {
textures.setTexture2DArray(v[i] || emptyTexture2dArray, units[i]);
}
}
// Helper to pick the right setter for a pure (bottom-level) array
function getPureArraySetter(type) {
switch (type) {
case 0x1406:
return setValueV1fArray; // FLOAT
case 0x8b50:
return setValueV2fArray; // _VEC2
case 0x8b51:
return setValueV3fArray; // _VEC3
case 0x8b52:
return setValueV4fArray; // _VEC4
case 0x8b5a:
return setValueM2Array; // _MAT2
case 0x8b5b:
return setValueM3Array; // _MAT3
case 0x8b5c:
return setValueM4Array; // _MAT4
case 0x1404:
case 0x8b56:
return setValueV1iArray; // INT, BOOL
case 0x8b53:
case 0x8b57:
return setValueV2iArray; // _VEC2
case 0x8b54:
case 0x8b58:
return setValueV3iArray; // _VEC3
case 0x8b55:
case 0x8b59:
return setValueV4iArray; // _VEC4
case 0x1405:
return setValueV1uiArray; // UINT
case 0x8dc6:
return setValueV2uiArray; // _VEC2
case 0x8dc7:
return setValueV3uiArray; // _VEC3
case 0x8dc8:
return setValueV4uiArray; // _VEC4
case 0x8b5e: // SAMPLER_2D
case 0x8d66: // SAMPLER_EXTERNAL_OES
case 0x8dca: // INT_SAMPLER_2D
case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D
case 0x8b62: // SAMPLER_2D_SHADOW
return setValueT1Array;
case 0x8b5f: // SAMPLER_3D
case 0x8dcb: // INT_SAMPLER_3D
case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D
return setValueT3DArray;
case 0x8b60: // SAMPLER_CUBE
case 0x8dcc: // INT_SAMPLER_CUBE
case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE
case 0x8dc5: // SAMPLER_CUBE_SHADOW
return setValueT6Array;
case 0x8dc1: // SAMPLER_2D_ARRAY
case 0x8dcf: // INT_SAMPLER_2D_ARRAY
case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY
case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW
return setValueT2DArrayArray;
}
}
// --- Uniform Classes ---
function SingleUniform(id, activeInfo, addr) {
this.id = id;
this.addr = addr;
this.cache = [];
this.setValue = getSingularSetter(activeInfo.type);
// this.path = activeInfo.name; // DEBUG
}
function PureArrayUniform(id, activeInfo, addr) {
this.id = id;
this.addr = addr;
this.cache = [];
this.size = activeInfo.size;
this.setValue = getPureArraySetter(activeInfo.type);
// this.path = activeInfo.name; // DEBUG
}
PureArrayUniform.prototype.updateCache = function (data) {
const cache = this.cache;
if (data instanceof Float32Array && cache.length !== data.length) {
this.cache = new Float32Array(data.length);
}
copyArray(cache, data);
};
function StructuredUniform(id) {
this.id = id;
this.seq = [];
this.map = {};
}
StructuredUniform.prototype.setValue = function (gl, value, textures) {
const seq = this.seq;
for (let i = 0, n = seq.length; i !== n; ++i) {
const u = seq[i];
u.setValue(gl, value[u.id], textures);
}
};
// --- Top-level ---
// Parser - builds up the property tree from the path strings
const RePathPart = /(\w+)(\])?(\[|\.)?/g;
// extracts
// - the identifier (member name or array index)
// - followed by an optional right bracket (found when array index)
// - followed by an optional left bracket or dot (type of subscript)
//
// Note: These portions can be read in a non-overlapping fashion and
// allow straightforward parsing of the hierarchy that WebGL encodes
// in the uniform names.
function addUniform(container, uniformObject) {
container.seq.push(uniformObject);
container.map[uniformObject.id] = uniformObject;
}
function parseUniform(activeInfo, addr, container) {
const path = activeInfo.name,
pathLength = path.length;
// reset RegExp object, because of the early exit of a previous run
RePathPart.lastIndex = 0;
while (true) {
const match = RePathPart.exec(path),
matchEnd = RePathPart.lastIndex;
let id = match[1];
const idIsIndex = match[2] === ']',
subscript = match[3];
if (idIsIndex) id = id | 0; // convert to integer
if (subscript === undefined || (subscript === '[' && matchEnd + 2 === pathLength)) {
// bare name or "pure" bottom-level array "[0]" suffix
addUniform(container, subscript === undefined ? new SingleUniform(id, activeInfo, addr) : new PureArrayUniform(id, activeInfo, addr));
break;
} else {
// step into inner node / create it in case it doesn't exist
const map = container.map;
let next = map[id];
if (next === undefined) {
next = new StructuredUniform(id);
addUniform(container, next);
}
container = next;
}
}
}
// Root Container
function WebGLUniforms(gl, program) {
this.seq = [];
this.map = {};
const n = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < n; ++i) {
const info = gl.getActiveUniform(program, i),
addr = gl.getUniformLocation(program, info.name);
parseUniform(info, addr, this);
}
}
WebGLUniforms.prototype.setValue = function (gl, name, value, textures) {
const u = this.map[name];
if (u !== undefined) u.setValue(gl, value, textures);
};
WebGLUniforms.prototype.setOptional = function (gl, object, name) {
const v = object[name];
if (v !== undefined) this.setValue(gl, name, v);
};
// Static interface
WebGLUniforms.upload = function (gl, seq, values, textures) {
for (let i = 0, n = seq.length; i !== n; ++i) {
const u = seq[i],
v = values[u.id];
if (v.needsUpdate !== false) {
// note: always updating when .needsUpdate is undefined
u.setValue(gl, v.value, textures);
}
}
};
WebGLUniforms.seqWithValue = function (seq, values) {
const r = [];
for (let i = 0, n = seq.length; i !== n; ++i) {
const u = seq[i];
if (u.id in values) r.push(u);
}
return r;
};
export { WebGLUniforms };
|