Writing 0-Alpha in Metal shader doesn't render correctly

Multi tool use
Multi tool use


Writing 0-Alpha in Metal shader doesn't render correctly



I'm trying to Chroma-Key an mp4 video playing over a UIView.



I'm getting a texture every frame like so:


var fullTex:CVMetalTexture?
let w = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
let h = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);

CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
videoTextureCache!,
pixelBuffer,
nil,
MTLPixelFormat.bgra8Unorm,
w, h, 0,
&fullTex)
let fullTexture:MTLTexture? = CVMetalTextureGetTexture(fullTex!)



Passing it to my shader like this:


let commandBuffer = commandQueue.makeCommandBuffer()!
let commandEncoder = commandBuffer.makeComputeCommandEncoder()!
commandEncoder.setComputePipelineState(pipelineState)
commandEncoder.setTexture(fullTexture, index: 0)
commandEncoder.setTexture(fullTexture, index: 1)
commandEncoder.dispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup)
commandEncoder.endEncoding()
commandBuffer.commit();
commandBuffer.waitUntilCompleted()

// This proves the shader created transparency, but it doesn't render as such on screen
let i = UIImage(pixelBuffer: pixelBuffer, context: CIContext())

//This writes to a AVSampleBufferDisplayLayer
self.displayPixelBuffer(pixelBuffer: pixelBuffer, atTime: outputItemTime)



The shader looks like this:


kernel void GreenScreen(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]]) {

float4 color = inTexture.read(gid);
float4 combinedColor = float4(color.rgb,1.0);
if (color.r < 0.5)
combinedColor = float4(0.0,0,0,0);

outTexture.write(combinedColor, gid);
}



Display like so:


func displayPixelBuffer(pixelBuffer: CVPixelBuffer, atTime outputTime: CMTime) {
var err: OSStatus = noErr

if videoInfo == nil || false == CMVideoFormatDescriptionMatchesImageBuffer(videoInfo!, pixelBuffer) {
if videoInfo != nil {
videoInfo = nil
}
err = CMVideoFormatDescriptionCreateForImageBuffer(nil, pixelBuffer, &videoInfo)
if (err != noErr) {
DLog("Error at CMVideoFormatDescriptionCreateForImageBuffer (err)")
}
}

var sampleTimingInfo = CMSampleTimingInfo(duration: kCMTimeInvalid, presentationTimeStamp: outputTime, decodeTimeStamp: kCMTimeInvalid)
var sampleBuffer: CMSampleBuffer?
err = CMSampleBufferCreateForImageBuffer(nil, pixelBuffer, true, nil, nil, videoInfo!, &sampleTimingInfo, &sampleBuffer)
if (err != noErr) {
DLog("Error at CMSampleBufferCreateForImageBuffer (err)")
}

if sampleLayer.isReadyForMoreMediaData {
sampleLayer.enqueue(sampleBuffer!)
}
}



Sample Layer added like this:


private func setupVideoPlaybackLayer() {

self.view.isOpaque = false
self.view.layer.isOpaque = false

sampleLayer = AVSampleBufferDisplayLayer()
sampleLayer.backgroundColor = UIColor.green.cgColor
sampleLayer.frame = view.layer.frame
self.view.layer.addSublayer(sampleLayer)
}



But when the movie plays back, the areas I expect to be transparent are black, the UIView behind it is blue.



Any ideas?





How are you rendering it? That seems crucial, since you say the texture has correct transparency.
– Ken Thomases
Jun 30 at 14:08





@KenThomases I've added the code that takes the pixel buffer and enqueues it to the AVSampleBufferDisplayLayer
– Chris
Jun 30 at 14:11





Have you set the isOpaque property of the view containing the AVSampleBufferDisplayLayer to false?
– warrenm
Jun 30 at 14:55


isOpaque


false





@warrenm - Yes I have it set to false, I've updated the code above to show it.
– Chris
Jun 30 at 15:01






It's interesting that if I render to a MTKView drawable it works...just not with the sample layer....
– Chris
Jun 30 at 15:13









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Ud0,bz,V UmVSeXxLp7wFI0w299sKKPtu3GN48 1sC0eXHbzAafSdsJACcSqQWJY5
yu8TtFDyoeVxB5I34W APl,ouQyD SDy haf,IwH3s my6tlDTwi,bkwNdNoG34jD H2XBB Vo

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask