Porting GLSL to HLSL causes error in various occasions [bug]

  • #1, z stroncisMonday, 08. May 2017, 17:30 hodinky 8 years ago
    Defining variable with parameters (in this case boolean) and then using it in conditional "if()" with arguments, cross-compiler generates bad HLSL code and parser throws an error. I think it's confused, because this of this pattern "name()" is used in "if( ... )" conditional place. But because of this specific cause, there's a workaround, code comparison below.

    Error cause:
    bool myVar (in vec2 one, in vec2 two) { ... }
    
    if ( myVar(uvVarying.xy, s1) )
    ...

    it causes this HLSL
     if( myVar(input.uvVarying.xy, s1);
    )
      {
    ...


    Solution to problem:
    bool caseOne = myVar(uvVarying.xy, s1);
    
    if ( caseOne )
    ...

    Bažant

    42 Posts


  • #2, z stroncisTuesday, 09. May 2017, 22:12 hodinky 8 years ago
    Another, very similar problem, so i'm adding to same thread. Passing arguments in this fashion "c.rgb = vec3((c.r+c.g+c.b)/3.0);" causes error when generating HLSL "error X3014: incorrect number of arguments to numeric-type constructor".

    Workaround:
    float bw = (c.r+c.g+c.b)/3.0;
    c.rgb = vec3(bw, bw, bw);

    Tip: in some cases division by 4 creates luma closer to original. Even better dividing channels separatedly with ratios R=0.2 G=1 B=0.1, that's because human eye perceives most of green color and least of blue. This also opens doors for more creative bw conversions, which used in photography by adding color filters on camera lenses or in post production.

    Bažant

    42 Posts

  • #3, z SimonSWednesday, 10. May 2017, 17:01 hodinky 8 years ago
    First one I'll fix when I have time, but you really shouldn't branch like that. Better keep branching out of the shader.

    Second is wrong for most shader languages, it's only for convenience.

    Kapitán

    1582 Posts

  • #4, z stroncisWednesday, 10. May 2017, 18:20 hodinky 8 years ago
    Yes, i'm just reading about GPU structure, not that i understand much, but it have just one instruction pointer and most cases can be solved with min/max/clamp, instead of branching.

    As for second one, i had feeling, that problem not in compiler, just need to read specifications more thoroughly.

    Thank You.

    Bažant

    42 Posts