From d6d5ad46566d3f064ed485b9bcbb1c01b1db889e Mon Sep 17 00:00:00 2001 From: JC <29726242+jc211@users.noreply.github.com> Date: Tue, 18 Apr 2023 16:20:35 +1000 Subject: [PATCH] isolate opengl functions --- CMakeLists.txt | 1 + .../marching_cubes.h | 5 +- .../neural-graphics-primitives/opengl_utils.h | 27 +++++++ src/marching_cubes.cu | 74 +----------------- src/opengl_utils.cu | 78 +++++++++++++++++++ src/testbed.cu | 5 +- 6 files changed, 113 insertions(+), 77 deletions(-) create mode 100644 include/neural-graphics-primitives/opengl_utils.h create mode 100644 src/opengl_utils.cu diff --git a/CMakeLists.txt b/CMakeLists.txt index d6ac83e9a..4a586de19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -275,6 +275,7 @@ list(APPEND NGP_SOURCES src/tinyexr_wrapper.cu src/tinyobj_loader_wrapper.cpp src/triangle_bvh.cu + src/opengl_utils.cu ) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/include/neural-graphics-primitives/marching_cubes.h b/include/neural-graphics-primitives/marching_cubes.h index 5d7ba5092..f128c42fd 100644 --- a/include/neural-graphics-primitives/marching_cubes.h +++ b/include/neural-graphics-primitives/marching_cubes.h @@ -15,6 +15,7 @@ #pragma once #include +#include #include @@ -64,10 +65,6 @@ void draw_mesh_gl( const vec2& screen_center, int mesh_render_mode ); - -void glCheckError(const char* file, unsigned int line); -uint32_t compile_shader(bool pixel, const char* code); -bool check_shader(uint32_t handle, const char* desc, bool program); #endif void save_density_grid_to_png(const tcnn::GPUMemory& density, const fs::path& path, ivec3 res3d, float thresh, bool swap_y_z = true, float density_range = 4.f); diff --git a/include/neural-graphics-primitives/opengl_utils.h b/include/neural-graphics-primitives/opengl_utils.h new file mode 100644 index 000000000..8b22e1b86 --- /dev/null +++ b/include/neural-graphics-primitives/opengl_utils.h @@ -0,0 +1,27 @@ +#pragma once +#include + +#ifdef NGP_GUI +# ifdef _WIN32 +# include +# else +# include +# endif +# include +# include +#endif + +NGP_NAMESPACE_BEGIN +#ifdef NGP_GUI + +enum eShaderType { + Fragment, + Vertex, + Geometry +}; + +void glCheckError(const char* file, unsigned int line); +bool check_shader(uint32_t handle, const char* desc, bool program); +uint32_t compile_shader(eShaderType shader_type, const char* code); +#endif //NGP_GUI +NGP_NAMESPACE_END \ No newline at end of file diff --git a/src/marching_cubes.cu b/src/marching_cubes.cu index daff134fd..12ffd43a7 100644 --- a/src/marching_cubes.cu +++ b/src/marching_cubes.cu @@ -17,22 +17,13 @@ #include #include // helpers to generate random values, directions #include +#include #include #include #include -#ifdef NGP_GUI -# ifdef _WIN32 -# include -# else -# include -# endif -# include -# include -#endif - #include using namespace tcnn; @@ -49,65 +40,6 @@ ivec3 get_marching_cubes_res(uint32_t res_1d, const BoundingBox &aabb) { } #ifdef NGP_GUI -void glCheckError(const char* file, unsigned int line) { - GLenum errorCode = glGetError(); - while (errorCode != GL_NO_ERROR) { - std::string fileString(file); - std::string error = "unknown error"; - // clang-format off - switch (errorCode) { - case GL_INVALID_ENUM: error = "GL_INVALID_ENUM"; break; - case GL_INVALID_VALUE: error = "GL_INVALID_VALUE"; break; - case GL_INVALID_OPERATION: error = "GL_INVALID_OPERATION"; break; - case GL_STACK_OVERFLOW: error = "GL_STACK_OVERFLOW"; break; - case GL_STACK_UNDERFLOW: error = "GL_STACK_UNDERFLOW"; break; - case GL_OUT_OF_MEMORY: error = "GL_OUT_OF_MEMORY"; break; - } - // clang-format on - - tlog::error() << "OpenglError : file=" << file << " line=" << line << " error:" << error; - errorCode = glGetError(); - } -} - -bool check_shader(uint32_t handle, const char* desc, bool program) { - GLint status = 0, log_length = 0; - if (program) { - glGetProgramiv(handle, GL_LINK_STATUS, &status); - glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); - } else { - glGetShaderiv(handle, GL_COMPILE_STATUS, &status); - glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); - } - if ((GLboolean)status == GL_FALSE) { - tlog::error() << "Failed to compile shader: " << desc; - } - if (log_length > 1) { - std::vector log; log.resize(log_length+1); - if (program) { - glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)log.data()); - } else { - glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)log.data()); - } - log.back() = 0; - tlog::error() << log.data(); - } - return (GLboolean)status == GL_TRUE; -} - -uint32_t compile_shader(bool pixel, const char* code) { - GLuint g_VertHandle = glCreateShader(pixel ? GL_FRAGMENT_SHADER : GL_VERTEX_SHADER ); - const char* glsl_version = "#version 140\n"; - const GLchar* strings[2] = { glsl_version, code}; - glShaderSource(g_VertHandle, 2, strings, NULL); - glCompileShader(g_VertHandle); - if (!check_shader(g_VertHandle, pixel? "pixel" : "vertex", false)) { - glDeleteShader(g_VertHandle); - return 0; - } - return g_VertHandle; -} - void draw_mesh_gl( const GPUMemory& verts, const GPUMemory& normals, @@ -171,7 +103,7 @@ void draw_mesh_gl( cudaGLUnmapBufferObject(els); if (!program) { - vs = compile_shader(false, R"foo( + vs = compile_shader(eShaderType::Vertex, R"foo( in vec3 pos; in vec3 nor; in vec3 col; @@ -196,7 +128,7 @@ void main() gl_Position = p; } )foo"); - ps = compile_shader(true, R"foo( + ps = compile_shader(eShaderType::Fragment, R"foo( out vec4 o; in vec3 vtxcol; uniform int mode; diff --git a/src/opengl_utils.cu b/src/opengl_utils.cu new file mode 100644 index 000000000..98c0d03bd --- /dev/null +++ b/src/opengl_utils.cu @@ -0,0 +1,78 @@ +#include + + +#include + +NGP_NAMESPACE_BEGIN +#ifdef NGP_GUI +void glCheckError(const char* file, unsigned int line) { + GLenum errorCode = glGetError(); + while (errorCode != GL_NO_ERROR) { + std::string fileString(file); + std::string error = "unknown error"; + // clang-format off + switch (errorCode) { + case GL_INVALID_ENUM: error = "GL_INVALID_ENUM"; break; + case GL_INVALID_VALUE: error = "GL_INVALID_VALUE"; break; + case GL_INVALID_OPERATION: error = "GL_INVALID_OPERATION"; break; + case GL_STACK_OVERFLOW: error = "GL_STACK_OVERFLOW"; break; + case GL_STACK_UNDERFLOW: error = "GL_STACK_UNDERFLOW"; break; + case GL_OUT_OF_MEMORY: error = "GL_OUT_OF_MEMORY"; break; + } + // clang-format on + + tlog::error() << "OpenglError : file=" << file << " line=" << line << " error:" << error; + errorCode = glGetError(); + } +} + +bool check_shader(uint32_t handle, const char* desc, bool program) { + GLint status = 0, log_length = 0; + if (program) { + glGetProgramiv(handle, GL_LINK_STATUS, &status); + glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); + } else { + glGetShaderiv(handle, GL_COMPILE_STATUS, &status); + glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); + } + if ((GLboolean)status == GL_FALSE) { + tlog::error() << "Failed to compile shader: " << desc; + } + if (log_length > 1) { + std::vector log; log.resize(log_length+1); + if (program) { + glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)log.data()); + } else { + glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)log.data()); + } + log.back() = 0; + tlog::error() << log.data(); + } + return (GLboolean)status == GL_TRUE; +} + +uint32_t compile_shader(eShaderType shader_type, const char* code) { + // translate enum to opengl shader type + GLenum shader_type_gl = 0; + std::string shader_type_str; + switch(shader_type) { + case eShaderType::Vertex: shader_type_gl = GL_VERTEX_SHADER; shader_type_str = "vertex"; break; + case eShaderType::Fragment: shader_type_gl = GL_FRAGMENT_SHADER; shader_type_str = "fragment"; break; + case eShaderType::Geometry: shader_type_gl = GL_GEOMETRY_SHADER; shader_type_str = "geometry"; break; + default: tlog::error() << "Unknown shader type"; return 0; + } + + GLuint g_VertHandle = glCreateShader(shader_type_gl); + const char* glsl_version = "#version 140\n"; + const GLchar* strings[2] = { glsl_version, code}; + glShaderSource(g_VertHandle, 2, strings, NULL); + glCompileShader(g_VertHandle); + if (!check_shader(g_VertHandle, shader_type_str.c_str(), false)) { + glDeleteShader(g_VertHandle); + return 0; + } + return g_VertHandle; +} + +#endif //NGP_GUI +NGP_NAMESPACE_END \ No newline at end of file diff --git a/src/testbed.cu b/src/testbed.cu index 22cf2e30d..f33c4d241 100644 --- a/src/testbed.cu +++ b/src/testbed.cu @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -2991,8 +2992,8 @@ void Testbed::create_second_window() { void main(){\n\ fragColor = texture(screenTex, texCoords.xy);\n\ }"; - vs = compile_shader(false, copy_shader_vert); - ps = compile_shader(true, copy_shader_frag); + vs = compile_shader(eShaderType::Vertex, copy_shader_vert); + ps = compile_shader(eShaderType::Fragment, copy_shader_frag); } m_second_window.window = glfwCreateWindow(win_w, win_h, "Fullscreen Output", NULL, m_glfw_window); if (win_x!=0x40000000) glfwSetWindowPos(m_second_window.window, win_x, win_y);