IOS8新技术metal入门

       这里是参考http://metalbyexample.com/up-and-running-1/,我这里只是大概初步引导,还是看原文过瘾。metal技术需要新机调试,移动设备需要是a7 a8处理器,比如iPhone6,iPhone5c,ipad air2不能用模拟器来调试。

1、首先创建了一个简单的singleView项目。并添加框架,metal  uikit  foundation quartzcore。

2、添加类MetalView,继承 UIView,在类中声明属性

@property (nonatomic,strong) id<MTLDevice> device;

@property (nonatomic,weak) CAMetalLayer *metalLayer;

 在类实现中,添加方法

+ (id)layerClass

{

    return [CAMetalLayerclass];

}


- (instancetype)initWithCoder:(NSCoder *)aDecoder

{

    if ((self = [superinitWithCoder:aDecoder]))

    {

        _metalLayer = (CAMetalLayer *)[selflayer];

        _device =MTLCreateSystemDefaultDevice();

        _metalLayer.device =_device;

        _metalLayer.pixelFormat =MTLPixelFormatBGRA8Unorm;

    }

    

    returnself;

}


- (void)didMoveToWindow

{

    [selfredraw];

}


- (void)redraw

{

    id<CAMetalDrawable> drawable = [self.metalLayernextDrawable];

    

    id<MTLTexture> texture = drawable.texture;

    

    MTLRenderPassDescriptor *passDescriptor = [MTLRenderPassDescriptorrenderPassDescriptor];

    passDescriptor.colorAttachments[0].texture = texture;

    passDescriptor.colorAttachments[0].loadAction =MTLLoadActionClear;

    passDescriptor.colorAttachments[0].storeAction =MTLStoreActionStore;

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


    id<MTLCommandQueue> commandQueue = [self.devicenewCommandQueue];

    id<MTLCommandBuffer> commandBuffer = [commandQueuecommandBuffer];

    id <MTLRenderCommandEncoder> commandEncoder = [commandBufferrenderCommandEncoderWithDescriptor:passDescriptor];

    [commandEncoder endEncoding];

    [commandBuffer presentDrawable:drawable];

    [commandBuffer commit];

}

大概意思是,在cpu上创建好绘制的各种参数,然后传输到gpu,由gpu解析执行需要绘制的命令参数。有opengl或gpu编程经验会很好理解的。

3、把storyboard的里的view类属性选成MetalView类型,然后就commad+r吧,执行看看,红色背景图。

更改passDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1.00.00.01.0);这里的颜色。

源代码可以到原文中下载。我这也有http://download.csdn.net/detail/huyisu/8409671

你可能感兴趣的:(IOS8新技术metal入门)