OGLConstantBuffer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Graphics/Graphics.h"
  23. #include "../../Graphics/GraphicsImpl.h"
  24. #include "../../IO/Log.h"
  25. #include "../../Graphics/ConstantBuffer.h"
  26. #include "../../DebugNew.h"
  27. namespace Atomic
  28. {
  29. ConstantBuffer::ConstantBuffer(Context* context) :
  30. Object(context),
  31. GPUObject(GetSubsystem<Graphics>())
  32. {
  33. }
  34. ConstantBuffer::~ConstantBuffer()
  35. {
  36. Release();
  37. }
  38. void ConstantBuffer::Release()
  39. {
  40. if (object_)
  41. {
  42. if (!graphics_)
  43. return;
  44. #ifndef GL_ES_VERSION_2_0
  45. graphics_->SetUBO(0);
  46. glDeleteBuffers(1, &object_);
  47. #endif
  48. object_ = 0;
  49. }
  50. shadowData_.Reset();
  51. size_ = 0;
  52. }
  53. void ConstantBuffer::OnDeviceReset()
  54. {
  55. if (size_)
  56. SetSize(size_); // Recreate
  57. }
  58. bool ConstantBuffer::SetSize(unsigned size)
  59. {
  60. if (!size)
  61. {
  62. LOGERROR("Can not create zero-sized constant buffer");
  63. return false;
  64. }
  65. // Round up to next 16 bytes
  66. size += 15;
  67. size &= 0xfffffff0;
  68. size_ = size;
  69. dirty_ = false;
  70. shadowData_ = new unsigned char[size_];
  71. memset(shadowData_.Get(), 0, size_);
  72. if (graphics_)
  73. {
  74. #ifndef GL_ES_VERSION_2_0
  75. if (!object_)
  76. glGenBuffers(1, &object_);
  77. graphics_->SetUBO(object_);
  78. glBufferData(GL_UNIFORM_BUFFER, size_, shadowData_.Get(), GL_DYNAMIC_DRAW);
  79. #endif
  80. }
  81. return true;
  82. }
  83. void ConstantBuffer::SetParameter(unsigned offset, unsigned size, const void* data)
  84. {
  85. if (offset + size > size_)
  86. return; // Would overflow the buffer
  87. memcpy(&shadowData_[offset], data, size);
  88. dirty_ = true;
  89. }
  90. void ConstantBuffer::SetVector3ArrayParameter(unsigned offset, unsigned rows, const void* data)
  91. {
  92. if (offset + rows * 4 * sizeof(float) > size_)
  93. return; // Would overflow the buffer
  94. float* dest = (float*)&shadowData_[offset];
  95. const float* src = (const float*)data;
  96. while (rows--)
  97. {
  98. *dest++ = *src++;
  99. *dest++ = *src++;
  100. *dest++ = *src++;
  101. ++dest; // Skip over the w coordinate
  102. }
  103. dirty_ = true;
  104. }
  105. void ConstantBuffer::Apply()
  106. {
  107. if (dirty_ && object_)
  108. {
  109. #ifndef GL_ES_VERSION_2_0
  110. graphics_->SetUBO(object_);
  111. glBufferData(GL_UNIFORM_BUFFER, size_, shadowData_.Get(), GL_DYNAMIC_DRAW);
  112. #endif
  113. dirty_ = false;
  114. }
  115. }
  116. }