From 0ed616e55c18a44be7a77007f17494971e82320c Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Mon, 17 Mar 2025 09:48:39 -0700 Subject: [PATCH 1/4] Add getters for device callback functions and user pointers to the C API. --- include/embree4/rtcore_device.h | 12 +++++++++ kernels/common/rtcore.cpp | 44 +++++++++++++++++++++++++++++++++ kernels/common/state.h | 20 +++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/include/embree4/rtcore_device.h b/include/embree4/rtcore_device.h index 5d46b453ed..58e51ee5e4 100644 --- a/include/embree4/rtcore_device.h +++ b/include/embree4/rtcore_device.h @@ -108,10 +108,22 @@ typedef void (*RTCErrorFunction)(void* userPtr, enum RTCError code, const char* /* Sets the error callback function. */ RTC_API void rtcSetDeviceErrorFunction(RTCDevice device, RTCErrorFunction error, void* userPtr); +/* Gets the error callback function pointer. */ +RTC_API RTCErrorFunction rtcGetDeviceErrorFunction(RTCDevice device); + +/* Gets the error callback function user pointer. */ +RTC_API void* rtcGetDeviceErrorFunctionUserPtr(RTCDevice device); + /* Memory monitor callback function */ typedef bool (*RTCMemoryMonitorFunction)(void* ptr, ssize_t bytes, bool post); /* Sets the memory monitor callback function. */ RTC_API void rtcSetDeviceMemoryMonitorFunction(RTCDevice device, RTCMemoryMonitorFunction memoryMonitor, void* userPtr); +/* Gets the memory monitor callback function pointer. */ +RTC_API RTCMemoryMonitorFunction rtcGetDeviceMemoryMonitorFunction(RTCDevice device); + +/* Gets the memory monitor callback function user pointer. */ +RTC_API void* rtcGetDeviceMemoryMonitorFunctionUserPtr(RTCDevice device); + RTC_NAMESPACE_END diff --git a/kernels/common/rtcore.cpp b/kernels/common/rtcore.cpp index 2478b6ff99..abefd86bed 100644 --- a/kernels/common/rtcore.cpp +++ b/kernels/common/rtcore.cpp @@ -163,6 +163,28 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_END(device); } + RTC_API RTCErrorFunction rtcGetDeviceErrorFunction(RTCDevice hdevice) + { + Device* device = (Device*) hdevice; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetDeviceErrorFunction); + RTC_VERIFY_HANDLE(hdevice); + return device->getErrorFunction(); + RTC_CATCH_END(device); + return nullptr; + } + + RTC_API void* rtcGetDeviceErrorFunctionUserPtr(RTCDevice hdevice) + { + Device* device = (Device*) hdevice; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetDeviceErrorFunctionUserPtr); + RTC_VERIFY_HANDLE(hdevice); + return device->getErrorFunctionUserPtr(); + RTC_CATCH_END(device); + return nullptr; + } + RTC_API void rtcSetDeviceMemoryMonitorFunction(RTCDevice hdevice, RTCMemoryMonitorFunction memoryMonitor, void* userPtr) { Device* device = (Device*) hdevice; @@ -172,6 +194,28 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_END(device); } + RTC_API RTCMemoryMonitorFunction rtcGetDeviceMemoryMonitorFunction(RTCDevice hdevice) + { + Device* device = (Device*) hdevice; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetDeviceMemoryMonitorFunction); + RTC_VERIFY_HANDLE(hdevice); + return device->getMemoryMonitorFunction(); + RTC_CATCH_END(device); + return nullptr; + } + + RTC_API void* rtcGetDeviceMemoryMonitorFunctionUserPtr(RTCDevice hdevice) + { + Device* device = (Device*) hdevice; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetDeviceMemoryMonitorFunctionUserPtr); + RTC_VERIFY_HANDLE(hdevice); + return device->getMemoryMonitorFunctionUserPtr(); + RTC_CATCH_END(device); + return nullptr; + } + RTC_API RTCBuffer rtcNewBuffer(RTCDevice hdevice, size_t byteSize) { RTC_CATCH_BEGIN; diff --git a/kernels/common/state.h b/kernels/common/state.h index 2617a783c2..af19c505a8 100644 --- a/kernels/common/state.h +++ b/kernels/common/state.h @@ -189,6 +189,16 @@ namespace embree error_function_userptr = uptr; } + RTCErrorFunction getErrorFunction() const + { + return error_function; + } + + void* getErrorFunctionUserPtr() const + { + return error_function_userptr; + } + RTCErrorFunction error_function; void* error_function_userptr; @@ -199,6 +209,16 @@ namespace embree memory_monitor_userptr = uptr; } + RTCMemoryMonitorFunction getMemoryMonitorFunction() const + { + return memory_monitor_function; + } + + void* getMemoryMonitorFunctionUserPtr() const + { + return memory_monitor_userptr; + } + RTCMemoryMonitorFunction memory_monitor_function; void* memory_monitor_userptr; }; From 0cb4666f47ed62b9440275d768a6c7c83afe48db Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Mon, 17 Mar 2025 09:52:41 -0700 Subject: [PATCH 2/4] Add IsBufferShared and GetBufferSize to the C API to inspect an already created RTCBuffer. --- include/embree4/rtcore_buffer.h | 6 ++++++ kernels/common/buffer.h | 15 +++++++++++++++ kernels/common/rtcore.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/include/embree4/rtcore_buffer.h b/include/embree4/rtcore_buffer.h index 8721ce4a44..3ab66bb6d8 100644 --- a/include/embree4/rtcore_buffer.h +++ b/include/embree4/rtcore_buffer.h @@ -41,6 +41,12 @@ RTC_API RTCBuffer rtcNewBuffer(RTCDevice device, size_t byteSize); /* Creates a new shared buffer. */ RTC_API RTCBuffer rtcNewSharedBuffer(RTCDevice device, void* ptr, size_t byteSize); +/* Returns if the buffer is a shared buffer. */ +RTC_API bool rtcIsBufferShared(RTCBuffer buffer); + +/* Returns the size of the buffer in bytes. */ +RTC_API size_t rtcGetBufferSize(RTCBuffer buffer); + /* Returns a pointer to the buffer data. */ RTC_API void* rtcGetBufferData(RTCBuffer buffer); diff --git a/kernels/common/buffer.h b/kernels/common/buffer.h index 831f5815e8..1037267d36 100644 --- a/kernels/common/buffer.h +++ b/kernels/common/buffer.h @@ -111,6 +111,11 @@ namespace embree __forceinline size_t bytes() const { return numBytes; } + + /*! returns the number of bytes of the buffer */ + __forceinline bool isShared() const { + return shared; + } /*! returns true of the buffer is not empty */ __forceinline operator bool() const { @@ -215,6 +220,16 @@ namespace embree volatile int MAYBE_UNUSED w = *((int*)getPtr(size()-1)+3); // FIXME: is failing hard avoidable? } + /*! returns the buffer object */ + __forceinline Ref getBuffer() { + return buffer; + } + + /*! returns the offset of the view from the base pointer */ + __forceinline size_t getOffset() const { + return ptr_ofs - buffer->getPtr(); + } + public: char* ptr_ofs; //!< base pointer plus offset size_t stride; //!< stride of the buffer in bytes diff --git a/kernels/common/rtcore.cpp b/kernels/common/rtcore.cpp index abefd86bed..6088d49993 100644 --- a/kernels/common/rtcore.cpp +++ b/kernels/common/rtcore.cpp @@ -240,6 +240,30 @@ RTC_NAMESPACE_BEGIN; return nullptr; } + RTC_API bool rtcIsBufferShared(RTCBuffer hbuffer) + { + Buffer* buffer = (Buffer*)hbuffer; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcIsBufferShared); + RTC_VERIFY_HANDLE(hbuffer); + RTC_ENTER_DEVICE(hbuffer); + return buffer->isShared(); + RTC_CATCH_END2(buffer); + return false; + } + + RTC_API size_t rtcGetBufferSize(RTCBuffer hbuffer) + { + Buffer* buffer = (Buffer*)hbuffer; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetBufferSize); + RTC_VERIFY_HANDLE(hbuffer); + RTC_ENTER_DEVICE(hbuffer); + return buffer->bytes(); + RTC_CATCH_END2(buffer); + return 0; + } + RTC_API void* rtcGetBufferData(RTCBuffer hbuffer) { Buffer* buffer = (Buffer*)hbuffer; From 63b71d1d899cf369e37408c3eea617b28f714b4b Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Mon, 17 Mar 2025 09:56:06 -0700 Subject: [PATCH 3/4] Added a new macro for allowing default values in the C++ API. --- kernels/rtcore_config.h.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernels/rtcore_config.h.in b/kernels/rtcore_config.h.in index a202085258..135dd8a566 100644 --- a/kernels/rtcore_config.h.in +++ b/kernels/rtcore_config.h.in @@ -36,6 +36,7 @@ # define RTC_NAMESPACE_END } # define RTC_NAMESPACE_USE using namespace @EMBREE_API_NAMESPACE@; # define RTC_API_EXTERN_C +# define RTC_DEFAULT_VALUE(value) = value # undef EMBREE_API_NAMESPACE #else # define RTC_NAMESPACE_BEGIN @@ -46,6 +47,7 @@ # else # define RTC_API_EXTERN_C # endif +# define RTC_DEFAULT_VALUE(value) #endif #if defined(ISPC) From 54cbdf17307c52b5c8822d4846c0919d11c5c76d Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Mon, 17 Mar 2025 10:03:54 -0700 Subject: [PATCH 4/4] Added getters for if the scene is modified, the progress monitor function, the progress user pointer, and the scene build quality. Also added getters for getting the number of attached geometries as well as the array of geometries attached to the scene. All of these are available in the C API. --- include/embree4/rtcore_scene.h | 20 ++++++ kernels/common/rtcore.cpp | 128 +++++++++++++++++++++++++++++++++ kernels/common/scene.cpp | 10 +++ kernels/common/scene.h | 2 + 4 files changed, 160 insertions(+) diff --git a/include/embree4/rtcore_scene.h b/include/embree4/rtcore_scene.h index b02244cdc9..9446955a57 100644 --- a/include/embree4/rtcore_scene.h +++ b/include/embree4/rtcore_scene.h @@ -102,6 +102,14 @@ RTC_API void rtcAttachGeometryByID(RTCScene scene, RTCGeometry geometry, unsigne /* Detaches the geometry from the scene. */ RTC_API void rtcDetachGeometry(RTCScene scene, unsigned int geomID); +/* Gets the number of geometry IDs attached to the scene. To get the array of valid IDs allocate an array of the correct size and pass it as geomIDs. */ +RTC_API size_t rtcGetNumAttachedGeometryIDs(RTCScene scene); +RTC_API void rtcGetAttachedGeometryIDs(RTCScene scene, size_t* numGeomIDs, unsigned int* geomIDs RTC_DEFAULT_VALUE(nullptr)); + +/* Gets the number of geometries attached to the scene. To get the array of valid geometries allocate an array of the correct size and pass it as geometries. */ +RTC_API size_t rtcGetNumAttachedGeometries(RTCScene scene); +RTC_API void rtcGetAttachedGeometries(RTCScene scene, size_t* numGeoms, RTCGeometry* geometries RTC_DEFAULT_VALUE(nullptr)); + /* Gets a geometry handle from the scene. This function is not thread safe and should get used during rendering. */ RTC_API RTCGeometry rtcGetGeometry(RTCScene scene, unsigned int geomID); @@ -121,6 +129,9 @@ RTC_API void rtcCommitScene(RTCScene scene); /* Commits the scene from multiple threads. */ RTC_API void rtcJoinCommitScene(RTCScene scene); +/* Returns if the scene has been modified since it was last committed. */ +RTC_API bool rtcIsSceneModified(RTCScene scene); + /* Progress monitor callback function */ typedef bool (*RTCProgressMonitorFunction)(void* ptr, double n); @@ -128,9 +139,18 @@ typedef bool (*RTCProgressMonitorFunction)(void* ptr, double n); /* Sets the progress monitor callback function of the scene. */ RTC_API void rtcSetSceneProgressMonitorFunction(RTCScene scene, RTCProgressMonitorFunction progress, void* ptr); +/* Gets the progress monitor callback function of the scene. */ +RTC_API RTCProgressMonitorFunction rtcGetSceneProgressMonitorFunction(RTCScene scene); + +/* Gets the progress monitor callback function user pointer of the scene. */ +RTC_API void* rtcGetSceneProgressMonitorFunctionUserPtr(RTCScene scene); + /* Sets the build quality of the scene. */ RTC_API void rtcSetSceneBuildQuality(RTCScene scene, enum RTCBuildQuality quality); +/* Gets the build quality of the scene. */ +RTC_API enum RTCBuildQuality rtcGetSceneBuildQuality(RTCScene scene); + /* Sets the scene flags. */ RTC_API void rtcSetSceneFlags(RTCScene scene, enum RTCSceneFlags flags); diff --git a/kernels/common/rtcore.cpp b/kernels/common/rtcore.cpp index 6088d49993..71eb46937f 100644 --- a/kernels/common/rtcore.cpp +++ b/kernels/common/rtcore.cpp @@ -333,6 +333,32 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_END2(scene); } + RTC_API RTCProgressMonitorFunction rtcGetSceneProgressMonitorFunction(RTCScene hscene) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetSceneProgressMonitorFunction); + RTC_VERIFY_HANDLE(hscene); + RTC_ENTER_DEVICE(hscene); + Lock lock(g_mutex); + return scene->getProgressMonitorFunction(); + RTC_CATCH_END2(scene); + return nullptr; + } + + RTC_API void* rtcGetSceneProgressMonitorFunctionUserPtr(RTCScene hscene) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetSceneProgressMonitorFunctionUserPtr); + RTC_VERIFY_HANDLE(hscene); + RTC_ENTER_DEVICE(hscene); + Lock lock(g_mutex); + return scene->getProgressMonitorFunctionUserPtr(); + RTC_CATCH_END2(scene); + return nullptr; + } + RTC_API void rtcSetSceneBuildQuality (RTCScene hscene, RTCBuildQuality quality) { Scene* scene = (Scene*) hscene; @@ -348,6 +374,18 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_END2(scene); } + RTC_API RTCBuildQuality rtcGetSceneBuildQuality (RTCScene hscene) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetSceneBuildQuality); + RTC_VERIFY_HANDLE(hscene); + RTC_ENTER_DEVICE(hscene); + return scene->getBuildQuality(); + RTC_CATCH_END2(scene); + return RTC_BUILD_QUALITY_MEDIUM; + } + RTC_API void rtcSetSceneFlags (RTCScene hscene, RTCSceneFlags flags) { Scene* scene = (Scene*) hscene; @@ -400,6 +438,18 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_END2(scene); } + RTC_API bool rtcIsSceneModified(RTCScene hscene) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcIsSceneModified); + RTC_VERIFY_HANDLE(hscene); + RTC_ENTER_DEVICE(hscene); + return scene->isModified(); + RTC_CATCH_END2(scene); + return false; + } + RTC_API void rtcGetSceneBounds(RTCScene hscene, RTCBounds* bounds_o) { Scene* scene = (Scene*) hscene; @@ -2109,6 +2159,84 @@ RTC_API void rtcSetGeometryTransform(RTCGeometry hgeometry, unsigned int timeSte RTC_CATCH_END2(geometry); } + RTC_API size_t rtcGetNumAttachedGeometryIDs (RTCScene hscene) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetNumAttachedGeometryIDs); + RTC_VERIFY_HANDLE(hscene); + RTC_ENTER_DEVICE(hscene); + unsigned int numGeomIDs = scene->size(); + for (unsigned int i = 0; i < scene->size(); ++i) { + Geometry* ptr = scene->get(i); + if (!ptr) { + numGeomIDs -= 1; + } + } + return numGeomIDs; + RTC_CATCH_END2(scene); + return 0; + } + + RTC_API void rtcGetAttachedGeometryIDs (RTCScene hscene, size_t* numGeomIDs, unsigned int* geomIDs) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetAttachedGeometryIDs); + RTC_VERIFY_HANDLE(hscene); + RTC_VERIFY_HANDLE(numGeomIDs); + RTC_ENTER_DEVICE(hscene); + *numGeomIDs = scene->size(); + for (unsigned int i = 0; i < scene->size(); ++i) { + Geometry* ptr = scene->get(i); + if (!ptr) { + *numGeomIDs -= 1; + } else if (geomIDs) { + *geomIDs++ = i; + } + } + RTC_CATCH_END2(scene); + } + + RTC_API size_t rtcGetNumAttachedGeometries (RTCScene hscene) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetNumAttachedGeometryIDs); + RTC_VERIFY_HANDLE(hscene); + RTC_ENTER_DEVICE(hscene); + unsigned int numGeomIDs = scene->size(); + for (unsigned int i = 0; i < scene->size(); ++i) { + Geometry* ptr = scene->get(i); + if (!ptr) { + numGeomIDs -= 1; + } + } + return numGeomIDs; + RTC_CATCH_END2(scene); + return 0; + } + + RTC_API void rtcGetAttachedGeometries (RTCScene hscene, size_t* numGeoms, RTCGeometry* geometries) + { + Scene* scene = (Scene*) hscene; + RTC_CATCH_BEGIN; + RTC_TRACE(rtcGetAttachedGeometries); + RTC_VERIFY_HANDLE(hscene); + RTC_VERIFY_HANDLE(numGeoms); + RTC_ENTER_DEVICE(hscene); + *numGeoms = scene->size(); + for (unsigned int i = 0; i < scene->size(); ++i) { + Geometry* ptr = scene->get(i); + if (!ptr) { + *numGeoms -= 1; + } else if (geometries) { + *geometries++ = (RTCGeometry) ptr; + } + } + RTC_CATCH_END2(scene); + } + RTC_API RTCGeometry rtcGetGeometry (RTCScene hscene, unsigned int geomID) { Scene* scene = (Scene*) hscene; diff --git a/kernels/common/scene.cpp b/kernels/common/scene.cpp index d40c701b83..f7cf648388 100644 --- a/kernels/common/scene.cpp +++ b/kernels/common/scene.cpp @@ -1033,6 +1033,16 @@ namespace embree progress_monitor_ptr = ptr; } + RTCProgressMonitorFunction Scene::getProgressMonitorFunction() const + { + return progress_monitor_function; + } + + void* Scene::getProgressMonitorFunctionUserPtr() const + { + return progress_monitor_ptr; + } + void Scene::progressMonitor(double dn) { if (progress_monitor_function) { diff --git a/kernels/common/scene.h b/kernels/common/scene.h index f996154945..4444d128b0 100644 --- a/kernels/common/scene.h +++ b/kernels/common/scene.h @@ -328,6 +328,8 @@ namespace embree std::atomic progress_monitor_counter; void progressMonitor(double nprims); void setProgressMonitorFunction(RTCProgressMonitorFunction func, void* ptr); + RTCProgressMonitorFunction getProgressMonitorFunction() const; + void* getProgressMonitorFunctionUserPtr() const; private: GeometryCounts world; //!< counts for geometry