当我尝试在Mac上为我的应用程序编译GLSL着色器时,它失败并出现着色器的版本冲突错误:
Error compiling vertex shader:
ERROR: 0:1: '' : version '130' is not supported
Error compiling shader:
ERROR: 0:1: '' : version '130' is not supported
着色器就是这样:
顶点着色器:
#version 130
in vec2 in_vPos;
in vec2 in_vTexCoord;
out vec2 s_vTexCoord;
void main()
{
gl_Position = vec4(in_vPos, 0, 1);
s_vTexCoord = (in_vTexCoord + vec2(1, 1)) / 2;
}
片段着色器:
#version 130
in vec2 s_vTexCoord;
out vec4 s_colOut;
uniform sampler2DRect s_texSampler;
uniform vec4 s_colBlend;
void main()
{
vec4 pixel = texture(s_texSampler, s_vTexCoord * textureSize(s_texSampler));
s_colOut = s_colBlend * pixel;
}
这是我初始化SDL渲染器和Glew的方式:
ren_pRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
glEnable(GL_MULTISAMPLE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE | SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
static bool _bInitializedGlew = false;
if(!_bInitializedGlew) {
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err != GLEW_OK) {
printf("Glew initialization error: %d\n", err);
}
_bInitializedGlew = true;
}
这就是我编译着色器的方法:
unsigned int CShader::CompileShader(const char* str, int type, bool &bSuccess)
{
// create a new shader
GLuint iShader = glCreateShader(type);
const GLchar* aSourceVertex[] = { str };
glShaderSource(iShader, 1, aSourceVertex, NULL);
glCompileShader(iShader);
// check if compiling went okay
GLint bShaderCompiled = GL_FALSE;
glGetShaderiv(iShader, GL_COMPILE_STATUS, &bShaderCompiled);
if(bShaderCompiled != GL_TRUE) {
// it did not.
printf("Error compiling %sshader:\n", (type == GL_VERTEX_SHADER ? "vertex " : ""));
int iLogLength = 0;
int iMaxLength = 0;
glGetShaderiv(iShader, GL_INFO_LOG_LENGTH, &iMaxLength);
char* buffer = new char[iMaxLength];
glGetShaderInfoLog(iShader, iMaxLength, &iLogLength, buffer);
if(iLogLength > 0) {
printf("%s\n", buffer);
}
delete[] buffer;
// report it back
bSuccess = false;
// and delete the shader
glDeleteShader(iShader);
} else {
// it worked!
bSuccess = true;
}
// return shader
return iShader;
}
之前我四处寻找答案,无法找到可以帮助我的确凿Stackoverflow答案,尽管它确实指出了SDL_GL_SetAttribute
用于设置GL版本和核心配置文件的正确方向。所以在上面的代码中,我将它设置为3.1 Core,according to Wikipedia,#version 140
,但即使我使用它,我也会得到同样的错误:
Error compiling vertex shader:
ERROR: 0:1: '' : version '140' is not supported
Error compiling shader:
ERROR: 0:1: '' : version '140' is not supported
编辑:我将属性中的版本更改为3.2 Core + Forward Compat,现在使用#version 150
我遇到了同样的问题,说150不受支持。
当我打印glGetString(GL_SHADING_LANGUAGE_VERSION)
的结果时,我得到1.20
。
即使我在创建SDL窗口后明确创建了上下文,它仍会报告1.20
:
win_pWindow = SDL_CreateWindow(strTitle, iX, iY, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | ulFlags);
win_pContext = SDL_GL_CreateContext(win_pWindow);
printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
答案 0 :(得分:3)
Error compiling vertex shader: ERROR: 0:1: '' : version '130' is not supported
这正是你的问题所在。 GLSL版本1.30涉及OpenGL-3.0;由于该版本没有核心/兼容性配置文件,但MacOS X不支持它。
对于现代OpenGL,MacOS X仅支持核心配置文件。因此,您需要核心配置文件上下文,并且必须使用核心配置文件版本编写着色器。 OpenGL-3核心配置文件仅在OpenGL-3.2中引入,相应的GLSL版本为1.50。所以你必须在那里写#version 150
。
答案 1 :(得分:2)
您没有创建您认为正在创建的上下文版本:
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE | SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG)
无效,SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG
不属于SDL_GL_CONTEXT_PROFILE_MASK
标志,而属于SDL_GL_CONTEXT_FLAGS
。
实际上,SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG
与SDL_GL_CONTEXT_PROFILE_COMPATIBILITY
具有相同的值,因此您确实同时请求了核心和兼容性配置文件。我怀疑你有一个遗留的上下文,在OSX上限于GL< = 2.1。
答案 2 :(得分:2)
确保您使用SDL_GLSetAttribute
设置版本等后,两者窗口和上下文。所以你的代码应该是这样的:
SDL_GLSetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GLSetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GLSetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
//create window
//create context
这就是我设法在我的mac上获得3.3上下文的方法。从您的代码中,我无法看到您是否在属性之后创建了上下文和窗口。
答案 3 :(得分:0)
我还是OpenGL和GLFW的新手,我不确定是什么问题,但是在macOS上也有同样的问题,但是将以下代码块放在这个位置可以解决问题。
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow *window = glfwCreateWindow( ... );