123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- /**
- * @file llgltfbufferutil.h
- * @brief LL GLTF Implementation
- *
- * $LicenseInfo:firstyear=2024&license=viewergpl$
- *
- * Copyright (c) 2024, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- // Inline template implementations for copying data out of GLTF buffers.
- // DO NOT include from header files to avoid the need to rebuild the whole
- // project whenever we add support for more types.
- // Also, make sure to include after the other llgltf*.h headers, to that the
- // proper math types used in the corresponding module are #include'd already.
- #ifndef LL_LLGLTFBUFFERUTIL_H
- #define LL_LLGLTFBUFFERUTIL_H
- #include "llstrider.h"
- // Suppress unused function warning: clang complains here but these
- // specializations are definitely used.
- #if LL_CLANG
- # pragma clang diagnostic push
- # pragma clang diagnostic ignored "-Wunused-function"
- #endif
- namespace LLGLTF
- {
- // Copy one Scalar from src to dst
- template<class S, class T>
- static void copyScalar(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- // Copy one vec2 from src to dst
- template<class S, class T>
- static void copyVec2(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- // Copy one vec3 from src to dst
- template<class S, class T>
- static void copyVec3(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- // Copy one vec4 from src to dst
- template<class S, class T>
- static void copyVec4(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- // Copy one mat2 from src to dst
- template<class S, class T>
- static void copyMat2(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- // Copy one mat3 from src to dst
- template<class S, class T>
- static void copyMat3(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- // Copy one mat4 from src to dst
- template<class S, class T>
- static void copyMat4(S* src, T& dst)
- {
- llerrs << "TODO: implement " << LL_FUNC << llendl;
- }
- //=========================================================================
- // Concrete implementations for different types of source and destination
- //=========================================================================
- template<>
- LL_INLINE void copyScalar<F32, F32>(F32* src, F32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<U32, U32>(U32* src, U32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<S32, S32>(S32* src, S32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<U32, U16>(U32* src, U16& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<S32, S16>(S32* src, S16& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<U16, U16>(U16* src, U16& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<S16, S16>(S16* src, S16& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<U16, U32>(U16* src, U32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<S16, S32>(S16* src, S32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<U8, U16>(U8* src, U16& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<S8, S16>(S8* src, S16& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<U8, U32>(U8* src, U32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyScalar<S8, S32>(S8* src, S32& dst)
- {
- dst = *src;
- }
- template<>
- LL_INLINE void copyVec2<F32, LLVector2>(F32* src, LLVector2& dst)
- {
- dst.set(src[0], src[1]);
- }
- template<>
- LL_INLINE void copyVec3<F32, LLVector3>(F32* src, LLVector3& dst)
- {
- dst.set(src[0], src[1], src[2]);
- }
- template<>
- LL_INLINE void copyVec3<F32, LLVector4a>(F32* src, LLVector4a& dst)
- {
- dst.load3(src);
- }
- template<>
- LL_INLINE void copyVec3<U16, LLColor4U>(U16* src, LLColor4U& dst)
- {
- dst.set(src[0], src[1], src[2], 255);
- }
- template<>
- LL_INLINE void copyVec4<U8, LLColor4U>(U8* src, LLColor4U& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<U16, LLColor4U>(U16* src, LLColor4U& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<F32, LLColor4U>(F32* src, LLColor4U& dst)
- {
- dst.set(src[0]*255, src[1]*255, src[2]*255, src[3]*255);
- }
- template<>
- LL_INLINE void copyVec4<F32, LLVector4a>(F32* src, LLVector4a& dst)
- {
- dst.loadua(src);
- }
- template<>
- LL_INLINE void copyVec4<U32, LLVector4a>(U32* src, LLVector4a& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<S32, LLVector4a>(S32* src, LLVector4a& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<U16, LLVector4a>(U16* src, LLVector4a& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<S16, LLVector4a>(S16* src, LLVector4a& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<U8, LLVector4a>(U8* src, LLVector4a& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<S8, LLVector4a>(S8* src, LLVector4a& dst)
- {
- dst.set(src[0], src[1], src[2], src[3]);
- }
- template<>
- LL_INLINE void copyVec4<F32, LLQuaternion>(F32* src, LLQuaternion& dst)
- {
- dst.setRaw(src);
- }
- template<>
- LL_INLINE void copyMat4<F32, LLMatrix4>(F32* src, LLMatrix4& dst)
- {
- for (U32 i = 0; i < 4; ++i)
- {
- for (U32 j = 0; j < 4; ++j)
- {
- dst.mMatrix[i][j] = *src++;
- }
- }
- }
- #if 1 // *TODO: get rid of glh (again) ! HB
- template<>
- LL_INLINE void copyVec3<F32, glh::vec3f>(F32* src, glh::vec3f& dst)
- {
- dst.set_value(src[0], src[1], src[2]);
- }
- template<>
- LL_INLINE void copyVec4<F32, glh::quaternionf>(F32* src, glh::quaternionf& dst)
- {
- dst.set_value(src);
- }
- template<>
- LL_INLINE void copyMat4<F32, glh::matrix4f>(F32* src, glh::matrix4f& dst)
- {
- dst.set_value(src);
- }
- #endif
- //=========================================================================
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyScalar(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyScalar(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyVec2(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyVec2(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyVec3(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyVec3(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyVec4(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyVec4(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyMat2(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyMat2(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyMat3(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyMat3(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- // Copy from src to dst, stride is the number of bytes between each element
- // in src, count is number of elements to copy.
- template<class S, class T>
- LL_INLINE static void copyMat4(S* src, LLStrider<T> dst, S32 stride,
- S32 count)
- {
- for (S32 i = 0; i < count; ++i)
- {
- copyMat4(src, *dst++);
- src = (S*)((U8*)src + stride);
- }
- }
- template<class S, class T>
- LL_INLINE static void copy(Asset& asset, Accessor& accessor, const S* src,
- LLStrider<T>& dst, S32 bstride)
- {
- if (accessor.mType == (S32)Accessor::Type::SCALAR)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 1 : bstride;
- copyScalar((S*)src, dst, stride, accessor.mCount);
- }
- else if (accessor.mType == (S32)Accessor::Type::VEC2)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 2 : bstride;
- copyVec2((S*)src, dst, stride, accessor.mCount);
- }
- else if (accessor.mType == (S32)Accessor::Type::VEC3)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 3 : bstride;
- copyVec3((S*)src, dst, stride, accessor.mCount);
- }
- else if (accessor.mType == (S32)Accessor::Type::VEC4)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 4 : bstride;
- copyVec4((S*)src, dst, stride, accessor.mCount);
- }
- else if (accessor.mType == (S32)Accessor::Type::MAT2)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 4 : bstride;
- copyMat2((S*)src, dst, stride, accessor.mCount);
- }
- else if (accessor.mType == (S32)Accessor::Type::MAT3)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 9 : bstride;
- copyMat3((S*)src, dst, stride, accessor.mCount);
- }
- else if (accessor.mType == (S32)Accessor::Type::MAT4)
- {
- S32 stride = bstride == 0 ? sizeof(S) * 16 : bstride;
- copyMat4((S*)src, dst, stride, accessor.mCount);
- }
- else
- {
- llerrs << "Unsupported accessor type" << llendl;
- }
- }
- // Copy data from accessor to strider
- template<class T>
- LL_INLINE static void copy(Asset& asset, Accessor& accessor, LLStrider<T>& dst)
- {
- const BufferView& buffviewp = asset.mBufferViews[accessor.mBufferView];
- const Buffer& buffer = asset.mBuffers[buffviewp.mBuffer];
- const U8* src = buffer.mData.data() + buffviewp.mByteOffset +
- accessor.mByteOffset;
- if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_FLOAT)
- {
- LLGLTF::copy(asset, accessor, (const F32*)src, dst, buffviewp.mByteStride);
- }
- else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT)
- {
- LLGLTF::copy(asset, accessor, (const U16*)src, dst, buffviewp.mByteStride);
- }
- else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT)
- {
- LLGLTF::copy(asset, accessor, (const U32*)src, dst, buffviewp.mByteStride);
- }
- else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE)
- {
- LLGLTF::copy(asset, accessor, (const U8*)src, dst, buffviewp.mByteStride);
- }
- else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_SHORT)
- {
- LLGLTF::copy(asset, accessor, (const S16*)src, dst, buffviewp.mByteStride);
- }
- else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_BYTE)
- {
- LLGLTF::copy(asset, accessor, (const S8*)src, dst, buffviewp.mByteStride);
- }
- else if (accessor.mComponentType == TINYGLTF_COMPONENT_TYPE_DOUBLE)
- {
- LLGLTF::copy(asset, accessor, (const F64*)src, dst, buffviewp.mByteStride);
- }
- else
- {
- llerrs << "Unsupported component type" << llendl;
- }
- }
- // Copy data from accessor to vector
- template<class T>
- LL_INLINE static void copy(Asset& asset, Accessor& accessor, std::vector<T>& dst)
- {
- dst.resize(accessor.mCount);
- LLStrider<T> strider = dst.data();
- copy(asset, accessor, strider);
- }
- }
- #if LL_CLANG
- # pragma clang diagnostic pop
- #endif
- #endif // LL_LLGLTFBUFFERUTIL_H
|