Working commit, weird memory allocate/deallocate bug

This commit is contained in:
Xiao Qi 2023-02-22 14:48:19 +08:00
parent 5cd0b272c9
commit 966ce21686
3 changed files with 100 additions and 38 deletions

View File

@ -71,13 +71,16 @@ constexpr std::string_view FBX_EXTENSION{ ".fbx" };
constexpr std::string_view GLTF_EXTENSION{ ".gltf" }; constexpr std::string_view GLTF_EXTENSION{ ".gltf" };
// ATTRIBUTE NAMES // ATTRIBUTE NAMES
// BASIC NEEDED
constexpr std::string_view ATT_POSITION {"POSITION"}; constexpr std::string_view ATT_POSITION {"POSITION"};
constexpr std::string_view ATT_NORMAL { "NORMAL" }; constexpr std::string_view ATT_NORMAL { "NORMAL" };
constexpr std::string_view ATT_TANGENT { "TANGENT" }; constexpr std::string_view ATT_TANGENT { "TANGENT" };
constexpr std::string_view ATT_JOINT { "JOINTS_0" };
constexpr std::string_view ATT_COLOUR { "COLOR_0" };
constexpr std::string_view ATT_TEXCOORD { "TEXCOORD_0" }; constexpr std::string_view ATT_TEXCOORD { "TEXCOORD_0" };
// VARIABLE ATTRIBUTES
constexpr std::string_view ATT_WEIGHTS { "WEIGHTS_0" }; constexpr std::string_view ATT_WEIGHTS { "WEIGHTS_0" };
constexpr std::string_view ATT_JOINT{ "JOINTS_0" };
constexpr std::string_view ATT_COLOUR{ "COLOR_0" };
constexpr std::string_view EXTERNALS[] = { constexpr std::string_view EXTERNALS[] = {
FBX_EXTENSION, FBX_EXTENSION,

View File

@ -12,6 +12,15 @@
*****************************************************************************/ *****************************************************************************/
#define TINYGLTF_IMPLEMENTATION
#define TINYGLTF_NO_EXTERNAL_IMAGE
#define TINYGLTF_USE_CPP14
#define TINYGLTF_NO_INCLUDE_STB_IMAGE
#define TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE
#define TINYGLTF_NO_STB_IMAGE_WRITE
#define TINYGLTF_NO_STB_IMAGE
#define TINYGLTF_USE_CPP14
#include "MeshCompiler.h" #include "MeshCompiler.h"
#include "MeshWriter.h" #include "MeshWriter.h"
@ -24,8 +33,9 @@
namespace SH_COMP namespace SH_COMP
{ {
AccessorReference MeshCompiler::accessors{ nullptr };
uint32_t MeshCompiler::rigNodeIDCounter { 0 }; BufferViewReference MeshCompiler::bufferViews{ nullptr };
BufferData MeshCompiler::buffer{ nullptr };
void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept
{ {
@ -52,9 +62,9 @@ namespace SH_COMP
void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept
{ {
auto const& accessors { data.accessors }; accessors = &data.accessors;
auto const& bufferViews { data.bufferViews }; bufferViews = &data.bufferViews;
auto const& bufferData { data.buffers[0].data.data() }; buffer = data.buffers[0].data.data();
for (auto const& mesh : data.meshes) for (auto const& mesh : data.meshes)
{ {
@ -64,33 +74,86 @@ namespace SH_COMP
try try
{ {
// Get Accessors //meshIn.vertexPosition = FetchData<SHVec3>(primitive.attributes.at(ATT_POSITION.data()));
auto const& positionAccessor { accessors[primitive.attributes.at(ATT_POSITION.data())]}; //meshIn.vertexNormal = FetchData<SHVec3>(primitive.attributes.at(ATT_NORMAL.data()));
auto const& normalAccessor { accessors[primitive.attributes.at(ATT_NORMAL.data())]}; //meshIn.vertexTangent = FetchData<SHVec3>(primitive.attributes.at(ATT_TANGENT.data()));
auto const& tangentAccessor { accessors[primitive.attributes.at(ATT_TANGENT.data())]}; //meshIn.texCoords = FetchData<SHVec2>(primitive.attributes.at(ATT_TEXCOORD.data()));
meshIn.vertexPosition.resize(positionAccessor.count); auto accessor = &(*accessors)[primitive.attributes.at(ATT_POSITION.data())];
auto const& positionView { bufferViews[positionAccessor.bufferView] }; auto view = &(*bufferViews)[accessor->bufferView];
meshIn.vertexPosition.resize(accessor->count);
std::memcpy( std::memcpy(
meshIn.vertexPosition.data(), meshIn.vertexPosition.data(),
bufferData + positionView.byteOffset, buffer + view->byteOffset,
positionView.byteLength view->byteLength
); );
meshIn.vertexNormal.resize(normalAccessor.count); accessor = &(*accessors)[primitive.indices];
view = &(*bufferViews)[accessor->bufferView];
meshIn.indices.resize(accessor->count);
std::memcpy(
meshIn.indices.data(),
buffer + view->byteOffset,
view->byteLength
);
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)
{ {
std::cout << "[Model Compiler] Failed to load gltf\n"; std::cout << "[Model Compiler] Failed to load critical data from gltf\n";
} }
} }
} }
inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept
{
// Mesh Headers
asset.meshHeaders.resize(asset.meshes.size());
asset.header.meshCount = asset.meshes.size();
for (auto i{ 0 }; i < asset.header.meshCount; ++i)
{
auto const& mesh = asset.meshes[i];
auto& head = asset.meshHeaders[i];
head.charCount = mesh.name.size();
head.indexCount = mesh.indices.size();
head.vertexCount = mesh.vertexPosition.size();
head.boneCount = mesh.bonesInfo.size();
}
}
void MeshCompiler::LoadAndCompile(AssetPath path) noexcept void MeshCompiler::LoadAndCompile(AssetPath path) noexcept
{ {
auto const asset = new ModelAsset(); auto const asset = new ModelAsset();
LoadFromFile(path, *asset); LoadFromFile(path, *asset);
BuildHeaders(*asset);
MeshWriter::CompileMeshBinary(path, *asset); MeshWriter::CompileMeshBinary(path, *asset);
delete asset; delete asset;

View File

@ -12,15 +12,6 @@
*****************************************************************************/ *****************************************************************************/
#pragma once #pragma once
#define TINYGLTF_IMPLEMENTATION
#define TINYGLTF_NO_EXTERNAL_IMAGE
#define TINYGLTF_USE_CPP14
#define TINYGLTF_NO_INCLUDE_STB_IMAGE
#define TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE
#define TINYGLTF_NO_STB_IMAGE_WRITE
#define TINYGLTF_NO_STB_IMAGE
#define TINYGLTF_USE_CPP14
#include <vector> #include <vector>
#include "Types/AnimationAsset.h" #include "Types/AnimationAsset.h"
@ -30,27 +21,32 @@
//Forward Declare //Forward Declare
namespace tinygltf namespace tinygltf
{ {
struct Accessor;
struct BufferView;
class Model; class Model;
} }
namespace SH_COMP namespace SH_COMP
{ {
class tinygltf::Model; using MeshVectorRef = std::vector<MeshData>&;
using AnimVectorRef = std::vector<AnimData>&;
using ModelRef = ModelAsset&;
using ModelData = tinygltf::Model;
using AccessorReference = std::vector<tinygltf::Accessor> const*;
using BufferViewReference = std::vector<tinygltf::BufferView> const*;
using BufferData = unsigned char const*;
class MeshCompiler class MeshCompiler
{ {
using MeshVectorRef = std::vector<MeshData>&; static AccessorReference accessors;
using AnimVectorRef = std::vector<AnimData>&; static BufferViewReference bufferViews;
static BufferData buffer;
using ModelRef = ModelAsset&;
using ModelData = tinygltf::Model;
static uint32_t rigNodeIDCounter;
static void LoadFromFile(AssetPath path, ModelRef asset) noexcept; static void LoadFromFile(AssetPath path, ModelRef asset) noexcept;
static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept;
static inline void BuildHeaders(ModelRef asset) noexcept;
static void ProcessModel(ModelData const&, ModelRef asset) noexcept;
public: public:
static void LoadAndCompile(AssetPath path) noexcept; static void LoadAndCompile(AssetPath path) noexcept;