BsRenderTarget.h 7.5 KB

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