GLSL 编译器错误错误 C0000:语法错误,意外标识符,在令牌 <var> 处需要“::”

GLSL Compiler Error error C0000: syntax error, unexpected identifier, expecting "::" at token <var>

我的系统配置如下: SDL2、Fedora 21、Nvidia GTX 驱动程序。

[mhoggan@localhost build]$ glxinfo | grep version
server glx version string: 1.4
client glx version string: 1.4
GLX version: 1.4
OpenGL core profile version string: 4.4.0 NVIDIA 346.47
OpenGL core profile shading language version string: 4.40 NVIDIA via Cg compiler
OpenGL version string: 4.5.0 NVIDIA 346.47
OpenGL shading language version string: 4.50 NVIDIA
OpenGL ES profile version string: OpenGL ES 3.1 NVIDIA 346.47
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.10
    GL_EXT_shader_implicit_conversions, GL_EXT_shader_integer_mix, 
[mhoggan@localhost build]$ uname -a
Linux localhost.san.rr.com 3.19.3-200.fc21.x86_64 #1 SMP Thu Mar 26 21:39:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

在使用 glCreateShader 创建着色器并将以下源传递到 glCompileShader 之后,顶点和片段着色器出现相同的错误:

Going to compile vertex source:
attribute vec3 position;attribute vec4 color;varying frag_color;void main() {  gl_Position = vec4(position, 1.0);  frag_color = color;} 

Going to compile fragment source:
varying vec4 frag_color;void main() {  gl_FragColor = frag_color;}

我得到的错误是:

Vertex shader compilation: 0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "frag_color"

Frament shader compilation: 0(1) : error C0000: syntax error, unexpected identifier, expecting "::" at token "frag_color"

导致此错误的代码在两者之间是相同的 类,但产生以下输出的调用顺序是:

// Note that after each opengl call I call glError thorugh the macro
// GL_CALL. I have verified there are no errors from glError.
_fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER); GL_CALL 
if(_fragment_shader_id == 0) {           
  compiler_errors = "ERROR: Error to get fragment shader id.";
  return compiler_errors;                
}

const GLchar *source = _fragment_shader_code.data();
printf("Going to compile fragment source: %s\n", source);
GLint source_cnt = 0;                    
glShaderSource(_fragment_shader_id, 1, &source, source_cnt);
GL_CALL                                  
glCompileShader(_fragment_shader_id); GL_CALL

为了查看它是否可能是版本问题,我尝试使用 SDL2 的 API.

将上下文创建更改为各种版本
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);

//Create window                          
gWindow = SDL_CreateWindow("Project", SDL_WINDOWPOS_UNDEFINED,
  SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
  SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if(gWindow == NULL) {                    
  printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
  assert(false);                         
} else {                                 
  gContext = SDL_GL_CreateContext(gWindow);     
  if(gContext == NULL) {                 
    printf("OpenGL 2.1 context could not be created! SDL Error: %s\n",
      SDL_GetError());                   
    assert(false);

您尚未在顶点着色器中为 "varying frag_color" 指定类型。我假设你的意思是 "varying vec4 frag_color".