From 4ab924f251bd5ea6c3b2a62bdecfcddaeec95840 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 31 Jul 2023 21:43:20 +0200 Subject: [PATCH] Decent asset loading --- CMakeSettings.json | 10 -------- src/Editor/Asset/FontLoader.cpp | 10 ++++++++ src/Editor/Asset/MaterialLoader.cpp | 19 +++++++++++++-- src/Editor/Asset/MeshLoader.cpp | 13 ++++++++++- src/Editor/Asset/TextureLoader.cpp | 36 +++++++++++++++++++++-------- src/Editor/main.cpp | 2 -- src/Engine/Asset/Asset.cpp | 7 ------ src/Engine/Asset/Asset.h | 3 +-- src/Engine/Asset/AssetRegistry.cpp | 25 ++++++++------------ src/Engine/Asset/AssetRegistry.h | 8 +++---- src/Engine/Asset/TextureAsset.cpp | 33 +++++++++++--------------- src/Engine/Asset/TextureAsset.h | 2 -- 12 files changed, 94 insertions(+), 74 deletions(-) diff --git a/CMakeSettings.json b/CMakeSettings.json index 6d53b96..9dd9604 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -7,19 +7,9 @@ "configurationType": "Debug", "generator": "Visual Studio 17 2022 Win64", "intelliSenseMode": "windows-msvc-x64", - "cmakeExecutable": "D:/Programms/CMake/bin/cmake.exe", "inheritEnvironments": [ "msvc_x64" ], "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=d -DCMAKE_PLATFORM=x64 -DCMAKE_BUILD_TYPE=Debug", "buildRoot": "C:/Users/Dynamitos/Seele/bin/Debug" - }, - { - "name": "Release", - "configurationType": "Release", - "buildCommandArgs": "", - "ctestCommandArgs": "", - "generator": "Visual Studio 17 2022 Win64", - "intelliSenseMode": "windows-msvc-x64", - "inheritEnvironments": [] } ] } \ No newline at end of file diff --git a/src/Editor/Asset/FontLoader.cpp b/src/Editor/Asset/FontLoader.cpp index 56e3bef..a672a4f 100644 --- a/src/Editor/Asset/FontLoader.cpp +++ b/src/Editor/Asset/FontLoader.cpp @@ -72,5 +72,15 @@ void FontLoader::import(FontImportArgs args, PFontAsset asset) } FT_Done_Face(face); FT_Done_FreeType(ft); + + auto stream = AssetRegistry::createWriteStream((std::filesystem::path(asset->getFolderPath()) / asset->getName()).replace_extension("asset").string(), std::ios::binary); + + ArchiveBuffer archive; + Serialization::save(archive, FontAsset::IDENTIFIER); + Serialization::save(archive, asset->getName()); + Serialization::save(archive, asset->getFolderPath()); + asset->save(archive); + archive.writeToStream(stream); + asset->setStatus(Asset::Status::Ready); } \ No newline at end of file diff --git a/src/Editor/Asset/MaterialLoader.cpp b/src/Editor/Asset/MaterialLoader.cpp index 3d2536a..44738d1 100644 --- a/src/Editor/Asset/MaterialLoader.cpp +++ b/src/Editor/Asset/MaterialLoader.cpp @@ -38,9 +38,9 @@ void MaterialLoader::importAsset(MaterialImportArgs args) void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset) { - auto stream = std::ifstream(args.filePath.c_str()); + auto jsonstream = std::ifstream(args.filePath.c_str()); json j; - stream >> j; + jsonstream >> j; std::string materialName = j["name"].get() + "Material"; Gfx::PDescriptorLayout layout = graphics->createDescriptorLayout(materialName + "Layout"); //Shader file needs to conform to the slang standard, which prohibits _ @@ -221,9 +221,24 @@ void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset) std::move(codeExp), std::move(mat) ); + asset->material->compile(); graphics->getShaderCompiler()->registerMaterial(asset->material); asset->setStatus(Asset::Status::Ready); + + if (asset->getName().empty()) + { + return; + } + + auto stream = AssetRegistry::createWriteStream((std::filesystem::path(asset->folderPath) / asset->getName()).string().append(".asset"), std::ios::binary); + + ArchiveBuffer archive; + Serialization::save(archive, MaterialAsset::IDENTIFIER); + Serialization::save(archive, asset->getName()); + Serialization::save(archive, asset->getFolderPath()); + asset->save(archive); + archive.writeToStream(stream); ////co_return; } diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 76d47d5..57a1abc 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -323,6 +323,17 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset) } } meshAsset->physicsMesh = std::move(collider); + + + auto stream = AssetRegistry::createWriteStream((std::filesystem::path(meshAsset->getFolderPath()) / meshAsset->getName()).replace_extension("asset").string(), std::ios::binary); + + ArchiveBuffer archive; + Serialization::save(archive, MeshAsset::IDENTIFIER); + Serialization::save(archive, meshAsset->getName()); + Serialization::save(archive, meshAsset->getFolderPath()); + meshAsset->save(archive); + archive.writeToStream(stream); + meshAsset->setStatus(Asset::Status::Ready); - std::cout << "Finished loading " << args.filePath<< std::endl; + std::cout << "Finished loading " << args.filePath << std::endl; } diff --git a/src/Editor/Asset/TextureLoader.cpp b/src/Editor/Asset/TextureLoader.cpp index 1d13a3e..5ed41ac 100644 --- a/src/Editor/Asset/TextureLoader.cpp +++ b/src/Editor/Asset/TextureLoader.cpp @@ -47,6 +47,7 @@ PTextureAsset TextureLoader::getPlaceholderTexture() void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) { + int totalWidth, totalHeight, n; unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4); ktxTexture2* kTexture; @@ -109,28 +110,45 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) 0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char)); } std::cout << "starting compression of " << textureAsset->getAssetIdentifier() << std::endl; - ktxBasisParams params = { + + ktxBasisParams params2 = { .structSize = sizeof(ktxBasisParams), - .uastc = KTX_TRUE, + .uastc = false, .threadCount = std::thread::hardware_concurrency(), + .compressionLevel = 5, .qualityLevel = 0, }; - ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, ¶ms); + + ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, ¶ms2); assert(error == KTX_SUCCESS); - //error = ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0); - //assert(error == KTX_SUCCESS); - ktx_uint8_t* dest; ktx_size_t size; ktxTexture_WriteToMemory(ktxTexture(kTexture), &dest, &size); Array memory(size); std::memcpy(memory.data(), dest, size); - - textureAsset->createFromMemory(std::move(memory), graphics); - free(dest); + stbi_image_free(data); + + ArchiveBuffer temp(graphics); + Serialization::save(temp, memory); + temp.rewind(); + textureAsset->load(temp); + if (textureAsset->getName().empty()) + { + return; + } + auto stream = AssetRegistry::createWriteStream((std::filesystem::path(textureAsset->folderPath) / textureAsset->getName()).replace_extension("asset").string(), std::ios::binary); + + ArchiveBuffer archive; + Serialization::save(archive, TextureAsset::IDENTIFIER); + Serialization::save(archive, textureAsset->getName()); + Serialization::save(archive, textureAsset->getFolderPath()); + Serialization::save(archive, memory); + archive.writeToStream(stream); + + ////co_return; } \ No newline at end of file diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 3b953d2..21f7567 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -204,8 +204,6 @@ int main() .type = TextureImportType::TEXTURE_CUBEMAP, }); - AssetRegistry::saveRegistry(); - WindowCreateInfo mainWindowInfo; mainWindowInfo.title = "SeeleEngine"; mainWindowInfo.width = 1280; diff --git a/src/Engine/Asset/Asset.cpp b/src/Engine/Asset/Asset.cpp index f234356..da51349 100644 --- a/src/Engine/Asset/Asset.cpp +++ b/src/Engine/Asset/Asset.cpp @@ -31,13 +31,6 @@ Asset::~Asset() { } -void Asset::updateByteSize() -{ - ArchiveBuffer buffer; - save(buffer); - byteSize = buffer.size(); -} - std::string Asset::getFolderPath() const { return folderPath; diff --git a/src/Engine/Asset/Asset.h b/src/Engine/Asset/Asset.h index b95b9af..c2a7245 100644 --- a/src/Engine/Asset/Asset.h +++ b/src/Engine/Asset/Asset.h @@ -23,14 +23,13 @@ public: virtual void save(ArchiveBuffer& buffer) const = 0; virtual void load(ArchiveBuffer& buffer) = 0; + bool isModified() const; // returns the assets name std::string getName() const; // returns the (virtual) folder path std::string getFolderPath() const; // returns the identifier with which it can be found from the asset registry std::string getAssetIdentifier() const; - // returns the size of the assets data in bytes(excluding name and folder) - uint64 getByteSize() const { return byteSize; } constexpr Status getStatus() { diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index 6c49b43..8db48a2 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -77,12 +77,12 @@ PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath) return folder->materials.at(fileName); } -std::ofstream AssetRegistry::createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode) +std::ofstream AssetRegistry::createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) { return get().internalCreateWriteStream(relativePath, openmode); } -std::ifstream AssetRegistry::createReadStream(const std::string& relativePath, std::ios_base::openmode openmode) +std::ifstream AssetRegistry::createReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) { return get().internalCreateReadStream(relativePath, openmode); } @@ -229,9 +229,6 @@ void AssetRegistry::loadAsset(ArchiveBuffer& buffer) std::string folderPath; Serialization::load(buffer, folderPath); - uint64 assetSize; - Serialization::load(buffer, assetSize); - AssetFolder* folder = getOrCreateFolder(folderPath); PAsset asset; @@ -294,7 +291,7 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy if (name.empty()) return; - std::string path = (folderPath / name).string().append(".asset"); + std::string path = (folderPath / name).replace_extension("asset").string(); auto assetStream = createWriteStream(std::move(path), std::ios::binary); ArchiveBuffer assetBuffer(graphics); // write identifier @@ -303,13 +300,9 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy Serialization::save(assetBuffer, name); // write folder Serialization::save(assetBuffer, folderPath.string()); - // write the asset size - asset->updateByteSize(); - Serialization::save(assetBuffer, asset->getByteSize()); // write asset data asset->save(assetBuffer); assetBuffer.writeToStream(assetStream); - } std::filesystem::path AssetRegistry::getRootFolder() @@ -367,17 +360,17 @@ AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string fullPat return result; } -std::ofstream AssetRegistry::internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode) +std::ofstream AssetRegistry::internalCreateWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) { - auto fullPath = rootFolder; - fullPath.append(relativePath); + auto fullPath = rootFolder / relativePath; + std::filesystem::create_directories(fullPath.parent_path()); return std::ofstream(fullPath.string(), openmode); } -std::ifstream AssetRegistry::internalCreateReadStream(const std::string& relativePath, std::ios_base::openmode openmode) +std::ifstream AssetRegistry::internalCreateReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode) { - auto fullPath = rootFolder; - fullPath.append(relativePath); + auto fullPath = rootFolder / relativePath; + std::filesystem::create_directories(fullPath.parent_path()); return std::ifstream(fullPath.string(), openmode); } diff --git a/src/Engine/Asset/AssetRegistry.h b/src/Engine/Asset/AssetRegistry.h index 794209b..81a7f36 100644 --- a/src/Engine/Asset/AssetRegistry.h +++ b/src/Engine/Asset/AssetRegistry.h @@ -25,8 +25,8 @@ public: static PFontAsset findFont(const std::string& name); static PMaterialAsset findMaterial(const std::string& filePath); - static std::ofstream createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out); - static std::ifstream createReadStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::in); + static std::ofstream createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::out); + static std::ifstream createReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::in); static void set(AssetRegistry registry); @@ -63,8 +63,8 @@ private: void registerFont(PFontAsset font); void registerMaterial(PMaterialAsset material); - std::ofstream internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out); - std::ifstream internalCreateReadStream(const std::string& relaitvePath, std::ios_base::openmode openmode = std::ios::in); + std::ofstream internalCreateWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::out); + std::ifstream internalCreateReadStream(const std::filesystem::path& relaitvePath, std::ios_base::openmode openmode = std::ios::in); std::filesystem::path rootFolder; AssetFolder* assetRoot; diff --git a/src/Engine/Asset/TextureAsset.cpp b/src/Engine/Asset/TextureAsset.cpp index ce02940..f0b6558 100644 --- a/src/Engine/Asset/TextureAsset.cpp +++ b/src/Engine/Asset/TextureAsset.cpp @@ -25,8 +25,7 @@ TextureAsset::~TextureAsset() void TextureAsset::save(ArchiveBuffer& buffer) const { - /*Array textureData; - ktxTexture2* kTexture; + /*ktxTexture2* kTexture; ktxTextureCreateInfo createInfo = { .vkFormat = (uint32_t)texture->getFormat(), .baseWidth = texture->getSizeX(), @@ -47,8 +46,9 @@ void TextureAsset::save(ArchiveBuffer& buffer) const { // technically, downloading cant be const, because we have to allocate temporary buffers and change layouts // but practically the texture stays the same - const_cast(texture.getHandle())->download(0, depth, face, textureData); - KTX_CHECK(ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, textureData.data(), textureData.size())); + Array faceData; + const_cast(texture.getHandle())->download(0, depth, face, faceData); + KTX_CHECK(ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, faceData.data(), faceData.size())); } } char writer[100]; @@ -57,7 +57,7 @@ void TextureAsset::save(ArchiveBuffer& buffer) const (ktx_uint32_t)strlen(writer) + 1, writer); - //ktx_error_code_e error = ktxTexture2_CompressBasis(kTexture, 0); + KTX_CHECK(ktxTexture2_CompressAstc(kTexture, 0)); ktx_uint8_t* texData; ktx_size_t texSize; @@ -65,29 +65,24 @@ void TextureAsset::save(ArchiveBuffer& buffer) const Array rawData(texSize); std::memcpy(rawData.data(), texData, texSize); - free(texData); - */ - Serialization::save(buffer, textureData); + Serialization::save(buffer, rawData); + free(texData);*/ + assert(false); // TODO } void TextureAsset::load(ArchiveBuffer& buffer) { + Gfx::PGraphics graphics = buffer.getGraphics(); Array rawData; Serialization::load(buffer, rawData); - createFromMemory(std::move(rawData), buffer.getGraphics()); -} - -void TextureAsset::createFromMemory(Array memory, Gfx::PGraphics graphics) -{ - textureData = std::move(memory); ktxTexture2* kTexture; - std::cout << "Loading texture " << name << std::endl; - KTX_CHECK(ktxTexture_CreateFromMemory(textureData.data(), - textureData.size(), + KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(), + rawData.size(), KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, (ktxTexture**)&kTexture)); - ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0); + ktx_error_code_e e = ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0); + assert(e == ktx_error_code_e::KTX_SUCCESS); TextureCreateInfo createInfo = { .resourceData = { @@ -118,4 +113,4 @@ void TextureAsset::createFromMemory(Array memory, Gfx::PGraphics graphics } texture->transferOwnership(Gfx::QueueType::GRAPHICS); ktxTexture_Destroy(ktxTexture(kTexture)); -} \ No newline at end of file +} diff --git a/src/Engine/Asset/TextureAsset.h b/src/Engine/Asset/TextureAsset.h index 1126302..264d9ce 100644 --- a/src/Engine/Asset/TextureAsset.h +++ b/src/Engine/Asset/TextureAsset.h @@ -13,7 +13,6 @@ public: virtual ~TextureAsset(); virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; - void createFromMemory(Array memory, Gfx::PGraphics graphics); void setTexture(Gfx::PTexture _texture) { texture = _texture; @@ -23,7 +22,6 @@ public: return texture; } private: - Array textureData; Gfx::PTexture texture; friend class TextureLoader; };