/**
 * Transfer bitmap contents to Sampler2D, effectively moving data to CPU, making it readable
 * @param {WebGL2RenderingContext} gl
 * @param {Sampler2D} sampler
 * @param {ImageBitmap|ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement} bitmap
 */
export function bitmap2sampler_gl(gl, sampler, bitmap) {
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
    gl.drawBuffers([gl.COLOR_ATTACHMENT0]);

    const w = sampler.width;
    const h = sampler.height;

    if (sampler.itemSize !== 4) {
        throw new Error(`Expected sampler.itemSize to be 4, instead was '${sampler.itemSize}'`);
    }

    /**
     *
     * @type {Uint8ClampedArray}
     */
    const target_data = sampler.data;

    gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, target_data);
}
