gfxVertexFormat.cpp 5.8 KB

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