Skip to content

Commit 7ce3e77

Browse files
committed
CCDrawNode metal support.
1 parent 5124bee commit 7ce3e77

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

cocos2d/CCDrawNode.m

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#import "Support/CGPointExtension.h"
3333
#import "CCNode_Private.h"
3434
#import "CCColor.h"
35+
#import "CCConfiguration.h"
36+
#import "CCMetalSupport_Private.h"
3537

3638
#ifdef ANDROID // Many Android devices do NOT support GL_OES_standard_derivatives correctly
3739
static NSString *CCDrawNodeShaderSource =
@@ -59,12 +61,25 @@ @implementation CCDrawNode {
5961

6062
+ (CCShader *)fragmentShader
6163
{
62-
static CCShader *shader = nil;
63-
static dispatch_once_t once = 0L;
64-
dispatch_once(&once, ^{
65-
shader = [[CCShader alloc] initWithFragmentShaderSource:CCDrawNodeShaderSource];
66-
});
67-
return shader;
64+
static CCShader *shader = nil;
65+
static dispatch_once_t once = 0L;
66+
dispatch_once(&once, ^{
67+
#if __CC_METAL_SUPPORTED_AND_ENABLED
68+
if([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIMetal){
69+
id<MTLLibrary> library = [CCMetalContext currentContext].library;
70+
NSAssert(library, @"Metal shader library not found.");
71+
72+
id<MTLFunction> vertexFunc = [library newFunctionWithName:@"CCVertexFunctionDefault"];
73+
74+
shader = [[CCShader alloc] initWithMetalVertexFunction:vertexFunc fragmentFunction:[library newFunctionWithName:@"CCFragmentFunctionDefaultDrawNode"]];
75+
shader.debugName = @"CCFragmentFunctionDefaultDrawNode";
76+
} else
77+
#endif
78+
{
79+
shader = [[CCShader alloc] initWithFragmentShaderSource:CCDrawNodeShaderSource];
80+
}
81+
});
82+
return shader;
6883
}
6984

7085
#pragma mark memory

cocos2d/Platforms/iOS/CCShaders.metal

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ CCFragmentFunctionDefaultTextureA8Color(
7171
return in.color*cc_MainTexture.sample(cc_MainTextureSampler, in.texCoord1).a;
7272
}
7373

74+
fragment half4
75+
CCFragmentFunctionDefaultDrawNode(
76+
const CCFragData in [[stage_in]]
77+
){
78+
return in.color*smoothstep(0.0, length(fwidth(in.texCoord1)), 1.0 - length(in.texCoord1));
79+
}
80+
7481
fragment half4
7582
CCFragmentFunctionUnsupported(
7683
const CCFragData in [[stage_in]]

0 commit comments

Comments
 (0)