Setting world matrix on shader not working

Started by
4 comments, last by dario_ramos 11 years, 1 month ago

I used to handle transformation matrices (world, view, projection) at "device level", to say so. That is, I used calls like this:


m_pd3dDevice->SetTransform( D3DTS_WORLD, pNewVal );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); 

Everything was working perfectly that way. But when I added support for the HLSL shader model 3.0, I was forced to define a vertex shader (my effect wouldn't compile). So far, I hadn't needed a vertex shader because there was no need for custom transformations at that level. Anyway, it seems that when you define your vertex shader, you must perform the world-view-projection transformation there. That was the way I understood it, correct me if I'm wrong. So my vertex and pixel shader ended up like this:


sampler2D Texture0;
float4x4 g_matProjection;
float4x4 g_matView;
float4x4 g_matWorld;

struct VertexShaderInput{
    float4 Position : POSITION;
    float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput {
    float4 Position : POSITION;
    float2 TexCoord : TEXCOORD0;
};
VertexShaderOutput BasicVertexShader( VertexShaderInput input ) {
   VertexShaderOutput output;
   output.Position = mul( input.Position, g_matWorld );
   output.Position = mul( output.Position, g_matView );
   output.Position = mul( output.Position, g_matProjection );
   output.TexCoord = input.TexCoord; //Just pass it along
   return output;
}

struct PixelShaderOutput {
    float4 color : COLOR0;
};
PixelShaderOutput PixelShaderFirstPass( VertexShaderOutput input ) {
    //Magic happens here
    //We use the tex2D function to sample Texture0
}

To make this work, I replaced the SetTransform calls with calls that set the appropiate effect variable, like so:


m_pd3dDevice->SetTransform( D3DTS_WORLD, pNewVal ); // Now  m_pEffect->setParameter( "g_matWorld", pNewVal );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );  //Now m_pEffect->setParameter( "g_matView", &matView );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );  //Now m_pEffect->setParameter( "g_matProjection", &matProj );


void CEffect::setParameter( const string& strName, const D3DXMATRIXA16 * newVal ) {
   if( !m_pEffect ) return;
   D3DXHANDLE handle;
   assert( handle = m_pEffect->GetParameterByName( NULL, strName.c_str() ) );
   assert( SUCCEEDED( m_pEffect->SetMatrix( handle, newVal ) ) );
} 

With this new setup, pan and zoom work, but when I draw my textured quads, they all end up in the same position, one on top of the other (I verified this using PIX, and I also checked that the world matrix is set right before drawing each quad, and with different values). I'm at a loss now. Did I misunderstand something about the way this should be done? The view and projection matrices are set BEFORE calling m_pEffect->Begin(), and the world transform is set between m_pEffect->Begin() and m_pEffect->End(). Could this be the reason?

Advertisement

As I suspected, if you set an effect parameter while you're between Begin() and End() calls, you need to call CommitChanges for the effect to notice the change. So now that I call that function after setting the g_matWorld parameter, everything works fine again.

However, I now need to call CommitChanges for each of my texture quads. I'm gonna check with PIX, but isn't this inefficient? Is there another, more efficient, way to position my textured quads before drawing them?

Generally for textured quads (assuming a 2D view) you'd update their positions on the CPU then write the modified positions into a dynamic vertex buffer, meaning that you only need to send your matrices to the shader once (at the start of each frame). This can be a very cheap operation and enables you to get much larger draw batches, which should more than offset the overhead from doing positions on the CPU.

If you really don't want to do this, you can continue as you are, and get some performance back by caching out your "m_pEffect->GetParameterByName( NULL, strName.c_str() )" call at load time, then reusing the cached handle rather than having to get it each time you set a matrix. That's a recommendation from the D3D docs by the way, and will save you some overhead.

CommitChanges is not necessarily that expensive. All that the Effects framework does is store any Set call in a system memory copy, set a dirty flag, then sends it to the shader for real (in this case by calling IDirect3DDevice9::SetVertexShaderConstantF) when CommitChanges is called (or a new pass is begun). So the total overhead of your CommitChanges will be something like a function call, some "if"s and 16 floats going to the GPU. True, it may pile up if you're drawing lots and lots of quads each of which has their own world matrix, which brings me back to the first point - try doing the position transform on the CPU instead and get those bigger batches going.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Generally for textured quads (assuming a 2D view) you'd update their positions on the CPU then write the modified positions into a dynamic vertex buffer, meaning that you only need to send your matrices to the shader once (at the start of each frame). This can be a very cheap operation and enables you to get much larger draw batches, which should more than offset the overhead from doing positions on the CPU.

You can take this one step further, and use instanced rendering to draw your quads. This will allow you to only provide the modified rotation/translation information in an instance buffer, which should cut down on the amount of memory that you need to upload for the draw call.
In your code you have this line (although it might just be an example as you commented it out:

Now m_pEffect->setParameter( "g_matWorld", pNewVal );

Shouldn't pNewVal read &worldMatrix here?
Just my 2 cents

In your code you have this line (although it might just be an example as you commented it out:

Now m_pEffect->setParameter( "g_matWorld", pNewVal );

Shouldn't pNewVal read &worldMatrix here?
Just my 2 cents

It does look inconsistent out of context, doesn't it? It's actually OK since that call is wrapped inside a setter-style method which receives pNewVal as a parameter. The others don't have that style because they received a local variable with that name.

My original problem is solved now; I didn't know that it was necessary to call CommitChanges if you changed effect parameters while rendering a pass. I'll now apply the performance tips the other guys gave me and see how it goes.

This topic is closed to new replies.

Advertisement