CmGLIndexBuffer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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. */
  24. #include "CmGLIndexBuffer.h"
  25. #include "CmGLHardwareBufferManager.h"
  26. #include "CmException.h"
  27. namespace CamelotFramework
  28. {
  29. GLIndexBuffer::GLIndexBuffer(HardwareBufferManager* mgr, IndexType idxType,
  30. UINT32 numIndexes, GpuBufferUsage usage)
  31. : IndexBuffer(mgr, idxType, numIndexes, usage, false)
  32. { }
  33. GLIndexBuffer::~GLIndexBuffer()
  34. { }
  35. void GLIndexBuffer::initialize_internal()
  36. {
  37. glGenBuffers(1, &mBufferId );
  38. if (!mBufferId)
  39. {
  40. CM_EXCEPT(InternalErrorException,
  41. "Cannot create GL index buffer");
  42. }
  43. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  44. // Initialise buffer and set usage
  45. glBufferData(GL_ELEMENT_ARRAY_BUFFER, mSizeInBytes, NULL,
  46. GLHardwareBufferManager::getGLUsage(mUsage));
  47. IndexBuffer::initialize_internal();
  48. }
  49. void GLIndexBuffer::destroy_internal()
  50. {
  51. glDeleteBuffers(1, &mBufferId);
  52. IndexBuffer::destroy_internal();
  53. }
  54. void* GLIndexBuffer::lockImpl(UINT32 offset,
  55. UINT32 length, GpuLockOptions options)
  56. {
  57. GLenum access = 0;
  58. if(mIsLocked)
  59. {
  60. CM_EXCEPT(InternalErrorException,
  61. "Invalid attempt to lock an index buffer that has already been locked");
  62. }
  63. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBufferId);
  64. if ((options == GBL_WRITE_ONLY) || (options == GBL_WRITE_ONLY_NO_OVERWRITE) || (options == GBL_WRITE_ONLY_DISCARD))
  65. {
  66. access = GL_MAP_WRITE_BIT;
  67. if(options == GBL_WRITE_ONLY_DISCARD)
  68. access |= GL_MAP_INVALIDATE_BUFFER_BIT;
  69. else if(options == GBL_WRITE_ONLY_NO_OVERWRITE)
  70. access |= GL_MAP_UNSYNCHRONIZED_BIT;
  71. }
  72. else if (options == GBL_READ_ONLY)
  73. access = GL_MAP_READ_BIT;
  74. else
  75. access = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
  76. void* pBuffer = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, offset, length, access);
  77. if(pBuffer == 0)
  78. {
  79. CM_EXCEPT(InternalErrorException, "Index Buffer: Out of memory");
  80. }
  81. void* retPtr = static_cast<void*>(static_cast<unsigned char*>(pBuffer) + offset);
  82. mIsLocked = true;
  83. return retPtr;
  84. }
  85. void GLIndexBuffer::unlockImpl(void)
  86. {
  87. glBindBuffer(GL_ARRAY_BUFFER, mBufferId);
  88. if(!glUnmapBuffer(GL_ARRAY_BUFFER))
  89. {
  90. CM_EXCEPT(InternalErrorException, "Buffer data corrupted, please reload");
  91. }
  92. mIsLocked = false;
  93. }
  94. void GLIndexBuffer::readData(UINT32 offset, UINT32 length,
  95. void* pDest)
  96. {
  97. void* bufferData = lock(offset, length, GBL_READ_ONLY);
  98. memcpy(pDest, bufferData, length);
  99. unlock();
  100. }
  101. void GLIndexBuffer::writeData(UINT32 offset, UINT32 length,
  102. const void* pSource, BufferWriteType writeFlags)
  103. {
  104. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  105. if(writeFlags == BufferWriteType::Discard)
  106. lockOption = GBL_WRITE_ONLY_DISCARD;
  107. else if(writeFlags == BufferWriteType::NoOverwrite)
  108. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  109. void* bufferData = lock(offset, length, lockOption);
  110. memcpy(bufferData, pSource, length);
  111. unlock();
  112. }
  113. }