#include "mirage_util.slang"

struct ParamBuffer
{
    matrix transform;
};
ParameterBlock<ParamBuffer> param_buffer : register(b1);

struct PSInput {
    float4 position : SV_POSITION; // 裁剪空间坐标
    float4 color : COLOR; // 颜色
    float2 lineWidth : TEXCOORD0; // 线段宽度
    float2 uv : TEXCOORD1; // 纹理坐标
};

[shader("vertex")]
PSInput vertex_main(VSInput input)
{
    PSInput output;
    output.position = mul(float4(input.position, 0, 1.0), param_buffer.transform);
    output.color = input.color;
    output.lineWidth = input.param_a.xy;
    output.uv = input.uv;
    return output;
}

[shader("pixel")]
float4 pixel_main(PSInput input) : SV_TARGET
{
    // 计算线段宽度和矩形大小
    float2 lineWidth = input.lineWidth;
    float2 uv = input.uv;

    bool left = uv.x > lineWidth.x;
    bool right = uv.x < 1 - lineWidth.x;
    bool top = uv.y > lineWidth.y;
    bool bottom = uv.y < 1 - lineWidth.y;

    // 如果像素在矩形内部区域（同时在所有边界内），则丢弃它
    if (left && right && top && bottom)
    {
        discard;
    }
    return input.color;
}
