import { make_stratified_sample } from "./make_stratified_sample.js";

/**
 * Based on https://github.com/TheRealMJP/DXRPathTracer/blob/e01b893c76ad629fa6e715d35b98901f5195d000/SampleFramework12/v1.02/Graphics/Sampling.cpp#L440
 * @param {number[]} result
 * @param {number} result_offset
 * @param {number} num_samples_x
 * @param {number} num_samples_y
 * @param {function():number} random
 */
export function generate_stratified_samples_2d(result, result_offset, num_samples_x, num_samples_y, random) {

    const delta_x = 1 / num_samples_x;
    const delta_y = 1 / num_samples_y;

    let sample_index = 0;
    for (let y = 0; y < num_samples_y; y++) {
        for (let x = 0; x < num_samples_x; x++) {

            const address = result_offset + sample_index * 2;

            result[address] = make_stratified_sample(x, delta_x, random);
            result[address + 1] = make_stratified_sample(y, delta_y, random);

            ++sample_index;
        }
    }

    return sample_index;
}

