gfxGLVertexDecl.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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( gglHasExtension(ARB_vertex_attrib_binding) )
  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( gglHasExtension(ARB_vertex_attrib_binding) )
  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. vertexSize += element.getSizeInBytes();
  88. }
  89. // Loop thru the vertex format elements adding the array state...
  90. U32 texCoordIndex = 0;
  91. for ( U32 i=0; i < mFormat->getElementCount(); i++ )
  92. {
  93. const GFXVertexElement &element = mFormat->getElement( i );
  94. if(element.getStreamIndex() != stream)
  95. continue;
  96. glVerticesFormat.increment();
  97. glVertexAttribData &glElement = glVerticesFormat.last();
  98. glElement.stream = element.getStreamIndex();
  99. if ( element.isSemantic( GFXSemantic::POSITION ) )
  100. {
  101. glElement.attrIndex = Torque::GL_VertexAttrib_Position;
  102. glElement.elementCount = element.getSizeInBytes() / 4;
  103. glElement.normalized = false;
  104. glElement.type = GL_FLOAT;
  105. glElement.stride = vertexSize;
  106. glElement.pointerFirst = (void*)buffer;
  107. buffer += element.getSizeInBytes();
  108. }
  109. else if ( element.isSemantic( GFXSemantic::NORMAL ) )
  110. {
  111. glElement.attrIndex = Torque::GL_VertexAttrib_Normal;
  112. glElement.elementCount = 3;
  113. glElement.normalized = false;
  114. glElement.type = GL_FLOAT;
  115. glElement.stride = vertexSize;
  116. glElement.pointerFirst = (void*)buffer;
  117. buffer += element.getSizeInBytes();
  118. }
  119. else if ( element.isSemantic( GFXSemantic::TANGENT ) )
  120. {
  121. glElement.attrIndex = Torque::GL_VertexAttrib_Tangent;
  122. glElement.elementCount = 3;
  123. glElement.normalized = false;
  124. glElement.type = GL_FLOAT;
  125. glElement.stride = vertexSize;
  126. glElement.pointerFirst = (void*)buffer;
  127. buffer += element.getSizeInBytes();
  128. }
  129. else if ( element.isSemantic( GFXSemantic::TANGENTW ) )
  130. {
  131. glElement.attrIndex = Torque::GL_VertexAttrib_TangentW;
  132. glElement.elementCount = element.getSizeInBytes()/4;
  133. glElement.normalized = false;
  134. glElement.type = GL_FLOAT;
  135. glElement.stride = vertexSize;
  136. glElement.pointerFirst = (void*)buffer;
  137. buffer += element.getSizeInBytes();
  138. }
  139. else if ( element.isSemantic( GFXSemantic::BINORMAL ) )
  140. {
  141. glElement.attrIndex = Torque::GL_VertexAttrib_Binormal;
  142. glElement.elementCount = 3;
  143. glElement.normalized = false;
  144. glElement.type = GL_FLOAT;
  145. glElement.stride = vertexSize;
  146. glElement.pointerFirst = (void*)buffer;
  147. buffer += element.getSizeInBytes();
  148. }
  149. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  150. {
  151. glElement.attrIndex = Torque::GL_VertexAttrib_Color;
  152. glElement.elementCount = element.getSizeInBytes();
  153. glElement.normalized = true;
  154. glElement.type = GL_UNSIGNED_BYTE;
  155. glElement.stride = vertexSize;
  156. glElement.pointerFirst = (void*)buffer;
  157. buffer += element.getSizeInBytes();
  158. }
  159. else // Everything else is a texture coordinate.
  160. {
  161. String name = element.getSemantic();
  162. glElement.elementCount = element.getSizeInBytes() / 4;
  163. texCoordIndex = getMax(texCoordIndex, element.getSemanticIndex());
  164. glElement.attrIndex = Torque::GL_VertexAttrib_TexCoord0 + texCoordIndex;
  165. glElement.normalized = false;
  166. glElement.type = GL_FLOAT;
  167. glElement.stride = vertexSize;
  168. glElement.pointerFirst = (void*)buffer;
  169. buffer += element.getSizeInBytes();
  170. ++texCoordIndex;
  171. }
  172. AssertFatal(!( mVertexAttribActiveMask & BIT(glElement.attrIndex) ), "GFXGLVertexBuffer::_initVerticesFormat - Duplicate vertex attrib index");
  173. mVertexAttribActiveMask |= BIT(glElement.attrIndex);
  174. }
  175. mVertexSize[stream] = vertexSize;
  176. AssertFatal(vertexSize == buffer, "");
  177. }