如何以bgra8Unorm像素格式绘制MTLTexture [英] How to draw on MTLTexture in bgra8Unorm pixel format
问题描述
当我使用 rgba32Float
像素格式的 MTLTexture
绘制代码时,我的代码可以正常工作,然后可以从其中删除 CVPixelBuffer
.
但是 FlutterTexture
需要 bgra8Unorm
格式.由于性能开销,我不想转换 CVPixelBuffer
.
因此,我尝试使用 bgra8Unorm
像素格式在 MTLTexture
上进行渲染,但是以下片段着色器代码无法编译:
片段vector_uchar4fragmentShader2(顶点内插[[stage_in]]){返回0xFFFFFFFF;}
有错误:片段函数的无效返回类型'vector_uchar4'
我尝试将其替换为 uint
类型,但是由于崩溃而崩溃:
致命错误:尝试!"表达式意外引发错误:错误域= AGXMetalA11代码= 3"uint类型的输出与MTLPixelFormatBGRA8Unorm颜色附件不兼容."UserInfo = {NSLocalizedDescription = uint类型的输出与MTLPixelFormatBGRA8Unorm颜色附件不兼容.}
如果我使用 vector_float4
或 vector_half4
返回类型,则我的纹理和缓冲区为空.
我必须将哪种返回类型用于 bgra8Unorm
像素格式并获取非空图像?完全可以用金属吗?
我在
最后,此代码按预期绘制图像:
片段float4fragmentShader2(顶点内插[[stage_in]]){//...rgba8unorm< float4>rgba;rgba = float4(color.r,color.g,color.b,1.0);返回rgba;}
如果有人可以解释幕后发生的事情,我真的很想不浪费金钱.
My code works when I draw on MTLTexture
with rgba32Float
pixel format, I can take then CVPixelBuffer
out of it.
But FlutterTexture
requires bgra8Unorm
format. I do not want to convert CVPixelBuffer
due to performance overhead.
So I'm trying to render on MTLTexture
with bgra8Unorm
pixel format, but the following fragment shader code won't compile:
fragment vector_uchar4 fragmentShader2(Vertex interpolated [[stage_in]]) {
return 0xFFFFFFFF;
}
With error: Invalid return type 'vector_uchar4' for fragment function
I've tried to replace it with uint
type, but it crashes with error:
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.}
If I use vector_float4
or vector_half4
return type my texture and buffers are empty.
Which return type I have to use for bgra8Unorm
pixel format and get non empty image? Is it possible with metal at all?
I've found answer on page 30 of Metal Shading Language specification
And finally this code draws image as expected:
fragment float4 fragmentShader2(Vertex interpolated [[stage_in]]) {
// ...
rgba8unorm<float4> rgba;
rgba = float4(color.r, color.g, color.b, 1.0);
return rgba;
}
If someone can explain what is happening under the hood, I would really like to not waste bounty.
这篇关于如何以bgra8Unorm像素格式绘制MTLTexture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!