All files / Sources/Rendering/OpenGL/ShaderProgram index.js

60.64% Statements 208/343
50.65% Branches 77/152
71.05% Functions 27/38
60.52% Lines 207/342

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x             82287x             82287x   82287x 82287x                         5357x   5357x 275x                     275x                       275x       275x         275x         275x 275x     5357x                                   5357x 1005x       1005x 1005x 1005x     5404x   5357x 814x 814x     5357x           5357x 275x       275x             275x   275x 275x       275x             275x 275x 275x     5357x 4895x 4895x 1x 1x   4894x 4894x 4894x     5357x 2713x 2713x       2713x 2713x 2713x     5357x 29645x 29645x 2224x 2224x   27421x 27421x     5357x 8x 8x 2x 2x   6x 6x     5357x 5724x 5724x 2869x 2869x   2855x 2855x     5357x 3x 3x       3x 3x     5357x 7x 7x 1x 1x   6x     6x 6x     5357x                   5357x                         5357x                   5357x 163x 163x       163x     163x 163x     5357x 16131x 16131x 5170x 5170x   10961x     10961x 10961x     5357x 36x 36x 7x 7x   29x 29x     5357x 19x 19x       19x   19x     19x     19x 19x     5357x                   5357x 44x 44x 36x 36x   8x   8x 8x   8x     8x 8x     5357x 36x 36x       36x 36x     5357x                                   5357x                   5357x 59424x         59424x 59424x 55948x     3476x 3476x 629x 629x 629x     2847x 2847x     5357x 29576x         29576x 29576x 27713x     1863x             1863x 1863x   1863x 995x     868x     5357x 11069x         11069x 4966x     6103x             6103x 6103x 5679x   424x   424x     5357x 550x       550x         550x 275x 275x       275x 275x     550x 275x     275x   550x 275x     275x     550x 550x 550x     5357x                                                                         5357x 5356x 5356x 5356x 5356x     5357x 399x                                                       1x                                               5357x     5357x 5357x 5357x 5357x 5357x 5357x 5357x 5357x     5357x 5357x 5357x                         5357x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkShader from 'vtk.js/Sources/Rendering/OpenGL/Shader';
 
const { vtkErrorMacro } = macro;
 
// perform in place string substitutions, indicate if a substitution was done
// this is useful for building up shader strings which typically involve
// lots of string substitutions. Return true if a substitution was done.
export function substitute(source, search, replace, all) {
  // We only accept strings or array of strings, typeof is faster than Array.isArray
  const replaceStr = typeof replace === 'string' ? replace : replace.join('\n');
 
  // We don't need to instantiate a RegExp if we don't want a global substitution.
  // In all other cases, we need to take the provided string or RegExp and
  // instantiate a new one to add the `g` flag.
  // Argument defaults are transpiled to slow `arguments`-based operations
  // better assume undefined as flag to know if the value is set or not
  const replaceSearch = all === false ? search : new RegExp(search, 'g');
 
  const resultstr = source.replace(replaceSearch, replaceStr);
  return {
    // If the result is different than the input, we did perform a replacement
    replace: resultstr !== replaceStr,
    result: resultstr,
  };
}
 
// ----------------------------------------------------------------------------
// vtkShaderProgram methods
// ----------------------------------------------------------------------------
 
function vtkShaderProgram(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkShaderProgram');
 
  publicAPI.compileShader = () => {
    Iif (!model.vertexShader.compile()) {
      vtkErrorMacro(
        model.vertexShader
          .getSource()
          .split('\n')
          .map((line, index) => `${index}: ${line}`)
          .join('\n')
      );
      vtkErrorMacro(model.vertexShader.getError());
      return 0;
    }
    Iif (!model.fragmentShader.compile()) {
      vtkErrorMacro(
        model.fragmentShader
          .getSource()
          .split('\n')
          .map((line, index) => `${index}: ${line}`)
          .join('\n')
      );
      vtkErrorMacro(model.fragmentShader.getError());
      return 0;
    }
    // skip geometry for now
    Iif (!publicAPI.attachShader(model.vertexShader)) {
      vtkErrorMacro(model.error);
      return 0;
    }
    Iif (!publicAPI.attachShader(model.fragmentShader)) {
      vtkErrorMacro(model.error);
      return 0;
    }
 
    Iif (!publicAPI.link()) {
      vtkErrorMacro(`Links failed: ${model.error}`);
      return 0;
    }
 
    publicAPI.setCompiled(true);
    return 1;
  };
 
  publicAPI.cleanup = () => {
    if (model.shaderType === 'Unknown' || model.handle === 0) {
      return;
    }
    publicAPI.release();
    if (model.vertexShaderHandle !== 0) {
      model.context.detachShader(model.handle, model.vertexShaderHandle);
      model.vertexShaderHandle = 0;
    }
    if (model.fragmentShaderHandle !== 0) {
      model.context.detachShader(model.handle, model.fragmentShaderHandle);
      model.fragmentShaderHandle = 0;
    }
    model.context.deleteProgram(model.handle);
    model.handle = 0;
    publicAPI.setCompiled(false);
  };
 
  publicAPI.bind = () => {
    Iif (!model.linked && !publicAPI.link()) {
      return false;
    }
 
    model.context.useProgram(model.handle);
    publicAPI.setBound(true);
    return true;
  };
 
  publicAPI.isBound = () => !!model.bound;
 
  publicAPI.release = () => {
    model.context.useProgram(null);
    publicAPI.setBound(false);
  };
 
  publicAPI.setContext = (ctx) => {
    model.vertexShader.setContext(ctx);
    model.fragmentShader.setContext(ctx);
    model.geometryShader.setContext(ctx);
  };
 
  publicAPI.link = () => {
    Iif (model.linked) {
      return true;
    }
 
    Iif (model.handle === 0) {
      model.error =
        'Program has not been initialized, and/or does not have shaders.';
      return false;
    }
 
    // clear out the list of uniforms used
    model.uniformLocs = {};
 
    model.context.linkProgram(model.handle);
    const isCompiled = model.context.getProgramParameter(
      model.handle,
      model.context.LINK_STATUS
    );
    Iif (!isCompiled) {
      const lastError = model.context.getProgramInfoLog(model.handle);
      vtkErrorMacro(`Error linking shader ${lastError}`);
      model.handle = 0;
      return false;
    }
 
    publicAPI.setLinked(true);
    model.attributeLocs = {};
    return true;
  };
 
  publicAPI.setUniformMatrix = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    const f32 = new Float32Array(v);
    model.context.uniformMatrix4fv(location, false, f32);
    return true;
  };
 
  publicAPI.setUniformMatrix3x3 = (name, v) => {
    const location = publicAPI.findUniform(name);
    Iif (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    const f32 = new Float32Array(v);
    model.context.uniformMatrix3fv(location, false, f32);
    return true;
  };
 
  publicAPI.setUniformf = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform1f(location, v);
    return true;
  };
 
  publicAPI.setUniformfv = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform1fv(location, v);
    return true;
  };
 
  publicAPI.setUniformi = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform1i(location, v);
    return true;
  };
 
  publicAPI.setUniformiv = (name, v) => {
    const location = publicAPI.findUniform(name);
    Iif (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform1iv(location, v);
    return true;
  };
 
  publicAPI.setUniform2f = (name, v1, v2) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    Iif (v2 === undefined) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform2f(location, v1, v2);
    return true;
  };
 
  publicAPI.setUniform2fv = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform2fv(location, v);
    return true;
  };
 
  publicAPI.setUniform2i = (name, v1, v2) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    if (v2 === undefined) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform2i(location, v1, v2);
    return true;
  };
 
  publicAPI.setUniform2iv = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform2iv(location, v);
    return true;
  };
 
  publicAPI.setUniform3f = (name, a1, a2, a3) => {
    const location = publicAPI.findUniform(name);
    Iif (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    Iif (a3 === undefined) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform3f(location, a1, a2, a3);
    return true;
  };
 
  publicAPI.setUniform3fArray = (name, a) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    Iif (!Array.isArray(a) || a.length !== 3) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform3f(location, a[0], a[1], a[2]);
    return true;
  };
 
  publicAPI.setUniform3fv = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform3fv(location, v);
    return true;
  };
 
  publicAPI.setUniform3i = (name, ...args) => {
    const location = publicAPI.findUniform(name);
    Iif (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    let array = args;
    // allow an array passed as a single argument
    Iif (array.length === 1 && Array.isArray(array[0])) {
      array = array[0];
    }
    Iif (array.length !== 3) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform3i(location, array[0], array[1], array[2]);
    return true;
  };
 
  publicAPI.setUniform3iv = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform3iv(location, v);
    return true;
  };
 
  publicAPI.setUniform4f = (name, ...args) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    let array = args;
    // allow an array passed as a single argument
    if (array.length === 1 && Array.isArray(array[0])) {
      array = array[0];
    }
    Iif (array.length !== 4) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform4f(location, array[0], array[1], array[2], array[3]);
    return true;
  };
 
  publicAPI.setUniform4fv = (name, v) => {
    const location = publicAPI.findUniform(name);
    Iif (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform4fv(location, v);
    return true;
  };
 
  publicAPI.setUniform4i = (name, ...args) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    let array = args;
    // allow an array passed as a single argument
    if (array.length === 1 && Array.isArray(array[0])) {
      array = array[0];
    }
    if (array.length !== 4) {
      throw new RangeError('Invalid number of values for array');
    }
    model.context.uniform4i(location, array[0], array[1], array[2], array[3]);
    return true;
  };
 
  publicAPI.setUniform4iv = (name, v) => {
    const location = publicAPI.findUniform(name);
    if (location === -1) {
      model.error = `Could not set uniform ${name} . No such uniform.`;
      return false;
    }
    model.context.uniform4iv(location, v);
    return true;
  };
 
  publicAPI.findUniform = (name) => {
    Iif (!name || !model.linked) {
      return -1;
    }
 
    // see if we have cached the result
    let loc = model.uniformLocs[name];
    if (loc !== undefined) {
      return loc;
    }
 
    loc = model.context.getUniformLocation(model.handle, name);
    if (loc === null) {
      model.error = `Uniform ${name} not found in current shader program.`;
      model.uniformLocs[name] = -1;
      return -1;
    }
 
    model.uniformLocs[name] = loc;
    return loc;
  };
 
  publicAPI.isUniformUsed = (name) => {
    Iif (!name) {
      return false;
    }
 
    // see if we have cached the result
    let loc = model.uniformLocs[name];
    if (loc !== undefined) {
      return loc !== null;
    }
 
    Iif (!model.linked) {
      vtkErrorMacro(
        'attempt to find uniform when the shader program is not linked'
      );
      return false;
    }
 
    loc = model.context.getUniformLocation(model.handle, name);
    model.uniformLocs[name] = loc;
 
    if (loc === null) {
      return false;
    }
 
    return true;
  };
 
  publicAPI.isAttributeUsed = (name) => {
    Iif (!name) {
      return false;
    }
 
    // see if we have cached the result
    if (name in model.attributeLocs) {
      return true;
    }
 
    Iif (!model.linked) {
      vtkErrorMacro(
        'attempt to find uniform when the shader program is not linked'
      );
      return false;
    }
 
    const loc = model.context.getAttribLocation(model.handle, name);
    if (loc === -1) {
      return false;
    }
    model.attributeLocs[name] = loc;
 
    return true;
  };
 
  publicAPI.attachShader = (shader) => {
    Iif (shader.getHandle() === 0) {
      model.error = 'Shader object was not initialized, cannot attach it.';
      return false;
    }
    Iif (shader.getShaderType() === 'Unknown') {
      model.error = 'Shader object is of type Unknown and cannot be used.';
      return false;
    }
 
    if (model.handle === 0) {
      const thandle = model.context.createProgram();
      Iif (thandle === 0) {
        model.error = 'Could not create shader program.';
        return false;
      }
      model.handle = thandle;
      model.linked = false;
    }
 
    if (shader.getShaderType() === 'Vertex') {
      Iif (model.vertexShaderHandle !== 0) {
        model.context.detachShader(model.handle, model.vertexShaderHandle);
      }
      model.vertexShaderHandle = shader.getHandle();
    }
    if (shader.getShaderType() === 'Fragment') {
      Iif (model.fragmentShaderHandle !== 0) {
        model.context.detachShader(model.handle, model.fragmentShaderHandle);
      }
      model.fragmentShaderHandle = shader.getHandle();
    }
 
    model.context.attachShader(model.handle, shader.getHandle());
    publicAPI.setLinked(false);
    return true;
  };
 
  publicAPI.detachShader = (shader) => {
    if (shader.getHandle() === 0) {
      model.error = 'shader object was not initialized, cannot attach it.';
      return false;
    }
    if (shader.getShaderType() === 'Unknown') {
      model.error = 'Shader object is of type Unknown and cannot be used.';
      return false;
    }
    if (model.handle === 0) {
      model.error = 'This shader program has not been initialized yet.';
    }
 
    switch (shader.getShaderType()) {
      case 'Vertex':
        if (model.vertexShaderHandle !== shader.getHandle()) {
          model.error = 'The supplied shader was not attached to this program.';
          return false;
        }
        model.context.detachShader(model.handle, shader.getHandle());
        model.vertexShaderHandle = 0;
        model.linked = false;
        return true;
      case 'Fragment':
        if (model.fragmentShaderHandle !== shader.getHandle()) {
          model.error = 'The supplied shader was not attached to this program.';
          return false;
        }
        model.context.detachShader(model.handle, shader.getHandle());
        model.fragmentShaderHandle = 0;
        model.linked = false;
        return true;
      default:
        return false;
    }
  };
 
  publicAPI.setContext = (ctx) => {
    model.context = ctx;
    model.vertexShader.setContext(ctx);
    model.fragmentShader.setContext(ctx);
    model.geometryShader.setContext(ctx);
  };
 
  publicAPI.setLastCameraMTime = (mtime) => {
    model.lastCameraMTime = mtime;
  };
 
  // publicAPI.enableAttributeArray = (name) => {
  //   const location = publicAPI.findAttributeArray(name);
  //   if (location === -1) {
  //     model.error = `Could not enable attribute ${name} No such attribute.`;
  //     return false;
  //   }
  //   model.context.enableVertexAttribArray(location);
  //   return true;
  // };
 
  // publicAPI.disableAttributeArray = (name) => {
  //   const location = publicAPI.findAttributeArray(name);
  //   if (location === -1) {
  //     model.error = `Could not enable attribute ${name} No such attribute.`;
  //     return false;
  //   }
  //   model.context.disableVertexAttribArray(location);
  //   return true;
  // };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  vertexShaderHandle: 0,
  fragmentShaderHandle: 0,
  geometryShaderHandle: 0,
  vertexShader: null,
  fragmentShader: null,
  geometryShader: null,
 
  linked: false,
  bound: false,
  compiled: false,
  error: '',
  handle: 0,
  numberOfOutputs: 0,
  attributesLocs: null,
  uniformLocs: null,
  md5Hash: 0,
  context: null,
  lastCameraMTime: null,
};
 
// ----------------------------------------------------------------------------
 
function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Instantiate internal objects
  model.attributesLocs = {};
  model.uniformLocs = {};
  model.vertexShader = vtkShader.newInstance();
  model.vertexShader.setShaderType('Vertex');
  model.fragmentShader = vtkShader.newInstance();
  model.fragmentShader.setShaderType('Fragment');
  model.geometryShader = vtkShader.newInstance();
  model.geometryShader.setShaderType('Geometry');
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.get(publicAPI, model, ['lastCameraMTime']);
  macro.setGet(publicAPI, model, [
    'error',
    'handle',
    'compiled',
    'bound',
    'md5Hash',
    'vertexShader',
    'fragmentShader',
    'geometryShader',
    'linked',
  ]);
 
  // Object methods
  vtkShaderProgram(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
const newInstance = macro.newInstance(extend, 'vtkShaderProgram');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, substitute };