diff --git a/taichi/backends/codegen_opengl.cpp b/taichi/backends/codegen_opengl.cpp index 03a9af7c93890..ae8579695edc6 100644 --- a/taichi/backends/codegen_opengl.cpp +++ b/taichi/backends/codegen_opengl.cpp @@ -507,12 +507,6 @@ FunctionType OpenglCodeGen::gen(void) FunctionType OpenglCodeGen::compile(Program &program, Kernel &kernel) { - static bool warned; - if (!warned) { - TI_WARN("OpenGL backend currently WIP, MAY NOT WORK"); - warned = true; - } - this->prog_ = &program; this->kernel_ = &kernel; diff --git a/taichi/platform/opengl/opengl_api.cpp b/taichi/platform/opengl/opengl_api.cpp index 68783c1cf3433..12f3f78a0407e 100644 --- a/taichi/platform/opengl/opengl_api.cpp +++ b/taichi/platform/opengl/opengl_api.cpp @@ -2,7 +2,6 @@ #ifdef TI_WITH_OPENGL -#define GLEW_STATIC #include #include #endif @@ -205,43 +204,42 @@ struct GLSSBO void initialize_opengl() { - glfwInit(); - // Compute Shader requires OpenGL 4.3+ (or OpenGL ES 3.1+) - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - // GLEW cannot load GL without a context - // And the best way to make context is by creating a window - // Then hide it immediately, LOL - GLFWwindow *window = glfwCreateWindow(1, 1, "Make GLEW Happy", nullptr, nullptr); - if (!window) { - const char *desc = nullptr; - GLint status = glfwGetError(&desc); - if (!desc) desc = "Unknown Error"; - TI_ERROR("[glsl] cannot create GLFW window: error {}: {}", status, desc); - } - glfwHideWindow(window); - glfwMakeContextCurrent(window); - GLint status = glewInit(); - if (status != GLEW_OK) { - TI_ERROR("[glsl] cannot initialize GLEW: {}", glewGetErrorString(status)); - } - const char *gl_version = (const char *)glGetString(GL_VERSION); - if (!gl_version) { - TI_WARN("[glsl] cannot get OpenGL version"); - } else { - TI_INFO("[glsl] OpenGL {}", gl_version); - } + static bool gl_inited = false; + if (gl_inited) + return; + TI_WARN("OpenGL backend currently WIP, MAY NOT WORK"); + gl_inited = true; + + glfwInit(); + // Compute Shader requires OpenGL 4.3+ (or OpenGL ES 3.1+) + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + // GLEW cannot load GL without a context + // And the best way to make context is by creating a window + // Then hide it immediately, LOL + GLFWwindow *window = glfwCreateWindow(1, 1, "Make GLEW Happy", nullptr, nullptr); + if (!window) { + const char *desc = nullptr; + GLint status = glfwGetError(&desc); + if (!desc) desc = "Unknown Error"; + TI_ERROR("[glsl] cannot create GLFW window: error {}: {}", status, desc); + } + glfwHideWindow(window); + glfwMakeContextCurrent(window); + GLint status = glewInit(); + if (status != GLEW_OK) { + TI_ERROR("[glsl] cannot initialize GLEW: {}", glewGetErrorString(status)); + } + const char *gl_version = (const char *)glGetString(GL_VERSION); + if (!gl_version) { + TI_WARN("[glsl] cannot get OpenGL version"); + } else { + TI_INFO("[glsl] OpenGL {}", gl_version); + } } void launch_glsl_kernel(std::string source, std::vector iov) { - static bool gl_inited = false; - if (!gl_inited) { - // TODO: move this to somewhere like main() - initialize_opengl(); - gl_inited = true; - } - GLShader shader(source); GLProgram program(shader); program.link(); diff --git a/taichi/platform/opengl/opengl_api.h b/taichi/platform/opengl/opengl_api.h index 665589c556118..0eb23347fc3bb 100644 --- a/taichi/platform/opengl/opengl_api.h +++ b/taichi/platform/opengl/opengl_api.h @@ -12,6 +12,7 @@ TLANG_NAMESPACE_BEGIN namespace opengl { +void initialize_opengl(); bool is_opengl_api_available(); void launch_glsl_kernel(std::string source, std::vector iov); diff --git a/taichi/program.cpp b/taichi/program.cpp index a1966765c0875..f1dce2960855c 100644 --- a/taichi/program.cpp +++ b/taichi/program.cpp @@ -221,6 +221,7 @@ void Program::materialize_layout() { opengl::OpenglStructCompiler scomp; opengl_struct_compiled_ = scomp.run(*snode_root); TI_INFO("OpenGL root buffer size: {} B", opengl_struct_compiled_->root_size); + opengl::initialize_opengl(); } }