Explorar el Código

- Making MaterialRuntimeVariable COW
- Work on the SceneDrawer

Panagiotis Christopoulos Charitos hace 14 años
padre
commit
642d6ae6c7

+ 93 - 7
anki/renderer/SceneDrawer.cpp

@@ -14,6 +14,31 @@
 namespace anki {
 
 
+//==============================================================================
+boost::array<const char*, SceneDrawer::B_NUM> SceneDrawer::buildinsTxt = {{
+	// Matrices
+	"modelMat",
+	"viewMat",
+	"projectionMat",
+	"modelViewMat",
+	"viewProjectionMat",
+	"normalMat",
+	"modelViewProjectionMat",
+	// FAIs (for materials in blending stage)
+	"msNormalFai",
+	"msDiffuseFai",
+	"msSpecularFai",
+	"msDepthFai",
+	"isFai",
+	"ppsPrePassFai",
+	"ppsPostPassFai",
+	// Other
+	"rendererSize",
+	"sceneAmbientColor",
+	"blurring"
+}};
+
+
 //==============================================================================
 SceneDrawer::UsrDefVarVisitor::UsrDefVarVisitor(
 	const MaterialRuntimeVariable& udvr_,
@@ -107,6 +132,74 @@ void SceneDrawer::setupShaderProg(
 			Mat4::combineTransformations(viewMat, modelMat);
 	}
 
+	// normalMat?
+	if(mtl.variableExistsAndInKey("normalMat", pt))
+	{
+		normalMat = modelViewMat.getRotationPart();
+	}
+
+	// modelViewProjectionMat?
+	if(mtl.variableExistsAndInKey("modelViewProjectionMat", pt))
+	{
+		modelViewProjectionMat = projectionMat * modelViewMat;
+	}
+
+	//
+	// Set the buildinId
+	//
+	BOOST_FOREACH(const MaterialRuntimeVariable& mrvar, mtlr.getVariables())
+	{
+		const MaterialVariable& mvar = mrvar.getMaterialVariable();
+
+		// Skip some
+		if(!mvar.inPass(key))
+		{
+			continue;
+		}
+
+		// Set the buildinId for all
+		if(mrvar.getBuildinId() == -1)
+		{
+			for(uint i = 0; i < B_NUM; i++)
+			{
+				if(mvar.getName() == buildinsTxt[i])
+				{
+					mrvar.setBuildinId(i);
+					break;
+				}
+			}
+
+			// If still -1 thn set it as B_NUM
+			if(mrvar.getBuildinId() == -1)
+			{
+				mrvar.setBuildinId(B_NUM);
+			}
+		}
+
+		// Sanity checks
+		if(mrvar.getBuildinId() == B_NUM && !mvar.getInitialized())
+		{
+			ANKI_WARNING("Uninitialized variable: " << mvar.getName());
+		}
+
+		if(mrvar.getBuildinId() != B_NUM && mvar.getInitialized())
+		{
+			ANKI_WARNING("The renderer will override the given value for: " <<
+				mvar.getName());
+		}
+
+		// Big switch
+		switch(mrvar.getBuildinId())
+		{
+			case B_MODEL_MAT:
+				mvar.getShaderProgramUniformVariable(key).set(modelMat);
+				break;
+			case B_VIEW_MAT:
+				break;
+		}
+	}
+
+
 	// set matrices
 	if(mtl.variableExistsAndInKey("modelMat", pt))
 	{
@@ -284,11 +377,4 @@ void SceneDrawer::renderRenderableNode(const RenderableNode& node,
 }
 
 
-//==============================================================================
-void SceneDrawer::setTheBuildins(MaterialRuntime& m)
-{
-
-}
-
-
 } // end namespace

+ 36 - 43
anki/renderer/SceneDrawer.h

@@ -34,58 +34,53 @@ public:
 private:
 	/// Standard attribute variables that are acceptable inside the
 	/// ShaderProgram
-	enum ShaderProgramVariable_
+	enum Buildins
 	{
-			// Attributes
-			SPV_POSITION,
-			SPV_TANGENT,
-			SPV_NORMAL,
-			SPV_TEX_COORDS,
-			// Uniforms
-			// Matrices
-			SPV_MODEL_MAT,
-			SPV_VIEW_MAT,
-			SPV_PROJECTION_MAT,
-			SPV_MODELVIEW_MAT,
-			SPV_VIEWPROJECTION_MAT,
-			SPV_NORMAL_MAT,
-			SPV_MODELVIEWPROJECTION_MAT,
-			// FAIs (for materials in blending stage)
-			SPV_MS_NORMAL_FAI,
-			SPV_MS_DIFFUSE_FAI,
-			SPV_MS_SPECULAR_FAI,
-			SPV_MS_DEPTH_FAI,
-			SPV_IS_FAI,
-			SPV_PPS_PRE_PASS_FAI,
-			SPV_PPS_POST_PASS_FAI,
-			// Other
-			SPV_RENDERER_SIZE,
-			SPV_SCENE_AMBIENT_COLOR,
-			SPV_BLURRING,
-			// num
-			SPV_NUM ///< The number of all buildin variables
+		// Matrices
+		B_MODEL_MAT,
+		B_VIEW_MAT,
+		B_PROJECTION_MAT,
+		B_MODELVIEW_MAT,
+		B_VIEWPROJECTION_MAT,
+		B_NORMAL_MAT,
+		B_MODELVIEWPROJECTION_MAT,
+		// FAIs (for materials in blending stage)
+		B_MS_NORMAL_FAI,
+		B_MS_DIFFUSE_FAI,
+		B_MS_SPECULAR_FAI,
+		B_MS_DEPTH_FAI,
+		B_IS_FAI,
+		B_PPS_PRE_PASS_FAI,
+		B_PPS_POST_PASS_FAI,
+		// Other
+		B_RENDERER_SIZE,
+		B_SCENE_AMBIENT_COLOR,
+		B_BLURRING,
+		// num
+		B_NUM ///< The number of all buildin variables
 	};
 
 	/// Set the uniform using this visitor
 	class UsrDefVarVisitor: public boost::static_visitor<>
 	{
-		public:
-			const MaterialRuntimeVariable& udvr;
-			const Renderer& r;
-			const PassLevelKey& key;
-			uint& texUnit;
+	public:
+		const MaterialRuntimeVariable& udvr;
+		const Renderer& r;
+		const PassLevelKey& key;
+		uint& texUnit;
 
-			UsrDefVarVisitor(const MaterialRuntimeVariable& udvr,
-				const Renderer& r, const PassLevelKey& key, uint& texUnit);
+		UsrDefVarVisitor(const MaterialRuntimeVariable& udvr,
+			const Renderer& r, const PassLevelKey& key, uint& texUnit);
 
-			/// Functor
-			template<typename Type>
-			void operator()(const Type& x) const;
+		/// Functor
+		template<typename Type>
+		void operator()(const Type& x) const;
 
-			/// Functor
-			void operator()(const TextureResourcePointer* x) const;
+		/// Functor
+		void operator()(const TextureResourcePointer* x) const;
 	};
 
+	static boost::array<const char*, B_NUM> buildinsTxt;
 	const Renderer& r; ///< Keep it here cause the class wants a few stuff
 					   ///< from it
 
@@ -105,8 +100,6 @@ private:
 		const Camera& cam,
 		const Renderer& r,
 		float blurring);
-
-	void setTheBuildins();
 };
 
 

+ 0 - 1
anki/resource/ResourceManager.inl.h

@@ -1,7 +1,6 @@
 #include "anki/resource/ResourceManager.h"
 #include "anki/util/Exception.h"
 #include "anki/util/Assert.h"
-#include <iostream>
 
 
 namespace anki {

+ 12 - 58
anki/scene/MaterialRuntime.cpp

@@ -14,22 +14,9 @@ namespace anki {
 //==============================================================================
 
 //==============================================================================
-template <>
-void MaterialRuntimeVariable::ConstructVisitor::
-	operator()<TextureResourcePointer >(const TextureResourcePointer& x) const
-{
-	var.data = &x;
-}
-
-
-//==============================================================================
-MaterialRuntimeVariable::MaterialRuntimeVariable(
-	const MaterialVariable& mvar_)
-	: mvar(mvar_), buildinId(-1)
-{
-	// Initialize the data using a visitor
-	boost::apply_visitor(ConstructVisitor(*this), mvar.getVariant());
-}
+MaterialRuntimeVariable::MaterialRuntimeVariable(const MaterialVariable& mv)
+	: mvar(mv), buildinId(-1)
+{}
 
 
 //==============================================================================
@@ -37,56 +24,23 @@ MaterialRuntimeVariable::~MaterialRuntimeVariable()
 {}
 
 
-
 //==============================================================================
-template<>
-MaterialRuntimeVariable::ConstPtrRsrcPtrTexture&
-	MaterialRuntimeVariable::getValue<
-	MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>()
+const MaterialRuntimeVariable::Variant&
+	MaterialRuntimeVariable::getVariantConst() const
 {
-	throw ANKI_EXCEPTION("You shouldn't call this getter");
-	return boost::get<ConstPtrRsrcPtrTexture>(data);
+	return copyVariant ? *copyVariant : mvar.getVariant();
 }
 
 
 //==============================================================================
-template<>
-void MaterialRuntimeVariable::setValue<
-	MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>(
-	const ConstPtrRsrcPtrTexture& v)
-{
-	throw ANKI_EXCEPTION("You shouldn't call this setter");
-	boost::get<ConstPtrRsrcPtrTexture>(data) = v;
-}
-
-//==============================================================================
-template<typename Type>
-void MaterialRuntimeVariable::SetUniformVisitor::operator()(
-	const Type& x) const
+MaterialRuntimeVariable::Variant& MaterialRuntimeVariable::getVariantMutable()
 {
-	uni.set(x);
-}
-
-
-//==============================================================================
-template<>
-void MaterialRuntimeVariable::SetUniformVisitor::
-	operator()<MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>(
-	const ConstPtrRsrcPtrTexture& x) const
-{
-	uni.set(*(x->get()), texUnit);
-	++texUnit;
-}
-
-
-//==============================================================================
-void MaterialRuntimeVariable::setUniformVariable(const PassLevelKey& k,
-	uint& texUnit)
-{
-	const ShaderProgramUniformVariable& uni =
-		mvar.getShaderProgramUniformVariable(k);
+	if(copyVariant.get() == NULL)
+	{
+		copyVariant.reset(new Variant(mvar.getVariant()));
+	}
 
-	boost::apply_visitor(SetUniformVisitor(uni, texUnit), data);
+	return *copyVariant;
 }
 
 

+ 18 - 72
anki/scene/MaterialRuntime.h

@@ -18,18 +18,15 @@ class MaterialVariable;
 
 
 /// Variable of runtime materials
-/// This holds a copy of the MtlUserDefinedVar's data in order to be changed
+/// This holds a copy of the MaterialVariable's data in order to be read write
 /// inside the main loop
 class MaterialRuntimeVariable
 {
 public:
-	typedef const TextureResourcePointer* ConstPtrRsrcPtrTexture;
-
 	/// The data union. The Texture resource is read-only at runtime
 	/// Don't EVER replace the texture with const Texture*. The asynchronous
 	/// operations will fail
-	typedef boost::variant<float, Vec2, Vec3, Vec4, Mat3,
-		Mat4, ConstPtrRsrcPtrTexture> Variant;
+	typedef MaterialVariable::Variant Variant;
 
 	/// Constructor. Initialize using a material variable
 	MaterialRuntimeVariable(const MaterialVariable& mv);
@@ -44,13 +41,13 @@ public:
 		return mvar;
 	}
 
-	const Variant& getDataVariant() const
+	const Variant& getVariant() const
 	{
-		return data;
+		return getVariantConst();
 	}
-	Variant& getDataVariant()
+	Variant& getVariant()
 	{
-		return data;
+		return getVariantMutable();
 	}
 
 	/// Get the value of the variant
@@ -59,22 +56,20 @@ public:
 	template<typename Type>
 	const Type& getValue() const
 	{
-		return boost::get<Type>(data);
+		return boost::get<Type>(getVariantConst());
 	}
 
-	/// Get the value of the variant
-	/// @exception boost::exception when you try to get the incorrect data
-	/// type
+	/// @copydoc getValue
 	template<typename Type>
 	Type& getValue()
 	{
-		return boost::get<Type>(data);
+		return boost::get<Type>(getVariantMutable());
 	}
 
 	template<typename Type>
 	void setValue(const Type& v)
 	{
-		boost::get<Type>(data) = v;
+		boost::get<Type>(getVariantMutable()) = v;
 	}
 
 	int getBuildinId() const
@@ -91,67 +86,18 @@ public:
 	}
 	/// @}
 
-	/// Call one of the setters of the uniform variable
-	void setUniformVariable(const PassLevelKey& k, uint& texUnif);
-
 private:
-	/// Initialize the data using a visitor
-	class ConstructVisitor: public boost::static_visitor<void>
-	{
-	public:
-		MaterialRuntimeVariable& var;
-
-		ConstructVisitor(MaterialRuntimeVariable& var_)
-			: var(var_)
-		{}
-
-		/// Template method that applies to all Variant values except texture
-		/// resource
-		template<typename Type>
-		void operator()(const Type& x) const
-		{
-			var.getDataVariant() = x;
-		}
-	};
-
-	/// Set visitor
-	class SetUniformVisitor: public boost::static_visitor<void>
-	{
-	public:
-		const ShaderProgramUniformVariable& uni;
-		uint& texUnit;
-
-		SetUniformVisitor(const ShaderProgramUniformVariable& uni_,
-			uint& texUnit_)
-			: uni(uni_), texUnit(texUnit_)
-		{}
-
-		template<typename Type>
-		void operator()(const Type& x) const;
-	};
-
 	const MaterialVariable& mvar; ///< Know the resource
-	Variant data; /// The data
-	int buildinId;
-};
-
+	boost::scoped_ptr<Variant> copyVariant; /// The copy of the data
+	int buildinId; ///< -1 is "dont know yet"
 
-// Declare specialized
-template <>
-void MaterialRuntimeVariable::ConstructVisitor::
-	operator()<TextureResourcePointer >(const TextureResourcePointer& x) const;
+	/// Return either the mvar's variant or your own if exists
+	const Variant& getVariantConst() const;
 
-// Declare specialized
-template<>
-MaterialRuntimeVariable::ConstPtrRsrcPtrTexture&
-	MaterialRuntimeVariable::getValue<
-	MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>();
-
-// Declare specialized
-template<>
-void MaterialRuntimeVariable::setValue<
-	MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>(
-	const ConstPtrRsrcPtrTexture& v);
+	/// Implement the COW technique. If someone asks the variant for writing
+	/// then create a copy
+	Variant& getVariantMutable();
+};
 
 
 /// One layer above the material resource