ConstantBuffer.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2008-2023 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_D3D11
  49. if (gapi == GAPI_D3D11)
  50. return Release_D3D11();
  51. #endif
  52. }
  53. void ConstantBuffer::OnDeviceReset()
  54. {
  55. GAPI gapi = Graphics::GetGAPI();
  56. #ifdef URHO3D_OPENGL
  57. if (gapi == GAPI_OPENGL)
  58. return OnDeviceReset_OGL();
  59. #endif
  60. #ifdef URHO3D_D3D11
  61. if (gapi == GAPI_D3D11)
  62. return OnDeviceReset_D3D11();
  63. #endif
  64. }
  65. bool ConstantBuffer::SetSize(unsigned size)
  66. {
  67. GAPI gapi = Graphics::GetGAPI();
  68. #ifdef URHO3D_OPENGL
  69. if (gapi == GAPI_OPENGL)
  70. return SetSize_OGL(size);
  71. #endif
  72. #ifdef URHO3D_D3D11
  73. if (gapi == GAPI_D3D11)
  74. return SetSize_D3D11(size);
  75. #endif
  76. return {}; // Prevent warning
  77. }
  78. void ConstantBuffer::Apply()
  79. {
  80. GAPI gapi = Graphics::GetGAPI();
  81. #ifdef URHO3D_OPENGL
  82. if (gapi == GAPI_OPENGL)
  83. return Apply_OGL();
  84. #endif
  85. #ifdef URHO3D_D3D11
  86. if (gapi == GAPI_D3D11)
  87. return Apply_D3D11();
  88. #endif
  89. }
  90. }