// Compute Shader: INTER_AREA 图像缩小
Texture2D<float4> InputTexture : register(t0); // 输入纹理
RWTexture2D<float4> OutputTexture : register(u0); // 输出纹理

[numthreads(16, 16, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
   // 获取输出图像的尺寸
    uint outputWidth, outputHeight;
    OutputTexture.GetDimensions(outputWidth, outputHeight);

    // 获取输入图像的尺寸
    uint inputWidth, inputHeight;
    InputTexture.GetDimensions(inputWidth, inputHeight);

    // 计算缩放比例
    float scaleW = float(inputWidth) / float(outputWidth);
    float scaleH = float(inputHeight) / float(outputHeight);

    // 计算当前输出像素对应的输入区域
    float srcX = id.x * scaleW;
    float srcY = id.y * scaleH;
    float srcXEnd = (id.x + 1) * scaleW;
    float srcYEnd = (id.y + 1) * scaleH;

    // 计算区域的边界
    uint xStart = uint(srcX);
    uint yStart = uint(srcY);
    uint xEnd = uint(srcXEnd);
    uint yEnd = uint(srcYEnd);

    // 处理边界情况
    xStart = max(0, min(xStart, inputWidth - 1));
    yStart = max(0, min(yStart, inputHeight - 1));
    xEnd = max(0, min(xEnd, inputWidth - 1));
    yEnd = max(0, min(yEnd, inputHeight - 1));

    // 计算区域内的像素总和
    float4 sum = float4(0, 0, 0, 0);
    uint count = 0;

    for (uint yi = yStart; yi < yEnd; ++yi)
    {
        for (uint xi = xStart; xi < xEnd; ++xi)
        {
            sum += InputTexture[uint2(xi, yi)];
            count++;
        }
    }
     // 计算平均值
    float4 avg = count == 0 ? InputTexture[uint2(xStart, yStart)] : sum / float(count);
    // 写入输出像素
    OutputTexture[id.xy] = avg;
}