BsRenderTarget.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 width of the render target, in pixels.
  28. *
  29. * @note Sim thread only.
  30. */
  31. UINT32 getWidth() const { return mWidth; }
  32. /**
  33. * @brief Returns height of the render target, in pixels.
  34. *
  35. * @note Sim thread only.
  36. */
  37. UINT32 getHeight() const { return mHeight; }
  38. /**
  39. * @brief Gets the number of samples used for multisampling.
  40. * (0 if multisampling is not used).
  41. */
  42. UINT32 getMultisampleCount() const { return mMultisampleCount; }
  43. /**
  44. * @brief Returns true if the render target will wait for vertical sync
  45. * before swapping buffers. This will eliminate tearing but may increase
  46. * input latency.
  47. */
  48. bool getVSync() const { return mVSync; }
  49. /**
  50. * @brief Returns how often should the frame be presented in respect to
  51. * display device refresh rate. Normal value is 1 where it will
  52. * match the refresh rate. Higher values will decrease the frame
  53. * rate (e.g. present interval of 2 on 60Hz refresh rate will display
  54. * at most 30 frames per second).
  55. */
  56. UINT32 getVSyncInterval() const { return mVSyncInterval; }
  57. /**
  58. * @brief Returns true if pixels written to the render target will be gamma corrected.
  59. */
  60. bool isHwGammaEnabled() const { return mHwGamma; }
  61. /**
  62. * @brief Returns true if the render target can be used for rendering.
  63. *
  64. * @note Core thread only.
  65. */
  66. bool isActive() const { return mActive; }
  67. /**
  68. * @brief Returns render target priority. Targets with higher priority will be
  69. * rendered before ones with lower priority.
  70. */
  71. INT32 getPriority() const { return mPriority; }
  72. /**
  73. * @brief Returns true if the render target is a render window.
  74. */
  75. bool isWindow() const { return mIsWindow; }
  76. /**
  77. * @brief Does the texture need to be vertically flipped because of different screen space coordinate systems.
  78. * (i.e. is origin top left or bottom left. Engine default is top left.)
  79. */
  80. bool requiresTextureFlipping() const { return mRequiresTextureFlipping; }
  81. protected:
  82. friend class RenderTargetCore;
  83. friend class RenderTarget;
  84. /**
  85. * @brief Copies all internal data to the specified buffer.
  86. */
  87. virtual void copyToBuffer(UINT8* buffer) const;
  88. /**
  89. * @brief Initializes all internal data from the specified buffer.
  90. */
  91. virtual void copyFromBuffer(UINT8* buffer);
  92. /**
  93. * @brief Returns the size of the buffer needed to hold all internal data.
  94. */
  95. virtual UINT32 getSize() const;
  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. };
  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 : public CoreObjectCore
  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. protected:
  161. friend class RenderTarget;
  162. /**
  163. * @copydoc CoreObjectCore::syncFromCore
  164. */
  165. virtual CoreSyncData syncFromCore(FrameAlloc* allocator);
  166. /**
  167. * @copydoc CoreObjectCore::syncToCore
  168. */
  169. virtual void syncToCore(const CoreSyncData& data);
  170. RenderTargetProperties* mProperties;
  171. RenderTarget* mParent;
  172. };
  173. /**
  174. * @brief Render target is a frame buffer or a texture that the render
  175. * system renders to.
  176. *
  177. * @note Sim thread unless noted otherwise. Retrieve core implementation from getCore()
  178. * for core thread only functionality.
  179. */
  180. class BS_CORE_EXPORT RenderTarget : public CoreObject
  181. {
  182. public:
  183. virtual ~RenderTarget();
  184. /**
  185. * @brief Does the texture need to be vertically flipped because of different screen space coordinate systems.
  186. * (i.e. is origin top left or bottom left. Engine default is top left.)
  187. */
  188. virtual bool requiresTextureFlipping() const = 0;
  189. /**
  190. * @brief Queries the render target for a custom attribute. This may be anything and is
  191. * implementation specific.
  192. */
  193. virtual void getCustomAttribute(const String& name, void* pData) const;
  194. /**
  195. * @brief Returns properties that describe the render target.
  196. *
  197. * @note Sim thread only.
  198. */
  199. const RenderTargetProperties& getProperties() const;
  200. /**
  201. * @brief Retrieves a core implementation of a render target usable only from the
  202. * core thread.
  203. */
  204. RenderTargetCore* getCore() const;
  205. /**
  206. * @brief Event that gets triggered whenever the render target is resized.
  207. *
  208. * @note Sim thread only.
  209. */
  210. mutable Event<void()> onResized;
  211. protected:
  212. RenderTarget();
  213. /**
  214. * @brief Creates a new instance of render target properties used for storing
  215. * render target data and providing easy access to it.
  216. */
  217. virtual RenderTargetProperties* createProperties() const = 0;
  218. /**
  219. * @copydoc CoreObject::syncToCore
  220. */
  221. virtual CoreSyncData syncToCore(FrameAlloc* allocator);
  222. /**
  223. * @copydoc CoreObject::syncFromCore
  224. */
  225. virtual void syncFromCore(const CoreSyncData& data);
  226. RenderTargetProperties* mProperties;
  227. };
  228. }