XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Checking current transform before calling SetTransform()

Last post 10/31/2009 3:05 PM by Erida Samojlova. 4 replies.
  • 10/25/2009 10:58 AM

    Checking current transform before calling SetTransform()

    Hello. Excuse for bad English.
    I have learned a basic concepts of DX programming and now I'm trying to write my own mmm... can't say Engine, maybe a little abstraction.
    And have a few questions.

    Should I check current transform before calling IDirect3DDevice9::SetTransform() to optimize my program? Or DirectX checks this itself? And what things should I check manually for better optimization?
    Now I'm using this function:

    void Render::SetTransform(D3DTRANSFORMSTATETYPE Type, const D3DXMATRIX* pTransform) 
      assert(pTransform); 
     
      if ( m_pDevice )
      {
        D3DXMATRIX CurTransform; 
        m_pDevice->GetTransform(Type, &CurTransform); 
       
        if ( CurTransform != *pTransform ) 
          m_pDevice->SetTransform(Type, pTransform);
        else [...]
      }
      else [...]

  • 10/25/2009 11:37 PM In reply to

    Re: Checking current transform before calling SetTransform()

    You should batch your rendercalls on the transforms, you shouldn't read back from the device if it's not nessecary. In this case you could more easily achieve this by having a current transform in your render class and check against that instead of reading back from the device.
  • 10/26/2009 10:17 PM In reply to

    Re: Checking current transform before calling SetTransform()

    > NightCreature
    Keep transforms inside class mmm... It's a good idea, thanks. But it's strange that DirectX does not check current transform before setting a new one.

    And what things should I check besides transforms? Maybe lights or textures?
  • 10/27/2009 6:45 PM In reply to

    Re: Checking current transform before calling SetTransform()

    It does this for a good reason reading back from the GPU is slow, the GPU is designed to accept data not to send it back to the CPU. For this reason DX never checks any render state before setting it, it will just set it. Batching is up to the developer although the driver does some optimizations as well.
  • 10/31/2009 3:05 PM In reply to

    Re: Checking current transform before calling SetTransform()

    > NightCreature
    Thanks for the explanation.
     
    Here is a part of code for wraping transforms in my Render class. Maybe it helps someone.


    class Render
    {
    public:
      int SetTransform(D3DTRANSFORMSTATETYPE Type, const D3DXMATRIX* pTransform);
      int GetTransform(D3DTRANSFORMSTATETYPE Type, D3DXMATRIX* pOut);

    private:
      D3DXMATRIX m_CurProjTransform;
      D3DXMATRIX m_CurViewTransform;
      D3DXMATRIX m_CurWorldTransform;
    };

    //----------------------------------------------------------------------------
    int Render::SetTransform(D3DTRANSFORMSTATETYPE Type,
      const D3DXMATRIX* pTransform)
    {
      // -1 - Неизвестный тип преобразования (Unknown transform type)
      // -2 - Преобразования равны (Transforms are equal)

      assert(pTransform);
     
      D3DXMATRIX CurTransform;
      if ( Type == D3DTS_PROJECTION )
        CurTransform = m_CurProjTransform;
      else if ( Type == D3DTS_VIEW )
        CurTransform = m_CurViewTransform;
      else if ( Type == D3DTS_WORLD )
        CurTransform = m_CurWorldTransform;
      else
         // Неизвестное преобразование
        return -1;
     
      if ( CurTransform != *pTransform )
      {
        m_pDevice->SetTransform(Type, pTransform);

        return 0;
      }

      return -2;
    }

    //----------------------------------------------------------------------------
    int Render::GetTransform(D3DTRANSFORMSTATETYPE Type, D3DXMATRIX* pOut)
    {
      // -1 - Неизвестный тип преобразования (Unknown transform type)

      assert(pOut);

      if ( Type == D3DTS_PROJECTION )
      {
        *pOut = m_CurProjTransform;
        
        return 0;
      }
      if ( Type == D3DTS_VIEW )
      {
        *pOut = m_CurViewTransform;

        return 0;
      }
      if ( Type == D3DTS_WORLD )
      {
        *pOut = m_CurWorldTransform;
        
        return 0;
      }
     
      // Неизвестное преобразование
      return -1;
    }

Page 1 of 1 (5 items) Previous Next