BsTextureEx.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Extensions/BsTextureEx.h"
  4. #include "Extensions/BsAsyncOpEx.h"
  5. #include "BsScriptPixelData.generated.h"
  6. using namespace std::placeholders;
  7. namespace bs
  8. {
  9. HTexture TextureEx::create(PixelFormat format, UINT32 width, UINT32 height, UINT32 depth, TextureType texType,
  10. TextureUsage usage, UINT32 numSamples, bool hasMipmaps, bool gammaCorrection)
  11. {
  12. int numMips = 0;
  13. if (hasMipmaps)
  14. numMips = PixelUtil::getMaxMipmaps(width, height, 1, format);
  15. TEXTURE_DESC texDesc;
  16. texDesc.type = texType;
  17. texDesc.width = width;
  18. texDesc.height = height;
  19. if (texType == TEX_TYPE_3D)
  20. texDesc.depth = depth;
  21. else
  22. texDesc.depth = 1;
  23. texDesc.numMips = numMips;
  24. texDesc.format = format;
  25. texDesc.usage = usage;
  26. texDesc.hwGamma = gammaCorrection;
  27. texDesc.numSamples = numSamples;
  28. return Texture::create(texDesc);
  29. }
  30. PixelFormat TextureEx::getPixelFormat(const HTexture& thisPtr)
  31. {
  32. return thisPtr->getProperties().getFormat();
  33. }
  34. TextureUsage TextureEx::getUsage(const HTexture& thisPtr)
  35. {
  36. return (TextureUsage)thisPtr->getProperties().getUsage();
  37. }
  38. TextureType TextureEx::getType(const HTexture& thisPtr)
  39. {
  40. return thisPtr->getProperties().getTextureType();
  41. }
  42. UINT32 TextureEx::getWidth(const HTexture& thisPtr)
  43. {
  44. return thisPtr->getProperties().getWidth();
  45. }
  46. UINT32 TextureEx::getHeight(const HTexture& thisPtr)
  47. {
  48. return thisPtr->getProperties().getHeight();
  49. }
  50. UINT32 TextureEx::getDepth(const HTexture& thisPtr)
  51. {
  52. return thisPtr->getProperties().getDepth();
  53. }
  54. bool TextureEx::getGammaCorrection(const HTexture& thisPtr)
  55. {
  56. return thisPtr->getProperties().isHardwareGammaEnabled();
  57. }
  58. UINT32 TextureEx::getSampleCount(const HTexture& thisPtr)
  59. {
  60. return thisPtr->getProperties().getNumSamples();
  61. }
  62. UINT32 TextureEx::getMipmapCount(const HTexture& thisPtr)
  63. {
  64. return thisPtr->getProperties().getNumMipmaps();
  65. }
  66. SPtr<PixelData> TextureEx::getPixels(const HTexture& thisPtr, UINT32 face, UINT32 mipLevel)
  67. {
  68. SPtr<PixelData> pixelData = thisPtr->getProperties().allocBuffer(face, mipLevel);
  69. thisPtr->readCachedData(*pixelData, face, mipLevel);
  70. return pixelData;
  71. }
  72. SPtr<AsyncOpEx> TextureEx::getGPUPixels(const HTexture& thisPtr, UINT32 face, UINT32 mipLevel)
  73. {
  74. SPtr<PixelData> readData = thisPtr->getProperties().allocBuffer(face, mipLevel);
  75. AsyncOp asyncOp = thisPtr->readData(readData, face, mipLevel);
  76. std::function<MonoObject*(const AsyncOp&)> asyncOpToMono =
  77. [&readData](const AsyncOp& op)
  78. {
  79. return ScriptPixelData::create(readData);
  80. };
  81. return bs_shared_ptr_new<AsyncOpEx>(asyncOp, asyncOpToMono);
  82. }
  83. void TextureEx::setPixels(const HTexture& thisPtr, const SPtr<PixelData>& data, UINT32 face, UINT32 mipLevel)
  84. {
  85. if (data != nullptr)
  86. thisPtr->writeData(data, face, mipLevel, false);
  87. }
  88. void TextureEx::setPixelsArray(const HTexture& thisPtr, const Vector<Color>& colors, UINT32 face, UINT32 mipLevel)
  89. {
  90. UINT32 numElements = (UINT32)colors.size();
  91. const TextureProperties& props = thisPtr->getProperties();
  92. UINT32 texNumElements = props.getWidth() * props.getHeight() * props.getDepth();
  93. if (texNumElements != numElements)
  94. {
  95. LOGWRN("SetPixels called with incorrect dimensions. Ignoring call.");
  96. return;
  97. }
  98. SPtr<PixelData> pixelData = bs_shared_ptr_new<PixelData>(props.getWidth(), props.getHeight(), props.getDepth(),
  99. props.getFormat());
  100. pixelData->allocateInternalBuffer();
  101. pixelData->setColors(colors);
  102. thisPtr->writeData(pixelData, face, mipLevel, false);
  103. }
  104. }