//$c1 AWK 10/09/12 prj 14327 created

// this line is needed to get the associated xml files
#define SW_PARAMETER_FILES "global.xml, edgezbufferfixresult42.xml"
#define SW_INCLUDE "utils.glsl"



// screen position
varying vec2 v_texcoord;


#ifdef _VERTEX_PROGRAM
void main()
{
	gl_Position=vec4(gl_Vertex.xy,0.5,1.0);
	v_texcoord=gl_MultiTexCoord0.xy;
}
#endif //_VERTEX_PROGRAM


#ifdef _FRAGMENT_PROGRAM

//layout(r32ui, binding = 0) uniform uimage2D faceCompIDs;

bool checkEdge(uint ifaceID, uint icompID, vec2 uv)
{
	uvec2 val = texture(edgeZbufferFixFaceIDs42, uv).xy;
	uint sfaceID = val.x;
	uint scompID = val.y;
	if(sfaceID == 0)
		return true;

	if(ifaceID > sfaceID)
		return true;

	if(ifaceID == sfaceID && icompID > scompID)
		return true;

	return false;  
}


bool checkEdges(uint ifaceID, uint icompID, vec2 texcoord, vec2 offset)
{
	vec2 uv;
	uv.x = texcoord.x + offset.x;
	uv.y = texcoord.y;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.y = texcoord.y + offset.y;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.x = texcoord.x;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.x = texcoord.x - offset.x;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.y = texcoord.y;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.y = texcoord.y - offset.y;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.x = texcoord.x;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	uv.x = texcoord.x + offset.x;
	if(checkEdge(ifaceID, icompID, uv))
		return true;

	return false;  
}


void main()
{
	uvec2 val = texture(edgeZbufferFixFaceIDs42, v_texcoord).xy;
	uint faceID = val.x;
	uint compID = val.y;

	if(faceID == 0)
		discard;

	vec2 offset = vec2(edgeZBufferFixSampleOffsetX42,edgeZBufferFixSampleOffsetY42);

	if(checkEdges(faceID, compID, v_texcoord, offset) == false)
		discard;

	gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
#endif //_FRAGMENT_PROGRAM
