Browse Source

Material refactor - Things working again

BearishSun 9 years ago
parent
commit
8a5d6db814

+ 1 - 1
Source/BansheeCore/Include/BsGpuParam.h

@@ -217,7 +217,7 @@ namespace BansheeEngine
 		TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent);
 
 		/** @copydoc TGpuDataParam::set */
-		void set(const TextureType& texture, const TextureSurface& surface) const;
+		void set(const TextureType& texture, const TextureSurface& surface = TextureSurface()) const;
 
 		/** @copydoc TGpuDataParam::get */
 		TextureType get() const;

+ 9 - 2
Source/BansheeCore/Include/BsGpuParamsSet.h

@@ -43,12 +43,13 @@ namespace BansheeEngine
 		struct BlockInfo
 		{
 			BlockInfo(const String& name, const ParamBlockPtrType& buffer, bool shareable)
-				:name(name), buffer(buffer), shareable(shareable)
+				:name(name), buffer(buffer), shareable(shareable), allowUpdate(true)
 			{ }
 
 			String name;
 			ParamBlockPtrType buffer;
 			bool shareable;
+			bool allowUpdate;
 		};
 
 		/** Information about how a data parameter maps from a material parameter into a parameter block buffer. */
@@ -142,12 +143,18 @@ namespace BansheeEngine
 		/**
 		 * Assign a parameter block buffer with the specified name to all the relevant child GpuParams.
 		 *
+		 * @param[in]	name			Name of the buffer to set.
+		 * @param[in]	paramBlock		Parameter block to assign.
+		 * @param[in]	ignoreInUpdate	If true the buffer will not be updated during the update() call. This is useful
+		 *								if the caller wishes to manually update the buffer contents externally, to prevent
+		 *								overwriting manually written data during update.
+		 *
 		 * @note	
 		 * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or
 		 * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters
 		 * in the buffers need only be set once and then reused multiple times.
 		 */
-		void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock);
+		void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock, bool ignoreInUpdate = false);
 
 		/** Returns the number of passes the set contains the parameters for. */
 		UINT32 getNumPasses() const { return (UINT32)mPassParams.size(); }

+ 4 - 2
Source/BansheeCore/Source/BsGpuParamsSet.cpp

@@ -713,7 +713,8 @@ namespace BansheeEngine
 	}
 
 	template<bool Core>
-	void TGpuParamsSet<Core>::setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock)
+	void TGpuParamsSet<Core>::setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock, 
+		bool ignoreInUpdate)
 	{
 		UINT32 foundIdx = (UINT32)-1;
 		for(UINT32 i = 0; i < (UINT32)mBlocks.size(); i++)
@@ -738,6 +739,7 @@ namespace BansheeEngine
 		}
 
 		mBlocks[foundIdx].buffer = paramBlock;
+		mBlocks[foundIdx].allowUpdate = !ignoreInUpdate;
 
 		UINT32 numPasses = (UINT32)mPassParams.size();
 		for (UINT32 j = 0; j < numPasses; j++)
@@ -769,7 +771,7 @@ namespace BansheeEngine
 		for(auto& paramInfo : mDataParamInfos)
 		{
 			ParamBlockPtrType paramBlock = mBlocks[paramInfo.blockIdx].buffer;
-			if (paramBlock == nullptr)
+			if (paramBlock == nullptr || !mBlocks[paramInfo.blockIdx].allowUpdate)
 				continue;
 
 			const MaterialParams::ParamData* materialParamInfo = params->getParamData(paramInfo.paramIdx);

+ 3 - 3
Source/RenderBeast/Include/BsLightRendering.h

@@ -40,9 +40,9 @@ namespace BansheeEngine
 		SPtr<MaterialCore> mMaterial;
 		SPtr<GpuParamsSetCore> mParamsSet;
 
-		MaterialParamTextureCore mGBufferA;
-		MaterialParamTextureCore mGBufferB;
-		MaterialParamTextureCore mGBufferDepth;
+		GpuParamTextureCore mGBufferA;
+		GpuParamTextureCore mGBufferB;
+		GpuParamTextureCore mGBufferDepth;
 		PerLightParamBuffer mBuffer;
 	};
 

+ 9 - 10
Source/RenderBeast/Include/BsPostProcessing.h

@@ -50,8 +50,7 @@ namespace BansheeEngine
 		SPtr<RenderTextureCore> getOutput() const { return mOutput; }
 	private:
 		DownsampleParams mParams;
-		MaterialParamVec2Core mInvTexSize;
-		MaterialParamTextureCore mInputTexture;
+		GpuParamTextureCore mInputTexture;
 
 		POOLED_RENDER_TEXTURE_DESC mOutputDesc;
 		SPtr<RenderTextureCore> mOutput;
@@ -95,8 +94,8 @@ namespace BansheeEngine
 		static const UINT32 HISTOGRAM_NUM_TEXELS = (THREAD_GROUP_SIZE_X * THREAD_GROUP_SIZE_Y) / 4;
 	private:
 		EyeAdaptHistogramParams mParams;
-		MaterialParamTextureCore mSceneColor;
-		MaterialParamLoadStoreTextureCore mOutputTex;
+		GpuParamTextureCore mSceneColor;
+		GpuParamLoadStoreTextureCore mOutputTex;
 
 		POOLED_RENDER_TEXTURE_DESC mOutputDesc;
 		SPtr<RenderTextureCore> mOutput;
@@ -128,8 +127,8 @@ namespace BansheeEngine
 	private:
 		EyeAdaptHistogramReduceParams mParams;
 
-		MaterialParamTextureCore mHistogramTex;
-		MaterialParamTextureCore mEyeAdaptationTex;
+		GpuParamTextureCore mHistogramTex;
+		GpuParamTextureCore mEyeAdaptationTex;
 
 		POOLED_RENDER_TEXTURE_DESC mOutputDesc;
 		SPtr<RenderTextureCore> mOutput;
@@ -151,7 +150,7 @@ namespace BansheeEngine
 		void execute(PostProcessInfo& ppInfo, float frameDelta);
 	private:
 		EyeAdaptationParams mParams;
-		MaterialParamTextureCore mReducedHistogramTex;
+		GpuParamTextureCore mReducedHistogramTex;
 	};
 
 	BS_PARAM_BLOCK_BEGIN(CreateTonemapLUTParams)
@@ -214,9 +213,9 @@ namespace BansheeEngine
 	private:
 		TonemappingParams mParams;
 
-		MaterialParamTextureCore mInputTex;
-		MaterialParamTextureCore mColorLUT;
-		MaterialParamTextureCore mEyeAdaptationTex;
+		GpuParamTextureCore mInputTex;
+		GpuParamTextureCore mColorLUT;
+		GpuParamTextureCore mEyeAdaptationTex;
 	};
 
 	/**

+ 5 - 4
Source/RenderBeast/Source/BsLightRendering.cpp

@@ -15,15 +15,17 @@ namespace BansheeEngine
 	LightRenderingParams::LightRenderingParams(const SPtr<MaterialCore>& material, const SPtr<GpuParamsSetCore>& paramsSet)
 		:mMaterial(material), mParamsSet(paramsSet)
 	{
+		SPtr<GpuParamsCore> fragmentParams = mParamsSet->getGpuParams(GPT_FRAGMENT_PROGRAM);
+
 		auto& texParams = material->getShader()->getTextureParams();
 		for (auto& entry : texParams)
 		{
 			if (entry.second.rendererSemantic == RPS_GBufferA)
-				mGBufferA = material->getParamTexture(entry.second.name);
+				fragmentParams->getTextureParam(entry.second.name, mGBufferA);
 			else if (entry.second.rendererSemantic == RPS_GBufferB)
-				mGBufferB = material->getParamTexture(entry.second.name);
+				fragmentParams->getTextureParam(entry.second.name, mGBufferB);
 			else if (entry.second.rendererSemantic == RPS_GBufferDepth)
-				mGBufferDepth = material->getParamTexture(entry.second.name);
+				fragmentParams->getTextureParam(entry.second.name, mGBufferDepth);
 		}
 	}
 
@@ -37,7 +39,6 @@ namespace BansheeEngine
 		mParamsSet->setParamBlockBuffer("PerLight", getBuffer());
 		mParamsSet->setParamBlockBuffer("PerCamera", perCamera);
 
-		mMaterial->updateParamsSet(mParamsSet);
 		gRendererUtility().setPassParams(mParamsSet);
 	}
 

+ 3 - 3
Source/RenderBeast/Source/BsObjectRendering.cpp

@@ -50,9 +50,9 @@ namespace BansheeEngine
 		
 		// Note: Perhaps perform buffer validation to ensure expected buffer has the same size and layout as the provided
 		// buffer, and show a warning otherwise. But this is perhaps better handled on a higher level.
-		element.params->setParamBlockBuffer(perFrameBlockName, mPerFrameParams.getBuffer());
-		element.params->setParamBlockBuffer(perCameraBlockName, mPerCameraParams.getBuffer());
-		element.params->setParamBlockBuffer(perObjectBlockName, mPerObjectParams.getBuffer());
+		element.params->setParamBlockBuffer(perFrameBlockName, mPerFrameParams.getBuffer(), true);
+		element.params->setParamBlockBuffer(perCameraBlockName, mPerCameraParams.getBuffer(), true);
+		element.params->setParamBlockBuffer(perObjectBlockName, mPerObjectParams.getBuffer(), true);
 
 		if (!boneMatricesParamName.empty())
 		{

+ 15 - 12
Source/RenderBeast/Source/BsPostProcessing.cpp

@@ -13,9 +13,7 @@ namespace BansheeEngine
 	DownsampleMat::DownsampleMat()
 	{
 		mParamsSet->setParamBlockBuffer("Input", mParams.getBuffer());
-
-		mInputTexture = mMaterial->getParamTexture("gInputTex");
-		mInvTexSize = mMaterial->getParamVec2("gInvTexSize");
+		mParamsSet->getGpuParams(GPT_FRAGMENT_PROGRAM)->getTextureParam("gInputTex", mInputTexture);
 	}
 
 	void DownsampleMat::_initDefines(ShaderDefines& defines)
@@ -67,8 +65,10 @@ namespace BansheeEngine
 	{
 		mParamsSet->setParamBlockBuffer("Input", mParams.getBuffer());
 
-		mSceneColor = mMaterial->getParamTexture("gSceneColorTex");
-		mOutputTex = mMaterial->getParamLoadStoreTexture("gOutputTex");
+		SPtr<GpuParamsCore> computeParams = mParamsSet->getGpuParams(GPT_COMPUTE_PROGRAM);
+
+		computeParams->getTextureParam("gSceneColorTex", mSceneColor);
+		computeParams->getLoadStoreTextureParam("gOutputTex", mOutputTex);
 	}
 
 	void EyeAdaptHistogramMat::_initDefines(ShaderDefines& defines)
@@ -152,8 +152,10 @@ namespace BansheeEngine
 	{
 		mParamsSet->setParamBlockBuffer("Input", mParams.getBuffer());
 
-		mHistogramTex = mMaterial->getParamTexture("gHistogramTex");
-		mEyeAdaptationTex = mMaterial->getParamTexture("gEyeAdaptationTex");
+		SPtr<GpuParamsCore> fragmentParams = mParamsSet->getGpuParams(GPT_FRAGMENT_PROGRAM);
+
+		fragmentParams->getTextureParam("gHistogramTex", mHistogramTex);
+		fragmentParams->getTextureParam("gEyeAdaptationTex", mEyeAdaptationTex);
 	}
 
 	void EyeAdaptHistogramReduceMat::_initDefines(ShaderDefines& defines)
@@ -211,8 +213,7 @@ namespace BansheeEngine
 	EyeAdaptationMat::EyeAdaptationMat()
 	{
 		mParamsSet->setParamBlockBuffer("Input", mParams.getBuffer());
-
-		mReducedHistogramTex = mMaterial->getParamTexture("gHistogramTex");
+		mParamsSet->getGpuParams(GPT_FRAGMENT_PROGRAM)->getTextureParam("gHistogramTex", mReducedHistogramTex);
 	}
 
 	void EyeAdaptationMat::_initDefines(ShaderDefines& defines)
@@ -344,9 +345,11 @@ namespace BansheeEngine
 	{
 		mParamsSet->setParamBlockBuffer("Input", mParams.getBuffer());
 
-		mInputTex = mMaterial->getParamTexture("gInputTex");
-		mColorLUT = mMaterial->getParamTexture("gColorLUT");
-		mEyeAdaptationTex = mMaterial->getParamTexture("gEyeAdaptationTex");
+		mParamsSet->getGpuParams(GPT_VERTEX_PROGRAM)->getTextureParam("gEyeAdaptationTex", mEyeAdaptationTex);
+
+		SPtr<GpuParamsCore> fragmentParams = mParamsSet->getGpuParams(GPT_FRAGMENT_PROGRAM);
+		fragmentParams->getTextureParam("gInputTex", mInputTex);
+		fragmentParams->getTextureParam("gColorLUT", mColorLUT);
 	}
 
 	template<bool GammaOnly, bool AutoExposure>

+ 1 - 0
Source/RenderBeast/Source/BsRenderBeast.cpp

@@ -788,6 +788,7 @@ namespace BansheeEngine
 		}
 
 		mObjectRenderer->setPerObjectParams(element, mRenderableShaderData[rendererId], worldViewProjMatrix, boneMatrices);
+		material->updateParamsSet(element.params);
 
 		if (bindPass)
 			RendererUtility::instance().setPass(material, passIdx);

+ 6 - 2
Source/RenderBeast/Source/BsSamplerOverrides.cpp

@@ -135,10 +135,14 @@ namespace BansheeEngine
 				}
 			}
 
+			output->numOverrides = (UINT32)overrides.size();
 			output->overrides = (SamplerOverride*)outputData;
-			memcpy(output->overrides, overrides.data(), overrides.size() * sizeof(SamplerOverride));
 
-			output->numOverrides = (UINT32)overrides.size();
+			for(UINT32 i = 0; i < output->numOverrides; i++)
+			{
+				new (&output->overrides[i].state) SPtr<SamplerStateCore>();
+				output->overrides[i] = overrides[i];
+			}
 		}
 		bs_frame_clear();