IndexBuffer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "GraphicsHandle.h"
  25. RENDERER_BEGIN
  26. /**
  27. * @addtogroup gfx
  28. * @{
  29. */
  30. class DeviceGraphics;
  31. /**
  32. * IndexBuffer manages the GL buffer for element indices.
  33. * It creates, updates and destoies the GL buffer.
  34. */
  35. class IndexBuffer final : public GraphicsHandle
  36. {
  37. public:
  38. /**
  39. * Creates an IndexBuffer object
  40. * @param[in] device DeviceGraphics pointer.
  41. * @param[in] format Index element format.
  42. * @param[in] usage The usage pattern of the index buffer.
  43. * @param[in] data Data pointer, could be nullptr, then nothing is updated to the buffer.
  44. * @param[in] dataByteLength Data's byte length.
  45. * @param[in] numIndices Count of indices.
  46. */
  47. RENDERER_DEFINE_CREATE_METHOD_6(IndexBuffer, init, DeviceGraphics*, IndexFormat, Usage, const void*, size_t, uint32_t)
  48. IndexBuffer();
  49. virtual ~IndexBuffer();
  50. /**
  51. * Initializes the index buffer with necessary options.
  52. * @param[in] device DeviceGraphics pointer.
  53. * @param[in] format Index element format.
  54. * @param[in] usage The usage pattern of the index buffer.
  55. * @param[in] data Data pointer, could be nullptr, then nothing is updated to the buffer.
  56. * @param[in] dataByteLength Data's byte length.
  57. * @param[in] numIndices Count of indices.
  58. */
  59. bool init(DeviceGraphics* device, IndexFormat format, Usage usage, const void* data, size_t dataByteLength, uint32_t numIndices);
  60. /**
  61. * Update data to the GL index buffer
  62. * @param[in] offset Destination offset for filling buffer data.
  63. * @param[in] data Data to be updated.
  64. * @param[in] dataByteLength Data byte length to be updated.
  65. */
  66. void update(uint32_t offset, const void* data, size_t dataByteLength);
  67. /**
  68. * Gets the count of indices.
  69. */
  70. inline uint32_t getCount() const { return _numIndices; }
  71. /**
  72. * Sets the count of indices.
  73. */
  74. inline void setCount(uint32_t numIndices) { _numIndices = numIndices; }
  75. /**
  76. * Gets the format of indices.
  77. */
  78. inline IndexFormat getFormat() const { return _format; }
  79. /**
  80. * Sets the format of indices.
  81. */
  82. inline void setFormat(IndexFormat format) { _format = format; }
  83. /**
  84. * Gets bytes occupied by each index.
  85. */
  86. inline uint32_t getBytesPerIndex() const { return _bytesPerIndex; }
  87. /**
  88. * Sets bytes occupied by each index.
  89. */
  90. inline void setBytesPerIndex(uint32_t bytesPerIndex) {_bytesPerIndex = bytesPerIndex; }
  91. /**
  92. * Gets usage pattern of the index buffer
  93. */
  94. inline Usage getUsage() const { return _usage; }
  95. /**
  96. * Sets usage pattern of the index buffer
  97. */
  98. inline void setUsage(Usage usage) { _usage = usage; }
  99. /**
  100. * Gets byte size of the index buffer
  101. */
  102. inline uint32_t getBytes() const { return _bytes; }
  103. /**
  104. * Sets byte size of the index buffer
  105. */
  106. inline void setBytes(uint32_t bytes) { _bytes = bytes; }
  107. using FetchDataCallback = std::function<uint8_t*(size_t*)>;
  108. void setFetchDataCallback(const FetchDataCallback& cb) { _fetchDataCallback = cb; }
  109. uint8_t* invokeFetchDataCallback(size_t* bytes) {
  110. if (_fetchDataCallback == nullptr)
  111. {
  112. *bytes = 0;
  113. return nullptr;
  114. }
  115. return _fetchDataCallback(bytes);
  116. }
  117. /**
  118. * Destroies the index buffer
  119. */
  120. void destroy();
  121. private:
  122. DeviceGraphics* _device;
  123. IndexFormat _format;
  124. Usage _usage;
  125. uint32_t _numIndices;
  126. uint32_t _bytesPerIndex;
  127. uint32_t _bytes;
  128. bool _needExpandDataStore = true;
  129. FetchDataCallback _fetchDataCallback;
  130. CC_DISALLOW_COPY_ASSIGN_AND_MOVE(IndexBuffer)
  131. };
  132. // end of gfx group
  133. /// @}
  134. RENDERER_END