MTLDevice
MTLDevice ํ๋กํ ์ฝ์ ๋ช ๋ น์ ์คํํ ์ ์๋ ํ๋์ GPU๋ฅผ ๋ํ๋ ๋๋ค.
MTLDevice ํ๋กํ ์ฝ์ ์ฅ์น ์์ฑ์ ์กฐ์ฌ,
๋ฒํผ ๋ฐ ํ ์ค์ฒ์ ๊ฐ์ ๋ค๋ฅธ ์ฅ์น๋ณ ๊ฐ์ฒด๋ฅผ ์์ฑ,
์คํ์ ์ํด GPU์ ์ ์ถํ ๋ ๋๋ง ๋ฐ ์ปดํจํ ๋ช ๋ น์ ์ธ์ฝ๋ฉํ๊ณ ๋๊ธฐ์ด์ ์ถ๊ฐํ๋ ๋ฉ์๋๋ค์ ์ง์ํฉ๋๋ค.
์๋ก์ด command queue๋ฅผ ์์ฑํ๊ณ ๋ฉ๋ชจ๋ฆฌ์์ ๋ฒํผ๋ฅผ ํ ๋นํ๊ณ ,
ํ ์ค์ฒ๋ฅผ ์์ฑํ๊ณ , ์ฅ์น์ ๊ธฐ๋ฅ์ ๋ํด ์ฟผ๋ฆฌํ๋ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
์์คํ ์์ ๊ธฐ๋ณธ ์ฅ์น๋ฅผ ์ป์ผ๋ ค๋ฉด MTLCreateSystemDefaultDevice ํจ์๋ฅผ ํธ์ถํ์ญ์์ค.
Command Queue
command queue๋ command buffer์ ํ๋ก ๊ตฌ์ฑ๋๋ฉฐ, command queue๋ ์ด๋ฌํ command buffer์ ์คํ์์๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
์ผ๋ฐ์ ์ผ๋ก command queue๋ ์ค๋ ๋ ์ธ์ดํํ๋ฉฐ, ์ฌ๋ฌ ํ์ฑ command buffer๋ฅผ ๋์์ ์ธ์ฝ๋ฉ ํ ์ ์์ต๋๋ค.
Command Buffer
command buffer๋ ํน์ ์ฅ์น์์ ์คํํ๊ธฐ ์ํ ์ธ์ฝ๋ฉ๋ ๋ช ๋ น์ด ํฌํจ๋์ด ์์ต๋๋ค.
command encoder๋ command buffer์ ๋ ๋๋ง, ์ปดํจํ ๋ฐ ๋ธ๋ฆฌํ ๋ช ๋ น์ ์ถ๊ฐํ๊ณ
์ด๋ฌํ command buffer๋ค์ ์ฅ์น์์ ์คํ๋๋๋ก ์ปค๋ฐ๋ฉ๋๋ค.
์ผ๋ฐ์ ์ธ ์ฑ์์๋ ๋ ๋๋ง์ ์ ์ฒด ํ๋ ์์ด ๋จ์ผ command buffer๋ก ์ธ์ฝ๋ฉ๋ฉ๋๋ค.
ํด๋น ํ๋ ์์ ๋ ๋๋งํ๋ ๋ฐ ์ฌ๋ฌ ๋ ๋๋ง ํจ์ค, ์ปดํจํ ์ฒ๋ฆฌ ๊ธฐ๋ฅ ๋๋ ๋ธ๋ฆฟ ์์ ์ด ํฌํจ๋๋ ๊ฒฝ์ฐ์๋ ๋ง์ฐฌ๊ฐ์ง ์ ๋๋ค.
command buffer๋ transient ํ๊ณ ์ผํ์ฉ ๊ฐ์ฒด์ด๋ฉฐ ์ฌ์ฌ์ฉ์ ์ง์ํ์ง ์์ต๋๋ค.
command buffer๊ฐ ์คํ์ ์ํด ์ปค๋ฐ๋๋ฉด ์ ์ผํ ์ ํจํ ์์ ์
command buffer๊ฐ ์ค์ผ์ค ๋๊ฑฐ๋ ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๊ณ , command buffer ์คํ์ ์ํ๋ฅผ ์ฒดํฌํ๋ ๊ฒ์ ๋๋ค.
command buffer๋ ๋ํ ์ฑ์์ ๋ ๋ฆฝ์ ์ผ๋ก ์ถ์ ํ ์ ์๋ ์ ์ผํ ์์ ๋จ์๋ฅผ ๋ํ๋ ๋๋ค.
Metal ๋ฉ๋ชจ๋ฆฌ ๋ชจ๋ธ์ ์ํด ์ค์ ๋ ์ผ๊ด์ฑ ๊ฒฝ๊ณ๋ฅผ ์ ์ํฉ๋๋ค.
Creating a Command Buffer
MTLCommandBuffer ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ค๋ฉด, MTLCommandQueue์ commandBuffer ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋๋ค.
MTLCommandBuffer๋ ๋ง๋ค์ด์ง MTLCommandQueue์๋ง ์ปค๋ฐ๋ ์ ์๋ค.
command Buffer ๋ฉ์๋์ ์ํด ์์ฑ๋ command buffer๋ ์คํ์ ํ์ํ ๋ฐ์ดํฐ๋ฅผ ์ ์งํฉ๋๋ค.
Executing Commands
MTLCommandBuffer๋ ๋ค์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ฌ command queue์์ command buffer์ ์คํ์์๋ฅผ ์ค์ ํฉ๋๋ค.
command buffer๋ ์ปค๋ฐ๋ ๋๊น์ง ์คํ๋์ง ์์ต๋๋ค.
์ปค๋ฐ๋๋ฉด command buffer๋ ๋๊ธฐ์ด์ ์ถ๊ฐ๋ ์์๋๋ก ์คํ๋ฉ๋๋ค.
- enqueue
command queue์ command buffer๋ฅผ ์ํ ์๋ฆฌ๋ฅผ ์์ฝํฉ๋๋ค. ํ์ง๋ง commitํ์ง ์์ต๋๋ค.
๋์ค์ ์ปค๋ฐ๋๊ฒ ๋๋ฉด ์ด์ ์ enqueue๋ command buffer ํ์ ์คํ๋ฉ๋๋ค.
- commit
์ด์ ์ enqueue๋ buffer๊ฐ ์ปค๋ฐ๋ ํ command buffer๊ฐ ์ต๋ํ ๋นจ๋ฆฌ ์คํ๋ ์ ์๊ฒ ํฉ๋๋ค.
๋ง์ฝ buffer๊ฐ enqueue๋์ง ์์์๋ค๋ฉด ์์์ ์ผ๋ก enqueue๋ฅผ ํธ์ถํฉ๋๋ค.
Registering Handler Blocks for Command Buffer Execution
์๋ ๋์ด๋ MTLCommandBuffer ๋ฉ์๋๋ ๋ช ๋ น ์คํ์ ๋ชจ๋ํฐ๋งํฉ๋๋ค.
Scheduled ์ Completed ํธ๋ค๋ฌ๋ ์ ์ ๋์ง ์์ ์ค๋ ๋์์ ์คํ ์์๋๋ก ํธ์ถ๋ฉ๋๋ค.
์ฌ๊ธฐ์ ์ผ์ด๋๋ ์์ ์ ๋น ๋ฅด๊ฒ ์๋ฃ๋์ด์ผ ํฉ๋๋ค.
- addScheduledHandler
Scheduled ๋ ๋ ํธ์ถ๋ ๋ธ๋ก ๋ฑ๋ก.
๋ค๋ฅธ MTLCommandBuffer ๊ฐ์ฒด๋ ์์คํ ์ ๋ค๋ฅธ API์ ์ํด ์ ์ถ๋ ์์ ๊ฐ์ ์ข ์์ฑ์ด ์ถฉ์กฑ๋๋ฉด Scheduled๋ก ๊ฐ์ฃผ
์ฌ๋ฌ๊ฐ ๋ฑ๋ก ๊ฐ๋ฅ
- waitUntilScheduled
command buffer๊ฐ Schedule ๋๊ณ addScheduledHandler์ ์ํด ๋ฑ๋ก๋ ๋ชจ๋ ํธ๋ค๋ฌ๊ฐ ์๋ฃ๋ ํ ๋๊ธฐ์ ์ผ๋ก ๊ธฐ๋ค๋ฆฌ๊ณ ๋ฐํ.
- addCompletedHandler
device๊ฐ command buffer์ ๋ํ ์คํ์ ์๋ฃํ ์งํ ๋ฐ๋ก ํธ์ถ.
์ฌ๋ฌ๊ฐ ๋ฑ๋ก ๊ฐ๋ฅ
- waitUntilCompleted
device๊ฐ command buffer์ ์คํ์ ์๋ฃํ๊ณ addCompletedHandler์ ์ํด ๋ฑ๋ก๋ ๋ชจ๋ ํธ๋ค๋ฌ๊ฐ ๋ฐํ๋ ํ ๋๊ธฐ์ ์ผ๋ก ๋๊ธฐํ๊ณ ๋ฐํ
- presentDrawable
command buffer๊ฐ schedule ๋ ๋ ํ์ ๊ฐ๋ฅํ ๋ฆฌ์์ค(CAMetalDrawable)์ ์ปจํ ์ธ ๋ฅผ ์ ๊ณตํฉ๋๋ค.
Command Encoder
command encoder๋ GPU๊ฐ ์คํํ ์ ์๋ ํ์์ผ๋ก transient object๋ก commands์ state๋ฅผ
์ฐ๊ธฐ์ํด ๋จ์ผ command buffer์ ํ๋ฒ ์ฌ์ฉํ๋ ๊ฐ์ฒด์ ๋๋ค.
๋ง์ command encoder ๊ฐ์ฒด๋ command buffer์ command๋ฅผ ์ถ๊ฐํฉ๋๋ค.
command encoder๊ฐ ํ์ฑํ ๋์์ ๋, command buffer์ ๋ํ ๋ ์ ๊ถํ์ ๊ฐ์ต๋๋ค.
์ธ์ฝ๋ฉ์ด ๋๋๋ฉด endEncoding์ ํธ์ถํฉ๋๋ค.
Creating a Command Encoder Object
command encoder๋ ํน์ command buffer์ ๋ช ๋ น์ ์ถ๊ฐํ๋ฏ๋ก ํจ๊ป ์ฌ์ฉํ๋ ค๋ MTLCommandBuffer๊ฐ์ฒด์์
command encoder๋ฅผ ๋ง๋ญ๋๋ค.
๋ค์ ๋ฉ์๋๋ค์ ์ฌ์ฉํ์ฌ ๊ฐ ์ ํ์ command encoder๋ฅผ ๋ง๋ญ๋๋ค.
- renderCommandEncoderWithDescriptor
MTLRenderPassDescriptor์ attachment์ ๋ํ ๊ทธ๋ํฝ ๋ ๋๋ง์ ์ํ MTLRenderCommandEcoder ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
- computeCommandEncoder
๋ฐ์ดํฐ ๋ณ๋ ฌ ๊ณ์ฐ์ ์ํ MTLComputeCommandEncoder ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
- blitCommandEncoder
๋ฉ๋ชจ๋ฆฌ ์์ ์ ์ํ MTLBlitCommandEncoder ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
- parallelRenderCommandEncoderWithDescriptor
๊ณต์ MTLRenderPassDescriptor์ ์ง์ ๋ attachment๋ก ๊ณ์ ๋ ๋๋งํ๋ ๋์์ ์ฌ๋ฌ
MTLRenerCommandEncoder ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ์ค๋ ๋์์ ์คํํ ์ ์๋๋ก ํ๋ MTLParallelRenderCommandEncoder๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
Render Command Encoder
๊ทธ๋ํฝ ๋ ๋๋ง์ ๋ ๋๋ง ํจ์ค๋ก ์ค๋ช ํ ์ ์์ต๋๋ค.
MTLRenderCommandEncoder ๊ฐ์ฒด๋ ๋จ์ผ ๋ ๋๋ง ํจ์ค์ ๊ด๋ จ๋ ๋ ๋๋ง ์ํ ๋ฐ drawing command๋ฅผ ๋ํ๋ ๋๋ค.
MTLRenderCommandEncoder์๋ ๋ ๋๋ง ๋ช ๋ น์ destination ์ญํ ์ ํ๋ MTLRenderPassDescriptor๊ฐ ํ์ํฉ๋๋ค.
MTLRenderPassDescriptor๋ ์์, depth ๋ฐ stencil attachment๋ฅผ ํฌํจํฉ๋๋ค.
๋ค์ ๋ฉ์๋๋ค์ ๊ฐ์ง๊ณ ์์ต๋๋ค.
- vertex, fragment ํน์ texture image๋ฐ์ดํฐ๊ฐ ํฌํจ๋ ๋ฒํผ ๋ฐ ํ ์ค์ฒ ๊ฐ์ฒด์ ๊ฐ์ ๊ทธ๋ํฝ ๋ฆฌ์์ค๋ฅผ ์ง์ ํฉ๋๋ค.
- vertex, fragment ์์ด๋๋ฅผ ํฌํจํ๋ ์ปดํ์ผ๋ ๋ ๋๋ง ์ํ๊ฐ ๋ค์ด์๋ MTLRenderPipelineState ๊ฐ์ฒด๋ฅผ ์ง์ ํฉ๋๋ค.
- viewport, triangle fill mode, scissor rectangle, depth and stencil tests, ๊ธฐํ ๊ฐ ๋ฑ์ ํฌํจํ fixed-function ์ํ๋ฅผ ์ง์ ํฉ๋๋ค.
- 3D primitive ๊ทธ๋ฆฌ๊ธฐ
Compute Command Encoder
data-parallel ์ปดํจํ ์ ๊ฒฝ์ฐ MTLComputeCommandEncoder ํ๋กํ ์ฝ์
compute function๊ณผ function์ ์ธ์(ํ ์ค์ฒ, ๋ฒํผ ๋ฐ ์ํ๋ฌ ์ํ)๋ฅผ ์ง์ ํ ์ ์๋ command buffer์์
๋ช ๋ น์ ์ธ์ฝ๋ฉํ๊ณ ์คํ์ ์ํด ์ปดํจํ ํจ์๋ฅผ ๋์คํจ์น ํ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
Compute command encoder ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด MTLCommandBuffer์
computeCommandEncoder ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
Blit Command Encoder
MTLBlitCommandEncoder ํ๋กํ ์ฝ์๋ ๋ฒํผ(MTLBuffer)์ ํ ์ค์ฒ(MTLTexture) ๊ฐ์
๋ฉ๋ก๋ฆฌ ๋ณต์ฌ ์์ ์ ๋ํ ๋ช ๋ น์ ์ถ๊ฐํ๋ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
MTLBlitCommandEncoder ํ๋กํ ์ฝ์ ํ ์ค์ฒ๋ฅผ ๋จ์์ผ๋ก ์ฑ์ฐ๊ณ ๋ฐ๋งต์ ์์ฑํ๋ ๋ฉ์๋๋ ์ ๊ณตํฉ๋๋ค.
Blit ๋ช ๋ น ์ธ์ฝ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด MTLCommandBuffer์ blitCommandEncoder ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์.
MTLCommandQueue
command queue์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ ํ๋กํ ์ฝ์ ๋๋ค.
์ฃผ๋ก command buffer ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์ง์ํฉ๋๋ค.
MTLCommandBuffer
์ด ํ๋กํ ์ฝ์ command buffer์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๊ณ ,
command encoder ์์ฑ,
์คํ์ ์ํด command buffer ๋๊ธฐ์ด ์ถ๊ฐ, ์ํํ์ธ ๋ฐ ๊ธฐํ ์์ ์ ์ํ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ด ํ๋กํ ์ฝ์ ๋ค์ํ ์ข ๋ฅ์ GPU ์ํฌ๋ก๋๋ฅผ command buffer๋ก ์ธ์ฝ๋ฉํ๊ธฐ ์ํ ๋ค์๊ณผ ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ์ง์ํฉ๋๋ค.
MTLRenderCommandEncoder - ๋จ์ผ ๋ ๋๋ง ํจ์ค์ ๋ํ ๊ทธ๋ํฝ ๋ ๋๋ง ๋ช ๋ น ์ธ์ฝ๋ฉ
MTLComputeCommandEncoder - ๋ฐ์ดํฐ ๋ณ๋ ฌ ๊ณ์ฐ ์์ ๋ถํ๋ฅผ ์ธ์ฝ๋ฉ
MTLBlitCommandEncoder - ๋ฒํผ์ ํ ์ค์ฒ ๊ฐ์ ๊ฐ๋จํ ๋ณต์ฌ ์์ ๋ฟ๋ง ์๋๋ผ ๋ฐ๋งต ์์ฑ๊ณผ ๊ฐ์ ์ ํธ๋ฆฌํฐ ์์ ๋ ์ธ์ฝ๋ฉ
๋จ์ผ command encoder๋ง ํ์ฑํ๋์ด command buffer์ ๋ช ๋ น์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
๋ค๋ฅธ command encoder๋ฅผ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๊ธฐ์กด command encoder๊ฐ ์ข ๋ฃ๋์ด์ผ ํฉ๋๋ค.
ํ๋์ ์์ธ๋ก MTLParellelRenderCommandEncoder ํ๋กํ ์ฝ์ด ์์ต๋๋ค.
๋ชจ๋ ์ธ์ฝ๋ฉ์ด ์๋ฃ๋๋ฉด MTLCommandBuffer ๊ฐ์ฒด ์์ฒด๋ฅผ ์ปค๋ฐํ์ฌ
command buffer๋ฅผ GPU์์ ์คํํ ์ค๋น๊ฐ ๋๊ฒ์ผ๋ก ํ์ํฉ๋๋ค.
MTLCommandQueue ๋ ์ด๋ฏธ command queue์ ์๋ ๋ค๋ฅธ MTLCommandBuffer๊ฐ์ฒด์ ๊ด๋ จํ์ฌ
์ปค๋ฐ๋ MTLCommandBuffer ๊ฐ์ฒด์ ๋ช ๋ น์ด ์คํ๋๋ ์๊ธฐ๋ฅผ ์ ์ดํฉ๋๋ค.
๋ค์ ๊ทธ๋ฆผ์ command queue, command buffer ๋ฐ command encoder ๊ฐ์ฒด๊ฐ ์ด๋ป๊ฒ ๊ด๋ จ๋์ด ์๋์ง ๋ณด์ฌ์ค๋๋ค.
๊ฐ ์ด(๋ฒํผ, ํ ์ค์ฒ, ์ํ๋ฌ, depth, stencil, ํ์ดํ๋ผ์ธ ์ํ)๋ command encoder์ ๊ด๋ จ๋ ๋ฆฌ์์ค์ ์ํ์ ๋๋ค.

Transient and Non-transient Objects in Metal
Metal์ ์ผ๋ถ๊ฐ์ฒด๋ ์ผ์์ ์ด๊ณ ๋งค์ฐ ๊ฐ๋ณ๋๋ก ์ค๊ณ๋์์ง๋ง, ์ด๋ค ๊ฐ์ฒด๋ ๋ ๋น์ธ๊ณ ์ค๋ซ๋์ ์ง์๋ ์ ์์ผ๋ฉฐ,
์ฑ ์๋ช ๋์ ์ง์๋ ์๋ ์์ต๋๋ค.
command buffer์ command encoder๋ ์ผ์์ ์ด๋ฉฐ ์ผํ์ฉ์ผ๋ก ์ค๊ณ๋์์ต๋๋ค.
ํ ๋น ๋ฐ ๋นํ ๋น ๋น์ฉ์ด ์ ๋ ดํ๋ฏ๋ก ์์ฑ ๋ฉ์๋๋ autoreleased ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
๋ค์ ๊ฐ์ฒด๋ transientํ์ง ์์ต๋๋ค. ์ฑ๋ฅ์ ๋ฏผ๊ฐํ ์ฝ๋์์ ์ด๋ฌํ ๊ฐ์ฒด๋ฅผ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ๋ฐ๋ณต์์ฑํ์ง ๋ง์ญ์์ค.
- Command queues
- Data buffers
- Textures
- Sampler states
- Libraries
- Compute states
- Rendere pipeline states
- Depth/stencil states