OGLIndexBuffer.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "IndexBuffer.h"
  28. #include "Log.h"
  29. #include <cstring>
  30. #include "DebugNew.h"
  31. OBJECTTYPESTATIC(IndexBuffer);
  32. IndexBuffer::IndexBuffer(Context* context) :
  33. Object(context),
  34. GPUObject(GetSubsystem<Graphics>()),
  35. indexCount_(0),
  36. indexSize_(0),
  37. dynamic_(false),
  38. dataLost_(false)
  39. {
  40. // Force shadowing mode if graphics subsystem does not exist
  41. if (!graphics_)
  42. shadowed_ = true;
  43. }
  44. IndexBuffer::~IndexBuffer()
  45. {
  46. Release();
  47. }
  48. void IndexBuffer::OnDeviceLost()
  49. {
  50. GPUObject::OnDeviceLost();
  51. dataLost_ = true;
  52. }
  53. void IndexBuffer::OnDeviceReset()
  54. {
  55. if (!object_)
  56. {
  57. Create();
  58. if (UpdateToGPU())
  59. dataLost_ = false;
  60. }
  61. }
  62. void IndexBuffer::Release()
  63. {
  64. if (object_ && graphics_)
  65. {
  66. if (graphics_->GetIndexBuffer() == this)
  67. graphics_->SetIndexBuffer(0);
  68. glDeleteBuffers(1, &object_);
  69. object_ = 0;
  70. }
  71. }
  72. void IndexBuffer::SetShadowed(bool enable)
  73. {
  74. // If no graphics subsystem, can not disable shadowing
  75. if (!graphics_)
  76. enable = true;
  77. if (enable != shadowed_)
  78. {
  79. if (enable && indexCount_ && indexSize_)
  80. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  81. else
  82. shadowData_.Reset();
  83. shadowed_ = enable;
  84. }
  85. }
  86. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  87. {
  88. dynamic_ = dynamic;
  89. indexCount_ = indexCount;
  90. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  91. if (shadowed_ && indexCount_ && indexSize_)
  92. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  93. else
  94. shadowData_.Reset();
  95. return Create();
  96. }
  97. bool IndexBuffer::SetData(const void* data)
  98. {
  99. if (!data)
  100. {
  101. LOGERROR("Null pointer for index buffer data");
  102. return false;
  103. }
  104. if (object_)
  105. {
  106. graphics_->SetIndexBuffer(0);
  107. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  108. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  109. }
  110. if (shadowData_ && data != shadowData_.Get())
  111. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  112. return true;
  113. }
  114. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  115. {
  116. if (start == 0 && count == indexCount_)
  117. return SetData(data);
  118. if (!data)
  119. {
  120. LOGERROR("Null pointer for index buffer data");
  121. return false;
  122. }
  123. if (start + count > indexCount_)
  124. {
  125. LOGERROR("Illegal range for setting new index buffer data");
  126. return false;
  127. }
  128. if (!count)
  129. return true;
  130. if (object_)
  131. {
  132. graphics_->SetIndexBuffer(0);
  133. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  134. if (!discard || start != 0)
  135. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, count * indexSize_, data);
  136. else
  137. glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  138. }
  139. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  140. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  141. return true;
  142. }
  143. bool IndexBuffer::UpdateToGPU()
  144. {
  145. if (object_ && shadowData_)
  146. {
  147. graphics_->SetIndexBuffer(0);
  148. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  149. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, shadowData_.Get(), dynamic_ ? GL_DYNAMIC_DRAW :
  150. GL_STATIC_DRAW);
  151. return true;
  152. }
  153. else
  154. return false;
  155. }
  156. void IndexBuffer::ClearDataLost()
  157. {
  158. dataLost_ = false;
  159. }
  160. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  161. {
  162. if (!shadowData_)
  163. return false;
  164. minVertex = M_MAX_UNSIGNED;
  165. unsigned maxVertex = 0;
  166. if (indexSize_ == sizeof(unsigned))
  167. {
  168. unsigned* indices = (unsigned*)shadowData_.Get();
  169. for (unsigned i = 0; i < count; ++i)
  170. {
  171. if (indices[i] < minVertex)
  172. minVertex = indices[i];
  173. if (indices[i] > maxVertex)
  174. maxVertex = indices[i];
  175. }
  176. }
  177. else
  178. {
  179. unsigned short* indices = (unsigned short*)shadowData_.Get();
  180. for (unsigned i = 0; i < count; ++i)
  181. {
  182. if (indices[i] < minVertex)
  183. minVertex = indices[i];
  184. if (indices[i] > maxVertex)
  185. maxVertex = indices[i];
  186. }
  187. }
  188. vertexCount = maxVertex - minVertex + 1;
  189. return true;
  190. }
  191. bool IndexBuffer::Create()
  192. {
  193. if (!indexCount_)
  194. {
  195. Release();
  196. return true;
  197. }
  198. if (graphics_)
  199. {
  200. graphics_->SetIndexBuffer(0);
  201. if (!object_)
  202. glGenBuffers(1, &object_);
  203. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  204. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  205. }
  206. return true;
  207. }