texture g_texDepth;
sampler2D g_sDepth = sampler_state
{
    Texture = <g_texDepth>;
    AddressU = Clamp;
    AddressV = Clamp;
    MipFilter = NONE;
    MagFilter = POINT;
    MinFilter = POINT;
};

texture g_texWorld;
sampler2D g_sWorld = sampler_state
{
    Texture = <g_texWorld>;
    AddressU = Clamp;
    AddressV = Clamp;
    MipFilter = NONE;
    MagFilter = POINT;
    MinFilter = POINT;
};

#define WPT_DEPTH 0
#define WPT_LINEARDEPTH 1
#define WPT_WORLDPOS 2

float3 GetFragmentWorldPos(float2 fragcoord, int wpt)
{
	float3 wpos;

	if (wpt == WPT_LINEARDEPTH)
	{
		float depth = tex2D(g_sDepth, fragcoord).r;
		//float3 eyepoint = depth * ViewVec.xyz;

		float2 v = mul(fragcoord * float2(2, -2) - float2(1, -1) + float2(-1, 1) / g_RTSize, g_matInvProj);
		
		float4 vwpos = mul(float4(depth * float3(v , 1), 1), g_matInvView);

		wpos = vwpos.xyz;

		//return float4(wpos.xxx / 2000, 1);
		//return depth / 2000;
		//return float4(ViewVec + 0.5, 1);
	}
	else if (wpt == WPT_DEPTH)
	{
		float depth = tex2D(g_sDepth, fragcoord).r;
		float4 vp = float4(fragcoord * float2(2, -2) - float2(1, -1) + float2(-1, 1) / g_RTSize, depth, 1);
		////float4 vcam = mul(vp, matInvProj);
		////vcam.xyz /= vcam.w;
		float4 v = mul(vp, g_matInvViewProj);
		wpos = v.xyz/v.w;
	}
	else if (wpt == WPT_WORLDPOS)
	{
		wpos = tex2D(g_sWorld, fragcoord).rgb;
	}

	return wpos;
}