diff --git a/cmake/TaichiCore.cmake b/cmake/TaichiCore.cmake index 53590fea1a433a..69db6709ddea4b 100644 --- a/cmake/TaichiCore.cmake +++ b/cmake/TaichiCore.cmake @@ -88,7 +88,8 @@ if (TI_WITH_CUDA) target_link_libraries(${LIBRARY_NAME} ${llvm_ptx_libs}) endif() -target_link_libraries(${LIBRARY_NAME} /usr/lib/libGLESv2_nvidia.so GLESv2_nvidia) +#target_link_libraries(${LIBRARY_NAME} /usr/lib/libGL.so GL) +target_link_libraries(${LIBRARY_NAME} /usr/lib/libGLEW.so GLEW) target_link_libraries(${LIBRARY_NAME} /usr/lib/libglfw.so glfw) # add_executable(runtime runtime/runtime.cpp) diff --git a/taichi/backends/codegen_opengl.cpp b/taichi/backends/codegen_opengl.cpp index afb02cea22b229..731ca5c83eef07 100644 --- a/taichi/backends/codegen_opengl.cpp +++ b/taichi/backends/codegen_opengl.cpp @@ -47,6 +47,7 @@ class KernelGen : public IRVisitor bool is_top_level_{true}; std::string kernel_name_; + std::string glsl_kernel_name_; std::string glsl_kernel_prefix_; int glsl_kernel_count_{0}; @@ -71,11 +72,23 @@ class KernelGen : public IRVisitor void generate_header() { emit("#version 430 core"); - //emit("#version 310 es"); emit("#extension GL_ARB_compute_shader: enable"); - emit("layout (location = 0) in float input;"); - emit("layout (location = 1) out float output;"); emit(""); + emit("layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;"); + emit(""); + emit("layout(std430, binding = 0) buffer data"); + emit("{{"); + emit(" float tmp7;"); + emit("}};"); + emit(""); + } + + void generate_bottom() + { + emit("void main()"); + emit("{{"); + emit(" {}();", glsl_kernel_name_); + emit("}}"); } void visit(Block *stmt) override @@ -190,7 +203,7 @@ class KernelGen : public IRVisitor std::string make_kernel_name() { - return fmt::format("{}_{}", glsl_kernel_prefix_, glsl_kernel_count_++); + return fmt::format("{}{}", glsl_kernel_prefix_, glsl_kernel_count_++); } void emit_glsl_kernel_func_sig(const std::string &kernel_name) @@ -201,10 +214,10 @@ class KernelGen : public IRVisitor void generate_serial_kernel(OffloadedStmt *stmt) { TI_ASSERT(stmt->task_type == OffloadedStmt::TaskType::serial); const std::string glsl_kernel_name = make_kernel_name(); + this->glsl_kernel_name_ = glsl_kernel_name; emit_glsl_kernel_func_sig(glsl_kernel_name); emit("{{ // serial"); stmt->body->accept(this); - emit("output = 233.0;"); emit("}}\n"); } @@ -237,6 +250,7 @@ class KernelGen : public IRVisitor generate_header(); irpass::print(kernel->ir); kernel->ir->accept(this); + generate_bottom(); } }; diff --git a/taichi/platform/opengl/opengl_api.cpp b/taichi/platform/opengl/opengl_api.cpp index 33447427f5a630..4489ee04883a37 100644 --- a/taichi/platform/opengl/opengl_api.cpp +++ b/taichi/platform/opengl/opengl_api.cpp @@ -1,8 +1,9 @@ -//#define USE_GLEW +#define USE_GLEW #ifdef USE_GLEW +#define GLEW_STATIC #include #else -#include +//#include #endif #include #include "opengl_api.h" @@ -37,9 +38,10 @@ struct GLShader GLShader &compile(const std::string &source) { - GLsizei length = 1; - const GLchar *source_cstr = source.c_str(); - glShaderSource(id_, 1, &source_cstr, &length); + // https://stackoverflow.com/questions/28527956/get-011-error-syntax-error-unexpected-end-when-trying-to-compile-shader + GLchar *source_cstr = new GLchar[source.size() + 1]; + std::strcpy(source_cstr, source.c_str()); + glShaderSource(id_, 1, &source_cstr, NULL); TI_INFO("[glsl] compiling shader:\n{}", source); glCompileShader(id_); GLint status = GL_TRUE; @@ -49,7 +51,7 @@ struct GLShader glGetShaderiv(id_, GL_INFO_LOG_LENGTH, &logLength); GLchar *log = new GLchar[logLength]; glGetShaderInfoLog(id_, logLength, &logLength, log); - TI_ERROR("[glsl] shader compile error: {}", log); + TI_ERROR("[glsl] shader compile error:\n{}", log); } return *this; } @@ -92,7 +94,7 @@ struct GLProgram glGetProgramiv(id_, GL_INFO_LOG_LENGTH, &logLength); GLchar *log = new GLchar[logLength]; glGetProgramInfoLog(id_, logLength, &logLength, log); - TI_ERROR("[glsl] program link error: {}", log); + TI_ERROR("[glsl] program link error:\n{}", log); } return *this; } @@ -112,53 +114,44 @@ struct GLProgram }; // https://blog.csdn.net/ylbs110/article/details/52074826 -struct GLVertexBuffer +// https://www.khronos.org/opengl/wiki/Shader_Storage_Buffer_Object +struct GLSSBO { GLuint id_; - GLVertexBuffer() + GLSSBO() { glGenBuffers(1, &id_); } - ~GLVertexBuffer() + ~GLSSBO() { glDeleteBuffers(1, &id_); } - GLVertexBuffer &bind() + GLSSBO &bind() { - glBindVertexArray(id_); + glBindBuffer(GL_SHADER_STORAGE_BUFFER, id_); return *this; } - GLVertexBuffer &delete_VAO() // ??? - { - glDeleteVertexArrays(1, &id_); - return *this; - } - - GLVertexBuffer &bind_data(void *data, size_t size, GLuint draw_type = GL_STATIC_DRAW) + GLSSBO &bind_data(void *data, size_t size, GLuint usage = GL_STATIC_READ) { this->bind(); - glBufferData(GL_ARRAY_BUFFER, size, data, draw_type); + glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, usage); return *this; } - // layout (location = location); type x[count]; and sizeof(x) = size; &x = vao + offset - GLVertexBuffer &set_attrib(GLuint type, size_t location, size_t offset, size_t count, size_t size) + GLSSBO &bind_base(size_t index) { this->bind(); - glVertexAttribPointer(location, count, GL_FLOAT, GL_FALSE, size, NULL); - glEnableVertexAttribArray(0); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, id_); return *this; } }; -void launch_glsl_kernel(std::string source) +void initialize_opengl() { - static bool gl_inited = false; - if (!gl_inited) { TI_INFO("[glsl] initializing GLFW (with hint GL 4.3)"); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); @@ -172,6 +165,19 @@ void launch_glsl_kernel(std::string source) TI_ERROR("[glsl] cannot initialize GLEW: {}", glewGetErrorString(status)); } #endif + const char *gl_version = (const char *)glGetString(GL_VERSION); + if (!gl_version) { + TI_WARN("[glsl] cannot get GL_VERSION"); + } else { + TI_INFO("[glsl] GL_VERSION: {}", gl_version); + } +} + +void launch_glsl_kernel(std::string source) +{ + static bool gl_inited = false; + if (!gl_inited) { + initialize_opengl(); gl_inited = true; } GLShader shader(source); @@ -179,16 +185,18 @@ void launch_glsl_kernel(std::string source) program.link(); program.use(); - GLVertexBuffer vbo; - float data[2] = {0.5, 0.0}; - vbo.bind_data(data, sizeof(data)); - vbo.set_attrib(GL_FLOAT, 0, 0, 1, 1 * sizeof(float)); - vbo.set_attrib(GL_FLOAT, 1, 1, 1, 1 * sizeof(float)); - TI_INFO("[glsl] output before: {}", data[1]); + struct { + float number; + } data; + data.number = 233.3; + GLSSBO ssbo; + ssbo.bind_data(&data, sizeof(data), GL_DYNAMIC_READ); + ssbo.bind_base(0); + TI_INFO("[glsl] dispatching compute..."); // https://www.khronos.org/opengl/wiki/Compute_Shader glDispatchCompute(1, 1, 1); - TI_INFO("[glsl] output after: {}", data[1]); + TI_INFO("[glsl] data.number = {}", data.number); } bool is_opengl_api_available()