蘋果的 Metal 工程

來源:互聯網
上載者:User

標籤:log   紋理   dex   eve   類型   傳遞資料   gpu   ima   size   

Basic Buffers

當向頂點著色器傳遞資料過多(大於 4096 位元組)時, setVertexBytes:length:atIndex: 方法不允許使用,應該使用 setVertexBytes:length:atIndex: 方法提高效能。
這時,參數應該是 MTLBuffer類型,可以被 GPU 訪問的記憶體。
_vertexBuffer.contents 方法返回可以被 CPU 訪問的記憶體介面,即這塊兒記憶體被 CPU 和 GPU 共用。

Basic Texturing

MTLPixelFormatBGRA8Unorm 的像素格式。?

2D 紋理的座標
?

Reading a texel is also known as sampling

Hello Compute

data-parallel computations using the GPU.

在 GPU 發展曆史中,平行處理的架構一直沒有變化,而處理核心的可程式化特性越來越強。這使得 GPU 從 fixed-function pipeline 轉向 programmable pipeline,也使得通用 GPU 編程 (GPGPU) 變得可行。

一個 MTLComputePipelineState 對象可以直接由一個 kernel function 產生。

 // Create a compute kernel functionid <MTLFunction> kernelFunction = [defaultLibrary newFunctionWithName:@"grayscaleKernel"];// Create a compute kernel_computePipelineState = [_device newComputePipelineStateWithFunction:kernelFunction

把映像分塊平行處理

    // Set the compute kernel's thread group size of 16x16    _threadgroupSize = MTLSizeMake(16, 16, 1);    // Calculate the number of rows and columsn of thread groups given the width of our input image.    //   Ensure we cover the entire image (or more) so we process every pixel.    _threadgroupCount.width  = (_inputTexture.width  + _threadgroupSize.width -  1) / _threadgroupSize.width;    _threadgroupCount.height = (_inputTexture.height + _threadgroupSize.height - 1) / _threadgroupSize.height;    // Since we're only dealing with a 2D data set, set depth to 1    _threadgroupCount.depth = 1;   [computeEncoder dispatchThreadgroups:_threadgroupCount               threadsPerThreadgroup:_threadgroupSize];
CPU and GPU Synchronization

CPU 和 GPU 是兩個非同步處理器,但是它們共用快取,因此需要在並行的同時避免同時讀寫資料。
?

在中,每一幀中,CPU 和 GPU 不會同時工作,雖然避免了同時讀寫資料,但是降低了效能。
?

在中,CPU 和 GPU 會同時讀寫相同的資料,引起競爭。
?

可以用多個緩衝區來達到提高效能和避免資料同時讀寫的問題。CPU 和 GPU 不同時讀寫相同的緩衝區。
當 GPU 執行完 command buffer 後,會調用這個 handler

 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer){    dispatch_semaphore_signal(block_sema);}];
LOD with Function Specialization

level of detail (LOD)

細節越逼真,消耗的資源越多。因此要在效能和細節的豐富度之間做權衡。

if(highLOD){    // Render high-quality model}else if(mediumLOD){    // Render medium-quality model}else if(lowLOD){    // Render low-quality model}

但是使用 GPU 寫出上面的代碼的話,效能不高。GPU 可以並行的指令數依賴於為函數分配的寄存器數目。GPU 編譯器需要為函數分配可能用到的最大數目寄存器,即使有些分支永遠不可能執行。因此,分支語句顯著增加了需要的寄存器數目,並顯著降低了 GPU 的並行數目

蘋果的 Metal 工程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.