BsRenderTarget.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsPixelUtil.h"
  6. #include "BsViewport.h"
  7. #include "BsCoreObject.h"
  8. #include "BsEvent.h"
  9. namespace bs
  10. {
  11. /** @addtogroup RenderAPI
  12. * @{
  13. */
  14. /** Structure that contains information about what part of the texture represents the render surface. */
  15. struct BS_CORE_EXPORT RENDER_SURFACE_DESC
  16. {
  17. RENDER_SURFACE_DESC() { }
  18. HTexture texture;
  19. /** First face of the texture to bind (array index in texture arrays, or Z slice in 3D textures). */
  20. UINT32 face = 0;
  21. /**
  22. * Number of faces to bind (entries in a texture array, or Z slices in 3D textures). When zero the entire resource
  23. * will be bound.
  24. */
  25. UINT32 numFaces = 0;
  26. /** If the texture has multiple mips, which one to bind (only one can be bound for rendering). */
  27. UINT32 mipLevel = 0;
  28. };
  29. namespace ct
  30. {
  31. /**
  32. * @see bs::RENDER_SURFACE_DESC
  33. *
  34. * @note References core textures instead of texture handles.
  35. */
  36. struct BS_CORE_EXPORT RENDER_SURFACE_DESC
  37. {
  38. RENDER_SURFACE_DESC() { }
  39. SPtr<Texture> texture;
  40. /** First face of the texture to bind (array index in texture arrays, or Z slice in 3D textures). */
  41. UINT32 face = 0;
  42. /**
  43. * Number of faces to bind (entries in a texture array, or Z slices in 3D textures). When zero the entire resource
  44. * will be bound.
  45. */
  46. UINT32 numFaces = 0;
  47. /** If the texture has multiple mips, which one to bind (only one can be bound for rendering). */
  48. UINT32 mipLevel = 0;
  49. };
  50. }
  51. /** Contains various properties that describe a render target. */
  52. class BS_CORE_EXPORT RenderTargetProperties
  53. {
  54. public:
  55. RenderTargetProperties() { }
  56. virtual ~RenderTargetProperties() { }
  57. /** Returns width of the render target, in pixels. */
  58. UINT32 getWidth() const { return mWidth; }
  59. /** Returns height of the render target, in pixels. */
  60. UINT32 getHeight() const { return mHeight; }
  61. /**
  62. * Returns the number of three dimensional slices of the render target. This will be number of layers for array
  63. * textures or number of faces cube textures.
  64. */
  65. UINT32 getNumSlices() const { return mNumSlices; }
  66. /** Gets the number of samples used for multisampling. (0 or 1 if multisampling is not used). */
  67. UINT32 getMultisampleCount() const { return mMultisampleCount; }
  68. /**
  69. * Returns true if the render target will wait for vertical sync before swapping buffers. This will eliminate
  70. * tearing but may increase input latency.
  71. */
  72. bool getVSync() const { return mVSync; }
  73. /**
  74. * Returns how often should the frame be presented in respect to display device refresh rate. Normal value is 1
  75. * where it will match the refresh rate. Higher values will decrease the frame rate (for example present interval of
  76. * 2 on 60Hz refresh rate will display at most 30 frames per second).
  77. */
  78. UINT32 getVSyncInterval() const { return mVSyncInterval; }
  79. /** Returns true if pixels written to the render target will be gamma corrected. */
  80. bool isHwGammaEnabled() const { return mHwGamma; }
  81. /**
  82. * Returns true if the render target can be used for rendering.
  83. *
  84. * @note Core thread only.
  85. */
  86. bool isActive() const { return mActive; }
  87. /**
  88. * Controls in what order is the render target rendered to compared to other render targets. Targets with higher
  89. * priority will be rendered before ones with lower priority.
  90. */
  91. INT32 getPriority() const { return mPriority; }
  92. /** Returns true if the render target is a render window. */
  93. bool isWindow() const { return mIsWindow; }
  94. /**
  95. * Does the texture need to be vertically flipped because of different screen space coordinate systems. (Determines
  96. * is origin top left or bottom left. Engine default is top left.)
  97. */
  98. bool requiresTextureFlipping() const { return mRequiresTextureFlipping; }
  99. protected:
  100. friend class ct::RenderTarget;
  101. friend class RenderTarget;
  102. UINT32 mWidth = 0;
  103. UINT32 mHeight = 0;
  104. UINT32 mNumSlices = 0;
  105. UINT32 mColorDepth = 32;
  106. INT32 mPriority = 0;
  107. UINT32 mVSyncInterval = 1;
  108. bool mActive = true;
  109. bool mHwGamma = false;
  110. bool mVSync = false;
  111. bool mRequiresTextureFlipping = false;
  112. bool mIsWindow = false;
  113. UINT32 mMultisampleCount = 0;
  114. };
  115. /**
  116. * Render target is a frame buffer or a texture that the render system renders the scene to.
  117. *
  118. * @note
  119. * Sim thread unless noted otherwise. Retrieve core implementation from getCore() for core thread only functionality.
  120. */
  121. class BS_CORE_EXPORT RenderTarget : public CoreObject
  122. {
  123. public:
  124. RenderTarget();
  125. virtual ~RenderTarget() { }
  126. /** Queries the render target for a custom attribute. This may be anything and is implementation specific. */
  127. virtual void getCustomAttribute(const String& name, void* pData) const;
  128. /**
  129. * @copydoc ct::RenderTarget::setPriority
  130. *
  131. * @note This is an @ref asyncMethod "asynchronous method".
  132. */
  133. void setPriority(INT32 priority);
  134. /**
  135. * Returns properties that describe the render target.
  136. *
  137. * @note Sim thread only.
  138. */
  139. const RenderTargetProperties& getProperties() const;
  140. /** Retrieves a core implementation of a render target usable only from the core thread. */
  141. SPtr<ct::RenderTarget> getCore() const;
  142. /**
  143. * Event that gets triggered whenever the render target is resized.
  144. *
  145. * @note Sim thread only.
  146. */
  147. mutable Event<void()> onResized;
  148. protected:
  149. friend class ct::RenderTarget;
  150. /** Returns properties that describe the render target. */
  151. virtual const RenderTargetProperties& getPropertiesInternal() const = 0;
  152. };
  153. /** @} */
  154. namespace ct
  155. {
  156. /** @addtogroup RenderAPI-Internal
  157. * @{
  158. */
  159. /**
  160. * Provides access to internal render target implementation usable only from the core thread.
  161. *
  162. * @note Core thread only.
  163. */
  164. class BS_CORE_EXPORT RenderTarget : public CoreObject
  165. {
  166. public:
  167. /** Frame buffer type when double-buffering is used. */
  168. enum FrameBuffer
  169. {
  170. FB_FRONT,
  171. FB_BACK,
  172. FB_AUTO
  173. };
  174. RenderTarget();
  175. virtual ~RenderTarget() { }
  176. /**
  177. * Sets a priority that determines in which orders the render targets the processed.
  178. *
  179. * @param[in] priority The priority. Higher value means the target will be rendered sooner.
  180. */
  181. void setPriority(INT32 priority);
  182. /**
  183. * Swaps the frame buffers to display the next frame.
  184. *
  185. * @param[in] syncMask Optional synchronization mask that determines for which queues should the system wait
  186. * before performing the swap buffer operation. By default the system waits for all queues.
  187. * However if certain queues are performing non-rendering operations, or operations not
  188. * related to this render target, you can exclude them from the sync mask for potentially
  189. * better performance. You can use CommandSyncMask to generate a valid sync mask.
  190. */
  191. virtual void swapBuffers(UINT32 syncMask = 0xFFFFFFFF) {}
  192. /** Queries the render target for a custom attribute. This may be anything and is implementation specific. */
  193. virtual void getCustomAttribute(const String& name, void* pData) const;
  194. /** Returns properties that describe the render target. */
  195. const RenderTargetProperties& getProperties() const;
  196. protected:
  197. friend class bs::RenderTarget;
  198. /** Returns properties that describe the render target. */
  199. virtual const RenderTargetProperties& getPropertiesInternal() const = 0;
  200. };
  201. /** @} */
  202. }
  203. }