gfxTextureHandle.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/gfxTextureHandle.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/gfxTextureManager.h"
  26. GFXTexHandle GFXTexHandle::ZERO;
  27. GFXTexHandle GFXTexHandle::ONE;
  28. GFXTexHandle GFXTexHandle::ZUP;
  29. GFXTexHandle::GFXTexHandle( GFXTextureObject *obj )
  30. {
  31. StrongObjectRef::set( obj );
  32. }
  33. GFXTexHandle::GFXTexHandle( const GFXTexHandle &handle, const String &desc )
  34. {
  35. StrongObjectRef::set( handle.getPointer() );
  36. #ifdef TORQUE_DEBUG
  37. if ( getPointer() )
  38. getPointer()->mDebugDescription = desc;
  39. #endif
  40. }
  41. GFXTexHandle::GFXTexHandle( const String &texName, GFXTextureProfile *profile, const String &desc )
  42. {
  43. set( texName, profile, desc );
  44. }
  45. GFXTexHandle::GFXTexHandle(const String &texNameR, const String &texNameG, const String &texNameB, const String &texNameA, U32 inputKey[4], GFXTextureProfile *profile, const String &desc)
  46. {
  47. set(texNameR, texNameG, texNameB, texNameA, inputKey, profile, desc);
  48. }
  49. bool GFXTexHandle::set( const String &texName, GFXTextureProfile *profile, const String &desc )
  50. {
  51. // Clear the existing texture first, so that
  52. // its memory is free for the new allocation.
  53. free();
  54. // Create and set the new texture.
  55. AssertFatal( texName.isNotEmpty(), "Texture name is empty" );
  56. StrongObjectRef::set( TEXMGR->createTexture( texName, profile ) );
  57. #ifdef TORQUE_DEBUG
  58. if ( getPointer() )
  59. getPointer()->mDebugDescription = desc;
  60. #endif
  61. return isValid();
  62. }
  63. bool GFXTexHandle::set(const String &texNameR, const String &texNameG, const String &texNameB, const String &texNameA, U32 inputKey[4], GFXTextureProfile *profile, const String &desc)
  64. {
  65. // Clear the existing texture first, so that
  66. // its memory is free for the new allocation.
  67. free();
  68. StrongObjectRef::set( TEXMGR->createCompositeTexture( texNameR, texNameG, texNameB, texNameA, inputKey, profile ) );
  69. #ifdef TORQUE_DEBUG
  70. if ( getPointer() )
  71. getPointer()->mDebugDescription = desc;
  72. #endif
  73. return isValid();
  74. }
  75. GFXTexHandle::GFXTexHandle( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc )
  76. {
  77. set( bmp, profile, deleteBmp, desc );
  78. }
  79. bool GFXTexHandle::set( GBitmap *bmp, GFXTextureProfile *profile, bool deleteBmp, const String &desc )
  80. {
  81. // Clear the existing texture first, so that
  82. // its memory is free for the new allocation.
  83. free();
  84. // Create and set the new texture.
  85. AssertFatal( bmp, "Bitmap is NULL" );
  86. StrongObjectRef::set( TEXMGR->createTexture( bmp, String(), profile, deleteBmp ) );
  87. #ifdef TORQUE_DEBUG
  88. if ( getPointer() )
  89. getPointer()->mDebugDescription = desc;
  90. #endif
  91. return isValid();
  92. }
  93. GFXTexHandle::GFXTexHandle( DDSFile *dds, GFXTextureProfile *profile, bool deleteDDS, const String &desc )
  94. {
  95. set( dds, profile, deleteDDS, desc );
  96. }
  97. bool GFXTexHandle::set( DDSFile *dds, GFXTextureProfile *profile, bool deleteDDS, const String &desc )
  98. {
  99. // Clear the existing texture first, so that
  100. // its memory is free for the new allocation.
  101. free();
  102. // Create and set the new texture.
  103. AssertFatal( dds, "Bitmap is NULL" );
  104. StrongObjectRef::set( TEXMGR->createTexture( dds, profile, deleteDDS ) );
  105. #ifdef TORQUE_DEBUG
  106. if ( getPointer() )
  107. getPointer()->mDebugDescription = desc;
  108. #endif
  109. return isValid();
  110. }
  111. GFXTexHandle::GFXTexHandle( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels, S32 antialiasLevel)
  112. {
  113. set( width, height, format, profile, desc, numMipLevels, antialiasLevel );
  114. }
  115. bool GFXTexHandle::set( U32 width, U32 height, GFXFormat format, GFXTextureProfile *profile, const String &desc, U32 numMipLevels, S32 antialiasLevel)
  116. {
  117. // Clear the existing texture first, so that
  118. // its memory is free for the new allocation.
  119. free();
  120. // Create and set the new texture.
  121. StrongObjectRef::set( TEXMGR->createTexture( width, height, format, profile, numMipLevels, antialiasLevel ) );
  122. #ifdef TORQUE_DEBUG
  123. if ( getPointer() )
  124. getPointer()->mDebugDescription = desc;
  125. #endif
  126. return isValid();
  127. }
  128. bool GFXTexHandle::set(U32 width, U32 height, U32 depth, GFXFormat format, GFXTextureProfile* profile, const String& desc, U32 numMipLevels)
  129. {
  130. // Clear the existing texture first, so that
  131. // its memory is free for the new allocation.
  132. free();
  133. // Create and set the new texture.
  134. StrongObjectRef::set(TEXMGR->createTexture(width, height, depth, format, profile, numMipLevels));
  135. #ifdef TORQUE_DEBUG
  136. if ( getPointer() )
  137. getPointer()->mDebugDescription = desc;
  138. #endif
  139. return isValid();
  140. }
  141. void GFXTexHandle::refresh()
  142. {
  143. TEXMGR->reloadTexture( getPointer() );
  144. }