BsGLVertexArrayObjectManager.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "BsGLVertexArrayObjectManager.h"
  2. #include "BsGLVertexBuffer.h"
  3. #include "BsVertexDeclaration.h"
  4. #include "BsGLSLGpuProgram.h"
  5. #include "BsGLHardwareBufferManager.h"
  6. #include "BsRenderStats.h"
  7. #define VBO_BUFFER_OFFSET(i) ((char *)NULL + (i))
  8. namespace BansheeEngine
  9. {
  10. GLVertexArrayObject::GLVertexArrayObject()
  11. :mHandle(0), mVertProgId(0), mAttachedBuffers(nullptr), mNumBuffers(0)
  12. { }
  13. GLVertexArrayObject::GLVertexArrayObject(GLuint handle, UINT64 vertexProgramId,
  14. GLVertexBufferCore** attachedBuffers, UINT32 numBuffers)
  15. :mHandle(handle), mVertProgId(vertexProgramId), mAttachedBuffers(attachedBuffers), mNumBuffers(numBuffers)
  16. { }
  17. ::std::size_t GLVertexArrayObject::Hash::operator()(const GLVertexArrayObject &vao) const
  18. {
  19. std::size_t seed = 0;
  20. hash_combine(seed, vao.mVertProgId);
  21. for (UINT32 i = 0; i < vao.mNumBuffers; i++)
  22. hash_combine(seed, vao.mAttachedBuffers[i]->getGLBufferId());
  23. return seed;
  24. }
  25. bool GLVertexArrayObject::Equal::operator()(const GLVertexArrayObject &a, const GLVertexArrayObject &b) const
  26. {
  27. if (a.mVertProgId != b.mVertProgId)
  28. return false;
  29. if (a.mNumBuffers != b.mNumBuffers)
  30. return false;
  31. for (UINT32 i = 0; i < a.mNumBuffers; i++)
  32. {
  33. if (a.mAttachedBuffers[i]->getGLBufferId() != b.mAttachedBuffers[i]->getGLBufferId())
  34. return false;
  35. }
  36. return true;
  37. }
  38. bool GLVertexArrayObject::operator== (const GLVertexArrayObject& obj)
  39. {
  40. if (mVertProgId != obj.mVertProgId)
  41. return false;
  42. if (mNumBuffers != obj.mNumBuffers)
  43. return false;
  44. for (UINT32 i = 0; i < mNumBuffers; i++)
  45. {
  46. if (mAttachedBuffers[i]->getGLBufferId() != obj.mAttachedBuffers[i]->getGLBufferId())
  47. return false;
  48. }
  49. return true;
  50. }
  51. bool GLVertexArrayObject::operator!= (const GLVertexArrayObject& obj)
  52. {
  53. return !operator==(obj);
  54. }
  55. GLVertexArrayObjectManager::~GLVertexArrayObjectManager()
  56. {
  57. assert(mVAObjects.size() == 0 && "VertexArrayObjectManager getting shut down but not all VA objects were released.");
  58. }
  59. const GLVertexArrayObject& GLVertexArrayObjectManager::getVAO(const SPtr<GLSLGpuProgramCore>& vertexProgram,
  60. const SPtr<VertexDeclarationCore>& vertexDecl, const Vector<SPtr<VertexBufferCore>>& boundBuffers)
  61. {
  62. UINT16 maxStreamIdx = 0;
  63. const List<VertexElement>& decl = vertexDecl->getProperties().getElements();
  64. for (auto& elem : decl)
  65. maxStreamIdx = std::max(maxStreamIdx, elem.getStreamIdx());
  66. UINT32 numStreams = maxStreamIdx + 1;
  67. UINT32 numUsedBuffers = 0;
  68. INT32* streamToSeqIdx = stackAllocN<INT32>(numStreams);
  69. GLVertexBufferCore** usedBuffers = stackAllocN<GLVertexBufferCore*>((UINT32)boundBuffers.size());
  70. memset(usedBuffers, 0, (UINT32)boundBuffers.size() * sizeof(GLVertexBuffer*));
  71. for (UINT32 i = 0; i < numStreams; i++)
  72. streamToSeqIdx[i] = -1;
  73. for (auto& elem : decl)
  74. {
  75. UINT16 streamIdx = elem.getStreamIdx();
  76. if (streamIdx >= (UINT32)boundBuffers.size())
  77. continue;
  78. if (streamToSeqIdx[streamIdx] != -1) // Already visited
  79. continue;
  80. SPtr<VertexBufferCore> vertexBuffer = boundBuffers[streamIdx];
  81. streamToSeqIdx[streamIdx] = (INT32)numUsedBuffers;
  82. if (vertexBuffer != nullptr)
  83. usedBuffers[numUsedBuffers] = static_cast<GLVertexBufferCore*>(vertexBuffer.get());
  84. else
  85. usedBuffers[numUsedBuffers] = nullptr;
  86. numUsedBuffers++;
  87. }
  88. GLVertexArrayObject wantedVAO(0, vertexProgram->getGLHandle(), usedBuffers, numUsedBuffers);
  89. auto findIter = mVAObjects.find(wantedVAO);
  90. if (findIter != mVAObjects.end())
  91. {
  92. stackDeallocLast(usedBuffers);
  93. stackDeallocLast(streamToSeqIdx);
  94. return *findIter; // Found existing, return that
  95. }
  96. // Need to create new VAO
  97. const List<VertexElement>& inputAttributes = vertexProgram->getInputAttributes().getProperties().getElements();
  98. glGenVertexArrays(1, &wantedVAO.mHandle);
  99. glBindVertexArray(wantedVAO.mHandle);
  100. for (auto& elem : decl)
  101. {
  102. UINT16 streamIdx = elem.getStreamIdx();
  103. INT32 seqIdx = streamToSeqIdx[streamIdx];
  104. if (seqIdx == -1)
  105. continue;
  106. bool foundSemantic = false;
  107. GLint attribLocation = 0;
  108. for (auto iter = inputAttributes.begin(); iter != inputAttributes.end(); ++iter)
  109. {
  110. if (iter->getSemantic() == elem.getSemantic() && iter->getSemanticIdx() == elem.getSemanticIdx())
  111. {
  112. foundSemantic = true;
  113. attribLocation = iter->getOffset();
  114. break;
  115. }
  116. }
  117. if (!foundSemantic) // Shader needs to have a matching input attribute, otherwise we skip it
  118. continue;
  119. // TODO - We might also want to check the size of input and buffer, and make sure they match? Or does OpenGL handle that internally?
  120. GLVertexBufferCore* vertexBuffer = usedBuffers[seqIdx];
  121. const VertexBufferProperties& vbProps = vertexBuffer->getProperties();
  122. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->getGLBufferId());
  123. void* bufferData = VBO_BUFFER_OFFSET(elem.getOffset());
  124. GLboolean normalized = GL_FALSE;
  125. switch (elem.getType())
  126. {
  127. case VET_COLOR:
  128. case VET_COLOR_ABGR:
  129. case VET_COLOR_ARGB:
  130. normalized = GL_TRUE;
  131. break;
  132. default:
  133. break;
  134. };
  135. UINT16 typeCount = VertexElement::getTypeCount(elem.getType());
  136. GLenum glType = GLHardwareBufferCoreManager::getGLType(elem.getType());
  137. GLsizei vertexSize = static_cast<GLsizei>(vbProps.getVertexSize());
  138. glVertexAttribPointer(attribLocation, typeCount, glType, normalized,
  139. vertexSize, bufferData);
  140. glEnableVertexAttribArray(attribLocation);
  141. }
  142. wantedVAO.mAttachedBuffers = (GLVertexBufferCore**)bs_alloc(numUsedBuffers * sizeof(GLVertexBufferCore*));
  143. for (UINT32 i = 0; i < numUsedBuffers; i++)
  144. {
  145. wantedVAO.mAttachedBuffers[i] = usedBuffers[i];
  146. usedBuffers[i]->registerVAO(wantedVAO);
  147. }
  148. stackDeallocLast(usedBuffers);
  149. stackDeallocLast(streamToSeqIdx);
  150. auto iter = mVAObjects.insert(wantedVAO);
  151. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_VertexArrayObject);
  152. return *iter.first;
  153. }
  154. // Note: This must receieve a copy and not a ref because original will be destroyed
  155. void GLVertexArrayObjectManager::notifyBufferDestroyed(GLVertexArrayObject vao)
  156. {
  157. mVAObjects.erase(vao);
  158. for (UINT32 i = 0; i < vao.mNumBuffers; i++)
  159. {
  160. vao.mAttachedBuffers[i]->unregisterVAO(vao);
  161. }
  162. glDeleteVertexArrays(1, &vao.mHandle);
  163. bs_free(vao.mAttachedBuffers);
  164. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_VertexArrayObject);
  165. }
  166. }