BsD3D9VertexDeclaration.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. D3D9VertexDeclaration::D3D9VertexDeclaration(const VertexDeclaration::VertexElementList& elements)
  10. :VertexDeclaration(elements)
  11. { }
  12. D3D9VertexDeclaration::~D3D9VertexDeclaration()
  13. { }
  14. void D3D9VertexDeclaration::destroy_internal()
  15. {
  16. releaseDeclaration();
  17. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_VertexDeclaration);
  18. VertexDeclaration::destroy_internal();
  19. }
  20. void D3D9VertexDeclaration::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  21. {
  22. }
  23. void D3D9VertexDeclaration::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* D3D9VertexDeclaration::getD3DVertexDeclaration()
  34. {
  35. IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
  36. auto it = mMapDeviceToDeclaration.find(pCurDevice);
  37. IDirect3DVertexDeclaration9* lpVertDecl = NULL;
  38. // Case we have to create the declaration for this device.
  39. if (it == mMapDeviceToDeclaration.end() || it->second == NULL)
  40. {
  41. D3DVERTEXELEMENT9* d3delems = bs_newN<D3DVERTEXELEMENT9, PoolAlloc>((UINT32)(mElementList.size() + 1));
  42. VertexElementList::const_iterator i, iend;
  43. unsigned int idx;
  44. iend = mElementList.end();
  45. for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx)
  46. {
  47. d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
  48. d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
  49. d3delems[idx].Stream = i->getStreamIdx();
  50. d3delems[idx].Type = D3D9Mappings::get(i->getType());
  51. d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
  52. if (i->getSemantic() == VES_COLOR)
  53. {
  54. d3delems[idx].UsageIndex = 0;
  55. }
  56. else
  57. {
  58. d3delems[idx].UsageIndex = static_cast<BYTE>(i->getSemanticIdx());
  59. }
  60. }
  61. // Add terminator
  62. d3delems[idx].Stream = 0xff;
  63. d3delems[idx].Offset = 0;
  64. d3delems[idx].Type = D3DDECLTYPE_UNUSED;
  65. d3delems[idx].Method = 0;
  66. d3delems[idx].Usage = 0;
  67. d3delems[idx].UsageIndex = 0;
  68. HRESULT hr = pCurDevice->CreateVertexDeclaration(d3delems, &lpVertDecl);
  69. if (FAILED(hr))
  70. {
  71. BS_EXCEPT(InternalErrorException, "Cannot create D3D9 vertex declaration: ");
  72. }
  73. bs_deleteN<PoolAlloc>(d3delems, (UINT32)(mElementList.size() + 1));
  74. mMapDeviceToDeclaration[pCurDevice] = lpVertDecl;
  75. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_VertexDeclaration);
  76. }
  77. // Declaration already exits.
  78. else
  79. {
  80. lpVertDecl = mMapDeviceToDeclaration[pCurDevice];
  81. }
  82. return lpVertDecl;
  83. }
  84. void D3D9VertexDeclaration::releaseDeclaration()
  85. {
  86. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  87. auto it = mMapDeviceToDeclaration.begin();
  88. while (it != mMapDeviceToDeclaration.end())
  89. {
  90. SAFE_RELEASE(it->second);
  91. ++it;
  92. }
  93. mMapDeviceToDeclaration.clear();
  94. }
  95. }