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

[OpenGL] use GLAD as API loader (replace GLEW) #819

Merged
merged 14 commits into from
Apr 21, 2020
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
path = external/glfw
url = https://github.com/glfw/glfw
shallow = true
[submodule "external/glad"]
path = external/glad
url = https://github.com/taichi-dev/taichi_glad_ready
shallow = true
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"
- "%PYTHON% bin/taichi test -t 2 -a x64"
- "cd python && %PYTHON% build.py try_upload"
56 changes: 30 additions & 26 deletions cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ set(CORE_LIBRARY_NAME taichi_core)
option(USE_STDCPP "Use -stdlib=libc++" OFF)
option(TI_WITH_CUDA "Build with the CUDA backend" ON)
option(TI_WITH_OPENGL "Build with the OpenGL backend" ON)
option(GLEW_USE_STATIC_LIBS OFF)

if (APPLE)
if (TI_WITH_CUDA)
set(TI_WITH_CUDA OFF)
message(WARNING "CUDA not supported on OS X. Setting TI_WITH_CUDA to OFF.")
endif()
if (TI_WITH_OPENGL)
set(TI_WITH_OPENGL OFF)
message(WARNING "OpenGL not supported on OS X. Setting TI_WITH_OPENGL to OFF.")
endif()
endif()

if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/external/glad/src/glad.c")
set(TI_WITH_OPENGL OFF)
message(WARNING "external/glad submodule not detected. Settings TI_WITH_OPENGL to OFF.")
endif()

file(GLOB TAICHI_CORE_SOURCE
"taichi/*/*/*/*.cpp" "taichi/*/*/*.cpp" "taichi/*/*.cpp" "taichi/*.cpp"
"taichi/*/*/*/*.h" "taichi/*/*/*.h" "taichi/*/*.h" "taichi/*.h" "external/*.c" "tests/cpp/*.cpp")
"taichi/*/*/*/*.h" "taichi/*/*/*.h" "taichi/*/*.h" "taichi/*.h" "tests/cpp/*.cpp")

file(GLOB TAICHI_BACKEND_SOURCE "taichi/backends/**/*.cpp" "taichi/backends/**/*.h")

Expand All @@ -25,8 +33,8 @@ file(GLOB TAICHI_OPENGL_SOURCE "taichi/backends/opengl/*.h" "taichi/backends/ope
list(REMOVE_ITEM TAICHI_CORE_SOURCE ${TAICHI_BACKEND_SOURCE})

if (TI_WITH_CUDA)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_CUDA")
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_CUDA_SOURCE})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_CUDA")
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_CUDA_SOURCE})
endif()

if(NOT CUDA_VERSION)
Expand All @@ -37,6 +45,14 @@ endif()
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_METAL_SOURCE})
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_OPENGL_SOURCE})

if (TI_WITH_OPENGL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_OPENGL")
# Q: Why not external/glad/src/*.c?
# A: To ensure glad submodule exists when TI_WITH_OPENGL is ON.
file(GLOB TAICHI_GLAD_SOURCE "external/glad/src/glad.c")
list(APPEND TAICHI_CORE_SOURCE ${TAICHI_GLAD_SOURCE})
endif()

add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_CORE_SOURCE} ${PROJECT_SOURCES})

if (APPLE)
Expand All @@ -47,32 +63,20 @@ endif()
include_directories(${CMAKE_SOURCE_DIR})
include_directories(external/include)
include_directories(external/spdlog/include)
if (TI_WITH_OPENGL)
include_directories(external/glad/include)
endif()

set(LIBRARY_NAME ${CORE_LIBRARY_NAME})

if (TI_WITH_OPENGL)
if(NOT GLEW_VERSION)
set(GLEW_VERSION 2.0.0)
endif()
find_package(GLEW ${GLEW_VERSION})
if (GLEW_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTI_WITH_OPENGL")
message("Building with GLEW ${GLEW_VERSION}")
message("Using GLEW: ${GLEW_LIBRARIES}")
target_include_directories(${LIBRARY_NAME} PUBLIC ${GLEW_INCLUDE_DIRS})
target_link_libraries(${LIBRARY_NAME} ${GLEW_LIBRARIES} GLEW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGLEW_STATIC")

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

message("Building with GLFW")
add_subdirectory(external/glfw)
target_link_libraries(${LIBRARY_NAME} glfw)
else()
message(WARNING "GLEW not found, ignoring TI_WITH_OPENGL.")
endif()
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

message("Building with GLFW")
add_subdirectory(external/glfw)
target_link_libraries(${LIBRARY_NAME} glfw)
endif()

# http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project
Expand Down
1 change: 1 addition & 0 deletions external/glad
Submodule glad added at 7ec5f9
22 changes: 9 additions & 13 deletions taichi/backends/opengl/opengl_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "taichi/program/program.h"

#ifdef TI_WITH_OPENGL
#include "GL/glew.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#endif

Expand Down Expand Up @@ -245,7 +245,6 @@ void initialize_opengl() {
static bool gl_inited = false;
if (gl_inited)
return;
TI_WARN("OpenGL backend currently WIP, MAY NOT WORK");
gl_inited = true;

glfwInit();
Expand All @@ -257,7 +256,7 @@ void initialize_opengl() {
// 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);
glfwCreateWindow(1, 1, "Make OpenGL Context", nullptr, nullptr);
if (!window) {
const char *desc = nullptr;
int status = glfwGetError(&desc);
Expand All @@ -267,16 +266,13 @@ void initialize_opengl() {
}
glfwHideWindow(window);
glfwMakeContextCurrent(window);
int status = glewInit();
if (status != GLEW_OK) {
TI_ERROR("[glsl] cannot initialize GLEW: {}", glewGetErrorString(status));
}
TI_INFO("[glsl] OpenGL {}", (const char *)glGetString(GL_VERSION));
TI_INFO("[glsl] GLSL {}",
(const char *)glGetString(GL_SHADING_LANGUAGE_VERSION));
#define PER_OPENGL_EXTENSION(x) \
if ((opengl_has_##x = glewGetExtension(#x))) \
TI_INFO("[glsl] Found " #x);

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
TI_ERROR("[glsl] cannot initialize GLAD");
}
#define PER_OPENGL_EXTENSION(x) \
if ((opengl_has_##x = GLAD_##x)) \
TI_TRACE("[glsl] Found " #x);
#include "taichi/inc/opengl_extension.inc.h"
#undef PER_OPENGL_EXTENSION
if (!opengl_has_GL_ARB_compute_shader)
Expand Down
4 changes: 2 additions & 2 deletions taichi/program/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ void Program::materialize_layout() {
} else if (config.arch == Arch::opengl) {
opengl::OpenglStructCompiler scomp;
opengl_struct_compiled_ = scomp.run(*snode_root);
TI_INFO("OpenGL root buffer size: {} B",
opengl_struct_compiled_->root_size);
TI_TRACE("OpenGL root buffer size: {} B",
opengl_struct_compiled_->root_size);
opengl_kernel_launcher_ = std::make_unique<opengl::GLSLLauncher>(
opengl_struct_compiled_->root_size);
}
Expand Down