IndexBuffer.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // This file contains IndexBuffer code common to all graphics APIs.
  23. #include "../Precompiled.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../Graphics/IndexBuffer.h"
  26. #include "../IO/Log.h"
  27. #include "../DebugNew.h"
  28. namespace Atomic
  29. {
  30. IndexBuffer::IndexBuffer(Context* context, bool forceHeadless) :
  31. Object(context),
  32. GPUObject(forceHeadless ? (Graphics*)0 : GetSubsystem<Graphics>()),
  33. indexCount_(0),
  34. indexSize_(0),
  35. lockState_(LOCK_NONE),
  36. lockStart_(0),
  37. lockCount_(0),
  38. lockScratchData_(0),
  39. shadowed_(false),
  40. dynamic_(false),
  41. discardLock_(false)
  42. {
  43. // Force shadowing mode if graphics subsystem does not exist
  44. if (!graphics_)
  45. shadowed_ = true;
  46. }
  47. IndexBuffer::~IndexBuffer()
  48. {
  49. Release();
  50. }
  51. void IndexBuffer::SetShadowed(bool enable)
  52. {
  53. // If no graphics subsystem, can not disable shadowing
  54. if (!graphics_)
  55. enable = true;
  56. if (enable != shadowed_)
  57. {
  58. if (enable && indexCount_ && indexSize_)
  59. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  60. else
  61. shadowData_.Reset();
  62. shadowed_ = enable;
  63. }
  64. }
  65. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  66. {
  67. Unlock();
  68. indexCount_ = indexCount;
  69. indexSize_ = (unsigned)(largeIndices ? sizeof(unsigned) : sizeof(unsigned short));
  70. dynamic_ = dynamic;
  71. if (shadowed_ && indexCount_ && indexSize_)
  72. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  73. else
  74. shadowData_.Reset();
  75. return Create();
  76. }
  77. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  78. {
  79. if (!shadowData_)
  80. {
  81. ATOMIC_LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  82. return false;
  83. }
  84. if (start + count > indexCount_)
  85. {
  86. ATOMIC_LOGERROR("Illegal index range for querying used vertices");
  87. return false;
  88. }
  89. minVertex = M_MAX_UNSIGNED;
  90. unsigned maxVertex = 0;
  91. if (indexSize_ == sizeof(unsigned))
  92. {
  93. unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
  94. for (unsigned i = 0; i < count; ++i)
  95. {
  96. if (indices[i] < minVertex)
  97. minVertex = indices[i];
  98. if (indices[i] > maxVertex)
  99. maxVertex = indices[i];
  100. }
  101. }
  102. else
  103. {
  104. unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
  105. for (unsigned i = 0; i < count; ++i)
  106. {
  107. if (indices[i] < minVertex)
  108. minVertex = indices[i];
  109. if (indices[i] > maxVertex)
  110. maxVertex = indices[i];
  111. }
  112. }
  113. vertexCount = maxVertex - minVertex + 1;
  114. return true;
  115. }
  116. }