【问题标题】:How to draw on MTLTexture in bgra8Unorm pixel format如何以 bgra8Unorm 像素格式在 MTLTexture 上绘图
【发布时间】:2021-06-09 05:27:57
【问题描述】:

当我使用rgba32Float 像素格式绘制MTLTexture 时,我的代码可以工作,然后我可以从中取出CVPixelBuffer

但是FlutterTexture 需要bgra8Unorm 格式。由于性能开销,我不想转换 CVPixelBuffer

所以我尝试使用bgra8Unorm 像素格式在MTLTexture 上进行渲染,但以下片段着色器代码无法编译:

fragment vector_uchar4 fragmentShader2(Vertex interpolated [[stage_in]]) {
    return 0xFFFFFFFF;
}

出现错误:Invalid return type 'vector_uchar4' for fragment function 我尝试将其替换为 uint 类型,但它会因错误而崩溃:

Fatal error: 'try!' expression unexpectedly raised an error:
Error Domain=AGXMetalA11 Code=3 
"output of type uint is not compatible with a MTLPixelFormatBGRA8Unorm color attachement."
UserInfo={NSLocalizedDescription=output of type uint is not compatible with a MTLPixelFormatBGRA8Unorm color attachement.}

如果我使用vector_float4vector_half4 返回类型,我的纹理和缓冲区都是空的。

对于bgra8Unorm 像素格式,我必须使用哪种返回类型并获得非空图像?金属有可能吗?

【问题讨论】:

    标签: ios swift flutter metal metalkit


    【解决方案1】:

    我在Metal Shading Language specification的第30页找到了答案

    最后这段代码按预期绘制图像:

    fragment float4 fragmentShader2(Vertex interpolated [[stage_in]]) {
        // ...
        rgba8unorm<float4> rgba;
        rgba = float4(color.r, color.g, color.b, 1.0);
        return rgba;
    }
    

    如果有人可以解释幕后发生的事情,我真的不想浪费赏金。

    【讨论】:

      【解决方案2】:

      这取决于许多不同的因素。在大多数情况下,您应该使用float4half4

      所有支持金属的现代 Apple GPU,旨在对(32 位或 64 位)浮点数据执行计算。这就是 GPU 的工作方式,这意味着着色器对 FloatSnormUnorm 格式计算的任何读取操作都将在 32 位或 64 位浮点上执行,无论原始输入格式如何。 在任何写入操作上,着色器都会执行从 32 位或 64 位浮点到目标格式的转换。
      转换规则见Metal Shading Language specification217页

      任何使用FloatSnormUnorm 后缀的金属格式都是浮点格式,而UintSint 是无符号和有符号整数。

      Float - 任何由 metal 定义的表示形式的浮点值。
      Unorm - [0.0, 1.0] 范围内的浮点值。
      Snorm - [-1.0, 1.0] 范围内的浮点值。
      Uint - 一个无符号整数。
      Sint - 有符号整数。

      【讨论】:

      • 它取决于哪些因素?我在哪里可以读到它们? float4 也似乎不正确,因为它是 32 字节而不是单字节
      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 2013-09-26
      • 2014-04-21
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多