BsD3D9VertexDeclaration.cpp 3.1 KB

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