Sfoglia il codice sorgente

Clear material param dirty flags after update

BearishSun 9 anni fa
parent
commit
65006380e7

+ 13 - 11
Source/BansheeCore/Include/BsGpuParamsSet.h

@@ -88,7 +88,7 @@ namespace BansheeEngine
 
 	public:
 		TGpuParamsSet() {}
-		TGpuParamsSet(const SPtr<TechniqueType>& technique, const ShaderType& shader, UINT32 techniqueIdx, 
+		TGpuParamsSet(const SPtr<TechniqueType>& technique, const ShaderType& shader,
 			const SPtr<MaterialParamsType>& params);
 		~TGpuParamsSet();
 
@@ -162,18 +162,20 @@ namespace BansheeEngine
 		/**
 		 * Updates internal GPU params for all passes and stages from the provided material parameters object.
 		 *
-		 * @param[in]	params		Object containing the parameter data to update from. Layout of the object must match the
-		 *							object used for creating this object (be created for the same shader).
-		 * @param[in]	updateAll	By default the system will only update parameters marked as dirty in @p params. If this
-		 *							is set to true, all parameters will be updated instead.
+		 * @param[in]	params			Object containing the parameter data to update from. Layout of the object must match the
+		 *								object used for creating this object (be created for the same shader).
+		 * @param[in]	dirtyBitIdx		Index to use when checking if parameters are dirty. Must be in range [0, 31]. Allows
+		 *								the same material params to record dirty state for multiple sets of GPU params
+		 *								(each with their own index).
+		 * @param[in]	updateAll		By default the system will only update parameters marked as dirty in @p params. If this
+		 *								is set to true, all parameters will be updated instead.
 		 */
-		void update(const SPtr<MaterialParamsType>& params, bool updateAll = false);
+		void update(const SPtr<MaterialParamsType>& params, UINT32 dirtyBitIdx, bool updateAll = false);
 
 		static const UINT32 NUM_STAGES;
 	private:
 		template<bool Core2> friend class TMaterial;
 
-		UINT32 mTechniqueIdx;
 		Vector<PassParams> mPassParams;
 		Vector<BlockInfo> mBlocks;
 		Vector<DataParamInfo> mDataParamInfos;
@@ -185,9 +187,9 @@ namespace BansheeEngine
 	{
 	public:
 		GpuParamsSet() { }
-		GpuParamsSet(const SPtr<Technique>& technique, const HShader& shader, UINT32 techniqueIdx, 
+		GpuParamsSet(const SPtr<Technique>& technique, const HShader& shader, 
 			const SPtr<MaterialParams>& params)
-			:TGpuParamsSet(technique, shader, techniqueIdx, params)
+			:TGpuParamsSet(technique, shader, params)
 		{ }
 	};
 
@@ -196,9 +198,9 @@ namespace BansheeEngine
 	{
 	public:
 		GpuParamsSetCore() { }
-		GpuParamsSetCore(const SPtr<TechniqueCore>& technique, const SPtr<ShaderCore>& shader, UINT32 techniqueIdx,
+		GpuParamsSetCore(const SPtr<TechniqueCore>& technique, const SPtr<ShaderCore>& shader,
 			const SPtr<MaterialParamsCore>& params)
-			:TGpuParamsSet(technique, shader, techniqueIdx, params)
+			:TGpuParamsSet(technique, shader, params)
 		{ }
 	};
 

+ 5 - 1
Source/BansheeCore/Include/BsMaterial.h

@@ -147,10 +147,14 @@ namespace BansheeEngine
 		 * will not be able to track which parameters were updated since the last call.
 		 *
 		 * @param[in]	paramsSet		Parameter set to update.
+		 * @param[in]	dirtyBitIdx		Index to use when checking if parameters are dirty. Must be in range [0, 30]. Allows
+		 *								the same material params to record dirty state for multiple sets of GPU params
+		 *								(each with their own index). Dirty state for the specified index will be cleared
+		 *								after the call.
 		 * @param[in]	forceRefresh	If true all material parameters will be assigned to the params set, regardless if
 		 *								they are marked dirty or not.
 		 */
-		void updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, bool forceRefresh = false);
+		void updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, UINT32 dirtyBitIdx = 0, bool forceRefresh = false);
 
 		/**   
 		 * Assigns a float value to the shader parameter with the specified name. 

+ 9 - 3
Source/BansheeCore/Include/BsMaterialParams.h

@@ -136,10 +136,9 @@ namespace BansheeEngine
 			const ParamData** output) const;
 
 		/**
-		 * Returns information about a parameter at the specified global index, as retrieved by getParamIndex(). Returns
-		 * null if the index is out of range.
+		 * Returns information about a parameter at the specified global index, as retrieved by getParamIndex(). 
 		 */
-		const ParamData* getParamData(UINT32 index) const;
+		const ParamData* getParamData(UINT32 index) const { return &mParams[index]; }
 
 		/** Returns the total number of parameters managed by this object. */
 		UINT32 getNumParams() const { return (UINT32)mParams.size(); }
@@ -191,6 +190,13 @@ namespace BansheeEngine
 			return &mDataParamsBuffer[index];
 		}
 
+		/** 
+		 * Clears dirty flags for all parameters, for the specified index. 
+		 *
+		 * @param[in]	index	Index of the bit to clear. Must be in range [0-31]
+		 */
+		void clearDirtyFlags(UINT32 index);
+
 	protected:
 		const static UINT32 STATIC_BUFFER_SIZE = 256;
 

+ 10 - 10
Source/BansheeCore/Source/BsGpuParamsSet.cpp

@@ -415,9 +415,9 @@ namespace BansheeEngine
 	const UINT32 TGpuParamsSet<Core>::NUM_STAGES = 6;
 
 	template<bool Core>
-	TGpuParamsSet<Core>::TGpuParamsSet(const SPtr<TechniqueType>& technique, const ShaderType& shader, UINT32 techniqueIdx,
+	TGpuParamsSet<Core>::TGpuParamsSet(const SPtr<TechniqueType>& technique, const ShaderType& shader,
 		const SPtr<MaterialParamsType>& params)
-		:mPassParams(technique->getNumPasses()), mTechniqueIdx(techniqueIdx)
+		:mPassParams(technique->getNumPasses())
 	{
 		UINT32 numPasses = technique->getNumPasses();
 
@@ -757,15 +757,15 @@ namespace BansheeEngine
 	}
 
 	template<bool Core>
-	void TGpuParamsSet<Core>::update(const SPtr<MaterialParamsType>& params, bool updateAll)
+	void TGpuParamsSet<Core>::update(const SPtr<MaterialParamsType>& params, UINT32 dirtyBitIdx, bool updateAll)
 	{
 		// Note: Instead of iterating over every single parameter, it might be more efficient for @p params to keep
 		// a ring buffer and a version number. Then we could just iterate over the ring buffer and only access dirty
 		// parameters. If the version number is too high (larger than ring buffer can store), then we force update for all.
 
 		// Maximum of 31 techniques are supported. Bit 32 is reserved.
-		assert(mTechniqueIdx < 31);
-		UINT32 dirtyFlagCheck = 1 << mTechniqueIdx;
+		assert(dirtyBitIdx < 31);
+		UINT32 dirtyFlagMask = 1 << dirtyBitIdx;
 
 		// Update data params
 		for(auto& paramInfo : mDataParamInfos)
@@ -775,7 +775,7 @@ namespace BansheeEngine
 				continue;
 
 			const MaterialParams::ParamData* materialParamInfo = params->getParamData(paramInfo.paramIdx);
-			if ((materialParamInfo->dirtyFlags & dirtyFlagCheck) == 0 && !updateAll)
+			if ((materialParamInfo->dirtyFlags & dirtyFlagMask) == 0 && !updateAll)
 				continue;
 
 			UINT32 arraySize = materialParamInfo->arraySize == 0 ? 1 : materialParamInfo->arraySize;
@@ -883,7 +883,7 @@ namespace BansheeEngine
 						const ObjectParamInfo& paramInfo = stageInfo.textures[k];
 
 						const MaterialParams::ParamData* materialParamInfo = params->getParamData(paramInfo.paramIdx);
-						if ((materialParamInfo->dirtyFlags & dirtyFlagCheck) == 0 && !updateAll)
+						if ((materialParamInfo->dirtyFlags & dirtyFlagMask) == 0 && !updateAll)
 							continue;
 
 						TextureType texture;
@@ -897,7 +897,7 @@ namespace BansheeEngine
 						const ObjectParamInfo& paramInfo = stageInfo.loadStoreTextures[k];
 
 						const MaterialParams::ParamData* materialParamInfo = params->getParamData(paramInfo.paramIdx);
-						if ((materialParamInfo->dirtyFlags & dirtyFlagCheck) == 0 && !updateAll)
+						if ((materialParamInfo->dirtyFlags & dirtyFlagMask) == 0 && !updateAll)
 							continue;
 
 						TextureSurface surface;
@@ -912,7 +912,7 @@ namespace BansheeEngine
 						const ObjectParamInfo& paramInfo = stageInfo.buffers[k];
 
 						const MaterialParams::ParamData* materialParamInfo = params->getParamData(paramInfo.paramIdx);
-						if ((materialParamInfo->dirtyFlags & dirtyFlagCheck) == 0 && !updateAll)
+						if ((materialParamInfo->dirtyFlags & dirtyFlagMask) == 0 && !updateAll)
 							continue;
 
 						BufferType buffer;
@@ -926,7 +926,7 @@ namespace BansheeEngine
 						const ObjectParamInfo& paramInfo = stageInfo.samplerStates[k];
 
 						const MaterialParams::ParamData* materialParamInfo = params->getParamData(paramInfo.paramIdx);
-						if ((materialParamInfo->dirtyFlags & dirtyFlagCheck) == 0 && !updateAll)
+						if ((materialParamInfo->dirtyFlags & dirtyFlagMask) == 0 && !updateAll)
 							continue;
 
 						SamplerStateType samplerState;

+ 4 - 4
Source/BansheeCore/Source/BsMaterial.cpp

@@ -40,15 +40,15 @@ namespace BansheeEngine
 			return nullptr;
 
 		SPtr<TechniqueType> technique = mTechniques[techniqueIdx];
-		return bs_shared_ptr_new<GpuParamsSetType>(technique, mShader, techniqueIdx, mParams);
+		return bs_shared_ptr_new<GpuParamsSetType>(technique, mShader, mParams);
 	}
 
 	template<bool Core>
-	void TMaterial<Core>::updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, bool forceRefresh)
+	void TMaterial<Core>::updateParamsSet(const SPtr<GpuParamsSetType>& paramsSet, UINT32 dirtyBitIdx, bool forceRefresh)
 	{
-		paramsSet->update(mParams, forceRefresh);
+		paramsSet->update(mParams, dirtyBitIdx, forceRefresh);
 
-		// TODO - Clear dirty flags
+		mParams->clearDirtyFlags(dirtyBitIdx);
 	}
 
 	template<bool Core>

+ 8 - 9
Source/BansheeCore/Source/BsMaterialParams.cpp

@@ -146,15 +146,6 @@ namespace BansheeEngine
 		return GetParamResult::Success;
 	}
 
-	const MaterialParamsBase::ParamData* MaterialParamsBase::getParamData(UINT32 index) const
-	{
-		if (index == -1)
-			return nullptr;
-
-		const ParamData& param = mParams[index];
-		return &param;
-	}
-
 	void MaterialParamsBase::reportGetParamError(GetParamResult errorCode, const String& name, UINT32 arrayIdx) const
 	{
 		switch (errorCode)
@@ -173,6 +164,14 @@ namespace BansheeEngine
 		}
 	}
 
+	void MaterialParamsBase::clearDirtyFlags(UINT32 techniqueIdx)
+	{
+		UINT32 mask = ~(1 << techniqueIdx);
+
+		for (auto& entry : mParams)
+			entry.dirtyFlags &= mask;
+	}
+
 	RTTITypeBase* MaterialParamStructData::getRTTIStatic()
 	{
 		return MaterialParamStructDataRTTI::instance();

+ 2 - 2
Source/RenderBeast/Source/BsRenderBeast.cpp

@@ -183,7 +183,7 @@ namespace BansheeEngine
 					matInfo.params[0] = renElement.material->createParamsSet(0);
 					matInfo.matVersion = renElement.material->getVersion();
 
-					renElement.material->updateParamsSet(matInfo.params[0], true);
+					renElement.material->updateParamsSet(matInfo.params[0], 0, true);
 					renElement.material->setRendererData(matInfo);
 					renElement.params = matInfo.params[0];
 				}
@@ -198,7 +198,7 @@ namespace BansheeEngine
 						matInfo.params[0] = renElement.material->createParamsSet(0);
 						matInfo.matVersion = renElement.material->getVersion();
 
-						renElement.material->updateParamsSet(matInfo.params[0], true);
+						renElement.material->updateParamsSet(matInfo.params[0], 0, true);
 					}
 
 					renElement.params = matInfo.params[0];