import { drawFullscreenQuad } from '../render/fullscreenquad.js';
import { copyTexture, fetchSampler } from '../utility/misc.js';
import { Disposable } from '@zephyr3d/base';

/**
 * Rendering layer of post processing effects
 * @public
 *
 */ var PostEffectLayer = /*#__PURE__*/ function(PostEffectLayer) {
    PostEffectLayer[PostEffectLayer["opaque"] = 0] = "opaque";
    PostEffectLayer[PostEffectLayer["transparent"] = 1] = "transparent";
    PostEffectLayer[PostEffectLayer["end"] = 2] = "end";
    return PostEffectLayer;
}({});
/**
 * Base class for any type of post effect
 * @public
 */ class AbstractPostEffect extends Disposable {
    static _defaultRenderStates = {};
    _enabled;
    _layer;
    /**
   * Creates an instance of a post effect
   * @param name - Name of the post effect
   */ constructor(){
        super();
        this._enabled = true;
        this._layer = 2;
    }
    /** Whether this post effect is enabled */ get enabled() {
        return this._enabled;
    }
    set enabled(val) {
        this._enabled = !!val;
    }
    /** Whether this post effect will be rendered at opaque phase */ get layer() {
        return this._layer;
    }
    /**
   * Check if the post effect should be rendered upside down.
   * @param device - The device object
   * @returns true if the post effect should be rendered upside down
   */ needFlip(device) {
        return device.type === 'webgpu' && !!device.getFramebuffer();
    }
    /**
   * Checks whether this post effect requires the linear depth texture
   * @returns true if the linear depth texture is required.
   */ requireLinearDepthTexture(_ctx) {
        return false;
    }
    /**
   * Checks whether this post effect requires the scene depth buffer
   * @returns true if the scene depth buffer is required.
   */ requireDepthAttachment(_ctx) {
        return false;
    }
    /**
   * Apply the post effect
   * @param camera - Camera used the render the scene
   * @param inputColorTexture - The previous scene color texture
   * @param sceneDepthTexture - The linear scene depth texture
   * @param srgbOutput - Whether the result should be gamma corrected
   *
   * @remarks
   * The frame buffer of the post effect is already set when apply() is called.
   */ apply(ctx, inputColorTexture, sceneDepthTexture, srgbOutput) {
        this.passThrough(ctx, inputColorTexture, srgbOutput);
    }
    /**
   *
   * @param ctx - Draw context
   * @param inputColorTexture - Input color texture
   * @param srgbOutput - Whether the result should be gamma corrected
   */ passThrough(ctx, inputColorTexture, srgbOutput, renderStates) {
        copyTexture(inputColorTexture, ctx.device.getFramebuffer(), fetchSampler('clamp_nearest_nomip'), renderStates, 0, srgbOutput);
    }
    /**
   * Draws a fullscreen quad
   * @param renderStateSet - Render states that will be used when drawing the fullscreen quad.
   */ drawFullscreenQuad(renderStateSet) {
        drawFullscreenQuad(renderStateSet);
    }
    /** @internal */ createVertexLayout(device) {
        return device.createVertexLayout({
            vertexBuffers: [
                {
                    buffer: device.createVertexBuffer('position_f32x2', new Float32Array([
                        -1,
                        -1,
                        1,
                        -1,
                        -1,
                        1,
                        1,
                        1
                    ]))
                }
            ]
        });
    }
    onDispose() {
        super.onDispose();
        this.destroy();
    }
    /** @internal */ createRenderStates(device) {
        const renderStates = device.createRenderStateSet();
        renderStates.useRasterizerState().setCullMode('none');
        renderStates.useDepthState().enableTest(false).enableWrite(false);
        return renderStates;
    }
    /** @internal */ destroy() {}
    /** @internal */ static getDefaultRenderState(ctx, compareFunc) {
        let renderState = this._defaultRenderStates[compareFunc];
        if (!renderState) {
            renderState = ctx.device.createRenderStateSet();
            renderState.useRasterizerState().setCullMode('none');
            renderState.useDepthState().enableTest(compareFunc !== 'always').enableWrite(false).setCompareFunc(compareFunc);
            this._defaultRenderStates[compareFunc] = renderState;
        }
        return renderState;
    }
}

export { AbstractPostEffect, PostEffectLayer };
//# sourceMappingURL=posteffect.js.map
