Odd shader compilation behavior

I don’t want to say it’s a bug without proof, but this is very odd stuff. If someone knows that I’m overlooking something obvious, please let me know.

I started building up a shader for my own terrain-like geometry, and I decided to try out the surface shader system. Here is what I tried to start with as a test.

Shader "Terrain/TerrainShad" {
    Properties {
		_MainTex ("Texture", 2D) = "white" {}
    }
	
	SubShader {
		Tags { "RenderType" = "Opaque" }
      
		CGPROGRAM
		#pragma surface surf Lambert
		
		struct Input {
			float4 worldPos;
		};

		sampler2D _MainTex;

		void surf (Input IN, inout SurfaceOutput o) {
			float2 calcUV = float2(IN.worldPos.x, IN.worldPos.z);
			
			float3 color = tex2D (_MainTex, calcUV).rgb;
			o.Albedo = color;
		}

		ENDCG
    } 
    Fallback "Diffuse"
}

Oddly, I get some errors when I import the shader.

Shader error in 'Terrain/TerrainShad': Program 'frag_surf', assignment of incompatible types at line 77
Shader error in 'Terrain/TerrainShad': Program 'vert_surf', assignment of incompatible types at line 77

I tried to open the compiled shader, but the entire CG block is replaced with an empty multiline comment, so I have no idea what the surface shader system expanded my shader into. I narrowed the issue down to using worldPos (a float4) to do anything related to texture lookups, even if I ensure that I’m using a float2 for the actual tex2D call. I’m assuming that the parser is doing something ridiculous and putting references to worldPos in the tex2D call, resulting in it thinking that I’m trying to use a float4 as UV coords.

I compiled a similar shader directly using cgc

struct Input {
	float4 worldPos;
};

sampler2D _MainTex;

void surf (Input IN, out float4 color : COLOR) {
	float2 calcUV = float2(IN.worldPos.x, IN.worldPos.z);
	
	color = tex2D (_MainTex, calcUV);
}
PS C:\Users\Stephen\desktop> cgc -profile ps_2_0 -entry surf shad.cg
shad.cg
11 lines, 0 errors.
ps_2_0
// cgc version 3.0.0016, build date Feb 11 2011
// command line args: -profile ps_2_0
// source file: shad.cg

I don’t feel like I’m doing anything tricky, especially since I’ve successfully used a similar shader in the OGRE rendering engine, also using Cg. It feels as if the surface shader parser is doing weird things that amount to buggy behavior.

Does anyone know what’s up, or alternatively, does anyone have any ideas to actually get the full source of the compiled shader so I can get a better idea of what’s happening?

Thank you.

Try a float3 for worldPos?

That was exactly the problem, it should be float3. I feel like I may be insane, because I could swear that every time I looked at the documentation and examples, worldPos was defined as a float4. I’m looking once more and they’re all float3. I changed it to float3 and it compiles just fine.

I’m glad it was something so trivial. I have no idea why I kept seeing it as float4.

Thanks, niosop.