gfxVertexFormat.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/gfxVertexFormat.h"
  24. #include "platform/profiler.h"
  25. #include "core/util/hashFunction.h"
  26. #include "gfx/gfxDevice.h"
  27. namespace GFXSemantic
  28. {
  29. const String POSITION = String( "POSITION" ).intern();
  30. const String NORMAL = String( "NORMAL" ).intern();
  31. const String BINORMAL = String( "BINORMAL" ).intern();
  32. const String TANGENT = String( "TANGENT" ).intern();
  33. const String TANGENTW = String( "TANGENTW" ).intern();
  34. const String COLOR = String( "COLOR" ).intern();
  35. const String TEXCOORD = String( "TEXCOORD" ).intern();
  36. const String BLENDINDICES = String( "BLENDINDICES" ).intern();
  37. const String BLENDWEIGHT = String( "BLENDWEIGHT" ).intern();
  38. const String PADDING = String( "PADDING" ).intern();
  39. }
  40. U32 GFXVertexElement::getSizeInBytes() const
  41. {
  42. switch ( mType )
  43. {
  44. case GFXDeclType_Float:
  45. return 4;
  46. case GFXDeclType_Float2:
  47. return 8;
  48. case GFXDeclType_Float3:
  49. return 12;
  50. case GFXDeclType_Float4:
  51. return 16;
  52. case GFXDeclType_Color:
  53. return 4;
  54. case GFXDeclType_UByte4:
  55. return 4;
  56. default:
  57. return 0;
  58. };
  59. }
  60. GFXVertexFormat::GFXVertexFormat()
  61. : mDirty( true ),
  62. mHasNormal( false ),
  63. mHasColor( false ),
  64. mHasTangent( false ),
  65. mHasInstancing( false ),
  66. mHasBlendIndices(false),
  67. mTexCoordCount( 0 ),
  68. mSizeInBytes( 0 ),
  69. mDecl( NULL )
  70. {
  71. VECTOR_SET_ASSOCIATION( mElements );
  72. }
  73. void GFXVertexFormat::copy( const GFXVertexFormat &format )
  74. {
  75. mDirty = format.mDirty;
  76. mHasNormal = format.mHasNormal;
  77. mHasTangent = format.mHasTangent;
  78. mHasColor = format.mHasColor;
  79. mHasInstancing = format.mHasInstancing;
  80. mHasBlendIndices = format.mHasBlendIndices;
  81. mTexCoordCount = format.mTexCoordCount;
  82. mSizeInBytes = format.mSizeInBytes;
  83. mDescription = format.mDescription;
  84. mElements = format.mElements;
  85. mDecl = format.mDecl;
  86. }
  87. void GFXVertexFormat::append( const GFXVertexFormat &format, U32 streamIndex )
  88. {
  89. for ( U32 i=0; i < format.getElementCount(); i++ )
  90. {
  91. mElements.increment();
  92. mElements.last() = format.getElement( i );
  93. if ( streamIndex != -1 )
  94. mElements.last().mStreamIndex = streamIndex;
  95. }
  96. mDirty = true;
  97. }
  98. void GFXVertexFormat::clear()
  99. {
  100. mDirty = true;
  101. mElements.clear();
  102. mDecl = NULL;
  103. }
  104. void GFXVertexFormat::addElement( const String& semantic, GFXDeclType type, U32 index, U32 stream )
  105. {
  106. mDirty = true;
  107. mElements.increment();
  108. GFXVertexElement& lastElement = mElements.last();
  109. lastElement.mStreamIndex = stream;
  110. lastElement.mSemantic = semantic.intern();
  111. lastElement.mSemanticIndex = index;
  112. lastElement.mType = type;
  113. }
  114. const String& GFXVertexFormat::getDescription() const
  115. {
  116. if ( mDirty )
  117. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  118. return mDescription;
  119. }
  120. GFXVertexDecl* GFXVertexFormat::getDecl() const
  121. {
  122. if ( !mDecl || mDirty )
  123. const_cast<GFXVertexFormat*>(this)->_updateDecl();
  124. return mDecl;
  125. }
  126. bool GFXVertexFormat::hasNormal() const
  127. {
  128. if ( mDirty )
  129. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  130. return mHasNormal;
  131. }
  132. bool GFXVertexFormat::hasTangent() const
  133. {
  134. if ( mDirty )
  135. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  136. return mHasTangent;
  137. }
  138. bool GFXVertexFormat::hasColor() const
  139. {
  140. if ( mDirty )
  141. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  142. return mHasColor;
  143. }
  144. bool GFXVertexFormat::hasInstancing() const
  145. {
  146. if (mDirty)
  147. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  148. return mHasInstancing;
  149. }
  150. bool GFXVertexFormat::hasBlendIndices() const
  151. {
  152. if ( mDirty )
  153. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  154. return mHasBlendIndices;
  155. }
  156. U32 GFXVertexFormat::getNumBlendIndices() const
  157. {
  158. if ( mDirty )
  159. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  160. if ( !mHasBlendIndices )
  161. return 0;
  162. U32 numIndices = 0;
  163. for ( U32 i=0; i < mElements.size(); i++ )
  164. {
  165. const GFXVertexElement &element = mElements[i];
  166. if ( element.isSemantic( GFXSemantic::BLENDINDICES ) )
  167. numIndices++;
  168. }
  169. return numIndices;
  170. }
  171. U32 GFXVertexFormat::getTexCoordCount() const
  172. {
  173. if ( mDirty )
  174. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  175. return mTexCoordCount;
  176. }
  177. U32 GFXVertexFormat::getSizeInBytes() const
  178. {
  179. if ( mDirty )
  180. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  181. return mSizeInBytes;
  182. }
  183. void GFXVertexFormat::enableInstancing()
  184. {
  185. mHasInstancing = true;
  186. }
  187. void GFXVertexFormat::_updateDirty()
  188. {
  189. PROFILE_SCOPE( GFXVertexFormat_updateDirty );
  190. mTexCoordCount = 0;
  191. mHasColor = false;
  192. mHasBlendIndices = false;
  193. mHasNormal = false;
  194. mHasTangent = false;
  195. mSizeInBytes = 0;
  196. String desc;
  197. for ( U32 i=0; i < mElements.size(); i++ )
  198. {
  199. const GFXVertexElement &element = mElements[i];
  200. desc += String::ToString( "%d,%s,%d,%d\n", element.mStreamIndex,
  201. element.mSemantic.c_str(),
  202. element.mSemanticIndex,
  203. element.mType );
  204. if ( element.isSemantic( GFXSemantic::NORMAL ) )
  205. mHasNormal = true;
  206. else if ( element.isSemantic( GFXSemantic::TANGENT ) )
  207. mHasTangent = true;
  208. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  209. mHasColor = true;
  210. else if ( element.isSemantic( GFXSemantic::TEXCOORD ) )
  211. ++mTexCoordCount;
  212. else if ( element.isSemantic( GFXSemantic::BLENDINDICES ) )
  213. mHasBlendIndices = true;
  214. mSizeInBytes += element.getSizeInBytes();
  215. }
  216. // Intern the string for fast compares later.
  217. mDescription = desc.intern();
  218. mDirty = false;
  219. }
  220. void GFXVertexFormat::_updateDecl()
  221. {
  222. PROFILE_SCOPE( GFXVertexFormat_updateDecl );
  223. if ( mDirty )
  224. _updateDirty();
  225. mDecl = GFX->allocVertexDecl( this );
  226. }