reflectionManager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _REFLECTIONMANAGER_H_
  23. #define _REFLECTIONMANAGER_H_
  24. #ifndef _GFXDEVICE_H_
  25. #include "gfx/gfxDevice.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _UTIL_DELEGATE_H_
  31. #include "core/util/delegate.h"
  32. #endif
  33. #ifndef _GFXTEXTUREHANDLE_H_
  34. #include "gfx/gfxTextureHandle.h"
  35. #endif
  36. #ifndef _TSINGLETON_H_
  37. #include "core/util/tSingleton.h"
  38. #endif
  39. #ifndef _REFLECTOR_H_
  40. #include "scene/reflector.h"
  41. #endif
  42. class PlatformTimer;
  43. class BaseMatInstance;
  44. enum ReflectMode
  45. {
  46. ReflectNever = 0,
  47. ReflectDynamic,
  48. ReflectAlways
  49. };
  50. typedef Delegate<bool(bool)> ReflectDelegate;
  51. class SceneObject;
  52. struct Reflector
  53. {
  54. SceneObject *object;
  55. ReflectDelegate updateFn;
  56. F32 priority;
  57. U32 maxRateMs;
  58. F32 maxDist;
  59. U32 lastUpdateMs;
  60. F32 score;
  61. bool updated;
  62. bool tried;
  63. bool hasTexture;
  64. };
  65. typedef Vector<Reflector> ReflectorVec;
  66. GFX_DeclareTextureProfile( ReflectRenderTargetProfile );
  67. GFX_DeclareTextureProfile( RefractTextureProfile );
  68. class ReflectionManager
  69. {
  70. public:
  71. ReflectionManager();
  72. virtual ~ReflectionManager();
  73. static void initConsole();
  74. /// Called to change the reflection texture format.
  75. void setReflectFormat( GFXFormat format ) { mReflectFormat = format; }
  76. /// Returns the current reflection format.
  77. GFXFormat getReflectFormat() const { return mReflectFormat; }
  78. /// Doll out callbacks to registered objects based on
  79. /// scoring and elapsed time. This should be called
  80. /// once for each viewport that renders.
  81. void update( F32 timeSlice,
  82. const Point2I &resolution,
  83. const CameraQuery &query );
  84. void registerReflector( ReflectorBase *reflector );
  85. void unregisterReflector( ReflectorBase *reflector );
  86. GFXTexHandle allocRenderTarget( const Point2I &size );
  87. GFXTextureObject* getRefractTex( bool forceUpdate = false );
  88. BaseMatInstance* getReflectionMaterial( BaseMatInstance *inMat ) const;
  89. const U32& getLastUpdateMs() const { return mLastUpdateMs; }
  90. protected:
  91. bool _handleDeviceEvent( GFXDevice::GFXDeviceEventType evt );
  92. protected:
  93. /// ReflectionManager tries not to spend more than this amount of time
  94. /// updating reflections per frame.
  95. static U32 smFrameReflectionMS;
  96. /// RefractTex has dimensions equal to the active render target scaled in
  97. /// both x and y by this float.
  98. static F32 smRefractTexScale;
  99. /// A timer used for tracking update time.
  100. PlatformTimer *mTimer;
  101. /// All registered reflections which we handle updating.
  102. ReflectorList mReflectors;
  103. /// Refraction texture copied from the backbuffer once per frame that
  104. /// gets used by all WaterObjects.
  105. GFXTexHandle mRefractTex;
  106. /// The texture format to use for reflection and
  107. /// refraction texture sources.
  108. GFXFormat mReflectFormat;
  109. /// Set when the refraction texture is dirty
  110. /// and requires an update.
  111. bool mUpdateRefract;
  112. /// Platform time in milliseconds of the last update.
  113. U32 mLastUpdateMs;
  114. public:
  115. // For ManagedSingleton.
  116. static const char* getSingletonName() { return "ReflectionManager"; }
  117. };
  118. /// Returns the ReflectionManager singleton.
  119. #define REFLECTMGR ManagedSingleton<ReflectionManager>::instance()
  120. #endif // _REFLECTIONMANAGER_H_