gfxTextureArray.cpp 4.9 KB

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