hlsl2glslfork icon indicating copy to clipboard operation
hlsl2glslfork copied to clipboard

Implicit compile-type casting not working

Open philip-peterson opened this issue 11 years ago • 1 comments

Consider the following code from a surface shader:

float bar() {
    return tex2D(_PermTexture, float2(.5f, .5f));
}

float foo(float baz) {
    float2 mycoord = float2(baz, .2);
    return mycoord.x;
}

void surf (Input IN, inout EditorSurfaceOutput o) {
    o.Normal = float3(0.0,0.0,1.0);
    o.Alpha = 1.0;
    o.Albedo = 0.0;
    o.Emission = 0.0;
    o.Gloss = 0.0;
    o.Specular = 0.0;
    o.Custom = 0.0;

    o.Albedo = foo(bar());
    o.Normal = normalize(o.Normal);
}

The function float bar() is declared to return a single float value. The expression after return is the result of a call to tex2D, which is a float3, but since the function's signature is of type float, the first channel should be implicitly selected and used as the output, i.e. line 2 should effectively have a .x inserted before the semicolon by the compiler.

However, when we try to construct a float2 using baz and another float (line 6), the compiler/translator sees that baz "returned a float3" (line 2) and decides that a float3 and another float is "too much data". It is, in essence ignoring the fact that foo returns a float AND the fact that the dummy variable baz is declared to be of type float (line 5), and instead is looking solely at the code body (line 2) to determine a datatype.

There is no way, however that "baz" will end up being any more than a float at runtime. So there is no way (line 6) can be providing too much information to the float2 constructor at runtime. Yet, the compiler dies with the message "too much data in type constructor" at line 6.

philip-peterson avatar Apr 14 '13 10:04 philip-peterson

Since you're talking about surface shaders, that sounds like it's something Unity specific. You're sure the error is caused by hlsl2glsl, and not by any of the other ten-or-so shader compilers that Unity uses? (might be Cg, HLSL, HLSL360, CgPS3 etc.)

Do you have full shader that causes the problem?

aras-p avatar Apr 14 '13 12:04 aras-p