Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I made a game on PSM, ported from a previous iOS/Android game. I test it on the emulator and PSVita and runs fine. However, as I test it on a PlayStation Certified Android device, it crashes on load. I'm getting a FileLoadException on the following line:

shaderProgram = new ShaderProgram("/Application/shaders/Render.cgx");

This is my vertex shader:

void main( float4 in spritePosition  : POSITION,
           float2 in textureCoords : TEXCOORD0,
           float4 out gl_Position: POSITION,
           float2 out textureVarying: TEXCOORD0,
           float4 out colorVarying : COLOR0,
           uniform float4x4 projectionMatrix,
           uniform float4 color)
{
    gl_Position = mul(spritePosition, projectionMatrix);
    textureVarying = textureCoords;

    colorVarying = color;
}

and my fragment shader:

void main( float4 in colorVarying : COLOR0,
           float2 in textureVarying: TEXCOORD0,
           float4 out gl_FragColor: COLOR,
           uniform sampler2D spriteTexture: TEXUNIT0)
{
    float4 colorPremultiplied = float4(colorVarying.xyz * colorVarying.w, colorVarying.w);
    gl_FragColor = tex2D(spriteTexture, textureVarying) * colorPremultiplied;
}

What could be causing this problem?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

The answer is in the documentation, in "API Overview/Graphics Overview":

Shader descriptions have the following restrictions in comparison with standard Cg language.

  • Variables and functions with the same names as GLSL and Cg reserved words cannot be used.

It turns that the emulator, the PSVita and Android devices all have very different rendering engines, and the shader language used by PSM somewhat allows shader compilation for all targets using a variant of Cg.

  • The emulator uses DirectX, which uses HLSL. In this case, variable semantics come from the names after the colons in the function declarations, such as COLOR0, and TEXUNIT0 (which are aptly called variable semantics). You can name your variables whatever you like as long as you set the semantics correctly.

  • I don't know what the PSVita uses, but it also seems to work with variable semantics.

  • However, Android devices use GLES, which uses GLSL. In this case, variable semantics are determined by the variable name, such as gl_Position and gl_FragColor instead of the declared semantics.

Most likely runtime shader compilation happens in two steps: first the pseudo-Cg code is translated into the target platform's supported shader language; and then the resulting code is compiled by the actual platform.

In Android, I guess the translation for this sample shader goes from this (pseudo-Cg):

void main(  float4 in variable1 : POSITION
            float4 out variable2 : POSITION)
{
    variable2 = something;
}

to this (GLSL):

attribute vec4 variable1;

void main()
{
    vec4 variable2 = something; // Make variable2 a local variable
    gl_Position = variable2; // And assign it to gl_Position at the very end
}

Notice that the variable names are kept untouched from the original code. So if my shader uses a reserved variable name like this (pseudo-Cg):

void main(  float4 in variable1 : POSITION
            float4 out gl_Position : POSITION)
{
    gl_Position = something;
}

The shader code will get translated like this (GLSL):

attribute vec4 variable1;

void main()
{
    vec4 gl_Position = something; // <-- ERROR! gl_Position is reserved, you can't define it!
    gl_Position = gl_Position;
}

Which will not compile.

So simply changing the offending variable names to some non-reserved name fixes this problem.

Just posting this in case somebody else runs into a similar problem.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.