My custom shader doesn't work on my macOS app

Hi there,
I’m using cocos2d 3.17 with Xcode 11. I’m doing a porting of a iOS app to a Mac app but the shader doesn’t work. The app gives me this error

1_ I think this is caused by this line in CCGLProgram, because the compileTimeHeaders it’s actually empty and it remain empty, for this the compiler cannot initialize the shaders and gives us the first error. I don’t understand: how can I compile shaders if they are not initialized?

bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source, const std::string& compileTimeHeaders, const std::string& convertedDefines)

{ …

std::string headersDef;
if (compileTimeHeaders.empty()) {
#if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
headersDef = (type == GL_VERTEX_SHADER ? “precision mediump float;\n precision mediump int;\n” : “precision mediump float;\n precision mediump int;\n”);
// Bugfix to make shader variables types constant to be understood by the current Android Virtual Devices or Emulators. This will also eliminate the 0x501 and 0x502 OpenGL Errors during emulation.
// Changed shader data types mediump to highp to remove possible sprite joggling on some Android phones.
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
headersDef = “#version 100\n precision highp float;\n precision highp int;\n”;
#elif (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
headersDef = (type == GL_VERTEX_SHADER ? “precision highp float;\n precision highp int;\n” : “precision mediump float;\n precision mediump int;\n”);
#endif
}else{
headersDef = compileTimeHeaders;
}

const GLchar *sources[] = {
    headersDef.c_str(),
    COCOS2D_SHADER_UNIFORMS,
    convertedDefines.c_str(),
    source};

2_ The second one is my actual custom shader that fails with the error signed by the arrow. When I play it on iOS it works, but if I use the same code for the Mac app, it gives me this trouble.

Thank you if someone will help me and sorry for my English.
Bye

try removing the lowp qualifier before vec4

Thank you for the reply! I changed it like this:

varying vec4 u_fragmentColor;
varying vec4 v_fragmentColor;

and I worked. But now, the shader.frag gives me this error:

shader.frag
#version 120

#ifdef GL_ES
precision lowp float;
#endif

uniform sampler2D u_texture;
varying vec4 v_fragmentColor;
uniform mat4 u_rotation;

void main()
{
mat4 t1= mat4(1);
mat4 t2= mat4(1);
t1[3] = vec4(-0.5,-0.5,1,1);
t2[3] = vec4(+0.5,+0.5,1,1);
vec2 pos = (t2 * u_rotation * t1 * vec4(gl_PointCoord, 0, 1)).xy;
gl_FragColor = v_fragmentColor * texture2D(u_texture, pos);
}

cocos2d: ERROR: 0:18: ‘’ : #version must occur before any other statement in the program

be careful that you only remove the lowp qualifier, it looks like you changed u_fragmentColor from a uniform to a varying which is not the same thing,
and for the second error try removing the #version 120 and see if it fixes it

First fix worked! Thank you!

and for the second error try removing the #version 120 and see if it fixes it

I’m not sure if this line is correct. If I remove it, this errors appear
cocos2d: ERROR: 0:34: Use of undeclared identifier 'gl_PointCoord’

ERROR: 0:35: Use of undeclared identifier 'pos’

Hi @ele_elion,
i think you need to use one of the create functions that will pass in “#version 120” as compileTimeHeaders. For eg:
https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/renderer/CCGLProgram.cpp#L179
or
don’t use their create, but alloc with new and init with this
https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/renderer/CCGLProgram.cpp#L312

Reason cos when you look at the sources
https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/renderer/CCGLProgram.cpp#L502
There are some defines that are added before the contents from your shader file.

And yes, you will need the #version 120 directive
https://stackoverflow.com/questions/7859848/gl-pointcoord-unavailable-on-mac

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.