gfxVertexFormat.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. }
  37. U32 GFXVertexElement::getSizeInBytes() const
  38. {
  39. switch ( mType )
  40. {
  41. case GFXDeclType_Float:
  42. return 4;
  43. case GFXDeclType_Float2:
  44. return 8;
  45. case GFXDeclType_Float3:
  46. return 12;
  47. case GFXDeclType_Float4:
  48. return 16;
  49. case GFXDeclType_Color:
  50. return 4;
  51. default:
  52. return 0;
  53. };
  54. }
  55. GFXVertexFormat::GFXVertexFormat()
  56. : mDirty( true ),
  57. mHasColor( false ),
  58. mHasNormal( false ),
  59. mHasTangent( false ),
  60. mHasInstancing( false ),
  61. mTexCoordCount( 0 ),
  62. mSizeInBytes( 0 ),
  63. mDecl( NULL )
  64. {
  65. VECTOR_SET_ASSOCIATION( mElements );
  66. }
  67. void GFXVertexFormat::copy( const GFXVertexFormat &format )
  68. {
  69. mDirty = format.mDirty;
  70. mHasNormal = format.mHasNormal;
  71. mHasTangent = format.mHasTangent;
  72. mHasColor = format.mHasColor;
  73. mHasInstancing = format.mHasInstancing;
  74. mTexCoordCount = format.mTexCoordCount;
  75. mSizeInBytes = format.mSizeInBytes;
  76. mDescription = format.mDescription;
  77. mElements = format.mElements;
  78. mDecl = format.mDecl;
  79. }
  80. void GFXVertexFormat::append( const GFXVertexFormat &format, U32 streamIndex )
  81. {
  82. for ( U32 i=0; i < format.getElementCount(); i++ )
  83. {
  84. mElements.increment();
  85. mElements.last() = format.getElement( i );
  86. if ( streamIndex != -1 )
  87. mElements.last().mStreamIndex = streamIndex;
  88. }
  89. mDirty = true;
  90. }
  91. void GFXVertexFormat::clear()
  92. {
  93. mDirty = true;
  94. mElements.clear();
  95. mDecl = NULL;
  96. }
  97. void GFXVertexFormat::addElement( const String& semantic, GFXDeclType type, U32 index, U32 stream )
  98. {
  99. mDirty = true;
  100. mElements.increment();
  101. GFXVertexElement& lastElement = mElements.last();
  102. lastElement.mStreamIndex = stream;
  103. lastElement.mSemantic = semantic.intern();
  104. lastElement.mSemanticIndex = index;
  105. lastElement.mType = type;
  106. }
  107. const String& GFXVertexFormat::getDescription() const
  108. {
  109. if ( mDirty )
  110. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  111. return mDescription;
  112. }
  113. GFXVertexDecl* GFXVertexFormat::getDecl() const
  114. {
  115. if ( !mDecl || mDirty )
  116. const_cast<GFXVertexFormat*>(this)->_updateDecl();
  117. return mDecl;
  118. }
  119. bool GFXVertexFormat::hasNormal() const
  120. {
  121. if ( mDirty )
  122. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  123. return mHasNormal;
  124. }
  125. bool GFXVertexFormat::hasTangent() const
  126. {
  127. if ( mDirty )
  128. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  129. return mHasTangent;
  130. }
  131. bool GFXVertexFormat::hasColor() const
  132. {
  133. if ( mDirty )
  134. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  135. return mHasColor;
  136. }
  137. bool GFXVertexFormat::hasInstancing() const
  138. {
  139. if (mDirty)
  140. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  141. return mHasInstancing;
  142. }
  143. U32 GFXVertexFormat::getTexCoordCount() const
  144. {
  145. if ( mDirty )
  146. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  147. return mTexCoordCount;
  148. }
  149. U32 GFXVertexFormat::getSizeInBytes() const
  150. {
  151. if ( mDirty )
  152. const_cast<GFXVertexFormat*>(this)->_updateDirty();
  153. return mSizeInBytes;
  154. }
  155. void GFXVertexFormat::enableInstancing()
  156. {
  157. mHasInstancing = true;
  158. }
  159. void GFXVertexFormat::_updateDirty()
  160. {
  161. PROFILE_SCOPE( GFXVertexFormat_updateDirty );
  162. mTexCoordCount = 0;
  163. mHasColor = false;
  164. mHasNormal = false;
  165. mHasTangent = false;
  166. mSizeInBytes = 0;
  167. String desc;
  168. for ( U32 i=0; i < mElements.size(); i++ )
  169. {
  170. const GFXVertexElement &element = mElements[i];
  171. desc += String::ToString( "%d,%s,%d,%d\n", element.mStreamIndex,
  172. element.mSemantic.c_str(),
  173. element.mSemanticIndex,
  174. element.mType );
  175. if ( element.isSemantic( GFXSemantic::NORMAL ) )
  176. mHasNormal = true;
  177. else if ( element.isSemantic( GFXSemantic::TANGENT ) )
  178. mHasTangent = true;
  179. else if ( element.isSemantic( GFXSemantic::COLOR ) )
  180. mHasColor = true;
  181. else if ( element.isSemantic( GFXSemantic::TEXCOORD ) )
  182. ++mTexCoordCount;
  183. mSizeInBytes += element.getSizeInBytes();
  184. }
  185. // Intern the string for fast compares later.
  186. mDescription = desc.intern();
  187. mDirty = false;
  188. }
  189. void GFXVertexFormat::_updateDecl()
  190. {
  191. PROFILE_SCOPE( GFXVertexFormat_updateDecl );
  192. if ( mDirty )
  193. _updateDirty();
  194. mDecl = GFX->allocVertexDecl( this );
  195. }