import '../../../../globals.js';
// Copyright 2023-2024, University of Colorado Boulder
/**
 * TODO: doc
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import { alpenglow, webgpu } from '../../imports.js';
let globalId = 1;
export default class ComputePass {
    constructor(encoder, computePassDescriptor) {
        this.id = globalId++;
        this.currentPipeline = null;
        this.currentBindGroups = new Map();
        this.computePassEncoder = webgpu.encoderBeginComputePass(encoder, computePassDescriptor);
    }
    prepare(computePipeline, bindGroups) {
        if (this.currentPipeline !== computePipeline) {
            webgpu.computePassEncoderSetPipeline(this.computePassEncoder, computePipeline.pipeline);
            this.currentPipeline = computePipeline;
        }
        for (let i = 0; i < bindGroups.length; i++) {
            const bindGroup = bindGroups[i];
            const currentBindGroup = this.currentBindGroups.get(i);
            if (currentBindGroup !== bindGroup) {
                webgpu.passEncoderSetBindGroup(this.computePassEncoder, i, bindGroup.bindGroup);
                this.currentBindGroups.set(i, bindGroup);
            }
        }
    }
    attemptLogBarrier(computePipeline) {
        if (computePipeline.logBarrierPipeline) {
            this.currentPipeline = null;
            webgpu.computePassEncoderSetPipeline(this.computePassEncoder, computePipeline.logBarrierPipeline);
            webgpu.computePassEncoderDispatchWorkgroups(this.computePassEncoder, 1, 1, 1);
        }
    }
    dispatchPipeline(computePipeline, bindGroups, dispatchX = 1, dispatchY = 1, dispatchZ = 1) {
        this.prepare(computePipeline, bindGroups);
        webgpu.computePassEncoderDispatchWorkgroups(this.computePassEncoder, dispatchX, dispatchY, dispatchZ);
        this.attemptLogBarrier(computePipeline);
        // allow chaining
        return this;
    }
    dispatchPipelineIndirect(computePipeline, bindGroups, indirectBuffer, indirectOffset) {
        this.prepare(computePipeline, bindGroups);
        webgpu.computePassEncoderDispatchWorkgroupsIndirect(this.computePassEncoder, indirectBuffer, indirectOffset);
        this.attemptLogBarrier(computePipeline);
        // allow chaining
        return this;
    }
    end() {
        webgpu.computePassEncoderEnd(this.computePassEncoder);
    }
}
alpenglow.register('ComputePass', ComputePass);
//# sourceMappingURL=ComputePass.js.map