111
111
@" cc_FragTexCoord2 = cc_TexCoord2;\n "
112
112
@" }\n " ;
113
113
114
+ static NSString *CCMetalShaderHeader =
115
+ @" using namespace metal;\n\n "
116
+ @" typedef struct CCVertex {\n "
117
+ @" float4 position;\n "
118
+ @" float2 texCoord1;\n "
119
+ @" float2 texCoord2;\n "
120
+ @" float4 color;\n "
121
+ @" } CCVertex;\n "
122
+ @" typedef struct CCFragData {\n\n "
123
+ @" float4 position [[position]];\n "
124
+ @" float2 texCoord1;\n "
125
+ @" float2 texCoord2;\n "
126
+ @" half4 color;\n "
127
+ @" } CCFragData;\n "
128
+ @" typedef struct CCGlobalUniforms {\n\n "
129
+ @" float4x4 projection;\n "
130
+ @" float4x4 projectionInv;\n "
131
+ @" float2 viewSize;\n "
132
+ @" float2 viewSizeInPixels;\n "
133
+ @" float4 time;\n "
134
+ @" float4 sinTime;\n "
135
+ @" float4 cosTime;\n "
136
+ @" float4 random01;\n "
137
+ @" } CCGlobalUniforms;\n " ;
138
+
114
139
typedef void (* GetShaderivFunc) (GLuint shader, GLenum pname, GLint* param);
115
140
typedef void (* GetShaderInfoLogFunc) (GLuint shader, GLsizei bufSize, GLsizei* length, GLchar* infoLog);
116
141
@@ -554,43 +579,13 @@ -(instancetype)initWithMetalVertexShaderSource:(NSString *)vertexSource fragment
554
579
{
555
580
CCMetalContext *context = [CCMetalContext currentContext ];
556
581
557
- // TODO this is a terrible terrible hack.
558
- NSString *header = CC_METAL (
559
- using namespace metal;
560
-
561
- typedef struct CCVertex {
562
- float4 position;
563
- float2 texCoord1;
564
- float2 texCoord2;
565
- float4 color;
566
- } CCVertex;
567
-
568
- typedef struct CCFragData {
569
- float4 position [[position]];
570
- float2 texCoord1;
571
- float2 texCoord2;
572
- half4 color;
573
- } CCFragData;
574
-
575
- typedef struct CCGlobalUniforms {
576
- float4x4 projection;
577
- float4x4 projectionInv;
578
- float2 viewSize;
579
- float2 viewSizeInPixels;
580
- float4 time;
581
- float4 sinTime;
582
- float4 cosTime;
583
- float4 random01;
584
- } CCGlobalUniforms;
585
- );
586
-
587
582
id <MTLFunction > vertexFunction = nil ;
588
583
if (vertexSource == CCDefaultVShader){
589
584
// Use the default vertex shader.
590
585
vertexFunction = [context.library newFunctionWithName: @" CCVertexFunctionDefault" ];
591
586
} else {
592
587
// Append on the standard header since JIT compiled shaders can't use #import
593
- vertexSource = [header stringByAppendingString: vertexSource];
588
+ vertexSource = [CCMetalShaderHeader stringByAppendingString: vertexSource];
594
589
595
590
// Compile the vertex shader.
596
591
NSError *verr = nil ;
@@ -601,7 +596,7 @@ -(instancetype)initWithMetalVertexShaderSource:(NSString *)vertexSource fragment
601
596
}
602
597
603
598
// Append on the standard header since JIT compiled shaders can't use #import
604
- fragmentSource = [header stringByAppendingString: fragmentSource];
599
+ fragmentSource = [CCMetalShaderHeader stringByAppendingString: fragmentSource];
605
600
606
601
// compile the fragment shader.
607
602
NSError *ferr = nil ;
0 commit comments