OGLVertexBuffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "ArrayPtr.h"
  24. #include "GPUObject.h"
  25. #include "GraphicsDefs.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. /// Recreate the GPU resource and restore data if applicable.
  38. virtual void OnDeviceReset();
  39. /// Release the buffer.
  40. virtual void Release();
  41. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  42. void SetShadowed(bool enable);
  43. /// Set size and vertex elements and dynamic mode. Previous data will be lost.
  44. bool SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic = false);
  45. /// Set all data in the buffer.
  46. bool SetData(const void* data);
  47. /// Set a data range in the buffer. Optionally discard data outside the range.
  48. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  49. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  50. void* Lock(unsigned start, unsigned count, bool discard = false);
  51. /// Unlock the buffer and apply changes to the GPU buffer.
  52. void Unlock();
  53. /// Return whether CPU memory shadowing is enabled.
  54. bool IsShadowed() const { return shadowed_; }
  55. /// Return whether is dynamic.
  56. bool IsDynamic() const { return dynamic_; }
  57. /// Return whether is currently locked.
  58. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  59. /// Return number of vertices.
  60. unsigned GetVertexCount() const {return vertexCount_; }
  61. /// Return vertex size.
  62. unsigned GetVertexSize() const { return vertexSize_; }
  63. /// Return bitmask of vertex elements.
  64. unsigned GetElementMask() const { return elementMask_; }
  65. /// Return offset of a specified element within a vertex.
  66. unsigned GetElementOffset(VertexElement element) const { return elementOffset_[element]; }
  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 in bytes.
  76. static const unsigned elementSize[];
  77. /// Vertex element OpenGL types.
  78. static const unsigned elementType[];
  79. /// Vertex element OpenGL component counts.
  80. static const unsigned elementComponents[];
  81. /// Vertex element OpenGL normalization.
  82. static const unsigned elementNormalize[];
  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. /// Shadow data.
  91. SharedArrayPtr<unsigned char> shadowData_;
  92. /// Number of vertices.
  93. unsigned vertexCount_;
  94. /// Vertex size.
  95. unsigned vertexSize_;
  96. /// Vertex element bitmask.
  97. unsigned elementMask_;
  98. /// Vertex element offsets.
  99. unsigned elementOffset_[MAX_VERTEX_ELEMENTS];
  100. /// Buffer locking state.
  101. LockState lockState_;
  102. /// Lock start vertex.
  103. unsigned lockStart_;
  104. /// Lock number of vertices.
  105. unsigned lockCount_;
  106. /// Scratch buffer for fallback locking.
  107. void* lockScratchData_;
  108. /// Shadowed flag.
  109. bool shadowed_;
  110. /// Dynamic flag.
  111. bool dynamic_;
  112. };
  113. }