BsTextureRTTI.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(mTextureType, TextureType, Texture)
  25. BS_SETGET_MEMBER(mFormat, PixelFormat, Texture)
  26. INT32& getUsage(Texture* obj) { return obj->mUsage; }
  27. void setUsage(Texture* obj, INT32& val)
  28. {
  29. // Render target and depth stencil texture formats are for in-memory use only
  30. // and don't make sense when serialized
  31. if (val == TU_DEPTHSTENCIL || val == TU_RENDERTARGET)
  32. obj->mUsage = TU_STATIC;
  33. else
  34. obj->mUsage = val;
  35. }
  36. #define BS_ADD_PLAINFIELD(name, id, parentType) \
  37. addPlainField(#name, id##, &##parentType##::get##name, &##parentType##::Set##name);
  38. PixelDataPtr getPixelData(Texture* obj, UINT32 idx)
  39. {
  40. UINT32 face = (size_t)Math::floor(idx / (float)(obj->getNumMipmaps() + 1));
  41. UINT32 mipmap = idx % (obj->getNumMipmaps() + 1);
  42. UINT32 subresourceIdx = obj->mapToSubresourceIdx(face, mipmap);
  43. PixelDataPtr pixelData = obj->allocateSubresourceBuffer(subresourceIdx);
  44. GpuResourcePtr sharedTexPtr = std::static_pointer_cast<GpuResource>(obj->getThisPtr());
  45. // We want the data right away so queue directly to main core thread queue and block until we get it
  46. pixelData->_lock();
  47. gCoreThread().queueReturnCommand(
  48. std::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(), sharedTexPtr, subresourceIdx,
  49. std::static_pointer_cast<GpuResourceData>(pixelData), std::placeholders::_1), true);
  50. return pixelData;
  51. }
  52. void setPixelData(Texture* obj, UINT32 idx, PixelDataPtr data)
  53. {
  54. Vector<PixelDataPtr>* pixelData = any_cast<Vector<PixelDataPtr>*>(obj->mRTTIData);
  55. (*pixelData)[idx] = data;
  56. }
  57. UINT32 getPixelDataArraySize(Texture* obj)
  58. {
  59. return obj->getNumFaces() * (obj->getNumMipmaps() + 1);
  60. }
  61. void setPixelDataArraySize(Texture* obj, UINT32 size)
  62. {
  63. Vector<PixelDataPtr>* pixelData = any_cast<Vector<PixelDataPtr>*>(obj->mRTTIData);
  64. pixelData->resize(size);
  65. }
  66. public:
  67. TextureRTTI()
  68. {
  69. BS_ADD_PLAINFIELD(mSize, 0, TextureRTTI)
  70. BS_ADD_PLAINFIELD(mHeight, 2, TextureRTTI)
  71. BS_ADD_PLAINFIELD(mWidth, 3, TextureRTTI)
  72. BS_ADD_PLAINFIELD(mDepth, 4, TextureRTTI)
  73. BS_ADD_PLAINFIELD(mNumMipmaps, 5, TextureRTTI)
  74. BS_ADD_PLAINFIELD(mHwGamma, 6, TextureRTTI)
  75. BS_ADD_PLAINFIELD(mMultisampleCount, 7, TextureRTTI)
  76. BS_ADD_PLAINFIELD(mTextureType, 9, TextureRTTI)
  77. BS_ADD_PLAINFIELD(mFormat, 10, TextureRTTI)
  78. addPlainField("mUsage", 11, &TextureRTTI::getUsage, &TextureRTTI::setUsage);
  79. addReflectablePtrArrayField("mPixelData", 12, &TextureRTTI::getPixelData, &TextureRTTI::getPixelDataArraySize,
  80. &TextureRTTI::setPixelData, &TextureRTTI::setPixelDataArraySize);
  81. }
  82. virtual void onDeserializationStarted(IReflectable* obj)
  83. {
  84. Texture* texture = static_cast<Texture*>(obj);
  85. texture->mRTTIData = bs_new<Vector<PixelDataPtr>, PoolAlloc>();
  86. }
  87. virtual void onDeserializationEnded(IReflectable* obj)
  88. {
  89. Texture* texture = static_cast<Texture*>(obj);
  90. if(texture->mRTTIData.empty())
  91. return;
  92. // A bit clumsy initializing with already set values, but I feel its better than complicating things and storing the values
  93. // in mRTTIData.
  94. texture->initialize(texture->getTextureType(), texture->getWidth(), texture->getHeight(), texture->getDepth(),
  95. texture->getNumMipmaps(), texture->getFormat(), texture->getUsage(), texture->isHardwareGammaEnabled(),
  96. texture->getMultisampleCount());
  97. Vector<PixelDataPtr>* pixelData = any_cast<Vector<PixelDataPtr>*>(texture->mRTTIData);
  98. for(size_t i = 0; i < pixelData->size(); i++)
  99. {
  100. UINT32 face = (size_t)Math::floor(i / (float)(texture->getNumMipmaps() + 1));
  101. UINT32 mipmap = i % (texture->getNumMipmaps() + 1);
  102. UINT32 subresourceIdx = texture->mapToSubresourceIdx(face, mipmap);
  103. GpuResourcePtr sharedTexPtr = std::static_pointer_cast<GpuResource>(texture->getThisPtr());
  104. pixelData->at(i)->_lock();
  105. gCoreThread().queueReturnCommand(std::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(),
  106. sharedTexPtr, subresourceIdx, pixelData->at(i), false, std::placeholders::_1));
  107. }
  108. bs_delete<PoolAlloc>(pixelData);
  109. texture->mRTTIData = nullptr;
  110. }
  111. virtual const String& getRTTIName()
  112. {
  113. static String name = "Texture";
  114. return name;
  115. }
  116. virtual UINT32 getRTTIId()
  117. {
  118. return TID_Texture;
  119. }
  120. virtual std::shared_ptr<IReflectable> newRTTIObject()
  121. {
  122. return TextureManager::instance()._createEmpty();
  123. }
  124. };
  125. }