Created
March 9, 2017 22:46
-
-
Save v3c70r/e517596bb0db68963b228fcc559af7ca to your computer and use it in GitHub Desktop.
GLSL_Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GL/glew.h> | |
#include <string> | |
#include <fstream> | |
#include <sstream> | |
#include <iostream> | |
#include <GLFW/glfw3.h> | |
const size_t MAX_SHADER_LOG_LENGTH = 2048; | |
std::string clangifyError(const char* err, const std::string& fileName) | |
{ | |
// Sample input: | |
// 0(17) : warning C7022: unrecognized profile specifier "asdf" | |
// 0(17) : error C0502: syntax error at token "asdf" | |
// Expected output | |
// fxaa.fs:22:37: error: use of undeclared identifier 'tex_coordsV' | |
// 0:17:0: error: syntax error at token "asdf" | |
std::stringstream ss; | |
std::string log(err); | |
unsigned first = log.find_first_of("("); | |
unsigned second = log.find_first_of(")"); | |
std::string rowNum = log.substr(first+1, second-first-1); | |
ss<<fileName<<":"<<log.substr(first+1, second-first-1)<<":0: "<<std::endl; | |
return ss.str(); | |
} | |
int main(int argc, char** argv) | |
{ | |
// TODO: Parameter validation | |
std::string fileName(argv[1]); | |
std::ifstream shaderIfstream(fileName); | |
std::string ext(fileName.substr(fileName.find_last_of(".")+1)); | |
GLenum shaderType; | |
if (ext == "fs") | |
shaderType = GL_FRAGMENT_SHADER; | |
else if (ext == "gs") | |
shaderType = GL_GEOMETRY_SHADER; | |
else if (ext == "vs") | |
shaderType = GL_VERTEX_SHADER; | |
else | |
shaderType = GL_INVALID_ENUM; | |
std::stringstream ss; | |
ss<<shaderIfstream.rdbuf(); | |
std::string shaderStr = ss.str(); | |
if (!glfwInit()) | |
{ | |
std::cerr<<"Could not init GLFW\n"; | |
return -1; | |
} | |
// Init OpenGL context | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); | |
GLFWwindow* offscreen_context = glfwCreateWindow(640, 480, "", NULL, NULL); | |
if (!offscreen_context) | |
{ | |
std::cerr <<"Unable to create window\n"; | |
return -1; | |
} | |
glfwMakeContextCurrent(offscreen_context); | |
glewExperimental = GL_TRUE; | |
if( glewInit() != GLEW_OK ) | |
{ | |
std::cerr << "Unable to init glew \n"; | |
return -1; | |
} | |
GLuint shader = glCreateShader(shaderType); | |
const GLchar *p = (const GLchar*) shaderStr.c_str(); | |
glShaderSource(shader, 1, &p, nullptr); | |
glCompileShader(shader); | |
int params = -1; | |
glGetShaderiv(shader, GL_COMPILE_STATUS, ¶ms); | |
if (params != GL_TRUE) | |
{ | |
GLchar log[MAX_SHADER_LOG_LENGTH]; | |
glGetShaderInfoLog(shader, MAX_SHADER_LOG_LENGTH, nullptr, log); | |
std::cerr<<clangifyError(log, fileName); | |
} | |
glDeleteShader(shader); | |
glfwDestroyWindow(offscreen_context); | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment