gfxTextureArray.cpp 4.8 KB

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