Skip to content

Commit 5e0e52d

Browse files
committed
Working on CCMetalContext.
1 parent e2c7c01 commit 5e0e52d

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

cocos2d/Platforms/iOS/CCMetalSupport.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ -(instancetype)init
3333
{
3434
if((self = [super init])){
3535
_device = MTLCreateSystemDefaultDevice();
36+
_commandQueue = [_device newCommandQueue];
37+
_currentCommandBuffer = [_commandQueue commandBuffer];
3638
}
3739

3840
return self;
@@ -54,6 +56,35 @@ +(void)setCurrentContext:(CCMetalContext *)context
5456
}
5557
}
5658

59+
-(void)setDestinationTexture:(id<MTLTexture>)destinationTexture
60+
{
61+
if(_destinationTexture != destinationTexture){
62+
MTLRenderPassColorAttachmentDescriptor *colorAttachment = [MTLRenderPassColorAttachmentDescriptor new];
63+
colorAttachment.texture = destinationTexture;
64+
colorAttachment.loadAction = MTLLoadActionClear;
65+
colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 0);
66+
colorAttachment.storeAction = MTLStoreActionStore;
67+
68+
MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
69+
renderPassDescriptor.colorAttachments[0] = colorAttachment;
70+
71+
_currentRenderCommandEncoder = [self.currentCommandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
72+
_destinationTexture = destinationTexture;
73+
}
74+
}
75+
76+
-(void)prepareCommandBuffer
77+
{
78+
_currentCommandBuffer = [_commandQueue commandBuffer];
79+
_currentCommandBuffer.label = @"Main Cocos2D Command Buffer";
80+
}
81+
82+
-(void)commitCurrentCommandBuffer
83+
{
84+
[_currentCommandBuffer commit];
85+
_currentCommandBuffer = nil;
86+
}
87+
5788
@end
5889

5990

cocos2d/Platforms/iOS/CCMetalSupport_Private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@
3434
@interface CCMetalContext : NSObject
3535

3636
@property(nonatomic, readonly) id<MTLDevice> device;
37+
@property(nonatomic, readonly) id<MTLCommandQueue> commandQueue;
38+
@property(nonatomic, readonly) id<MTLCommandBuffer> currentCommandBuffer;
39+
40+
@property(nonatomic, strong) id<MTLTexture> destinationTexture;
3741
@property(nonatomic, readonly) id<MTLRenderCommandEncoder> currentRenderCommandEncoder;
3842

3943
+(instancetype)currentContext;
4044
+(void)setCurrentContext:(CCMetalContext *)context;
4145

46+
-(void)prepareCommandBuffer;
47+
-(void)commitCurrentCommandBuffer;
48+
4249
@end
4350

4451

cocos2d/Platforms/iOS/CCMetalView.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
@interface CCMetalView : UIView<CCDirectorView>
1111

12-
@property(nonatomic, readonly) id<MTLDevice> device;
13-
@property(nonatomic, readonly) id<MTLCommandBuffer> currentCommandBuffer;
14-
@property(nonatomic, readonly) id<MTLTexture> currentFramebufferTexture;
15-
1612
/** returns surface size in pixels */
1713
@property(nonatomic,readonly) CGSize surfaceSize;
1814

cocos2d/Platforms/iOS/CCMetalView.m

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
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

Comments
 (0)