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

71.31% Statements 92/129
45.83% Branches 22/48
85.71% Functions 18/21
70.96% Lines 88/124

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                    34x   90x       34x   45x 45x 45x     34x 45x             45x 45x     45x       34x       34x   45x 45x 45x     34x 45x             45x 45x 45x         34x       34x 49x 49x 49x   49x 49x 31x   49x     34x 18x             18x 18x 18x     34x 26x   26x             26x 26x 8x 8x               26x 26x                 34x                                                                 34x 8x             8x 8x 8x                           34x                                               34x   34x 53x 21x   32x 32x 32x 32x 32x       34x 32x         34x 4929x 4925x     34x 10x             10x 10x   10x 10x 10x 10x 10x             10x       10x 10x 10x           10x                 34x           1x                           34x     34x   34x         34x 34x       34x       1x        
import * as macro from 'vtk.js/Sources/macros';
import vtkOpenGLTexture from 'vtk.js/Sources/Rendering/OpenGL/Texture';
import { VtkDataTypes } from 'vtk.js/Sources/Common/Core/DataArray/Constants';
import { Filter } from 'vtk.js/Sources/Rendering/OpenGL/Texture/Constants';
 
// ----------------------------------------------------------------------------
// vtkFramebuffer methods
// ----------------------------------------------------------------------------
function vtkFramebuffer(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkFramebuffer');
 
  publicAPI.getBothMode = () => model.context.FRAMEBUFFER;
  // publicAPI.getDrawMode = () => model.context.DRAW_FRAMEBUFFER;
  // publicAPI.getReadMode = () => model.context.READ_FRAMEBUFFER;
 
  publicAPI.saveCurrentBindingsAndBuffers = (modeIn) => {
    const mode =
      typeof modeIn !== 'undefined' ? modeIn : publicAPI.getBothMode();
    publicAPI.saveCurrentBindings(mode);
    publicAPI.saveCurrentBuffers(mode);
  };
 
  publicAPI.saveCurrentBindings = (modeIn) => {
    Iif (!model.context) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling saveCurrentBindings'
      );
      return;
    }
 
    const gl = model.context;
    model.previousDrawBinding = gl.getParameter(
      model.context.FRAMEBUFFER_BINDING
    );
    model.previousActiveFramebuffer =
      model._openGLRenderWindow.getActiveFramebuffer();
  };
 
  publicAPI.saveCurrentBuffers = (modeIn) => {
    // noop on webgl 1
  };
 
  publicAPI.restorePreviousBindingsAndBuffers = (modeIn) => {
    const mode =
      typeof modeIn !== 'undefined' ? modeIn : publicAPI.getBothMode();
    publicAPI.restorePreviousBindings(mode);
    publicAPI.restorePreviousBuffers(mode);
  };
 
  publicAPI.restorePreviousBindings = (modeIn) => {
    Iif (!model.context) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling restorePreviousBindings'
      );
      return;
    }
 
    const gl = model.context;
    gl.bindFramebuffer(gl.FRAMEBUFFER, model.previousDrawBinding);
    model._openGLRenderWindow.setActiveFramebuffer(
      model.previousActiveFramebuffer
    );
  };
 
  publicAPI.restorePreviousBuffers = (modeIn) => {
    // currently a noop on webgl1
  };
 
  publicAPI.bind = (modeArg = null) => {
    let mode = modeArg;
    if (mode === null) {
      mode = model.context.FRAMEBUFFER;
    }
    model.context.bindFramebuffer(mode, model.glFramebuffer);
    for (let i = 0; i < model.colorBuffers.length; i++) {
      model.colorBuffers[i].bind();
    }
    model._openGLRenderWindow.setActiveFramebuffer(publicAPI);
  };
 
  publicAPI.create = (width, height) => {
    Iif (!model.context) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling create'
      );
      return;
    }
 
    model.glFramebuffer = model.context.createFramebuffer();
    model.glFramebuffer.width = width;
    model.glFramebuffer.height = height;
  };
 
  publicAPI.setColorBuffer = (texture, attachment = 0) => {
    const gl = model.context;
 
    Iif (!gl) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling setColorBuffer'
      );
      return;
    }
 
    let glAttachment = gl.COLOR_ATTACHMENT0;
    if (attachment > 0) {
      if (model._openGLRenderWindow.getWebgl2()) {
        glAttachment += attachment;
      } else E{
        macro.vtkErrorMacro(
          'Using multiple framebuffer attachments requires WebGL 2'
        );
        return;
      }
    }
    model.colorBuffers[attachment] = texture;
    gl.framebufferTexture2D(
      gl.FRAMEBUFFER,
      glAttachment,
      gl.TEXTURE_2D,
      texture.getHandle(),
      0
    );
  };
 
  publicAPI.removeColorBuffer = (attachment = 0) => {
    const gl = model.context;
 
    if (!gl) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling removeColorBuffer'
      );
      return;
    }
 
    let glAttachment = gl.COLOR_ATTACHMENT0;
    if (attachment > 0) {
      if (model._openGLRenderWindow.getWebgl2()) {
        glAttachment += attachment;
      } else {
        macro.vtkErrorMacro(
          'Using multiple framebuffer attachments requires WebGL 2'
        );
        return;
      }
    }
 
    gl.framebufferTexture2D(
      gl.FRAMEBUFFER,
      glAttachment,
      gl.TEXTURE_2D,
      null,
      0
    );
 
    model.colorBuffers = model.colorBuffers.splice(attachment, 1);
  };
 
  publicAPI.setDepthBuffer = (texture) => {
    Iif (!model.context) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling setDepthBuffer'
      );
      return;
    }
 
    if (model._openGLRenderWindow.getWebgl2()) {
      const gl = model.context;
      gl.framebufferTexture2D(
        gl.FRAMEBUFFER,
        gl.DEPTH_ATTACHMENT,
        gl.TEXTURE_2D,
        texture.getHandle(),
        0
      );
    } else E{
      macro.vtkErrorMacro(
        'Attaching depth buffer textures to fbo requires WebGL 2'
      );
    }
  };
 
  publicAPI.removeDepthBuffer = () => {
    if (!model.context) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling removeDepthBuffer'
      );
      return;
    }
 
    if (model._openGLRenderWindow.getWebgl2()) {
      const gl = model.context;
      gl.framebufferTexture2D(
        gl.FRAMEBUFFER,
        gl.DEPTH_ATTACHMENT,
        gl.TEXTURE_2D,
        null,
        0
      );
    } else {
      macro.vtkErrorMacro(
        'Attaching depth buffer textures to framebuffers requires WebGL 2'
      );
    }
  };
 
  publicAPI.getGLFramebuffer = () => model.glFramebuffer;
 
  publicAPI.setOpenGLRenderWindow = (rw) => {
    if (model._openGLRenderWindow === rw) {
      return;
    }
    publicAPI.releaseGraphicsResources();
    model._openGLRenderWindow = rw;
    model.context = null;
    if (rw) {
      model.context = model._openGLRenderWindow.getContext();
    }
  };
 
  publicAPI.releaseGraphicsResources = () => {
    Iif (model.glFramebuffer) {
      model.context.deleteFramebuffer(model.glFramebuffer);
    }
  };
 
  publicAPI.getSize = () => {
    if (model.glFramebuffer == null) return null;
    return [model.glFramebuffer.width, model.glFramebuffer.height];
  };
 
  publicAPI.populateFramebuffer = () => {
    Iif (!model.context) {
      macro.vtkErrorMacro(
        'you must set the OpenGLRenderWindow before calling populateFrameBuffer'
      );
      return;
    }
 
    publicAPI.bind();
    const gl = model.context;
 
    const texture = vtkOpenGLTexture.newInstance();
    texture.setOpenGLRenderWindow(model._openGLRenderWindow);
    texture.setMinificationFilter(Filter.LINEAR);
    texture.setMagnificationFilter(Filter.LINEAR);
    texture.create2DFromRaw(
      model.glFramebuffer.width,
      model.glFramebuffer.height,
      4,
      VtkDataTypes.UNSIGNED_CHAR,
      null
    );
    publicAPI.setColorBuffer(texture);
 
    // for now do not count on having a depth buffer texture
    // as they are not standard webgl 1
    model.depthTexture = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, model.depthTexture);
    gl.renderbufferStorage(
      gl.RENDERBUFFER,
      gl.DEPTH_COMPONENT16,
      model.glFramebuffer.width,
      model.glFramebuffer.height
    );
    gl.framebufferRenderbuffer(
      gl.FRAMEBUFFER,
      gl.DEPTH_ATTACHMENT,
      gl.RENDERBUFFER,
      model.depthTexture
    );
  };
 
  // For backwards compatibility. Use getColorBuffers()[0] going forward.
  publicAPI.getColorTexture = () => model.colorBuffers[0];
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
  // _openGLRenderWindow: null,
  glFramebuffer: null,
  colorBuffers: null,
  depthTexture: null,
  previousDrawBinding: 0,
  previousReadBinding: 0,
  previousDrawBuffer: 0,
  previousReadBuffer: 0,
  previousActiveFramebuffer: null,
};
 
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
 
  Iif (model.colorBuffers) {
    macro.vtkErrorMacro(
      'you cannot initialize colorBuffers through the constructor. You should call setColorBuffer() instead.'
    );
  }
  model.colorBuffers = [];
  macro.getArray(publicAPI, model, ['colorBuffers']);
 
  // For more macro methods, see "Sources/macros.js"
  // Object specific methods
  vtkFramebuffer(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
export const newInstance = macro.newInstance(extend, 'vtkFramebuffer');
 
// ----------------------------------------------------------------------------
export default { newInstance, extend };