diff --git a/appveyor.yml b/appveyor.yml index dddcd4d9b2199..f1d48487d0494 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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" diff --git a/taichi/backends/opengl/opengl_api.cpp b/taichi/backends/opengl/opengl_api.cpp index b62d59b0a35c4..398ad4250586a 100644 --- a/taichi/backends/opengl/opengl_api.cpp +++ b/taichi/backends/opengl/opengl_api.cpp @@ -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 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+) @@ -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(false); + return false; + } if (!desc) desc = "Unknown Error"; TI_ERROR("[glsl] cannot create GLFW window: error {}: {}", status, desc); @@ -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(false); + return false; + } TI_ERROR("Your OpenGL does not support GL_ARB_compute_shader extension"); + } + + supported = std::make_optional(true); + return true; } void display_kernel_info(std::string const &kernel_name, @@ -478,7 +501,7 @@ GLSLLaunchGuard::~GLSLLaunchGuard() { } bool is_opengl_api_available() { - return true; + return initialize_opengl(true); } int opengl_get_threads_per_group() { @@ -522,7 +545,7 @@ bool is_opengl_api_available() { return false; } -void initialize_opengl() { +bool initialize_opengl(bool error_tolerance) { TI_NOT_IMPLEMENTED; } diff --git a/taichi/backends/opengl/opengl_api.h b/taichi/backends/opengl/opengl_api.h index 27643117b4c85..3e632aace3818 100644 --- a/taichi/backends/opengl/opengl_api.h +++ b/taichi/backends/opengl/opengl_api.h @@ -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;