Last active
March 27, 2018 05:09
-
-
Save edom18/0944186d0484e1a5271a to your computer and use it in GitHub Desktop.
iOSの新グラフィックAPI - Metal入門してみる ref: https://qiita.com/edo_m18/items/0fd6971c98c27a125d6e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QuartzCore; | |
// CAMetalLayerの生成 | |
CAMetalLayer *metalLayer = [CAMetalLayer layer]; | |
metalLayer.device = device; | |
metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm; | |
metalLayer.framebufferOnly = YES; | |
metalLayer.frame = self.view.layer.frame; | |
[self.view.layer addSublayer:metalLayer]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id <MTLCommandQueue> commandQueue = [device newCommandQueue]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fragment half4 basic_fragment() { | |
return half4(1.0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MTLDeivceの生成 | |
id <MTLDevice> device = MTLCreateSystemDefaultDevice(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Render Command Encoderの生成 | |
id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; | |
if (renderEncoder) { | |
[renderEncoder setRenderPipelineState:self.pipelineState]; | |
[renderEncoder setVertexBuffer:self.vertexBuffer offset:0 atIndex:0]; | |
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3 instanceCount:1]; | |
[renderEncoder endEncoding]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id <MTLRenderPipelineState> pipelineState; | |
id <MTLLibrary> defaultLibrary = [device newDefaultLibrary]; | |
id <MTLFunction> fragmentProgram = [defaultLibrary newFunctionWithName:@"basic_fragment"]; | |
id <MTLFunction> vertexProgram = [defaultLibrary newFunctionWithName:@"basic_vertex"]; | |
MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init]; | |
pipelineStateDescriptor.vertexFunction = vertexProgram; | |
pipelineStateDescriptor.fragmentFunction = fragmentProgram; | |
[pipelineStateDescriptor.colorAttachments objectAtIndexedSubscript:0].pixelFormat = MTLPixelFormatBGRA8Unorm; | |
NSError *pipelineError = nil; | |
pipelineState = [device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&pipelineError]; | |
if (!pipelineState) { | |
NSLog(@"Failed to create pipeline state, error %@", pipelineError); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 頂点情報 | |
CGFloat vertexData[] = { | |
0.0, 1.0, 0.0, | |
-1.0, -1.0, 0.0, | |
1.0, -1.0, 0.0, | |
}; | |
NSInteger dataSize = sizeof(vertexData); | |
id <MTLBuffer> vertexBuffer = [device newBufferWithBytes:vertexData length:dataSize options:MTLResourceOptionCPUCacheModeDefault]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vertex float4 basic_vertex( | |
const device packed_float3 *vertex_array [[buffer(0)]], | |
unsigned int vid [[vertex_id]]) { | |
return float4(vertex_array[vid], 1.0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameloop)]; | |
[timer addToRunLoop:NSRunLoop.mainRunLoop forMode:NSDefaultRunLoopMode]; | |
///////////////////////////////////////////////////////////////////////////// | |
- (void)gameloop | |
{ | |
@autoreleasepool { | |
[self render]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Render Pass Descriptorの生成。 | |
MTLRenderPassDescriptor *renderPassDescriptor = [[MTLRenderPassDescriptor alloc] init]; | |
renderPassDescriptor.colorAttachments[0].texture = texture; | |
renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear; | |
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 104.0/255.0, 5.0/255.0, 1.0); | |
renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a new command queue | |
_commandQueue = [_device newCommandQueue]; | |
///////////////////////////////////////////////////////////////////////////// | |
id <MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 頂点データを定義する | |
static const float vertexData[] = { | |
0.0, -1.0, 0.0, | |
-1.0, 1.0, 0.0, | |
1.0, 1.0, 0.0 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id <MTLBuffer> buffer = [device newBufferWithBytes:vertexData length:sizeof(vertexData) options:MTLResourceOptionCPUCacheModelDefault]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static const float uniformData[] { | |
1.0, 1.0, 1.0, 1.0, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id <MTLBuffer> uniformBuffer = [device newBufferWithLength:sizeof(uniformData) options:MTLResourceOptionCPUCacheModelDefault]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void *buf = [uniformBuffer contents]; | |
memcpy(buf, uniformData, sizeof(uniformData)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[commandBuffer presentDrawable:view.currentDrawable]; | |
[commandBuffer commit]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 構造体の宣言 | |
struct VertexIn { | |
packed_float3 position; | |
packed_float4 color; | |
}; | |
struct VertexOut { | |
float4 position [[position]]; | |
float4 color; | |
}; | |
vertex VertexOut vertex_shader_function(const device VertexIn *vertex_array [[buffer(0)]], unsigned int vid [[vertex_id]]) { | |
// do something. | |
} | |
fragment half4 fragment_shader_function(VertexOut input [[stage_in]]) { | |
// do something. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vertex float4 vertex_function(const device packed_float3 *vertex_array [[buffer(0)]], | |
const device Uniforms &uniforms [[buffer(1)]], | |
unsigned int vid [[vertex_id]]) { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id <MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; | |
[renderEncoder setVertexBuffer:vertexBuffer offset:0 atIndex:0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CGFloat data[] = { | |
1, 2, 3, 4, 5, 6, 7, // 7個でひとつの頂点分の情報になる | |
8, 9, 10, 11, 12, 13, 14, | |
... | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment