IndexBuffer.h 4.6 KB

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