// Compile with // Implementation copied from James Legg from mesa bug 89590 // c++ -std=c++11 ./shader_crash.cc -Wall $(pkg-config --cflags --libs sdl2 gl) -o shared_constant_names #define GL_GLEXT_PROTOTYPES 1 #include #include #include #include #include const GLchar * const vertex_shader_source = R"foo(#version 120 attribute vec3 in_position; attribute vec2 in_texcoord; uniform mat4 mw; uniform mat4 mv; uniform mat4 vc; varying vec2 texcoord; void main() { vec4 w = mw * vec4(in_position, 1.0); vec4 v = mv * w; vec4 c = vc * v; gl_Position = c; texcoord = in_texcoord; } )foo"; const GLchar * const fragment_shader_source = R"foo(#version 120 uniform vec2 uvbias; varying vec2 texcoord; void main() { vec2 tmp = texcoord; vec2 uv = tmp * uvbias; tmp = (sign(uv) * floor(abs(uv))) + vec2(0.5, 0.5); tmp = tmp / uvbias; } )foo"; const GLfloat vertex_positions[][2] = {{-0.4f, -0.4f}, {-0.4f, 0.4f}, {0.4f, 0.4f}}; int main() { SDL_Init(SDL_INIT_VIDEO); SDL_Window * const window = SDL_CreateWindow( "test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 256, 256, SDL_WINDOW_OPENGL); if (!window) { std::cerr << "Could not create window: " << SDL_GetError() << '\n'; return 1; } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); SDL_GLContext gl_context = SDL_GL_CreateContext(window); glClear(GL_COLOR_BUFFER_BIT); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions), vertex_positions, GL_STATIC_DRAW); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, const_cast(&vertex_shader_source), 0); glCompileShader(vertex_shader); { GLint is_compiled; glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &is_compiled); assert(is_compiled == GL_TRUE); } const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, const_cast(&fragment_shader_source), 0); glCompileShader(fragment_shader); { GLint is_compiled; glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &is_compiled); assert(is_compiled == GL_TRUE); } GLuint shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glBindAttribLocation(shader_program, 0, "in_position"); glLinkProgram(shader_program); { GLint is_linked; glGetProgramiv(shader_program, GL_LINK_STATUS, &is_linked); assert(is_linked == GL_TRUE); } glUseProgram(shader_program); glDrawArrays(GL_TRIANGLES, 0, 3); SDL_GL_DeleteContext(gl_context); SDL_DestroyWindow(window); }