BsTextureData.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "BsTextureData.h"
  2. #include "BsException.h"
  3. namespace BansheeEngine
  4. {
  5. TextureData::TextureData(UINT32 width, UINT32 height, UINT32 size,
  6. PixelFormat format, UINT32 depth, INT32 flags, UINT32 numMipmaps)
  7. :mWidth(width), mHeight(height), mDepth(depth), mSize(size), mFormat(format),
  8. mFlags(flags), mNumMipmaps(numMipmaps)
  9. {
  10. mBPP = static_cast<UINT8>(PixelUtil::getNumElemBytes(mFormat)) * 8;
  11. mData = (UINT8*)bs_alloc<ScratchAlloc>(size);
  12. }
  13. TextureData::~TextureData()
  14. {
  15. if(mData != nullptr)
  16. bs_free<ScratchAlloc>(mData);
  17. }
  18. void TextureData::getPixels(UINT32 mip, PixelData& output)
  19. {
  20. if(mip < 0 || mip > mNumMipmaps)
  21. {
  22. BS_EXCEPT(InvalidParametersException, "Mip out of range: " + toString(mip) + ". While maximum available mip is: " + toString((mNumMipmaps)));
  23. }
  24. // Calculate mipmap offset and size
  25. UINT8 *offset = const_cast<UINT8*>(getData());
  26. UINT8 offsetSoFar = 0;
  27. UINT32 width = getWidth(), height = getHeight(), depth = getDepth();
  28. UINT32 numMips = getNumMipmaps();
  29. // Figure out the offsets
  30. UINT32 finalWidth = 0, finalHeight = 0, finalDepth = 0;
  31. for(UINT32 curMip = 0; curMip <= numMips; ++curMip)
  32. {
  33. if (curMip == mip)
  34. {
  35. offset += offsetSoFar;
  36. finalWidth = width;
  37. finalHeight = height;
  38. finalDepth = depth;
  39. }
  40. offsetSoFar += PixelUtil::getMemorySize(width, height, depth, getFormat());
  41. /// Half size in each dimension
  42. if(width!=1) width /= 2;
  43. if(height!=1) height /= 2;
  44. if(depth!=1) depth /= 2;
  45. }
  46. PixelData myData(finalWidth, finalHeight, finalDepth, getFormat());
  47. myData.setExternalBuffer(offset);
  48. PixelUtil::bulkPixelConversion(myData, output);
  49. }
  50. }