CmTextureRTTI.h 4.6 KB

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