import { WEBGLRenderbuffer, withGLParameters } from '@luma.gl/webgl';
import { default as LayersPass } from './layers-pass';
export default class ShadowPass extends LayersPass {
  constructor(device, props) {
    super(device, props);
    this.shadowMap = void 0;
    this.depthBuffer = void 0;
    this.fbo = void 0;
    this.shadowMap = device.createTexture({
      width: 1,
      height: 1,
      sampler: {
        minFilter: 'linear',
        magFilter: 'linear',
        addressModeU: 'clamp-to-edge',
        addressModeV: 'clamp-to-edge'
      }
    });
    this.depthBuffer = new WEBGLRenderbuffer(device, {
      format: 'depth16unorm',
      width: 1,
      height: 1
    });
    this.fbo = device.createFramebuffer({
      id: 'shadowmap',
      width: 1,
      height: 1,
      colorAttachments: [this.shadowMap],
      depthStencilAttachment: this.depthBuffer
    });
  }

  render(params) {
    const target = this.fbo;
    withGLParameters(this.device, {
      depthRange: [0, 1],
      depthTest: true,
      blend: false,
      clearColor: [1, 1, 1, 1]
    }, () => {
      const pixelRatio = this.device.canvasContext.cssToDeviceRatio();
      const viewport = params.viewports[0];
      const width = viewport.width * pixelRatio;
      const height = viewport.height * pixelRatio;

      if (width !== target.width || height !== target.height) {
        target.resize({
          width,
          height
        });
      }

      super.render({ ...params,
        target,
        pass: 'shadow'
      });
    });
  }

  shouldDrawLayer(layer) {
    return layer.props.shadowEnabled !== false;
  }

  getModuleParameters() {
    return {
      drawToShadowMap: true
    };
  }

  delete() {
    if (this.fbo) {
      this.fbo.destroy();
      this.fbo = null;
    }

    if (this.shadowMap) {
      this.shadowMap.destroy();
      this.shadowMap = null;
    }

    if (this.depthBuffer) {
      this.depthBuffer.destroy();
      this.depthBuffer = null;
    }
  }

}
//# sourceMappingURL=shadow-pass.js.map