BsRenderTarget.h 7.5 KB

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