Decent asset loading

This commit is contained in:
Dynamitos
2023-07-31 21:43:20 +02:00
parent 1668df467a
commit 4ab924f251
12 changed files with 94 additions and 74 deletions
-10
View File
@@ -7,19 +7,9 @@
"configurationType": "Debug", "configurationType": "Debug",
"generator": "Visual Studio 17 2022 Win64", "generator": "Visual Studio 17 2022 Win64",
"intelliSenseMode": "windows-msvc-x64", "intelliSenseMode": "windows-msvc-x64",
"cmakeExecutable": "D:/Programms/CMake/bin/cmake.exe",
"inheritEnvironments": [ "msvc_x64" ], "inheritEnvironments": [ "msvc_x64" ],
"cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=d -DCMAKE_PLATFORM=x64 -DCMAKE_BUILD_TYPE=Debug", "cmakeCommandArgs": "-DCMAKE_DEBUG_POSTFIX=d -DCMAKE_PLATFORM=x64 -DCMAKE_BUILD_TYPE=Debug",
"buildRoot": "C:/Users/Dynamitos/Seele/bin/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": []
} }
] ]
} }
+10
View File
@@ -72,5 +72,15 @@ void FontLoader::import(FontImportArgs args, PFontAsset asset)
} }
FT_Done_Face(face); FT_Done_Face(face);
FT_Done_FreeType(ft); 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); asset->setStatus(Asset::Status::Ready);
} }
+17 -2
View File
@@ -38,9 +38,9 @@ void MaterialLoader::importAsset(MaterialImportArgs args)
void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset) 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; json j;
stream >> j; jsonstream >> j;
std::string materialName = j["name"].get<std::string>() + "Material"; std::string materialName = j["name"].get<std::string>() + "Material";
Gfx::PDescriptorLayout layout = graphics->createDescriptorLayout(materialName + "Layout"); Gfx::PDescriptorLayout layout = graphics->createDescriptorLayout(materialName + "Layout");
//Shader file needs to conform to the slang standard, which prohibits _ //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(codeExp),
std::move(mat) std::move(mat)
); );
asset->material->compile(); asset->material->compile();
graphics->getShaderCompiler()->registerMaterial(asset->material); graphics->getShaderCompiler()->registerMaterial(asset->material);
asset->setStatus(Asset::Status::Ready); 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; ////co_return;
} }
+12 -1
View File
@@ -323,6 +323,17 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset)
} }
} }
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);
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); meshAsset->setStatus(Asset::Status::Ready);
std::cout << "Finished loading " << args.filePath<< std::endl; std::cout << "Finished loading " << args.filePath << std::endl;
} }
+27 -9
View File
@@ -47,6 +47,7 @@ PTextureAsset TextureLoader::getPlaceholderTexture()
void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
{ {
int totalWidth, totalHeight, n; int totalWidth, totalHeight, n;
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4); unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
ktxTexture2* kTexture; ktxTexture2* kTexture;
@@ -109,28 +110,45 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char)); 0, 0, 0, data, totalWidth * totalHeight * 4 * sizeof(unsigned char));
} }
std::cout << "starting compression of " << textureAsset->getAssetIdentifier() << std::endl; std::cout << "starting compression of " << textureAsset->getAssetIdentifier() << std::endl;
ktxBasisParams params = {
ktxBasisParams params2 = {
.structSize = sizeof(ktxBasisParams), .structSize = sizeof(ktxBasisParams),
.uastc = KTX_TRUE, .uastc = false,
.threadCount = std::thread::hardware_concurrency(), .threadCount = std::thread::hardware_concurrency(),
.compressionLevel = 5,
.qualityLevel = 0, .qualityLevel = 0,
}; };
ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params);
ktx_error_code_e error = ktxTexture2_CompressBasisEx(kTexture, &params2);
assert(error == KTX_SUCCESS); assert(error == KTX_SUCCESS);
//error = ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0);
//assert(error == KTX_SUCCESS);
ktx_uint8_t* dest; ktx_uint8_t* dest;
ktx_size_t size; ktx_size_t size;
ktxTexture_WriteToMemory(ktxTexture(kTexture), &dest, &size); ktxTexture_WriteToMemory(ktxTexture(kTexture), &dest, &size);
Array<uint8> memory(size); Array<uint8> memory(size);
std::memcpy(memory.data(), dest, size); std::memcpy(memory.data(), dest, size);
textureAsset->createFromMemory(std::move(memory), graphics);
free(dest); free(dest);
stbi_image_free(data); 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; ////co_return;
} }
-2
View File
@@ -204,8 +204,6 @@ int main()
.type = TextureImportType::TEXTURE_CUBEMAP, .type = TextureImportType::TEXTURE_CUBEMAP,
}); });
AssetRegistry::saveRegistry();
WindowCreateInfo mainWindowInfo; WindowCreateInfo mainWindowInfo;
mainWindowInfo.title = "SeeleEngine"; mainWindowInfo.title = "SeeleEngine";
mainWindowInfo.width = 1280; mainWindowInfo.width = 1280;
-7
View File
@@ -31,13 +31,6 @@ Asset::~Asset()
{ {
} }
void Asset::updateByteSize()
{
ArchiveBuffer buffer;
save(buffer);
byteSize = buffer.size();
}
std::string Asset::getFolderPath() const std::string Asset::getFolderPath() const
{ {
return folderPath; return folderPath;
+1 -2
View File
@@ -23,14 +23,13 @@ public:
virtual void save(ArchiveBuffer& buffer) const = 0; virtual void save(ArchiveBuffer& buffer) const = 0;
virtual void load(ArchiveBuffer& buffer) = 0; virtual void load(ArchiveBuffer& buffer) = 0;
bool isModified() const;
// returns the assets name // returns the assets name
std::string getName() const; std::string getName() const;
// returns the (virtual) folder path // returns the (virtual) folder path
std::string getFolderPath() const; std::string getFolderPath() const;
// returns the identifier with which it can be found from the asset registry // returns the identifier with which it can be found from the asset registry
std::string getAssetIdentifier() const; 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() constexpr Status getStatus()
{ {
+9 -16
View File
@@ -77,12 +77,12 @@ PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
return folder->materials.at(fileName); 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); 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); return get().internalCreateReadStream(relativePath, openmode);
} }
@@ -229,9 +229,6 @@ void AssetRegistry::loadAsset(ArchiveBuffer& buffer)
std::string folderPath; std::string folderPath;
Serialization::load(buffer, folderPath); Serialization::load(buffer, folderPath);
uint64 assetSize;
Serialization::load(buffer, assetSize);
AssetFolder* folder = getOrCreateFolder(folderPath); AssetFolder* folder = getOrCreateFolder(folderPath);
PAsset asset; PAsset asset;
@@ -294,7 +291,7 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy
if (name.empty()) if (name.empty())
return; 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); auto assetStream = createWriteStream(std::move(path), std::ios::binary);
ArchiveBuffer assetBuffer(graphics); ArchiveBuffer assetBuffer(graphics);
// write identifier // write identifier
@@ -303,13 +300,9 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy
Serialization::save(assetBuffer, name); Serialization::save(assetBuffer, name);
// write folder // write folder
Serialization::save(assetBuffer, folderPath.string()); Serialization::save(assetBuffer, folderPath.string());
// write the asset size
asset->updateByteSize();
Serialization::save(assetBuffer, asset->getByteSize());
// write asset data // write asset data
asset->save(assetBuffer); asset->save(assetBuffer);
assetBuffer.writeToStream(assetStream); assetBuffer.writeToStream(assetStream);
} }
std::filesystem::path AssetRegistry::getRootFolder() std::filesystem::path AssetRegistry::getRootFolder()
@@ -367,17 +360,17 @@ AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string fullPat
return result; 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; auto fullPath = rootFolder / relativePath;
fullPath.append(relativePath); std::filesystem::create_directories(fullPath.parent_path());
return std::ofstream(fullPath.string(), openmode); 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; auto fullPath = rootFolder / relativePath;
fullPath.append(relativePath); std::filesystem::create_directories(fullPath.parent_path());
return std::ifstream(fullPath.string(), openmode); return std::ifstream(fullPath.string(), openmode);
} }
+4 -4
View File
@@ -25,8 +25,8 @@ public:
static PFontAsset findFont(const std::string& name); static PFontAsset findFont(const std::string& name);
static PMaterialAsset findMaterial(const std::string& filePath); 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::ofstream createWriteStream(const std::filesystem::path& 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::ifstream createReadStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode = std::ios::in);
static void set(AssetRegistry registry); static void set(AssetRegistry registry);
@@ -63,8 +63,8 @@ private:
void registerFont(PFontAsset font); void registerFont(PFontAsset font);
void registerMaterial(PMaterialAsset material); void registerMaterial(PMaterialAsset material);
std::ofstream internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out); std::ofstream internalCreateWriteStream(const std::filesystem::path& 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::ifstream internalCreateReadStream(const std::filesystem::path& relaitvePath, std::ios_base::openmode openmode = std::ios::in);
std::filesystem::path rootFolder; std::filesystem::path rootFolder;
AssetFolder* assetRoot; AssetFolder* assetRoot;
+14 -19
View File
@@ -25,8 +25,7 @@ TextureAsset::~TextureAsset()
void TextureAsset::save(ArchiveBuffer& buffer) const void TextureAsset::save(ArchiveBuffer& buffer) const
{ {
/*Array<uint8> textureData; /*ktxTexture2* kTexture;
ktxTexture2* kTexture;
ktxTextureCreateInfo createInfo = { ktxTextureCreateInfo createInfo = {
.vkFormat = (uint32_t)texture->getFormat(), .vkFormat = (uint32_t)texture->getFormat(),
.baseWidth = texture->getSizeX(), .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 // technically, downloading cant be const, because we have to allocate temporary buffers and change layouts
// but practically the texture stays the same // but practically the texture stays the same
const_cast<Gfx::Texture*>(texture.getHandle())->download(0, depth, face, textureData); Array<uint8> faceData;
KTX_CHECK(ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, textureData.data(), textureData.size())); const_cast<Gfx::Texture*>(texture.getHandle())->download(0, depth, face, faceData);
KTX_CHECK(ktxTexture_SetImageFromMemory(ktxTexture(kTexture), 0, depth, face, faceData.data(), faceData.size()));
} }
} }
char writer[100]; char writer[100];
@@ -57,7 +57,7 @@ void TextureAsset::save(ArchiveBuffer& buffer) const
(ktx_uint32_t)strlen(writer) + 1, (ktx_uint32_t)strlen(writer) + 1,
writer); writer);
//ktx_error_code_e error = ktxTexture2_CompressBasis(kTexture, 0); KTX_CHECK(ktxTexture2_CompressAstc(kTexture, 0));
ktx_uint8_t* texData; ktx_uint8_t* texData;
ktx_size_t texSize; ktx_size_t texSize;
@@ -65,29 +65,24 @@ void TextureAsset::save(ArchiveBuffer& buffer) const
Array<uint8> rawData(texSize); Array<uint8> rawData(texSize);
std::memcpy(rawData.data(), texData, texSize); std::memcpy(rawData.data(), texData, texSize);
free(texData); Serialization::save(buffer, rawData);
*/ free(texData);*/
Serialization::save(buffer, textureData); assert(false); // TODO
} }
void TextureAsset::load(ArchiveBuffer& buffer) void TextureAsset::load(ArchiveBuffer& buffer)
{ {
Gfx::PGraphics graphics = buffer.getGraphics();
Array<uint8> rawData; Array<uint8> rawData;
Serialization::load(buffer, rawData); Serialization::load(buffer, rawData);
createFromMemory(std::move(rawData), buffer.getGraphics());
}
void TextureAsset::createFromMemory(Array<uint8> memory, Gfx::PGraphics graphics)
{
textureData = std::move(memory);
ktxTexture2* kTexture; ktxTexture2* kTexture;
std::cout << "Loading texture " << name << std::endl; KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(),
KTX_CHECK(ktxTexture_CreateFromMemory(textureData.data(), rawData.size(),
textureData.size(),
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
(ktxTexture**)&kTexture)); (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 = { TextureCreateInfo createInfo = {
.resourceData = { .resourceData = {
@@ -118,4 +113,4 @@ void TextureAsset::createFromMemory(Array<uint8> memory, Gfx::PGraphics graphics
} }
texture->transferOwnership(Gfx::QueueType::GRAPHICS); texture->transferOwnership(Gfx::QueueType::GRAPHICS);
ktxTexture_Destroy(ktxTexture(kTexture)); ktxTexture_Destroy(ktxTexture(kTexture));
} }
-2
View File
@@ -13,7 +13,6 @@ public:
virtual ~TextureAsset(); virtual ~TextureAsset();
virtual void save(ArchiveBuffer& buffer) const override; virtual void save(ArchiveBuffer& buffer) const override;
virtual void load(ArchiveBuffer& buffer) override; virtual void load(ArchiveBuffer& buffer) override;
void createFromMemory(Array<uint8> memory, Gfx::PGraphics graphics);
void setTexture(Gfx::PTexture _texture) void setTexture(Gfx::PTexture _texture)
{ {
texture = _texture; texture = _texture;
@@ -23,7 +22,6 @@ public:
return texture; return texture;
} }
private: private:
Array<uint8> textureData;
Gfx::PTexture texture; Gfx::PTexture texture;
friend class TextureLoader; friend class TextureLoader;
}; };