Skip to content

Commit 5128f96

Browse files
committed
Cleaning up the metal shader header for JIT compiled shaders.
1 parent c9ac7ba commit 5128f96

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

cocos2d/CCShader.m

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@
111111
@" cc_FragTexCoord2 = cc_TexCoord2;\n"
112112
@"}\n";
113113

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+
114139
typedef void (* GetShaderivFunc) (GLuint shader, GLenum pname, GLint* param);
115140
typedef void (* GetShaderInfoLogFunc) (GLuint shader, GLsizei bufSize, GLsizei* length, GLchar* infoLog);
116141

@@ -554,43 +579,13 @@ -(instancetype)initWithMetalVertexShaderSource:(NSString *)vertexSource fragment
554579
{
555580
CCMetalContext *context = [CCMetalContext currentContext];
556581

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-
587582
id<MTLFunction> vertexFunction = nil;
588583
if(vertexSource == CCDefaultVShader){
589584
// Use the default vertex shader.
590585
vertexFunction = [context.library newFunctionWithName:@"CCVertexFunctionDefault"];
591586
} else {
592587
// Append on the standard header since JIT compiled shaders can't use #import
593-
vertexSource = [header stringByAppendingString:vertexSource];
588+
vertexSource = [CCMetalShaderHeader stringByAppendingString:vertexSource];
594589

595590
// Compile the vertex shader.
596591
NSError *verr = nil;
@@ -601,7 +596,7 @@ -(instancetype)initWithMetalVertexShaderSource:(NSString *)vertexSource fragment
601596
}
602597

603598
// Append on the standard header since JIT compiled shaders can't use #import
604-
fragmentSource = [header stringByAppendingString:fragmentSource];
599+
fragmentSource = [CCMetalShaderHeader stringByAppendingString:fragmentSource];
605600

606601
// compile the fragment shader.
607602
NSError *ferr = nil;

0 commit comments

Comments
 (0)