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