OGLConstantBuffer.cpp 3.4 KB

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