GLSL版本兼容性

时间:2016-02-23 16:32:03

标签: opengl opengl-es shader

我有使用GLES 3.0的着色器:

#version 300 es

#ifdef GL_ES
precision mediump float;
#endif

in vec2 v_tex_coord;
uniform sampler2D s_texture;
uniform vec4 u_color;

out vec4 fragmentColor;

void main()
{
    fragmentColor = texture(s_texture, v_tex_coord)*u_color;
}

我希望他们能够使用OpenGL 4.1(而不是ES)。 是否存在在一个着色器中声明ES和桌面版本的方法?

1 个答案:

答案 0 :(得分:3)

您需要使用前缀字符串。 glShaderSource获取一个字符串数组,在处理之前,它将在概念上连接在一起。因此,您需要从着色器文件中删除#version声明,并将版本声明放入在运行时生成的字符串中。像这样:

GLuint CreateShader(const std::string &shader)
{
  GLuint shaderName = glCreateShader(GL_VERTEX_SHADER);

  const GLchar *strings[2];
  if(IS_USING_GLES)
    strings[0] = "#version 300 es";
  else
    strings[0] = "#version 410 core\n";

  strings[1] = (const GLchar*)shader.c_str();
  glShaderSource(shaderName, 2, strings, NULL);

  //Compile your shader and check for errors.

  return shaderName;
}

您的运行时应该知道它是ES还是桌面GL。

此外,ifdef声明周围不需要precision。在4.1之前,精确声明已成为桌面GL的一部分。虽然不可否认,他们实际上并没有做任何事情;)

相关问题