From 4192bceb4f7fa060812fc8a0b7cdf3726f0f61a0 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 9762f43e4a..52cea1830c 100644 --- a/include/embree4/rtcore_device.h +++ b/include/embree4/rtcore_device.h @@ -116,10 +116,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 34306436e5..f8c58ddbef 100644 --- a/kernels/common/rtcore.cpp +++ b/kernels/common/rtcore.cpp @@ -181,6 +181,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; @@ -190,6 +212,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 539d4604143768e0a1603f212975575f1f480757 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 | 32 +++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/include/embree4/rtcore_buffer.h b/include/embree4/rtcore_buffer.h index eb7deca26c..2901106b75 100644 --- a/include/embree4/rtcore_buffer.h +++ b/include/embree4/rtcore_buffer.h @@ -56,6 +56,12 @@ RTC_API_CPP sycl::event rtcCommitBufferWithQueue(RTCBuffer buffer, sycl::queue q #endif +/* 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 2306757f29..0dc5c659b1 100644 --- a/kernels/common/buffer.h +++ b/kernels/common/buffer.h @@ -175,6 +175,11 @@ namespace embree return numBytes; } + /*! returns if the buffer is shared */ + __forceinline bool isShared() const { + return shared; + } + /*! returns true of the buffer is not empty */ __forceinline operator bool() const { return ptr; @@ -349,6 +354,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 char* dptr_ofs; //!< base pointer plus offset in device memory diff --git a/kernels/common/rtcore.cpp b/kernels/common/rtcore.cpp index f8c58ddbef..8d36134c46 100644 --- a/kernels/common/rtcore.cpp +++ b/kernels/common/rtcore.cpp @@ -289,6 +289,28 @@ 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); + 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); + return buffer->bytes(); + RTC_CATCH_END2(buffer); + return 0; + } + RTC_API void* rtcGetBufferData(RTCBuffer hbuffer) { Buffer* buffer = (Buffer*)hbuffer; @@ -329,7 +351,7 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_END2(buffer); } - RTC_API RTCScene rtcNewScene (RTCDevice hdevice) + RTC_API RTCScene rtcNewScene (RTCDevice hdevice) { RTC_CATCH_BEGIN; RTC_TRACE(rtcNewScene); @@ -365,7 +387,7 @@ RTC_NAMESPACE_BEGIN; return (RTCTraversable)nullptr; } - RTC_API void rtcSetSceneProgressMonitorFunction(RTCScene hscene, RTCProgressMonitorFunction progress, void* ptr) + RTC_API void rtcSetSceneProgressMonitorFunction(RTCScene hscene, RTCProgressMonitorFunction progress, void* ptr) { Scene* scene = (Scene*) hscene; RTC_CATCH_BEGIN; @@ -419,7 +441,7 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_BEGIN; RTC_TRACE(rtcCommitScene); RTC_VERIFY_HANDLE(hscene); - + scene->commit(false); #if defined(EMBREE_SYCL_SUPPORT) @@ -435,7 +457,7 @@ RTC_NAMESPACE_BEGIN; RTC_CATCH_BEGIN; RTC_TRACE(rtcJoinCommitScene); RTC_VERIFY_HANDLE(hscene); - + scene->commit(true); RTC_CATCH_END2(scene); } @@ -1406,7 +1428,7 @@ RTC_NAMESPACE_BEGIN; rtcForwardOccluded16Ex(valid, args, (RTCScene)htraversable, iray, instID, instPrimID); } - RTC_API void rtcRetainScene (RTCScene hscene) + RTC_API void rtcRetainScene (RTCScene hscene) { Scene* scene = (Scene*) hscene; RTC_CATCH_BEGIN; From 2eb540535c054a1fa648fae55fab7c853667ee33 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 b9df4430b6..842c8e0203 100644 --- a/kernels/rtcore_config.h.in +++ b/kernels/rtcore_config.h.in @@ -37,6 +37,7 @@ # define RTC_NAMESPACE_USE using namespace @EMBREE_API_NAMESPACE@; # define RTC_API_EXTERN_C # define RTC_API_EXTERN_CPP +# define RTC_DEFAULT_VALUE(value) = value # undef EMBREE_API_NAMESPACE #else # define RTC_NAMESPACE_BEGIN @@ -48,6 +49,7 @@ # else # define RTC_API_EXTERN_C # endif +# define RTC_DEFAULT_VALUE(value) #endif #if defined(ISPC) From 49e5042c0098c891a5cb74f896f946d097608fdb 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 74f7ccff2b..26c2e5d41e 100644 --- a/include/embree4/rtcore_scene.h +++ b/include/embree4/rtcore_scene.h @@ -107,6 +107,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); @@ -120,6 +128,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); @@ -127,9 +138,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 8d36134c46..2ec9c1d0f1 100644 --- a/kernels/common/rtcore.cpp +++ b/kernels/common/rtcore.cpp @@ -398,6 +398,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; @@ -412,6 +438,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; @@ -462,6 +500,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; @@ -2361,6 +2411,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 84a84f8c69..320930c529 100644 --- a/kernels/common/scene.cpp +++ b/kernels/common/scene.cpp @@ -1089,6 +1089,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 9ac1dd5eeb..46e7de4d52 100644 --- a/kernels/common/scene.h +++ b/kernels/common/scene.h @@ -383,6 +383,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