#version 430 core

layout(binding = 0) uniform usampler2D nearStencil;
layout(r32ui, binding = 0) uniform uimage2D fragmentOffset;
layout(rgba16f, binding = 1) uniform writeonly image2D fragmentNormal;
layout(r32f, binding = 2) uniform writeonly image2D fragmentDepth;
layout(r16ui, binding = 3) uniform writeonly uimage2D fragmentStencil;

layout(pixel_center_integer) in vec4 gl_FragCoord;

uniform uint pitch;
uniform uint alignment;

#define REDUCE_FACTOR $REDUCE_FACTOR$

void main()
{
  uint stencil = texelFetch(nearStencil, ivec2(gl_FragCoord.xy), 0).x;

  if (stencil != 0u)
  {
    uint index = imageAtomicAdd(fragmentOffset, ivec2(gl_FragCoord.xy), 1) - alignment;
    ivec2 pos = ivec2(index % (pitch / REDUCE_FACTOR), index / (pitch / REDUCE_FACTOR)) * REDUCE_FACTOR + ivec2(gl_FragCoord.xy) % REDUCE_FACTOR;
    imageStore(fragmentNormal, pos, vec4(0.0, 0.0, 1.0, 0.0));
    imageStore(fragmentDepth, pos, vec4(0.0));
    imageStore(fragmentStencil, pos, uvec4(stencil));
  }
}
