XNA 4.0 beta: EffectPass not a simple wrapper!

來源:互聯網
上載者:User

  So, the beta is out!

      XNA 4.0進行了大幅更新,大部分改動都不錯,不過一直有2點讓我非常不爽:1. render target必須和某個depth buffer綁定到一起;2. 沒有了SetxxShader和SetShaderConstant等底層函數。前者對deffered shading非常不友好,後者讓我寫的整個材質系統都不能用了。相比之下,後者更讓我擔心,因為CTP中的Effect和EffectPass僅僅是DX Effect系統之上的一個簡單wrapper眾所周知,DirectX中Effect的效能一直是被人詬病的地方。好在Shawn告訴我:

“The Windows EffectPass.Apply implementation in the CTP is not complete, and many optimizations not yet implemented. The CTP implementation is functionally correct, but not from a performance standpoint, so you should not worry too much about Windows performance in the CTP.”

 

     似乎Beta版已經解決了這個問題,4.0中的EffectPass不再是DX Effect上的一個簡單wrapper,xna team做了很大的努力減少狀態改變,以至於PIX分析xna 4.0的程式時有可能出錯,Shawn在blog中說:

"The problem occurs because PIX does not support some crazy tricks that GS4 uses to optimize state management" 。

 

      下面是同一段代碼在CTP和beta版下的對比:

//test code//
.....set parameter for first object
effectPass.Apply();
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,vertices.Count, 0, primitiveCount);
//only change world matrix and draw the same object again
effect.World = Matrix.CreateTranslation(1, 0, 0);
effectPass.Apply();
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,vertices.Count, 0, primitiveCount);

//Pix data for ctp:
ID3DXEffect::BeginPass(0)                          
ID3DXEffect::EndPass()                         
ID3DXEffect::End()                         
IDirect3DDevice9::BeginScene()                         
IDirect3DDevice9::DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12)//draw the 1 primitive                  
ID3DXEffect::SetMatrix(0xFF6B4D6B, 0x0039E6F0)                     
ID3DXEffect::SetMatrix(0xFF6B4E13, 0x0039E684)                     
ID3DXEffect::SetMatrix(0xFF6B4DBF, 0x0039E684)                     
ID3DXEffect::Begin(0x0039E7EC, 0x00000001)                 
ID3DXEffect::BeginPass(0)                          
 IDirect3DDevice9::SetVertexShader(0x06487820)                     //redundance!!!!!!
 IDirect3DDevice9::SetVertexShaderConstantF(0, 0x015851C8, 1)      //redundance!!!!!!!!      
 IDirect3DDevice9::SetVertexShaderConstantF(14, 0x015852A8, 1)     //redundance !!!!!!    
 IDirect3DDevice9::SetVertexShaderConstantF(15, 0x015852B8, 4)             
 IDirect3DDevice9::SetVertexShaderConstantF(19, 0x015852F8, 3)             
 IDirect3DDevice9::SetVertexShaderConstantF(23, 0x01585338, 3)             
 IDirect3DDevice9::SetPixelShader(0x06486AE8)   3914655838         //redundance!!!!!
 IDirect3DDevice9::SetPixelShaderConstantF(0, 0x01586D40, 15)      //redundance!!!!!!    
ID3DXEffect::EndPass()                         
ID3DXEffect::End()                     
IDirect3DDevice9::DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12)// draw the 2 primitive      

//Pix data for beta with d3dx function call
ID3DXEffect::BeginPass(0)                
IDirect3DDevice9::BeginScene()
IDirect3DDevice9::DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12)        
ID3DXEffect::SetMatrix(0xFF84502B, 0x0023E444)                    
ID3DXEffect::SetMatrix(0xFF8450D3, 0x0023E3D8)                    
ID3DXEffect::SetMatrix(0xFF84507F, 0x0023E3D8)                    
ID3DXEffect::CommitChanges()                    
  IDirect3DDevice9::SetVertexShaderConstantF(15, 0x00FB5668, 4)
  IDirect3DDevice9::SetVertexShaderConstantF(19, 0x00FB56A8, 3)
  IDirect3DDevice9::SetVertexShaderConstantF(23, 0x00FB56E8, 3)
IDirect3DDevice9::DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12)

//Pix data for beta without d3dx function:
IDirect3DDevice9::BeginScene()                
IDirect3DDevice9::DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12)
IDirect3DDevice9::SetVertexShaderConstantF(15, 0x007D5660, 4)
IDirect3DDevice9::SetVertexShaderConstantF(19, 0x007D56A0, 3)
IDirect3DDevice9::SetVertexShaderConstantF(23, 0x007D56E0, 3)
IDirect3DDevice9::DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12)                    

 

 

     可以看到beta版中,完全過濾了冗餘的狀態設定,測試代碼中的effect.World會更新world marix,worldViewProjMatrix以及WorldInverseTranspose,因此渲染第二個對象前出現3個參數更新是正確的。測試程式裡只渲染了2個圖形,因此,BeginScene在第一次DP之前出現也是正確的。

 

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.