Skip to content

Commit

Permalink
[OpenGL] Detect driver existence in with_opengl() (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate authored Apr 26, 2020
1 parent f3184a2 commit adb1c0d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ build_script:
- cd ..
- '%PYTHON% -c "import taichi"'
- "%PYTHON% examples/laplace.py"
- "%PYTHON% bin/taichi test -t 2 -na opengl"
- "%PYTHON% bin/taichi test -t 2"
- "cd python && %PYTHON% build.py try_upload"
39 changes: 31 additions & 8 deletions taichi/backends/opengl/opengl_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,18 @@ struct GLSSBO {
}
};

void initialize_opengl() {
static bool gl_inited = false;
if (gl_inited)
return;
gl_inited = true;
bool initialize_opengl(bool error_tolerance) {
static std::optional<bool> supported; // std::nullopt

if (supported.has_value()) { // this function has been called before
if (supported.value()) { // detected to be true in last call
return true;
} else {
if (!error_tolerance) // not called from with_opengl
TI_ERROR("OpenGL not supported");
return false;
}
}

glfwInit();
// Compute Shader requires OpenGL 4.3+ (or OpenGL ES 3.1+)
Expand All @@ -262,6 +269,13 @@ void initialize_opengl() {
if (!window) {
const char *desc = nullptr;
int status = glfwGetError(&desc);
if (error_tolerance && status == GLFW_API_UNAVAILABLE) {
// error tolerated, returning false
// note that we only tolerate GLFW_API_UNAVAILABLE
TI_TRACE("GLFW: OpenGL API unavailable");
supported = std::make_optional<bool>(false);
return false;
}
if (!desc)
desc = "Unknown Error";
TI_ERROR("[glsl] cannot create GLFW window: error {}: {}", status, desc);
Expand All @@ -277,8 +291,17 @@ void initialize_opengl() {
TI_TRACE("[glsl] Found " #x);
#include "taichi/inc/opengl_extension.inc.h"
#undef PER_OPENGL_EXTENSION
if (!opengl_has_GL_ARB_compute_shader)
if (!opengl_has_GL_ARB_compute_shader) {
if (error_tolerance) {
TI_INFO("Your OpenGL does not support GL_ARB_compute_shader extension");
supported = std::make_optional<bool>(false);
return false;
}
TI_ERROR("Your OpenGL does not support GL_ARB_compute_shader extension");
}

supported = std::make_optional<bool>(true);
return true;
}

void display_kernel_info(std::string const &kernel_name,
Expand Down Expand Up @@ -478,7 +501,7 @@ GLSLLaunchGuard::~GLSLLaunchGuard() {
}

bool is_opengl_api_available() {
return true;
return initialize_opengl(true);
}

int opengl_get_threads_per_group() {
Expand Down Expand Up @@ -522,7 +545,7 @@ bool is_opengl_api_available() {
return false;
}

void initialize_opengl() {
bool initialize_opengl(bool error_tolerance) {
TI_NOT_IMPLEMENTED;
}

Expand Down
2 changes: 1 addition & 1 deletion taichi/backends/opengl/opengl_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OffloadedStmt;

namespace opengl {

void initialize_opengl();
bool initialize_opengl(bool error_tolerance = false);
bool is_opengl_api_available();
int opengl_get_threads_per_group();
#define PER_OPENGL_EXTENSION(x) extern bool opengl_has_##x;
Expand Down

0 comments on commit adb1c0d

Please sign in to comment.