setupRenderBufferStorage method

void setupRenderBufferStorage(
  1. dynamic renderbuffer,
  2. RenderTarget renderTarget,
  3. bool isMultisample
)

Implementation

void setupRenderBufferStorage(renderbuffer, RenderTarget renderTarget, bool isMultisample) {
  gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);

  if (renderTarget.depthBuffer && !renderTarget.stencilBuffer) {
    var glInternalFormat = gl.DEPTH_COMPONENT16;

    if (isMultisample || useMultisampledRenderToTexture(renderTarget)) {
      var depthTexture = renderTarget.depthTexture;

      if (depthTexture != null) {
        if (depthTexture.type == FloatType) {
          glInternalFormat = gl.DEPTH_COMPONENT32F;
        } else if (depthTexture.type == UnsignedIntType) {
          glInternalFormat = gl.DEPTH_COMPONENT24;
        }
      }

      var samples = getRenderTargetSamples(renderTarget);

      // gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples,
      //     glInternalFormat, renderTarget.width, renderTarget.height);

      if (useMultisampledRenderToTexture(renderTarget)) {
        multisampledRenderToTextureExtension.renderbufferStorageMultisampleEXT(
            gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width.toInt(), renderTarget.height.toInt());
      } else {
        gl.renderbufferStorageMultisample(
            gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width.toInt(), renderTarget.height.toInt());
      }
    } else {
      gl.renderbufferStorage(
          gl.RENDERBUFFER, glInternalFormat, renderTarget.width.toInt(), renderTarget.height.toInt());
    }

    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
  } else if (renderTarget.depthBuffer && renderTarget.stencilBuffer) {
    var samples = getRenderTargetSamples(renderTarget);
    if (isMultisample && useMultisampledRenderToTexture(renderTarget) == false) {
      var samples = getRenderTargetSamples(renderTarget);

      gl.renderbufferStorageMultisample(
          gl.RENDERBUFFER, samples, gl.DEPTH24_STENCIL8, renderTarget.width.toInt(), renderTarget.height.toInt());
    } else if (useMultisampledRenderToTexture(renderTarget)) {
      multisampledRenderToTextureExtension.renderbufferStorageMultisampleEXT(
          _gl.RENDERBUFFER, samples, _gl.DEPTH24_STENCIL8, renderTarget.width.toInt(), renderTarget.height.toInt());
    } else {
      gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height);
    }

    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
  } else {
    // Use the first texture for MRT so far
    var texture = renderTarget.isWebGLMultipleRenderTargets == true ? renderTarget.texture[0] : renderTarget.texture;

    var glFormat = utils.convert(texture.format);
    var glType = utils.convert(texture.type);
    var glInternalFormat = getInternalFormat(texture.internalFormat, glFormat, glType, texture.encoding);

    var samples = getRenderTargetSamples(renderTarget);

    if (isMultisample && useMultisampledRenderToTexture(renderTarget) == false) {
      gl.renderbufferStorageMultisample(
          gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height);
    } else if (useMultisampledRenderToTexture(renderTarget)) {
      multisampledRenderToTextureExtension.renderbufferStorageMultisampleEXT(
          _gl.RENDERBUFFER, samples, glInternalFormat, renderTarget.width, renderTarget.height);
    } else {
      gl.renderbufferStorage(gl.RENDERBUFFER, glInternalFormat, renderTarget.width, renderTarget.height);
    }
  }

  gl.bindRenderbuffer(gl.RENDERBUFFER, null);
}