Code Monkey home page Code Monkey logo

Comments (4)

vk2gpu avatar vk2gpu commented on May 18, 2024

Another example where the initial value in the loop is invalid, when using the same index variable across 2 loops:

Original HLSL:

int loopNum;

float4 main ( float4 i : POSITION ) : COLOR0 
{ 
    int myIdx;
    float4 output = float4( 0.0f, 0.0f, 0.0f, 0.0f );

    myIdx = 1;
    for(; myIdx < loopNum; ++myIdx )
    {
        output.x += 1.0f;
        output.y += 2.0f;
        output.z += 3.0f;
    }

    myIdx = 2;
    for(; myIdx < loopNum; ++myIdx )
    {
        output.x += 1.0f;
        output.y += 2.0f;
        output.z += 3.0f;
    }
    return output;
} 

GLSL from HLSL2GLSL:

#version 140
uniform int loopNum;
vec4 xlat_main( in vec4 i ) {
    int myIdx;
    vec4 xlat_varoutput = vec4( 0.0, 0.0, 0.0, 0.0);
        myIdx = 1;
    for ( ; (myIdx < loopNum); (++myIdx)) {
        xlat_varoutput.x += 1.0;
                xlat_varoutput.y += 2.0;
        xlat_varoutput.z += 3.0;
    }
        myIdx = 2;
    for ( ; (myIdx < loopNum); (++myIdx)) {
        xlat_varoutput.x += 1.0;
                xlat_varoutput.y += 2.0;
        xlat_varoutput.z += 3.0;
    }
    return xlat_varoutput;
}
out vec4 fragData[4];
void main() {
    vec4 xl_retval;
    xl_retval = xlat_main( vec4(0.0));
    fragData[0] = vec4(xl_retval);
}

// uniforms:
// loopNum:<none> type 5 arrsize 0

Raw GLSL from glsloptimiser:

#version 140
uniform int loopNum;
out vec4 fragData[4];
vec4 xlat_main (
  in vec4 i_1
)
{
  vec4 xlat_varoutput_2;
  int myIdx_3;
  vec4 tmpvar_4;
  tmpvar_4 = vec4(0.0, 0.0, 0.0, 0.0);
  xlat_varoutput_2 = tmpvar_4;
  int tmpvar_5;
  tmpvar_5 = 1;
  myIdx_3 = tmpvar_5;
  while (true) {
    if (!((myIdx_3 < loopNum))) {
      break;
    };
    float tmpvar_6;
    tmpvar_6 = (xlat_varoutput_2.x + 1.0);
    xlat_varoutput_2.x = tmpvar_6;
    float tmpvar_7;
    tmpvar_7 = (xlat_varoutput_2.y + 2.0);
    xlat_varoutput_2.y = vec2(tmpvar_7).y;
    float tmpvar_8;
    tmpvar_8 = (xlat_varoutput_2.z + 3.0);
    xlat_varoutput_2.z = vec3(tmpvar_8).z;
    int tmpvar_9;
    tmpvar_9 = (myIdx_3 + 1);
    myIdx_3 = tmpvar_9;
  };
  int tmpvar_10;
  tmpvar_10 = 2;
  myIdx_3 = tmpvar_10;
  while (true) {
    if (!((myIdx_3 < loopNum))) {
      break;
    };
    float tmpvar_11;
    tmpvar_11 = (xlat_varoutput_2.x + 1.0);
    xlat_varoutput_2.x = tmpvar_11;
    float tmpvar_12;
    tmpvar_12 = (xlat_varoutput_2.y + 2.0);
    xlat_varoutput_2.y = vec2(tmpvar_12).y;
    float tmpvar_13;
    tmpvar_13 = (xlat_varoutput_2.z + 3.0);
    xlat_varoutput_2.z = vec3(tmpvar_13).z;
    int tmpvar_14;
    tmpvar_14 = (myIdx_3 + 1);
    myIdx_3 = tmpvar_14;
  };
  return xlat_varoutput_2;
}

void main ()
{
  vec4 xl_retval_15;
  vec4 tmpvar_16;
  tmpvar_16 = xlat_main (vec4(0.0, 0.0, 0.0, 0.0));
  vec4 tmpvar_17;
  tmpvar_17 = tmpvar_16;
  xl_retval_15 = tmpvar_17;
  vec4 tmpvar_18;
  tmpvar_18 = xl_retval_15.xyzw;
  vec4 tmpvar_19;
  tmpvar_19 = tmpvar_18;
  fragData[0] = tmpvar_19;
}

Optmised GLSL:

#version 140
uniform int loopNum;
out vec4 fragData[4];
void main ()
{
  vec4 xlat_varoutput_1;
  xlat_varoutput_1 = vec4(0.0, 0.0, 0.0, 0.0);
  for (; myIdx_2 < loopNum; myIdx_2++) {
    xlat_varoutput_1.x = (xlat_varoutput_1.x + 1.0);
    xlat_varoutput_1.y = (xlat_varoutput_1.y + 2.0);
    xlat_varoutput_1.z = (xlat_varoutput_1.z + 3.0);
  };
  for (int myIdx_2; myIdx_2 < loopNum; myIdx_2++) {
    xlat_varoutput_1.x = (xlat_varoutput_1.x + 1.0);
    xlat_varoutput_1.y = (xlat_varoutput_1.y + 2.0);
    xlat_varoutput_1.z = (xlat_varoutput_1.z + 3.0);
  };
  fragData[0] = xlat_varoutput_1;
}


// inputs: 0, stats: 13 alu 0 tex 4 flow

from glsl-optimizer.

aras-p avatar aras-p commented on May 18, 2024

Fixed your second problem, not sure if fixes the first one. Do you have full shader that reproduces it?

from glsl-optimizer.

anchsm avatar anchsm commented on May 18, 2024

I'm getting a very similar issue from a simple pixel-shader, even after Aras' non-Inductors fix. This code (from hlsl2glsl) generates a non-initialized loop counter.

(I cut this code down a lot, I don't really want an empty loop, it's just a short way to show the bug)

#version 140
struct VS_OUTPUT {
    vec4 position;
    vec4 color;
    vec2 uv0;
};
uniform vec2 uv_offset = vec2( 0.0, 0.0);
uniform sampler2D input_texture ;
uniform vec2 blur_offset;
uniform float width = 8.0;
uniform float weight[16];
vec4 pixel_shader( in VS_OUTPUT xlat_var_input );
vec4 pixel_shader( in VS_OUTPUT xlat_var_input ) {
    vec4 color = texture( input_texture, xlat_var_input.uv0);
    int i = 1;
    for ( ; (float(i) < width); (i++)) {
        float factor = weight[i];
    }
    return color;
}
varying vec4 xlv_COLOR0;
varying vec2 xlv_TEXCOORD0;
void main() {
    vec4 xl_retval;
    VS_OUTPUT xlt_xlat_var_input;
    xlt_xlat_var_input.position = vec4(0.0);
    xlt_xlat_var_input.color = vec4(xlv_COLOR0);
    xlt_xlat_var_input.uv0 = vec2(xlv_TEXCOORD0);
    xl_retval = pixel_shader( xlt_xlat_var_input);
    gl_FragData[0] = vec4(xl_retval);
}

In the output, loop counter "i_1" is uninitialized:

#version 140
uniform sampler2D input_texture;
uniform float width = 8.0;
in vec2 xlv_TEXCOORD0;
void main ()
{
  vec4 tmpvar_2;
  tmpvar_2 = texture (input_texture, xlv_TEXCOORD0);
  for (int i_1; float(i_1) < width; i_1++) {
  };
  gl_FragData[0] = tmpvar_2;
}

from glsl-optimizer.

anchsm avatar anchsm commented on May 18, 2024

Looks like the optimizer only handles loop terminators where one side of the condition is a constant. From loop_analysis::visit_leave :

     ir_rvalue *counter = cond->operands[0]->as_dereference_variable();
     ir_constant *limit = cond->operands[1]->as_constant();
     enum ir_expression_operation cmp = cond->operation;

     if (limit == NULL) {
        counter = cond->operands[1]->as_dereference_variable();
        limit = cond->operands[0]->as_constant();

        switch (cmp) {
        case ir_binop_less:    cmp = ir_binop_greater; break;
        case ir_binop_greater: cmp = ir_binop_less;    break;
        case ir_binop_lequal:  cmp = ir_binop_gequal;  break;
        case ir_binop_gequal:  cmp = ir_binop_lequal;  break;
        default: assert(!"Should not get here.");
        }
     }

     if ((counter == NULL) || (limit == NULL))
        break;

from glsl-optimizer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.