【发布时间】:2020-10-10 04:51:02
【问题描述】:
我有内核函数,它必须将从 pixelBuffer(ARFrame.capturedImage) 创建的 Y 和 CbCr 纹理转换为 RGB 纹理,如苹果指南 https://developer.apple.com/documentation/arkit/displaying_an_ar_experience_with_metal 但我克服了明亮的纹理
kernel void renderTexture(texture2d<float, access::sample> capturedImageTextureY [[ texture(0) ]],
texture2d<float, access::sample> capturedImageTextureCbCr [[ texture(1) ]],
texture2d<float, access::read_write> outTextue [[texture(2)]],
uint2 size [[threads_per_grid]],
uint2 pid [[thread_position_in_grid]]){
constexpr sampler colorSampler(mip_filter::linear,
mag_filter::linear,
min_filter::linear);
const float4x4 ycbcrToRGBTransform = float4x4(
float4(+1.0000f, +1.0000f, +1.0000f, +0.0000f),
float4(+0.0000f, -0.3441f, +1.7720f, +0.0000f),
float4(+1.4020f, -0.7141f, +0.0000f, +0.0000f),
float4(-0.7010f, +0.5291f, -0.8860f, +1.0000f)
);
float2 texCoord;
texCoord.x = float(pid.x) / size.x;
texCoord.y = float(pid.y) / size.y;
// Sample Y and CbCr textures to get the YCbCr color at the given texture coordinate
float4 ycbcr = float4(capturedImageTextureY.sample(colorSampler, texCoord).r,
capturedImageTextureCbCr.sample(colorSampler, texCoord).rg, 1.0);
float4 color = ycbcrToRGBTransform * ycbcr;
outTextue.write(color, pid);
}
我使用以下代码创建 CGImage:
var cgImage: CGImage?
VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &cgImage)
cgImage 有正常的闪电
当我尝试使用 MTKTextureLoader 从 cgImage 创建纹理时,我也遇到了光照纹理
如何在 cgImage 中获得具有普通光的 MTLTexture
cgImage:(预期结果)
内核函数:
使用此代码创建纹理:
let descriptor = MTLTextureDescriptor()
descriptor.width = Int(Self.maxTextureSize.width)
descriptor.height = Int(Self.maxTextureSize.height)
descriptor.usage = [.shaderWrite, .shaderRead]
let texture = MTLCreateSystemDefaultDevice()?.makeTexture(descriptor: descriptor)
并使用内核函数写入像素。
已经尝试过MTLTextureDescriptor的不同pixelFormats
纹理加载器:
let textureLoader = MTKTextureLoader(device: MTLCreateSystemDefaultDevice()!)
let texturee = try! textureLoader.newTexture(cgImage: cgImage!, options: [.SRGB : (false as NSNumber)])
已经尝试过不同的 MTKTextureLoader.Options
GitHub 项目演示问题: PixelBufferToMTLTexture
【问题讨论】:
-
通过这些示例很难理解问题所在,尝试将最小项目上传到 GitHub。
-
你的 iPhone 型号是什么?
-
iPad Pro 第 4 代 12.9,Iphone XS
-
场景套件似乎为您的纹理添加了额外的伽马校正
-
您可以通过在着色器中添加伽马校正方程来解决此问题。
标签: swift textures metal metalkit