BsD3D9VertexDeclaration.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "BsD3D9VertexDeclaration.h"
  2. #include "BsD3D9Mappings.h"
  3. #include "BsException.h"
  4. #include "BsD3D9RenderAPI.h"
  5. #include "BsD3D9ResourceManager.h"
  6. #include "BsRenderStats.h"
  7. namespace BansheeEngine
  8. {
  9. D3D9VertexDeclarationCore::D3D9VertexDeclarationCore(const List<VertexElement>& elements)
  10. :VertexDeclarationCore(elements)
  11. { }
  12. D3D9VertexDeclarationCore::~D3D9VertexDeclarationCore()
  13. {
  14. releaseDeclaration();
  15. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_VertexDeclaration);
  16. }
  17. void D3D9VertexDeclarationCore::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  18. {
  19. }
  20. void D3D9VertexDeclarationCore::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  21. {
  22. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  23. auto it = mMapDeviceToDeclaration.find(d3d9Device);
  24. if (it != mMapDeviceToDeclaration.end())
  25. {
  26. SAFE_RELEASE(it->second);
  27. mMapDeviceToDeclaration.erase(it);
  28. }
  29. }
  30. IDirect3DVertexDeclaration9* D3D9VertexDeclarationCore::getD3DVertexDeclaration()
  31. {
  32. IDirect3DDevice9* pCurDevice = D3D9RenderAPI::getActiveD3D9Device();
  33. auto it = mMapDeviceToDeclaration.find(pCurDevice);
  34. IDirect3DVertexDeclaration9* lpVertDecl = NULL;
  35. const List<VertexElement>& elementList = getProperties().getElements();
  36. // Case we have to create the declaration for this device.
  37. if (it == mMapDeviceToDeclaration.end() || it->second == NULL)
  38. {
  39. D3DVERTEXELEMENT9* d3delems = bs_newN<D3DVERTEXELEMENT9>((UINT32)( getProperties().getElements().size() + 1));
  40. unsigned int idx;
  41. auto iend = elementList.end();
  42. auto i = elementList.begin();
  43. for (idx = 0, i = elementList.begin(); i != iend; ++i, ++idx)
  44. {
  45. d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
  46. d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
  47. d3delems[idx].Stream = i->getStreamIdx();
  48. d3delems[idx].Type = D3D9Mappings::get(i->getType());
  49. d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
  50. d3delems[idx].UsageIndex = static_cast<BYTE>(i->getSemanticIdx());
  51. }
  52. // Add terminator
  53. d3delems[idx].Stream = 0xff;
  54. d3delems[idx].Offset = 0;
  55. d3delems[idx].Type = D3DDECLTYPE_UNUSED;
  56. d3delems[idx].Method = 0;
  57. d3delems[idx].Usage = 0;
  58. d3delems[idx].UsageIndex = 0;
  59. HRESULT hr = pCurDevice->CreateVertexDeclaration(d3delems, &lpVertDecl);
  60. if (FAILED(hr))
  61. {
  62. BS_EXCEPT(InternalErrorException, "Cannot create D3D9 vertex declaration: ");
  63. }
  64. bs_deleteN(d3delems, (UINT32)(elementList.size() + 1));
  65. mMapDeviceToDeclaration[pCurDevice] = lpVertDecl;
  66. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_VertexDeclaration);
  67. }
  68. // Declaration already exits.
  69. else
  70. {
  71. lpVertDecl = mMapDeviceToDeclaration[pCurDevice];
  72. }
  73. return lpVertDecl;
  74. }
  75. void D3D9VertexDeclarationCore::releaseDeclaration()
  76. {
  77. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  78. auto it = mMapDeviceToDeclaration.begin();
  79. while (it != mMapDeviceToDeclaration.end())
  80. {
  81. SAFE_RELEASE(it->second);
  82. ++it;
  83. }
  84. mMapDeviceToDeclaration.clear();
  85. }
  86. }