7878 " uniform highp vec4 cc_SinTime;\n "
7979 " uniform highp vec4 cc_CosTime;\n "
8080 " uniform highp vec4 cc_Random01;\n\n "
81- " uniform lowp sampler2D cc_MainTexture;\n\n "
82- " varying lowp vec4 cc_FragColor;\n "
81+ " uniform " XSTR(CC_SHADER_COLOR_PRECISION) " sampler2D cc_MainTexture;\n\n "
82+ " varying " XSTR(CC_SHADER_COLOR_PRECISION) " vec4 cc_FragColor;\n "
8383 " varying highp vec2 cc_FragTexCoord1;\n "
8484 " varying highp vec2 cc_FragTexCoord2;\n\n "
8585 " // End Cocos2D shader header.\n\n " ;
9797
9898static const GLchar *CCFragmentShaderHeader =
9999 " #ifdef GL_ES\n "
100- " precision mediump float;\n \n"
100+ " precision " XSTR(CC_SHADER_DEFAULT_FRAGMENT_PRECISION) " float;\n "
101101 " #endif\n\n "
102102 " // End Cocos2D fragment shader header.\n\n " ;
103103
@@ -235,8 +235,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
235235static CCUniformSetter
236236SetFloat (NSString *name, GLint location)
237237{
238- return ^(CCRenderer *renderer, NSNumber *value ){
239- value = value ?: @ 0 ;
238+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
239+ NSNumber * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: @( 0.0 ) ;
240240 NSCAssert ([value isKindOfClass: [NSNumber class ]], @" Shader uniform '%@ ' value must be wrapped in a NSNumber." , name);
241241
242242 glUniform1f (location, value.floatValue );
@@ -246,8 +246,29 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
246246static CCUniformSetter
247247SetVec2 (NSString *name, GLint location)
248248{
249- return ^(CCRenderer *renderer, NSValue *value){
250- value = value ?: [NSValue valueWithGLKVector2: GLKVector2Make (0 .0f , 0 .0f )];
249+ NSString *textureName = nil ;
250+ bool pixelSize = [name hasSuffix: @" PixelSize" ];
251+ if (pixelSize){
252+ textureName = [name substringToIndex: name.length - @" PixelSize" .length];
253+ } else if ([name hasSuffix: @" Size" ]){
254+ textureName = [name substringToIndex: name.length - @" Size" .length];
255+ }
256+
257+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
258+ NSValue *value = shaderUniforms[name] ?: globalShaderUniforms[name];
259+
260+ // Fall back on looking up the actual texture size if the name matches a texture.
261+ if (value == nil && textureName){
262+ CCTexture *texture = shaderUniforms[textureName] ?: globalShaderUniforms[textureName];
263+ GLKVector2 sizeInPixels = GLKVector2Make (texture.pixelWidth , texture.pixelHeight );
264+
265+ GLKVector2 size = GLKVector2MultiplyScalar (sizeInPixels, pixelSize ? 1.0 : 1.0 /texture.contentScale );
266+ value = [NSValue valueWithGLKVector2: size];
267+ }
268+
269+ // Finally fall back on 0.
270+ if (value == nil ) value = [NSValue valueWithGLKVector2: GLKVector2Make (0 .0f , 0 .0f )];
271+
251272 NSCAssert ([value isKindOfClass: [NSValue class ]], @" Shader uniform '%@ ' value must be wrapped in a NSValue." , name);
252273
253274 if (strcmp (value.objCType , @encode (GLKVector2)) == 0 ){
@@ -268,8 +289,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
268289static CCUniformSetter
269290SetVec3 (NSString *name, GLint location)
270291{
271- return ^(CCRenderer *renderer, NSValue *value ){
272- value = value ?: [NSValue valueWithGLKVector3: GLKVector3Make (0 .0f , 0 .0f , 0 .0f )];
292+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
293+ NSValue * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKVector3: GLKVector3Make (0 .0f , 0 .0f , 0 .0f )];
273294 NSCAssert ([value isKindOfClass: [NSValue class ]], @" Shader uniform '%@ ' value must be wrapped in a NSValue." , name);
274295 NSCAssert (strcmp(value.objCType, @encode (GLKVector3)) == 0, @"Shader uniformm 'vec3 %@' value must be passed using [NSValue valueWithGLKVector3:]", name);
275296
@@ -281,8 +302,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
281302static CCUniformSetter
282303SetVec4 (NSString *name, GLint location)
283304{
284- return ^(CCRenderer *renderer, id value ){
285- value = value ?: [NSValue valueWithGLKVector4: GLKVector4Make (0 .0f , 0 .0f , 0 .0f , 1 .0f )];
305+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
306+ NSValue * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKVector4: GLKVector4Make (0 .0f , 0 .0f , 0 .0f , 1 .0f )];
286307
287308 if ([value isKindOfClass: [NSValue class ]]){
288309 NSCAssert (strcmp([(NSValue *)value objCType ], @encode (GLKVector4)) == 0, @"Shader uniformm 'vec4 %@' value must be passed using [NSValue valueWithGLKVector4:].", name);
@@ -301,8 +322,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
301322static CCUniformSetter
302323SetMat4 (NSString *name, GLint location)
303324{
304- return ^(CCRenderer *renderer, NSValue *value ){
305- value = value ?: [NSValue valueWithGLKMatrix4: GLKMatrix4Identity];
325+ return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
326+ NSValue * value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKMatrix4: GLKMatrix4Identity];
306327 NSCAssert ([value isKindOfClass: [NSValue class ]], @" Shader uniform '%@ ' value must be wrapped in a NSValue." , name);
307328 NSCAssert (strcmp(value.objCType, @encode (GLKMatrix4)) == 0, @"Shader uniformm 'mat4 %@' value must be passed using [NSValue valueWithGLKMatrix4:]", name);
308329
@@ -344,8 +365,8 @@ -(NSDictionary *)uniformSettersForProgram:(GLuint)program
344365 case GL_FLOAT_MAT4: uniformSetters[name] = SetMat4 (name, location); break ;
345366 case GL_SAMPLER_2D: {
346367 // Sampler setters are handled a differently since the real work is binding the texture and not setting the uniform value.
347- uniformSetters[name] = ^(CCRenderer *renderer, CCTexture *texture ){
348- texture = texture ?: [CCTexture none ];
368+ uniformSetters[name] = ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms ){
369+ CCTexture * texture = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [CCTexture none ];
349370 NSAssert ([texture isKindOfClass: [CCTexture class ]], @" Shader uniform '%@ ' value must be a CCTexture object." , name);
350371
351372 // Bind the texture to the texture unit for the uniform.
0 commit comments