Fixing mesh import

This commit is contained in:
Dynamitos
2024-04-23 23:21:30 +02:00
parent 72336aa64f
commit 6b91568423
13 changed files with 103 additions and 18 deletions
+2
View File
@@ -50,6 +50,7 @@ find_package(Ktx CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED) find_package(fmt CONFIG REQUIRED)
find_package(VulkanMemoryAllocator CONFIG REQUIRED) find_package(VulkanMemoryAllocator CONFIG REQUIRED)
find_package(OpenMP REQUIRED)
if(UNIX) if(UNIX)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
@@ -75,6 +76,7 @@ target_link_libraries(Engine PUBLIC crcpp)
target_link_libraries(Engine PUBLIC fmt::fmt) target_link_libraries(Engine PUBLIC fmt::fmt)
target_link_libraries(Engine PUBLIC GPUOpen::VulkanMemoryAllocator) target_link_libraries(Engine PUBLIC GPUOpen::VulkanMemoryAllocator)
target_link_libraries(Engine PUBLIC slang) target_link_libraries(Engine PUBLIC slang)
target_link_libraries(Engine PUBLIC OpenMP::OpenMP_CXX)
if(APPLE) if(APPLE)
target_link_libraries(Engine PUBLIC metal) target_link_libraries(Engine PUBLIC metal)
SET(CMAKE_OSX_DEPLOYMENT_TARGET 14.3) SET(CMAKE_OSX_DEPLOYMENT_TARGET 14.3)
+1
View File
@@ -41,5 +41,6 @@ struct Scene
StructuredBuffer<uint8_t> primitiveIndices; StructuredBuffer<uint8_t> primitiveIndices;
StructuredBuffer<uint32_t> vertexIndices; StructuredBuffer<uint32_t> vertexIndices;
}; };
layout(set=1)
ParameterBlock<Scene> pScene; ParameterBlock<Scene> pScene;
+2 -1
View File
@@ -11,4 +11,5 @@ interface IVertexData
VertexAttributes getAttributes(uint index); VertexAttributes getAttributes(uint index);
}; };
uniform ParameterBlock<IVertexData> pVertexData; layout(set=2)
ParameterBlock<IVertexData> pVertexData;
+30 -5
View File
@@ -243,8 +243,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
Array<Vector> colors(mesh->mNumVertices); Array<Vector> colors(mesh->mNumVertices);
StaticMeshVertexData* vertexData = StaticMeshVertexData::getInstance(); StaticMeshVertexData* vertexData = StaticMeshVertexData::getInstance();
#pragma omp parallel for
for (uint32 i = 0; i < mesh->mNumVertices; ++i) for (int32 i = 0; i < mesh->mNumVertices; ++i)
{ {
positions[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z); positions[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
if (mesh->HasTextureCoords(0)) if (mesh->HasTextureCoords(0))
@@ -275,7 +275,6 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
colors[i] = Vector(1, 1, 1); colors[i] = Vector(1, 1, 1);
} }
} }
MeshId id = vertexData->allocateVertexData(mesh->mNumVertices); MeshId id = vertexData->allocateVertexData(mesh->mNumVertices);
vertexData->loadPositions(id, positions); vertexData->loadPositions(id, positions);
vertexData->loadTexCoords(id, texCoords); vertexData->loadTexCoords(id, texCoords);
@@ -285,7 +284,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
vertexData->loadColors(id, colors); vertexData->loadColors(id, colors);
Array<uint32> indices(mesh->mNumFaces * 3); Array<uint32> indices(mesh->mNumFaces * 3);
for (size_t faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) #pragma omp parallel for
for (int32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex)
{ {
indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0]; indices[faceIndex * 3 + 0] = mesh->mFaces[faceIndex].mIndices[0];
indices[faceIndex * 3 + 1] = mesh->mFaces[faceIndex].mIndices[1]; indices[faceIndex * 3 + 1] = mesh->mFaces[faceIndex].mIndices[1];
@@ -349,6 +349,27 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path&
}); });
} }
} }
Matrix4 convertMatrix(aiMatrix4x4 matrix)
{
return Matrix4(
matrix.a1, matrix.a2, matrix.a3, matrix.a4,
matrix.b1, matrix.b2, matrix.b3, matrix.b4,
matrix.c1, matrix.c2, matrix.c3, matrix.c4,
matrix.d1, matrix.d2, matrix.d3, matrix.d4
);
}
Matrix4 MeshLoader::loadNodeTransform(aiNode* node)
{
Matrix4 parent = Matrix4(1);
if (node->mParent != nullptr)
{
parent = loadNodeTransform(node->mParent);
}
return parent * convertMatrix(node->mTransformation);
}
void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset) void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset)
{ {
std::cout << "Starting to import "<<args.filePath<< std::endl; std::cout << "Starting to import "<<args.filePath<< std::endl;
@@ -382,13 +403,17 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset)
{ {
for(uint32 i = 0; i < meshNode->mNumMeshes; ++i) for(uint32 i = 0; i < meshNode->mNumMeshes; ++i)
{ {
if (globalMeshes[meshNode->mMeshes[i]] == nullptr)
{
continue;
}
meshes.add(std::move(globalMeshes[meshNode->mMeshes[i]])); meshes.add(std::move(globalMeshes[meshNode->mMeshes[i]]));
meshes.back()->transform = loadNodeTransform(meshNode);
} }
} }
meshAsset->meshes = std::move(meshes); meshAsset->meshes = std::move(meshes);
meshAsset->physicsMesh = std::move(collider); meshAsset->physicsMesh = std::move(collider);
auto stream = AssetRegistry::createWriteStream((std::filesystem::path(meshAsset->getFolderPath()) / meshAsset->getName()).replace_extension("asset").string(), std::ios::binary); auto stream = AssetRegistry::createWriteStream((std::filesystem::path(meshAsset->getFolderPath()) / meshAsset->getName()).replace_extension("asset").string(), std::ios::binary);
ArchiveBuffer archive; ArchiveBuffer archive;
+2
View File
@@ -6,6 +6,7 @@
struct aiScene; struct aiScene;
struct aiTexel; struct aiTexel;
struct aiNode;
namespace Seele namespace Seele
{ {
DECLARE_REF(Mesh) DECLARE_REF(Mesh)
@@ -29,6 +30,7 @@ private:
void loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes, Component::Collider& collider); void loadGlobalMeshes(const aiScene* scene, const Array<PMaterialInstanceAsset>& materials, Array<OMesh>& globalMeshes, Component::Collider& collider);
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels); void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels);
Matrix4 loadNodeTransform(aiNode* node);
void import(MeshImportArgs args, PMeshAsset meshAsset); void import(MeshImportArgs args, PMeshAsset meshAsset);
Gfx::PGraphics graphics; Gfx::PGraphics graphics;
}; };
+4 -4
View File
@@ -57,10 +57,10 @@ int main() {
AssetImporter::importMesh(MeshImportArgs{ AssetImporter::importMesh(MeshImportArgs{
.filePath = sourcePath / "import/models/cube.fbx", .filePath = sourcePath / "import/models/cube.fbx",
}); });
//AssetImporter::importMesh(MeshImportArgs{ AssetImporter::importMesh(MeshImportArgs{
// .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.blend", .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.blend",
// .importPath = "Whitechapel" .importPath = "Whitechapel"
// }); });
WindowCreateInfo mainWindowInfo; WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine"; mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1920; mainWindowInfo.width = 1920;
+2
View File
@@ -14,6 +14,7 @@ Mesh::~Mesh()
void Mesh::save(ArchiveBuffer& buffer) const void Mesh::save(ArchiveBuffer& buffer) const
{ {
Serialization::save(buffer, transform);
Serialization::save(buffer, vertexData->getTypeName()); Serialization::save(buffer, vertexData->getTypeName());
Serialization::save(buffer, vertexCount); Serialization::save(buffer, vertexCount);
Serialization::save(buffer, indices); Serialization::save(buffer, indices);
@@ -25,6 +26,7 @@ void Mesh::save(ArchiveBuffer& buffer) const
void Mesh::load(ArchiveBuffer& buffer) void Mesh::load(ArchiveBuffer& buffer)
{ {
std::string typeName; std::string typeName;
Serialization::load(buffer, transform);
Serialization::load(buffer, typeName); Serialization::load(buffer, typeName);
Serialization::load(buffer, vertexCount); Serialization::load(buffer, vertexCount);
vertexData = VertexData::findByTypeName(typeName); vertexData = VertexData::findByTypeName(typeName);
+2
View File
@@ -11,6 +11,8 @@ public:
Mesh(); Mesh();
~Mesh(); ~Mesh();
// transform from importing
Matrix4 transform = Matrix4(1);
VertexData* vertexData; VertexData* vertexData;
MeshId id; MeshId id;
uint64 vertexCount; uint64 vertexCount;
+1
View File
@@ -150,5 +150,6 @@ void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipeline
createInfo.entryPoint = "fragmentMain"; createInfo.entryPoint = "fragmentMain";
collection.fragmentShader = graphics->createFragmentShader(createInfo); collection.fragmentShader = graphics->createFragmentShader(createInfo);
} }
collection.pipelineLayout->create();
shaders[perm] = std::move(collection); shaders[perm] = std::move(collection);
} }
+1 -1
View File
@@ -40,7 +40,7 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform)
{ {
matInstanceData.meshes.add(MeshInstanceData{ matInstanceData.meshes.add(MeshInstanceData{
.instance = InstanceData { .instance = InstanceData {
.transformMatrix = transform.toMatrix(), .transformMatrix = mesh->transform * transform.toMatrix(),
}, },
.data = data, .data = data,
}); });
+6 -2
View File
@@ -324,11 +324,15 @@ PipelineLayout::~PipelineLayout() {}
Map<uint32, VkPipelineLayout> cachedLayouts; Map<uint32, VkPipelineLayout> cachedLayouts;
void PipelineLayout::create() { void PipelineLayout::create() {
vulkanDescriptorLayouts.resize(descriptorSetLayouts.size());
for (auto [name, desc] : descriptorSetLayouts) { for (auto [name, desc] : descriptorSetLayouts) {
PDescriptorLayout layout = desc.cast<DescriptorLayout>(); PDescriptorLayout layout = desc.cast<DescriptorLayout>();
layout->create(); layout->create();
vulkanDescriptorLayouts[parameterMapping[layout->getName()]] = layout->getHandle(); uint32 parameterIndex = parameterMapping[layout->getName()];
if (parameterIndex > vulkanDescriptorLayouts.size())
{
vulkanDescriptorLayouts.resize(parameterIndex + 1);
}
vulkanDescriptorLayouts[parameterIndex] = layout->getHandle();
} }
Array<VkPushConstantRange> vkPushConstants(pushConstants.size()); Array<VkPushConstantRange> vkPushConstants(pushConstants.size());
+11 -5
View File
@@ -16,7 +16,7 @@ Slang::ComPtr<slang::IBlob> Seele::generateShader(const ShaderCreateInfo& create
} }
slang::SessionDesc sessionDesc; slang::SessionDesc sessionDesc;
sessionDesc.flags = 0; sessionDesc.flags = 0;
slang::CompilerOptionEntry option[2]; StaticArray<slang::CompilerOptionEntry, 4> option;
option[0].name = slang::CompilerOptionName::DumpIntermediates; option[0].name = slang::CompilerOptionName::DumpIntermediates;
option[0].value = slang::CompilerOptionValue(); option[0].value = slang::CompilerOptionValue();
option[0].value.kind = slang::CompilerOptionValueKind::Int; option[0].value.kind = slang::CompilerOptionValueKind::Int;
@@ -25,8 +25,16 @@ Slang::ComPtr<slang::IBlob> Seele::generateShader(const ShaderCreateInfo& create
option[1].value = slang::CompilerOptionValue(); option[1].value = slang::CompilerOptionValue();
option[1].value.kind = slang::CompilerOptionValueKind::Int; option[1].value.kind = slang::CompilerOptionValueKind::Int;
option[1].value.intValue0 = 1; option[1].value.intValue0 = 1;
sessionDesc.compilerOptionEntries = option; option[2].name = slang::CompilerOptionName::DebugInformation;
sessionDesc.compilerOptionEntryCount = 2; option[2].value = slang::CompilerOptionValue();
option[2].value.kind = slang::CompilerOptionValueKind::Int;
option[2].value.intValue0 = 3;
option[3].name = slang::CompilerOptionName::DebugInformationFormat;
option[3].value = slang::CompilerOptionValue();
option[3].value.kind = slang::CompilerOptionValueKind::Int;
option[3].value.stringValue0 = "c7";
sessionDesc.compilerOptionEntries = option.data();
sessionDesc.compilerOptionEntryCount = option.size();
sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR; sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
Array<slang::PreprocessorMacroDesc> macros; Array<slang::PreprocessorMacroDesc> macros;
for(const auto& [key, val] : createInfo.defines) for(const auto& [key, val] : createInfo.defines)
@@ -41,8 +49,6 @@ Slang::ComPtr<slang::IBlob> Seele::generateShader(const ShaderCreateInfo& create
slang::TargetDesc targetDesc; slang::TargetDesc targetDesc;
targetDesc.profile = globalSession->findProfile("sm_6_6"); targetDesc.profile = globalSession->findProfile("sm_6_6");
targetDesc.format = target; targetDesc.format = target;
targetDesc.compilerOptionEntryCount = 2;
targetDesc.compilerOptionEntries = option;
sessionDesc.targetCount = 1; sessionDesc.targetCount = 1;
sessionDesc.targets = &targetDesc; sessionDesc.targets = &targetDesc;
StaticArray<const char*, 3> searchPaths = {"shaders/", "shaders/lib/", "shaders/generated/"}; StaticArray<const char*, 3> searchPaths = {"shaders/", "shaders/lib/", "shaders/generated/"};
+39
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "ArchiveBuffer.h" #include "ArchiveBuffer.h"
#include "Math/Vector.h" #include "Math/Vector.h"
#include "Math/Matrix.h"
namespace Seele namespace Seele
{ {
@@ -52,6 +53,44 @@ namespace Serialization
save(buffer, vec.y); save(buffer, vec.y);
save(buffer, vec.z); save(buffer, vec.z);
} }
static void load(ArchiveBuffer& buffer, Matrix4& mat)
{
load(buffer, mat[0][0]);
load(buffer, mat[0][1]);
load(buffer, mat[0][2]);
load(buffer, mat[0][3]);
load(buffer, mat[1][0]);
load(buffer, mat[1][1]);
load(buffer, mat[1][2]);
load(buffer, mat[1][3]);
load(buffer, mat[2][0]);
load(buffer, mat[2][1]);
load(buffer, mat[2][2]);
load(buffer, mat[2][3]);
load(buffer, mat[3][0]);
load(buffer, mat[3][1]);
load(buffer, mat[3][2]);
load(buffer, mat[3][3]);
}
static void save(ArchiveBuffer& buffer, const Matrix4& mat)
{
save(buffer, mat[0][0]);
save(buffer, mat[0][1]);
save(buffer, mat[0][2]);
save(buffer, mat[0][3]);
save(buffer, mat[1][0]);
save(buffer, mat[1][1]);
save(buffer, mat[1][2]);
save(buffer, mat[1][3]);
save(buffer, mat[2][0]);
save(buffer, mat[2][1]);
save(buffer, mat[2][2]);
save(buffer, mat[2][3]);
save(buffer, mat[3][0]);
save(buffer, mat[3][1]);
save(buffer, mat[3][2]);
save(buffer, mat[3][3]);
}
static void load(ArchiveBuffer& buffer, Vector& vec) static void load(ArchiveBuffer& buffer, Vector& vec)
{ {
load(buffer, vec.x); load(buffer, vec.x);