BsD3D9VertexDeclaration.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D9VertexDeclaration.h"
  4. #include "BsD3D9Mappings.h"
  5. #include "BsException.h"
  6. #include "BsD3D9RenderAPI.h"
  7. #include "BsD3D9ResourceManager.h"
  8. #include "BsRenderStats.h"
  9. namespace BansheeEngine
  10. {
  11. D3D9VertexDeclarationCore::D3D9VertexDeclarationCore(const List<VertexElement>& elements)
  12. :VertexDeclarationCore(elements)
  13. { }
  14. D3D9VertexDeclarationCore::~D3D9VertexDeclarationCore()
  15. {
  16. releaseDeclaration();
  17. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_VertexDeclaration);
  18. }
  19. void D3D9VertexDeclarationCore::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  20. {
  21. }
  22. void D3D9VertexDeclarationCore::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  23. {
  24. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  25. auto it = mMapDeviceToDeclaration.find(d3d9Device);
  26. if (it != mMapDeviceToDeclaration.end())
  27. {
  28. SAFE_RELEASE(it->second);
  29. mMapDeviceToDeclaration.erase(it);
  30. }
  31. }
  32. IDirect3DVertexDeclaration9* D3D9VertexDeclarationCore::getD3DVertexDeclaration()
  33. {
  34. IDirect3DDevice9* pCurDevice = D3D9RenderAPI::getActiveD3D9Device();
  35. auto it = mMapDeviceToDeclaration.find(pCurDevice);
  36. IDirect3DVertexDeclaration9* lpVertDecl = NULL;
  37. const List<VertexElement>& elementList = getProperties().getElements();
  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>((UINT32)( getProperties().getElements().size() + 1));
  42. unsigned int idx;
  43. auto iend = elementList.end();
  44. auto i = elementList.begin();
  45. for (idx = 0, i = elementList.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. d3delems[idx].UsageIndex = static_cast<BYTE>(i->getSemanticIdx());
  53. }
  54. // Add terminator
  55. d3delems[idx].Stream = 0xff;
  56. d3delems[idx].Offset = 0;
  57. d3delems[idx].Type = D3DDECLTYPE_UNUSED;
  58. d3delems[idx].Method = 0;
  59. d3delems[idx].Usage = 0;
  60. d3delems[idx].UsageIndex = 0;
  61. HRESULT hr = pCurDevice->CreateVertexDeclaration(d3delems, &lpVertDecl);
  62. if (FAILED(hr))
  63. {
  64. BS_EXCEPT(InternalErrorException, "Cannot create D3D9 vertex declaration: ");
  65. }
  66. bs_deleteN(d3delems, (UINT32)(elementList.size() + 1));
  67. mMapDeviceToDeclaration[pCurDevice] = lpVertDecl;
  68. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_VertexDeclaration);
  69. }
  70. // Declaration already exits.
  71. else
  72. {
  73. lpVertDecl = mMapDeviceToDeclaration[pCurDevice];
  74. }
  75. return lpVertDecl;
  76. }
  77. void D3D9VertexDeclarationCore::releaseDeclaration()
  78. {
  79. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  80. auto it = mMapDeviceToDeclaration.begin();
  81. while (it != mMapDeviceToDeclaration.end())
  82. {
  83. SAFE_RELEASE(it->second);
  84. ++it;
  85. }
  86. mMapDeviceToDeclaration.clear();
  87. }
  88. }