import { ShaderHelper } from '../material/shader/helper.js';
import '../material/lambert.js';
import '../material/blinn.js';
import '../material/unlit.js';
import '../material/particle.js';
import '@zephyr3d/device';
import '../values.js';
import '@zephyr3d/base';
import '../material/meshmaterial.js';
import '../material/grassmaterial.js';
import '../material/terrain-cm.js';
import '../material/pbrmr.js';
import '../material/pbrsg.js';
import '../material/pbrblueprint.js';
import '../material/sprite.js';
import '../material/sprite_std.js';
import '../utility/blueprint/material/texture.js';

class GlobalBindGroupAllocator {
    static _layouts = {};
    static _allocators = [];
    _bindGroups;
    constructor(){
        this._bindGroups = {};
    }
    static get() {
        return this._allocators.pop() ?? new GlobalBindGroupAllocator();
    }
    static release(allocator) {
        this._allocators.push(allocator);
    }
    /**
   * Allocate global bind group according to current draw context
   * @param ctx - Draw context
   * @returns Global bind group
   */ getGlobalBindGroup(ctx) {
        const hash = ctx.renderPassHash;
        let bindGroup = this._bindGroups[hash];
        if (!bindGroup) {
            let layout = GlobalBindGroupAllocator._layouts[hash];
            if (!layout) {
                const ret = ctx.device.programBuilder.buildRender({
                    vertex (pb) {
                        ShaderHelper.prepareVertexShader(pb, ctx);
                        pb.main(function() {});
                    },
                    fragment (pb) {
                        ShaderHelper.prepareFragmentShader(pb, ctx);
                        pb.main(function() {});
                    }
                });
                layout = ret[2][0];
                GlobalBindGroupAllocator._layouts[hash] = layout;
            }
            bindGroup = ctx.device.createBindGroup(layout);
            this._bindGroups[hash] = bindGroup;
        }
        return bindGroup;
    }
}

export { GlobalBindGroupAllocator };
//# sourceMappingURL=globalbindgroup_allocator.js.map
