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

53.55% Statements 98/183
47.57% Branches 49/103
92.85% Functions 13/14
56.39% Lines 97/172

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                  5088x     5088x       5088x 2138x 2138x         2138x         2138x 2138x 2138x                         5088x     40874x   5088x   4161x 2138x   4161x 4161x     4161x                                                                       5088x   8650x 1228x     1228x   7422x                                                                     5088x 8539x 8539x 1117x     1117x     8539x 8539x     5088x 8539x 8539x             8539x 8539x 8539x     5088x                   5073x                         5088x                       5404x         5404x                 5404x 2138x   5404x     5404x       5404x   5404x 5404x 5404x 5404x 5404x 5404x 5404x 5404x 5404x 5404x   5404x           5404x 5404x 5404x                 5404x 331x     331x       5404x     5404x                                               5404x     5088x                       141x                         141x       141x   141x   141x 379x 379x               379x 379x     379x         141x     5088x 5672x         5672x                               5672x     5088x 5087x 54x   5033x 5033x 5033x 5033x 5033x                 1x                         5088x     5088x     5088x     5088x     5088x         5088x         1x                
import macro from 'vtk.js/Sources/macros';
import { ObjectType } from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';
 
// ----------------------------------------------------------------------------
// vtkOpenGLVertexArrayObject methods
// ----------------------------------------------------------------------------
 
function vtkOpenGLVertexArrayObject(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkOpenGLVertexArrayObject');
 
  // Public API methods
  publicAPI.exposedMethod = () => {
    // This is a publicly exposed method of this object
  };
 
  publicAPI.initialize = () => {
    model.instancingExtension = null;
    Iif (!model._openGLRenderWindow.getWebgl2()) {
      model.instancingExtension = model.context.getExtension(
        'ANGLE_instanced_arrays'
      );
    }
    if (
      !model.forceEmulation &&
      model._openGLRenderWindow &&
      model._openGLRenderWindow.getWebgl2()
    ) {
      model.extension = null;
      model.supported = true;
      model.handleVAO = model.context.createVertexArray();
    } else E{
      model.extension = model.context.getExtension('OES_vertex_array_object');
      // Start setting up VAO
      if (!model.forceEmulation && model.extension) {
        model.supported = true;
        model.handleVAO = model.extension.createVertexArrayOES();
      } else {
        model.supported = false;
      }
    }
  };
 
  publicAPI.isReady = () =>
    // We either probed and allocated a VAO, or are falling back as the current
    // hardware does not support VAOs.
    model.handleVAO !== 0 || model.supported === false;
 
  publicAPI.bind = () => {
    // Either simply bind the VAO, or emulate behavior by binding all attributes.
    if (!publicAPI.isReady()) {
      publicAPI.initialize();
    }
    if (publicAPI.isReady() && model.supported) {
      Iif (model.extension) {
        model.extension.bindVertexArrayOES(model.handleVAO);
      } else {
        model.context.bindVertexArray(model.handleVAO);
      }
    } else Eif (publicAPI.isReady()) {
      const gl = model.context;
      for (let ibuff = 0; ibuff < model.buffers.length; ++ibuff) {
        const buff = model.buffers[ibuff];
        model.context.bindBuffer(gl.ARRAY_BUFFER, buff.buffer);
        for (let iatt = 0; iatt < buff.attributes.length; ++iatt) {
          const attrIt = buff.attributes[iatt];
          const matrixCount = attrIt.isMatrix ? attrIt.size : 1;
          for (let i = 0; i < matrixCount; ++i) {
            gl.enableVertexAttribArray(attrIt.index + i);
            gl.vertexAttribPointer(
              attrIt.index + i,
              attrIt.size,
              attrIt.type,
              attrIt.normalize,
              attrIt.stride,
              attrIt.offset + (attrIt.stride * i) / attrIt.size
            );
            if (attrIt.divisor > 0) {
              if (model.instancingExtension) {
                model.instancingExtension.vertexAttribDivisorANGLE(
                  attrIt.index + i,
                  1
                );
              } else {
                gl.vertexAttribDivisor(attrIt.index + i, 1);
              }
            }
          }
        }
      }
    }
  };
 
  publicAPI.release = () => {
    // Either simply release the VAO, or emulate behavior by releasing all attributes.
    if (publicAPI.isReady() && model.supported) {
      Iif (model.extension) {
        model.extension.bindVertexArrayOES(null);
      } else {
        model.context.bindVertexArray(null);
      }
    } else Iif (publicAPI.isReady()) {
      const gl = model.context;
      for (let ibuff = 0; ibuff < model.buffers.length; ++ibuff) {
        const buff = model.buffers[ibuff];
        model.context.bindBuffer(gl.ARRAY_BUFFER, buff.buffer);
        for (let iatt = 0; iatt < buff.attributes.length; ++iatt) {
          const attrIt = buff.attributes[iatt];
          const matrixCount = attrIt.isMatrix ? attrIt.size : 1;
          for (let i = 0; i < matrixCount; ++i) {
            gl.enableVertexAttribArray(attrIt.index + i);
            gl.vertexAttribPointer(
              attrIt.index + i,
              attrIt.size,
              attrIt.type,
              attrIt.normalize,
              attrIt.stride,
              attrIt.offset + (attrIt.stride * i) / attrIt.size
            );
            if (attrIt.divisor > 0) {
              if (model.instancingExtension) {
                model.instancingExtension.vertexAttribDivisorANGLE(
                  attrIt.index + i,
                  0
                );
              } else {
                gl.vertexAttribDivisor(attrIt.index + i, 0);
              }
            }
            gl.disableVertexAttribArray(attrIt.index + i);
          }
        }
      }
    }
  };
 
  publicAPI.shaderProgramChanged = () => {
    publicAPI.release();
    if (model.handleVAO) {
      Iif (model.extension) {
        model.extension.deleteVertexArrayOES(model.handleVAO);
      } else {
        model.context.deleteVertexArray(model.handleVAO);
      }
    }
    model.handleVAO = 0;
    model.handleProgram = 0;
  };
 
  publicAPI.releaseGraphicsResources = () => {
    publicAPI.shaderProgramChanged();
    Iif (model.handleVAO) {
      if (model.extension) {
        model.extension.deleteVertexArrayOES(model.handleVAO);
      } else {
        model.context.deleteVertexArray(model.handleVAO);
      }
    }
    model.handleVAO = 0;
    model.supported = true;
    model.handleProgram = 0;
  };
 
  publicAPI.addAttributeArray = (
    program,
    buffer,
    name,
    offset,
    stride,
    elementType,
    elementTupleSize,
    normalize
  ) =>
    publicAPI.addAttributeArrayWithDivisor(
      program,
      buffer,
      name,
      offset,
      stride,
      elementType,
      elementTupleSize,
      normalize,
      0,
      false
    );
 
  publicAPI.addAttributeArrayWithDivisor = (
    program,
    buffer,
    name,
    offset,
    stride,
    elementType,
    elementTupleSize,
    normalize,
    divisor,
    isMatrix
  ) => {
    Iif (!program) {
      return false;
    }
 
    // Check the program is bound, and the buffer is valid.
    Iif (
      !program.isBound() ||
      buffer.getHandle() === 0 ||
      buffer.getType() !== ObjectType.ARRAY_BUFFER
    ) {
      return false;
    }
 
    // Perform initialization if necessary, ensure program matches VAOs.
    if (model.handleProgram === 0) {
      model.handleProgram = program.getHandle();
    }
    Iif (!publicAPI.isReady()) {
      publicAPI.initialize();
    }
    Iif (!publicAPI.isReady() || model.handleProgram !== program.getHandle()) {
      return false;
    }
 
    const gl = model.context;
 
    const attribs = {};
    attribs.name = name;
    attribs.index = gl.getAttribLocation(model.handleProgram, name);
    attribs.offset = offset;
    attribs.stride = stride;
    attribs.type = elementType;
    attribs.size = elementTupleSize;
    attribs.normalize = normalize;
    attribs.isMatrix = isMatrix;
    attribs.divisor = divisor;
 
    Iif (attribs.Index === -1) {
      return false;
    }
 
    // Always make the call as even the first use wants the attrib pointer setting
    // up when we are emulating.
    buffer.bind();
    gl.enableVertexAttribArray(attribs.index);
    gl.vertexAttribPointer(
      attribs.index,
      attribs.size,
      attribs.type,
      attribs.normalize,
      attribs.stride,
      attribs.offset
    );
 
    if (divisor > 0) {
      Iif (model.instancingExtension) {
        model.instancingExtension.vertexAttribDivisorANGLE(attribs.index, 1);
      } else {
        gl.vertexAttribDivisor(attribs.index, 1);
      }
    }
 
    attribs.buffer = buffer.getHandle();
 
    // If vertex array objects are not supported then build up our list.
    Iif (!model.supported) {
      // find the buffer
      let buffFound = false;
      for (let ibuff = 0; ibuff < model.buffers.length; ++ibuff) {
        const buff = model.buffers[ibuff];
        if (buff.buffer === attribs.buffer) {
          buffFound = true;
          let found = false;
          for (let iatt = 0; iatt < buff.attributes.length; ++iatt) {
            const attrIt = buff.attributes[iatt];
            if (attrIt.name === name) {
              found = true;
              buff.attributes[iatt] = attribs;
            }
          }
          if (!found) {
            buff.attributes.push(attribs);
          }
        }
      }
      if (!buffFound) {
        model.buffers.push({ buffer: attribs.buffer, attributes: [attribs] });
      }
    }
    return true;
  };
 
  publicAPI.addAttributeMatrixWithDivisor = (
    program,
    buffer,
    name,
    offset,
    stride,
    elementType,
    elementTupleSize,
    normalize,
    divisor
  ) => {
    // bind the first row of values
    const result = publicAPI.addAttributeArrayWithDivisor(
      program,
      buffer,
      name,
      offset,
      stride,
      elementType,
      elementTupleSize,
      normalize,
      divisor,
      true
    );
 
    Iif (!result) {
      return result;
    }
 
    const gl = model.context;
 
    const index = gl.getAttribLocation(model.handleProgram, name);
 
    for (let i = 1; i < elementTupleSize; i++) {
      gl.enableVertexAttribArray(index + i);
      gl.vertexAttribPointer(
        index + i,
        elementTupleSize,
        elementType,
        normalize,
        stride,
        offset + (stride * i) / elementTupleSize
      );
      if (divisor > 0) {
        Iif (model.instancingExtension) {
          model.instancingExtension.vertexAttribDivisorANGLE(index + i, 1);
        } else {
          gl.vertexAttribDivisor(index + i, 1);
        }
      }
    }
 
    return true;
  };
 
  publicAPI.removeAttributeArray = (name) => {
    Iif (!publicAPI.isReady() || model.handleProgram === 0) {
      return false;
    }
 
    // If we don't have real VAOs find the entry and remove it too.
    Iif (!model.supported) {
      for (let ibuff = 0; ibuff < model.buffers.length; ++ibuff) {
        const buff = model.buffers[ibuff];
        for (let iatt = 0; iatt < buff.attributes.length; ++iatt) {
          const attrIt = buff.attributes[iatt];
          if (attrIt.name === name) {
            buff.attributes.splice(iatt, 1);
            if (!buff.attributes.length) {
              model.buffers.splice(ibuff, 1);
            }
            return true;
          }
        }
      }
    }
 
    return true;
  };
 
  publicAPI.setOpenGLRenderWindow = (rw) => {
    if (model._openGLRenderWindow === rw) {
      return;
    }
    publicAPI.releaseGraphicsResources();
    model._openGLRenderWindow = rw;
    model.context = null;
    if (rw) {
      model.context = model._openGLRenderWindow.getContext();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  forceEmulation: false,
  handleVAO: 0,
  handleProgram: 0,
  supported: true,
  buffers: null,
  context: null,
  // _openGLRenderWindow: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Internal objects initialization
  model.buffers = [];
 
  // Object methods
  macro.obj(publicAPI, model);
 
  // Create get-only macros
  macro.get(publicAPI, model, ['supported']);
 
  // Create get-set macros
  macro.setGet(publicAPI, model, ['forceEmulation']);
 
  // For more macro methods, see "Sources/macros.js"
 
  // Object specific methods
  vtkOpenGLVertexArrayObject(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkOpenGLVertexArrayObject'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };