BsLightProbeVolumeRTTI.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Reflection/BsRTTIType.h"
  6. #include "Renderer/BsLightProbeVolume.h"
  7. #include "Renderer/BsRenderer.h"
  8. #include "CoreThread/BsCoreThread.h"
  9. #include "RTTI/BsTextureRTTI.h"
  10. namespace bs
  11. {
  12. /** @cond RTTI */
  13. /** @addtogroup RTTI-Impl-Engine
  14. * @{
  15. */
  16. BS_ALLOW_MEMCPY_SERIALIZATION(LightProbeSHCoefficients)
  17. /** Serializable information about a single light probe. */
  18. struct SavedLightProbeInfo
  19. {
  20. Vector<Vector3> positions;
  21. Vector<LightProbeSHCoefficients> coefficients;
  22. };
  23. template<> struct RTTIPlainType<SavedLightProbeInfo>
  24. {
  25. enum { id = TID_SavedLightProbeInfo }; enum { hasDynamicSize = 1 };
  26. static void toMemory(const SavedLightProbeInfo& data, char* memory)
  27. {
  28. UINT32 size = getDynamicSize(data);
  29. UINT32 curSize = sizeof(UINT32);
  30. memcpy(memory, &size, curSize);
  31. memory += curSize;
  32. UINT32 version = 0;
  33. memory = rttiWriteElem(version, memory);
  34. memory = rttiWriteElem(data.positions, memory);
  35. memory = rttiWriteElem(data.coefficients, memory);
  36. }
  37. static UINT32 fromMemory(SavedLightProbeInfo& data, char* memory)
  38. {
  39. UINT32 size;
  40. memcpy(&size, memory, sizeof(UINT32));
  41. memory += sizeof(UINT32);
  42. UINT32 version;
  43. memory = rttiReadElem(version, memory);
  44. switch(version)
  45. {
  46. case 0:
  47. rttiReadElem(data.positions, memory);
  48. rttiReadElem(data.coefficients, memory);
  49. break;
  50. default:
  51. LOGERR("Unknown version of SavedLightProbeInfo data. Unable to deserialize.");
  52. break;
  53. }
  54. return size;
  55. }
  56. static UINT32 getDynamicSize(const SavedLightProbeInfo& data)
  57. {
  58. UINT64 dataSize = rttiGetElemSize(data.positions) + rttiGetElemSize(data.coefficients) + sizeof(UINT32) * 2;
  59. #if BS_DEBUG_MODE
  60. if(dataSize > std::numeric_limits<UINT32>::max())
  61. {
  62. BS_EXCEPT(InternalErrorException, "Data overflow! Size doesn't fit into 32 bits.");
  63. }
  64. #endif
  65. return (UINT32)dataSize;
  66. }
  67. };
  68. class BS_CORE_EXPORT LightProbeVolumeRTTI : public RTTIType <LightProbeVolume, IReflectable, LightProbeVolumeRTTI>
  69. {
  70. private:
  71. BS_BEGIN_RTTI_MEMBERS
  72. BS_RTTI_MEMBER_REFL(mTransform, 0)
  73. BS_RTTI_MEMBER_PLAIN(mActive, 1)
  74. BS_RTTI_MEMBER_PLAIN(mMobility, 2)
  75. BS_RTTI_MEMBER_PLAIN(mVolume, 3)
  76. BS_RTTI_MEMBER_PLAIN(mCellCount, 4)
  77. BS_END_RTTI_MEMBERS
  78. SavedLightProbeInfo& getProbeInfo(LightProbeVolume* obj)
  79. {
  80. obj->updateCoefficients();
  81. UINT32 numProbes = (UINT32)obj->mProbes.size();
  82. SavedLightProbeInfo savedLightProbeInfo;
  83. savedLightProbeInfo.coefficients.resize(numProbes);
  84. savedLightProbeInfo.positions.resize(numProbes);
  85. UINT32 idx = 0;
  86. for(auto& entry : obj->mProbes)
  87. {
  88. savedLightProbeInfo.positions[idx] = entry.second.position;
  89. savedLightProbeInfo.coefficients[idx] = entry.second.coefficients;
  90. idx++;
  91. }
  92. obj->mRTTIData = savedLightProbeInfo;
  93. return any_cast_ref<SavedLightProbeInfo>(obj->mRTTIData);
  94. }
  95. void setProbeInfo(LightProbeVolume* obj, SavedLightProbeInfo& data)
  96. {
  97. obj->mProbes.clear();
  98. UINT32 numProbes = (UINT32)data.positions.size();
  99. for(UINT32 i = 0; i < numProbes; ++i)
  100. {
  101. UINT32 handle = obj->mNextProbeId++;
  102. LightProbeVolume::ProbeInfo probeInfo;
  103. probeInfo.flags = LightProbeFlags::Clean;
  104. probeInfo.position = data.positions[i];
  105. probeInfo.coefficients = data.coefficients[i];
  106. obj->mProbes[handle] = probeInfo;
  107. }
  108. }
  109. public:
  110. LightProbeVolumeRTTI()
  111. :mInitMembers(this)
  112. {
  113. addPlainField("mProbeInfo", 5, &LightProbeVolumeRTTI::getProbeInfo, &LightProbeVolumeRTTI::setProbeInfo,
  114. RTTI_Flag_SkipInReferenceSearch);
  115. }
  116. void onSerializationEnded(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  117. {
  118. // Clear temporary data
  119. LightProbeVolume* volume = static_cast<LightProbeVolume*>(obj);
  120. volume->mRTTIData = nullptr;
  121. }
  122. void onDeserializationEnded(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  123. {
  124. // Note: Since this is a CoreObject I should call initialize() right after deserialization,
  125. // but since this specific type is used in Components we delay initialization until Component
  126. // itself does it. Keep this is mind in case this ever needs to be deserialized for non-Component
  127. // purposes (you'll need to call initialize manually).
  128. }
  129. const String& getRTTIName() override
  130. {
  131. static String name = "LightProbeVolume";
  132. return name;
  133. }
  134. UINT32 getRTTIId() override
  135. {
  136. return TID_LightProbeVolume;
  137. }
  138. SPtr<IReflectable> newRTTIObject() override
  139. {
  140. return LightProbeVolume::createEmpty();
  141. }
  142. };
  143. /** @} */
  144. /** @endcond */
  145. }