From 44f5ac32a68a617f93704d44c4339f7db13b323e Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 15 Dec 2018 23:09:32 +0100 Subject: Hacky D2Q9 BGK LBM on GPU using GLSL compute shaders Improvised on top of computicles's scaffolding. Works in a world where _works_ is defined as "displays stuff on screen that invokes thoughts of fluid movement". --- src/shader/util.cc | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/shader/util.cc (limited to 'src/shader/util.cc') diff --git a/src/shader/util.cc b/src/shader/util.cc new file mode 100644 index 0000000..be59ac1 --- /dev/null +++ b/src/shader/util.cc @@ -0,0 +1,56 @@ +#include "util.h" + +#include +#include + +namespace util { + +GLint getUniform(GLuint program, const std::string& name) { + const GLint uniform = glGetUniformLocation(program, name.c_str()); + + if ( uniform == -1 ) { + std::cerr << "Could not bind uniform " << name << std::endl; + return -1; + } + + return uniform; +} + +GLint compileShader(const std::string& source, GLenum type) { + GLint shader = glCreateShader(type); + + if ( !shader ) { + std::cerr << "Cannot create a shader of type " << type << std::endl; + return -1; + } + + const char* source_data = source.c_str(); + const int source_length = source.size(); + + glShaderSource(shader, 1, &source_data, &source_length); + glCompileShader(shader); + + GLint compiled; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + + if ( !compiled ) { + std::cerr << "Cannot compile shader" << std::endl; + + GLint maxLength = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength); + + std::vector errorLog(maxLength); + glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]); + + for( auto c : errorLog ) { + std::cerr << c; + } + std::cerr << std::endl; + + return -1; + } + + return shader; +} + +} -- cgit v1.2.3