【发布时间】: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