commit 471ac2e987dae201c7b1034920910dca2bd339fc
parent e6031c817e61b1c21b4cb68ab0a6dd08e86ea7f0
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 18 Sep 2020 16:58:16 +0200
Implement the suvm_device functions
Diffstat:
| A | cmake/CMakeLists.txt | | | 101 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/suvm_c.h | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/suvm_device.c | | | 164 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/suvm_device.h | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 382 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -0,0 +1,101 @@
+# Copyright (C) 2020 |Meso|Star> (contact@meso-star.com)
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 2.8)
+project(suvm C)
+enable_testing()
+
+set(SUVM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
+option(NO_TEST "Do not build tests" OFF)
+
+################################################################################
+# Check dependencies
+################################################################################
+find_package(Embree 3.6 REQUIRED)
+find_package(RCMake 0.4 REQUIRED)
+find_package(RSys 0.10 REQUIRED)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
+include(rcmake)
+include(rcmake_runtime)
+
+include_directories(${RSys_INCLUDE_DIR})
+
+if(CMAKE_COMPILER_IS_GNUCC)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
+endif()
+
+################################################################################
+# Configure and define targets
+################################################################################
+set(VERSION_MAJOR 0)
+set(VERSION_MINOR 0)
+set(VERSION_PATCH 0)
+set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
+
+set(SUVM_FILES_SRC
+ suvm_device.c)
+set(SUVM_FILES_INC
+ suvm_device.h)
+set(SUVM_FILES_INC_API
+ suvm.h)
+
+set(SUVM_FILES_DOC COPYING README.md)
+
+# Prepend each file in the `SUVM_FILES_<SRC|INC>' list by `SUVM_SOURCE_DIR'
+rcmake_prepend_path(SUVM_FILES_SRC ${SUVM_SOURCE_DIR})
+rcmake_prepend_path(SUVM_FILES_INC ${SUVM_SOURCE_DIR})
+rcmake_prepend_path(SUVM_FILES_INC_API ${SUVM_SOURCE_DIR})
+rcmake_prepend_path(SUVM_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
+
+add_library(suvm SHARED ${SUVM_FILES_SRC} ${SUVM_FILES_INC} ${SUVM_FILES_INC_API})
+target_link_libraries(suvm RSys ${EMBREE_LIBRARIES})
+
+set_target_properties(suvm PROPERTIES
+ DEFINE_SYMBOL SUVM_SHARED_BUILD
+ VERSION ${VERSION}
+ SOVERSION ${VERSION_MAJOR})
+
+rcmake_setup_devel(suvm StarUVM ${VERSION} star/suvm_version.h)
+
+################################################################################
+# Add tests
+################################################################################
+if(NOT NO_TEST)
+ # function(build_test _name)
+ # add_executable(${_name}
+ # ${SUVM_SOURCE_DIR}/${_name}.c
+ # ${SUVM_SOURCE_DIR}/test_svx_utils.h)
+ # target_link_libraries(${_name} svx RSys ${ARGN})
+ # endfunction()
+ #
+ # function(new_test _name)
+ # build_test(${_name} ${ARGN})
+ # add_test(${_name} ${_name})
+ # endfunction()
+
+ # new_test(test_svx_bintree)
+endif()
+
+################################################################################
+# Define output & install directories
+################################################################################
+install(TARGETS suvm
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin)
+install(FILES ${SUVM_FILES_INC_API} DESTINATION include/star)
+install(FILES ${SUVM_FILES_DOC} DESTINATION share/doc/star-uvm)
+
diff --git a/src/suvm_c.h b/src/suvm_c.h
@@ -0,0 +1,54 @@
+/* Copyright (C) 2020 |Meso|Star> (contact@meso-star.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef SUVM_C_H
+#define SUVM_C_H
+
+#include <rsys/rsys.h>
+#include <embree3/rtcore.h>
+
+static FINLINE res_T
+rtc_error_to_res_T(const enum RTCError err)
+{
+ switch(err) {
+ case RTC_ERROR_NONE: return RES_OK;
+ case RTC_ERROR_UNKNOWN: return RES_UNKNOWN_ERR;
+ case RTC_ERROR_INVALID_ARGUMENT: return RES_BAD_ARG;
+ case RTC_ERROR_INVALID_OPERATION: return RES_BAD_ARG;
+ case RTC_ERROR_OUT_OF_MEMORY: return RES_MEM_ERR;
+ case RTC_ERROR_UNSUPPORTED_CPU: return RES_BAD_ARG;
+ case RTC_ERROR_CANCELLED: return RES_UNKNOWN_ERR;
+ default: FATAL("Unreachable code\n"); break;
+ }
+}
+
+static INLINE const char*
+rtc_error_string(const enum RTCError err)
+{
+ const char* str = NULL;
+ switch(err) {
+ case RTC_ERROR_NONE: str = "No error"; break;
+ case RTC_ERROR_UNKNOWN: str = "Unknown error"; break;
+ case RTC_ERROR_INVALID_ARGUMENT: str = "Invalid argument"; break;
+ case RTC_ERROR_INVALID_OPERATION: str = "Invalid operation"; break;
+ case RTC_ERROR_OUT_OF_MEMORY: str = "Out of memory"; break;
+ case RTC_ERROR_UNSUPPORTED_CPU: str = "Unsupported CPU"; break;
+ case RTC_ERROR_CANCELLED: str = "Cancelled operation"; break;
+ default: FATAL("Unreachable code\n"); break;
+ }
+ return str;
+}
+
+#endif /* SUVM_C_H */
diff --git a/src/suvm_device.c b/src/suvm_device.c
@@ -0,0 +1,164 @@
+/* Copyright (C) 2020 |Meso|Star> (contact@meso-star.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "suvm.h"
+#include "suvm_c.h"
+#include "suvm_device.h"
+
+#include <rsys/logger.h>
+#include <rsys/mem_allocator.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+log_msg
+ (const struct suvm_device* dev,
+ const enum log_type stream,
+ const char* msg,
+ va_list vargs)
+{
+ ASSERT(dev && msg);
+ if(dev->verbose) {
+ res_T res; (void)res;
+ res = logger_vprint(dev->logger, stream, msg, vargs);
+ ASSERT(res == RES_OK);
+ }
+}
+
+static void
+device_release(ref_T* ref)
+{
+ struct suvm_device* dev;
+ ASSERT(ref);
+ dev = CONTAINER_OF(ref, struct suvm_device, ref);
+ rtcReleaseDevice(dev->rtc);
+ MEM_RM(dev->allocator, dev);
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+suvm_device_create
+ (struct logger* log,
+ struct mem_allocator* mem_allocator,
+ int verbose,
+ struct suvm_device** out_dev)
+{
+ char embree_opts[512];
+ struct suvm_device* dev = NULL;
+ struct mem_allocator* allocator = NULL;
+ struct logger* logger = NULL;
+ int sz;
+ res_T res = RES_OK;
+
+ if(!out_dev) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
+ logger = log ? log : LOGGER_DEFAULT;
+
+ dev = MEM_CALLOC(allocator, 1, sizeof(*dev));
+ if(!dev) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&dev->ref);
+ dev->allocator = allocator;
+ dev->logger = logger;
+ dev->verbose = verbose;
+
+ sz = snprintf(embree_opts, sizeof(embree_opts), "verbose=%d", verbose);
+ if((size_t)sz >= sizeof(embree_opts)) {
+ log_err(dev, "Could not setup the Embree option string.\n");
+ res = RES_MEM_ERR;
+ goto error;
+ }
+
+ dev->rtc = rtcNewDevice(embree_opts);
+ if(dev->rtc == NULL) {
+ const enum RTCError err = rtcGetDeviceError(NULL);
+ log_err(dev, "Could not create the emree device -- %s.\n",
+ rtc_error_string(err));
+ res = rtc_error_to_res_T(err);
+ goto error;
+ }
+
+exit:
+ if(out_dev) *out_dev = dev;
+ return res;
+error:
+ if(dev) {
+ SUVM(device_ref_put(dev));
+ dev = NULL;
+ }
+ goto exit;
+}
+
+res_T
+suvm_device_ref_get(struct suvm_device* dev)
+{
+ if(!dev) return RES_BAD_ARG;
+ ref_get(&dev->ref);
+ return RES_OK;
+}
+
+res_T
+suvm_device_ref_put(struct suvm_device* dev)
+{
+ if(!dev) return RES_BAD_ARG;
+ ref_put(&dev->ref, device_release);
+ return RES_OK;
+}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+void
+log_info(struct suvm_device* dev, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(dev && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(dev, LOG_OUTPUT, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_err(struct suvm_device* dev, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(dev && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(dev, LOG_ERROR, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_warn(struct suvm_device* dev, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(dev && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(dev, LOG_WARNING, msg, vargs_list);
+ va_end(vargs_list);
+}
+
diff --git a/src/suvm_device.h b/src/suvm_device.h
@@ -0,0 +1,63 @@
+/* Copyright (C) 2020 |Meso|Star> (contact@meso-star.com)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef SUVM_DEVICE_H
+#define SUVM_DEVICE_H
+
+#include <rsys/ref_count.h>
+#include <embree3/rtcore.h>
+
+struct logger;
+struct mem_allocator;
+
+struct suvm_device {
+ int verbose;
+ struct logger* logger;
+ struct mem_allocator* allocator;
+ RTCDevice rtc; /* Embree device */
+ ref_T ref;
+};
+
+extern LOCAL_SYM void
+log_info
+ (struct suvm_device* dev,
+const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+extern LOCAL_SYM void
+log_err
+ (struct suvm_device* dev,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+extern LOCAL_SYM void
+log_warn
+ (struct suvm_device* dev,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+#endif /* SUVM_DEVICE_H */