本文介绍了结果 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 https://developer.apple.com/documentation/arkit/displaying_an_ar_experience_with_metalBut 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 轻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:15