Skip to content

Commit

Permalink
initialize_opengl in materialize_layout
Browse files Browse the repository at this point in the history
fix typo
  • Loading branch information
archibate committed Feb 27, 2020
1 parent 4ee3102 commit b0a8886
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
6 changes: 0 additions & 6 deletions taichi/backends/codegen_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
66 changes: 32 additions & 34 deletions taichi/platform/opengl/opengl_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#ifdef TI_WITH_OPENGL

#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#endif
Expand Down Expand Up @@ -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> 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();
Expand Down
1 change: 1 addition & 0 deletions taichi/platform/opengl/opengl_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> iov);

Expand Down
1 change: 1 addition & 0 deletions taichi/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit b0a8886

Please sign in to comment.