rtcore_common.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <stddef.h>
  5. #include <sys/types.h>
  6. #include <stdbool.h>
  7. #include "rtcore_config.h"
  8. RTC_NAMESPACE_BEGIN
  9. #if defined(_WIN32)
  10. #if defined(_M_X64)
  11. typedef long long ssize_t;
  12. #else
  13. typedef int ssize_t;
  14. #endif
  15. #endif
  16. #if defined(_WIN32) && !defined(__MINGW32__)
  17. # define RTC_ALIGN(...) __declspec(align(__VA_ARGS__))
  18. #else
  19. # define RTC_ALIGN(...) __attribute__((aligned(__VA_ARGS__)))
  20. #endif
  21. #if !defined (RTC_DEPRECATED)
  22. #ifdef __GNUC__
  23. #define RTC_DEPRECATED __attribute__((deprecated))
  24. #elif defined(_MSC_VER)
  25. #define RTC_DEPRECATED __declspec(deprecated)
  26. #else
  27. #define RTC_DEPRECATED
  28. #endif
  29. #endif
  30. #if defined(_WIN32)
  31. # define RTC_FORCEINLINE __forceinline
  32. #else
  33. # define RTC_FORCEINLINE inline __attribute__((always_inline))
  34. #endif
  35. /* Invalid geometry ID */
  36. #define RTC_INVALID_GEOMETRY_ID ((unsigned int)-1)
  37. /* Maximum number of time steps */
  38. #define RTC_MAX_TIME_STEP_COUNT 129
  39. /* Formats of buffers and other data structures */
  40. enum RTCFormat
  41. {
  42. RTC_FORMAT_UNDEFINED = 0,
  43. /* 8-bit unsigned integer */
  44. RTC_FORMAT_UCHAR = 0x1001,
  45. RTC_FORMAT_UCHAR2,
  46. RTC_FORMAT_UCHAR3,
  47. RTC_FORMAT_UCHAR4,
  48. /* 8-bit signed integer */
  49. RTC_FORMAT_CHAR = 0x2001,
  50. RTC_FORMAT_CHAR2,
  51. RTC_FORMAT_CHAR3,
  52. RTC_FORMAT_CHAR4,
  53. /* 16-bit unsigned integer */
  54. RTC_FORMAT_USHORT = 0x3001,
  55. RTC_FORMAT_USHORT2,
  56. RTC_FORMAT_USHORT3,
  57. RTC_FORMAT_USHORT4,
  58. /* 16-bit signed integer */
  59. RTC_FORMAT_SHORT = 0x4001,
  60. RTC_FORMAT_SHORT2,
  61. RTC_FORMAT_SHORT3,
  62. RTC_FORMAT_SHORT4,
  63. /* 32-bit unsigned integer */
  64. RTC_FORMAT_UINT = 0x5001,
  65. RTC_FORMAT_UINT2,
  66. RTC_FORMAT_UINT3,
  67. RTC_FORMAT_UINT4,
  68. /* 32-bit signed integer */
  69. RTC_FORMAT_INT = 0x6001,
  70. RTC_FORMAT_INT2,
  71. RTC_FORMAT_INT3,
  72. RTC_FORMAT_INT4,
  73. /* 64-bit unsigned integer */
  74. RTC_FORMAT_ULLONG = 0x7001,
  75. RTC_FORMAT_ULLONG2,
  76. RTC_FORMAT_ULLONG3,
  77. RTC_FORMAT_ULLONG4,
  78. /* 64-bit signed integer */
  79. RTC_FORMAT_LLONG = 0x8001,
  80. RTC_FORMAT_LLONG2,
  81. RTC_FORMAT_LLONG3,
  82. RTC_FORMAT_LLONG4,
  83. /* 32-bit float */
  84. RTC_FORMAT_FLOAT = 0x9001,
  85. RTC_FORMAT_FLOAT2,
  86. RTC_FORMAT_FLOAT3,
  87. RTC_FORMAT_FLOAT4,
  88. RTC_FORMAT_FLOAT5,
  89. RTC_FORMAT_FLOAT6,
  90. RTC_FORMAT_FLOAT7,
  91. RTC_FORMAT_FLOAT8,
  92. RTC_FORMAT_FLOAT9,
  93. RTC_FORMAT_FLOAT10,
  94. RTC_FORMAT_FLOAT11,
  95. RTC_FORMAT_FLOAT12,
  96. RTC_FORMAT_FLOAT13,
  97. RTC_FORMAT_FLOAT14,
  98. RTC_FORMAT_FLOAT15,
  99. RTC_FORMAT_FLOAT16,
  100. /* 32-bit float matrix (row-major order) */
  101. RTC_FORMAT_FLOAT2X2_ROW_MAJOR = 0x9122,
  102. RTC_FORMAT_FLOAT2X3_ROW_MAJOR = 0x9123,
  103. RTC_FORMAT_FLOAT2X4_ROW_MAJOR = 0x9124,
  104. RTC_FORMAT_FLOAT3X2_ROW_MAJOR = 0x9132,
  105. RTC_FORMAT_FLOAT3X3_ROW_MAJOR = 0x9133,
  106. RTC_FORMAT_FLOAT3X4_ROW_MAJOR = 0x9134,
  107. RTC_FORMAT_FLOAT4X2_ROW_MAJOR = 0x9142,
  108. RTC_FORMAT_FLOAT4X3_ROW_MAJOR = 0x9143,
  109. RTC_FORMAT_FLOAT4X4_ROW_MAJOR = 0x9144,
  110. /* 32-bit float matrix (column-major order) */
  111. RTC_FORMAT_FLOAT2X2_COLUMN_MAJOR = 0x9222,
  112. RTC_FORMAT_FLOAT2X3_COLUMN_MAJOR = 0x9223,
  113. RTC_FORMAT_FLOAT2X4_COLUMN_MAJOR = 0x9224,
  114. RTC_FORMAT_FLOAT3X2_COLUMN_MAJOR = 0x9232,
  115. RTC_FORMAT_FLOAT3X3_COLUMN_MAJOR = 0x9233,
  116. RTC_FORMAT_FLOAT3X4_COLUMN_MAJOR = 0x9234,
  117. RTC_FORMAT_FLOAT4X2_COLUMN_MAJOR = 0x9242,
  118. RTC_FORMAT_FLOAT4X3_COLUMN_MAJOR = 0x9243,
  119. RTC_FORMAT_FLOAT4X4_COLUMN_MAJOR = 0x9244,
  120. /* special 12-byte format for grids */
  121. RTC_FORMAT_GRID = 0xA001
  122. };
  123. /* Build quality levels */
  124. enum RTCBuildQuality
  125. {
  126. RTC_BUILD_QUALITY_LOW = 0,
  127. RTC_BUILD_QUALITY_MEDIUM = 1,
  128. RTC_BUILD_QUALITY_HIGH = 2,
  129. RTC_BUILD_QUALITY_REFIT = 3,
  130. };
  131. /* Axis-aligned bounding box representation */
  132. struct RTC_ALIGN(16) RTCBounds
  133. {
  134. float lower_x, lower_y, lower_z, align0;
  135. float upper_x, upper_y, upper_z, align1;
  136. };
  137. /* Linear axis-aligned bounding box representation */
  138. struct RTC_ALIGN(16) RTCLinearBounds
  139. {
  140. struct RTCBounds bounds0;
  141. struct RTCBounds bounds1;
  142. };
  143. /* Intersection context flags */
  144. enum RTCIntersectContextFlags
  145. {
  146. RTC_INTERSECT_CONTEXT_FLAG_NONE = 0,
  147. RTC_INTERSECT_CONTEXT_FLAG_INCOHERENT = (0 << 0), // optimize for incoherent rays
  148. RTC_INTERSECT_CONTEXT_FLAG_COHERENT = (1 << 0) // optimize for coherent rays
  149. };
  150. /* Arguments for RTCFilterFunctionN */
  151. struct RTCFilterFunctionNArguments
  152. {
  153. int* valid;
  154. void* geometryUserPtr;
  155. struct RTCIntersectContext* context;
  156. struct RTCRayN* ray;
  157. struct RTCHitN* hit;
  158. unsigned int N;
  159. };
  160. /* Filter callback function */
  161. typedef void (*RTCFilterFunctionN)(const struct RTCFilterFunctionNArguments* args);
  162. /* Intersection context passed to intersect/occluded calls */
  163. struct RTCIntersectContext
  164. {
  165. enum RTCIntersectContextFlags flags; // intersection flags
  166. RTCFilterFunctionN filter; // filter function to execute
  167. #if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
  168. unsigned int instStackSize; // Number of instances currently on the stack.
  169. #endif
  170. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT]; // The current stack of instance ids.
  171. #if RTC_MIN_WIDTH
  172. float minWidthDistanceFactor; // curve radius is set to this factor times distance to ray origin
  173. #endif
  174. };
  175. /* Initializes an intersection context. */
  176. RTC_FORCEINLINE void rtcInitIntersectContext(struct RTCIntersectContext* context)
  177. {
  178. unsigned l = 0;
  179. context->flags = RTC_INTERSECT_CONTEXT_FLAG_INCOHERENT;
  180. context->filter = NULL;
  181. #if RTC_MAX_INSTANCE_LEVEL_COUNT > 1
  182. context->instStackSize = 0;
  183. #endif
  184. for (; l < RTC_MAX_INSTANCE_LEVEL_COUNT; ++l)
  185. context->instID[l] = RTC_INVALID_GEOMETRY_ID;
  186. #if RTC_MIN_WIDTH
  187. context->minWidthDistanceFactor = 0.0f;
  188. #endif
  189. }
  190. /* Point query structure for closest point query */
  191. struct RTC_ALIGN(16) RTCPointQuery
  192. {
  193. float x; // x coordinate of the query point
  194. float y; // y coordinate of the query point
  195. float z; // z coordinate of the query point
  196. float time; // time of the point query
  197. float radius; // radius of the point query
  198. };
  199. /* Structure of a packet of 4 query points */
  200. struct RTC_ALIGN(16) RTCPointQuery4
  201. {
  202. float x[4]; // x coordinate of the query point
  203. float y[4]; // y coordinate of the query point
  204. float z[4]; // z coordinate of the query point
  205. float time[4]; // time of the point query
  206. float radius[4]; // radius of the point query
  207. };
  208. /* Structure of a packet of 8 query points */
  209. struct RTC_ALIGN(32) RTCPointQuery8
  210. {
  211. float x[8]; // x coordinate of the query point
  212. float y[8]; // y coordinate of the query point
  213. float z[8]; // z coordinate of the query point
  214. float time[8]; // time of the point query
  215. float radius[8]; // radius ofr the point query
  216. };
  217. /* Structure of a packet of 16 query points */
  218. struct RTC_ALIGN(64) RTCPointQuery16
  219. {
  220. float x[16]; // x coordinate of the query point
  221. float y[16]; // y coordinate of the query point
  222. float z[16]; // z coordinate of the query point
  223. float time[16]; // time of the point quey
  224. float radius[16]; // radius of the point query
  225. };
  226. struct RTCPointQueryN;
  227. struct RTC_ALIGN(16) RTCPointQueryContext
  228. {
  229. // accumulated 4x4 column major matrices from world space to instance space.
  230. // undefined if size == 0.
  231. float world2inst[RTC_MAX_INSTANCE_LEVEL_COUNT][16];
  232. // accumulated 4x4 column major matrices from instance space to world space.
  233. // undefined if size == 0.
  234. float inst2world[RTC_MAX_INSTANCE_LEVEL_COUNT][16];
  235. // instance ids.
  236. unsigned int instID[RTC_MAX_INSTANCE_LEVEL_COUNT];
  237. // number of instances currently on the stack.
  238. unsigned int instStackSize;
  239. };
  240. /* Initializes an intersection context. */
  241. RTC_FORCEINLINE void rtcInitPointQueryContext(struct RTCPointQueryContext* context)
  242. {
  243. context->instStackSize = 0;
  244. context->instID[0] = RTC_INVALID_GEOMETRY_ID;
  245. }
  246. struct RTC_ALIGN(16) RTCPointQueryFunctionArguments
  247. {
  248. // The (world space) query object that was passed as an argument of rtcPointQuery. The
  249. // radius of the query can be decreased inside the callback to shrink the
  250. // search domain. Increasing the radius or modifying the time or position of
  251. // the query results in undefined behaviour.
  252. struct RTCPointQuery* query;
  253. // Used for user input/output data. Will not be read or modified internally.
  254. void* userPtr;
  255. // primitive and geometry ID of primitive
  256. unsigned int primID;
  257. unsigned int geomID;
  258. // the context with transformation and instance ID stack
  259. struct RTCPointQueryContext* context;
  260. // If the current instance transform M (= context->world2inst[context->instStackSize])
  261. // is a similarity matrix, i.e there is a constant factor similarityScale such that,
  262. // for all x,y: dist(Mx, My) = similarityScale * dist(x, y),
  263. // The similarity scale is 0, if the current instance transform is not a
  264. // similarity transform and vice versa. The similarity scale allows to compute
  265. // distance information in instance space and scale the distances into world
  266. // space by dividing with the similarity scale, for example, to update the
  267. // query radius. If the current instance transform is not a similarity
  268. // transform (similarityScale = 0), the distance computation has to be
  269. // performed in world space to ensure correctness. if there is no instance
  270. // transform (context->instStackSize == 0), the similarity scale is 1.
  271. float similarityScale;
  272. };
  273. typedef bool (*RTCPointQueryFunction)(struct RTCPointQueryFunctionArguments* args);
  274. RTC_NAMESPACE_END