LoginSignup
1
2

More than 5 years have passed since last update.

[iOS]MetalでGPUコンピューティング(12) MTLRenderPipelineState

Posted at

この記事は、Metal Advent Calendar2016の17日目です。

これまで、MetalのGPUコンピューティングについて解説記事を書いてきました。
[iOS] MetalでGPUコンピューティング (1) 最小限のコードの記述と特性の把握
[iOS] MetalでGPUコンピューティング (2) 群知能
[iOS] MetalでGPUコンピューティング (3) MTLDevice
[iOS] MetalでGPUコンピューティング (4) MTKView
[iOS] MetalでGPUコンピューティング (5) MTLLibrary
[iOS] MetalでGPUコンピューティング (6) MTLCommandQueue
[iOS] MetalでGPUコンピューティング (7) MTLCommandBuffer
[iOS] MetalでGPUコンピューティング (8) MTLComputeCommandEncoder
[iOS] MetalでGPUコンピューティング (9) MTLComputePipelineState
[iOS] MetalでGPUコンピューティング(10) Metal Shading Languageで記述されたライフゲームのロジック
[iOS] MetalでGPUコンピューティング(11) MTLRenderCommandEncoder

本記事では、前回に引き続きAppleが提供するサンプルコードの解説を行います。

扱うサンプルコードは、前回と同じライフゲームのアプリ、MetalGameOfLifeです。
MetalGameOfLife

IMG_5933.PNG
(実行画面)

今回は、サンプルコード内のMTLRenderPipelineStateについて解説を行います。
MTLRenderPipelineStateは、グライフィックのレンダリング用のパイプラインに描画用の関数や頂点の設定などの状態をエンコードするために用いられます。
コンピューティング用のMTLComputePipelineStateと対をなしています。
MTLRenderPipelineStateはクラスではなくプロトコルで、MTLDeviceのオブジェクトにより生成されます。
MTLRenderPipelineStateのオブジェクトの生成はコストが高いので、アプリ起動時に一度生成すれば十分です。

繰り返しになるのですが、このサンプルコードは、主に以下のファイルで構成されています。

AAPLRender.h
AAPLRender.m
AAPLViewController.h
AAPLViewController.m
Sharder.metal

このうち、AAPLRender.mには並列コンピューティング及び描画のCPU側のロジックが、Shader.metalには頂点シェーダー、フラグメントシェーダー、GPUコンピューティング用のシェーダーが書かれています。

ここからは、サンプルコード内におけるMTLRenderPipelineStateの使用箇所を解説していきます。
AAPLRender.mに以下の記述があります。

AAPLRender.m
@property (nonatomic, strong) id<MTLRenderPipelineState> renderPipelineState;
...
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
pipelineStateDescriptor.label = @"Fullscreen Quad Pipeline";
pipelineStateDescriptor.vertexFunction = vertexProgram;
pipelineStateDescriptor.fragmentFunction = fragmentProgram;
pipelineStateDescriptor.vertexDescriptor = vertexDescriptor;
pipelineStateDescriptor.colorAttachments[0].pixelFormat = self.view.colorPixelFormat;
_renderPipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&error];                                                                   

MTLRenderPipelineStateのオブジェクトの生成時には、MTLRenderPipelineDescriptorを介して頂点シェーダーの関数、フラグメントシェーダーの関数、頂点の設定、ピクセルフォーマットを指定します。

頂点シェーダーの関数、フラグメントシェーダーの関数は以下のようにMTLLibraryのオブジェクトを介してMTLFunctionのオブジェクトvertexProgram、fragmentProgramに格納されています。

AAPLRender.m
    id<MTLFunction> vertexProgram = [_library newFunctionWithName:@"lighting_vertex"];
    id<MTLFunction> fragmentProgram = [_library newFunctionWithName:@"lighting_fragment"];

@"lighting_vertexと@"lighting_fragment"がシェーダー内に記述された関数名です。

MTLRenderPipelineStateオブジェクトは、以下の箇所でコマンドエンコーダーに設定されます。

AAPLRender.m
[renderEncoder setRenderPipelineState:self.renderPipelineState];

このように、MTLRenderPipelineStateは描画用の様々な設定をエンコードし格納する役割を担っています。

今回はライフゲームのサンプルコード内におけるMTLRenderPipelineStateの解説を行いました。
次回以降、さらに他の箇所についての解説を行なっていきます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2