Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement C bindings #87

Merged
merged 8 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
strategy:
matrix:
build-type: [Debug, Release]
c-api: [ON, OFF]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
Expand All @@ -23,7 +24,7 @@ jobs:
- name: Configure CMake
working-directory: ${{runner.workspace}}/build
shell: bash # Necessary because of the $GITHUB_WORKSPACE variable
run: cmake -DCMAKE_BUILD_TYPE=${{matrix.build-type}} -DBUILD_TESTING=ON -S $GITHUB_WORKSPACE
run: cmake -DCMAKE_BUILD_TYPE=${{matrix.build-type}} -DBVH_BUILD_C_API=${{matrix.c-api}} -DBUILD_TESTING=ON -S $GITHUB_WORKSPACE

- name: Build
working-directory: ${{runner.workspace}}/build
Expand Down
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
cmake_minimum_required(VERSION 3.21)
project(bvh)
project(bvh VERSION 2.0)

option(BVH_BUILD_C_API "Builds the C API library wrapper" OFF)
option(BVH_STATIC_LINK_STDLIB_C_API "Link the C API library statically against the standard C++ library" OFF)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

add_subdirectory(src/bvh/v2)

Expand All @@ -8,4 +15,6 @@ if (PROJECT_IS_TOP_LEVEL)
if (BUILD_TESTING)
add_subdirectory(test)
endif()

include(cmake/Install.cmake)
endif()
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Here is a list of features supported by this library (changes from `v1` are indi
- [NEW] Variable amount of dimensions (e.g. 2D, 3D, 4D BVHs are supported) and different scalar types
(e.g. `float` or `double`),
- [NEW] Only depends on the standard library (parallelization uses a custom thread pool based on
`std::thread`).
`std::thread`),
- [NEW] C API for the high-level parts of the library are available.

## Building

Expand All @@ -64,10 +65,19 @@ If you want to build the examples, use:
cmake .. -DCMAKE_BUILD_TYPE=<Debug|Release> -DENABLE_TESTING=ON
cmake --build .

## C API

The library can be used via a small set of high-level C bindings. These bindings are not enabled by
default, but can be built by configuring CMake with `-DBVH_BUILD_C_API=ON`. Additionally, if the
intent is to use the library in a pure C environment which does not have the C++ standard library as
a dependency, it might be a good idea to statically the C++ standard library. That can be done by
adding the flag `-DBVH_STATIC_LINK_STDLIB_C_API=ON` to the CMake command line.

## Usage

The library contains several examples that are kept up-to-date with the API:

- A [basic example](test/simple_example.cpp) that traces one ray on a scene made of a couple of triangles,
- A [benchmarking utility](test/benchmark.cpp) that showcases what the library can do.
- A [serialization test](test/serialize.cpp) that shows how to save and load a BVH from a file.
- A [C API example](test/c_api_example.cpp) that shows how to use the C bindings to this library.
34 changes: 34 additions & 0 deletions cmake/Install.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
set(bvh_targets bvh)
if (BVH_BUILD_C_API)
list(APPEND bvh_targets bvh_c)
endif()

install(
TARGETS ${bvh_targets}
EXPORT bvh_exports
RUNTIME DESTINATION bin/
LIBRARY DESTINATION lib/
ARCHIVE DESTINATION lib/
INCLUDES DESTINATION include/)
install(
EXPORT bvh_exports
FILE bvh-targets.cmake
NAMESPACE bvh::v2::
DESTINATION lib/cmake/bvh/v2/)

include(CMakePackageConfigHelpers)

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/bvh-config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/bvh-config.cmake"
INSTALL_DESTINATION lib/cmake/bvh/v2/)

write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/bvh-config-version.cmake"
COMPATIBILITY AnyNewerVersion)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/bvh-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/bvh-config-version.cmake"
DESTINATION lib/cmake/bvh/v2/)
5 changes: 5 additions & 0 deletions cmake/bvh-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/bvh-targets.cmake")

check_required_components(bvh_c)
15 changes: 14 additions & 1 deletion src/bvh/v2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,18 @@ 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(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION include/bvh/v2
FILES_MATCHING PATTERN "*.h"
PATTERN "c_api" EXCLUDE)

if (BVH_BUILD_C_API)
add_subdirectory(c_api)
endif()
17 changes: 17 additions & 0 deletions src/bvh/v2/c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)

if (BVH_STATIC_LINK_STDLIB_C_API)
# Link statically against standard C++ library
target_link_options(bvh_c PRIVATE $<$<C_COMPILER_ID:GNU,Clang>:-static-libstdc++>)
endif()

install(FILES bvh.h DESTINATION include/bvh/v2/c_api/)
28 changes: 28 additions & 0 deletions src/bvh/v2/c_api/bvh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bvh/v2/c_api/bvh.h>
#include <bvh/v2/c_api/bvh_impl.h>
#include <bvh/v2/thread_pool.h>

namespace bvh::v2::c_api {

BVH_TYPES(float, 2, 2f)
BVH_TYPES(float, 3, 3f)
BVH_TYPES(double, 2, 2d)
BVH_TYPES(double, 3, 3d)

extern "C" {

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));
}

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

BVH_IMPL(float, 2, 2f)
BVH_IMPL(float, 3, 3f)
BVH_IMPL(double, 2, 2d)
BVH_IMPL(double, 3, 3d)

} // extern "C"
} // namespace bvh::v2::c_api
Loading
Loading