• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
dario_ramos

Setting world matrix on shader not working

5 posts in this topic

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?

Edited by dario_ramos
0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0