Fixed memory read issues

Templated load function
This commit is contained in:
Xiao Qi 2023-02-25 19:18:35 +08:00
parent d94e81231c
commit bc8347a161
5 changed files with 36 additions and 48 deletions

View File

@ -16,7 +16,7 @@ typedef std::filesystem::path AssetPath;
enum class BUFFER_TARGET : int enum class BUFFER_TARGET : int
{ {
ARRAY_BUFFER = 3496, ARRAY_BUFFER = 34962,
ELEMENT_ARRAY_BUFFER = 34963 ELEMENT_ARRAY_BUFFER = 34963
}; };

View File

@ -48,6 +48,8 @@ namespace SH_COMP
static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept; static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept;
static inline void BuildHeaders(ModelRef asset) noexcept; static inline void BuildHeaders(ModelRef asset) noexcept;
template<typename T>
static void FetchData(int accessorID, std::vector<T>& dst);
public: public:
static void LoadAndCompile(AssetPath path) noexcept; static void LoadAndCompile(AssetPath path) noexcept;
}; };

View File

@ -24,6 +24,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <algorithm>
#include "tiny_gltf.h" #include "tiny_gltf.h"
#include <map> #include <map>
@ -82,54 +83,25 @@ namespace SH_COMP
try try
{ {
//meshIn.vertexPosition = FetchData<SHVec3>(primitive.attributes.at(ATT_POSITION.data())); FetchData(primitive.attributes.at(ATT_POSITION.data()), meshIn.vertexPosition);
//meshIn.vertexNormal = FetchData<SHVec3>(primitive.attributes.at(ATT_NORMAL.data())); FetchData(primitive.attributes.at(ATT_NORMAL.data()), meshIn.vertexNormal);
//meshIn.vertexTangent = FetchData<SHVec3>(primitive.attributes.at(ATT_TANGENT.data())); FetchData(primitive.attributes.at(ATT_TEXCOORD.data()), meshIn.texCoords);
//meshIn.texCoords = FetchData<SHVec2>(primitive.attributes.at(ATT_TEXCOORD.data()));
auto accessor = &(*accessors)[primitive.attributes.at(ATT_POSITION.data())]; std::vector<unsigned short> indices_ushort;
auto view = &(*bufferViews)[accessor->bufferView]; FetchData(primitive.indices, indices_ushort);
meshIn.vertexPosition.resize(accessor->count); meshIn.indices.resize(indices_ushort.size());
std::memcpy( std::ranges::copy(indices_ushort, meshIn.indices.begin());
meshIn.vertexPosition.data(),
buffer + view->byteOffset,
view->byteLength
);
accessor = &(*accessors)[primitive.indices]; std::vector<SHVec4> intermediate;
view = &(*bufferViews)[accessor->bufferView]; FetchData(primitive.attributes.at(ATT_TANGENT.data()), intermediate);
meshIn.indices.resize(accessor->count); meshIn.vertexTangent.resize(intermediate.size());
std::memcpy( std::ranges::transform(
meshIn.indices.data(), intermediate,
buffer + view->byteOffset, meshIn.vertexTangent.begin(),
view->byteLength [](auto const& inTan)
); {
return SHVec3{ inTan.x, inTan.y, inTan.z };
accessor = &(*accessors)[primitive.attributes.at(ATT_NORMAL.data())]; }
view = &(*bufferViews)[accessor->bufferView];
meshIn.vertexNormal.resize(accessor->count);
std::memcpy(
meshIn.vertexNormal.data(),
buffer + view->byteOffset,
view->byteLength
);
accessor = &(*accessors)[primitive.attributes.at(ATT_TEXCOORD.data())];
view = &(*bufferViews)[accessor->bufferView];
meshIn.texCoords.resize(accessor->count);
std::memcpy(
meshIn.texCoords.data(),
buffer + view->byteOffset,
view->byteLength
);
accessor = &(*accessors)[primitive.attributes.at(ATT_TANGENT.data())];
view = &(*bufferViews)[accessor->bufferView];
meshIn.vertexTangent.resize(accessor->count);
std::memcpy(
meshIn.vertexTangent.data(),
buffer + view->byteOffset,
view->byteLength
); );
} }
catch (std::out_of_range e) catch (std::out_of_range e)
@ -141,6 +113,20 @@ namespace SH_COMP
} }
template <typename T>
void MeshCompiler::FetchData(int accessorID, std::vector<T>& dst)
{
auto const& accessor = (*accessors)[accessorID];
auto const& view = (*bufferViews)[accessor.bufferView];
dst.resize(3484);
std::cout << "buffer line\n";
std::memcpy(
dst.data(),
buffer + view.byteOffset,
view.byteLength
);
}
inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept
{ {
// Mesh Headers // Mesh Headers

View File

@ -57,7 +57,7 @@ int main(int argc, char* argv[])
} }
#else #else
SH_COMP::MeshCompiler::LoadAndCompile("racoon.gltf"); SH_COMP::MeshCompiler::LoadAndCompile("racoon_tiny.gltf");
#endif #endif
return 0; return 0;