Skip to content

Commit 7766408

Browse files
committed
Implemented Material caching.
1 parent 37d55e9 commit 7766408

File tree

10 files changed

+61
-19
lines changed

10 files changed

+61
-19
lines changed

MP-APS/.vs/MP-APS/v15/.suo

1 KB
Binary file not shown.

MP-APS/.vs/MP-APS/v15/Browse.VC.db

88 KB
Binary file not shown.

MP-APS/Core/RenderSystem.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ void RenderSystem::renderModelsWithTextures(GLShaderProgram& shader, const std::
221221
const auto meshes = model->GetMeshes();
222222
for (const auto& mesh : meshes) {
223223
glActiveTexture(GL_TEXTURE3);
224-
glBindTexture(GL_TEXTURE_2D, mesh.Material.AlbedoMap);
224+
glBindTexture(GL_TEXTURE_2D, mesh.Material->AlbedoMap);
225225
glActiveTexture(GL_TEXTURE4);
226-
glBindTexture(GL_TEXTURE_2D, mesh.Material.NormalMap);
226+
glBindTexture(GL_TEXTURE_2D, mesh.Material->NormalMap);
227227
glActiveTexture(GL_TEXTURE5);
228-
glBindTexture(GL_TEXTURE_2D, mesh.Material.MetallicMap);
228+
glBindTexture(GL_TEXTURE_2D, mesh.Material->MetallicMap);
229229
glActiveTexture(GL_TEXTURE6);
230-
glBindTexture(GL_TEXTURE_2D, mesh.Material.RoughnessMap);
230+
glBindTexture(GL_TEXTURE_2D, mesh.Material->RoughnessMap);
231231
//glActiveTexture(GL_TEXTURE7);
232232
//glBindTexture(GL_TEXTURE_2D, mesh.Material.AOMap);
233233

MP-APS/Mesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indic
77
}
88

99
/***********************************************************************************/
10-
Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterial& material) :
10+
Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterialPtr& material) :
1111
IndexCount(indices.size()),
1212
Material(material) {
1313

MP-APS/Mesh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
/***********************************************************************************/
1111
struct Mesh {
1212
Mesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices);
13-
Mesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterial& material);
13+
Mesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterialPtr& material);
1414

1515
void Clear();
1616

1717
auto GetTriangleCount() const noexcept { return IndexCount / 3; }
1818

1919
const std::size_t IndexCount;
2020
GLVertexArray VAO;
21-
PBRMaterial Material;
21+
PBRMaterialPtr Material;
2222

2323
private:
2424
void setupMesh(const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices);

MP-APS/Model.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <iostream>
1010
#include <ppl.h>
11+
#include "ResourceManager.h"
1112

1213
/***********************************************************************************/
1314
Model::Model(const std::string_view Path, const std::string_view Name, const bool flipWindingOrder, const bool loadMaterial) : m_name(Name), m_path(Path) {
@@ -18,13 +19,13 @@ Model::Model(const std::string_view Path, const std::string_view Name, const boo
1819
}
1920

2021
/***********************************************************************************/
21-
Model::Model(const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterial& material) noexcept : m_name(Name) {
22+
Model::Model(const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterialPtr& material) noexcept : m_name(Name) {
2223
m_meshes.emplace_back(vertices, indices, material);
2324
}
2425

2526
/***********************************************************************************/
2627
Model::Model(const std::string_view Name, const Mesh& mesh) noexcept : m_name(Name) {
27-
m_meshes.push_back(std::move(mesh));
28+
m_meshes.push_back(mesh);
2829
}
2930

3031
/***********************************************************************************/
@@ -203,12 +204,17 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, const bool loadMater
203204
// http://assimp.sourceforge.net/lib_html/structai_material.html
204205
if (loadMaterial) {
205206
if (mesh->mMaterialIndex >= 0) {
206-
PBRMaterial material;
207207
const auto* mat = scene->mMaterials[mesh->mMaterialIndex];
208208

209209
aiString name;
210210
mat->Get(AI_MATKEY_NAME, name);
211211

212+
// Is the material cached?
213+
const auto cachedMaterial = ResourceManager::GetInstance().GetMaterial(name.C_Str());
214+
if (cachedMaterial.has_value()) {
215+
return Mesh(vertices, indices, cachedMaterial.value());
216+
}
217+
212218
// Get the first texture for each texture type we need
213219
// since there could be multiple textures per type
214220
aiString albedoPath;
@@ -226,20 +232,16 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, const bool loadMater
226232
aiString alphaMaskPath;
227233
mat->GetTexture(aiTextureType_OPACITY, 0, &alphaMaskPath);
228234

229-
std::cout << "MATERIAL NAME: " << name.C_Str() << std::endl;
230-
231-
material.Init(name.C_Str(),
235+
const auto newMaterial = ResourceManager::GetInstance().CacheMaterial(name.C_Str(),
232236
m_path + albedoPath.C_Str(),
233237
"",
234238
m_path + metallicPath.C_Str(),
235239
m_path + normalPath.C_Str(),
236240
m_path + roughnessPath.C_Str(),
237-
m_path + alphaMaskPath.C_Str()
238-
);
241+
m_path + alphaMaskPath.C_Str());
239242

240243
++m_numMats;
241-
std::cout << "NUMBER OF MATERIALS: " << m_numMats << std::endl;
242-
return Mesh(vertices, indices, material);
244+
return Mesh(vertices, indices, newMaterial);
243245
}
244246
}
245247

MP-APS/Model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Model {
1515
public:
1616
Model() = default;
1717
Model(const std::string_view Path, const std::string_view Name, const bool flipWindingOrder = false, const bool loadMaterial = true);
18-
Model(const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterial& material) noexcept;
18+
Model(const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterialPtr& material) noexcept;
1919
Model(const std::string_view Name, const Mesh& mesh) noexcept;
2020
virtual ~Model() = default;
2121

MP-APS/PBRMaterial.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <string_view>
4+
#include <memory>
45

56
// Material for a PBR pipeline
67
struct PBRMaterial {
@@ -26,4 +27,6 @@ struct PBRMaterial {
2627
unsigned int RoughnessMap;
2728
unsigned int AlphaMask;
2829
// unsigned int HeightMap;
29-
};
30+
};
31+
32+
using PBRMaterialPtr = std::shared_ptr<PBRMaterial>;

MP-APS/ResourceManager.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ ModelPtr ResourceManager::CacheModel(const std::string_view name, const Model mo
165165
return m_modelCache.try_emplace(name.data(), std::make_shared<Model>(model)).first->second;
166166
}
167167

168+
/***********************************************************************************/
169+
std::optional<PBRMaterialPtr> ResourceManager::GetMaterial(const std::string_view name) const {
170+
171+
// Check if material exists
172+
const auto val = m_materialCache.find(name.data());
173+
174+
if (val == m_materialCache.end()) {
175+
return std::optional<PBRMaterialPtr>();
176+
}
177+
178+
return std::make_optional<PBRMaterialPtr>(val->second);
179+
}
180+
181+
/***********************************************************************************/
182+
PBRMaterialPtr ResourceManager::CacheMaterial(const std::string_view name, const std::string_view albedoPath, const std::string_view aoPath, const std::string_view metallicPath, const std::string_view normalPath, const std::string_view roughnessPath, const std::string_view alphaMaskPath) {
183+
auto mat = PBRMaterial();
184+
mat.Init(name, albedoPath, aoPath, metallicPath, normalPath, roughnessPath, alphaMaskPath);
185+
186+
return m_materialCache.try_emplace(name.data(), std::make_shared<PBRMaterial>(mat)).first->second;
187+
}
188+
168189
/***********************************************************************************/
169190
void ResourceManager::UnloadModel(const std::string_view modelName) {
170191
auto model = m_modelCache.find(modelName.data());

MP-APS/ResourceManager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Model.h"
44

55
#include <unordered_map>
6+
#include <optional>
67

78
class ResourceManager {
89
ResourceManager() = default;
@@ -32,13 +33,28 @@ class ResourceManager {
3233
// Add a loaded model the the model cache
3334
ModelPtr CacheModel(const std::string_view name, const Model model, const bool overwriteIfExists = false);
3435

36+
// Checks if a named material is cached. If so, we get an initialized optional. Otherwise we get an empty one.
37+
// It's up to the caller to check the result.
38+
std::optional<PBRMaterialPtr> GetMaterial(const std::string_view name) const;
39+
// Cache a material and get a handle back. Usually called if GetMaterial returns empty.
40+
PBRMaterialPtr CacheMaterial(const std::string_view name,
41+
const std::string_view albedoPath,
42+
const std::string_view aoPath,
43+
const std::string_view metallicPath,
44+
const std::string_view normalPath,
45+
const std::string_view roughnessPath,
46+
const std::string_view alphaMaskPath);
47+
48+
3549
// Removes a named model from cache.
3650
void UnloadModel(const std::string_view modelName);
3751

3852
auto GetNumLoadedTextures() const noexcept { return m_textureCache.size(); }
3953
auto GetNumLoadedModels() const noexcept { return m_modelCache.size(); }
54+
auto GetNumMaterials() const noexcept { return m_materialCache.size(); }
4055

4156
private:
4257
std::unordered_map<std::string, ModelPtr> m_modelCache;
4358
std::unordered_map<std::string, unsigned int> m_textureCache;
59+
std::unordered_map<std::string, PBRMaterialPtr> m_materialCache;
4460
};

0 commit comments

Comments
 (0)