Skip to content

Commit

Permalink
[aot] C-API opengl runtime interop (taichi-dev#7014)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
damnkk and pre-commit-ci[bot] authored Jan 3, 2023
1 parent 629a57a commit a1485cd
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
15 changes: 15 additions & 0 deletions c_api/include/taichi/taichi_opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
extern "C" {
#endif // __cplusplus

// Structure `TiOpenglRuntimeInteropInfo`
typedef struct TiOpenglRuntimeInteropInfo {
void *get_proc_addr;
} TiOpenglRuntimeInteropInfo;

// Function `ti_import_opengl_runtime`
TI_DLL_EXPORT void TI_API_CALL
ti_import_opengl_runtime(TiRuntime runtime,
TiOpenglRuntimeInteropInfo *interop_info);

// Function `ti_export_opengl_runtime`
TI_DLL_EXPORT void TI_API_CALL
ti_export_opengl_runtime(TiRuntime runtime,
TiOpenglRuntimeInteropInfo *interop_info);

// Structure `TiOpenglMemoryInteropInfo`
typedef struct TiOpenglMemoryInteropInfo {
GLuint buffer;
Expand Down
9 changes: 9 additions & 0 deletions c_api/src/taichi_opengl_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

OpenglRuntime::OpenglRuntime()
: GfxRuntime(taichi::Arch::opengl),
device_(),
gfx_runtime_(taichi::lang::gfx::GfxRuntime::Params{
host_result_buffer_.data(), &device_}) {
taichi::lang::DeviceCapabilityConfig caps{};
Expand All @@ -18,6 +19,14 @@ taichi::lang::gfx::GfxRuntime &OpenglRuntime::get_gfx_runtime() {
return gfx_runtime_;
}

void ti_export_opengl_runtime(TiRuntime runtime,
TiOpenglRuntimeInteropInfo *interop_info) {
TI_CAPI_TRY_CATCH_BEGIN();
// FIXME: (penguinliogn)
interop_info->get_proc_addr = taichi::lang::opengl::kGetOpenglProcAddr;
TI_CAPI_TRY_CATCH_END();
}

void ti_export_opengl_memory(TiRuntime runtime,
TiMemory memory,
TiOpenglMemoryInteropInfo *interop_info) {
Expand Down
2 changes: 2 additions & 0 deletions c_api/src/taichi_vulkan_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ VulkanRuntimeImported::Workaround::Workaround(
taichi::lang::vulkan::VulkanLoader::instance().load_instance(params.instance);
taichi::lang::vulkan::VulkanLoader::instance().load_device(params.device);
vk_device.vk_caps().vk_api_version = api_version;
// FIXME: (penguinliong) Workaround missing vulkan caps from import.
vk_device.vk_caps().external_memory = true;

taichi::lang::DeviceCapabilityConfig caps{};

Expand Down
38 changes: 38 additions & 0 deletions c_api/taichi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,44 @@
"taichi/taichi_core.h"
],
"declarations": [
{
"name": "opengl_runtime_interop_info",
"type": "structure",
"fields": [
{
"name": "get_proc_addr",
"type": "void*"
}
]
},
{
"name": "import_opengl_runtime",
"type": "function",
"parameters": [
{
"type": "handle.runtime"
},
{
"name": "interop_info",
"type": "structure.opengl_runtime_interop_info",
"by_mut": true
}
]
},
{
"name": "export_opengl_runtime",
"type": "function",
"parameters": [
{
"type": "handle.runtime"
},
{
"name": "interop_info",
"type": "structure.opengl_runtime_interop_info",
"by_mut": true
}
]
},
{
"name": "opengl_memory_interop_info",
"type": "structure",
Expand Down
6 changes: 5 additions & 1 deletion taichi/rhi/opengl/opengl_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ int opengl_max_grid_dim = 1024;
// without this global static boolean.
static bool kUseGles = false;
static std::optional<bool> supported; // std::nullopt
void *kGetOpenglProcAddr;

static void glfw_error_callback(int code, const char *description) {
TI_WARN("GLFW Error {}: {}", code, description);
Expand All @@ -44,6 +45,7 @@ bool initialize_opengl(bool use_gles, bool error_tolerance) {

// Code below is guaranteed to be called at most once.
int opengl_version = 0;
void *get_proc_addr = nullptr;

if (glfwInit()) {
glfwSetErrorCallback(glfw_error_callback);
Expand All @@ -53,7 +55,6 @@ bool initialize_opengl(bool use_gles, bool error_tolerance) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
} else {
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
Expand All @@ -73,6 +74,7 @@ bool initialize_opengl(bool use_gles, bool error_tolerance) {
TI_DEBUG("[glsl] cannot create GLFW window: error {}: {}", status, desc);
} else {
glfwMakeContextCurrent(window);
get_proc_addr = (void *)&glfwGetProcAddress;
if (use_gles) {
opengl_version = gladLoadGLES2(glfwGetProcAddress);
} else {
Expand Down Expand Up @@ -148,6 +150,7 @@ bool initialize_opengl(bool use_gles, bool error_tolerance) {

eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);

get_proc_addr = (void *)&glad_eglGetProcAddress;
if (use_gles) {
opengl_version = gladLoadGLES2(glad_eglGetProcAddress);
} else {
Expand Down Expand Up @@ -194,6 +197,7 @@ bool initialize_opengl(bool use_gles, bool error_tolerance) {

supported = std::make_optional<bool>(true);
kUseGles = use_gles;
kGetOpenglProcAddr = get_proc_addr;
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion taichi/rhi/opengl/opengl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ void GLStream::command_sync() {
}

GLDevice::GLDevice() : stream_(this) {
initialize_opengl(false, true);
DeviceCapabilityConfig caps{};
if (!is_gles()) {
// 64bit isn't supported in ES profile
Expand Down Expand Up @@ -555,9 +556,9 @@ GLint GLDevice::get_devalloc_size(DeviceAllocation handle) {
GLint size = 0;
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
check_opengl_error("glGetBufferParameteriv");
return size;
glBindBuffer(GL_ARRAY_BUFFER, 0);
check_opengl_error("glBindBuffer");
return size;
}

std::unique_ptr<Pipeline> GLDevice::create_pipeline(
Expand Down Expand Up @@ -871,7 +872,9 @@ void GLCommandList::CmdImageToBuffer::execute() {
(void *)offset);
check_opengl_error("glGetTexImage");
glBindTexture(image_dims, /*target=*/0);
check_opengl_error("glBindTexture");
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, /*target=*/0);
check_opengl_error("glBindBuffer");
}

} // namespace opengl
Expand Down
1 change: 1 addition & 0 deletions taichi/rhi/opengl/opengl_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace opengl {
class GLDevice;

void check_opengl_error(const std::string &msg = "OpenGL");
extern void *kGetOpenglProcAddr;

class GLResourceSet : public ShaderResourceSet {
public:
Expand Down

0 comments on commit a1485cd

Please sign in to comment.