precision highp float;

varying vec4 fragment_position;

void main()
{
  // First, shift depth value to positive.
  float value = fragment_position.z;
  value = (value + 1.0) / 2.0;

  // TODO: 2 components may be enough.
  const vec4 split = vec4(256.0 * 256.0, 256.0, 1, 0);
  const vec4 mask = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);

  vec4 value_split = split * value;
  vec4 value_fraction = fract(value_split);
  vec4 fraction_masked = value_fraction.xxyz * mask;
  vec4 compressed = value_fraction - fraction_masked;

  gl_FragColor = vec4(compressed.xyz, 1.0);
}