OGLConstantBuffer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Precompiled.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/GraphicsImpl.h"
  25. #include "../../Graphics/ConstantBuffer.h"
  26. #include "../../IO/Log.h"
  27. #include "../../DebugNew.h"
  28. namespace Atomic
  29. {
  30. void ConstantBuffer::Release()
  31. {
  32. if (object_.name_)
  33. {
  34. if (!graphics_)
  35. return;
  36. #ifndef GL_ES_VERSION_2_0
  37. graphics_->SetUBO(0);
  38. glDeleteBuffers(1, &object_.name_);
  39. #endif
  40. object_.name_ = 0;
  41. }
  42. shadowData_.Reset();
  43. size_ = 0;
  44. }
  45. void ConstantBuffer::OnDeviceReset()
  46. {
  47. if (size_)
  48. SetSize(size_); // Recreate
  49. }
  50. bool ConstantBuffer::SetSize(unsigned size)
  51. {
  52. if (!size)
  53. {
  54. ATOMIC_LOGERROR("Can not create zero-sized constant buffer");
  55. return false;
  56. }
  57. // Round up to next 16 bytes
  58. size += 15;
  59. size &= 0xfffffff0;
  60. size_ = size;
  61. dirty_ = false;
  62. shadowData_ = new unsigned char[size_];
  63. memset(shadowData_.Get(), 0, size_);
  64. if (graphics_)
  65. {
  66. #ifndef GL_ES_VERSION_2_0
  67. if (!object_.name_)
  68. glGenBuffers(1, &object_.name_);
  69. graphics_->SetUBO(object_.name_);
  70. glBufferData(GL_UNIFORM_BUFFER, size_, shadowData_.Get(), GL_DYNAMIC_DRAW);
  71. #endif
  72. }
  73. return true;
  74. }
  75. void ConstantBuffer::Apply()
  76. {
  77. if (dirty_ && object_.name_)
  78. {
  79. #ifndef GL_ES_VERSION_2_0
  80. graphics_->SetUBO(object_.name_);
  81. glBufferData(GL_UNIFORM_BUFFER, size_, shadowData_.Get(), GL_DYNAMIC_DRAW);
  82. #endif
  83. dirty_ = false;
  84. }
  85. }
  86. }