BsD3D9VertexDeclaration.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsD3D9VertexDeclaration.h"
  5. #include "BsD3D9Mappings.h"
  6. #include "BsException.h"
  7. #include "BsD3D9RenderSystem.h"
  8. #include "BsD3D9ResourceManager.h"
  9. #include "BsRenderStats.h"
  10. namespace BansheeEngine
  11. {
  12. D3D9VertexDeclaration::D3D9VertexDeclaration(const VertexDeclaration::VertexElementList& elements)
  13. :VertexDeclaration(elements)
  14. { }
  15. D3D9VertexDeclaration::~D3D9VertexDeclaration()
  16. { }
  17. void D3D9VertexDeclaration::destroy_internal()
  18. {
  19. releaseDeclaration();
  20. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_VertexDeclaration);
  21. VertexDeclaration::destroy_internal();
  22. }
  23. void D3D9VertexDeclaration::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  24. {
  25. }
  26. void D3D9VertexDeclaration::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  27. {
  28. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  29. auto it = mMapDeviceToDeclaration.find(d3d9Device);
  30. if (it != mMapDeviceToDeclaration.end())
  31. {
  32. SAFE_RELEASE(it->second);
  33. mMapDeviceToDeclaration.erase(it);
  34. }
  35. }
  36. IDirect3DVertexDeclaration9* D3D9VertexDeclaration::getD3DVertexDeclaration()
  37. {
  38. IDirect3DDevice9* pCurDevice = D3D9RenderSystem::getActiveD3D9Device();
  39. auto it = mMapDeviceToDeclaration.find(pCurDevice);
  40. IDirect3DVertexDeclaration9* lpVertDecl = NULL;
  41. // Case we have to create the declaration for this device.
  42. if (it == mMapDeviceToDeclaration.end() || it->second == NULL)
  43. {
  44. D3DVERTEXELEMENT9* d3delems = bs_newN<D3DVERTEXELEMENT9, PoolAlloc>((UINT32)(mElementList.size() + 1));
  45. VertexElementList::const_iterator i, iend;
  46. unsigned int idx;
  47. iend = mElementList.end();
  48. for (idx = 0, i = mElementList.begin(); i != iend; ++i, ++idx)
  49. {
  50. d3delems[idx].Method = D3DDECLMETHOD_DEFAULT;
  51. d3delems[idx].Offset = static_cast<WORD>(i->getOffset());
  52. d3delems[idx].Stream = i->getStreamIdx();
  53. d3delems[idx].Type = D3D9Mappings::get(i->getType());
  54. d3delems[idx].Usage = D3D9Mappings::get(i->getSemantic());
  55. if (i->getSemantic() == VES_COLOR)
  56. {
  57. d3delems[idx].UsageIndex = 0;
  58. }
  59. else
  60. {
  61. d3delems[idx].UsageIndex = static_cast<BYTE>(i->getSemanticIdx());
  62. }
  63. }
  64. // Add terminator
  65. d3delems[idx].Stream = 0xff;
  66. d3delems[idx].Offset = 0;
  67. d3delems[idx].Type = D3DDECLTYPE_UNUSED;
  68. d3delems[idx].Method = 0;
  69. d3delems[idx].Usage = 0;
  70. d3delems[idx].UsageIndex = 0;
  71. HRESULT hr = pCurDevice->CreateVertexDeclaration(d3delems, &lpVertDecl);
  72. if (FAILED(hr))
  73. {
  74. BS_EXCEPT(InternalErrorException, "Cannot create D3D9 vertex declaration: ");
  75. }
  76. bs_deleteN<PoolAlloc>(d3delems, (UINT32)(mElementList.size() + 1));
  77. mMapDeviceToDeclaration[pCurDevice] = lpVertDecl;
  78. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_VertexDeclaration);
  79. }
  80. // Declaration already exits.
  81. else
  82. {
  83. lpVertDecl = mMapDeviceToDeclaration[pCurDevice];
  84. }
  85. return lpVertDecl;
  86. }
  87. void D3D9VertexDeclaration::releaseDeclaration()
  88. {
  89. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  90. auto it = mMapDeviceToDeclaration.begin();
  91. while (it != mMapDeviceToDeclaration.end())
  92. {
  93. SAFE_RELEASE(it->second);
  94. ++it;
  95. }
  96. mMapDeviceToDeclaration.clear();
  97. }
  98. }