CmTextureRTTI.h 4.5 KB

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