BsRenderTarget.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /**
  91. * @brief Copies all data from the provided object to this object.
  92. */
  93. virtual void copyFrom(const RenderTargetProperties& other);
  94. protected:
  95. friend class RenderTargetCore;
  96. friend class RenderTarget;
  97. String mName;
  98. UINT32 mWidth = 0;
  99. UINT32 mHeight = 0;
  100. UINT32 mColorDepth = 32;
  101. INT32 mPriority = 0;
  102. UINT32 mVSyncInterval = 1;
  103. bool mActive = true;
  104. bool mHwGamma = false;
  105. bool mVSync = false;
  106. bool mRequiresTextureFlipping = false;
  107. bool mIsWindow = false;
  108. UINT32 mMultisampleCount = 0;
  109. String mMultisampleHint;
  110. };
  111. /**
  112. * @brief Provides access to internal render target implementation usable only from the core thread.
  113. *
  114. * @note Core thread only.
  115. */
  116. class BS_CORE_EXPORT RenderTargetCore
  117. {
  118. public:
  119. /**
  120. * @brief Frame buffer type when double-buffering is used.
  121. */
  122. enum FrameBuffer
  123. {
  124. FB_FRONT,
  125. FB_BACK,
  126. FB_AUTO
  127. };
  128. RenderTargetCore(RenderTarget* parent, RenderTargetProperties* properties);
  129. virtual ~RenderTargetCore();
  130. /**
  131. * @brief Makes the render target active or inactive. (e.g. for a window, it will hide or restore the window).
  132. *
  133. * @note Core thread only.
  134. */
  135. virtual void setActive(bool state) { mProperties->mActive = state; markCoreDirty(); }
  136. /**
  137. * @brief Sets a priority that determines in which orders the render targets the processed.
  138. *
  139. * @param priority The priority. Higher value means the target will be rendered sooner.
  140. */
  141. void setPriority(INT32 priority) { mProperties->mPriority = priority; markCoreDirty(); }
  142. /**
  143. * @brief Swaps the frame buffers to display the next frame.
  144. *
  145. * @note Core thread only.
  146. */
  147. virtual void swapBuffers() {};
  148. /**
  149. * @brief Queries the render target for a custom attribute. This may be anything and is
  150. * implementation specific.
  151. *
  152. * @note Core thread only.
  153. */
  154. virtual void getCustomAttribute(const String& name, void* pData) const;
  155. /**
  156. * @brief Returns properties that describe the render target.
  157. */
  158. const RenderTargetProperties& getProperties() const;
  159. /**
  160. * @brief Returns the non core version of the render target.
  161. */
  162. RenderTarget* getNonCore() const { return mParent; }
  163. /**
  164. * @brief Returns true if this object was modified and the sim thread version requires an update.
  165. */
  166. bool _isCoreDirty() const { return mCoreDirty; }
  167. /**
  168. * @brief Marks the object as clean. Usually called after the sim thread version was updated.
  169. */
  170. void _markCoreClean() { mCoreDirty = false; }
  171. protected:
  172. friend class RenderTarget;
  173. /**
  174. * @brief Marks this object as modified. Signals the system that the sim thread verison
  175. * of the object needs an update.
  176. */
  177. void markCoreDirty() { mCoreDirty = true; }
  178. RenderTargetProperties* mProperties;
  179. RenderTarget* mParent;
  180. private:
  181. bool mCoreDirty = true;
  182. };
  183. /**
  184. * @brief Render target is a frame buffer or a texture that the render
  185. * system renders to.
  186. *
  187. * @note Sim thread unless noted otherwise. Retrieve core implementation from getCore()
  188. * for core thread only functionality.
  189. */
  190. class BS_CORE_EXPORT RenderTarget : public CoreObject
  191. {
  192. public:
  193. virtual ~RenderTarget();
  194. /**
  195. * @brief Does the texture need to be vertically flipped because of different screen space coordinate systems.
  196. * (i.e. is origin top left or bottom left. Engine default is top left.)
  197. */
  198. virtual bool requiresTextureFlipping() const = 0;
  199. /**
  200. * @brief Queries the render target for a custom attribute. This may be anything and is
  201. * implementation specific.
  202. */
  203. virtual void getCustomAttribute(const String& name, void* pData) const;
  204. /**
  205. * @brief Returns properties that describe the render target.
  206. *
  207. * @note Sim thread only.
  208. */
  209. const RenderTargetProperties& getProperties() const;
  210. /**
  211. * @brief Retrieves a core implementation of a render target usable only from the
  212. * core thread.
  213. */
  214. RenderTargetCore* getCore() const;
  215. /**
  216. * @brief Event that gets triggered whenever the render target is resized.
  217. *
  218. * @note Sim thread only.
  219. */
  220. mutable Event<void()> onResized;
  221. protected:
  222. friend class RenderTargetManager;
  223. RenderTarget();
  224. /**
  225. * @brief Creates a new instance of render target properties used for storing
  226. * render target data and providing easy access to it.
  227. */
  228. virtual RenderTargetProperties* createProperties() const = 0;
  229. /**
  230. * @brief Creates a core implementation of a render target. This implementation
  231. * is to be used on the core thread only.
  232. */
  233. virtual RenderTargetCore* createCore() = 0;
  234. /**
  235. * @copydoc CoreObject::initialize_internal
  236. */
  237. virtual void initialize_internal();
  238. /**
  239. * @copydoc CoreObject::destroy_internal
  240. */
  241. virtual void destroy_internal();
  242. RenderTargetCore* mCore;
  243. RenderTargetProperties* mProperties;
  244. };
  245. }