OGLVertexBuffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "GPUObject.h"
  26. #include "GraphicsDefs.h"
  27. /// Hardware vertex buffer.
  28. class VertexBuffer : public Object, public GPUObject
  29. {
  30. OBJECT(VertexBuffer);
  31. public:
  32. /// Construct.
  33. VertexBuffer(Context* context);
  34. /// Destruct.
  35. virtual ~VertexBuffer();
  36. /// Mark the GPU resource destroyed on context destruction.
  37. virtual void OnDeviceLost();
  38. /// Recreate the GPU resource and restore data if applicable.
  39. virtual void OnDeviceReset();
  40. /// Release the buffer.
  41. virtual void Release();
  42. /// Enable shadowing in CPU memory. Shadowing is forced on if the graphics subsystem does not exist.
  43. void SetShadowed(bool enable);
  44. /// %Set size and vertex elements and dynamic mode. Previous data will be lost.
  45. bool SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic = false);
  46. /// %Set all data in the buffer.
  47. bool SetData(const void* data);
  48. /// %Set a data range in the buffer. Optionally discard data outside the range.
  49. bool SetDataRange(const void* data, unsigned start, unsigned count, bool discard = false);
  50. /// Lock the buffer for write-only editing. Return data pointer if successful. Optionally discard data outside the range.
  51. void* Lock(unsigned start, unsigned count, bool discard = false);
  52. /// Unlock the buffer and apply changes to the GPU buffer.
  53. void Unlock();
  54. /// Return whether CPU memory shadowing is enabled.
  55. bool IsShadowed() const { return shadowed_; }
  56. /// Return whether is dynamic.
  57. bool IsDynamic() const { return dynamic_; }
  58. /// Return whether is currently locked.
  59. bool IsLocked() const { return lockState_ != LOCK_NONE; }
  60. /// Return number of vertices.
  61. unsigned GetVertexCount() const {return vertexCount_; }
  62. /// Return vertex size.
  63. unsigned GetVertexSize() const { return vertexSize_; }
  64. /// Return bitmask of vertex elements.
  65. unsigned GetElementMask() const { return elementMask_; }
  66. /// Return offset of a specified element within a vertex.
  67. unsigned GetElementOffset(VertexElement element) const { return elementOffset_[element]; }
  68. /// Return CPU memory shadow data.
  69. unsigned char* GetShadowData() const { return shadowData_.Get(); }
  70. /// Return vertex size corresponding to a vertex element mask.
  71. static unsigned GetVertexSize(unsigned elementMask);
  72. /// Return element offset from an element mask.
  73. static unsigned GetElementOffset(unsigned elementMask, VertexElement element);
  74. /// Vertex element sizes in bytes.
  75. static const unsigned elementSize[];
  76. /// Vertex element OpenGL types.
  77. static const unsigned elementType[];
  78. /// Vertex element OpenGL component counts.
  79. static const unsigned elementComponents[];
  80. /// Vertex element OpenGL normalization.
  81. static const unsigned elementNormalize[];
  82. /// Vertex element names.
  83. static const String elementName[];
  84. private:
  85. /// Update offsets of vertex elements.
  86. void UpdateOffsets();
  87. /// Create buffer.
  88. bool Create();
  89. /// Update the shadow data to the GPU buffer.
  90. bool UpdateToGPU();
  91. /// Shadow data.
  92. SharedArrayPtr<unsigned char> shadowData_;
  93. /// Number of vertices.
  94. unsigned vertexCount_;
  95. /// Vertex size.
  96. unsigned vertexSize_;
  97. /// Vertex element bitmask.
  98. unsigned elementMask_;
  99. /// Vertex element offsets.
  100. unsigned elementOffset_[MAX_VERTEX_ELEMENTS];
  101. /// Buffer locking state.
  102. LockState lockState_;
  103. /// Lock start vertex.
  104. unsigned lockStart_;
  105. /// Lock number of vertices.
  106. unsigned lockCount_;
  107. /// Scratch buffer for fallback locking.
  108. void* lockScratchData_;
  109. /// Shadowed flag.
  110. bool shadowed_;
  111. /// Dynamic flag.
  112. bool dynamic_;
  113. };