SharpDX, how to set constant buffer when using effect / technique

Started by
1 comment, last by jkristia 10 years, 6 months ago

I want to change to use effect, and I have it working now, but I was not able to figure out how to set the constant buffers when using effects, so the 2 update calls

rc.Context.UpdateSubresource<cbObjStruct>(ref m_cbObj, m_perObjcBuf);

rc.Context.UpdateSubresource<cbTransform>(ref m_cbTransform, m_transformcBuf);

turns into this instead (ignore the extra transpose). Is there an easier way to update the constant buffer than setting it per variable? (obviously I will optimize it to per frame and per obj Update)


		void Update(RenderContext rc)
		{
			Matrix m1 = m_cbTransform.gWorld;
			m1.Transpose();
			EffectMatrixVariable m = m_effect.GetVariableByName("gWorld").AsMatrix();
			m.SetMatrix(m1);

			m1 = m_cbTransform.gView;
			m1.Transpose();
			m = m_effect.GetVariableByName("gView").AsMatrix();
			m.SetMatrix(m1);

			m1 = m_cbTransform.gViewProj;
			m1.Transpose();
			m = m_effect.GetVariableByName("gViewProj").AsMatrix();
			m.SetMatrix(m1);

			EffectVectorVariable v = m_effect.GetVariableByName("objColor").AsVector();
			v.Set(m_cbObj.ObjColor);

			m1 = m_cbObj.ObjWorld;
			m1.Transpose();
			m = m_effect.GetVariableByName("objWorld").AsMatrix();
			m.SetMatrix(m1);

			EffectPass renderpass = m_currentRender.GetPassByIndex(0);
			renderpass.Apply(rc.Context);
		}
Advertisement

The purpose of using the legacy Effects11/Techniques is to hide direct constant buffer manipulation and give an access to direct variable. Effects11 is automatically handling this for you: when you update a variable in a constant buffer, It will upload the constant buffer automatically. If you want to update directly the constant buffer, you can get it from effect using effect.GetConstantBufferByIndex, and do your update as usual. Though, I don't know if it is working well with the internals of Effects11.

Also usually, it is a better practice to store all AsMatrix/AsVector as part of initialization of your effect something like this:


// At init time
m_effect = ...;
worldVar = m_effect.GetVariableByName("gWorld").AsMatrix();
viewVar = m_effect.GetVariableByName("gView").AsMatrix();
viewProjVar = m_effect.GetVariableByName("gViewProj").AsMatrix();
objColorVar = m_effect.GetVariableByName("objColor").AsVector();
objWorldVar = effect.GetVariableByName("objWorld").AsMatrix();
					
//at update time
Matrix m1 = m_cbTransform.gWorld;
m1.Transpose();
worldVar.SetMatrix(m1);

m1 = m_cbTransform.gView;
m1.Transpose();
viewVar.SetMatrix(m1);

m1 = m_cbTransform.gViewProj;
m1.Transpose();
viewProjVar.SetMatrix(m1);

objColorVar.Set(m_cbObj.ObjColor);

m1 = m_cbObj.ObjWorld;
m1.Transpose();
objWorldVar.SetMatrix(m1);

EffectPass renderpass = m_currentRender.GetPassByIndex(0);
renderpass.Apply(rc.Context);

thank you for the information.

This topic is closed to new replies.

Advertisement