Panagiotis Christopoulos Charitos 11 سال پیش
والد
کامیت
a343045664

+ 2 - 2
include/anki/resource/Mesh.h

@@ -97,7 +97,7 @@ public:
 	Bool isCompatible(const Mesh& other) const;
 
 	/// Load from a .mesh file
-	void load(const char* filename, ResourceInitializer& init);
+	void load(const CString& filename, ResourceInitializer& init);
 
 protected:
 	/// Per sub mesh data
@@ -137,7 +137,7 @@ public:
 	{}
 
 	/// Load from a .mmesh file
-	void load(const char* filename, ResourceInitializer& init);
+	void load(const CString& filename, ResourceInitializer& init);
 };
 
 } // end namespace anki

+ 2 - 2
include/anki/resource/MeshLoader.h

@@ -78,7 +78,7 @@ public:
 
 	MeshLoader(TempResourceAllocator<U8>& alloc);
 
-	MeshLoader(const char* filename, TempResourceAllocator<U8>& alloc);
+	MeshLoader(const CString& filename, TempResourceAllocator<U8>& alloc);
 
 	~MeshLoader()
 	{}
@@ -125,7 +125,7 @@ public:
 
 	/// Load the mesh data from a binary file
 	/// @exception Exception
-	void load(const char* filename);
+	void load(const CString& filename);
 
 private:
 	MLVector<Vec3> m_positions; ///< Loaded from file

+ 19 - 11
include/anki/resource/Model.h

@@ -6,7 +6,7 @@
 #ifndef ANKI_RESOURCE_MODEL_H
 #define ANKI_RESOURCE_MODEL_H
 
-#include "anki/resource/Resource.h"
+#include "anki/resource/ResourceManager.h"
 #include "anki/Gl.h"
 #include "anki/collision/Obb.h"
 #include "anki/resource/RenderingKey.h"
@@ -26,6 +26,11 @@ public:
 	virtual ~ModelPatchBase()
 	{}
 
+	ModelPatchBase(ResourceAllocator<U8>& alloc)
+	:	m_vertJobs(alloc),
+		m_meshes(alloc)
+	{}
+
 	const Material& getMaterial() const
 	{
 		ANKI_ASSERT(m_mtl);
@@ -76,12 +81,12 @@ public:
 
 protected:
 	/// Array [lod][pass]
-	Vector<GlCommandBufferHandle> m_vertJobs;
+	ResourceVector<GlCommandBufferHandle> m_vertJobs;
 	Material* m_mtl = nullptr;
-	Vector<Mesh*> m_meshes; ///< One for each LOD
+	ResourceVector<Mesh*> m_meshes; ///< One for each LOD
 
 	/// Create vertex descriptors using a material and a mesh
-	void create();
+	void create(GlDevice* gl);
 
 private:
 	/// Called by @a create multiple times to create and populate a single
@@ -104,14 +109,17 @@ class ModelPatch: public ModelPatchBase
 {
 public:
 	/// Accepts a number of mesh filenames, one for each LOD
-	ModelPatch(const char* meshFNames[], U32 meshesCount,
-		const char* mtlFName);
+	ModelPatch(
+		CString meshFNames[], 
+		U32 meshesCount,
+		const CString& mtlFName, 
+		ResourceManager* resources);
 
 	~ModelPatch()
 	{}
 
 private:
-	Vector<MeshResourcePointerType> m_meshResources; ///< The geometries
+	ResourceVector<MeshResourcePointerType> m_meshResources; ///< Geometries
 	MaterialResourcePointer m_mtlResource; ///< Material
 };
 
@@ -158,7 +166,7 @@ public:
 
 	/// @name Accessors
 	/// @{
-	const Vector<ModelPatchBase*>& getModelPatches() const
+	const ResourceVector<ModelPatchBase*>& getModelPatches() const
 	{
 		return m_modelPatches;
 	}
@@ -169,14 +177,14 @@ public:
 	}
 	/// @}
 
-	void load(const char* filename);
+	void load(const CString& filename, ResourceInitializer& init);
 
 private:
 	/// The vector of ModelPatch
-	Vector<ModelPatchBase*> m_modelPatches;
+	ResourceVector<ModelPatchBase*> m_modelPatches;
 	Obb m_visibilityShape;
 	SkeletonResourcePointer m_skeleton;
-	Vector<AnimationResourcePointer> m_animations;
+	ResourceVector<AnimationResourcePointer> m_animations;
 };
 
 } // end namespace anki

+ 0 - 19
include/anki/resource/Resource.h

@@ -11,25 +11,6 @@
 
 namespace anki {
 
-#define ANKI_RESOURCE_TYPEDEFS(rsrc_, name_) \
-	class rsrc_; \
-	using name_ = ResourcePointer<rsrc_, ResourceManager>;
-
-ANKI_RESOURCE_TYPEDEFS(Animation, AnimationResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(TextureResource, TextureResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(ProgramResource, ProgramResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(Material, MaterialResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(Mesh, MeshResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(BucketMesh, BucketMeshResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(Skeleton, SkeletonResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(SkelAnim, SkelAnimResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(LightRsrc, LightRsrcResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(ParticleEmitterResource, ParticleEmitterResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(Script, ScriptResourcePointer)
-ANKI_RESOURCE_TYPEDEFS(Model, ModelResourcePointer)
-
-#undef ANKI_RESOURCE_TYPEDEFS
-
 } // end namespace anki
 
 #endif

+ 37 - 17
include/anki/resource/ResourceManager.h

@@ -18,15 +18,26 @@ namespace anki {
 class ConfigSet;
 class App;
 class GlDevice;
-
-class Animation;
-class Material;
-class Mesh;
-class Model;
-class BucketMesh;
-class ProgramResource;
-class ParticleEmitterResource;
-class TextureResource;
+class ResourceManager;
+
+#define ANKI_RESOURCE(rsrc_, name_) \
+	class rsrc_; \
+	using name_ = ResourcePointer<rsrc_, ResourceManager>;
+
+ANKI_RESOURCE(Animation, AnimationResourcePointer)
+ANKI_RESOURCE(TextureResource, TextureResourcePointer)
+ANKI_RESOURCE(ProgramResource, ProgramResourcePointer)
+ANKI_RESOURCE(Material, MaterialResourcePointer)
+ANKI_RESOURCE(Mesh, MeshResourcePointer)
+ANKI_RESOURCE(BucketMesh, BucketMeshResourcePointer)
+ANKI_RESOURCE(Skeleton, SkeletonResourcePointer)
+ANKI_RESOURCE(SkelAnim, SkelAnimResourcePointer)
+ANKI_RESOURCE(LightRsrc, LightRsrcResourcePointer)
+ANKI_RESOURCE(ParticleEmitterResource, ParticleEmitterResourcePointer)
+ANKI_RESOURCE(Script, ScriptResourcePointer)
+ANKI_RESOURCE(Model, ModelResourcePointer)
+
+#undef ANKI_RESOURCE
 
 /// @addtogroup resource
 /// @{
@@ -109,16 +120,23 @@ private:
 	}
 };
 
+#define ANKI_RESOURCE(type_) \
+	public TypeResourceManager<type_, ResourceManager>
+
 /// Resource manager. It holds a few global variables
 class ResourceManager: 
-	public TypeResourceManager<Animation, ResourceManager>,
-	public TypeResourceManager<Material, ResourceManager>,
-	public TypeResourceManager<Mesh, ResourceManager>,
-	public TypeResourceManager<Model, ResourceManager>,
-	public TypeResourceManager<BucketMesh, ResourceManager>,
-	public TypeResourceManager<ProgramResource, ResourceManager>,
-	public TypeResourceManager<ParticleEmitterResource, ResourceManager>,
-	public TypeResourceManager<TextureResource, ResourceManager>
+	ANKI_RESOURCE(Animation),
+	ANKI_RESOURCE(TextureResource),
+	ANKI_RESOURCE(ProgramResource),
+	ANKI_RESOURCE(Material),
+	ANKI_RESOURCE(Mesh),
+	ANKI_RESOURCE(BucketMesh),
+	ANKI_RESOURCE(Skeleton),
+	ANKI_RESOURCE(SkelAnim),
+	ANKI_RESOURCE(LightRsrc),
+	ANKI_RESOURCE(ParticleEmitterResource),
+	ANKI_RESOURCE(Script),
+	ANKI_RESOURCE(Model)
 {
 public:
 	ResourceManager(App* app, const ConfigSet& config);
@@ -192,6 +210,8 @@ private:
 	U32 m_textureAnisotropy;
 };
 
+#undef ANKI_RESOURCE
+
 /// @}
 
 } // end namespace anki

+ 8 - 7
src/resource/Mesh.cpp

@@ -4,6 +4,7 @@
 // http://www.anki3d.org/LICENSE
 
 #include "anki/resource/Mesh.h"
+#include "anki/resource/ResourceManager.h"
 #include "anki/resource/MeshLoader.h"
 #include "anki/util/Functions.h"
 #include "anki/misc/Xml.h"
@@ -22,7 +23,7 @@ Bool Mesh::isCompatible(const Mesh& other) const
 }
 
 //==============================================================================
-void Mesh::load(const char* filename, ResourceInitializer& init)
+void Mesh::load(const CString& filename, ResourceInitializer& init)
 {
 	try
 	{
@@ -112,7 +113,7 @@ void Mesh::createBuffers(const MeshLoader& loader,
 	}
 
 	// Create GL buffers
-	GlDevice& gl = init.m_gl;
+	GlDevice& gl = init.m_resources._getGlDevice();
 	GlCommandBufferHandle jobs(&gl);
 	
 	GlClientBufferHandle clientVertBuff(jobs, vbosize, &buff[0]);
@@ -224,12 +225,12 @@ void Mesh::getBufferInfo(const VertexAttribute attrib,
 //==============================================================================
 
 //==============================================================================
-void BucketMesh::load(const char* filename, ResourceInitializer& init)
+void BucketMesh::load(const CString& filename, ResourceInitializer& init)
 {
 	try
 	{
 		XmlDocument doc;
-		doc.loadFile(filename);
+		doc.loadFile(filename, init.m_tempAlloc);
 
 		XmlElement rootEl = doc.getChildElement("bucketMesh");
 		XmlElement meshesEl = rootEl.getChildElement("meshes");
@@ -244,7 +245,7 @@ void BucketMesh::load(const char* filename, ResourceInitializer& init)
 		U i = 0;
 		do
 		{
-			std::string subMeshFilename = meshEl.getText();
+			CString subMeshFilename = meshEl.getText();
 
 			// Load the submesh and if not the first load the append the 
 			// vertices to the fullMesh
@@ -259,7 +260,7 @@ void BucketMesh::load(const char* filename, ResourceInitializer& init)
 				}
 
 				// Load
-				subLoader.load(subMeshFilename.c_str());
+				subLoader.load(subMeshFilename);
 				loader = &subLoader;
 
 				// Sanity checks
@@ -282,7 +283,7 @@ void BucketMesh::load(const char* filename, ResourceInitializer& init)
 			else
 			{
 				// Load
-				fullLoader.load(subMeshFilename.c_str());
+				fullLoader.load(subMeshFilename);
 				loader = &fullLoader;
 
 				// Set properties

+ 4 - 3
src/resource/MeshLoader.cpp

@@ -68,14 +68,15 @@ MeshLoader::MeshLoader(TempResourceAllocator<U8>& alloc)
 {}
 
 //==============================================================================
-MeshLoader::MeshLoader(const char* filename, TempResourceAllocator<U8>& alloc)
-:	MeshLoader(filename, alloc)
+MeshLoader::MeshLoader(
+	const CString& filename, TempResourceAllocator<U8>& alloc)
+:	MeshLoader(alloc)
 {
 	load(filename);
 }
 
 //==============================================================================
-void MeshLoader::load(const char* filename)
+void MeshLoader::load(const CString& filename)
 {
 	// Try
 	try

+ 30 - 19
src/resource/Model.cpp

@@ -23,7 +23,8 @@ public:
 	VertexAttribute m_location;
 };
 
-static const Array<Attrib, (U)VertexAttribute::COUNT - 1> attribs = {{
+static const Array<Attrib, static_cast<U>(VertexAttribute::COUNT) - 1> 
+	attribs = {{
 	{"inPosition", VertexAttribute::POSITION},
 	{"inNormal", VertexAttribute::NORMAL},
 	{"inTangent", VertexAttribute::TANGENT},
@@ -69,7 +70,7 @@ void ModelPatchBase::createVertexDesc(
 		}
 		
 		vbo.bindVertexBuffer(vertexJobs, size, type, false, stride,
-			offset, (U)attrib.m_location);
+			offset, static_cast<U>(attrib.m_location));
 
 		++count;
 	}
@@ -160,7 +161,7 @@ void ModelPatchBase::getRenderingDataSub(
 }
 
 //==============================================================================
-void ModelPatchBase::create()
+void ModelPatchBase::create(GlDevice* gl)
 {
 	const Material& mtl = getMaterial();
 	U lodsCount = getLodsCount();
@@ -192,8 +193,7 @@ void ModelPatchBase::create()
 			prog = ppline.getAttachedProgram(GL_VERTEX_SHADER);
 			
 			// Create vert descriptor
-			GlDevice& gl = GlDeviceSingleton::get();
-			GlCommandBufferHandle vertJobs(&gl);
+			GlCommandBufferHandle vertJobs(gl);
 			createVertexDesc(prog, *mesh, vertJobs);
 
 			m_vertJobs[getVertexDescIdx(key)] = vertJobs;
@@ -230,16 +230,21 @@ U ModelPatchBase::getVertexDescIdx(const RenderingKey& key) const
 //==============================================================================
 template<typename MeshResourcePointerType>
 ModelPatch<MeshResourcePointerType>::ModelPatch(
-	const char* meshFNames[], U32 meshesCount, const char* mtlFName)
+	CString meshFNames[], 
+	U32 meshesCount, 
+	const CString& mtlFName,
+	ResourceManager* resources)
+:	ModelPatchBase(resources->_getAllocator()),
+	m_meshResources(resources->_getAllocator())
 {
 	ANKI_ASSERT(meshesCount > 0);
 	m_meshes.resize(meshesCount);
 	m_meshResources.resize(meshesCount);
 
 	// Load meshes
-	for(U32 i = 0; i < meshesCount; i++)
+	for(U i = 0; i < meshesCount; i++)
 	{
-		m_meshResources[i].load(meshFNames[i]);
+		m_meshResources[i].load(meshFNames[i], resources);
 		m_meshes[i] = m_meshResources[i].get();
 
 		// Sanity check
@@ -251,11 +256,11 @@ ModelPatch<MeshResourcePointerType>::ModelPatch(
 	}
 
 	// Load material
-	m_mtlResource.load(mtlFName);
+	m_mtlResource.load(mtlFName, resources);
 	m_mtl = m_mtlResource.get();
 
 	// Create VAOs
-	create();
+	create(&resources->_getGlDevice());
 }
 
 //==============================================================================
@@ -269,21 +274,23 @@ Model::Model()
 //==============================================================================
 Model::~Model()
 {
+	auto alloc = m_modelPatches.get_allocator();
+
 	for(ModelPatchBase* patch : m_modelPatches)
 	{
-		delete patch;
+		alloc.deleteInstance(patch);
 	}
 }
 
 //==============================================================================
-void Model::load(const char* filename)
+void Model::load(const CString& filename, ResourceInitializer& init)
 {
 	try
 	{
 		// Load
 		//
 		XmlDocument doc;
-		doc.loadFile(filename);
+		doc.loadFile(filename, init.m_tempAlloc);
 
 		XmlElement rootEl = doc.getChildElement("model");
 
@@ -291,7 +298,7 @@ void Model::load(const char* filename)
 		XmlElement collEl = rootEl.getChildElementOptional("collisionShape");
 		if(collEl)
 		{
-			std::string type = collEl.getChildElement("type").getText();
+			CString type = collEl.getChildElement("type").getText();
 			XmlElement valEl = collEl.getChildElement("value");
 			(void)valEl; // XXX
 
@@ -323,7 +330,7 @@ void Model::load(const char* filename)
 			XmlElement materialEl =
 				modelPatchEl.getChildElement("material");
 
-			Array<const char*, 3> meshesFnames;
+			Array<CString, 3> meshesFnames;
 			U meshesCount = 1;
 			ModelPatchBase* patch;
 
@@ -350,8 +357,10 @@ void Model::load(const char* filename)
 					meshesFnames[2] = meshEl2.getText();
 				}
 
-				patch = new ModelPatch<MeshResourcePointer>(
-					&meshesFnames[0], meshesCount, materialEl.getText());
+				patch = init.m_alloc.newInstance<
+					ModelPatch<MeshResourcePointer>>(
+					&meshesFnames[0], meshesCount, materialEl.getText(),
+					&init.m_resources);
 			}
 			else
 			{
@@ -376,8 +385,10 @@ void Model::load(const char* filename)
 					meshesFnames[2] = bmeshEl2.getText();
 				}
 
-				patch = new ModelPatch<BucketMeshResourcePointer>(
-					&meshesFnames[0], meshesCount, materialEl.getText());
+				patch = init.m_alloc.newInstance<
+					ModelPatch<BucketMeshResourcePointer>>(
+					&meshesFnames[0], meshesCount, materialEl.getText(),
+					&init.m_resources);
 			}
 
 			m_modelPatches.push_back(patch);

+ 1 - 1
src/resource/TextureResource.cpp

@@ -173,7 +173,7 @@ void TextureResource::loadInternal(const CString& filename,
 	init.m_repeat = true;
 
 	// anisotropyLevel
-	init.m_anisotropyLevel = rinit.m_resource.getTextureAnisotropy();
+	init.m_anisotropyLevel = rinit.m_resources.getTextureAnisotropy();
 
 	// genMipmaps
 	if(init.m_mipmapsCount == 1 || driverShouldGenMipmaps)