VertexBuffer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "VertexBuffer.h"
  21. #include "DeviceGraphics.h"
  22. #include "base/CCGLUtils.h"
  23. RENDERER_BEGIN
  24. VertexBuffer::VertexBuffer()
  25. : _device(nullptr)
  26. , _usage(Usage::STATIC)
  27. , _numVertices(0)
  28. , _bytes(0)
  29. {
  30. }
  31. VertexBuffer::~VertexBuffer()
  32. {
  33. destroy();
  34. }
  35. bool VertexBuffer::init(DeviceGraphics* device, VertexFormat* format, Usage usage, const void* data, size_t dataByteLength, uint32_t numVertices)
  36. {
  37. _device = device;
  38. setFormat(format);
  39. _usage = usage;
  40. _numVertices = numVertices;
  41. _needExpandDataStore = true;
  42. // calculate bytes
  43. _bytes = _format->_bytes * numVertices;
  44. // update
  45. glGenBuffers(1, &_glID);
  46. update(0, data, dataByteLength);
  47. // stats
  48. //REFINE: device._stats.ib += _bytes;
  49. return true;
  50. }
  51. void VertexBuffer::setFormat(VertexFormat* format)
  52. {
  53. if (_format == format)
  54. return;
  55. CC_SAFE_RELEASE(_format);
  56. _format = format;
  57. CC_SAFE_RETAIN(_format);
  58. }
  59. void VertexBuffer::update(uint32_t offset, const void* data, size_t dataByteLength)
  60. {
  61. if (_glID == 0)
  62. {
  63. RENDERER_LOGE("The buffer is destroyed");
  64. return;
  65. }
  66. if (data && dataByteLength + offset > _bytes)
  67. {
  68. if (offset) {
  69. RENDERER_LOGE("Failed to update index buffer data, bytes exceed.");
  70. return;
  71. }
  72. else {
  73. _needExpandDataStore = true;
  74. _bytes = offset + dataByteLength;
  75. _numVertices = _bytes / _format->_bytes;
  76. }
  77. }
  78. GLenum glUsage = (GLenum)_usage;
  79. ccBindBuffer(GL_ARRAY_BUFFER, _glID);
  80. if (_needExpandDataStore)
  81. {
  82. glBufferData(GL_ARRAY_BUFFER, _bytes, (const GLvoid*)data, glUsage);
  83. _needExpandDataStore = false;
  84. }
  85. else
  86. {
  87. glBufferSubData(GL_ARRAY_BUFFER, (GLintptr)offset, (GLsizeiptr)dataByteLength, (const GLvoid*)data);
  88. }
  89. ccBindBuffer(GL_ARRAY_BUFFER, 0);
  90. }
  91. void VertexBuffer::destroy()
  92. {
  93. if (_glID == 0)
  94. return;
  95. CC_SAFE_RELEASE_NULL(_format);
  96. ccDeleteBuffers(1, &_glID);
  97. //REFINE: _device._stats.ib -= _bytes;
  98. _glID = 0;
  99. }
  100. #if GFX_DEBUG > 0
  101. static void testVertexBuffer()
  102. {
  103. VertexFormat vertexFmt({
  104. { ATTRIB_NAME_POSITION, AttribType::FLOAT32, 2 }
  105. });
  106. DeviceGraphics* device = nullptr;
  107. float vertex[] = {
  108. -1, 0, 0
  109. -1, 1, 1
  110. };
  111. VertexBuffer* buffer = new VertexBuffer();
  112. buffer->init(device, vertexFmt, Usage::STATIC, vertex, sizeof(vertex), 3);
  113. buffer->release();
  114. }
  115. #endif
  116. RENDERER_END