I am trying to implement insertion sort in GLSL, but there is an error in the for-loop when compiling the shader:
three.min.js:592 THREE.WebGLShader: gl.getShaderInfoLog() ERROR: 0:254: 'j' : Loop index cannot be initialized with non-constant expression
for (int i = 0; i < n - 1; i++)
{
for (int j = i+1; j > 0; j--)
{
if (angle[j - 1] > angle[j])
{
float temp = angle[j - 1];
angle[j - 1] = angle[j];
angle[j] = temp;
}
}
}
Any idea how to solve this problem?
Use bubble sort instead of insertion sort which does not include a non-constant expression in the for-loop. Code is written below.
float temp = 0.0;
for (int write = 0; write < n; write++)
{
for (int sort = 0; sort < n - 1; sort++)
{
if (angle[sort] > angle[sort + 1])
{
temp = angle[sort + 1];
angle[sort + 1] = angle[sort];
angle[sort] = temp;
}
}
}