【问题标题】:What is the difference between [[textureCoordinates]] and [[position]] in Metal Shading LanguageMetal Shading Language 中的 [[textureCoordinates]] 和 [[position]] 有什么区别
【发布时间】:2023-12-25 00:49:01
【问题描述】:

我正在用金属和 swift 处理视频。我有一个顶点着色器,可以缩放帧以适应我的视图。在我的片段着色器中,如果我像这样使用纹理坐标进行采样,它会很好地工作: inputTexture.sample(sampler, inputFragment.textureCoordinates);

但如果我使用 read with position 代替,它会返回未缩放的帧: inputTexture.read(uint2(inputFragment.position.xy));

我假设这是通常的行为,我只是不知道为什么,所以我想知道是否有人可以解释两者之间的区别。谢谢!

【问题讨论】:

    标签: swift avfoundation metal


    【解决方案1】:

    textureCoordinates - 标准化纹理坐标

    对于 2D 纹理,归一化纹理坐标是从 0.0 到 在 x 和 y 方向上均为 1.0。 (0.0, 0.0) 的值指定纹理数据的第一个字节处的纹素(左上角) 图片)。 (1.0, 1.0) 的值指定最后一个字节的纹素 的纹理数据(图像的右下角)。


    position.xy - 顶点的剪辑空间位置。


    struct RasterizerData
    {
        // The [[position]] attribute qualifier of this member indicates this value is
        // the clip space position of the vertex when this structure is returned from
        // the vertex shader
        float4 position [[position]];
    
        // Since this member does not have a special attribute qualifier, the rasterizer
        // will interpolate its value with values of other vertices making up the triangle
        // and pass that interpolated value to the fragment shader for each fragment in
        // that triangle.
        float2 textureCoordinate;
    
    };
    

    【讨论】: