OGLConstantBuffer.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "../../GraphicsAPI/GraphicsImpl.h"
  7. #include "../../IO/Log.h"
  8. #include "../../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. void ConstantBuffer::Release_OGL()
  12. {
  13. if (object_.name_)
  14. {
  15. if (!graphics_)
  16. return;
  17. #ifndef GL_ES_VERSION_2_0
  18. graphics_->SetUBO_OGL(0);
  19. glDeleteBuffers(1, &object_.name_);
  20. #endif
  21. object_.name_ = 0;
  22. }
  23. shadowData_.Reset();
  24. size_ = 0;
  25. }
  26. void ConstantBuffer::OnDeviceReset_OGL()
  27. {
  28. if (size_)
  29. SetSize_OGL(size_); // Recreate
  30. }
  31. bool ConstantBuffer::SetSize_OGL(unsigned size)
  32. {
  33. if (!size)
  34. {
  35. URHO3D_LOGERROR("Can not create zero-sized constant buffer");
  36. return false;
  37. }
  38. // Round up to next 16 bytes
  39. size += 15;
  40. size &= 0xfffffff0;
  41. size_ = size;
  42. dirty_ = false;
  43. shadowData_ = new unsigned char[size_];
  44. memset(shadowData_.Get(), 0, size_);
  45. if (graphics_)
  46. {
  47. #ifndef GL_ES_VERSION_2_0
  48. if (!object_.name_)
  49. glGenBuffers(1, &object_.name_);
  50. graphics_->SetUBO_OGL(object_.name_);
  51. glBufferData(GL_UNIFORM_BUFFER, size_, shadowData_.Get(), GL_DYNAMIC_DRAW);
  52. #endif
  53. }
  54. return true;
  55. }
  56. void ConstantBuffer::Apply_OGL()
  57. {
  58. if (dirty_ && object_.name_)
  59. {
  60. #ifndef GL_ES_VERSION_2_0
  61. graphics_->SetUBO_OGL(object_.name_);
  62. glBufferData(GL_UNIFORM_BUFFER, size_, shadowData_.Get(), GL_DYNAMIC_DRAW);
  63. #endif
  64. dirty_ = false;
  65. }
  66. }
  67. }