Skip to content

Commit

Permalink
remove glad, add temporary rpc opengl test
Browse files Browse the repository at this point in the history
  • Loading branch information
phisiart committed Jan 2, 2018
1 parent 2d969b8 commit 95ccc12
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 162 deletions.
4 changes: 0 additions & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@
[submodule "dlpack"]
path = dlpack
url = https://github.com/dmlc/dlpack
[submodule "glad"]
path = glad
url = https://github.com/Dav1dde/glad.git
branch = c
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,8 @@ if(USE_OPENGL)
find_package(glfw3 QUIET REQUIRED)
message(STATUS "Build with OpenGL support")
include_directories(${OPENGL_INCLUDE_DIRS})
include_directories(glad/include)
list(APPEND TVM_RUNTIME_LINKER_LIBS ${OpenGL_LIBRARIES} glfw dl)
list(APPEND RUNTIME_SRCS ${RUNTIME_OPENGL_SRCS} glad/src/glad.c)
list(APPEND RUNTIME_SRCS ${RUNTIME_OPENGL_SRCS})
add_definitions(-DTVM_OPENGL_RUNTIME=1)
else(USE_OPENGL)
add_definitions(-DTVM_OPENGL_RUNTIME=0)
Expand Down
15 changes: 4 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ UNAME_S := $(shell uname -s)
# The flags
LLVM_CFLAGS= -fno-rtti -DDMLC_ENABLE_RTTI=0 -DDMLC_USE_FOPEN64=0
LDFLAGS = -pthread -lm -ldl
INCLUDE_FLAGS = -Iinclude -I$(DLPACK_PATH)/include -I$(DMLC_CORE_PATH)/include -IHalideIR/src -Itopi/include -Iglad/include
INCLUDE_FLAGS = -Iinclude -I$(DLPACK_PATH)/include -I$(DMLC_CORE_PATH)/include -IHalideIR/src -Itopi/include
CFLAGS = -std=c++11 -Wall -O2 $(INCLUDE_FLAGS) -fPIC
FRAMEWORKS =
OBJCFLAGS = -fno-objc-arc
EMCC_FLAGS= -std=c++11 -DDMLC_LOG_STACK_TRACE=0\
-Oz -s RESERVED_FUNCTION_POINTERS=2 -s MAIN_MODULE=1 -s NO_EXIT_RUNTIME=1\
-O0 -s RESERVED_FUNCTION_POINTERS=2 -s MAIN_MODULE=1 -s NO_EXIT_RUNTIME=1\
-s EXTRA_EXPORTED_RUNTIME_METHODS="['cwrap','getValue','setValue','addFunction']"\
-s USE_GLFW=3 -s USE_WEBGL2=1 -lglfw\
$(INCLUDE_FLAGS)

# llvm configuration
ifdef LLVM_CONFIG
LLVM_VERSION=$(shell $(LLVM_CONFIG) --version| cut -b 1,3)
Expand All @@ -55,7 +55,6 @@ CUDA_SRC = $(wildcard src/runtime/cuda/*.cc)
ROCM_SRC = $(wildcard src/runtime/rocm/*.cc)
OPENCL_SRC = $(wildcard src/runtime/opencl/*.cc)
OPENGL_SRC = $(wildcard src/runtime/opengl/*.cc)
GLAD_SRC = glad/src/glad.c
RPC_SRC = $(wildcard src/runtime/rpc/*.cc)
GRAPH_SRC = $(wildcard src/runtime/graph/*.cc)
RUNTIME_SRC = $(wildcard src/runtime/*.cc)
Expand All @@ -68,7 +67,6 @@ CUDA_OBJ = $(patsubst src/%.cc, build/%.o, $(CUDA_SRC))
ROCM_OBJ = $(patsubst src/%.cc, build/%.o, $(ROCM_SRC))
OPENCL_OBJ = $(patsubst src/%.cc, build/%.o, $(OPENCL_SRC))
OPENGL_OBJ = $(patsubst src/%.cc, build/%.o, $(OPENGL_SRC))
GLAD_OBJ = $(patsubst glad/src/%.c, build/%.o, $(GLAD_SRC))
RPC_OBJ = $(patsubst src/%.cc, build/%.o, $(RPC_SRC))
GRAPH_OBJ = $(patsubst src/%.cc, build/%.o, $(GRAPH_SRC))
CC_OBJ = $(patsubst src/%.cc, build/%.o, $(CC_SRC)) $(LLVM_OBJ)
Expand Down Expand Up @@ -121,13 +119,13 @@ endif

ifeq ($(USE_OPENGL), 1)
CFLAGS += -DTVM_OPENGL_RUNTIME=1
EMCC_FLAGS += -DTVM_OPENGL_RUNTIME=1
ifeq ($(UNAME_S), Darwin)
FRAMEWORKS += -framework OpenGL
else
LDFLAGS += -lGL -lglfw
endif
RUNTIME_DEP += $(OPENGL_OBJ)
RUNTIME_DEP += $(GLAD_OBJ)
else
CFLAGS += -DTVM_OPENGL_RUNTIME=0
endif
Expand Down Expand Up @@ -213,11 +211,6 @@ build/runtime/metal/%.o: src/runtime/metal/%.mm
$(CXX) $(OBJCFLAGS) $(CFLAGS) -MM -MT build/runtime/metal/$*.o $< >build/runtime/metal/$*.d
$(CXX) $(OBJCFLAGS) -c $(CFLAGS) -c $< -o $@

build/glad.o: glad/src/glad.c
@mkdir -p $(@D)
$(CXX) $(CFLAGS) -MM -MT build/glad.o $< >build/glad.d
$(CXX) -c $(CFLAGS) -c glad/src/glad.c -o build/glad.o

build/%.o: src/%.cc
@mkdir -p $(@D)
$(CXX) $(CFLAGS) -MM -MT build/$*.o $< >build/$*.d
Expand Down
1 change: 0 additions & 1 deletion glad
Submodule glad deleted from 5bf3ed
2 changes: 1 addition & 1 deletion python/tvm/exec/rpc_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def find_example_resource():
js_files = [
os.path.join(base_path, "web/tvm_runtime.js"),
os.path.join(base_path, "lib/libtvm_web_runtime.js"),
os.path.join(base_path, "lib/libtvm_web_runtime.js.mem")
# os.path.join(base_path, "lib/libtvm_web_runtime.js.mem")
]
for fname in [index_page] + js_files:
if not os.path.exists(fname):
Expand Down
178 changes: 167 additions & 11 deletions src/runtime/opengl/opengl_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <tvm/runtime/packed_func.h>
#include <tvm/runtime/device_api.h>
#include <dmlc/logging.h>
#include <glad/glad.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <string>
#include <utility>
Expand All @@ -22,10 +22,134 @@ namespace runtime {
namespace gl {

// This file contains the following classes.
class GLFunctionPointers;
class OpenGLWorkspace;
class Texture;
class Program;

inline GLFWglproc GetProcAddress(const char *procname) {
GLFWglproc proc = glfwGetProcAddress(procname);
CHECK(proc != nullptr) << "Cannot get function pointer " << procname;
return proc;
}

#define SetGLFunctionPointer(NAME) \
NAME(decltype(NAME)(GetProcAddress("gl" #NAME)))

/*!
* \brief The function pointers of all OpenGL APIs that are used.
* Must be constructed after creating an OpenGL context.
*/
class GLFunctionPointers {
public:
GLFunctionPointers()
: SetGLFunctionPointer(ActiveTexture),
SetGLFunctionPointer(AttachShader),
SetGLFunctionPointer(BindBuffer),
SetGLFunctionPointer(BindFramebuffer),
SetGLFunctionPointer(BindTexture),
SetGLFunctionPointer(BindVertexArray),
SetGLFunctionPointer(BufferData),
SetGLFunctionPointer(CheckFramebufferStatus),
SetGLFunctionPointer(Clear),
SetGLFunctionPointer(CompileShader),
SetGLFunctionPointer(CreateProgram),
SetGLFunctionPointer(CreateShader),
SetGLFunctionPointer(DeleteFramebuffers),
SetGLFunctionPointer(DeleteProgram),
SetGLFunctionPointer(DeleteShader),
SetGLFunctionPointer(DeleteTextures),
SetGLFunctionPointer(DetachShader),
SetGLFunctionPointer(DrawArrays),
SetGLFunctionPointer(DrawBuffers),
SetGLFunctionPointer(EnableVertexAttribArray),
SetGLFunctionPointer(Finish),
SetGLFunctionPointer(FramebufferTexture2D),
SetGLFunctionPointer(GenBuffers),
SetGLFunctionPointer(GenFramebuffers),
SetGLFunctionPointer(GenTextures),
SetGLFunctionPointer(GenVertexArrays),
SetGLFunctionPointer(GetAttribLocation),
SetGLFunctionPointer(GetError),
SetGLFunctionPointer(GetIntegerv),
SetGLFunctionPointer(GetProgramInfoLog),
SetGLFunctionPointer(GetProgramiv),
SetGLFunctionPointer(GetShaderInfoLog),
SetGLFunctionPointer(GetShaderiv),
SetGLFunctionPointer(GetString),
SetGLFunctionPointer(GetUniformLocation),
SetGLFunctionPointer(LinkProgram),
SetGLFunctionPointer(ReadPixels),
SetGLFunctionPointer(ShaderSource),
SetGLFunctionPointer(TexImage2D),
SetGLFunctionPointer(TexParameteri),
SetGLFunctionPointer(TexSubImage2D),
SetGLFunctionPointer(Uniform1i),
SetGLFunctionPointer(UseProgram),
SetGLFunctionPointer(VertexAttribPointer),
SetGLFunctionPointer(Viewport) {
LOG(INFO) << "Constructed GLFunctionPointers";
}

void (*ActiveTexture)(GLenum texture);
void (*AttachShader)(GLuint program, GLuint shader);
void (*BindBuffer)(GLenum target, GLuint buffer);
void (*BindFramebuffer)(GLenum target, GLuint framebuffer);
void (*BindTexture)(GLenum target, GLuint texture);
void (*BindVertexArray)(GLuint array);
void (*BufferData)(GLenum target, GLsizeiptr size, const GLvoid *data,
GLenum usage);
GLenum (*CheckFramebufferStatus)(GLenum target);
void (*Clear)(GLbitfield mask);
void (*CompileShader)(GLuint shader);
GLuint (*CreateProgram)();
GLuint (*CreateShader)(GLenum shader_type);
void (*DeleteFramebuffers)(GLsizei n, const GLuint *framebuffers);
void (*DeleteProgram)(GLuint program);
void (*DeleteShader)(GLuint shader);
void (*DeleteTextures)(GLsizei n, const GLuint *textures);
void (*DetachShader)(GLuint program, GLuint shader);
void (*DrawArrays)(GLenum mode, GLint first, GLsizei count);
void (*DrawBuffers)(GLsizei n, const GLenum *bufs);
void (*EnableVertexAttribArray)(GLuint index);
void (*Finish)();
void (*FramebufferTexture2D)(GLenum target, GLenum attachment,
GLenum textarget,GLuint texture, GLint level);
void (*GenBuffers)(GLsizei n, GLuint *buffers);
void (*GenFramebuffers)(GLsizei n, GLuint *ids);
void (*GenTextures)(GLsizei n, GLuint * textures);
void (*GenVertexArrays)(GLsizei n, GLuint *arrays);
GLint (*GetAttribLocation)(GLuint program, const GLchar *name);
GLenum (*GetError)();
void (*GetIntegerv)(GLenum pname, GLint * data);
void (*GetProgramInfoLog)(GLuint program, GLsizei maxLength, GLsizei *length,
GLchar *infoLog);
void (*GetProgramiv)(GLuint program, GLenum pname, GLint *params);
void (*GetShaderInfoLog)(GLuint shader, GLsizei max_length, GLsizei *length,
GLchar *info_log);
void (*GetShaderiv)(GLuint shader, GLenum pname, GLint *params);
const GLubyte *(*GetString)(GLenum name);
GLint (*GetUniformLocation)(GLuint program, const GLchar *name);
void (*LinkProgram)(GLuint program);
void (*ReadPixels)(GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type, GLvoid* data);
void (*ShaderSource)(GLuint shader, GLsizei count, const GLchar **string,
const GLint *length);
void (*TexImage2D)(GLenum target, GLint level, GLint internal_format,
GLsizei width, GLsizei height, GLint border, GLenum format,
GLenum type, const GLvoid *data);
void (*TexParameteri)(GLenum target, GLenum pname, GLint param);
void (*TexSubImage2D)(GLenum target, GLint level, GLint xoffset,
GLint yoffset, GLsizei width, GLsizei height,
GLenum format, GLenum type, const GLvoid* data);
void (*Uniform1i)(GLint location, GLint v0);
void (*UseProgram)(GLuint program);
void (*VertexAttribPointer)(GLuint index, GLint size, GLenum type,
GLboolean normalized, GLsizei stride,
const GLvoid * pointer);
void (*Viewport)(GLint x, GLint y, GLsizei width, GLsizei height);
};

/*!
* \brief Process global OpenGL workspace.
*/
Expand Down Expand Up @@ -57,6 +181,13 @@ class OpenGLWorkspace final : public DeviceAPI {
*/
std::unique_ptr<Program> CreateProgram(const char* fragment_shader_src);

std::unique_ptr<Texture> CreateTexture(size_t nbytes);

void PutTextureData(Texture *texture, GLint begin, GLsizei nelems,
const GLvoid* data);

void GetTextureData(const Texture *texture, GLvoid* data);

/*!
* \brief Get the global OpenGL workspace.
* \return The global OpenGL workspace.
Expand All @@ -76,10 +207,12 @@ class OpenGLWorkspace final : public DeviceAPI {

private:
friend class Texture;
friend class Program;

OpenGLWorkspace();

GLFWwindow* window_;
std::unique_ptr<GLFunctionPointers> gl;
GLuint vertex_shader_;
static const int kWindowWidth = 640;
static const int kWindowHeight = 480;
Expand All @@ -92,6 +225,12 @@ class OpenGLWorkspace final : public DeviceAPI {

void BindTextureUnit(GLuint unit, GLuint texture);

void OnDeleteTexture(GLuint texture);

void OnDeleteProgram(GLuint program);

void CheckOpenGLError();

GLuint NumTextureUnits();

/*!
Expand Down Expand Up @@ -121,27 +260,35 @@ class OpenGLWorkspace final : public DeviceAPI {
class Program {
public:
// Move constructor.
Program(Program&& other) noexcept : program_(other.program_) {
Program(Program&& other) noexcept
: workspace_(other.workspace_), program_(other.program_) {
other.program_ = kInvalidProgram;
}

Program(const Program& other) = delete;
Program& operator=(const Program& other) = delete;

~Program();
~Program() {
if (program_ != kInvalidProgram) {
workspace_->OnDeleteProgram(program_);
program_ = kInvalidProgram;
}
}

private:
friend class OpenGLWorkspace;

// Only OpenGLWorkspace can create a Program.
// We enforce this to make sure OpenGL is initialized.
explicit Program(GLuint program) : program_(program) {}
explicit Program(OpenGLWorkspace* workspace, GLuint program)
: workspace_(workspace), program_(program) {}

// The internal OpenGL program ID.
GLuint program() { return program_; }

static constexpr GLuint kInvalidProgram = static_cast<GLuint>(-1);

OpenGLWorkspace* workspace_;
GLuint program_;
};

Expand All @@ -154,35 +301,44 @@ class Texture {
public:
// Move constructor.
Texture(Texture&& other) noexcept
: texture_(other.texture_), width_(other.width_), height_(other.height_) {
: workspace_(other.workspace_), texture_(other.texture_),
width_(other.width_), height_(other.height_) {
other.texture_ = kInvalidTexture;
}

Texture(const Texture& other) = delete;
Texture& operator=(const Texture& other) = delete;

~Texture();
~Texture() {
if (texture_ != kInvalidTexture) {
LOG(INFO) << "Deleting texture [" << texture_ << "]";
workspace_->OnDeleteTexture(texture_);
texture_ = kInvalidTexture;
}
}

GLsizei width() const { return width_; }

GLsizei height() const { return height_; }

void GetData(GLvoid* data) const;

void PutData(GLint begin, GLsizei nelems, const GLvoid* data);

private:
friend class OpenGLWorkspace;

// Only OpenGLWorkspace can create a Texture.
// We enforce this to make sure OpenGL is initialized.
explicit Texture(size_t nbytes);
// Always only use the first dimension of a 2D texture.
// The reason of using 2D textures is that texelFetch only supports 2D textures.
explicit Texture(OpenGLWorkspace* workspace, GLuint texture, GLsizei width,
GLsizei height)
: workspace_(workspace), texture_(texture), width_(width),
height_(height) {}

// The internal texture ID.
GLuint texture() const { return texture_; }

static constexpr GLuint kInvalidTexture = static_cast<GLuint>(-1);

OpenGLWorkspace* workspace_;
GLuint texture_;
GLsizei width_;
GLsizei height_;
Expand Down
Loading

0 comments on commit 95ccc12

Please sign in to comment.