Skip to content

Commit

Permalink
[skip ci] really run shader
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Feb 18, 2020
1 parent 4f8b696 commit a65a224
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
3 changes: 2 additions & 1 deletion cmake/TaichiCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 19 additions & 5 deletions taichi/backends/codegen_opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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");
}

Expand Down Expand Up @@ -237,6 +250,7 @@ class KernelGen : public IRVisitor
generate_header();
irpass::print(kernel->ir);
kernel->ir->accept(this);
generate_bottom();
}
};

Expand Down
76 changes: 42 additions & 34 deletions taichi/platform/opengl/opengl_api.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//#define USE_GLEW
#define USE_GLEW
#ifdef USE_GLEW
#define GLEW_STATIC
#include <GL/glew.h>
#else
#include <GLES3/gl32.h>
//#include <GLES3/gl32.h>
#endif
#include <GLFW/glfw3.h>
#include "opengl_api.h"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -172,23 +165,38 @@ 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);
GLProgram program(shader);
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()
Expand Down

0 comments on commit a65a224

Please sign in to comment.