BsGLVertexArrayObjectManager.cpp 5.7 KB

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