D3D11IndexBuffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #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. OBJECT(IndexBuffer);
  33. public:
  34. /// Construct.
  35. IndexBuffer(Context* context);
  36. /// Destruct.
  37. virtual ~IndexBuffer();
  38. /// Release buffer.
  39. virtual void Release();
  40. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  41. void SetShadowed(bool enable);
  42. /// Set size and vertex elements and dynamic mode. Previous data will be lost.
  43. bool SetSize(unsigned indexCount, bool largeIndices, bool dynamic = false);
  44. /// Set all data in the buffer.
  45. bool SetData(const void* data);
  46. /// Set a data range in the buffer. Optionally discard data outside the range.
  47. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  48. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  49. void* Lock(unsigned start, unsigned count, bool discard = false);
  50. /// Unlock the buffer and apply changes to the GPU buffer.
  51. void Unlock();
  52. /// Return whether CPU memory shadowing is enabled.
  53. bool IsShadowed() const { return shadowed_; }
  54. /// Return whether is dynamic.
  55. bool IsDynamic() const { return dynamic_; }
  56. /// Return whether is currently locked.
  57. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  58. /// Return number of indices.
  59. unsigned GetIndexCount() const { return indexCount_; }
  60. /// Return index size.
  61. unsigned GetIndexSize() const { return indexSize_; }
  62. /// Return used vertex range from index range.
  63. bool GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount);
  64. /// Return CPU memory shadow data.
  65. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  66. /// Return shared array pointer to the CPU memory shadow data.
  67. SharedArrayPtr<unsigned char> GetShadowDataShared() const { return shadowData_; }
  68. private:
  69. /// Create buffer.
  70. bool Create();
  71. /// Update the shadow data to the GPU buffer.
  72. bool UpdateToGPU();
  73. /// Map the GPU buffer into CPU memory.
  74. void* MapBuffer(unsigned start, unsigned count, bool discard);
  75. /// Unmap the GPU buffer.
  76. void UnmapBuffer();
  77. /// Shadow data.
  78. SharedArrayPtr<unsigned char> shadowData_;
  79. /// Number of indices.
  80. unsigned indexCount_;
  81. /// Index size.
  82. unsigned indexSize_;
  83. /// Buffer locking state.
  84. LockState lockState_;
  85. /// Lock start vertex.
  86. unsigned lockStart_;
  87. /// Lock number of vertices.
  88. unsigned lockCount_;
  89. /// Scratch buffer for fallback locking.
  90. void* lockScratchData_;
  91. /// Dynamic flag.
  92. bool dynamic_;
  93. /// Shadowed flag.
  94. bool shadowed_;
  95. };
  96. }