VertexBuffer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include <functional>
  22. #include "../Macro.h"
  23. #include "../Types.h"
  24. #include "VertexFormat.h"
  25. #include "GraphicsHandle.h"
  26. RENDERER_BEGIN
  27. /**
  28. * @addtogroup gfx
  29. * @{
  30. */
  31. class DeviceGraphics;
  32. /**
  33. * VertexBuffer manages the GL vertex attributes buffer.
  34. * It creates, updates and destoies the GL vertex buffer.
  35. */
  36. class VertexBuffer final : public GraphicsHandle
  37. {
  38. public:
  39. /**
  40. * Creates a vertex buffer object
  41. * @param[in] device DeviceGraphics pointer
  42. * @param[in] format Specifies the vertex attributes layout
  43. * @param[in] usage The usage pattern
  44. * @param[in] data Data pointer, could be nullptr, then nothing is updated to the buffer.
  45. * @param[in] dataByteLength Data's byte length.
  46. * @param[in] numVertices Count of vertices.
  47. */
  48. RENDERER_DEFINE_CREATE_METHOD_6(VertexBuffer, init, DeviceGraphics*, VertexFormat*, Usage, const void*, size_t, uint32_t)
  49. VertexBuffer();
  50. virtual ~VertexBuffer();
  51. /**
  52. * Initializes a vertex buffer object
  53. * @param[in] device DeviceGraphics pointer
  54. * @param[in] format Specifies the vertex attributes layout
  55. * @param[in] usage The usage pattern
  56. * @param[in] data Data pointer, could be nullptr, then nothing is updated to the buffer.
  57. * @param[in] dataByteLength Data's byte length.
  58. * @param[in] numVertices Count of vertices.
  59. */
  60. bool init(DeviceGraphics* device, VertexFormat* format, Usage usage, const void* data, size_t dataByteLength, uint32_t numVertices);
  61. /**
  62. * Update data to the GL vertex buffer
  63. * @param[in] offset Destination offset for filling buffer data.
  64. * @param[in] data Data to be updated.
  65. * @param[in] dataByteLength Data byte length to be updated.
  66. */
  67. void update(uint32_t offset, const void* data, size_t dataByteLength);
  68. /**
  69. * Gets the count of vertices
  70. */
  71. inline uint32_t getCount() const { return _numVertices; }
  72. /**
  73. * Sets the count of vertices
  74. */
  75. inline void setCount(uint32_t numVertices) { _numVertices = numVertices; }
  76. /**
  77. * Gets the attributes layout and format of vertices.
  78. */
  79. inline const VertexFormat& getFormat() const { return *_format; }
  80. // inline void setFormat(VertexFormat&& format) { _format = std::move(format); }
  81. /**
  82. * Sets the attributes layout and format of vertices.
  83. */
  84. void setFormat(VertexFormat* format);
  85. /**
  86. * Gets usage pattern of the vertex buffer
  87. */
  88. inline Usage getUsage() const { return _usage; }
  89. /**
  90. * Sets usage pattern of the vertex buffer
  91. */
  92. inline void setUsage(Usage usage) { _usage = usage; }
  93. /**
  94. * Gets byte size of the vertex buffer
  95. */
  96. inline uint32_t getBytes() const { return _bytes; }
  97. /**
  98. * Sets byte size of the vertex buffer
  99. */
  100. inline void setBytes(uint32_t bytes) { _bytes = bytes; }
  101. using FetchDataCallback = std::function<uint8_t*(size_t*)>;
  102. void setFetchDataCallback(const FetchDataCallback& cb) { _fetchDataCallback = cb; }
  103. uint8_t* invokeFetchDataCallback(size_t* bytes) {
  104. if (_fetchDataCallback == nullptr)
  105. {
  106. *bytes = 0;
  107. return nullptr;
  108. }
  109. return _fetchDataCallback(bytes);
  110. }
  111. /**
  112. * Destroies the vertex buffer
  113. */
  114. void destroy();
  115. private:
  116. DeviceGraphics* _device = nullptr;
  117. VertexFormat* _format = nullptr;
  118. Usage _usage;
  119. uint32_t _numVertices;
  120. uint32_t _bytes;
  121. bool _needExpandDataStore = true;
  122. FetchDataCallback _fetchDataCallback;
  123. CC_DISALLOW_COPY_ASSIGN_AND_MOVE(VertexBuffer)
  124. };
  125. // end of gfx group
  126. /// @}
  127. RENDERER_END