Jump to content
  • Advertisement
Sign in to follow this  
jkristia

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

This topic is 2119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
		}

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!