import '../../../../../globals.js';
// Copyright 2023-2024, University of Colorado Boulder
/**
 * A simple unrolled loop that provides both a "blocked" and "striped" (coalesced) index for each iteration.
 *
 * @author Jonathan Olson <jonathan.olson@colorado.edu>
 */
import { alpenglow, conditionalIfWGSL, LOCAL_INDEXABLE_DEFAULTS, OPTIONAL_LENGTH_EXPRESSIONABLE_DEFAULTS, u32S, unrollWGSL, wgsl, WORKGROUP_INDEXABLE_DEFAULTS } from '../../../imports.js';
import { optionize3 } from '../../../../../phet-core/js/optionize.js';
export const COALESCED_LOOP_DEFAULTS = {
    ...OPTIONAL_LENGTH_EXPRESSIONABLE_DEFAULTS, // eslint-disable-line phet/no-object-spread-on-non-literals
    ...WORKGROUP_INDEXABLE_DEFAULTS, // eslint-disable-line phet/no-object-spread-on-non-literals
    ...LOCAL_INDEXABLE_DEFAULTS // eslint-disable-line phet/no-object-spread-on-non-literals
};
const coalescedLoopWGSL = (providedOptions) => {
    const options = optionize3()({}, COALESCED_LOOP_DEFAULTS, providedOptions);
    const workgroupSize = options.workgroupSize;
    const grainSize = options.grainSize;
    const lengthExpression = options.lengthExpression;
    const callback = options.callback;
    return wgsl `
    ${unrollWGSL(0, options.grainSize, i => wgsl `
      {
        let coalesced_local_index = ${u32S(i * workgroupSize)} + ${options.localIndex};
        let coalesced_data_index = ${options.workgroupIndex} * ${u32S(workgroupSize * grainSize)} + coalesced_local_index;
        ${conditionalIfWGSL(lengthExpression ? wgsl `coalesced_data_index < ${lengthExpression}` : null, wgsl `
          ${callback(wgsl `coalesced_local_index`, wgsl `coalesced_data_index`)}
        `)}
      }
    `)}
  `;
};
export default coalescedLoopWGSL;
alpenglow.register('coalescedLoopWGSL', coalescedLoopWGSL);
//# sourceMappingURL=coalescedLoopWGSL.js.map