#include "common.inc"

#define NORMAL_BUFFER 0
#define LIGHT_BUFFER 1
#define SPEC_BUFFER 2
#define DEPTH_BUFFER 3 
  
#ifdef VS

#if defined(USE_BINDING)
  layout( binding = VSCB1Index, std140 ) uniform VSCB1
#else
  layout( std140 ) uniform VSCB1
#endif
  {
    float2 topLeftPos;
    float2 scale;
    float bufferType;
    float3 padding;
  };
  
  layout ( location = 0 ) in float4 aPosition;
  out	float2 vUV;

  void main()
  {  
    float2 pos = aPosition.xy * scale.xy + topLeftPos.xy;
    gl_Position = float4( pos, 0.f, 1.f );
    vUV = aPosition.zw;
  }

#endif //VS

#ifdef PS

  layout ( location = 0 ) out float4 FragColor;

  in float2 vUV;

#if defined(USE_BINDING)
  layout( binding = 0 ) uniform sampler2D sTex;
#else
  uniform sampler2D sTex;
#endif



#if defined(USE_BINDING)
  layout( binding = PSCB1Index, std140 ) uniform PSCB1
#else
  layout (std140) uniform PSCB1
#endif
  {
    float2 topLeftPosPS;
    float2 scalePS;
    float bufferTypePS;
    float3 paddingPS;
  };

  void main()
  {
    float4 r = float4( 1.f, 0.f, 1.f, 1.f );
    
    if( bufferTypePS == NORMAL_BUFFER )
      r.xyz = texture( sTex, vUV ).xyz;
    else if( bufferTypePS == LIGHT_BUFFER )
      r.xyz = texture( sTex, vUV ).xyz;
    else if( bufferTypePS == SPEC_BUFFER )
      r.xyz = texture( sTex, vUV ).www;
    else if( bufferTypePS == DEPTH_BUFFER )
    {
      float2 encodedDepth = texture( sTex, vUV ).xy;
      float depth = dot( encodedDepth.xy, float2( 1.f, 1.f / 255.f ) );
      r.xyz = float3( depth, depth, depth );
    }

    FragColor = r;
  }

#endif //PS
