结果 MTLTexture 比 CGImage 轻

编程入门 行业动态 更新时间:2024-10-28 11:27:20
本文介绍了结果 MTLTexture 比 CGImage 轻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有内核函数,它必须将从 pixelBuffer(ARFrame.capturedImage) 创建的 Y 和 CbCr 纹理转换为 RGB 纹理,就像在苹果指南中一样

内核函数:

使用以下代码创建纹理:

let descriptor = MTLTextureDescriptor()descriptor.width = Int(Self.maxTextureSize.width)descriptor.height = Int(Self.maxTextureSize.height)descriptor.usage = [.shaderWrite, .shaderRead]让纹理 = MTLCreateSystemDefaultDevice()?.makeTexture(descriptor:descriptor)

并使用内核函数写入像素.

已经尝试过不同的 MTLTextureDescriptor pixelFormats

textureLoader:

let textureLoader = MTKTextureLoader(device: MTLCreateSystemDefaultDevice()!)让纹理=尝试!textureLoader.newTexture(cgImage: cgImage!, options: [.SRGB : (false as NSNumber)])

已经尝试过不同的 MTKTextureLoader.Options

GitHub 项目演示问题: PixelBufferToMTLTexture

解决方案

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

通过替换

outTextue.write(color, pid);

与:

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

I have kernel func which must convert Y and CbCr textures created from pixelBuffer(ARFrame.capturedImage) to RGB texture like in apple guide developer.apple/documentation/arkit/displaying_an_ar_experience_with_metal But I get over lighted texture

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);

}

I create CGImage with this code:

var cgImage: CGImage? VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &cgImage)

cgImage has normal lightning

when I try to create texture from cgImage with MTKTextureLoader I get over lighted texture too

How to get MTLTexture with normal light like in cgImage

cgImage: (expected result)

kernel func:

create texture with this code:

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)

and write pixels with kernel func.

already tried different pixelFormats of MTLTextureDescriptor

textureLoader:

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

already tried different MTKTextureLoader.Options

GitHub project demonstrating issue: PixelBufferToMTLTexture

解决方案

Problem was solved Thanks 0xBFE1A8, by adding gamma correction

by replacing

outTextue.write(color, pid);

with:

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

更多推荐

结果 MTLTexture 比 CGImage 轻

本文发布于:2023-07-30 16:25:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MTLTexture   CGImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!