#version 430 core
////////////////////////////////////////////////////////////////////////////////
// <copyright>
// Copyright (c) 2015 Hue AS. All rights reserved.
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, electronic, mechanical or otherwise, is prohibited
// without the prior written permission of the copyright owner.
// </copyright>
////////////////////////////////////////////////////////////////////////////////

#define HALF_W $HALF_COMPUTE_WIDTH$
#define H $COMPUTE_HEIGHT$

layout(binding=0) uniform usampler2D sums;
layout(r32ui, binding=0) restrict uniform uimage2D offsets;

layout(local_size_x = HALF_W, local_size_y = H) in;

void main() {
    ivec2 imgSize = imageSize(offsets);
    ivec2 imgPos1 = ivec2(2 * gl_GlobalInvocationID.x, gl_GlobalInvocationID.y);
    ivec2 imgPos2 = ivec2(2 * gl_GlobalInvocationID.x + 1, gl_GlobalInvocationID.y);
    uint sum = texelFetch(sums, ivec2(gl_WorkGroupID.xy), 0).x;
    if (all(lessThan(imgPos1, imgSize)))
        imageAtomicAdd(offsets, imgPos1, sum);
    if (all(lessThan(imgPos2, imgSize)))
        imageAtomicAdd(offsets, imgPos2, sum);
}
