首页 > 解决方案 > 内核中的透明图像 Alpha 问题

问题描述

我正在阅读透明纹理,其中 alpha 因位置而异。我正在将其写入内核函数中的另一个透明纹理。类型为 BGRA8。当我写的时候,白色代替了 alpha 为 0 的地方。

我添加了一个条件,例如当我返回源图像 alpha 零时。在那段时间,我在所需的纹理周围有一个白色边框。

怎么解决 ?

可能是什么问题。

kernel void computeTool(constant float4 *color [[buffer(0)]],
                        constant float2 *point [[buffer(1)]],
                        constant int &pointCount [[buffer(2)]],
                        texture2d<float,access::read_write> des [[texture(0)]],
                        texture2d<float,access::read> src [[texture(1)]],
                        uint2 gid [[thread_position_in_grid]])
{
    for (int i = 0; i < pointCount; ++i) {

        float2 x =    touchPointF(point[i])  ;


        if(gid.x > x.x  && gid.x < x.x + 150 && gid.y > x.y  && gid.y < x.y + 150){
            float2 ss = float2(gid)- float2(x);
            uint2 ssx = uint2(ss);

            float4 srcColor = src.read(ssx) * float4(1.0,0.0,0.0,1.0);

            des.write(srcColor, gid);


        }

    }
}

Src 纹理创建

public func imageToTexture(imageNamed: String, device: MTLDevice) -> MTLTexture {

    let image = UIImage(named: imageNamed)!

    var pixelBuffer: CVPixelBuffer?

    let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
                 kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue,
                 kCVPixelBufferMetalCompatibilityKey: kCFBooleanTrue]

    _ = UIScreen.main.scale

    let width = Int(image.size.width)
    let height = Int(image.size.height)

    var status = CVPixelBufferCreate(nil, width, height,
                                     kCVPixelFormatType_32BGRA, attrs as CFDictionary,
                                     &pixelBuffer)
    assert(status == noErr)

    let coreImage = CIImage(image: image)!
    let context = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!)
    context.render(coreImage, to: pixelBuffer!)

    var textureWrapper: CVMetalTexture?
    var textureCache:CVMetalTextureCache?

    _ = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device, nil, &textureCache)

    status = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
                                                       textureCache!, pixelBuffer!, nil, .bgra8Unorm,
                                                       CVPixelBufferGetWidth(pixelBuffer!), CVPixelBufferGetHeight(pixelBuffer!),
                                                       0,
                                                       &textureWrapper)


    let texture = CVMetalTextureGetTexture(textureWrapper!)!

    // use texture now for your Metal texture. the texture is now map-bound to the CVPixelBuffer's underlying memory.

    return texture
}

我正在写作的目的地纹理

let textureDescriptors = MTLTextureDescriptor()
textureDescriptors.textureType = MTLTextureType.type2D
let screenRatio = UIScreen.main.scale
 textureDescriptors.width = Int((DrawingManager.shared.size?.width)!) * Int(screenRatio)
 textureDescriptors.height = Int((DrawingManager.shared.size?.height)!) * Int(screenRatio)

textureDescriptors.pixelFormat = .bgra8Unorm
textureDescriptors.storageMode = .private
textureDescriptors.usage = [.renderTarget, .shaderRead]
ssTexture = device.makeTexture(descriptor: textureDescriptors)

标签: iosswiftmetalmetalkit

解决方案


推荐阅读