BsRenderTarget.h 8.0 KB

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