Metal系列四:Demo清屏加三角形

首先贴出Apple的官方Demo:地址
https://developer.apple.com/documentation/metal/devices_and_commands

这里有个我总结的流程图:
在这里插入图片描述

首先由个Metal的准备阶段,在准备阶段中,我们准备以下内容:

1.MTLDevice 获取默认设备
2.MTLCommandQueue 命令队列

1.声明全局变量

{
   
    id<MTLDevice> _device;
    id<MTLCommandQueue> _commandQueue;
}

2.初始化

- (nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)mtkView
{
   
    self = [super init];
    if(self)
    {
   
        _device = mtkView.device;

		_commandQueue = [_device newCommandQueue];
    }

    return self;
}
  1. 使用MetalKit
/// Called whenever the view needs to render
- (void)drawInMTKView:(nonnull MTKView *)view
{
   
    Color color = [self makeFancyColor];
    view.clearColor = MTLClearColorMake(color.red, color.green, color.blue, color.alpha);

    //为每个渲染传递创建一个新的命令缓冲区到当前drawable
    // Create a new command buffer for each render pass to the current drawable
    id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
    commandBuffer.label = @"MyCommand";

    //获取从视图的drawable生成的渲染过程描述符
    // Obtain a render pass descriptor, generated from the view's drawable
    MTLRenderPassDescriptor *renderPassDescriptor = view.currentRenderPassDescriptor;

    // If you've successfully obtained a render pass descriptor, you can render to
    // the drawable; otherwise you skip any rendering this frame because you have no
    // drawable to draw to
    //如果您已成功获得渲染过程描述符,则可以渲染到drawable; 否则你跳过任何渲染这个帧,因为你没有可绘制的绘图
    if(renderPassDescriptor != nil)
    {
   
        id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];

        renderEncoder.label 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值