Skip to content

Commit e2c7c01

Browse files
committed
Setting up the Metal view.
1 parent 4fe88cd commit e2c7c01

File tree

8 files changed

+107
-24
lines changed

8 files changed

+107
-24
lines changed

cocos2d/CCRenderer_private.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount, CC
171171

172172

173173
@interface CCRenderer(){
174-
CCGraphicsBuffer *_vertexBuffer;
174+
@public
175175
CCGraphicsBuffer *_elementBuffer;
176+
177+
@private
178+
CCGraphicsBuffer *_vertexBuffer;
176179
id<CCGraphicsBufferBindings> _bufferBindings;
177180

178181
NSDictionary *_globalShaderUniforms;

cocos2d/CCShader.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ -(instancetype)initWithProgram:(GLuint)program uniformSetters:(NSDictionary *)un
365365

366366
-(instancetype)initWithVertexShaderSource:(NSString *)vertexSource fragmentShaderSource:(NSString *)fragmentSource
367367
{
368+
#warning TODO
369+
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal) return self;
370+
368371
__block typeof(self) blockself = self;
369372

370373
CCRenderDispatch(NO, ^{

cocos2d/CCTextureCache.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ -(id) init
8686
{
8787
if( (self=[super init]) ) {
8888
_textures = [NSMutableDictionary dictionaryWithCapacity: 10];
89-
89+
9090
// init "global" stuff
9191
_loadingQueue = dispatch_queue_create("org.cocos2d.texturecacheloading", NULL);
9292
_dictQueue = dispatch_queue_create("org.cocos2d.texturecachedict", NULL);
93+
94+
#warning TODO
95+
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal) return self;
96+
9397
#if !__CC_PLATFORM_ANDROID
9498
#warning TODO might not be a GL view.
9599
CCGLView *view = (CCGLView*)[[CCDirector sharedDirector] view];

cocos2d/Platforms/iOS/CCAppDelegate.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ - (void) setupCocos2dWithOptions:(NSDictionary*)config
148148
// Create the main window
149149
window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
150150

151+
CGRect bounds = [window_ bounds];
151152

152153
// CCView creation
153154
// viewWithFrame: size of the OpenGL view. For full screen use [_window bounds]
@@ -166,7 +167,7 @@ - (void) setupCocos2dWithOptions:(NSDictionary*)config
166167
switch([CCConfiguration sharedConfiguration].graphicsAPI){
167168
case CCGraphicsAPIGL:
168169
ccview = [CCGLView
169-
viewWithFrame:[window_ bounds]
170+
viewWithFrame:bounds
170171
pixelFormat:config[CCSetupPixelFormat] ?: kEAGLColorFormatRGBA8
171172
depthFormat:[config[CCSetupDepthFormat] unsignedIntValue]
172173
preserveBackbuffer:[config[CCSetupPreserveBackbuffer] boolValue]
@@ -178,6 +179,8 @@ - (void) setupCocos2dWithOptions:(NSDictionary*)config
178179
#if __CC_METAL_SUPPORTED_AND_ENABLED
179180
case CCGraphicsAPIMetal:
180181
#warning TODO
182+
ccview = [[CCMetalView alloc] initWithFrame:bounds];
183+
break;
181184
#endif
182185
default: NSAssert(NO, @"Internal error: Graphics API not set up.");
183186
}

cocos2d/Platforms/iOS/CCMetalSupport.m

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,46 @@
2727

2828
#if __CC_METAL_SUPPORTED_AND_ENABLED
2929

30-
#import <Metal/Metal.h>
30+
@implementation CCMetalContext
31+
32+
-(instancetype)init
33+
{
34+
if((self = [super init])){
35+
_device = MTLCreateSystemDefaultDevice();
36+
}
37+
38+
return self;
39+
}
40+
41+
NSString *CURRENT_CONTEXT_KEY = @"CURRENT_CONTEXT_KEY";
42+
43+
+(instancetype)currentContext
44+
{
45+
return [NSThread currentThread].threadDictionary[CURRENT_CONTEXT_KEY];
46+
}
47+
48+
+(void)setCurrentContext:(CCMetalContext *)context
49+
{
50+
if(context){
51+
[NSThread currentThread].threadDictionary[CURRENT_CONTEXT_KEY] = context;
52+
} else {
53+
[[NSThread currentThread].threadDictionary removeObjectForKey:CURRENT_CONTEXT_KEY];
54+
}
55+
}
56+
57+
@end
3158

3259

3360
@implementation CCGraphicsBufferMetal {
61+
@public
3462
id<MTLBuffer> _buffer;
3563
}
3664

3765
-(instancetype)initWithCapacity:(NSUInteger)capacity elementSize:(size_t)elementSize type:(CCGraphicsBufferType)type;
3866
{
39-
if((self = [super init])){
40-
#warning TODO
41-
id<MTLDevice> device = nil;
42-
67+
if((self = [super initWithCapacity:capacity elementSize:elementSize type:type])){
4368
// Use write combining? Buffers are already write only for the GL renderer.
44-
_buffer = [device newBufferWithLength:capacity*elementSize options:MTLResourceOptionCPUCacheModeDefault];
69+
_buffer = [[CCMetalContext currentContext].device newBufferWithLength:capacity*elementSize options:MTLResourceOptionCPUCacheModeDefault];
4570

4671
_ptr = _buffer.contents;
4772
}
@@ -53,7 +78,12 @@ -(void)destroy {}
5378

5479
-(void)resize:(size_t)newCapacity;
5580
{
56-
#warning TODO
81+
id<MTLBuffer> newBuffer = [[CCMetalContext currentContext].device newBufferWithLength:newCapacity*_elementSize options:MTLResourceOptionCPUCacheModeDefault];
82+
memcpy(newBuffer.contents, _ptr, _capacity*_elementSize);
83+
84+
_capacity = newCapacity;
85+
_buffer = newBuffer;
86+
_ptr = _buffer.contents;
5787
}
5888

5989
-(void)prepare;
@@ -65,6 +95,27 @@ -(void)commit; {}
6595

6696
@end
6797

98+
99+
@implementation CCRenderCommandDrawMetal
100+
101+
static const MTLPrimitiveType MetalDrawModes[] = {
102+
GL_TRIANGLES,
103+
GL_LINES,
104+
};
105+
106+
-(void)invokeOnRenderer:(CCRenderer *)renderer
107+
{
108+
id<MTLRenderCommandEncoder> renderEncoder = [CCMetalContext currentContext].currentRenderCommandEncoder;
109+
CCGraphicsBufferMetal *indexBuffer = (CCGraphicsBufferMetal *)renderer->_elementBuffer;
110+
111+
[renderEncoder pushDebugGroup:@"CCRendererCommandDraw: Invoke"];
112+
[renderer setRenderState:_renderState];
113+
[renderEncoder drawIndexedPrimitives:MetalDrawModes[_mode] indexCount:_count indexType:MTLIndexTypeUInt16 indexBuffer:indexBuffer->_buffer indexBufferOffset:2*_first];
114+
[renderEncoder popDebugGroup];
115+
}
116+
117+
@end
118+
68119
#else
69120

70121
// Temporary

cocos2d/Platforms/iOS/CCMetalSupport_Private.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,30 @@
2424
*/
2525

2626
#import "ccMacros.h"
27-
2827
#if __CC_METAL_SUPPORTED_AND_ENABLED
2928

29+
#import <Metal/Metal.h>
30+
3031
#import "CCRenderer_Private.h"
3132

33+
34+
@interface CCMetalContext : NSObject
35+
36+
@property(nonatomic, readonly) id<MTLDevice> device;
37+
@property(nonatomic, readonly) id<MTLRenderCommandEncoder> currentRenderCommandEncoder;
38+
39+
+(instancetype)currentContext;
40+
+(void)setCurrentContext:(CCMetalContext *)context;
41+
42+
@end
43+
44+
3245
@interface CCGraphicsBufferMetal : CCGraphicsBuffer
3346
@end
3447

48+
49+
@interface CCRenderCommandDrawMetal : CCRenderCommandDraw
50+
@end
51+
52+
3553
#endif

cocos2d/Platforms/iOS/CCMetalView.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,17 @@
55
#import <UIKit/UIKit.h>
66
#import <Metal/Metal.h>
77

8-
#import "../../ccMacros.h"
8+
#import "CCDirectorView.h"
99

10-
@interface CCMetalView : UIView
10+
@interface CCMetalView : UIView<CCDirectorView>
1111

1212
@property(nonatomic, readonly) id<MTLDevice> device;
1313
@property(nonatomic, readonly) id<MTLCommandBuffer> currentCommandBuffer;
1414
@property(nonatomic, readonly) id<MTLTexture> currentFramebufferTexture;
1515

16-
///** pixel format: it could be RGBA8 (32-bit) or RGB565 (16-bit) */
17-
//@property(nonatomic,readonly) NSString* pixelFormat;
18-
//
19-
///** depth format of the render buffer: 0, 16 or 24 bits*/
20-
//@property(nonatomic,readonly) GLuint depthFormat;
21-
2216
/** returns surface size in pixels */
2317
@property(nonatomic,readonly) CGSize surfaceSize;
2418

25-
-(void)beginFrame;
26-
-(void) presentFrame;
27-
28-
- (CGPoint) convertPointFromViewToSurface:(CGPoint)point;
29-
- (CGRect) convertRectFromViewToSurface:(CGRect)rect;
3019
@end
3120

3221
#endif // __CC_PLATFORM_IOS

cocos2d/Platforms/iOS/CCMetalView.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#import "CCScene.h"
1414

1515
#import "CCDirector_Private.h"
16+
#import "CCMetalSupport_Private.h"
1617

1718
@implementation CCMetalView {
19+
CCMetalContext *_context;
1820
id<MTLCommandQueue> _queue;
1921
id<MTLDrawable> _currentDrawable;
2022

@@ -32,6 +34,11 @@ - (id) initWithFrame:(CGRect)frame
3234
{
3335
if((self = [super initWithFrame:frame]))
3436
{
37+
_context = [[CCMetalContext alloc] init];
38+
39+
#warning Temporary
40+
[CCMetalContext setCurrentContext:_context];
41+
3542
_device = MTLCreateSystemDefaultDevice();
3643
_queue = [_device newCommandQueue];
3744

@@ -163,6 +170,11 @@ - (void)presentFrame
163170
_currentDrawable = nil;
164171
}
165172

173+
-(void)addFrameCompletionHandler:(dispatch_block_t)handler
174+
{
175+
[_currentCommandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {handler();}];
176+
}
177+
166178
#pragma mark CCMetalView - Point conversion
167179

168180
- (CGPoint) convertPointFromViewToSurface:(CGPoint)point

0 commit comments

Comments
 (0)