CmTextureRTTI.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmRTTIType.h"
  4. #include "CmTexture.h"
  5. #include "CmManagedDataBlock.h"
  6. #include "CmMath.h"
  7. #include "CmApplication.h"
  8. #include "CmCoreThread.h"
  9. #include "CmRenderSystem.h"
  10. #include "CmTextureManager.h"
  11. #include "CmPixelData.h"
  12. namespace CamelotFramework
  13. {
  14. class CM_EXPORT TextureRTTI : public RTTIType<Texture, Resource, TextureRTTI>
  15. {
  16. private:
  17. CM_SETGET_MEMBER(mSize, UINT32, Texture)
  18. CM_SETGET_MEMBER(mHeight, UINT32, Texture)
  19. CM_SETGET_MEMBER(mWidth, UINT32, Texture)
  20. CM_SETGET_MEMBER(mDepth, UINT32, Texture)
  21. CM_SETGET_MEMBER(mNumMipmaps, UINT32, Texture)
  22. CM_SETGET_MEMBER(mHwGamma, bool, Texture)
  23. CM_SETGET_MEMBER(mFSAA, UINT32, Texture)
  24. CM_SETGET_MEMBER(mFSAAHint, String, Texture)
  25. CM_SETGET_MEMBER(mTextureType, TextureType, Texture)
  26. CM_SETGET_MEMBER(mFormat, PixelFormat, Texture)
  27. CM_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. boost::bind(&RenderSystem::readSubresource, RenderSystem::instancePtr(), sharedTexPtr, subresourceIdx, std::static_pointer_cast<GpuResourceData>(pixelData), _1), true);
  39. return pixelData;
  40. }
  41. void setPixelData(Texture* obj, UINT32 idx, PixelDataPtr data)
  42. {
  43. Vector<PixelDataPtr>::type* pixelData = boost::any_cast<Vector<PixelDataPtr>::type*>(obj->mRTTIData);
  44. (*pixelData)[idx] = data;
  45. }
  46. UINT32 getPixelDataArraySize(Texture* obj)
  47. {
  48. return obj->getNumFaces() * (obj->getNumMipmaps() + 1);
  49. }
  50. void setPixelDataArraySize(Texture* obj, UINT32 size)
  51. {
  52. Vector<PixelDataPtr>::type* pixelData = boost::any_cast<Vector<PixelDataPtr>::type*>(obj->mRTTIData);
  53. pixelData->resize(size);
  54. }
  55. public:
  56. TextureRTTI()
  57. {
  58. CM_ADD_PLAINFIELD(mSize, 0, TextureRTTI)
  59. CM_ADD_PLAINFIELD(mHeight, 2, TextureRTTI)
  60. CM_ADD_PLAINFIELD(mWidth, 3, TextureRTTI)
  61. CM_ADD_PLAINFIELD(mDepth, 4, TextureRTTI)
  62. CM_ADD_PLAINFIELD(mNumMipmaps, 5, TextureRTTI)
  63. CM_ADD_PLAINFIELD(mHwGamma, 6, TextureRTTI)
  64. CM_ADD_PLAINFIELD(mFSAA, 7, TextureRTTI)
  65. CM_ADD_PLAINFIELD(mFSAAHint, 8, TextureRTTI)
  66. CM_ADD_PLAINFIELD(mTextureType, 9, TextureRTTI)
  67. CM_ADD_PLAINFIELD(mFormat, 10, TextureRTTI)
  68. CM_ADD_PLAINFIELD(mUsage, 11, TextureRTTI)
  69. addReflectablePtrArrayField("mPixelData", 12, &TextureRTTI::getPixelData, &TextureRTTI::getPixelDataArraySize,
  70. &TextureRTTI::setPixelData, &TextureRTTI::setPixelDataArraySize);
  71. }
  72. virtual void onDeserializationStarted(IReflectable* obj)
  73. {
  74. Texture* texture = static_cast<Texture*>(obj);
  75. texture->mRTTIData = cm_new<Vector<PixelDataPtr>::type, PoolAlloc>();
  76. }
  77. virtual void onDeserializationEnded(IReflectable* obj)
  78. {
  79. Texture* texture = static_cast<Texture*>(obj);
  80. if(texture->mRTTIData.empty())
  81. return;
  82. // A bit clumsy initializing with already set values, but I feel its better than complicating things and storing the values
  83. // in mRTTIData.
  84. texture->initialize(texture->getTextureType(), texture->getWidth(), texture->getHeight(), texture->getDepth(),
  85. texture->getNumMipmaps(), texture->getFormat(), texture->getUsage(), texture->isHardwareGammaEnabled(),
  86. texture->getFSAA(), texture->getFSAAHint());
  87. Vector<PixelDataPtr>::type* pixelData = boost::any_cast<Vector<PixelDataPtr>::type*>(texture->mRTTIData);
  88. for(size_t i = 0; i < pixelData->size(); i++)
  89. {
  90. UINT32 face = (size_t)Math::floor(i / (float)(texture->getNumMipmaps() + 1));
  91. UINT32 mipmap = i % (texture->getNumMipmaps() + 1);
  92. UINT32 subresourceIdx = texture->mapToSubresourceIdx(face, mipmap);
  93. GpuResourcePtr sharedTexPtr = std::static_pointer_cast<GpuResource>(texture->getThisPtr());
  94. pixelData->at(i)->lock();
  95. gCoreThread().queueReturnCommand(boost::bind(&RenderSystem::writeSubresource, RenderSystem::instancePtr(), sharedTexPtr, subresourceIdx, pixelData->at(i), false, _1));
  96. }
  97. cm_delete<PoolAlloc>(pixelData);
  98. texture->mRTTIData = nullptr;
  99. }
  100. virtual const String& getRTTIName()
  101. {
  102. static String name = "Texture";
  103. return name;
  104. }
  105. virtual UINT32 getRTTIId()
  106. {
  107. return TID_Texture;
  108. }
  109. virtual std::shared_ptr<IReflectable> newRTTIObject()
  110. {
  111. return TextureManager::instance().createEmpty();
  112. }
  113. };
  114. }