setupRenderBufferStorage method

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

Implementation

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

  if (renderTarget.depthBuffer && !renderTarget.stencilBuffer) {
    dynamic glInternalFormat = WebGL.DEPTH_COMPONENT16;

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

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

      final samples = getRenderTargetSamples(renderTarget);

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

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

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

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

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

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

    final samples = getRenderTargetSamples(renderTarget);

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

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