|
27 | 27 |
|
28 | 28 | #if __CC_METAL_SUPPORTED_AND_ENABLED |
29 | 29 |
|
30 | | -@implementation CCMetalContext |
| 30 | +@implementation CCMetalContext { |
| 31 | + @public |
| 32 | + id<MTLRenderPipelineState> _tempPiplelineState; |
| 33 | +} |
31 | 34 |
|
32 | 35 | -(instancetype)init |
33 | 36 | { |
34 | 37 | if((self = [super init])){ |
35 | 38 | _device = MTLCreateSystemDefaultDevice(); |
| 39 | + _library = [_device newDefaultLibrary]; |
| 40 | + |
36 | 41 | _commandQueue = [_device newCommandQueue]; |
37 | 42 | _currentCommandBuffer = [_commandQueue commandBuffer]; |
| 43 | + |
| 44 | + #warning Temporary |
| 45 | + id <MTLFunction> vertexProgram = [_library newFunctionWithName:@"CCVertexFunctionDefault"]; |
| 46 | + id <MTLFunction> fragmentProgram = [_library newFunctionWithName:@"CCFragmentFunctionDefaultColor"]; |
| 47 | + |
| 48 | + MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init]; |
| 49 | + [pipelineStateDescriptor setSampleCount: 1]; |
| 50 | + [pipelineStateDescriptor setVertexFunction:vertexProgram]; |
| 51 | + [pipelineStateDescriptor setFragmentFunction:fragmentProgram]; |
| 52 | + |
| 53 | + MTLRenderPipelineColorAttachmentDescriptor *colorDescriptor = [MTLRenderPipelineColorAttachmentDescriptor new]; |
| 54 | + colorDescriptor.pixelFormat = MTLPixelFormatBGRA8Unorm; |
| 55 | + colorDescriptor.blendingEnabled = NO; |
| 56 | + pipelineStateDescriptor.colorAttachments[0] = colorDescriptor; |
| 57 | + |
| 58 | + _tempPiplelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:nil]; |
38 | 59 | } |
39 | 60 |
|
40 | 61 | return self; |
@@ -81,6 +102,8 @@ -(void)prepareCommandBuffer |
81 | 102 |
|
82 | 103 | -(void)commitCurrentCommandBuffer |
83 | 104 | { |
| 105 | + [_currentRenderCommandEncoder endEncoding]; |
| 106 | + |
84 | 107 | [_currentCommandBuffer commit]; |
85 | 108 | _currentCommandBuffer = nil; |
86 | 109 | } |
@@ -130,18 +153,23 @@ -(void)commit; {} |
130 | 153 | @implementation CCRenderCommandDrawMetal |
131 | 154 |
|
132 | 155 | static const MTLPrimitiveType MetalDrawModes[] = { |
133 | | - GL_TRIANGLES, |
134 | | - GL_LINES, |
| 156 | + MTLPrimitiveTypeTriangle, |
| 157 | + MTLPrimitiveTypeLine, |
135 | 158 | }; |
136 | 159 |
|
137 | 160 | -(void)invokeOnRenderer:(CCRenderer *)renderer |
138 | 161 | { |
139 | | - id<MTLRenderCommandEncoder> renderEncoder = [CCMetalContext currentContext].currentRenderCommandEncoder; |
140 | | - CCGraphicsBufferMetal *indexBuffer = (CCGraphicsBufferMetal *)renderer->_elementBuffer; |
| 162 | + CCMetalContext *context = [CCMetalContext currentContext]; |
| 163 | + id<MTLRenderCommandEncoder> renderEncoder = context.currentRenderCommandEncoder; |
| 164 | + |
| 165 | + id<MTLBuffer> vertexBuffer = ((CCGraphicsBufferMetal *)renderer->_vertexBuffer)->_buffer; |
| 166 | + id<MTLBuffer> indexBuffer = ((CCGraphicsBufferMetal *)renderer->_elementBuffer)->_buffer; |
141 | 167 |
|
142 | 168 | [renderEncoder pushDebugGroup:@"CCRendererCommandDraw: Invoke"]; |
143 | | - [renderer setRenderState:_renderState]; |
144 | | - [renderEncoder drawIndexedPrimitives:MetalDrawModes[_mode] indexCount:_count indexType:MTLIndexTypeUInt16 indexBuffer:indexBuffer->_buffer indexBufferOffset:2*_first]; |
| 169 | +// [renderer setRenderState:_renderState]; |
| 170 | + [renderEncoder setRenderPipelineState:context->_tempPiplelineState]; |
| 171 | + [renderEncoder setVertexBuffer:vertexBuffer offset:0 atIndex:0]; |
| 172 | + [renderEncoder drawIndexedPrimitives:MetalDrawModes[_mode] indexCount:_count indexType:MTLIndexTypeUInt16 indexBuffer:indexBuffer indexBufferOffset:2*_first]; |
145 | 173 | [renderEncoder popDebugGroup]; |
146 | 174 | } |
147 | 175 |
|
|
0 commit comments