36
36
#import " CCCache.h"
37
37
#import " CCGL.h"
38
38
#import " CCRenderDispatch.h"
39
+ #import " CCMetalSupport_Private.h"
39
40
40
41
41
42
const NSString *CCShaderUniformProjection = @" cc_Projection" ;
@@ -166,6 +167,7 @@ @implementation CCShaderCache
166
167
167
168
-(id )createSharedDataForKey : (id <NSCopying >)key
168
169
{
170
+ #warning TODO Need Metal path here.
169
171
NSString *shaderName = (NSString *)key;
170
172
171
173
NSString *fragmentName = [shaderName stringByAppendingPathExtension: @" fsh" ];
@@ -352,8 +354,10 @@ @implementation CCShader {
352
354
353
355
// MARK: Init Methods:
354
356
355
- -(instancetype )initWithProgram : (GLuint)program uniformSetters : (NSDictionary *)uniformSetters ownsProgram : (BOOL )ownsProgram
357
+ -(instancetype )initWithGLProgram : (GLuint)program uniformSetters : (NSDictionary *)uniformSetters ownsProgram : (BOOL )ownsProgram
356
358
{
359
+ NSAssert ([CCConfiguration sharedConfiguration ].graphicsAPI == CCGraphicsAPIGL, @" GL graphics not configured." );
360
+
357
361
if ((self = [super init ])){
358
362
_program = program;
359
363
_uniformSetters = uniformSetters;
@@ -363,6 +367,19 @@ -(instancetype)initWithProgram:(GLuint)program uniformSetters:(NSDictionary *)un
363
367
return self;
364
368
}
365
369
370
+ -(instancetype )initWithMetalVertexFunction : (id <MTLFunction >)vertexFunction fragmentFunction : (id <MTLFunction >)fragmentFunction
371
+ {
372
+ if ((self = [super init ])){
373
+ _vertexFunction = vertexFunction;
374
+ _fragmentFunction = fragmentFunction;
375
+
376
+ #warning TODO setup _uniformSetters
377
+ // _uniformSetters = uniformSetters;
378
+ }
379
+
380
+ return self;
381
+ }
382
+
366
383
-(instancetype )initWithVertexShaderSource : (NSString *)vertexSource fragmentShaderSource : (NSString *)fragmentSource
367
384
{
368
385
#warning TODO
@@ -393,7 +410,7 @@ -(instancetype)initWithVertexShaderSource:(NSString *)vertexSource fragmentShade
393
410
394
411
CCGL_DEBUG_POP_GROUP_MARKER ();
395
412
396
- blockself = [blockself initWithProgram : program uniformSetters: UniformSettersForProgram (program) ownsProgram: YES ];
413
+ blockself = [blockself initWithGLProgram : program uniformSetters: UniformSettersForProgram (program) ownsProgram: YES ];
397
414
});
398
415
399
416
return blockself;
@@ -418,7 +435,7 @@ - (void)dealloc
418
435
419
436
-(instancetype )copyWithZone : (NSZone *)zone
420
437
{
421
- return [[CCShader allocWithZone: zone] initWithProgram : _program uniformSetters: _uniformSetters ownsProgram: NO ];
438
+ return [[CCShader allocWithZone: zone] initWithGLProgram : _program uniformSetters: _uniformSetters ownsProgram: NO ];
422
439
}
423
440
424
441
static CCShaderCache *CC_SHADER_CACHE = nil ;
@@ -432,27 +449,49 @@ +(void)initialize
432
449
// +initialize may be called due to loading a subclass.
433
450
if (self != [CCShader class ]) return ;
434
451
435
- CC_SHADER_CACHE = [[CCShaderCache alloc ] init ];
436
-
437
- // Setup the builtin shaders.
438
- CC_SHADER_POS_COLOR = [[self alloc ] initWithFragmentShaderSource: @" void main(){gl_FragColor = cc_FragColor;}" ];
439
- CC_SHADER_POS_COLOR.debugName = @" CCPositionColorShader" ;
440
-
441
- CC_SHADER_POS_TEX_COLOR = [[self alloc ] initWithFragmentShaderSource: @" void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1);}" ];
442
- CC_SHADER_POS_TEX_COLOR.debugName = @" CCPositionTextureColorShader" ;
443
-
444
- CC_SHADER_POS_TEXA8_COLOR = [[self alloc ] initWithFragmentShaderSource: @" void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1).a;}" ];
445
- CC_SHADER_POS_TEXA8_COLOR.debugName = @" CCPositionTextureA8ColorShader" ;
452
+ NSAssert ([CCConfiguration sharedConfiguration ].graphicsAPI != CCGraphicsAPIInvalid, @" Graphics API not configured." );
446
453
447
- CC_SHADER_POS_TEX_COLOR_ALPHA_TEST = [[self alloc ] initWithFragmentShaderSource: CC_GLSL (
448
- uniform float cc_AlphaTestValue;
449
- void main (){
450
- vec4 tex = texture2D (cc_MainTexture, cc_FragTexCoord1);
451
- if (tex.a <= cc_AlphaTestValue) discard;
452
- gl_FragColor = cc_FragColor*tex;
453
- }
454
- )];
455
- CC_SHADER_POS_TEX_COLOR_ALPHA_TEST.debugName = @" CCPositionTextureColorAlphaTestShader" ;
454
+ #if __CC_METAL_SUPPORTED_AND_ENABLED
455
+ if ([CCConfiguration sharedConfiguration ].graphicsAPI == CCGraphicsAPIMetal){
456
+ id <MTLLibrary > library = [CCMetalContext currentContext ].library ;
457
+ id <MTLFunction > vertex = [library newFunctionWithName: @" CCVertexFunctionDefault" ];
458
+
459
+ CC_SHADER_POS_COLOR = [[self alloc ] initWithMetalVertexFunction: vertex fragmentFunction: [library newFunctionWithName: @" CCFragmentFunctionDefaultColor" ]];
460
+ CC_SHADER_POS_COLOR.debugName = @" CCPositionColorShader" ;
461
+
462
+ CC_SHADER_POS_TEX_COLOR = [[self alloc ] initWithMetalVertexFunction: vertex fragmentFunction: [library newFunctionWithName: @" CCFragmentFunctionDefaultTextureColor" ]];
463
+ CC_SHADER_POS_TEX_COLOR.debugName = @" CCPositionTextureColorShader" ;
464
+
465
+ CC_SHADER_POS_TEXA8_COLOR = [[self alloc ] initWithMetalVertexFunction: vertex fragmentFunction: [library newFunctionWithName: @" CCFragmentFunctionDefaultTextureA8Color" ]];
466
+ CC_SHADER_POS_TEXA8_COLOR.debugName = @" CCPositionTextureA8ColorShader" ;
467
+
468
+ CC_SHADER_POS_TEX_COLOR_ALPHA_TEST = [[self alloc ] initWithMetalVertexFunction: vertex fragmentFunction: [library newFunctionWithName: @" CCFragmentFunctionUnsupported" ]];
469
+ CC_SHADER_POS_TEX_COLOR_ALPHA_TEST.debugName = @" CCPositionTextureColorAlphaTestShader" ;
470
+ } else
471
+ #endif
472
+ {
473
+ CC_SHADER_CACHE = [[CCShaderCache alloc ] init ];
474
+
475
+ // Setup the builtin shaders.
476
+ CC_SHADER_POS_COLOR = [[self alloc ] initWithFragmentShaderSource: @" void main(){gl_FragColor = cc_FragColor;}" ];
477
+ CC_SHADER_POS_COLOR.debugName = @" CCPositionColorShader" ;
478
+
479
+ CC_SHADER_POS_TEX_COLOR = [[self alloc ] initWithFragmentShaderSource: @" void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1);}" ];
480
+ CC_SHADER_POS_TEX_COLOR.debugName = @" CCPositionTextureColorShader" ;
481
+
482
+ CC_SHADER_POS_TEXA8_COLOR = [[self alloc ] initWithFragmentShaderSource: @" void main(){gl_FragColor = cc_FragColor*texture2D(cc_MainTexture, cc_FragTexCoord1).a;}" ];
483
+ CC_SHADER_POS_TEXA8_COLOR.debugName = @" CCPositionTextureA8ColorShader" ;
484
+
485
+ CC_SHADER_POS_TEX_COLOR_ALPHA_TEST = [[self alloc ] initWithFragmentShaderSource: CC_GLSL (
486
+ uniform float cc_AlphaTestValue;
487
+ void main (){
488
+ vec4 tex = texture2D (cc_MainTexture, cc_FragTexCoord1);
489
+ if (tex.a <= cc_AlphaTestValue) discard;
490
+ gl_FragColor = cc_FragColor*tex;
491
+ }
492
+ )];
493
+ CC_SHADER_POS_TEX_COLOR_ALPHA_TEST.debugName = @" CCPositionTextureColorAlphaTestShader" ;
494
+ }
456
495
}
457
496
458
497
+(instancetype )positionColorShader
0 commit comments