gpt4 book ai didi

swift - 生成的 MTLTexture 比 CGImage 更轻

转载 作者:行者123 更新时间:2023-12-04 01:22:18 26 4
gpt4 key购买 nike

我有内核函数,它必须将从 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:(预期结果)

enter image description here

内核函数:

用这段代码创建纹理:

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 的不同像素格式

enter image description here

纹理加载器:

let textureLoader = MTKTextureLoader(device: MTLCreateSystemDefaultDevice()!)
let texturee = try! textureLoader.newTexture(cgImage: cgImage!, options: [.SRGB : (false as NSNumber)])

已经尝试过不同的 MTKTextureLoader.Options

enter image description here

GitHub 项目演示问题: PixelBufferToMTLTexture

最佳答案

问题已解决,感谢 0xBFE1A8,通过添加 Gamma 校正

通过替换

outTextue.write(color, pid);

与:

outTextue.write(float4(pow(color.rgb, float3(2,2,2)), color.a), pid);

关于swift - 生成的 MTLTexture 比 CGImage 更轻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62478638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com