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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值