llgltfbufferutil.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**
  2. * @file llgltfbufferutil.h
  3. * @brief LL GLTF Implementation
  4. *
  5. * $LicenseInfo:firstyear=2024&license=viewergpl$
  6. *
  7. * Copyright (c) 2024, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. // Inline template implementations for copying data out of GLTF buffers.
  33. // DO NOT include from header files to avoid the need to rebuild the whole
  34. // project whenever we add support for more types.
  35. // Also, make sure to include after the other llgltf*.h headers, to that the
  36. // proper math types used in the corresponding module are #include'd already.
  37. #ifndef LL_LLGLTFBUFFERUTIL_H
  38. #define LL_LLGLTFBUFFERUTIL_H
  39. #include "llstrider.h"
  40. // Suppress unused function warning: clang complains here but these
  41. // specializations are definitely used.
  42. #if LL_CLANG
  43. # pragma clang diagnostic push
  44. # pragma clang diagnostic ignored "-Wunused-function"
  45. #endif
  46. namespace LLGLTF
  47. {
  48. // Copy one Scalar from src to dst
  49. template<class S, class T>
  50. static void copyScalar(S* src, T& dst)
  51. {
  52. llerrs << "TODO: implement " << LL_FUNC << llendl;
  53. }
  54. // Copy one vec2 from src to dst
  55. template<class S, class T>
  56. static void copyVec2(S* src, T& dst)
  57. {
  58. llerrs << "TODO: implement " << LL_FUNC << llendl;
  59. }
  60. // Copy one vec3 from src to dst
  61. template<class S, class T>
  62. static void copyVec3(S* src, T& dst)
  63. {
  64. llerrs << "TODO: implement " << LL_FUNC << llendl;
  65. }
  66. // Copy one vec4 from src to dst
  67. template<class S, class T>
  68. static void copyVec4(S* src, T& dst)
  69. {
  70. llerrs << "TODO: implement " << LL_FUNC << llendl;
  71. }
  72. // Copy one mat2 from src to dst
  73. template<class S, class T>
  74. static void copyMat2(S* src, T& dst)
  75. {
  76. llerrs << "TODO: implement " << LL_FUNC << llendl;
  77. }
  78. // Copy one mat3 from src to dst
  79. template<class S, class T>
  80. static void copyMat3(S* src, T& dst)
  81. {
  82. llerrs << "TODO: implement " << LL_FUNC << llendl;
  83. }
  84. // Copy one mat4 from src to dst
  85. template<class S, class T>
  86. static void copyMat4(S* src, T& dst)
  87. {
  88. llerrs << "TODO: implement " << LL_FUNC << llendl;
  89. }
  90. //=========================================================================
  91. // Concrete implementations for different types of source and destination
  92. //=========================================================================
  93. template<>
  94. LL_INLINE void copyScalar<F32, F32>(F32* src, F32& dst)
  95. {
  96. dst = *src;
  97. }
  98. template<>
  99. LL_INLINE void copyScalar<U32, U32>(U32* src, U32& dst)
  100. {
  101. dst = *src;
  102. }
  103. template<>
  104. LL_INLINE void copyScalar<S32, S32>(S32* src, S32& dst)
  105. {
  106. dst = *src;
  107. }
  108. template<>
  109. LL_INLINE void copyScalar<U32, U16>(U32* src, U16& dst)
  110. {
  111. dst = *src;
  112. }
  113. template<>
  114. LL_INLINE void copyScalar<S32, S16>(S32* src, S16& dst)
  115. {
  116. dst = *src;
  117. }
  118. template<>
  119. LL_INLINE void copyScalar<U16, U16>(U16* src, U16& dst)
  120. {
  121. dst = *src;
  122. }
  123. template<>
  124. LL_INLINE void copyScalar<S16, S16>(S16* src, S16& dst)
  125. {
  126. dst = *src;
  127. }
  128. template<>
  129. LL_INLINE void copyScalar<U16, U32>(U16* src, U32& dst)
  130. {
  131. dst = *src;
  132. }
  133. template<>
  134. LL_INLINE void copyScalar<S16, S32>(S16* src, S32& dst)
  135. {
  136. dst = *src;
  137. }
  138. template<>
  139. LL_INLINE void copyScalar<U8, U16>(U8* src, U16& dst)
  140. {
  141. dst = *src;
  142. }
  143. template<>
  144. LL_INLINE void copyScalar<S8, S16>(S8* src, S16& dst)
  145. {
  146. dst = *src;
  147. }
  148. template<>
  149. LL_INLINE void copyScalar<U8, U32>(U8* src, U32& dst)
  150. {
  151. dst = *src;
  152. }
  153. template<>
  154. LL_INLINE void copyScalar<S8, S32>(S8* src, S32& dst)
  155. {
  156. dst = *src;
  157. }
  158. template<>
  159. LL_INLINE void copyVec2<F32, LLVector2>(F32* src, LLVector2& dst)
  160. {
  161. dst.set(src[0], src[1]);
  162. }
  163. template<>
  164. LL_INLINE void copyVec3<F32, LLVector3>(F32* src, LLVector3& dst)
  165. {
  166. dst.set(src[0], src[1], src[2]);
  167. }
  168. template<>
  169. LL_INLINE void copyVec3<F32, LLVector4a>(F32* src, LLVector4a& dst)
  170. {
  171. dst.load3(src);
  172. }
  173. template<>
  174. LL_INLINE void copyVec3<U16, LLColor4U>(U16* src, LLColor4U& dst)
  175. {
  176. dst.set(src[0], src[1], src[2], 255);
  177. }
  178. template<>
  179. LL_INLINE void copyVec4<U8, LLColor4U>(U8* src, LLColor4U& dst)
  180. {
  181. dst.set(src[0], src[1], src[2], src[3]);
  182. }
  183. template<>
  184. LL_INLINE void copyVec4<U16, LLColor4U>(U16* src, LLColor4U& dst)
  185. {
  186. dst.set(src[0], src[1], src[2], src[3]);
  187. }
  188. template<>
  189. LL_INLINE void copyVec4<F32, LLColor4U>(F32* src, LLColor4U& dst)
  190. {
  191. dst.set(src[0]*255, src[1]*255, src[2]*255, src[3]*255);
  192. }
  193. template<>
  194. LL_INLINE void copyVec4<F32, LLVector4a>(F32* src, LLVector4a& dst)
  195. {
  196. dst.loadua(src);
  197. }
  198. template<>
  199. LL_INLINE void copyVec4<U32, LLVector4a>(U32* src, LLVector4a& dst)
  200. {
  201. dst.set(src[0], src[1], src[2], src[3]);
  202. }
  203. template<>
  204. LL_INLINE void copyVec4<S32, LLVector4a>(S32* src, LLVector4a& dst)
  205. {
  206. dst.set(src[0], src[1], src[2], src[3]);
  207. }
  208. template<>
  209. LL_INLINE void copyVec4<U16, LLVector4a>(U16* src, LLVector4a& dst)
  210. {
  211. dst.set(src[0], src[1], src[2], src[3]);
  212. }
  213. template<>
  214. LL_INLINE void copyVec4<S16, LLVector4a>(S16* src, LLVector4a& dst)
  215. {
  216. dst.set(src[0], src[1], src[2], src[3]);
  217. }
  218. template<>
  219. LL_INLINE void copyVec4<U8, LLVector4a>(U8* src, LLVector4a& dst)
  220. {
  221. dst.set(src[0], src[1], src[2], src[3]);
  222. }
  223. template<>
  224. LL_INLINE void copyVec4<S8, LLVector4a>(S8* src, LLVector4a& dst)
  225. {
  226. dst.set(src[0], src[1], src[2], src[3]);
  227. }
  228. template<>
  229. LL_INLINE void copyVec4<F32, LLQuaternion>(F32* src, LLQuaternion& dst)
  230. {
  231. dst.setRaw(src);
  232. }
  233. template<>
  234. LL_INLINE void copyMat4<F32, LLMatrix4>(F32* src, LLMatrix4& dst)
  235. {
  236. for (U32 i = 0; i < 4; ++i)
  237. {
  238. for (U32 j = 0; j < 4; ++j)
  239. {
  240. dst.mMatrix[i][j] = *src++;
  241. }
  242. }
  243. }
  244. #if 1 // *TODO: get rid of glh (again) ! HB
  245. template<>
  246. LL_INLINE void copyVec3<F32, glh::vec3f>(F32* src, glh::vec3f& dst)
  247. {
  248. dst.set_value(src[0], src[1], src[2]);
  249. }
  250. template<>
  251. LL_INLINE void copyVec4<F32, glh::quaternionf>(F32* src, glh::quaternionf& dst)
  252. {
  253. dst.set_value(src);
  254. }
  255. template<>
  256. LL_INLINE void copyMat4<F32, glh::matrix4f>(F32* src, glh::matrix4f& dst)
  257. {
  258. dst.set_value(src);
  259. }
  260. #endif
  261. //=========================================================================
  262. // Copy from src to dst, stride is the number of bytes between each element
  263. // in src, count is number of elements to copy.
  264. template<class S, class T>
  265. LL_INLINE static void copyScalar(S* src, LLStrider<T> dst, S32 stride,
  266. S32 count)
  267. {
  268. for (S32 i = 0; i < count; ++i)
  269. {
  270. copyScalar(src, *dst++);
  271. src = (S*)((U8*)src + stride);
  272. }
  273. }
  274. // Copy from src to dst, stride is the number of bytes between each element
  275. // in src, count is number of elements to copy.
  276. template<class S, class T>
  277. LL_INLINE static void copyVec2(S* src, LLStrider<T> dst, S32 stride,
  278. S32 count)
  279. {
  280. for (S32 i = 0; i < count; ++i)
  281. {
  282. copyVec2(src, *dst++);
  283. src = (S*)((U8*)src + stride);
  284. }
  285. }
  286. // Copy from src to dst, stride is the number of bytes between each element
  287. // in src, count is number of elements to copy.
  288. template<class S, class T>
  289. LL_INLINE static void copyVec3(S* src, LLStrider<T> dst, S32 stride,
  290. S32 count)
  291. {
  292. for (S32 i = 0; i < count; ++i)
  293. {
  294. copyVec3(src, *dst++);
  295. src = (S*)((U8*)src + stride);
  296. }
  297. }
  298. // Copy from src to dst, stride is the number of bytes between each element
  299. // in src, count is number of elements to copy.
  300. template<class S, class T>
  301. LL_INLINE static void copyVec4(S* src, LLStrider<T> dst, S32 stride,
  302. S32 count)
  303. {
  304. for (S32 i = 0; i < count; ++i)
  305. {
  306. copyVec4(src, *dst++);
  307. src = (S*)((U8*)src + stride);
  308. }
  309. }
  310. // Copy from src to dst, stride is the number of bytes between each element
  311. // in src, count is number of elements to copy.
  312. template<class S, class T>
  313. LL_INLINE static void copyMat2(S* src, LLStrider<T> dst, S32 stride,
  314. S32 count)
  315. {
  316. for (S32 i = 0; i < count; ++i)
  317. {
  318. copyMat2(src, *dst++);
  319. src = (S*)((U8*)src + stride);
  320. }
  321. }
  322. // Copy from src to dst, stride is the number of bytes between each element
  323. // in src, count is number of elements to copy.
  324. template<class S, class T>
  325. LL_INLINE static void copyMat3(S* src, LLStrider<T> dst, S32 stride,
  326. S32 count)
  327. {
  328. for (S32 i = 0; i < count; ++i)
  329. {
  330. copyMat3(src, *dst++);
  331. src = (S*)((U8*)src + stride);
  332. }
  333. }
  334. // Copy from src to dst, stride is the number of bytes between each element
  335. // in src, count is number of elements to copy.
  336. template<class S, class T>
  337. LL_INLINE static void copyMat4(S* src, LLStrider<T> dst, S32 stride,
  338. S32 count)
  339. {
  340. for (S32 i = 0; i < count; ++i)
  341. {
  342. copyMat4(src, *dst++);
  343. src = (S*)((U8*)src + stride);
  344. }
  345. }
  346. template<class S, class T>
  347. LL_INLINE static void copy(Asset& asset, Accessor& accessor, const S* src,
  348. LLStrider<T>& dst, S32 bstride)
  349. {
  350. if (accessor.mType == (S32)Accessor::Type::SCALAR)
  351. {
  352. S32 stride = bstride == 0 ? sizeof(S) * 1 : bstride;
  353. copyScalar((S*)src, dst, stride, accessor.mCount);
  354. }
  355. else if (accessor.mType == (S32)Accessor::Type::VEC2)
  356. {
  357. S32 stride = bstride == 0 ? sizeof(S) * 2 : bstride;
  358. copyVec2((S*)src, dst, stride, accessor.mCount);
  359. }
  360. else if (accessor.mType == (S32)Accessor::Type::VEC3)
  361. {
  362. S32 stride = bstride == 0 ? sizeof(S) * 3 : bstride;
  363. copyVec3((S*)src, dst, stride, accessor.mCount);
  364. }
  365. else if (accessor.mType == (S32)Accessor::Type::VEC4)
  366. {
  367. S32 stride = bstride == 0 ? sizeof(S) * 4 : bstride;
  368. copyVec4((S*)src, dst, stride, accessor.mCount);
  369. }
  370. else if (accessor.mType == (S32)Accessor::Type::MAT2)
  371. {
  372. S32 stride = bstride == 0 ? sizeof(S) * 4 : bstride;
  373. copyMat2((S*)src, dst, stride, accessor.mCount);
  374. }
  375. else if (accessor.mType == (S32)Accessor::Type::MAT3)
  376. {
  377. S32 stride = bstride == 0 ? sizeof(S) * 9 : bstride;
  378. copyMat3((S*)src, dst, stride, accessor.mCount);
  379. }
  380. else if (accessor.mType == (S32)Accessor::Type::MAT4)
  381. {
  382. S32 stride = bstride == 0 ? sizeof(S) * 16 : bstride;
  383. copyMat4((S*)src, dst, stride, accessor.mCount);
  384. }
  385. else
  386. {
  387. llerrs << "Unsupported accessor type" << llendl;
  388. }
  389. }
  390. // Copy data from accessor to strider
  391. template<class T>
  392. LL_INLINE static void copy(Asset& asset, Accessor& accessor, LLStrider<T>& dst)
  393. {
  394. const BufferView& buffviewp = asset.mBufferViews[accessor.mBufferView];
  395. const Buffer& buffer = asset.mBuffers[buffviewp.mBuffer];
  396. const U8* src = buffer.mData.data() + buffviewp.mByteOffset +
  397. accessor.mByteOffset;
  398. if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_FLOAT)
  399. {
  400. LLGLTF::copy(asset, accessor, (const F32*)src, dst, buffviewp.mByteStride);
  401. }
  402. else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT)
  403. {
  404. LLGLTF::copy(asset, accessor, (const U16*)src, dst, buffviewp.mByteStride);
  405. }
  406. else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT)
  407. {
  408. LLGLTF::copy(asset, accessor, (const U32*)src, dst, buffviewp.mByteStride);
  409. }
  410. else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)
  411. {
  412. LLGLTF::copy(asset, accessor, (const U8*)src, dst, buffviewp.mByteStride);
  413. }
  414. else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_SHORT)
  415. {
  416. LLGLTF::copy(asset, accessor, (const S16*)src, dst, buffviewp.mByteStride);
  417. }
  418. else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_BYTE)
  419. {
  420. LLGLTF::copy(asset, accessor, (const S8*)src, dst, buffviewp.mByteStride);
  421. }
  422. else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_DOUBLE)
  423. {
  424. LLGLTF::copy(asset, accessor, (const F64*)src, dst, buffviewp.mByteStride);
  425. }
  426. else
  427. {
  428. llerrs << "Unsupported component type" << llendl;
  429. }
  430. }
  431. // Copy data from accessor to vector
  432. template<class T>
  433. LL_INLINE static void copy(Asset& asset, Accessor& accessor, std::vector<T>& dst)
  434. {
  435. dst.resize(accessor.mCount);
  436. LLStrider<T> strider = dst.data();
  437. copy(asset, accessor, strider);
  438. }
  439. }
  440. #if LL_CLANG
  441. # pragma clang diagnostic pop
  442. #endif
  443. #endif // LL_LLGLTFBUFFERUTIL_H