rtcore_builder.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #ifndef __RTCORE_BUILDER_H__
  17. #define __RTCORE_BUILDER_H__
  18. #include "rtcore.h"
  19. /*! \brief Defines an opaque BVH type */
  20. typedef struct __RTCBVH {}* RTCBVH;
  21. /*! \brief Defines an opaque thread local allocator type */
  22. typedef struct __RTCThreadLocalAllocator {}* RTCThreadLocalAllocator;
  23. /*! Quality settings for BVH build. */
  24. enum RTCBuildQuality
  25. {
  26. RTC_BUILD_QUALITY_LOW = 0, //!< build low quality BVH (good for dynamic scenes)
  27. RTC_BUILD_QUALITY_NORMAL = 1, //!< build standard quality BVH
  28. RTC_BUILD_QUALITY_HIGH = 2, //!< build high quality BVH
  29. };
  30. /*! Settings for builders */
  31. struct RTCBuildSettings
  32. {
  33. unsigned size; //!< Size of this structure in bytes. Makes future extension easier.
  34. RTCBuildQuality quality; //!< quality of BVH build
  35. unsigned maxBranchingFactor; //!< miximal branching factor of BVH to build
  36. unsigned maxDepth; //!< maximal depth of BVH to build
  37. unsigned sahBlockSize; //!< block size for SAH heuristic
  38. unsigned minLeafSize; //!< minimal size of a leaf
  39. unsigned maxLeafSize; //!< maximal size of a leaf
  40. float travCost; //!< estimated cost of one traversal step
  41. float intCost; //!< estimated cost of one primitive intersection
  42. unsigned extraSpace; //!< for spatial splitting we need extra space at end of primitive array
  43. };
  44. /*! Creates default build settings. */
  45. inline RTCBuildSettings rtcDefaultBuildSettings()
  46. {
  47. RTCBuildSettings settings;
  48. settings.size = sizeof(settings);
  49. settings.quality = RTC_BUILD_QUALITY_NORMAL;
  50. settings.maxBranchingFactor = 2;
  51. settings.maxDepth = 32;
  52. settings.sahBlockSize = 1;
  53. settings.minLeafSize = 1;
  54. settings.maxLeafSize = 32;
  55. settings.travCost = 1.0f;
  56. settings.intCost = 1.0f;
  57. settings.extraSpace = 0;
  58. return settings;
  59. }
  60. /*! Input primitives for the builder. Stores primitive bounds and ID. */
  61. struct RTCORE_ALIGN(32) RTCBuildPrimitive
  62. {
  63. float lower_x, lower_y, lower_z; //!< lower bounds in x/y/z
  64. int geomID; //!< first ID
  65. float upper_x, upper_y, upper_z; //!< upper bounds in x/y/z
  66. int primID; //!< second ID
  67. };
  68. /*! Creates a new BVH. */
  69. RTCORE_API RTCBVH rtcNewBVH(RTCDevice device);
  70. /*! Callback to create a node. */
  71. typedef void* (*RTCCreateNodeFunc) (RTCThreadLocalAllocator allocator, size_t numChildren, void* userPtr);
  72. /*! Callback to set the pointer to all children. */
  73. typedef void (*RTCSetNodeChildrenFunc) (void* nodePtr, void** children, size_t numChildren, void* userPtr);
  74. /*! Callback to set the bounds of all children. */
  75. typedef void (*RTCSetNodeBoundsFunc) (void* nodePtr, const RTCBounds** bounds, size_t numChildren, void* userPtr);
  76. /*! Callback to create a leaf node. */
  77. typedef void* (*RTCCreateLeafFunc) (RTCThreadLocalAllocator allocator, const RTCBuildPrimitive* prims, size_t numPrims, void* userPtr);
  78. /*! Callback to split a build primitive. */
  79. typedef void (*RTCSplitPrimitiveFunc) (const RTCBuildPrimitive& prim, unsigned dim, float pos, RTCBounds& lbounds, RTCBounds& rbounds, void* userPtr);
  80. /*! Callback to provide build progress. */
  81. typedef void (*RTCBuildProgressFunc) (size_t dn, void* userPtr);
  82. /*! builds the BVH */
  83. RTCORE_API void* rtcBuildBVH(RTCBVH bvh, //!< BVH to build
  84. const RTCBuildSettings& settings, //!< settings for BVH builder
  85. RTCBuildPrimitive* primitives, //!< list of input primitives
  86. size_t numPrimitives, //!< number of input primitives
  87. RTCCreateNodeFunc createNode, //!< creates a node
  88. RTCSetNodeChildrenFunc setNodeChildren, //!< sets pointer to all children
  89. RTCSetNodeBoundsFunc setNodeBounds, //!< sets bounds of all children
  90. RTCCreateLeafFunc createLeaf, //!< creates a leaf
  91. RTCSplitPrimitiveFunc splitPrimitive, //!< splits a primitive
  92. RTCBuildProgressFunc buildProgress, //!< used to report build progress
  93. void* userPtr //!< user pointer passed to callback functions
  94. );
  95. /*! Allocates memory using the thread local allocator. Use this function to allocate nodes in the callback functions. */
  96. RTCORE_API void* rtcThreadLocalAlloc(RTCThreadLocalAllocator allocator, size_t bytes, size_t align);
  97. /*! Makes the BVH static. No further rtcBVHBuild can be called anymore on the BVH. */
  98. RTCORE_API void rtcMakeStaticBVH(RTCBVH bvh);
  99. /*! Deletes the BVH including all allocated nodes. */
  100. RTCORE_API void rtcDeleteBVH(RTCBVH bvh);
  101. #endif