D3D11VertexBuffer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "../../Container/ArrayPtr.h"
  24. #include "../../Graphics/GPUObject.h"
  25. #include "../../Graphics/GraphicsDefs.h"
  26. namespace Atomic
  27. {
  28. /// Hardware vertex buffer.
  29. class ATOMIC_API VertexBuffer : public Object, public GPUObject
  30. {
  31. OBJECT(VertexBuffer);
  32. public:
  33. /// Construct.
  34. VertexBuffer(Context* context);
  35. /// Destruct.
  36. virtual ~VertexBuffer();
  37. /// Release buffer.
  38. virtual void Release();
  39. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  40. void SetShadowed(bool enable);
  41. /// Set size and vertex elements and dynamic mode. Previous data will be lost.
  42. bool SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic = false);
  43. /// Set all data in the buffer.
  44. bool SetData(const void* data);
  45. /// Set a data range in the buffer. Optionally discard data outside the range.
  46. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  47. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  48. void* Lock(unsigned start, unsigned count, bool discard = false);
  49. /// Unlock the buffer and apply changes to the GPU buffer.
  50. void Unlock();
  51. /// Return whether CPU memory shadowing is enabled.
  52. bool IsShadowed() const { return shadowed_; }
  53. /// Return whether is dynamic.
  54. bool IsDynamic() const { return dynamic_; }
  55. /// Return whether is currently locked.
  56. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  57. /// Return number of vertices.
  58. unsigned GetVertexCount() const { return vertexCount_; }
  59. /// Return vertex size.
  60. unsigned GetVertexSize() const { return vertexSize_; }
  61. /// Return bitmask of vertex elements.
  62. unsigned GetElementMask() const { return elementMask_; }
  63. /// Return offset of a specified element within a vertex.
  64. unsigned GetElementOffset(VertexElement element) const { return elementOffset_[element]; }
  65. /// Return buffer hash for building vertex declarations.
  66. unsigned long long GetBufferHash(unsigned streamIndex, unsigned useMask);
  67. /// Return CPU memory shadow data.
  68. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  69. /// Return shared array pointer to the CPU memory shadow data.
  70. SharedArrayPtr<unsigned char> GetShadowDataShared() const { return shadowData_; }
  71. /// Return vertex size corresponding to a vertex element mask.
  72. static unsigned GetVertexSize(unsigned elementMask);
  73. /// Return element offset from an element mask.
  74. static unsigned GetElementOffset(unsigned elementMask, VertexElement element);
  75. /// Vertex element sizes.
  76. static const unsigned elementSize[];
  77. /// Vertex element semantic names.
  78. static const char* elementSemantics[];
  79. /// Vertex element semantic indices.
  80. static const unsigned elementSemanticIndices[];
  81. /// Vertex element formats.
  82. static const unsigned elementFormats[];
  83. private:
  84. /// Update offsets of vertex elements.
  85. void UpdateOffsets();
  86. /// Create buffer.
  87. bool Create();
  88. /// Update the shadow data to the GPU buffer.
  89. bool UpdateToGPU();
  90. /// Map the GPU buffer into CPU memory.
  91. void* MapBuffer(unsigned start, unsigned count, bool discard);
  92. /// Unmap the GPU buffer.
  93. void UnmapBuffer();
  94. /// Shadow data.
  95. SharedArrayPtr<unsigned char> shadowData_;
  96. /// Number of vertices.
  97. unsigned vertexCount_;
  98. /// Vertex size.
  99. unsigned vertexSize_;
  100. /// Vertex element bitmask.
  101. unsigned elementMask_;
  102. /// Vertex element offsets.
  103. unsigned elementOffset_[MAX_VERTEX_ELEMENTS];
  104. /// Buffer locking state.
  105. LockState lockState_;
  106. /// Lock start vertex.
  107. unsigned lockStart_;
  108. /// Lock number of vertices.
  109. unsigned lockCount_;
  110. /// Scratch buffer for fallback locking.
  111. void* lockScratchData_;
  112. /// Dynamic flag.
  113. bool dynamic_;
  114. /// Shadowed flag.
  115. bool shadowed_;
  116. };
  117. }