Browse Source

Added resource management methods to Material & Shader

Marko Pintera 13 years ago
parent
commit
b64de5a14d

+ 5 - 2
CamelotClient/CamelotClient.cpp

@@ -163,7 +163,7 @@ int CALLBACK WinMain(
 	gResources().create(fragProgRef, "C:\\fragProgCg.vprog", true);
 	gResources().create(fragProgRef, "C:\\fragProgCg.vprog", true);
 	fragProgRef = static_resource_cast<HighLevelGpuProgram>(gResources().load("C:\\fragProgCg.vprog"));
 	fragProgRef = static_resource_cast<HighLevelGpuProgram>(gResources().load("C:\\fragProgCg.vprog"));
 
 
-	ShaderPtr testShader = ShaderPtr(new Shader("TestShader"));
+	ShaderPtr testShader = Shader::create("TestShader");
 	testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
 	testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
 	testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
 	testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
 	testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
 	testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
@@ -186,13 +186,16 @@ int CALLBACK WinMain(
 	newPassDX11->setVertexProgram(vertProgRef);
 	newPassDX11->setVertexProgram(vertProgRef);
 	newPassDX11->setFragmentProgram(fragProgRef);
 	newPassDX11->setFragmentProgram(fragProgRef);
 
 
-	MaterialHandle testMaterial = MaterialPtr(new Material());
+	MaterialHandle testMaterial = Material::create();
+	testMaterial.waitUntilLoaded(); // TODO - Material doesn't do anything GPU specific, so technically it should be possible to initialize on the spot
+	// but is that a good idea?
 	testMaterial->setShader(testShader);
 	testMaterial->setShader(testShader);
 
 
 	testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
 	testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
 
 
 	gResources().create(testMaterial, "C:\\testMaterial.mat", true);
 	gResources().create(testMaterial, "C:\\testMaterial.mat", true);
 	testMaterial = static_resource_cast<MaterialHandle>(gResources().load("C:\\testMaterial.mat"));
 	testMaterial = static_resource_cast<MaterialHandle>(gResources().load("C:\\testMaterial.mat"));
+	testMaterial.waitUntilLoaded();
 
 
 	/*TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));*/
 	/*TextureRef testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ImportTest.tga"));*/
 	TextureHandle testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ArenaTowerDFS.psd"));
 	TextureHandle testTex = static_resource_cast<Texture>(Importer::instance().import("C:\\ArenaTowerDFS.psd"));

+ 3 - 7
CamelotRenderer/Include/CmMaterial.h

@@ -31,13 +31,6 @@ namespace CamelotEngine
 	class CM_EXPORT Material : public Resource
 	class CM_EXPORT Material : public Resource
 	{
 	{
 	public:
 	public:
-		Material();
-
-		/**
-		 * @brief	Overridden from Resource.
-		 */
-		virtual void initialize_internal();
-
 		/**
 		/**
 		 * @brief	Sets a shader that will be used by the material. 
 		 * @brief	Sets a shader that will be used by the material. 
 		 * 			Shaders best technique will be retrieved and used in all subsequent Material
 		 * 			Shaders best technique will be retrieved and used in all subsequent Material
@@ -68,6 +61,7 @@ namespace CamelotEngine
 
 
 		PassParametersPtr getPassParameters(UINT32 passIdx) const;
 		PassParametersPtr getPassParameters(UINT32 passIdx) const;
 
 
+		static MaterialPtr create();
 	private:
 	private:
 		ShaderPtr mShader;
 		ShaderPtr mShader;
 		TechniquePtr mBestTechnique;
 		TechniquePtr mBestTechnique;
@@ -88,6 +82,8 @@ namespace CamelotEngine
 		map<String, TextureHandle>::type mTextureValues;
 		map<String, TextureHandle>::type mTextureValues;
 		map<String, SamplerStateHandle>::type mSamplerValues;
 		map<String, SamplerStateHandle>::type mSamplerValues;
 
 
+		Material();
+
 		void throwIfNotInitialized() const;
 		void throwIfNotInitialized() const;
 
 
 		template <typename T>
 		template <typename T>

+ 1 - 1
CamelotRenderer/Include/CmMaterialRTTI.h

@@ -494,7 +494,7 @@ namespace CamelotEngine
 
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
 		{
-			return std::shared_ptr<Material>(new Material());
+			return Material::create();
 		}
 		}
 	};
 	};
 }
 }

+ 4 - 12
CamelotRenderer/Include/CmShader.h

@@ -42,13 +42,6 @@ namespace CamelotEngine
 	class CM_EXPORT Shader : public Resource
 	class CM_EXPORT Shader : public Resource
 	{
 	{
 	public:
 	public:
-		Shader(const String& name);
-
-		/**
-		 * @brief	Inherited from Resource.
-		 */
-		virtual void initialize_internal();
-
 		TechniquePtr addTechnique(const String& renderSystem, const String& renderer);
 		TechniquePtr addTechnique(const String& renderSystem, const String& renderer);
 		
 		
 		void removeTechnique(UINT32 idx);
 		void removeTechnique(UINT32 idx);
@@ -81,6 +74,8 @@ namespace CamelotEngine
 		static bool isSampler(GpuParamObjectType type);
 		static bool isSampler(GpuParamObjectType type);
 		static bool isTexture(GpuParamObjectType type);
 		static bool isTexture(GpuParamObjectType type);
 		static bool isBuffer(GpuParamObjectType type);
 		static bool isBuffer(GpuParamObjectType type);
+
+		static ShaderPtr create(const String& name);
 	private:
 	private:
 		String mName;
 		String mName;
 		vector<TechniquePtr>::type mTechniques;
 		vector<TechniquePtr>::type mTechniques;
@@ -89,15 +84,12 @@ namespace CamelotEngine
 		map<String, SHADER_OBJECT_PARAM_DESC>::type mObjectParams;
 		map<String, SHADER_OBJECT_PARAM_DESC>::type mObjectParams;
 		map<String, SHADER_PARAM_BLOCK_DESC>::type mParamBlocks;
 		map<String, SHADER_PARAM_BLOCK_DESC>::type mParamBlocks;
 
 
+		Shader(const String& name);
+
 		/************************************************************************/
 		/************************************************************************/
 		/* 								RTTI		                     		*/
 		/* 								RTTI		                     		*/
 		/************************************************************************/
 		/************************************************************************/
 		
 		
-		/**
-		 * @brief	Serialization only
-		 */
-		Shader() {}
-
 	public:
 	public:
 		friend class ShaderRTTI;
 		friend class ShaderRTTI;
 		static RTTITypeBase* getRTTIStatic();
 		static RTTITypeBase* getRTTIStatic();

+ 1 - 1
CamelotRenderer/Include/CmShaderRTTI.h

@@ -219,7 +219,7 @@ namespace CamelotEngine
 
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
 		{
-			return std::shared_ptr<Shader>(new Shader());
+			return Shader::create("");
 		}
 		}
 	};
 	};
 }
 }

+ 9 - 6
CamelotRenderer/Source/CmMaterial.cpp

@@ -15,13 +15,7 @@ namespace CamelotEngine
 {
 {
 	Material::Material()
 	Material::Material()
 	{
 	{
-		// Material doesn't do anything render thread specific, so we can just initialize right away
-		initialize_internal();
-	}
 
 
-	void Material::initialize_internal()
-	{
-		Resource::initialize_internal();
 	}
 	}
 
 
 	void Material::setShader(ShaderPtr shader)
 	void Material::setShader(ShaderPtr shader)
@@ -818,6 +812,15 @@ namespace CamelotEngine
 		return iterFind->second.at(arrayIdx);
 		return iterFind->second.at(arrayIdx);
 	}
 	}
 
 
+	MaterialPtr Material::create()
+	{
+		MaterialPtr newMat(new Material());
+		newMat->setThisPtr(newMat);
+		newMat->initialize();
+
+		return newMat;
+	}
+
 	RTTITypeBase* Material::getRTTIStatic()
 	RTTITypeBase* Material::getRTTIStatic()
 	{
 	{
 		return MaterialRTTI::instance();
 		return MaterialRTTI::instance();

+ 9 - 3
CamelotRenderer/Source/CmShader.cpp

@@ -12,9 +12,6 @@ namespace CamelotEngine
 
 
 	}
 	}
 
 
-	void Shader::initialize_internal()
-	{	}
-
 	TechniquePtr Shader::addTechnique(const String& renderSystem, const String& renderer)
 	TechniquePtr Shader::addTechnique(const String& renderSystem, const String& renderer)
 	{
 	{
 		TechniquePtr technique = TechniquePtr(new Technique(renderSystem, renderer));
 		TechniquePtr technique = TechniquePtr(new Technique(renderSystem, renderer));
@@ -203,6 +200,15 @@ namespace CamelotEngine
 		return false;
 		return false;
 	}
 	}
 
 
+	ShaderPtr Shader::create(const String& name)
+	{
+		ShaderPtr newShader(new Shader(name));
+		newShader->setThisPtr(newShader);
+		newShader->initialize();
+
+		return newShader;
+	}
+
 	RTTITypeBase* Shader::getRTTIStatic()
 	RTTITypeBase* Shader::getRTTIStatic()
 	{
 	{
 		return ShaderRTTI::instance();
 		return ShaderRTTI::instance();

+ 4 - 4
CamelotRenderer/TODO.txt

@@ -41,10 +41,10 @@ Stuff that needs to derive from CoreGpuObject:
  - GpuParamBlock
  - GpuParamBlock
 ---
 ---
  Maybe
  Maybe
-  - Material
-  - Pass
-  - Shader
-  - Technique
+  - Material (DONE)
+  - Pass (NOT REQUIRED)
+  - Shader (DONE)
+  - Technique (NOT REQUIRED)
 
 
 Test if async loading still works
 Test if async loading still works