#include "common.hlsli"

struct v2p
{
    float4 factor : COLOR0;
    float3 p : TEXCOORD1;

    float4 hpos_curr : TEXCOORD2;
    float4 hpos_old : TEXCOORD3;

    float4 hpos : SV_POSITION;
};

TextureCube s_sky0 : register(t0);
TextureCube s_sky1 : register(t1);

struct sky
{
    float4 Color : SV_Target0;
    float2 Velocity : SV_Target1;
};

void main(in v2p I, out sky O)
{
	float3 TexCoord = I.p;
	
#ifndef USE_FULL_SKY_SPHERE
    RemapVector(TexCoord);
#endif

	float3 s0 = s_sky0.SampleLevel(smp_rtlinear, TexCoord, 0.0f).xyz;
	float3 s1 = s_sky1.SampleLevel(smp_rtlinear, TexCoord, 0.0f).xyz;
	float3 sky = lerp(s0, s1, I.factor.w);
    
#ifdef USE_BGRA_SKYCOLOR
    sky *= L_sky_color.zyx;
#else
    sky *= L_sky_color.xyz;
#endif

#ifdef USE_LEGACY_SKY_TONEMAP
	O.Color = float4(detonemap(sky * 0.66f), 0.0f);
#else
	O.Color = float4(PushGamma(sky), 0.0f);
#endif

	O.Velocity = I.hpos_curr.xy / I.hpos_curr.w - I.hpos_old.xy / I.hpos_old.w;
}

