gfxTextureArray.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. bool success = true;
  44. // Not initialized, infer it from the given array of textures
  45. if (mArraySize == 0)
  46. {
  47. bool found = false;
  48. for (const GFXTexHandle& texObj : textureArray)
  49. {
  50. if (texObj.isValid())
  51. {
  52. if (!found)
  53. {
  54. found = true;
  55. mFormat = texObj.getFormat();
  56. mWidth = texObj.getWidth();
  57. mHeight = texObj.getHeight();
  58. mMipLevels = texObj->getMipLevels();
  59. }
  60. if (mFormat != texObj.getFormat() || mWidth != texObj.getWidth() || mHeight != texObj.getHeight())
  61. {
  62. AssertWarn(true, "GFXTextureArray::fromTextureArray there was a mismatch in texture formats, defaulting to uncompressed format");
  63. Con::warnf("GFXTextureArray::fromTextureArray there was a mismatch in texture formats, defaulting to uncompressed format");
  64. success = false;
  65. mFormat = GFXFormatR8G8B8A8;
  66. }
  67. }
  68. }
  69. // One might think this should return false in this case, but the return value is mostly to highlight internal errors not input errors.
  70. if (!found) return true;
  71. //---------------------------------------------------------------------------------------
  72. // Create the texture array. Each element in the texture
  73. // array has the same format/dimensions.
  74. //---------------------------------------------------------------------------------------
  75. U32 size = capacity;
  76. if (size == 0)
  77. {
  78. size = textureArray.size();
  79. }
  80. set(mWidth, mHeight, size, mFormat, mMipLevels);
  81. }
  82. //---------------------------------------------------------------------------------------
  83. //---------------------------------------------------------------------------------------
  84. // Copy individual texture elements into texture array.
  85. //---------------------------------------------------------------------------------------
  86. // for each texture element...
  87. for (U32 i = 0; i < textureArray.size(); ++i)
  88. {
  89. if (textureArray[i].isValid())
  90. {
  91. setTexture(textureArray[i], i);
  92. }
  93. }
  94. //---------------------------------------------------------------------------------------
  95. return success;
  96. }
  97. void GFXTextureArray::setTexture(const GFXTexHandle& texture, U32 slot)
  98. {
  99. GFXTexHandle handle = texture;
  100. if (texture->getPath().isNotEmpty())
  101. {
  102. if (texture.getHeight() != mHeight || texture.getWidth() != mWidth || texture.getFormat() != mFormat || texture->getMipLevels() < mMipLevels)
  103. {
  104. if (texture.getHeight() != mHeight || texture.getWidth() != mWidth)
  105. {
  106. AssertWarn(true, "GFXTextureArray::setTexture all textures should be the same size");
  107. Con::warnf("GFXTextureArray::setTexture all textures should be the same size");
  108. }
  109. else if (texture->getMipLevels() < mMipLevels)
  110. {
  111. AssertWarn(true, "GFXTextureArray::setTexture all textures should have at least the same number of mips");
  112. Con::warnf("GFXTextureArray::setTexture all textures should have at least the same number of mips");
  113. }
  114. else
  115. {
  116. AssertWarn(true, "GFXTextureArray::setTexture all textures should have the same format");
  117. Con::warnf("GFXTextureArray::setTexture all textures should have the same format");
  118. }
  119. GBitmap* inBitmap = TEXMGR->loadUncompressedTexture(texture->getPath(), &GFXTexturePersistentProfile, mWidth, mHeight);
  120. if (!inBitmap->setFormat(mFormat))
  121. {
  122. AssertFatal(true, "GFXTextureArray::setTexture all textures must be convertible to GFXFormat " + mFormat);
  123. Con::errorf("GFXTextureArray::setTexture all textures must be convertible to GFXFormat" + mFormat);
  124. handle = NULL;
  125. delete inBitmap;
  126. }
  127. else
  128. {
  129. handle = TEXMGR->createTexture(inBitmap, "", &GFXStaticTextureProfile, true);
  130. }
  131. }
  132. }
  133. if (!handle.isValid())
  134. {
  135. return;
  136. }
  137. if (handle.getHeight() != mHeight || handle.getWidth() != mWidth || handle.getFormat() != mFormat || handle->getMipLevels() < mMipLevels)
  138. {
  139. AssertFatal(true, "GFXTextureArray::setTexture all textures must have the same size and format");
  140. Con::errorf("GFXTextureArray::setTexture all textures must have the same size and format");
  141. return;
  142. }
  143. mTextures[slot] = handle;
  144. _setTexture(handle, slot);
  145. }
  146. void GFXTextureArray::Release()
  147. {
  148. for (GFXTexHandle& mTexture : mTextures)
  149. {
  150. mTexture = NULL;
  151. }
  152. }
  153. const String GFXTextureArray::describeSelf() const
  154. {
  155. // We've got nothing
  156. return String();
  157. }