IndexBuffer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Copyright (c) 2008-2020 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. #pragma once
  23. #include "../Core/Object.h"
  24. #include "../Container/ArrayPtr.h"
  25. #include "../Graphics/GPUObject.h"
  26. #include "../Graphics/GraphicsDefs.h"
  27. namespace Urho3D
  28. {
  29. /// Hardware index buffer.
  30. class URHO3D_API IndexBuffer : public Object, public GPUObject
  31. {
  32. URHO3D_OBJECT(IndexBuffer, Object);
  33. public:
  34. /// Construct. Optionally force headless (no GPU-side buffer) operation.
  35. explicit IndexBuffer(Context* context, bool forceHeadless = false);
  36. /// Destruct.
  37. ~IndexBuffer() override;
  38. /// Mark the buffer destroyed on graphics context destruction. May be a no-op depending on the API.
  39. void OnDeviceLost() override;
  40. /// Recreate the buffer and restore data if applicable. May be a no-op depending on the API.
  41. void OnDeviceReset() override;
  42. /// Release buffer.
  43. void Release() override;
  44. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  45. /// @property
  46. void SetShadowed(bool enable);
  47. /// Set size and vertex elements and dynamic mode. Previous data will be lost.
  48. bool SetSize(unsigned indexCount, bool largeIndices, bool dynamic = false);
  49. /// Set all data in the buffer.
  50. bool SetData(const void* data);
  51. /// Set a data range in the buffer. Optionally discard data outside the range.
  52. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  53. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  54. void* Lock(unsigned start, unsigned count, bool discard = false);
  55. /// Unlock the buffer and apply changes to the GPU buffer.
  56. void Unlock();
  57. /// Return whether CPU memory shadowing is enabled.
  58. /// @property
  59. bool IsShadowed() const { return shadowed_; }
  60. /// Return whether is dynamic.
  61. /// @property
  62. bool IsDynamic() const { return dynamic_; }
  63. /// Return whether is currently locked.
  64. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  65. /// Return number of indices.
  66. /// @property
  67. unsigned GetIndexCount() const { return indexCount_; }
  68. /// Return index size in bytes.
  69. /// @property
  70. unsigned GetIndexSize() const { return indexSize_; }
  71. /// Return used vertex range from index range.
  72. bool GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount);
  73. /// Return CPU memory shadow data.
  74. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  75. /// Return shared array pointer to the CPU memory shadow data.
  76. SharedArrayPtr<unsigned char> GetShadowDataShared() const { return shadowData_; }
  77. private:
  78. /// Create buffer.
  79. bool Create();
  80. /// Update the shadow data to the GPU buffer.
  81. bool UpdateToGPU();
  82. /// Map the GPU buffer into CPU memory. Not used on OpenGL.
  83. void* MapBuffer(unsigned start, unsigned count, bool discard);
  84. /// Unmap the GPU buffer. Not used on OpenGL.
  85. void UnmapBuffer();
  86. /// Shadow data.
  87. SharedArrayPtr<unsigned char> shadowData_;
  88. /// Number of indices.
  89. unsigned indexCount_;
  90. /// Index size.
  91. unsigned indexSize_;
  92. /// Buffer locking state.
  93. LockState lockState_;
  94. /// Lock start vertex.
  95. unsigned lockStart_;
  96. /// Lock number of vertices.
  97. unsigned lockCount_;
  98. /// Scratch buffer for fallback locking.
  99. void* lockScratchData_;
  100. /// Dynamic flag.
  101. bool dynamic_;
  102. /// Shadowed flag.
  103. bool shadowed_;
  104. /// Discard lock flag. Used by OpenGL only.
  105. bool discardLock_;
  106. };
  107. }