# How do I avoid using the wrong texture2D function in GLSL?

I was developing a small engine running on a combination of SDL and OpenGL, and I want to use that engine for a school project, so I needed to test it on a school PC to see the performance. This resulted in a shader compile error:

Shader compile ERROR
DEBUG INFO: ERROR: 0:12: 'texture' : no matching overloaded function found (using implicit conversion)
ERROR 0:12: '=' : cannot convert from 'const float' to '4-component vector of float'

Failed to load uniform

This is my fragment shader, where the error occured:

    #version 130

in vec4 fragmentColor;
in vec2 fragmentUV;

out vec4 color;

uniform float time;
uniform sampler2D mySampler;

void main(){
vec4 textureColor =  texture(mySampler, fragmentUV);

color = textureColor * fragmentColor;
}


I solved it by using the texture2D function, instead of texture. However, the older function is not recommended in newer code. This leaves me with two questions:

1. Why did this happen? Could it had been because of older drivers being installed on these PCs?

2. What should I do when I want to follow the new guidelines, and also to make it runnable on older machines? I thought about using two different shader,s and letting the program decide on the version, but I´m not sure.

You could use a macro:

#if __VERSION__ < 130
#define TEXTURE2D texture2D
#else
#define TEXTURE2D texture
#endif

...

vec4 textureColor =  TEXTURE2D(mySampler, fragmentUV);


I found this answer, that seems to have a compatibility include that handles more than just texture2d.

Your home PC probably has a higher shader version than the school PC, because the GPU is older. Older glsl versions used the texture2d function, while newer ones simply use texture.

You don't need to have 2 different shader codes, you can make it dynamic with preprocessors:

#if __VERSION__ > 120
color = texture(sampler, texCoord);
#else
color = texture2D(sampler, texCoord);
#endif


When the code gets compiled, opengl will decide which version to use.