I have done some work with shaders before but I would consider myself relatively inexperienced with GLSL. I am trying to write a fluid solver that simulates smoke using a series of fragment shaders. Most of these involve taking discrete samples from the texture and then doing some operation onh them, so they all have similar compilation errors. Every pixel, and only each pixel, must be evaluated so I am using texelFetch as opposed to texture2D for my sampling. The following is an example of such a fragment shader:
uniform sampler2D velocity;
uniform sampler2D pressure;
uniform sampler2D solid;
uniform float numCellsX;
uniform float numCellsY;
void main(void) {
ivec2 pos = ivec2(gl_FragCoord.xy);
if (texelFetch(solid, pos.xy, 0).x > 0.0)
discard;
float c = texelFetch(pressure, pos.xy, 0).x;
float up = texelFetchOffset(pressure, pos.xy, 0, ivec2(0, 1)).x;
float down = texelFetchOffset(pressure, pos.xy, 0, ivec2(0, -1)).x;
float left = texelFetchOffset(pressure, pos.xy, 0, ivec2(-1, 0)).x;
float right = texelFetchOffset(pressure, pos.xy, 0, ivec2(1, 0)).x;
float upS = texelFetchOffset(solid, pos.xy, 0, ivec2(0, 1)).x;
float downS = texelFetchOffset(solid, pos.xy, 0, ivec2(0, -1)).x;
float leftS = texelFetchOffset(solid, pos.xy, 0, ivec2(-1, 0)).x;
float rightS = texelFetchOffset(solid, pos.xy, 0, ivec2(1, 0)).x;
if (upS > 0.0) up = c;
if (downS > 0.0) down = c;
if (leftS > 0.0) left = c;
if (rightS > 0.0) right = c;
gl_FragColor = texelFetch(velocity, pos.xy, 0) - vec2(0.5 * numCellsX * (right - left), 0.5 * numCellsY * (up - down));
}
And when I run this through glSlangValidator, I get the following errors:
ERROR: 0:14: 'texelFetch' : no matching overloaded function found
ERROR: 0:14: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:17: 'texelFetch' : no matching overloaded function found
ERROR: 0:17: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:18: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:18: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:19: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:19: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:20: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:20: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:21: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:21: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:23: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:23: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:24: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:24: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:25: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:25: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:26: 'texelFetchOffset' : no matching overloaded function found
ERROR: 0:26: 'scalar swizzle' : not supported with this profile: es
ERROR: 0:33: 'texelFetch' : no matching overloaded function found
ERROR: 0:33: 'assign' : cannot convert from 'temp 2-component vector of float'
to 'fragColor mediump 4-component vector of float FragColor'
ERROR: 23 compilation errors. No code generated.
There is pretty much an error on almost every line and I have found very little information online about these things. Per the GLSL reference manual:
gvec4 texelFetch(gsampler2D sampler, ivec2 P, int lod);
I don't quite understand why it isn't accepting my texelFetch calls. I also don't know what scalar swizzle is (can't find this anywhere online) or why its an issue.
not supported with this profile: es
Perhaps you should compile it with the desktop version of GLSL, instead of GLSL ES (presumably 2.0). You should always use a #version
declaration in your GLSL shaders.
Also, if you're using texelFetch
, why are you still writing to deprecated outputs like gl_FragColor
?
However:
cannot convert from 'temp 2-component vector of float'
That's true regardless of the version for this line:
texelFetch(velocity, pos.xy, 0) - vec2(0.5 * numCellsX * (right - left), 0.5 * numCellsY * (up - down))
texelFetch
returns a 4-element vector. You cannot subtract a 2 element vector from a 4-element vector.