標籤: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 工程