Reworked mesh asset loading
This commit is contained in:
@@ -26,69 +26,54 @@ void AssetRegistry::init(std::filesystem::path rootFolder, Gfx::PGraphics graphi
|
||||
get().initialize(rootFolder, graphics);
|
||||
}
|
||||
|
||||
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
|
||||
PMeshAsset AssetRegistry::findMesh(std::string_view folderPath, std::string_view filePath)
|
||||
{
|
||||
AssetFolder* folder = get().assetRoot;
|
||||
std::string fileName = filePath;
|
||||
size_t slashLoc = filePath.rfind("/");
|
||||
if(slashLoc != std::string::npos)
|
||||
if (!folderPath.empty())
|
||||
{
|
||||
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
|
||||
fileName = filePath.substr(slashLoc+1, filePath.size());
|
||||
folder = get().getOrCreateFolder(folderPath);
|
||||
}
|
||||
return folder->meshes.at(fileName);
|
||||
return folder->meshes.at(std::string(filePath));
|
||||
}
|
||||
|
||||
PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
|
||||
PTextureAsset AssetRegistry::findTexture(std::string_view folderPath, std::string_view filePath)
|
||||
{
|
||||
AssetFolder* folder = get().assetRoot;
|
||||
std::string fileName = filePath;
|
||||
size_t slashLoc = filePath.rfind("/");
|
||||
if (slashLoc != std::string::npos)
|
||||
if (!folderPath.empty())
|
||||
{
|
||||
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
|
||||
fileName = filePath.substr(slashLoc + 1, filePath.size());
|
||||
folder = get().getOrCreateFolder(folderPath);
|
||||
}
|
||||
return folder->textures.at(fileName);
|
||||
return folder->textures.at(std::string(filePath));
|
||||
}
|
||||
|
||||
PFontAsset AssetRegistry::findFont(const std::string& filePath)
|
||||
PFontAsset AssetRegistry::findFont(std::string_view folderPath, std::string_view filePath)
|
||||
{
|
||||
AssetFolder* folder = get().assetRoot;
|
||||
std::string fileName = filePath;
|
||||
size_t slashLoc = filePath.rfind("/");
|
||||
if(slashLoc != std::string::npos)
|
||||
if (!folderPath.empty())
|
||||
{
|
||||
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
|
||||
fileName = filePath.substr(slashLoc+1, filePath.size());
|
||||
folder = get().getOrCreateFolder(folderPath);
|
||||
}
|
||||
return folder->fonts.at(fileName);
|
||||
return folder->fonts.at(std::string(filePath));
|
||||
}
|
||||
|
||||
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
|
||||
PMaterialAsset AssetRegistry::findMaterial(std::string_view folderPath, std::string_view filePath)
|
||||
{
|
||||
AssetFolder* folder = get().assetRoot;
|
||||
std::string fileName = filePath;
|
||||
size_t slashLoc = filePath.rfind("/");
|
||||
if (slashLoc != std::string::npos)
|
||||
if (!folderPath.empty())
|
||||
{
|
||||
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
|
||||
fileName = filePath.substr(slashLoc + 1, filePath.size());
|
||||
folder = get().getOrCreateFolder(folderPath);
|
||||
}
|
||||
return folder->materials.at(fileName);
|
||||
return folder->materials.at(std::string(filePath));
|
||||
}
|
||||
|
||||
PMaterialInstanceAsset AssetRegistry::findMaterialInstance(const std::string &filePath)
|
||||
PMaterialInstanceAsset AssetRegistry::findMaterialInstance(std::string_view folderPath, std::string_view filePath)
|
||||
{
|
||||
AssetFolder* folder = get().assetRoot;
|
||||
std::string fileName = filePath;
|
||||
size_t slashLoc = filePath.rfind("/");
|
||||
if (slashLoc != std::string::npos)
|
||||
if (!folderPath.empty())
|
||||
{
|
||||
folder = get().getOrCreateFolder(filePath.substr(0, slashLoc));
|
||||
fileName = filePath.substr(slashLoc + 1, filePath.size());
|
||||
folder = get().getOrCreateFolder(folderPath);
|
||||
}
|
||||
return folder->instances.at(fileName);
|
||||
return folder->instances.at(std::string(filePath));
|
||||
}
|
||||
|
||||
std::ofstream AssetRegistry::createWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode)
|
||||
@@ -126,6 +111,31 @@ void AssetRegistry::saveRegistry()
|
||||
get().saveRegistryInternal();
|
||||
}
|
||||
|
||||
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string_view fullPath)
|
||||
{
|
||||
AssetFolder* result = assetRoot;
|
||||
std::string temp = std::string(fullPath);
|
||||
while (!temp.empty())
|
||||
{
|
||||
size_t slashLoc = temp.find("/");
|
||||
if (slashLoc == std::string::npos)
|
||||
{
|
||||
if (!result->children.contains(temp))
|
||||
{
|
||||
result->children[temp] = new AssetFolder(temp);
|
||||
}
|
||||
return result->children[temp];
|
||||
}
|
||||
std::string folderName = temp.substr(0, slashLoc);
|
||||
if (!result->children.contains(folderName))
|
||||
{
|
||||
result->children[folderName] = new AssetFolder(fullPath);
|
||||
}
|
||||
result = result->children[folderName];
|
||||
temp = temp.substr(slashLoc + 1, temp.size());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void AssetRegistry::initialize(const std::filesystem::path &_rootFolder, Gfx::PGraphics _graphics)
|
||||
{
|
||||
@@ -318,7 +328,7 @@ void AssetRegistry::saveAsset(PAsset asset, uint64 identifier, const std::filesy
|
||||
if (name.empty())
|
||||
return;
|
||||
|
||||
std::string path = (folderPath / name).replace_extension("asset").string();
|
||||
std::string path = (folderPath / name).string().append(".asset");
|
||||
auto assetStream = createWriteStream(std::move(path), std::ios::binary);
|
||||
ArchiveBuffer buffer(graphics);
|
||||
// write identifier
|
||||
@@ -367,31 +377,6 @@ void AssetRegistry::registerMaterialInstance(OMaterialInstanceAsset material)
|
||||
folder->instances[material->getName()] = std::move(material);
|
||||
}
|
||||
|
||||
AssetRegistry::AssetFolder* AssetRegistry::getOrCreateFolder(std::string fullPath)
|
||||
{
|
||||
AssetFolder* result = assetRoot;
|
||||
std::string temp = fullPath;
|
||||
while(!fullPath.empty())
|
||||
{
|
||||
size_t slashLoc = fullPath.find("/");
|
||||
if(slashLoc == std::string::npos)
|
||||
{
|
||||
if (!result->children.contains(fullPath))
|
||||
{
|
||||
result->children[fullPath] = new AssetFolder(fullPath);
|
||||
}
|
||||
return result->children[fullPath];
|
||||
}
|
||||
std::string folderName = fullPath.substr(0, slashLoc);
|
||||
if (!result->children.contains(folderName))
|
||||
{
|
||||
result->children[folderName] = new AssetFolder(temp);
|
||||
}
|
||||
result = result->children[folderName];
|
||||
fullPath = fullPath.substr(slashLoc+1, fullPath.size());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::ofstream AssetRegistry::internalCreateWriteStream(const std::filesystem::path& relativePath, std::ios_base::openmode openmode)
|
||||
{
|
||||
|
||||
@@ -21,11 +21,11 @@ public:
|
||||
|
||||
static std::filesystem::path getRootFolder();
|
||||
|
||||
static PMeshAsset findMesh(const std::string& filePath);
|
||||
static PTextureAsset findTexture(const std::string& filePath);
|
||||
static PFontAsset findFont(const std::string& name);
|
||||
static PMaterialAsset findMaterial(const std::string& filePath);
|
||||
static PMaterialInstanceAsset findMaterialInstance(const std::string& filePath);
|
||||
static PMeshAsset findMesh(std::string_view folderPath, std::string_view filePath);
|
||||
static PTextureAsset findTexture(std::string_view folderPath, std::string_view filePath);
|
||||
static PFontAsset findFont(std::string_view folderPath, std::string_view name);
|
||||
static PMaterialAsset findMaterial(std::string_view folderPath, std::string_view filePath);
|
||||
static PMaterialInstanceAsset findMaterialInstance(std::string_view folderPath, std::string_view filePath);
|
||||
|
||||
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);
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
AssetFolder(std::string_view folderPath);
|
||||
~AssetFolder();
|
||||
};
|
||||
AssetFolder* getOrCreateFolder(std::string foldername);
|
||||
AssetFolder* getOrCreateFolder(std::string_view foldername);
|
||||
AssetRegistry();
|
||||
static AssetRegistry* getInstance();
|
||||
private:
|
||||
|
||||
@@ -24,6 +24,7 @@ public:
|
||||
private:
|
||||
OMaterial material;
|
||||
friend class MaterialLoader;
|
||||
friend class MeshLoader;
|
||||
};
|
||||
DEFINE_REF(MaterialAsset)
|
||||
} // namespace Seele
|
||||
|
||||
@@ -22,16 +22,19 @@ MaterialInstanceAsset::~MaterialInstanceAsset()
|
||||
|
||||
void MaterialInstanceAsset::save(ArchiveBuffer& buffer) const
|
||||
{
|
||||
Serialization::save(buffer, baseMaterial->getAssetIdentifier());
|
||||
Serialization::save(buffer, baseMaterial->getFolderPath());
|
||||
Serialization::save(buffer, baseMaterial->getName());
|
||||
material->save(buffer);
|
||||
}
|
||||
|
||||
void MaterialInstanceAsset::load(ArchiveBuffer& buffer)
|
||||
{
|
||||
std::string folder;
|
||||
Serialization::load(buffer, folder);
|
||||
std::string id;
|
||||
Serialization::load(buffer, id);
|
||||
material = new MaterialInstance();
|
||||
material->load(buffer);
|
||||
baseMaterial = AssetRegistry::findMaterial(id);
|
||||
baseMaterial = AssetRegistry::findMaterial(folder, id);
|
||||
material->setBaseMaterial(baseMaterial);
|
||||
}
|
||||
|
||||
@@ -20,91 +20,60 @@ TextureAsset::TextureAsset(std::string_view folderPath, std::string_view name)
|
||||
|
||||
TextureAsset::~TextureAsset()
|
||||
{
|
||||
|
||||
ktxTexture_Destroy(ktxTexture(ktxHandle));
|
||||
}
|
||||
|
||||
void TextureAsset::save(ArchiveBuffer&) const
|
||||
void TextureAsset::save(ArchiveBuffer& buffer) const
|
||||
{
|
||||
/*ktxTexture2* kTexture;
|
||||
ktxTextureCreateInfo createInfo = {
|
||||
.vkFormat = (uint32_t)texture->getFormat(),
|
||||
.baseWidth = texture->getSizeX(),
|
||||
.baseHeight = texture->getSizeY(),
|
||||
.baseDepth = texture->getSizeZ(),
|
||||
.numDimensions = texture->getSizeZ() > 1 ? 3u : texture->getSizeY() > 1 ? 2u : 1u,
|
||||
.numLevels = texture->getMipLevels(),
|
||||
.numLayers = 1,
|
||||
.numFaces = texture->getNumFaces(),
|
||||
.isArray = false,
|
||||
.generateMipmaps = false,
|
||||
};
|
||||
KTX_CHECK(ktxTexture2_Create(&createInfo, KTX_TEXTURE_CREATE_ALLOC_STORAGE, &kTexture));
|
||||
|
||||
for (uint32 depth = 0; depth < texture->getSizeZ(); ++depth)
|
||||
{
|
||||
for (uint32 face = 0; face < texture->getNumFaces(); ++face)
|
||||
{
|
||||
// technically, downloading cant be const, because we have to allocate temporary buffers and change layouts
|
||||
// but practically the texture stays the same
|
||||
Array<uint8> faceData;
|
||||
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];
|
||||
snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1");
|
||||
ktxHashList_AddKVPair(&kTexture->kvDataHead, KTX_WRITER_KEY,
|
||||
ktxHashList_AddKVPair(&ktxHandle->kvDataHead, KTX_WRITER_KEY,
|
||||
(ktx_uint32_t)strlen(writer) + 1,
|
||||
writer);
|
||||
|
||||
KTX_CHECK(ktxTexture2_CompressAstc(kTexture, 0));
|
||||
|
||||
ktx_uint8_t* texData;
|
||||
ktx_size_t texSize;
|
||||
KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(kTexture), &texData, &texSize));
|
||||
KTX_CHECK(ktxTexture_WriteToMemory(ktxTexture(ktxHandle), &texData, &texSize));
|
||||
|
||||
Array<uint8> rawData(texSize);
|
||||
std::memcpy(rawData.data(), texData, texSize);
|
||||
Serialization::save(buffer, rawData);
|
||||
free(texData);*/
|
||||
assert(false); // TODO
|
||||
free(texData);
|
||||
}
|
||||
|
||||
void TextureAsset::load(ArchiveBuffer& buffer)
|
||||
{
|
||||
Gfx::PGraphics graphics = buffer.getGraphics();
|
||||
Serialization::load(buffer, rawPixels);
|
||||
Array<uint8> rawData;
|
||||
Serialization::load(buffer, rawData);
|
||||
ktxTexture2* kTexture;
|
||||
KTX_CHECK(ktxTexture_CreateFromMemory(rawData.data(),
|
||||
rawData.size(),
|
||||
KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT,
|
||||
(ktxTexture**)&kTexture));
|
||||
(ktxTexture**)&ktxHandle));
|
||||
|
||||
ktx_error_code_e e = ktxTexture2_TranscodeBasis(kTexture, KTX_TTF_BC7_RGBA, 0);
|
||||
assert(e == ktx_error_code_e::KTX_SUCCESS);
|
||||
//ktx_error_code_e e = ktxTexture2_TranscodeBasis(ktxHandle, KTX_TTF_BC7_RGBA, 0);
|
||||
//assert(e == ktx_error_code_e::KTX_SUCCESS);
|
||||
|
||||
TextureCreateInfo createInfo = {
|
||||
.sourceData = {
|
||||
.size = ktxTexture_GetDataSize(ktxTexture(kTexture)),
|
||||
.data = ktxTexture_GetData(ktxTexture(kTexture)),
|
||||
.size = ktxTexture_GetDataSize(ktxTexture(ktxHandle)),
|
||||
.data = ktxTexture_GetData(ktxTexture(ktxHandle)),
|
||||
.owner = Gfx::QueueType::TRANSFER,
|
||||
},
|
||||
.format = (Gfx::SeFormat)kTexture->vkFormat,
|
||||
.width = kTexture->baseWidth,
|
||||
.height = kTexture->baseHeight,
|
||||
.depth = kTexture->baseDepth,
|
||||
.mipLevels = kTexture->numLevels,
|
||||
.layers = kTexture->numFaces,
|
||||
.elements = kTexture->numLayers,
|
||||
.format = (Gfx::SeFormat)ktxHandle->vkFormat,
|
||||
.width = ktxHandle->baseWidth,
|
||||
.height = ktxHandle->baseHeight,
|
||||
.depth = ktxHandle->baseDepth,
|
||||
.mipLevels = ktxHandle->numLevels,
|
||||
.layers = ktxHandle->numFaces,
|
||||
.elements = ktxHandle->numLayers,
|
||||
.usage = Gfx::SE_IMAGE_USAGE_SAMPLED_BIT,
|
||||
};
|
||||
if (kTexture->isCubemap)
|
||||
if (ktxHandle->isCubemap)
|
||||
{
|
||||
texture = graphics->createTextureCube(createInfo);
|
||||
}
|
||||
else if (kTexture->isArray)
|
||||
else if (ktxHandle->isArray)
|
||||
{
|
||||
texture = graphics->createTexture3D(createInfo);
|
||||
}
|
||||
@@ -113,7 +82,6 @@ void TextureAsset::load(ArchiveBuffer& buffer)
|
||||
texture = graphics->createTexture2D(createInfo);
|
||||
}
|
||||
texture->transferOwnership(Gfx::QueueType::GRAPHICS);
|
||||
ktxTexture_Destroy(ktxTexture(kTexture));
|
||||
}
|
||||
|
||||
void TextureAsset::setTexture(Gfx::OTexture _texture)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "Asset.h"
|
||||
|
||||
struct ktxTexture2;
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_NAME_REF(Gfx, Texture)
|
||||
@@ -18,14 +19,10 @@ public:
|
||||
{
|
||||
return texture;
|
||||
}
|
||||
const Array<uint8>& getRawPixels()
|
||||
{
|
||||
return rawPixels;
|
||||
}
|
||||
uint32 getWidth();
|
||||
uint32 getHeight();
|
||||
private:
|
||||
Array<uint8> rawPixels;
|
||||
struct ktxTexture2* ktxHandle;
|
||||
Gfx::OTexture texture;
|
||||
friend class TextureLoader;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user