ConstantBuffer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Graphics/Graphics.h"
  5. #include "../GraphicsAPI/ConstantBuffer.h"
  6. #include "../IO/Log.h"
  7. #include "../DebugNew.h"
  8. namespace Urho3D
  9. {
  10. ConstantBuffer::ConstantBuffer(Context* context) :
  11. Object(context),
  12. GPUObject(GetSubsystem<Graphics>())
  13. {
  14. }
  15. ConstantBuffer::~ConstantBuffer()
  16. {
  17. Release();
  18. }
  19. void ConstantBuffer::SetParameter(unsigned offset, unsigned size, const void* data)
  20. {
  21. if (offset + size > size_)
  22. return; // Would overflow the buffer
  23. memcpy(&shadowData_[offset], data, size);
  24. dirty_ = true;
  25. }
  26. void ConstantBuffer::SetVector3ArrayParameter(unsigned offset, unsigned rows, const void* data)
  27. {
  28. if (offset + rows * 4 * sizeof(float) > size_)
  29. return; // Would overflow the buffer
  30. auto* dest = (float*)&shadowData_[offset];
  31. const auto* src = (const float*)data;
  32. while (rows--)
  33. {
  34. *dest++ = *src++;
  35. *dest++ = *src++;
  36. *dest++ = *src++;
  37. ++dest; // Skip over the w coordinate
  38. }
  39. dirty_ = true;
  40. }
  41. void ConstantBuffer::Release()
  42. {
  43. GAPI gapi = Graphics::GetGAPI();
  44. #ifdef URHO3D_OPENGL
  45. if (gapi == GAPI_OPENGL)
  46. return Release_OGL();
  47. #endif
  48. #ifdef URHO3D_D3D9
  49. if (gapi == GAPI_D3D9)
  50. return Release_D3D9();
  51. #endif
  52. #ifdef URHO3D_D3D11
  53. if (gapi == GAPI_D3D11)
  54. return Release_D3D11();
  55. #endif
  56. }
  57. void ConstantBuffer::OnDeviceReset()
  58. {
  59. GAPI gapi = Graphics::GetGAPI();
  60. #ifdef URHO3D_OPENGL
  61. if (gapi == GAPI_OPENGL)
  62. return OnDeviceReset_OGL();
  63. #endif
  64. #ifdef URHO3D_D3D9
  65. if (gapi == GAPI_D3D9)
  66. return OnDeviceReset_D3D9();
  67. #endif
  68. #ifdef URHO3D_D3D11
  69. if (gapi == GAPI_D3D11)
  70. return OnDeviceReset_D3D11();
  71. #endif
  72. }
  73. bool ConstantBuffer::SetSize(unsigned size)
  74. {
  75. GAPI gapi = Graphics::GetGAPI();
  76. #ifdef URHO3D_OPENGL
  77. if (gapi == GAPI_OPENGL)
  78. return SetSize_OGL(size);
  79. #endif
  80. #ifdef URHO3D_D3D9
  81. if (gapi == GAPI_D3D9)
  82. return SetSize_D3D9(size);
  83. #endif
  84. #ifdef URHO3D_D3D11
  85. if (gapi == GAPI_D3D11)
  86. return SetSize_D3D11(size);
  87. #endif
  88. return {}; // Prevent warning
  89. }
  90. void ConstantBuffer::Apply()
  91. {
  92. GAPI gapi = Graphics::GetGAPI();
  93. #ifdef URHO3D_OPENGL
  94. if (gapi == GAPI_OPENGL)
  95. return Apply_OGL();
  96. #endif
  97. #ifdef URHO3D_D3D9
  98. if (gapi == GAPI_D3D9)
  99. return Apply_D3D9();
  100. #endif
  101. #ifdef URHO3D_D3D11
  102. if (gapi == GAPI_D3D11)
  103. return Apply_D3D11();
  104. #endif
  105. }
  106. }