I have a fbo generated by last frame, which contain a color texture and a depth texture. Then i want blit that fbo to current MTKView, but failed. Thanks a lot for any suggestions.
Here is the code:
last frame:
id <MTLTexture> src_color;
id <MTLTexture> src_depth;
drawTo(src_color, src_depth);
current frame:
first, init a depth texture if null:
id<MTLTexture> depth_texture_;
if(depth_texture_) {
depth_texture_ = [device_ newTextureWithDescriptor:desc];
}
second, create desc for encoder:
current_desc = metal_view_.currentRenderPassDescriptor;
current_desc.depthAttachment.texture = depth_texture_;
current_desc.depthAttachment.loadAction = MTLLoadActionClear;
current_desc.depthAttachment.clearDepth = 1.0;
current_desc.stencilAttachment.texture = depth_texture_;
current_desc.stencilAttachment.loadAction = MTLLoadActionClear;
current_desc.stencilAttachment.clearStencil = 0;
current_desc.colorAttachments[0].loadAction = MTLLoadActionClear;
current_desc.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 1, 1, 1.0);
third, blit to current MTKView:
auto dst_color = current_desc.colorAttachments[0].texture;
auto dst_depth = current_desc.depthAttachment.texture;
id<MTLCommandBuffer> commandBuffer = [command_queue_ commandBuffer];
commandBuffer.label = @"blit";
id <MTLBlitCommandEncoder> blitEncoder = [commandBuffer blitCommandEncoder];
[blitEncoder copyFromTexture:src_color
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(srcRect.x, srcRect.y, 0)
sourceSize:MTLSizeMake(srcRect.width, srcRect.height, 1)
toTexture:dst_color
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(dstRect.x, dstRect.y, 0)];
[blitEncoder copyFromTexture:src_depth
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(srcRect.x, srcRect.y, 0)
sourceSize:MTLSizeMake(srcRect.width, srcRect.height, 1)
toTexture:dst_depth
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(dstRect.x, dstRect.y, 0)];
[blitEncoder endEncoding];
[commandBuffer commit];
[commandBuffer waitUntilCompleted];