[require]
shader model < 4.0

[rtv 0]
format r32g32b32a32-float
size (2dms, 4, 640, 480)

[input layout]
0 r32g32-float POSITION

% The pixels in the second leftmost column lose two of their samples on the left, in addition to the center.
% x = (1 + 0.1 - 320) / 320 = -0.9965625.
[vb 0]
-0.9965625 1.0
3.0 1.0
-0.9965625-3.0

[vertex shader]
void main(float2 pos : POSITION, out float4 out_pos : SV_Position, out float2 t : TEXCOORD0)
{
    out_pos = float4(pos, 0.0f, 1.0f);
    t = float2(pos.x * 320.0f + 320.0f, -pos.y * 240.0f + 240.0f);
}

% Center interpolation.
[pixel shader]
static const float2 sample_pos = {-0.001f, 0.000f};

float4 main(float2 t : TEXCOORD0) : SV_Target
{
    float2 ref = round(t);
    bool first_col = ref.x == 1.0f;
    float2 diff = t.xy - ref;
    float2 expected = first_col ? sample_pos : 0.0f;
    float2 err = abs(diff - expected);
    return floor(1000.0f * float4(err, 0.0f, 0.0f));
}

[test]
clear rtv 0 1.0 1.0 1.0 1.0
draw triangle list 3
probe (1, 1) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 1) f32(0.0, 0.0, 0.0, 0.0)
probe (1, 478) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 479) f32(0.0, 0.0, 0.0, 0.0)

% Centroid interpolation.
[pixel shader]
static const float2 sample_pos = {0.375f, -0.125f};

float4 main(centroid float2 t : TEXCOORD0) : SV_Target
{
    float2 ref = round(t);
    bool first_col = ref.x == 1.0f;
    float2 diff = t.xy - ref;
    float2 expected = first_col ? sample_pos : 0.0f;
    float2 err = abs(diff - expected);
    return floor(500.0f * float4(err, 0.0f, 0.0f));
}

[test]
clear rtv 0 1.0 1.0 1.0 1.0
draw triangle list 3
probe (1, 1) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 1) f32(0.0, 0.0, 0.0, 0.0)
probe (1, 478) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 479) f32(0.0, 0.0, 0.0, 0.0)

[pixel shader]
static const float2 sample_pos = {0.375f, -0.125f};

float4 main(float2 t : TEXCOORD0_centRoid) : SV_Target
{
    float2 ref = round(t);
    bool first_col = ref.x == 1.0f;
    float2 diff = t.xy - ref;
    float2 expected = first_col ? sample_pos : 0.0f;
    float2 err = abs(diff - expected);
    return floor(500.0f * float4(err, 0.0f, 0.0f));
}

[test]
clear rtv 0 1.0 1.0 1.0 1.0
draw triangle list 3
probe (1, 1) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 1) f32(0.0, 0.0, 0.0, 0.0)
probe (1, 478) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 479) f32(0.0, 0.0, 0.0, 0.0)

[pixel shader]
static const float2 sample_pos = {0.375f, -0.125f};

struct ps_in
{
    float2 t : TEXCOORD0_centRoid;
};

float4 main(ps_in data) : SV_Target
{
    float2 ref = round(data.t);
    bool first_col = ref.x == 1.0f;
    float2 diff = data.t.xy - ref;
    float2 expected = first_col ? sample_pos : 0.0f;
    float2 err = abs(diff - expected);
    return floor(500.0f * float4(err, 0.0f, 0.0f));
}

[test]
clear rtv 0 1.0 1.0 1.0 1.0
draw triangle list 3
probe (1, 1) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 1) f32(0.0, 0.0, 0.0, 0.0)
probe (1, 478) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 479) f32(0.0, 0.0, 0.0, 0.0)

% The "_centroid" semantic modifier can be specified together with the "centroid" prefix attribute.
[pixel shader]
static const float2 sample_pos = {0.375f, -0.125f};

float4 main(centroid float2 t : TEXCOORD0_centroid) : SV_Target
{
    float2 ref = round(t);
    bool first_col = ref.x == 1.0f;
    float2 diff = t.xy - ref;
    float2 expected = first_col ? sample_pos : 0.0f;
    float2 err = abs(diff - expected);
    return floor(500.0f * float4(err, 0.0f, 0.0f));
}

[test]
clear rtv 0 1.0 1.0 1.0 1.0
draw triangle list 3
probe (1, 1) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 1) f32(0.0, 0.0, 0.0, 0.0)
probe (1, 478) f32(0.5, 0.5, 0.5, 0.5)
probe (638, 479) f32(0.0, 0.0, 0.0, 0.0)

% The semantic name is this case is "TEXCOORD_centroid", which is not valid for SM < 4.
[pixel shader fail]
float4 main(float2 t : TEXCOORD_centroid0) : SV_Target
{
    return t.x;
}

% The semantic name is this case is "TEXCOORD0_centroid" as well, which is not valid for SM < 4.
[pixel shader fail]
float4 main(float2 t : TEXCOORD0_centroid_centroid) : SV_Target
{
    return t.x;
}

% The centroid mode can only be applied to texcoord registers in SM2,
% and cannot be applied to any semantic registers in SM3.
[pixel shader fail(sm<3)]
float4 main(centroid float2 t : COLOR) : SV_Target
{
    return t.x;
}

% The following semantics are invalid in SM2.

[require]
shader model >= 3.0
shader model < 4.0

[pixel shader fail]
float4 main(centroid float t : VFACE) : SV_Target
{
    return t;
}

[pixel shader fail]
float4 main(centroid float3 t : VPOS) : SV_Target
{
    return t.x;
}
