Skip to content

Commit

Permalink
[aot] Reorganized C-API headers (#6199)
Browse files Browse the repository at this point in the history
As @jim19930609 mentioned before including vulkan headers might be
contaminating the user namespaces so this PR removes the include and
macro. The users should include `vulkan/vulkan.h` *before*
`taichi/taichi.h`.
  • Loading branch information
PENGUINLIONG authored Oct 11, 2022
1 parent f97e48c commit e3aaff8
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 141 deletions.
15 changes: 11 additions & 4 deletions c_api/include/taichi/taichi.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

#include "taichi/taichi_core.h"

#if TI_WITH_VULKAN
#define VK_NO_PROTOTYPES 1
#ifdef TI_WITH_VULKAN
#ifndef TI_NO_VULKAN_INCLUDES
#include <vulkan/vulkan.h>
#endif // TI_NO_VULKAN_INCLUDES

#include "taichi/taichi_vulkan.h"
#endif // TI_WITH_VULKAN

#if TI_WITH_OPENGL
#ifdef TI_WITH_OPENGL
#ifndef TI_NO_OPENGL_INCLUDES
#include <GL/gl.h>
#endif // TI_NO_OPENGL_INCLUDES

#include "taichi/taichi_opengl.h"
#endif
#endif // TI_WITH_OPENGL
8 changes: 7 additions & 1 deletion c_api/include/taichi/taichi_core.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#pragma once
#include <taichi/taichi_platform.h>

#ifndef TI_C_API_VERSION
#define TI_C_API_VERSION 1000002
#endif // TI_C_API_VERSION

#include <taichi/taichi.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -251,6 +256,7 @@ typedef struct TiImageAllocateInfo {
TiImageExtent extent;
uint32_t mip_level_count;
TiFormat format;
TiBool export_sharing;
TiImageUsageFlags usage;
} TiImageAllocateInfo;

Expand Down
14 changes: 12 additions & 2 deletions c_api/include/taichi/taichi_opengl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once
#include <taichi/taichi_core.h>
#include <glad/gl.h>

#ifndef TI_WITH_OPENGL
#define TI_WITH_OPENGL 1
#endif // TI_WITH_OPENGL

#include <taichi/taichi.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -12,6 +16,12 @@ typedef struct TiOpenglMemoryInteropInfo {
uint64_t size;
} TiOpenglMemoryInteropInfo;

// function.import_opengl_memory
TI_DLL_EXPORT void TI_API_CALL
ti_import_opengl_memory(TiRuntime runtime,
TiMemory memory,
TiOpenglMemoryInteropInfo *interop_info);

// function.export_opengl_memory
TI_DLL_EXPORT void TI_API_CALL
ti_export_opengl_memory(TiRuntime runtime,
Expand Down
3 changes: 2 additions & 1 deletion c_api/include/taichi/taichi_unity.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <taichi/taichi_core.h>

#include <taichi/taichi.h>

#ifdef __cplusplus
extern "C" {
Expand Down
8 changes: 6 additions & 2 deletions c_api/include/taichi/taichi_vulkan.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once
#include <taichi/taichi_core.h>
#include <vulkan/vulkan.h>

#ifndef TI_WITH_VULKAN
#define TI_WITH_VULKAN 1
#endif // TI_WITH_VULKAN

#include <taichi/taichi.h>

#ifdef __cplusplus
extern "C" {
Expand Down
81 changes: 2 additions & 79 deletions c_api/src/c_api_test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,90 +37,13 @@ void check_runtime_error(TiRuntime runtime) {
#ifdef TI_WITH_LLVM
auto *llvm_runtime = dynamic_cast<capi::LlvmRuntime *>((Runtime *)runtime);
if (!llvm_runtime) {
TI_NOT_IMPLEMENTED;
ti_set_last_error(TI_ERROR_INVALID_STATE, "llvm_runtime");
}
llvm_runtime->check_runtime_error();
#else
TI_NOT_IMPLEMENTED;
ti_set_last_error(TI_ERROR_INVALID_STATE, "llvm_runtime");
#endif
}

TiNdarrayAndMem make_ndarray(TiRuntime runtime,
TiDataType dtype,
const int *arr_shape,
int arr_dims,
const int *element_shape,
int element_dims,
bool host_read,
bool host_write) {
size_t alloc_size = 1;
if (dtype == TiDataType::TI_DATA_TYPE_F64 ||
dtype == TiDataType::TI_DATA_TYPE_I64 ||
dtype == TiDataType::TI_DATA_TYPE_U64) {
alloc_size = 8;

} else if (dtype == TiDataType::TI_DATA_TYPE_F32 ||
dtype == TiDataType::TI_DATA_TYPE_I32 ||
dtype == TiDataType::TI_DATA_TYPE_U32) {
alloc_size = 4;

} else if (dtype == TI_DATA_TYPE_F16 || dtype == TI_DATA_TYPE_I16 ||
dtype == TI_DATA_TYPE_U16) {
alloc_size = 2;

} else if (dtype == TI_DATA_TYPE_I8 || dtype == TI_DATA_TYPE_U8) {
alloc_size = 1;

} else {
TI_ASSERT(false);
}

for (int i = 0; i < arr_dims; i++) {
alloc_size *= arr_shape[i];
}

for (int i = 0; i < element_dims; i++) {
alloc_size *= element_shape[i];
}

TiNdarrayAndMem res;
res.runtime_ = runtime;

TiMemoryAllocateInfo alloc_info;
alloc_info.size = alloc_size;
alloc_info.host_write = host_write;
alloc_info.host_read = host_read;
alloc_info.export_sharing = false;
alloc_info.usage = TiMemoryUsageFlagBits::TI_MEMORY_USAGE_STORAGE_BIT;

res.memory_ = ti_allocate_memory(res.runtime_, &alloc_info);

TiNdShape shape;
shape.dim_count = (uint32_t)arr_dims;
for (size_t i = 0; i < shape.dim_count; i++) {
shape.dims[i] = arr_shape[i];
}

TiNdShape e_shape;
e_shape.dim_count = (uint32_t)element_dims;
for (size_t i = 0; i < e_shape.dim_count; i++) {
e_shape.dims[i] = element_shape[i];
}

TiNdArray arg_array{};
arg_array.memory = res.memory_;
arg_array.shape = shape;
arg_array.elem_shape = e_shape;
arg_array.elem_type = dtype;

TiArgumentValue arg_value{};
arg_value.ndarray = arg_array;

res.arg_.type = TiArgumentType::TI_ARGUMENT_TYPE_NDARRAY;
res.arg_.value = arg_value;

return res;
}

} // namespace utils
} // namespace capi
19 changes: 2 additions & 17 deletions c_api/src/c_api_test_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <taichi/taichi_platform.h>
#include "taichi/taichi_core.h"
#include "taichi_core_impl.h"

namespace capi {
namespace utils {

Expand All @@ -9,20 +9,5 @@ TI_DLL_EXPORT bool TI_API_CALL is_opengl_available();
TI_DLL_EXPORT bool TI_API_CALL is_cuda_available();
TI_DLL_EXPORT void TI_API_CALL check_runtime_error(TiRuntime runtime);

typedef struct TiNdarrayAndMem {
TiRuntime runtime_;
TiMemory memory_;
TiArgument arg_;
} TiNdarrayAndMem;

TI_DLL_EXPORT TiNdarrayAndMem TI_API_CALL make_ndarray(TiRuntime runtime,
TiDataType dtype,
const int *arr_shape,
int arr_dims,
const int *element_shape,
int element_dims,
bool host_read = false,
bool host_write = false);

} // namespace utils
} // namespace capi
1 change: 1 addition & 0 deletions c_api/src/taichi_core_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "taichi_vulkan_impl.h"
#include "taichi_llvm_impl.h"
#include "taichi/program/ndarray.h"
#include "taichi/program/texture.h"

struct ErrorCache {
TiError error{TI_ERROR_SUCCESS};
Expand Down
40 changes: 29 additions & 11 deletions c_api/src/taichi_core_impl.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
#pragma once
#include <vector>
#include <memory>
#include <string>
#include <exception>
#include "taichi/taichi_core.h"
#include "taichi/aot/module_loader.h"
#include "taichi/rhi/device.h"
#include "taichi/program/texture.h"
#include "taichi/runtime/gfx/aot_module_loader_impl.h"

#include <stdexcept>

// Taichi runtime is not necessarily using the same 3rd-party headers as the
// user codes. For C-API implementations we only use the internal headers.
#ifdef TI_WITH_VULKAN
#ifndef VK_NO_PROTOTYPES
#define VK_NO_PROTOTYPES 1
#endif // VK_NO_PROTOTYPES
#include "vulkan/vulkan.h"
#define TI_NO_VULKAN_INCLUDES 1
#endif // TI_WITH_VULKAN

#ifdef TI_WITH_OPENGL
#include "glad/gl.h"
#define TI_NO_OPENGL_INCLUDES 1
#endif // TI_WITH_OPENGL

// Then Include all C-API symbols.
#include "taichi/taichi.h"

// Include for the base types.
#include "taichi/rhi/arch.h"
#define TI_RUNTIME_HOST 1
#include "taichi/program/context.h"
#undef TI_RUNTIME_HOST
#include "taichi/rhi/device.h"
#include "taichi/aot/graph_data.h"
#include "taichi/aot/module_loader.h"

// Error reporting.
// Error reporting helpers.
#define TI_CAPI_INCOMPLETE(x) ti_set_last_error(TI_ERROR_INCOMPLETE, #x);
#define TI_CAPI_INCOMPLETE_IF(x) \
if (x) { \
Expand Down Expand Up @@ -79,10 +101,6 @@
ti_set_last_error(TI_ERROR_INVALID_STATE, "c++ exception"); \
}

class Runtime;
class Context;
class AotModule;

class Runtime {
protected:
// 32 is a magic number in `taichi/inc/constants.h`.
Expand Down
1 change: 1 addition & 0 deletions c_api/src/taichi_gfx_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "taichi_gfx_impl.h"
#include "taichi/runtime/gfx/aot_module_loader_impl.h"

GfxRuntime::GfxRuntime(taichi::Arch arch) : Runtime(arch) {
}
Expand Down
1 change: 1 addition & 0 deletions c_api/src/taichi_gfx_impl.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "taichi_core_impl.h"
#include "taichi/runtime/gfx/runtime.h"

Expand Down
4 changes: 4 additions & 0 deletions c_api/src/taichi_llvm_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifdef TI_WITH_LLVM

#include "taichi_core_impl.h"
#include "taichi_llvm_impl.h"

Expand Down Expand Up @@ -128,3 +130,5 @@ void LlvmRuntime::wait() {
}

} // namespace capi

#endif // TI_WITH_LLVM
3 changes: 3 additions & 0 deletions c_api/src/taichi_llvm_impl.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#ifdef TI_WITH_LLVM

#include "taichi_core_impl.h"

Expand Down Expand Up @@ -41,3 +42,5 @@ class LlvmRuntime : public Runtime {
};

} // namespace capi

#endif // TI_WITH_LLVM
3 changes: 3 additions & 0 deletions c_api/src/taichi_opengl_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "taichi_opengl_impl.h"
#ifdef TI_WITH_OPENGL

OpenglRuntime::OpenglRuntime()
: GfxRuntime(taichi::Arch::opengl),
Expand Down Expand Up @@ -30,3 +31,5 @@ void ti_export_opengl_memory(TiRuntime runtime,
interop_info->size = runtime2->get_gl().get_devalloc_size(devalloc);
TI_CAPI_TRY_CATCH_END();
}

#endif // TI_WITH_OPENGL
5 changes: 4 additions & 1 deletion c_api/src/taichi_opengl_impl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#ifdef TI_WITH_OPENGL

#include "taichi_gfx_impl.h"
#include "taichi/taichi_opengl.h"
#include "taichi/rhi/opengl/opengl_device.h"

class OpenglRuntime : public GfxRuntime {
Expand All @@ -16,3 +17,5 @@ class OpenglRuntime : public GfxRuntime {
return device_;
}
};

#endif // TI_WITH_OPENGL
11 changes: 6 additions & 5 deletions c_api/src/taichi_vulkan_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifdef TI_WITH_VULKAN
#include "taichi_vulkan_impl.h"
#include "taichi/rhi/vulkan/vulkan_loader.h"
#include "vulkan/vulkan.h"

#ifdef ANDROID
#define VK_KHR_android_surface 1
#include "vulkan/vulkan_android.h"
#endif
#endif // ANDROID

VulkanRuntime::VulkanRuntime() : GfxRuntime(taichi::Arch::vulkan) {
}
Expand All @@ -28,9 +29,7 @@ VulkanRuntimeImported::Workaround::Workaround(
api_version);

vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10000);
if (api_version >= VK_API_VERSION_1_3) {
vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10500);
} else if (api_version >= VK_API_VERSION_1_2) {
if (api_version >= VK_API_VERSION_1_2) {
vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10500);
} else if (api_version >= VK_API_VERSION_1_1) {
vk_device.set_cap(taichi::lang::DeviceCapability::spirv_version, 0x10300);
Expand Down Expand Up @@ -362,3 +361,5 @@ void ti_export_vulkan_event(TiRuntime runtime,
interop_info->event = event2->vkapi_ref->event;
TI_CAPI_TRY_CATCH_END();
}

#endif // TI_WITH_VULKAN
10 changes: 5 additions & 5 deletions c_api/src/taichi_vulkan_impl.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once

#define VK_NO_PROTOTYPES
#include "taichi/taichi_vulkan.h"
#include "taichi/rhi/vulkan/vulkan_device.h"
#include "taichi/rhi/vulkan/vulkan_device_creator.h"
#ifdef TI_WITH_VULKAN

#include "taichi_core_impl.h"
#include "taichi_gfx_impl.h"
#include "taichi/rhi/vulkan/vulkan_device.h"
#include "taichi/rhi/vulkan/vulkan_device_creator.h"

class VulkanRuntime;
class VulkanRuntimeImported;
Expand Down Expand Up @@ -52,3 +50,5 @@ class VulkanRuntimeOwned : public VulkanRuntime {
virtual taichi::lang::Device &get() override final;
virtual taichi::lang::gfx::GfxRuntime &get_gfx_runtime() override final;
};

#endif // TI_WITH_VULKAN
Loading

0 comments on commit e3aaff8

Please sign in to comment.