【Metal API 教程:第一讲】用XCode6.1创建并调试Metal工程


Metal API


1.关于IOS 8 Metal


Metal API是apple 2014年在ios平台上推出的高效底层的3D图形API,它通过减少驱动层的API调用CPU的消耗提高渲染效率。

参考视频解说(百度云网盘链接)

2.创建Metal工程



首先要准备好苹果开发者的账号和一只iPhone 6/6s/iPad Air 2,Metal工程目前的编译和调试都需要真机进行调试。





如果 没有连接真机并把目标设为真机的话,会出现 Metal Compiler Error问题。


设为真机后,最后link上MetalFramework点播放键就能在iphone6上运行了。

3. 代码概览

a. 创建设备和指令队列

_device = MTLCreateSystemDefaultDevice();
// create a new command queue
_commandQueue = [_device newCommandQueue];
_defaultLibrary = [_device newDefaultLibrary];</span>

b.加载资源(Metal Shader 和 ConstBuffer)

id <MTLFunction> fragmentProgram = [_defaultLibrary newFunctionWithName:@"lighting_fragment"];
id <MTLFunction> vertexProgram = [_defaultLibrary newFunctionWithName:@"lighting_vertex"];

_vertexBuffer = [_device newBufferWithBytes:kCubeVertexData length:sizeof(kCubeVertexData) options:MTLResourceOptionCPUCacheModeDefault];
_vertexBuffer.label = @"Vertices";

c.初始化渲染管线

MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
pipelineStateDescriptor.label                           = @"MyPipeline";
pipelineStateDescriptor.sampleCount                     = _sampleCount;
pipelineStateDescriptor.vertexFunction                  = vertexProgram;
pipelineStateDescriptor.fragmentFunction                = fragmentProgram;
pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
pipelineStateDescriptor.depthAttachmentPixelFormat      = _depthPixelFormat;
    
// create a compiled pipeline state object. Shader functions (from the render pipeline descriptor)
// are compiled when this is created unlessed they are obtained from the device's cache
NSError *error = nil;
_pipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&error];
if(!_pipelineState) {
   NSLog(@">> ERROR: Couldnt create a pipeline: %@", error);      
// assert here if pipeline coudnt be created for any reason
   assert(0);
}

MTLDepthStencilDescriptor *depthStateDesc = [[MTLDepthStencilDescriptor alloc] init];
depthStateDesc.depthCompareFunction = MTLCompareFunctionLess;
depthStateDesc.depthWriteEnabled = YES;
_depthState = [_device newDepthStencilStateWithDescriptor:depthStateDesc]; //renderEncoder需要用到


d.执行渲染


    // create a new command buffer for each renderpass to the current drawable
    id <MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];

    // create a render command encoder so we can render into something
    MTLRenderPassDescriptor *renderPassDescriptor = view.renderPassDescriptor;
    if (renderPassDescriptor)
    {
        id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
        [renderEncoder pushDebugGroup:@"Boxes"];
        [renderEncoder setDepthStencilState:_depthState];
        [renderEncoder setRenderPipelineState:_pipelineState];
        [renderEncoder setVertexBuffer:_vertexBuffer offset:0 atIndex:0 ];
        
        // NOTE: this could be alot faster if we render using instancing, but in this case we want to emit lots of draw calls
        for (int i = 0; i < kNumberOfBoxes; i++) {
            //  set constant buffer for each box
            [renderEncoder setVertexBuffer:_dynamicConstantBuffer[_constantDataBufferIndex] offset:i*_sizeOfConstantT atIndex:1 ];
            
            // tell the render context we want to draw our primitives
            [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:36 instanceCount:1];
        }
        
        [renderEncoder endEncoding];
        [renderEncoder popDebugGroup];

        // call the view's completion handler which is required by the view since it will signal its semaphore and set up the next buffer
        __block dispatch_semaphore_t block_sema = _inflight_semaphore;
        [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
            dispatch_semaphore_signal(block_sema);
        }];
        
        // schedule a present once rendering to the framebuffer is complete
        [commandBuffer presentDrawable:view.currentDrawable];
        
        // finalize rendering here. this will push the command buffer to the GPU
        [commandBuffer commit];
    }



4.Metal 调试


GPU Trace查看界面

本文版权DsoTsin所有,转载文章请注明出处!


由于缺米,Metal教程等后续Mac到手之后再更新(虚拟机太慢了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值