1
Fork 0
mirror of https://github.com/redstrate/Novus.git synced 2025-04-25 13:17:46 +00:00

MDLPart: Add render material cache

This commit is contained in:
Joshua Goins 2024-05-27 13:15:40 -04:00
parent 4a777ff9db
commit 2252feeef3
3 changed files with 28 additions and 3 deletions

View file

@ -92,11 +92,11 @@ void MDLPart::addModel(physis_MDL mdl,
model.skinned = skinned;
std::transform(materials.begin(), materials.end(), std::back_inserter(model.materials), [this](const physis_Material &mat) {
return createMaterial(mat);
return createOrCacheMaterial(mat);
});
if (materials.empty()) {
model.materials.push_back(createMaterial(physis_Material{}));
model.materials.push_back(createOrCacheMaterial(physis_Material{}));
}
models.push_back(model);
@ -335,6 +335,28 @@ RenderMaterial MDLPart::createMaterial(const physis_Material &material)
return newMaterial;
}
RenderMaterial MDLPart::createOrCacheMaterial(const physis_Material &mat)
{
auto hash = getMaterialHash(mat);
if (!renderMaterialCache.contains(hash)) {
renderMaterialCache[hash] = createMaterial(mat);
}
return renderMaterialCache[hash];
}
uint64_t MDLPart::getMaterialHash(const physis_Material &mat)
{
// TODO: this hash is terrible
uint64_t hash = strlen(mat.shpk_name);
hash += mat.num_constants;
hash += mat.num_samplers;
hash += mat.num_shader_keys;
hash += mat.num_textures;
return hash;
}
void MDLPart::calculateBoneInversePose(physis_Skeleton &skeleton, physis_Bone &bone, physis_Bone *parent_bone)
{
const glm::mat4 parentMatrix = parent_bone == nullptr ? glm::mat4(1.0f) : boneData[parent_bone->index].inversePose;

View file

@ -89,6 +89,8 @@ protected:
private:
RenderMaterial createMaterial(const physis_Material &mat);
RenderMaterial createOrCacheMaterial(const physis_Material &mat);
uint64_t getMaterialHash(const physis_Material &mat);
void calculateBoneInversePose(physis_Skeleton &skeleton, physis_Bone &bone, physis_Bone *parent_bone);
void calculateBone(physis_Skeleton &skeleton, physis_Bone &bone, const physis_Bone *parent_bone);
@ -97,6 +99,7 @@ private:
FileCache &cache;
std::vector<DrawObject> models;
std::unordered_map<uint64_t, RenderMaterial> renderMaterialCache;
RenderManager *renderer = nullptr;
VulkanWindow *vkWindow = nullptr;

View file

@ -18,7 +18,7 @@ enum class MaterialType { Object, Skin };
struct RenderMaterial {
physis_Material mat;
MaterialType type = MaterialType::Object;
physis_SHPK shaderPackage;
physis_SHPK shaderPackage{};
std::optional<Texture> diffuseTexture;
std::optional<Texture> normalTexture;