1515#import " CCDirector_Private.h"
1616#import " CCMetalSupport_Private.h"
1717
18+
19+ #define CC_METAL_MAX_QUEUED_FRAMES 3
20+
21+
1822@implementation CCMetalView {
1923 CCMetalContext *_context;
20- id <MTLCommandQueue > _queue;
24+ // id<MTLCommandQueue> _queue;
2125 id <MTLDrawable > _currentDrawable;
2226
2327 dispatch_semaphore_t _queuedFramesSemaphore;
@@ -36,21 +40,17 @@ - (id) initWithFrame:(CGRect)frame
3640 {
3741 _context = [[CCMetalContext alloc ] init ];
3842
39- #warning Temporary
43+ #warning Temporary. Move into CCRenderDispatch?
4044 [CCMetalContext setCurrentContext: _context];
4145
42- _device = MTLCreateSystemDefaultDevice ();
43- _queue = [_device newCommandQueue ];
44-
45- #warning Magic number
46- _queuedFramesSemaphore = dispatch_semaphore_create (3 );
46+ _queuedFramesSemaphore = dispatch_semaphore_create (CC_METAL_MAX_QUEUED_FRAMES);
4747
4848 CAMetalLayer *layer = self.metalLayer ;
4949 layer.opaque = YES ;
5050 layer.pixelFormat = MTLPixelFormatBGRA8Unorm ;
5151 layer.framebufferOnly = YES ;
5252
53- layer.device = _device ;
53+ layer.device = _context. device ;
5454 layer.pixelFormat = MTLPixelFormatBGRA8Unorm ;
5555
5656 self.opaque = YES ;
@@ -141,12 +141,11 @@ -(void)beginFrame
141141 _layerSizeDidUpdate = NO ;
142142 }
143143
144- _currentCommandBuffer = [_queue commandBuffer ];
145- _currentCommandBuffer.label = @" Main Cocos2D Command Buffer" ;
144+ [_context prepareCommandBuffer ];
146145
147146 // Prevent the block from retaining self via the ivar.
148147 dispatch_semaphore_t sema = _queuedFramesSemaphore;
149- [_currentCommandBuffer addCompletedHandler: ^(id <MTLCommandBuffer > buffer){
148+ [_context.currentCommandBuffer addCompletedHandler: ^(id <MTLCommandBuffer > buffer){
150149 dispatch_semaphore_signal (sema);
151150 }];
152151
@@ -159,20 +158,20 @@ -(void)beginFrame
159158
160159 id <CAMetalDrawable > drawable = [self .metalLayer nextDrawable ];
161160 _currentDrawable = drawable;
162- _currentFramebufferTexture = drawable.texture ;
161+ _context. destinationTexture = drawable.texture ;
163162}
164163
165164- (void )presentFrame
166165{
167- [_currentCommandBuffer presentDrawable: _currentDrawable ];
168- [_currentCommandBuffer commit ];
169- _currentCommandBuffer = nil ;
166+ [_context commitCurrentCommandBuffer ];
167+
168+ [_currentDrawable present ] ;
170169 _currentDrawable = nil ;
171170}
172171
173172-(void )addFrameCompletionHandler : (dispatch_block_t )handler
174173{
175- [_currentCommandBuffer addCompletedHandler: ^(id <MTLCommandBuffer > buffer) {handler ();}];
174+ [_context.currentCommandBuffer addCompletedHandler: ^(id <MTLCommandBuffer > buffer) {handler ();}];
176175}
177176
178177#pragma mark CCMetalView - Point conversion
0 commit comments