GLSL - Strange behavior with "inout" parameter - Bug?

Started by
2 comments, last by kloffy 8 years, 6 months ago
Is the following fragment shader valid GLSL code? (This is a somewhat artificial example to illustrate the problem.)
[source=cpp]
#version 440 core

out vec4 frag_color;

void set_value(inout mat4[1] m, int i, int j, int k, float value)
{
m[j][k] = value;
}

void main()
{
mat4[1] color;
set_value(color, 0, 0, 0, 0.5);
set_value(color, 0, 0, 1, 0.5);
set_value(color, 0, 1, 0, 0.5);
set_value(color, 0, 1, 1, 1.0);
frag_color = vec4(color[0][0][0], color[0][0][1], color[0][1][0], color[0][1][1]);
}
[/source]
I would expect a gray fragment, but instead I get what seemingly random/flashing colors. I think the problem is the mat4 array inout parameter. Possibly a driver bug?
Advertisement
[source=cpp]
#version 440 core

out vec4 frag_color;

float get(vec4 v, int i, int j) { return v[j]; }
float get(vec4[1] v, int i, int j) { return v[j]; }
float get(mat2 m, int i, int j, int k) { return m[j][k]; }
float get(mat2[1] m, int i, int j, int k) { return m[j][k]; }

void set(inout vec4 v, int i, int j, float value) { v[j] = value; }
void set(inout vec4[1] v, int i, int j, float value) { v[j] = value; }
void set(inout mat2 m, int i, int j, int k, float value) { m[j][k] = value; }
void set(inout mat2[1] m, int i, int j, int k, float value) { m[j][k] = value; }

void main()
{
#if 0
// Success
vec4 color;
set(color, 0, 0, 0.5);
set(color, 0, 1, 0.5);
set(color, 0, 2, 0.5);
set(color, 0, 3, 1.0);
frag_color = vec4(get(color, 0, 0), get(color, 0, 1), get(color, 0, 2), get(color, 0, 3));
#else
// Failure
mat2 color;
set(color, 0, 0, 0, 0.5);
set(color, 0, 0, 1, 0.5);
set(color, 0, 1, 0, 0.5);
set(color, 0, 1, 1, 1.0);
frag_color = vec4(get(color, 0, 0, 0), get(color, 0, 0, 1), get(color, 0, 1, 0), get(color, 0, 1, 1));
#endif
}
[/source]
Ok, I have updated my drivers and extended my test code and either I am missing something blindingly obvious, or matrices do not work as inout parameters.
You did not fully initialize the matrix. Getting random junk should be expected, or—more likely—it should not compile.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

You did not fully initialize the matrix. Getting random junk should be expected, or—more likely—it should not compile.

Are you sure? I am setting all of its values. Regardless, even if I initialize the matrix, I do not get the expected gray color.

[source=cpp]
mat2 color = mat2(1.0);
set(color, 0, 0, 0, 0.5);
set(color, 0, 0, 1, 0.5);
set(color, 0, 1, 0, 0.5);
set(color, 0, 1, 1, 1.0);
frag_color = vec4(get(color, 0, 0, 0), get(color, 0, 0, 1), get(color, 0, 1, 0), get(color, 0, 1, 1));
[/source]

This topic is closed to new replies.

Advertisement