#version 430

layout(binding=0, r32f) uniform image2D pardexTransmit;

uniform float loop = 0.0;
uniform float wkw = 0.0;

layout(location = 0) out vec4 frag;

//ivec2 getTrip(int idt) {
//    ivec2 triBufSize = imageSize(pardexBucketPrefix);
//    ivec2 trip;
//    trip.y = idt/triBufSize.x;
//    trip.x = idt-trip.y*triBufSize.x;
//   // trip.y = 0;
//    return trip;
//}

int getInd(ivec2 uvi) {
    return uvi.x+uvi.y*int(wkw);
}

uniform float baseDim = 16.0;

// get 2D value from integer 3D
ivec2 get2D(ivec3 p3d) {

    int baseDimi = int(baseDim);
    int baseDimi2 = baseDimi*baseDimi;

    int zLevel = clamp(p3d.z, 0, baseDimi2-1);
    int zLevelY = zLevel/baseDimi;
    int zLevelX = zLevel-zLevelY*baseDimi;
    ivec2 zLevel2 = ivec2(zLevelX, zLevelY)*baseDimi2;
    ivec2 tdp = p3d.xy/baseDimi2;
    p3d.xy -= tdp*baseDimi2;
    p3d.xy = clamp(p3d.xy, 0, baseDimi2-1);
    return (p3d.xy+zLevel2);
}

void main() {
    ivec2 uv = ivec2(gl_FragCoord.xy);

    float sum = 1.0;
    for (int pz=0; pz<int(baseDim*baseDim); pz++) {
        ivec2 p2d = get2D(ivec3(gl_FragCoord.xy, pz));
        sum *= imageLoad(pardexTransmit, p2d).x;
        if (pz>0) {
            imageStore(pardexTransmit, p2d, vec4(sum));
        }
    }


    return;
    discard;
}
