gfxGLVertexDecl.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "gfx/gl/gfxGLDevice.h"
  2. #include "gfx/gl/gfxGLStateCache.h"
  3. #include "gfx/gl/gfxGLVertexAttribLocation.h"
  4. #include "gfx/gl/gfxGLVertexDecl.h"
  5. void GFXGLVertexDecl::init(const GFXVertexFormat *format)
  6. {
  7. AssertFatal(!mFormat, "");
  8. mFormat = format;
  9. for(int i = 0; i < GFXGL->getNumVertexStreams(); ++i)
  10. _initVerticesFormat(i);
  11. }
  12. void GFXGLVertexDecl::prepareVertexFormat() const
  13. {
  14. AssertFatal(mFormat, "GFXGLVertexDecl - Not inited");
  15. if( GFXGL->mCapabilities.vertexAttributeBinding )
  16. {
  17. for ( U32 i=0; i < glVerticesFormat.size(); i++ )
  18. {
  19. const glVertexAttribData &glElement = glVerticesFormat[i];
  20. glVertexAttribFormat( glElement.attrIndex, glElement.elementCount, glElement.type, glElement.normalized, (uintptr_t)glElement.pointerFirst );
  21. glVertexAttribBinding( glElement.attrIndex, glElement.stream );
  22. }
  23. updateActiveVertexAttrib( GFXGL->getOpenglCache()->getCacheVertexAttribActive() );
  24. return;
  25. }
  26. }
  27. void GFXGLVertexDecl::prepareBuffer_old(U32 stream, GLint mBuffer, GLint mDivisor) const
  28. {
  29. PROFILE_SCOPE(GFXGLVertexDecl_prepare);
  30. AssertFatal(mFormat, "GFXGLVertexDecl - Not inited");
  31. if( GFXGL->mCapabilities.vertexAttributeBinding )
  32. return;
  33. // Bind the buffer...
  34. glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
  35. GFXGL->getOpenglCache()->setCacheBinded(GL_ARRAY_BUFFER, mBuffer);
  36. // Loop thru the vertex format elements adding the array state...
  37. for ( U32 i=0; i < glVerticesFormat.size(); i++ )
  38. {
  39. // glEnableVertexAttribArray are called and cache in GFXGLDevice::preDrawPrimitive
  40. const glVertexAttribData &e = glVerticesFormat[i];
  41. if(e.stream != stream)
  42. continue;
  43. glVertexAttribPointer(
  44. e.attrIndex, // attribute
  45. e.elementCount, // number of elements per vertex, here (r,g,b)
  46. e.type, // the type of each element
  47. e.normalized, // take our values as-is
  48. e.stride, // stride between each position
  49. e.pointerFirst // offset of first element
  50. );
  51. glVertexAttribDivisor( e.attrIndex, mDivisor );
  52. }
  53. }
  54. void GFXGLVertexDecl::updateActiveVertexAttrib(U32 lastActiveMask) const
  55. {
  56. AssertFatal(mVertexAttribActiveMask, "GFXGLVertexDecl::updateActiveVertexAttrib - No vertex attribute are active");
  57. U32 lastActiveVerxtexAttrib = GFXGL->getOpenglCache()->getCacheVertexAttribActive();
  58. if(mVertexAttribActiveMask == lastActiveVerxtexAttrib)
  59. return;
  60. U32 forActiveMask = mVertexAttribActiveMask & ~lastActiveVerxtexAttrib;
  61. U32 forDeactiveMask = ~mVertexAttribActiveMask & lastActiveVerxtexAttrib;
  62. for(int i = 0; i < Torque::GL_VertexAttrib_COUNT; ++i)
  63. {
  64. if( BIT(i) & forActiveMask ) //if is active but not in last mask
  65. glEnableVertexAttribArray(i);
  66. else if( BIT(i) & forDeactiveMask ) // if not active but in last mask
  67. glDisableVertexAttribArray(i);
  68. }
  69. GFXGL->getOpenglCache()->setCacheVertexAttribActive(mVertexAttribActiveMask);
  70. }
  71. void GFXGLVertexDecl::_initVerticesFormat2()
  72. {
  73. for( U32 i=0; i < GFXGL->getNumVertexStreams(); ++i )
  74. {
  75. _initVerticesFormat(i);
  76. }
  77. }
  78. void GFXGLVertexDecl::_initVerticesFormat(U32 stream)
  79. {
  80. U32 buffer = 0;
  81. U32 vertexSize = 0;
  82. for ( U32 i=0; i < mFormat->getElementCount(); i++ )
  83. {
  84. const GFXVertexElement &element = mFormat->getElement( i );
  85. if(element.getStreamIndex() != stream)
  86. continue;
  87. AssertFatal(!mFormat->hasBlendIndices() || !element.isSemantic(GFXSemantic::TEXCOORD) || (mFormat->hasBlendIndices() && element.isSemantic(GFXSemantic::TEXCOORD) && element.getSemanticIndex() < 2), "skinning with more than 2 used texcoords!");
  88. vertexSize += element.getSizeInBytes();
  89. }
  90. // Loop thru the vertex format elements adding the array state...
  91. U32 texCoordIndex = 0;
  92. for ( U32 i=0; i < mFormat->getElementCount(); i++ )
  93. {
  94. const GFXVertexElement &element = mFormat->getElement( i );
  95. if(element.getStreamIndex() != stream)
  96. continue;
  97. glVerticesFormat.increment();
  98. glVertexAttribData &glElement = glVerticesFormat.last();
  99. glElement.stream = element.getStreamIndex();
  100. if ( element.isSemantic( GFXSemantic::POSITION ) )
  101. {
  102. glElement.attrIndex = Torque::GL_VertexAttrib_Position;
  103. glElement.elementCount = element.getSizeInBytes() / 4;
  104. glElement.normalized = false;
  105. glElement.type = GL_FLOAT;
  106. glElement.stride = vertexSize;
  107. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  108. buffer += element.getSizeInBytes();
  109. }
  110. else if ( element.isSemantic( GFXSemantic::NORMAL ) )
  111. {
  112. glElement.attrIndex = Torque::GL_VertexAttrib_Normal;
  113. glElement.elementCount = 3;
  114. glElement.normalized = false;
  115. glElement.type = GL_FLOAT;
  116. glElement.stride = vertexSize;
  117. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  118. buffer += element.getSizeInBytes();
  119. }
  120. else if ( element.isSemantic( GFXSemantic::TANGENT ) )
  121. {
  122. glElement.attrIndex = Torque::GL_VertexAttrib_Tangent;
  123. glElement.elementCount = 3;
  124. glElement.normalized = false;
  125. glElement.type = GL_FLOAT;
  126. glElement.stride = vertexSize;
  127. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  128. buffer += element.getSizeInBytes();
  129. }
  130. else if ( element.isSemantic( GFXSemantic::TANGENTW ) )
  131. {
  132. glElement.attrIndex = Torque::GL_VertexAttrib_TangentW;
  133. glElement.elementCount = element.getSizeInBytes()/4;
  134. glElement.normalized = false;
  135. glElement.type = GL_FLOAT;
  136. glElement.stride = vertexSize;
  137. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  138. buffer += element.getSizeInBytes();
  139. }
  140. else if ( element.isSemantic( GFXSemantic::BINORMAL ) )
  141. {
  142. glElement.attrIndex = Torque::GL_VertexAttrib_Binormal;
  143. glElement.elementCount = 3;
  144. glElement.normalized = false;
  145. glElement.type = GL_FLOAT;
  146. glElement.stride = vertexSize;
  147. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  148. buffer += element.getSizeInBytes();
  149. }
  150. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  151. {
  152. glElement.attrIndex = Torque::GL_VertexAttrib_Color;
  153. glElement.elementCount = element.getSizeInBytes();
  154. glElement.normalized = true;
  155. glElement.type = GL_UNSIGNED_BYTE;
  156. glElement.stride = vertexSize;
  157. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  158. buffer += element.getSizeInBytes();
  159. }
  160. else if ( element.isSemantic( GFXSemantic::BLENDWEIGHT ) )
  161. {
  162. glElement.attrIndex = Torque::GL_VertexAttrib_BlendWeight0 + element.getSemanticIndex();
  163. glElement.elementCount = 4;
  164. glElement.normalized = false;
  165. glElement.type = GL_FLOAT;
  166. glElement.stride = vertexSize;
  167. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  168. buffer += element.getSizeInBytes();
  169. }
  170. else if ( element.isSemantic( GFXSemantic::BLENDINDICES ) )
  171. {
  172. glElement.attrIndex = Torque::GL_VertexAttrib_BlendIndex0 + element.getSemanticIndex();
  173. glElement.elementCount = 4;
  174. glElement.normalized = false;
  175. glElement.type = GL_UNSIGNED_BYTE;
  176. glElement.stride = vertexSize;
  177. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  178. buffer += element.getSizeInBytes();
  179. }
  180. else // Everything else is a texture coordinate.
  181. {
  182. String name = element.getSemantic();
  183. glElement.elementCount = element.getSizeInBytes() / 4;
  184. texCoordIndex = getMax(texCoordIndex, element.getSemanticIndex());
  185. glElement.attrIndex = Torque::GL_VertexAttrib_TexCoord0 + texCoordIndex;
  186. glElement.normalized = false;
  187. glElement.type = GL_FLOAT;
  188. glElement.stride = vertexSize;
  189. glElement.pointerFirst = (void*)(uintptr_t)buffer;
  190. buffer += element.getSizeInBytes();
  191. ++texCoordIndex;
  192. }
  193. AssertFatal(!( mVertexAttribActiveMask & BIT(glElement.attrIndex) ), "GFXGLVertexBuffer::_initVerticesFormat - Duplicate vertex attrib index");
  194. mVertexAttribActiveMask |= BIT(glElement.attrIndex);
  195. }
  196. mVertexSize[stream] = vertexSize;
  197. AssertFatal(vertexSize == buffer, "");
  198. }