# 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.

## 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

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 on other sites

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 on other sites

thank you for the information.

• 9
• 18
• 29
• 11
• 24