Group Group Group Group Group Group Group Group Group

Metal color blend and renderCommandEncoder

#1

Hi,
I was confused by the blend function and renderCommandEncoder
I have a MTKView and

passDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1, 1, 1, 0.0);

I have render a texture on it and texture color is purple, then put this mtkview on vc.view

I set the vc.view.backgroundColor = green, and I just can see the mtkview is white and texture is purple

renderPipelineDesc.colorAttachments[0].blendingEnabled = YES;

renderPipelineDesc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd; 
renderPipelineDesc.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;

renderPipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorOneMinusSourceColor;
renderPipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;

renderPipelineDesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
renderPipelineDesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;

What I expect is see the purple texture on the green vc.view

Another problem is

(void)drawInMTKView:(MTKView *)view {
id currentDrawable = [self currentDrawable];
id framebufferTexture = currentDrawable.texture;
if (currentDrawable) {
MTLRenderPassDescriptor *passDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
passDescriptor.colorAttachments[0].texture = framebufferTexture;
passDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1, 1, 1, 0.0);
passDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
passDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
id commandBuffer = [self.commandQueue commandBuffer];
for (NSInteger i = 0; i < self.captions.count; i++) {
NSLog(@" i %ld",i);
YACaptionModel *captionModel = [self.captions objectAtIndex:i];
Uniform uniform = captionModel.uniform;
uniform.modelViewProjectionMatrix.columns[3][0] -= 0.01;
id uniformBuffer = [self.device newBufferWithBytes:&uniform length:sizeof(Uniform) options:MTLResourceOptionCPUCacheModeDefault];
id commandEncoder = [commandBuffer renderCommandEncoderWithDescriptor:passDescriptor];
[commandEncoder setRenderPipelineState:self.renderPipeline];
[commandEncoder setVertexBuffer:self.vertexBuffer offset:0 atIndex:0];
[commandEncoder setVertexBuffer:uniformBuffer offset:0 atIndex:1];
[commandEncoder setFragmentTexture:captionModel.texture atIndex:0];
[commandEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
[commandEncoder endEncoding];
captionModel.uniform = uniform;
}
[commandBuffer presentDrawable:currentDrawable];
[commandBuffer commit];
}
}

I have encode captions.count on the currentDrawable but I just can only see one appear from right and move to the left edge

Does anyone know the way to solve this?
Thanks so much

#2

Hi @monkyu

I don’t really understand either of the questions.

For the first one, fixed-function blending, as described in Chapter 10, Fragment Post-Processing, works on the current framebuffer. So the process blends pixels between draws into the same framebuffer.

You appear to have completely different views?

A couple of things for you to read:


For the second one, I have no idea what you are trying to achieve, or what captions is.

#3

Sorry for reply this so later. So much work need to handle…

Ok. Back to the topic, I still finding the way to solve my alpha problem, I just render lots of textures use the mtkview at the same time and adjust their position at the same time, so they looks like the captions in the living room (move from right to left)

Caption is…uh… you can image that is I need to render 100 textures at the same time, the difference between each texture is position, they all visible on the screen, so I need put multi render encoder in the same render loop

I will put my code on the github as soon as possible , thanks for the help sincerely.

#4

Groups of resources moving from right to left sounds like you need Argument Buffers (https://developer.apple.com/documentation/metal/buffers/basic_argument_buffers), which are beyond the scope of the Metal book, but the Apple documentation is good, and the samples are excellent.

Try downloading this app: https://docs-assets.developer.apple.com/published/036c5aa162/ArgumentBuffersWithGPUEncoding.zip and run it to see if it’s anything like what you are after. It’s moving lots of textures from left to right, and they are encoded into argument buffers on the GPU. If it is what you are after, the description of the app is here: https://developer.apple.com/documentation/metal/buffers/argument_buffers_with_gpu_encoding

The WWDC video covering argument buffers is: https://developer.apple.com/videos/play/wwdc2017/601/

1 Like