gfxTextureArray.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "gfxTextureArray.h"
  2. #include "gfxDevice.h"
  3. #include "gfxTextureManager.h"
  4. #include "bitmap/imageUtils.h"
  5. #include "console/console.h"
  6. GFXTextureArray::GFXTextureArray()
  7. : mFormat(GFXFormat_COUNT),
  8. mIsCompressed(false),
  9. mWidth(0),
  10. mHeight(0),
  11. mArraySize(0),
  12. mMipLevels(0)
  13. {
  14. }
  15. void GFXTextureArray::set(U32 width, U32 height, U32 size, GFXFormat format, U32 mipLevels)
  16. {
  17. if (mipLevels == 0 && width == height && isPow2(width))
  18. {
  19. mipLevels = mLog2(static_cast<F32>(width)) + 1;
  20. }
  21. if (
  22. mWidth == width &&
  23. mHeight == height &&
  24. mArraySize == size &&
  25. mFormat == format &&
  26. mMipLevels == mipLevels
  27. )
  28. {
  29. return;
  30. }
  31. Release();
  32. mWidth = width;
  33. mHeight = height;
  34. mArraySize = size;
  35. mFormat = format;
  36. mIsCompressed = ImageUtil::isCompressedFormat(mFormat);
  37. mMipLevels = getMax(mipLevels, static_cast<U32>(1));
  38. mTextures.setSize(size);
  39. init();
  40. }
  41. bool GFXTextureArray::fromTextureArray(const Vector<GFXTexHandle>& textureArray, U32 capacity)
  42. {
  43. PROFILE_SCOPE(GFXTextureArray_fromTextureArray)
  44. bool success = true;
  45. // Not initialized, infer it from the given array of textures
  46. if (mArraySize == 0)
  47. {
  48. bool found = false;
  49. for (const GFXTexHandle& texObj : textureArray)
  50. {
  51. if (texObj.isValid())
  52. {
  53. if (!found)
  54. {
  55. found = true;
  56. mFormat = texObj.getFormat();
  57. mWidth = texObj.getWidth();
  58. mHeight = texObj.getHeight();
  59. mMipLevels = texObj->getMipLevels();
  60. }
  61. if (mFormat != texObj.getFormat() || mWidth != texObj.getWidth() || mHeight != texObj.getHeight())
  62. {
  63. AssertWarn(true, "GFXTextureArray::fromTextureArray there was a mismatch in texture formats, defaulting to uncompressed format");
  64. Con::warnf("GFXTextureArray::fromTextureArray there was a mismatch in texture formats, defaulting to uncompressed format");
  65. success = false;
  66. mFormat = GFXFormatR8G8B8A8;
  67. }
  68. }
  69. }
  70. // One might think this should return false in this case, but the return value is mostly to highlight internal errors not input errors.
  71. if (!found) return true;
  72. //---------------------------------------------------------------------------------------
  73. // Create the texture array. Each element in the texture
  74. // array has the same format/dimensions.
  75. //---------------------------------------------------------------------------------------
  76. U32 size = capacity;
  77. if (size == 0)
  78. {
  79. size = textureArray.size();
  80. }
  81. set(mWidth, mHeight, size, mFormat, mMipLevels);
  82. }
  83. //---------------------------------------------------------------------------------------
  84. //---------------------------------------------------------------------------------------
  85. // Copy individual texture elements into texture array.
  86. //---------------------------------------------------------------------------------------
  87. // for each texture element...
  88. for (U32 i = 0; i < textureArray.size(); ++i)
  89. {
  90. if (textureArray[i].isValid())
  91. {
  92. setTexture(textureArray[i], i);
  93. }
  94. }
  95. //---------------------------------------------------------------------------------------
  96. return success;
  97. }
  98. void GFXTextureArray::setTexture(const GFXTexHandle& texture, U32 slot)
  99. {
  100. PROFILE_SCOPE(GFXTextureArray_setTexture)
  101. GFXTexHandle handle = texture;
  102. if (texture->getPath().isNotEmpty())
  103. {
  104. if (texture.getHeight() != mHeight || texture.getWidth() != mWidth || texture.getFormat() != mFormat || texture->getMipLevels() < mMipLevels)
  105. {
  106. if (texture.getHeight() != mHeight || texture.getWidth() != mWidth)
  107. {
  108. AssertWarn(true, avar("GFXTextureArray::setTexture all textures should be the same size: %i vs %i", texture.getHeight(), mHeight));
  109. Con::warnf(avar("GFXTextureArray::setTexture all textures should be the same size: %i vs %i", texture.getHeight(), mHeight));
  110. }
  111. else if (texture->getMipLevels() < mMipLevels)
  112. {
  113. AssertWarn(true, avar("GFXTextureArray::setTexture all textures should have at least the same number of mips: %i vs %i", texture->getMipLevels(), mMipLevels));
  114. Con::warnf(avar("GFXTextureArray::setTexture all textures should have at least the same number of mips: %i vs %i", texture->getMipLevels(), mMipLevels));
  115. }
  116. else
  117. {
  118. AssertWarn(true, "GFXTextureArray::setTexture all textures should have the same format");
  119. Con::warnf("GFXTextureArray::setTexture all textures should have the same format");
  120. }
  121. GBitmap* inBitmap = TEXMGR->loadUncompressedTexture(texture->getPath(), &GFXTexturePersistentProfile, mWidth, mHeight);
  122. if (!inBitmap->setFormat(mFormat))
  123. {
  124. AssertFatal(true, "GFXTextureArray::setTexture all textures must be convertible to GFXFormat " + mFormat);
  125. Con::errorf("GFXTextureArray::setTexture all textures must be convertible to GFXFormat" + mFormat);
  126. handle = NULL;
  127. delete inBitmap;
  128. }
  129. else
  130. {
  131. handle = TEXMGR->createTexture(inBitmap, "", &GFXStaticTextureProfile, true);
  132. }
  133. }
  134. }
  135. if (!handle.isValid())
  136. {
  137. return;
  138. }
  139. if (handle.getHeight() != mHeight || handle.getWidth() != mWidth || handle.getFormat() != mFormat || handle->getMipLevels() < mMipLevels)
  140. {
  141. AssertFatal(true, "GFXTextureArray::setTexture all textures must have the same size and format");
  142. Con::errorf("GFXTextureArray::setTexture all textures must have the same size and format");
  143. return;
  144. }
  145. mTextures[slot] = handle;
  146. _setTexture(handle, slot);
  147. }
  148. void GFXTextureArray::Release()
  149. {
  150. for (GFXTexHandle& mTexture : mTextures)
  151. {
  152. mTexture = NULL;
  153. }
  154. }
  155. const String GFXTextureArray::describeSelf() const
  156. {
  157. // We've got nothing
  158. return String();
  159. }