Metal名詞:
MTKView MetalKitView 顯示的view
MTLDevice?用來渲染的設備(又名GPU)
MTLCommandQueue?命令隊列
vector_uint2 視圖大小類型
MTLLibrary Metal庫
MTLFunction 庫中函數(shù)
MTLRenderPipelineDescriptor?管道狀態(tài)管道
MTLRenderPipelineState?同步創(chuàng)建并返回渲染管線狀態(tài)對象
MTLCommandBuffer 命令緩沖區(qū)
Metal渲染流程:
? ? ? ? //1.獲取GPU 設備
? ? ? ? _device= mtkView.device;
? ? ? ? //2.在項目中加載所有的(.metal)著色器文件
? ? ? ? // 從bundle中獲取.metal文件
? ? ? ? id<MTLLibrary> defaultLibrary = [_device newDefaultLibrary];
? ? ? ? //從庫中加載頂點函數(shù)
? ? ? ? id vertexFunction = [defaultLibrarynewFunctionWithName:@"vertexShader"];
? ? ? ? //從庫中加載片元函數(shù)
? ? ? ? id fragmentFunction = [defaultLibrarynewFunctionWithName:@"fragmentShader"];
? ? ? ? //3.配置用于創(chuàng)建管道狀態(tài)的管道
? ? ? ? MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
? ? ? ? //管道名稱
? ? ? ? pipelineStateDescriptor.label=@"Simple Pipeline";
? ? ? ? //可編程函數(shù),用于處理渲染過程中的各個頂點
? ? ? ? pipelineStateDescriptor.vertexFunction= vertexFunction;
? ? ? ? //可編程函數(shù),用于處理渲染過程中各個片段/片元
? ? ? ? pipelineStateDescriptor.fragmentFunction= fragmentFunction;
? ? ? ? //一組存儲顏色數(shù)據(jù)的組件
? ? ? ? pipelineStateDescriptor.colorAttachments[0].pixelFormat= mtkView.colorPixelFormat;
? ? ? ? //4.同步創(chuàng)建并返回渲染管線狀態(tài)對象
? ? ? ? _pipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&error];
? ? ? ? //判斷是否返回了管線狀態(tài)對象
? ? ? ?//5.創(chuàng)建命令隊列
? ? ? ? _commandQueue = [_device newCommandQueue];
//2-1. 頂點數(shù)據(jù)/顏色數(shù)據(jù)
? ? staticconstCCVertextriangleVertices[] 摇零;
? ?//2-2.為當前渲染的每個渲染傳遞創(chuàng)建一個新的命令緩沖區(qū)
? ? id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
? ? //指定緩存區(qū)名稱
? ? commandBuffer.label=@"MyCommand";
? ? // 2-3MTLRenderPassDescriptor:一組渲染目標颈渊,用作渲染通道生成的像素的輸出目標。
? ? MTLRenderPassDescriptor *renderPassDescriptor = view.currentRenderPassDescriptor;
? ? //2-4.創(chuàng)建渲染命令編碼器,這樣我們才可以渲染到something
? ? ? ? id renderEncoder =[commandBufferrenderCommandEncoderWithDescriptor:renderPassDescriptor];
? ? ? ? //渲染器名稱
? ? ? ? renderEncoder.label=@"MyRenderEncoder";
? ? ? ?//2-5.設置我們繪制的可繪制區(qū)域
? ? ? ?MTLViewportviewPort = {
? ? ? ? ? ? 0.0,0.0,_viewportSize.x,_viewportSize.y,-1.0,1.0
? ? ? ? };
? ? ? ? [renderEncodersetViewport:viewPort];
? ? ? //2-6.設置當前渲染管道狀態(tài)對象
? ? ? ? [renderEncodersetRenderPipelineState:_pipelineState];
? ? ? //2-7.從應用程序OC 代碼 中發(fā)送數(shù)據(jù)給Metal 頂點著色器 函數(shù)
? ? ? ?[renderEncodersetVertexBytes:triangleVertices
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? length:sizeof(triangleVertices)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? atIndex:CCVertexInputIndexVertices];
? ? ?//viewPortSize 數(shù)據(jù)
? ? ? [renderEncoder setVertexBytes:&_viewportSize
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? length:sizeof(_viewportSize)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? atIndex:CCVertexInputIndexViewportSize];
? ? ? //2-8.畫出三角形的3個頂點
? ? ? [renderEncoderdrawPrimitives:MTLPrimitiveTypeTriangle
? ? ? ? ? ? ? ? ? ? ? ? ? vertexStart:0
? ? ? ? ? ? ? ? ? ? ? ? ? vertexCount:3];
? ? ? //2-9.表示已該編碼器生成的命令都已完成,并且從NTLCommandBuffer中分離
? ? ? ? [renderEncoderendEncoding];
? ? ? ? //2-10.一旦框架緩沖區(qū)完成雾家,使用當前可繪制的進度表
? ? ? ? [commandBufferpresentDrawable:view.currentDrawable];
? ? ? ?//2-11.最后,在這里完成渲染并將命令緩沖區(qū)推送到GPU
? ? ? ?[commandBuffercommit];
Metal文件里面的兩個方法
//頂點著色函數(shù)
vertex RasterizerData
vertexShader(uintvertexID [[vertex_id]],
?? ? ? ? ? ? constantCCVertex*vertices [[buffer(CCVertexInputIndexVertices)]],
?? ? ? ? ? ? constantvector_uint2*viewportSizePointer [[buffer(CCVertexInputIndexViewportSize)]])
// 片元函數(shù)
fragmentfloat4fragmentShader(RasterizerDatain [[stage_in]])