BsTextureRTTI.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsRTTIType.h"
  4. #include "BsTexture.h"
  5. #include "BsManagedDataBlock.h"
  6. #include "BsMath.h"
  7. #include "BsCoreApplication.h"
  8. #include "BsCoreThread.h"
  9. #include "BsRenderSystem.h"
  10. #include "BsTextureManager.h"
  11. #include "BsPixelData.h"
  12. namespace BansheeEngine
  13. {
  14. class BS_CORE_EXPORT TextureRTTI : public RTTIType<Texture, Resource, TextureRTTI>
  15. {
  16. private:
  17. BS_SETGET_MEMBER(mSize, UINT32, Texture)
  18. BS_SETGET_MEMBER(mHeight, UINT32, Texture)
  19. BS_SETGET_MEMBER(mWidth, UINT32, Texture)
  20. BS_SETGET_MEMBER(mDepth, UINT32, Texture)
  21. BS_SETGET_MEMBER(mNumMipmaps, UINT32, Texture)
  22. BS_SETGET_MEMBER(mHwGamma, bool, Texture)
  23. BS_SETGET_MEMBER(mMultisampleCount, UINT32, Texture)
  24. BS_SETGET_MEMBER(mMultisampleHint, String, Texture)
  25. BS_SETGET_MEMBER(mTextureType, TextureType, Texture)
  26. BS_SETGET_MEMBER(mFormat, PixelFormat, Texture)
  27. BS_SETGET_MEMBER(mUsage, INT32, Texture)
  28. PixelDataPtr getPixelData(Texture* obj, UINT32 idx)
  29. {
  30. UINT32 face = (size_t)Math::floor(idx / (float)(obj->getNumMipmaps() + 1));
  31. UINT32 mipmap = idx % (obj->getNumMipmaps() + 1);
  32. UINT32 subresourceIdx = obj->mapToSubresourceIdx(face, mipmap);
  33. PixelDataPtr pixelData = obj->allocateSubresourceBuffer(subresourceIdx);
  34. GpuResourcePtr sharedTexPtr = std::static_pointer_cast<GpuResource>(obj->getThisPtr());
  35. // We want the data right away so queue directly to main core thread queue and block until we get it
  36. pixelData->_lock();
  37. gCoreThread().queueReturnCommand(
  38. std::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(), sharedTexPtr, subresourceIdx,
  39. std::static_pointer_cast<GpuResourceData>(pixelData), std::placeholders::_1), true);
  40. return pixelData;
  41. }
  42. void setPixelData(Texture* obj, UINT32 idx, PixelDataPtr data)
  43. {
  44. Vector<PixelDataPtr>* pixelData = any_cast<Vector<PixelDataPtr>*>(obj->mRTTIData);
  45. (*pixelData)[idx] = data;
  46. }
  47. UINT32 getPixelDataArraySize(Texture* obj)
  48. {
  49. return obj->getNumFaces() * (obj->getNumMipmaps() + 1);
  50. }
  51. void setPixelDataArraySize(Texture* obj, UINT32 size)
  52. {
  53. Vector<PixelDataPtr>* pixelData = any_cast<Vector<PixelDataPtr>*>(obj->mRTTIData);
  54. pixelData->resize(size);
  55. }
  56. public:
  57. TextureRTTI()
  58. {
  59. BS_ADD_PLAINFIELD(mSize, 0, TextureRTTI)
  60. BS_ADD_PLAINFIELD(mHeight, 2, TextureRTTI)
  61. BS_ADD_PLAINFIELD(mWidth, 3, TextureRTTI)
  62. BS_ADD_PLAINFIELD(mDepth, 4, TextureRTTI)
  63. BS_ADD_PLAINFIELD(mNumMipmaps, 5, TextureRTTI)
  64. BS_ADD_PLAINFIELD(mHwGamma, 6, TextureRTTI)
  65. BS_ADD_PLAINFIELD(mMultisampleCount, 7, TextureRTTI)
  66. BS_ADD_PLAINFIELD(mMultisampleHint, 8, TextureRTTI)
  67. BS_ADD_PLAINFIELD(mTextureType, 9, TextureRTTI)
  68. BS_ADD_PLAINFIELD(mFormat, 10, TextureRTTI)
  69. BS_ADD_PLAINFIELD(mUsage, 11, TextureRTTI)
  70. addReflectablePtrArrayField("mPixelData", 12, &TextureRTTI::getPixelData, &TextureRTTI::getPixelDataArraySize,
  71. &TextureRTTI::setPixelData, &TextureRTTI::setPixelDataArraySize);
  72. }
  73. virtual void onDeserializationStarted(IReflectable* obj)
  74. {
  75. Texture* texture = static_cast<Texture*>(obj);
  76. texture->mRTTIData = bs_new<Vector<PixelDataPtr>, PoolAlloc>();
  77. }
  78. virtual void onDeserializationEnded(IReflectable* obj)
  79. {
  80. Texture* texture = static_cast<Texture*>(obj);
  81. if(texture->mRTTIData.empty())
  82. return;
  83. // A bit clumsy initializing with already set values, but I feel its better than complicating things and storing the values
  84. // in mRTTIData.
  85. texture->initialize(texture->getTextureType(), texture->getWidth(), texture->getHeight(), texture->getDepth(),
  86. texture->getNumMipmaps(), texture->getFormat(), texture->getUsage(), texture->isHardwareGammaEnabled(),
  87. texture->getMultisampleCount(), texture->getMultisampleHint());
  88. Vector<PixelDataPtr>* pixelData = any_cast<Vector<PixelDataPtr>*>(texture->mRTTIData);
  89. for(size_t i = 0; i < pixelData->size(); i++)
  90. {
  91. UINT32 face = (size_t)Math::floor(i / (float)(texture->getNumMipmaps() + 1));
  92. UINT32 mipmap = i % (texture->getNumMipmaps() + 1);
  93. UINT32 subresourceIdx = texture->mapToSubresourceIdx(face, mipmap);
  94. GpuResourcePtr sharedTexPtr = std::static_pointer_cast<GpuResource>(texture->getThisPtr());
  95. pixelData->at(i)->_lock();
  96. gCoreThread().queueReturnCommand(std::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(),
  97. sharedTexPtr, subresourceIdx, pixelData->at(i), false, std::placeholders::_1));
  98. }
  99. bs_delete<PoolAlloc>(pixelData);
  100. texture->mRTTIData = nullptr;
  101. }
  102. virtual const String& getRTTIName()
  103. {
  104. static String name = "Texture";
  105. return name;
  106. }
  107. virtual UINT32 getRTTIId()
  108. {
  109. return TID_Texture;
  110. }
  111. virtual std::shared_ptr<IReflectable> newRTTIObject()
  112. {
  113. return TextureManager::instance()._createEmpty();
  114. }
  115. };
  116. }