Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isolate opengl functions #1330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
5 changes: 1 addition & 4 deletions include/neural-graphics-primitives/marching_cubes.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <neural-graphics-primitives/bounding_box.cuh>
#include <neural-graphics-primitives/opengl_utils.h>

#include <tiny-cuda-nn/common.h>

Expand Down Expand Up @@ -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<float>& density, const fs::path& path, ivec3 res3d, float thresh, bool swap_y_z = true, float density_range = 4.f);
Expand Down
27 changes: 27 additions & 0 deletions include/neural-graphics-primitives/opengl_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <neural-graphics-primitives/common.h>

#ifdef NGP_GUI
# ifdef _WIN32
# include <GL/gl3w.h>
# else
# include <GL/glew.h>
# endif
# include <GLFW/glfw3.h>
# include <cuda_gl_interop.h>
#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
74 changes: 3 additions & 71 deletions src/marching_cubes.cu
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,13 @@
#include <neural-graphics-primitives/common.h>
#include <neural-graphics-primitives/random_val.cuh> // helpers to generate random values, directions
#include <neural-graphics-primitives/thread_pool.h>
#include <neural-graphics-primitives/opengl_utils.h>

#include <tiny-cuda-nn/gpu_memory.h>
#include <filesystem/path.h>

#include <stdarg.h>

#ifdef NGP_GUI
# ifdef _WIN32
# include <GL/gl3w.h>
# else
# include <GL/glew.h>
# endif
# include <GLFW/glfw3.h>
# include <cuda_gl_interop.h>
#endif

#include <vector>

using namespace tcnn;
Expand All @@ -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<char> 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<vec3>& verts,
const GPUMemory<vec3>& normals,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
78 changes: 78 additions & 0 deletions src/opengl_utils.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <neural-graphics-primitives/opengl_utils.h>


#include <vector>

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<char> 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
5 changes: 3 additions & 2 deletions src/testbed.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <neural-graphics-primitives/trainable_buffer.cuh>
#include <neural-graphics-primitives/triangle_bvh.cuh>
#include <neural-graphics-primitives/triangle_octree.cuh>
#include <neural-graphics-primitives/opengl_utils.h>

#include <tiny-cuda-nn/encodings/grid.h>
#include <tiny-cuda-nn/loss.h>
Expand Down Expand Up @@ -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);
Expand Down