内核中的透明图像Alpha问题

时间:2019-01-04 08:06:44

标签: ios swift metal metalkit

我正在阅读透明的纹理,其中alpha的位置不同。我将其写入内核函数中的另一个透明纹理。类型是BGRA8。在我写书时,白色替换了alpha为0的地方。

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

如何解决?

可能是问题所在。

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)

0 个答案:

没有答案