Peter B. Peter B. - 1 month ago 20
C++ Question

'texture2D' : function is removed in Forward Compatibile context

I am trying to implement 5x5 convolution filter (Find Edges) using

GLSL
here is my code of
.glsl
source:

#version 150
#define SIZE 25

// A texture is expected
uniform sampler2D Texture;

// The vertex shader fill feed this input
in vec2 FragTexCoord;

// The final color
out vec4 FragmentColor;

float matr[25];
float matg[25];
float matb[25];

vec4 get_pixel(in vec2 coords, in float x, in float y) {
return texture2D(Texture, coords + vec2(x, y));
}

float convolve(in float[SIZE] kernel, in float[SIZE] matrix) {
float res = 0.0;
for (int i = 0; i < 25; i++)
res += kernel[i] * matrix[i];
return clamp(res, 0.0, 1.0);
}

void fill_matrix() {
float dxtex = 1.0 / float(textureSize(Texture, 0));
float dytex = 1.0 / float(textureSize(Texture, 0));
float[25] mat;
for (int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++) {
vec4 pixel = get_pixel(FragTexCoord,float(i - 2) * dxtex, float(j - 2) * dytex);
matr[i * 5 + j] = pixel[0];
matg[i * 5 + j] = pixel[1];
matb[i * 5 + j] = pixel[2];
}
}

void main() {

float[SIZE] ker_edge_detection = float[SIZE] (
.0,.0, -1., .0, .0,
.0, .0,-1., .0, .0,
.0, .0, 4., .0, .0,
.0, .0, -1., .0,.0,
.0, .0, -1., .0, .0
);

fill_matrix();

FragmentColor = vec4(convolve(ker_edge_detection,matr), convolve(ker_edge_detection,matg), convolve(ker_edge_detection,matb), 1.0);
}


When I run my code it gives me errors:

Error Compiling Fragment Shader ...
ERROR: 0:17: 'texture2D' : function is removed in Forward Compatibile context
ERROR: 0:17: 'texture2D' : no matching overloaded function found (using implicit conversion)


Error Linking Shader Program ...
Attached fragment shader is not compiled.

Function is removed in Forward Compatibile context


Weird thing is, when I've tried to run code on other linux and then windows machines it just worked fine. Also when I changed
texture2D
in
get_pixel()
function to return
texture
only it worked like a charm. Could someone explain where is a problem?

Answer

The error says everything you need to know. texture2D is an old function from the GLSL 1.00 days. It was removed and replaced with texture, which uses function overloading to work on most sampler types, rather than being restricted to sampler2D. So in a core profile or forward compatibility context, you cannot call texture2D.