BsRenderTarget.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsPixelUtil.h"
  4. #include "BsViewport.h"
  5. #include "BsCoreObject.h"
  6. #include "BsEvent.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Structure that contains information about
  11. * what part of the texture represents the render surface.
  12. */
  13. struct BS_CORE_EXPORT RENDER_SURFACE_DESC
  14. {
  15. TexturePtr texture;
  16. UINT32 face;
  17. UINT32 mipLevel;
  18. };
  19. /**
  20. * @brief Contains various properties that describe a render target.
  21. */
  22. class BS_CORE_EXPORT RenderTargetProperties
  23. {
  24. public:
  25. virtual ~RenderTargetProperties() { }
  26. /**
  27. * @brief Returns a name of the render target, used for easier identification.
  28. */
  29. const String& getName() const { return mName; }
  30. /**
  31. * @brief Returns width of the render target, in pixels.
  32. *
  33. * @note Sim thread only.
  34. */
  35. UINT32 getWidth() const { return mWidth; }
  36. /**
  37. * @brief Returns height of the render target, in pixels.
  38. *
  39. * @note Sim thread only.
  40. */
  41. UINT32 getHeight() const { return mHeight; }
  42. /**
  43. * @brief Gets the number of samples used for multisampling.
  44. * (0 if multisampling is not used).
  45. */
  46. UINT32 getMultisampleCount() const { return mMultisampleCount; }
  47. /**
  48. * @brief Returns true if the render target will wait for vertical sync
  49. * before swapping buffers. This will eliminate tearing but may increase
  50. * input latency.
  51. */
  52. bool getVSync() const { return mVSync; }
  53. /**
  54. * @brief Returns how often should the frame be presented in respect to
  55. * display device refresh rate. Normal value is 1 where it will
  56. * match the refresh rate. Higher values will decrease the frame
  57. * rate (e.g. present interval of 2 on 60Hz refresh rate will display
  58. * at most 30 frames per second).
  59. */
  60. UINT32 getVSyncInterval() const { return mVSyncInterval; }
  61. /**
  62. * @brief Returns true if pixels written to the render target will be gamma corrected.
  63. */
  64. bool isHwGammaEnabled() const { return mHwGamma; }
  65. /**
  66. * @brief Returns true if the render target can be used for rendering.
  67. *
  68. * @note Core thread only.
  69. */
  70. bool isActive() const { return mActive; }
  71. /**
  72. * @brief Returns render target priority. Targets with higher priority will be
  73. * rendered before ones with lower priority.
  74. */
  75. INT32 getPriority() const { return mPriority; }
  76. /**
  77. * @brief Returns true if the render target is a render window.
  78. */
  79. bool isWindow() const { return mIsWindow; }
  80. /**
  81. * @brief Does the texture need to be vertically flipped because of different screen space coordinate systems.
  82. * (i.e. is origin top left or bottom left. Engine default is top left.)
  83. */
  84. bool requiresTextureFlipping() const { return mRequiresTextureFlipping; }
  85. /**
  86. * @brief Copies all data from the provided object to this object.
  87. */
  88. virtual void copyFrom(const RenderTargetProperties& other);
  89. protected:
  90. friend class RenderTargetCore;
  91. friend class RenderTarget;
  92. String mName;
  93. UINT32 mWidth = 0;
  94. UINT32 mHeight = 0;
  95. UINT32 mColorDepth = 32;
  96. INT32 mPriority = 0;
  97. UINT32 mVSyncInterval = 1;
  98. bool mActive = true;
  99. bool mHwGamma = false;
  100. bool mVSync = false;
  101. bool mRequiresTextureFlipping = false;
  102. bool mIsWindow = false;
  103. UINT32 mMultisampleCount = 0;
  104. };
  105. /**
  106. * @brief Provides access to internal render target implementation usable only from the core thread.
  107. *
  108. * @note Core thread only.
  109. */
  110. class BS_CORE_EXPORT RenderTargetCore
  111. {
  112. public:
  113. /**
  114. * @brief Frame buffer type when double-buffering is used.
  115. */
  116. enum FrameBuffer
  117. {
  118. FB_FRONT,
  119. FB_BACK,
  120. FB_AUTO
  121. };
  122. RenderTargetCore(RenderTarget* parent, RenderTargetProperties* properties);
  123. virtual ~RenderTargetCore();
  124. /**
  125. * @brief Makes the render target active or inactive. (e.g. for a window, it will hide or restore the window).
  126. *
  127. * @note Core thread only.
  128. */
  129. virtual void setActive(bool state) { mProperties->mActive = state; markCoreDirty(); }
  130. /**
  131. * @brief Sets a priority that determines in which orders the render targets the processed.
  132. *
  133. * @param priority The priority. Higher value means the target will be rendered sooner.
  134. */
  135. void setPriority(INT32 priority) { mProperties->mPriority = priority; markCoreDirty(); }
  136. /**
  137. * @brief Swaps the frame buffers to display the next frame.
  138. *
  139. * @note Core thread only.
  140. */
  141. virtual void swapBuffers() {};
  142. /**
  143. * @brief Queries the render target for a custom attribute. This may be anything and is
  144. * implementation specific.
  145. *
  146. * @note Core thread only.
  147. */
  148. virtual void getCustomAttribute(const String& name, void* pData) const;
  149. /**
  150. * @brief Returns properties that describe the render target.
  151. */
  152. const RenderTargetProperties& getProperties() const;
  153. /**
  154. * @brief Returns the non core version of the render target.
  155. */
  156. RenderTarget* getNonCore() const { return mParent; }
  157. /**
  158. * @brief Returns true if this object was modified and the sim thread version requires an update.
  159. */
  160. bool _isCoreDirty() const { return mCoreDirty; }
  161. /**
  162. * @brief Marks the object as clean. Usually called after the sim thread version was updated.
  163. */
  164. void _markCoreClean() { mCoreDirty = false; }
  165. protected:
  166. friend class RenderTarget;
  167. /**
  168. * @brief Marks this object as modified. Signals the system that the sim thread verison
  169. * of the object needs an update.
  170. */
  171. void markCoreDirty() { mCoreDirty = true; }
  172. RenderTargetProperties* mProperties;
  173. RenderTarget* mParent;
  174. private:
  175. bool mCoreDirty = true;
  176. };
  177. /**
  178. * @brief Render target is a frame buffer or a texture that the render
  179. * system renders to.
  180. *
  181. * @note Sim thread unless noted otherwise. Retrieve core implementation from getCore()
  182. * for core thread only functionality.
  183. */
  184. class BS_CORE_EXPORT RenderTarget : public CoreObject
  185. {
  186. public:
  187. virtual ~RenderTarget();
  188. /**
  189. * @brief Does the texture need to be vertically flipped because of different screen space coordinate systems.
  190. * (i.e. is origin top left or bottom left. Engine default is top left.)
  191. */
  192. virtual bool requiresTextureFlipping() const = 0;
  193. /**
  194. * @brief Queries the render target for a custom attribute. This may be anything and is
  195. * implementation specific.
  196. */
  197. virtual void getCustomAttribute(const String& name, void* pData) const;
  198. /**
  199. * @brief Returns properties that describe the render target.
  200. *
  201. * @note Sim thread only.
  202. */
  203. const RenderTargetProperties& getProperties() const;
  204. /**
  205. * @brief Retrieves a core implementation of a render target usable only from the
  206. * core thread.
  207. */
  208. RenderTargetCore* getCore() const;
  209. /**
  210. * @brief Event that gets triggered whenever the render target is resized.
  211. *
  212. * @note Sim thread only.
  213. */
  214. mutable Event<void()> onResized;
  215. protected:
  216. friend class RenderTargetManager;
  217. RenderTarget();
  218. /**
  219. * @brief Creates a new instance of render target properties used for storing
  220. * render target data and providing easy access to it.
  221. */
  222. virtual RenderTargetProperties* createProperties() const = 0;
  223. /**
  224. * @brief Creates a core implementation of a render target. This implementation
  225. * is to be used on the core thread only.
  226. */
  227. virtual RenderTargetCore* createCore() = 0;
  228. /**
  229. * @copydoc CoreObject::initialize_internal
  230. */
  231. virtual void initialize_internal();
  232. /**
  233. * @copydoc CoreObject::destroy_internal
  234. */
  235. virtual void destroy_internal();
  236. RenderTargetCore* mCore;
  237. RenderTargetProperties* mProperties;
  238. };
  239. }