十七、Hello Metal

Metal

Metal是蘋果在WWDC2014上提出,18年替換OpenGL ES在iOS、macOS、tvOS中的一個渲染方案。Metal 是一種低層次的渲染應用程序編程接口,可以快速渲染圖形,也可以使用圖形處理器執(zhí)行數(shù)據(jù)并行計算。在需要用到設備上的GPU時,蘋果建議使用Metal(Metal最低支持A7處理器,即iPhone6s)。由于蘋果的封閉生態(tài)圈,蘋果希望用Metal:
1)實現(xiàn)自家芯片GPU的最大性能;
2)降低CPU功耗;
3)提升CPU和GPU的并行性;
4)高效的資源管理;

簡單使用
蘋果建議渲染循環(huán)和主邏輯代碼分離,所以新建類MetalRender;


- (id)initWithMetalKitView:(MTKView *)mtkView
{
    self = [super init];
    if (self) {
        _device = mtkView.device;
        /**
         所以程序與GPU交互的第一個對象就是MTLCommandQueue;使用MTLCommandQueue創(chuàng)建的對象,需加入到MTLCommandBuffer對象中,以確保
         它們能按照正確的順序發(fā)送到GPU,對應一幀都會創(chuàng)建一個MTLCommandBuffer與之相應的對象,并且填充GPU執(zhí)行的指令
         */
        _commandQueue = [_device newCommandQueue];
    }
    return self;
}
- (MyColor)settingColor
{
    // 1.增加顏色或減少顏色時的標記
    static Boolean flag = YES;
    // 2.顏色通道值0-3
    static NSInteger primaryChannel = 0;
    // 3.顏色通道數(shù)組colorChannels--顏色值
    static float colorChannels[] = {1.0,0.0,0.0,1.0};
    // 4.調(diào)整顏色的步長
    const float DynamicColorRate = 0.015;
    // 5.判斷
    if (flag) {
        // 動態(tài)信道索引在0,1,2,4 通道間切換
        NSUInteger dynamicChannelIndex = (primaryChannel + 1) % 3;
        // 按照顏色步長調(diào)整通道顏色值
        colorChannels[dynamicChannelIndex] += DynamicColorRate;
        // 當顏色通道對應顏色值 >= 1.0時
        if (colorChannels[dynamicChannelIndex] >= 1.0) {
            flag = NO;
            primaryChannel = dynamicChannelIndex;
        }

    }else{
        // 動態(tài)信道索引在0,1,2,4 通道間切換
        NSUInteger dynamicChannelIndex = (primaryChannel + 2) % 3;
        // 按照顏色步長調(diào)整通道顏色值
        colorChannels[dynamicChannelIndex] -= DynamicColorRate;
        // 當顏色通道對應顏色值 <= 1.0時
        if (colorChannels[dynamicChannelIndex] <= 0.0) {
            flag = YES;
        }
    }
    MyColor mycolor;
    mycolor.red = colorChannels[0];
    mycolor.green = colorChannels[1];
    mycolor.blue = colorChannels[2];
    mycolor.alpha = colorChannels[3];
    return mycolor;
}

#pragma mark - MTKViewDelegate

- (void)drawInMTKView:(MTKView *)view
{
    MyColor color = [self settingColor];
    
    view.clearColor = MTLClearColorMake(color.red, color.green, color.blue, color.alpha);
    // c創(chuàng)建一個MTLCommandBuffer對象,添加了_commandQueue,為每個渲染傳統(tǒng)創(chuàng)建一個命令緩沖區(qū);
    id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
    
    commandBuffer.label = @"Command Buffer";
    // 從視圖控制中獲取渲染描述符
    MTLRenderPassDescriptor * renderPassDescriptor = view.currentRenderPassDescriptor;
    
    if (renderPassDescriptor) {
        // 通過描述渲染符 renderPassDescriptor 創(chuàng)建 MTLRenderCommandEncoder對象
        id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
        renderEncoder.label = @"Render Encoder";
        
        // 結(jié)束 MTLRenderCommandEncoder 的工作
        [renderEncoder endEncoding];
        
        /**
        當編碼器結(jié)束工作后,命令緩存區(qū)會接到2個命令 1)present ;2)commit
         由于GPU是不會直接繪制到屏幕上,因此若不給出指令,則屏幕不會繪制內(nèi)容。
        */
        
        // 添加最后一個命令來顯示屏幕
        [commandBuffer presentDrawable:view.currentDrawable];
    }
    // 完成渲染并將命令緩存區(qū)提交給GPU
    [commandBuffer commit];
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容