D3D9VertexBuffer.h 5.0 KB

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