Skip to content

Commit

Permalink
Add test for C API bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
madmann91 committed May 20, 2024
1 parent 83a0360 commit 5303ce8
Show file tree
Hide file tree
Showing 11 changed files with 638 additions and 38 deletions.
5 changes: 4 additions & 1 deletion src/bvh/v2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ if (Threads_FOUND)
target_link_libraries(bvh INTERFACE Threads::Threads)
endif()

target_include_directories(bvh INTERFACE ../..)
target_include_directories(bvh INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>)

set_target_properties(bvh PROPERTIES CXX_STANDARD 20)

install(
Expand Down
3 changes: 3 additions & 0 deletions src/bvh/v2/c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
add_library(bvh_c SHARED bvh.cpp)
target_link_libraries(bvh_c PRIVATE bvh)
target_compile_definitions(bvh_c PRIVATE -DBVH_BUILD_API)
target_include_directories(bvh_c INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>)
set_target_properties(bvh_c PROPERTIES
CXX_STANDARD 20
CXX_VISIBILITY_PRESET hidden
Expand Down
4 changes: 2 additions & 2 deletions src/bvh/v2/c_api/bvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ BVH_TYPES(double, 3, 3d)

extern "C" {

struct bvh_thread_pool* bvh_thread_pool_create(size_t thread_count) {
BVH_EXPORT struct bvh_thread_pool* bvh_thread_pool_create(size_t thread_count) {
return reinterpret_cast<bvh_thread_pool*>(new bvh::v2::ThreadPool(thread_count));
}

void bvh_thread_pool_destroy(bvh_thread_pool* thread_pool) {
BVH_EXPORT void bvh_thread_pool_destroy(bvh_thread_pool* thread_pool) {
return delete reinterpret_cast<bvh::v2::ThreadPool*>(thread_pool);
}

Expand Down
17 changes: 8 additions & 9 deletions src/bvh/v2/c_api/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ extern "C" {
#endif

#ifdef _MSC_VER
#ifdef BVH_BUILD_API
#define BVH_API __declspec(dllexport)
#define BVH_EXPORT __declspec(dllexport)
#define BVH_IMPORT __declspec(dllimport)
#else
#define BVH_API __declspec(dllimport)
#define BVH_EXPORT __attribute__((visibility("default")))
#define BVH_IMPORT BVH_EXPORT
#endif
#elif defined(__GCC__) || defined(__CLANG__)
#define BVH_API __attribute__((visibility("default")))

#ifdef BVH_BUILD_API
#define BVH_API BVH_EXPORT
#else
#define BVH_API
#define BVH_API BVH_IMPORT
#endif

#define BVH_ROOT_INDEX 0
Expand Down Expand Up @@ -80,9 +82,6 @@ struct bvh_intersect_callbackd {
bool (*user_fn)(void*, double*, size_t begin, size_t end);
};

struct bvh_tri3f { struct bvh_vec3f v[3]; };
struct bvh_tri3d { struct bvh_vec3d v[3]; };

// Thread Pool ------------------------------------------------------------------------------------

// A thread count of zero instructs the thread pool to detect the number of threads available on the
Expand Down
46 changes: 23 additions & 23 deletions src/bvh/v2/c_api/bvh_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static void bvh_intersect_ray(
};

#define BVH_IMPL(T, Dim, suffix) \
typename BvhTypes<T, Dim>::Bvh* bvh##suffix##_build( \
BVH_EXPORT typename BvhTypes<T, Dim>::Bvh* bvh##suffix##_build( \
bvh_thread_pool* thread_pool, \
const typename BvhTypes<T, Dim>::BBox* bboxes, \
const typename BvhTypes<T, Dim>::Vec* centers, \
Expand All @@ -269,82 +269,82 @@ static void bvh_intersect_ray(
{ \
return bvh_build<T, Dim>(thread_pool, bboxes, centers, prim_count, config); \
} \
void bvh##suffix##_destroy(typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT void bvh##suffix##_destroy(typename BvhTypes<T, Dim>::Bvh* bvh) { \
delete reinterpret_cast<bvh::v2::Bvh<bvh::v2::Node<T, Dim>>*>(bvh); \
} \
void bvh##suffix##_save(const typename BvhTypes<T, Dim>::Bvh* bvh, FILE* file) { \
BVH_EXPORT void bvh##suffix##_save(const typename BvhTypes<T, Dim>::Bvh* bvh, FILE* file) { \
bvh_save<T, Dim>(bvh, file); \
} \
typename BvhTypes<T, Dim>::Bvh* bvh##suffix##_load(FILE* file) { \
BVH_EXPORT typename BvhTypes<T, Dim>::Bvh* bvh##suffix##_load(FILE* file) { \
return bvh_load<T, Dim>(file); \
} \
typename BvhTypes<T, Dim>::Node* bvh##suffix##_get_node(typename BvhTypes<T, Dim>::Bvh* bvh, size_t node_id) { \
BVH_EXPORT typename BvhTypes<T, Dim>::Node* bvh##suffix##_get_node(typename BvhTypes<T, Dim>::Bvh* bvh, size_t node_id) { \
return bvh_get_node<T, Dim>(bvh, node_id); \
} \
size_t bvh##suffix##_get_prim_id(const typename BvhTypes<T, Dim>::Bvh* bvh, size_t i) { \
BVH_EXPORT size_t bvh##suffix##_get_prim_id(const typename BvhTypes<T, Dim>::Bvh* bvh, size_t i) { \
return bvh_get_prim_id<T, Dim>(bvh, i); \
} \
size_t bvh##suffix##_get_prim_count(const typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT size_t bvh##suffix##_get_prim_count(const typename BvhTypes<T, Dim>::Bvh* bvh) { \
return bvh_get_prim_count<T, Dim>(bvh); \
} \
size_t bvh##suffix##_get_node_count(const typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT size_t bvh##suffix##_get_node_count(const typename BvhTypes<T, Dim>::Bvh* bvh) { \
return bvh_get_node_count<T, Dim>(bvh); \
} \
bool bvh_node##suffix##_is_leaf(const typename BvhTypes<T, Dim>::Node* node) { \
BVH_EXPORT bool bvh_node##suffix##_is_leaf(const typename BvhTypes<T, Dim>::Node* node) { \
return bvh_node_is_leaf<T, Dim>(node); \
} \
size_t bvh_node##suffix##_get_prim_count(const typename BvhTypes<T, Dim>::Node* node) { \
BVH_EXPORT size_t bvh_node##suffix##_get_prim_count(const typename BvhTypes<T, Dim>::Node* node) { \
return bvh_node_get_prim_count<T, Dim>(node); \
} \
void bvh_node##suffix##_set_prim_count(typename BvhTypes<T, Dim>::Node* node, size_t prim_count) { \
BVH_EXPORT void bvh_node##suffix##_set_prim_count(typename BvhTypes<T, Dim>::Node* node, size_t prim_count) { \
bvh_node_set_prim_count<T, Dim>(node, prim_count); \
} \
size_t bvh_node##suffix##_get_first_id(const typename BvhTypes<T, Dim>::Node* node) { \
BVH_EXPORT size_t bvh_node##suffix##_get_first_id(const typename BvhTypes<T, Dim>::Node* node) { \
return bvh_node_get_first_id<T, Dim>(node); \
} \
void bvh_node##suffix##_set_first_id(typename BvhTypes<T, Dim>::Node* node, size_t first_id) { \
BVH_EXPORT void bvh_node##suffix##_set_first_id(typename BvhTypes<T, Dim>::Node* node, size_t first_id) { \
bvh_node_set_first_id<T, Dim>(node, first_id); \
} \
typename BvhTypes<T, Dim>::BBox bvh_node##suffix##_get_bbox(const typename BvhTypes<T, Dim>::Node* node) { \
BVH_EXPORT typename BvhTypes<T, Dim>::BBox bvh_node##suffix##_get_bbox(const typename BvhTypes<T, Dim>::Node* node) { \
return bvh_node_get_bbox<T, Dim>(node); \
} \
void bvh_node##suffix##_set_bbox(typename BvhTypes<T, Dim>::Node* node, const typename BvhTypes<T, Dim>::BBox* bbox) { \
BVH_EXPORT void bvh_node##suffix##_set_bbox(typename BvhTypes<T, Dim>::Node* node, const typename BvhTypes<T, Dim>::BBox* bbox) { \
bvh_node_set_bbox<T, Dim>(node, bbox); \
} \
void bvh##suffix##_append_node(typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT void bvh##suffix##_append_node(typename BvhTypes<T, Dim>::Bvh* bvh) { \
bvh_append_node<T, Dim>(bvh); \
} \
void bvh##suffix##_remove_last_node(typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT void bvh##suffix##_remove_last_node(typename BvhTypes<T, Dim>::Bvh* bvh) { \
bvh_remove_last_node<T, Dim>(bvh); \
} \
void bvh##suffix##_refit(typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT void bvh##suffix##_refit(typename BvhTypes<T, Dim>::Bvh* bvh) { \
bvh_refit<T, Dim>(bvh); \
} \
void bvh##suffix##_optimize(bvh_thread_pool* thread_pool, typename BvhTypes<T, Dim>::Bvh* bvh) { \
BVH_EXPORT void bvh##suffix##_optimize(bvh_thread_pool* thread_pool, typename BvhTypes<T, Dim>::Bvh* bvh) { \
bvh_optimize<T, Dim>(thread_pool, bvh); \
} \
void bvh##suffix##_intersect_ray_any( \
BVH_EXPORT void bvh##suffix##_intersect_ray_any( \
const typename BvhTypes<T, Dim>::Bvh* bvh, \
const typename BvhTypes<T, Dim>::Ray* ray, \
const typename BvhCallback<T>::Type* callback) \
{ \
bvh_intersect_ray<T, Dim, true, false>(bvh, ray, callback); \
} \
void bvh##suffix##_intersect_ray_any_robust( \
BVH_EXPORT void bvh##suffix##_intersect_ray_any_robust( \
const typename BvhTypes<T, Dim>::Bvh* bvh, \
const typename BvhTypes<T, Dim>::Ray* ray, \
const typename BvhCallback<T>::Type* callback) \
{ \
bvh_intersect_ray<T, Dim, true, true>(bvh, ray, callback); \
} \
void bvh##suffix##_intersect_ray( \
BVH_EXPORT void bvh##suffix##_intersect_ray( \
const typename BvhTypes<T, Dim>::Bvh* bvh, \
const typename BvhTypes<T, Dim>::Ray* ray, \
const typename BvhCallback<T>::Type* callback) \
{ \
bvh_intersect_ray<T, Dim, false, false>(bvh, ray, callback); \
} \
void bvh##suffix##_intersect_ray_robust( \
BVH_EXPORT void bvh##suffix##_intersect_ray_robust( \
const typename BvhTypes<T, Dim>::Bvh* bvh, \
const typename BvhTypes<T, Dim>::Ray* ray, \
const typename BvhCallback<T>::Type* callback) \
Expand Down
24 changes: 22 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@ endif()
add_executable(simple_example simple_example.cpp)
target_link_libraries(simple_example PUBLIC bvh)
set_target_properties(simple_example PROPERTIES CXX_STANDARD 20)
add_test(NAME simple_example COMMAND simple_example)

add_executable(serialize serialize.cpp)
target_link_libraries(serialize PUBLIC bvh)
set_target_properties(serialize PROPERTIES CXX_STANDARD 20)
add_test(NAME serialize COMMAND serialize)

add_executable(benchmark benchmark.cpp load_obj.cpp)
target_link_libraries(benchmark PUBLIC bvh)
set_target_properties(benchmark PROPERTIES CXX_STANDARD 20)
add_test(
NAME benchmark
COMMAND
benchmark ${CMAKE_CURRENT_SOURCE_DIR}/scenes/cornell_box.obj
--eye 0 1 2
--dir 0 0 -1
--up 0 1 0)

add_test(NAME simple_example COMMAND simple_example)
add_test(NAME serialize COMMAND serialize)
if (BVH_BUILD_C_API)
add_executable(c_api_example c_api_example.c load_obj.cpp)
target_link_libraries(c_api_example PUBLIC bvh_c)

add_test(
NAME c_api_example
COMMAND
c_api_example
${CMAKE_CURRENT_SOURCE_DIR}/scenes/cornell_box.obj
--eye 0 1 2
--dir 0 0 -1
--up 0 1 0)
endif()
2 changes: 1 addition & 1 deletion test/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,6 @@ int main(int argc, char** argv) {
}

image.save(options.output_image);
std::cout << "Image saved as " << options.output_image << std::endl;
std::cout << "Image saved as '" << options.output_image << "'" << std::endl;
return 0;
}
Loading

0 comments on commit 5303ce8

Please sign in to comment.