【问题标题】:why sending RGBA data in a BGRA pipelineDescriptor work?为什么在 BGRA pipelineDescriptor 中发送 RGBA 数据有效?
【发布时间】:2020-02-14 08:10:17
【问题描述】:

我的 MTKView 在 BGRA 中,我在 BGRA 中设置了我的 pipelineDescriptor,如下所示:

pipelineDescriptor.colorAttachments.objectAtIndexedSubscript(0).setPixelFormat(MTLPixelFormatBGRA8Unorm);

现在的问题是,如果我发送以 RGBA 编码的数据(我的意思是颜色是以 R(4bytes)+G(4bytes)+B(4bytes)+A(4bytes)) 到下面的着色器就可以了!

RenderCommandEncoder.setVertexBuffer(LvertexBuffer{buffer}, 0{offset}, 0{atIndex})

#include <metal_stdlib>                                                                                         
using namespace metal;                                                                                          

#include <simd/simd.h>                                                                                          

struct VertexIn {                                                                                               
    vector_float4 color;                                                                                        
    vector_float2 pos;                                                                                          
};                                                                                                              

struct VertexOut {                                                                                              
    float4 color;                                                                                               
    float4 pos [[position]];                                                                                    
};                                                                                                              

vertex VertexOut vertexShader(                                                                                  
                              const device VertexIn *vertexArray [[buffer(0)]],                                 
                              unsigned int vid [[vertex_id]],                                                    
                              constant vector_uint2 *viewportSizePointer [[buffer(1)]] 
                             )                                                                                  
{                                                                                                               
    // Get the data for the current vertex.                                                                     
    VertexIn in = vertexArray[vid];                                                                             
    VertexOut out;

    ...  
    out.color = in.color;                                                                                       
    ....                                                                                                                                                                                                                                     
    return out;                                                                                                 
}                                                                                                               

fragment float4 fragmentShader(                                                                                 
                                VertexOut interpolated [[stage_in]],                                             
                                texture2d<half> colorTexture [[ texture(0) ]]                                             
                              )                                                                                 
{                                                                                                               
    return interpolated.color;                                                                                  
}

这怎么可能?是关于小端和大端的吗?

【问题讨论】:

  • 如果您将着色器定义为使用字节根据 BGRA 工作,那么您提交定义为 XRGB 的 32 位值,其中位 0->7 定义 B 值。然后将这些 8 位值转换为浮点数,但它们的精度不会神奇地增长,因此您可以使用半精度并避免浮点数。我建议您从一个已经有效的解决方案开始,并在此基础上进行构建。

标签: ios objective-c endianness metal metalkit


【解决方案1】:

无论渲染目标的像素格式如何,从片段函数返回的颜色都假定为 RGBA 顺序。它们会根据需要进行混合和/或转换以匹配目的地的格式。采样/写入其他纹理时也会发生同样的情况:颜色总是以 RGBA 顺序到达。

【讨论】:

  • 所以你的意思是在 vertexShader & fragmentShader 颜色总是按 RGBA 顺序排列?这样通过 pipelineDescriptor.colorAttachments 定义像素格式的目的是什么?
  • 渲染管线状态必须知道目标像素格式,这样它才能知道如何混合和转换片段函数返回的值。
猜你喜欢
  • 2023-02-16
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 2023-02-01
相关资源
最近更新 更多