James-Jones / HLSLCrossCompiler

Other
468 stars 81 forks source link

Implicit cast from float to uint in GLSL #20

Closed nosferalatu closed 10 years ago

nosferalatu commented 10 years ago

When calling glCompileShader() with GLSL generated by HLSLCrossCompiler, GL says 'error C7011: implicit cast from "float" to "uint"'. I'm using an NVIDIA GPU. I tracked down the issue to this line of D3D microcode:

movc r0.x, cb0[391].y, r0.x, l(0)

This cross compiles into:

if(vec4(GlobalsVS.g_bFade).x != 0) {
    Temp_uint[0].x = Temp_uint[0].x;
} else {
    Temp_uint[0].x = 0.000000;    // <-- This line causes the error
}

The issue seems to be that the HLSL literal 'l(0)' is cross compiled into '0.000000' and not '0'.

I'm about to start digging through the HLSLCrossCompiler code, but I'm not sure where to start. I assume that since it knows the left-hand side is a uint that it will be able to work out that the right-hand side should be a uint?

nosferalatu commented 10 years ago

Here's a small repro I've been able to make:

bool g_bBoolParam;
int  g_IntParam;

void TestVS(int4 vPos : POSITION, out float4 oPosition : POSITION)
{
    int4 value;
    if (g_bBoolParam)
        value = vPos;
    else
        value = 0;
    value += g_IntParam;
    oPosition = value;
}

I save the above as shader.hlsl and compile with:

fxc.exe /Tvs_4_0 /ETestVS /Foshader.bin shader.hlsl
hlslcc.exe -in=shader.bin -out=shader.glsl -lang=150

Also, I uncommented the ASSERT on line 2167 of toGLSLInstruction.c. That assertion is failing; the operand[2] is SVT_UINT and operand[3] is SVT_FLOAT.

James-Jones commented 10 years ago

The BitwiseOnFloat branch was created to sort out this and other problems. I'll see if I can finish it and move to master this\next week.

nosferalatu commented 10 years ago

Thanks, James. Is there anything I could do to help out? Is that branch in a working state that I could checkout and test with my shaders? Otherwise, I'll be looking forward to when that's merged with master.

We're integrating HLSLCrossCompiler into the shader pipeline at my employer, Double Fine. Our shader source code is ~15K lines of HLSL, although that's before shader permutation generation. Everything's been successfully converted by HLSLCC, but at runtime, GLSL reports many implicit casting errors. Some good news, though, is that those seem to be the only errors reported.

James-Jones commented 10 years ago

Its a proof-of-concept branch which has gotten out of date, difficult to merge and only fixes a subset of opcodes. If need be I can lay the foundations and then describe what is left to do for you to look at so you don't wait on me.

nosferalatu commented 10 years ago

I just checked out the BitwiseOnFloat branch. It passes the first test (this issue) but fails the second test (issue 21 https://github.com/James-Jones/HLSLCrossCompiler/issues/21 ). However, I uncommented the block of code in toGLSLInstruction.c line 1775, and both tests now work.

"If need be I can lay the foundations and then describe what is left to do for you to look at so you don't wait on me."

That sounds great to me. I'd be happy to help out when it's ready; HLSLCrossCompiler will be very useful to our studio. |I was hoping we'd be able to use hlslcc immediately (production deadlines are looming :) but with a bit more work I think we'll be able to use it.

James-Jones commented 10 years ago

The reported shader now compiles.