0

I have a vertex shader, shared between several pixel shaders - GLSL ES 2.0 on iOS. The VS outputs transformed position, normal and one set of 2D uv coords. Here is the bare-bones of it:

void main() {
    vec4 xlt_outPos;
    vec3 xlt_outNormal;
    vec2 xlt_uv0;
    xray_bone_vp( vec4(vertex), vec3(normal), vec2(uv), xlt_outPos, xlt_outNormal, xlt_uv0);
    gl_Position = vec4( xlt_outPos);
    xlv_TEXCOORD6 = vec3( xlt_outNormal);
    xlv_TEXCOORD0 = vec2( xlt_uv0);
}

This is based on a Cg shader which outputs uv to TEXCOORD0 and uses TEXCOORD6 for the normal.

My problem is that PS which take uv coords and normal as inputs are working fine, but those which take the normal only are not. If I change a PS method signature to be passed uv but don't use this, it works!

Is this expected, well-defined behaviour? If so, is there a way to avoid passing un-wanted parameters to shaders or having to create several versions of the VS?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • Could you share code of VS output structure and corresponding PS input in both working and broken shaders? In general these structures must be equal. – brigadir Feb 10 '14 at 14:39
  • I have it but not somewhere I can reach it right now. However I think "In general these structures must be equal" is the answer I'm looking for... if you can write that as an answer with any kind of references, the rep is yours :) – Mr. Boy Feb 17 '14 at 14:07
  • Okay. Anyway, it would be useful to look at your vertex-to-pixel-shader structure declaration. – brigadir Feb 17 '14 at 15:02

1 Answers1

1

I can suppose that VS output structure and PS input are different. Correct code should be like this:

// declare vertex-to-pixel data structure:
struct v_Output {
    float4 position : POSITION;
    float2 texCoord0 : TEXCOORD0;
    float2 texCoord1 : TEXCOORD1;
};

v_Output vertex_shader(...) {
    ...
    return (v_Output)output_data;
}

void pixel_shader(float2 texCoord0 : TEXCOORD0,
                  float2 texCoord1 : TEXCOORD1,
                  out float4 color : COLOR {
    color = ...;
}

And there are some rules:

  • you cannot require in pixel shader field that vertex shader doesn't produce
  • TEXCOORD# must be in non-interrupting order (`TEXCOORD0, TEXCOORD1...)

Most of all you should change TEXCOORD6 to TEXCOORD1. Anyway it's hard to propose anything without your code. And it would be nice to get exact compile error when you set up your shaders.

Also, take a look at this reference to learn more about Cg shading language: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter03.html

brigadir
  • 6,874
  • 6
  • 46
  • 81