Skip to content

Instantly share code, notes, and snippets.

@JohnnyonFlame
Created February 1, 2018 11:28
Show Gist options
  • Save JohnnyonFlame/d5e0daf640b3ebd3c02f6e0327d53b55 to your computer and use it in GitHub Desktop.
Save JohnnyonFlame/d5e0daf640b3ebd3c02f6e0327d53b55 to your computer and use it in GitHub Desktop.
Application sample that crashes Mesa (OpenGL version string: 3.0 Mesa 17.4.0-devel - padoka PPA)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <SDL.h>
#include <GL/glew.h>
char frag_str[] =
"#version 450\n"
"\n"
"in vec2 frag_texCoord;\n"
"out vec4 outColor;\n"
"uniform sampler2D tex;\n"
"\n"
"void main()\n"
"{ \n"
" vec2 coord = vec2(frag_texCoord.x, 1.0f - frag_texCoord.y);\n"
" outColor = texture(tex, coord);\n"
"\n"
" uvec4 attrDecoder = uvec4(floatBitsToInt(outColor));\n"
" attrDecoder = ((attrDecoder>>8)&0xFF)|((attrDecoder<<8)&0xFF00);\n"
" outColor.rgba = (vec4(unpackHalf2x16(attrDecoder.x|(attrDecoder.y<<16)),unpackHalf2x16(attrDecoder.z|(attrDecoder.w<<16))));\n"
"}\n";
char vert_str[] =
"#version 150\n"
"\n"
"layout (std140) uniform uboTransform\n"
"{\n"
" mat4 proj;\n"
" mat4 view;\n"
" mat4 model;\n"
" vec3 viewPos;\n"
"};\n"
"\n"
"in vec3 aVertex;\n"
"in vec3 aNormal;\n"
"in vec2 aTexCoord;\n"
"\n"
"out vec2 frag_texCoord;\n"
"out vec3 frag_normal;\n"
"out float frag_depth;\n"
"out vec3 frag_wPos;\n"
"out vec3 frag_wView;\n"
"\n"
"void main()\n"
"{\n"
" frag_texCoord = aTexCoord;\n"
" frag_normal = vec4(aNormal, 1.0f).xyz;\n"
" frag_wPos = (model * vec4(aVertex, 1.0f)).xyz;\n"
" frag_wView = viewPos;\n"
" gl_Position = proj * view * model * vec4(aVertex, 1.0f);\n"
" frag_depth = gl_Position.z;\n"
"}\n";
GLuint Load(const char *shader, GLuint type)
{
GLuint id = 0;
id = glCreateShader(type);
glShaderSource(id, 1, &shader, NULL);
glCompileShader(id);
GLint status;
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE)
{
std::string error(512, '\0');
glGetShaderInfoLog(id, 512, NULL, (char*)error.data());
std::cerr << "Error compiling shader:\n" << error;
return false;
}
return id;
}
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
exit(-1);
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 5 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 768, SDL_WINDOW_OPENGL);
if (!window)
{
std::cout << "Failed to initialize SDL Window/Renderer: " << SDL_GetError();
exit(-1);
}
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glcontext);
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if (glewError != GLEW_OK)
{
std::cout << "Error initializing GLEW! " << glewGetErrorString(glewError) << "\n";
exit(5);
}
int minor, major;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
std::cout << "Using GL version " << major << "." << minor << "\n";
GLuint frag = Load(frag_str, GL_FRAGMENT_SHADER);
GLuint vert = Load(vert_str, GL_VERTEX_SHADER);
GLuint prog = glCreateProgram();
glAttachShader(prog, frag);
glAttachShader(prog, vert);
glLinkProgram(prog);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment