BsRendererView.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsPostProcessing.h"
  6. #include "BsObjectRendering.h"
  7. #include "BsRenderQueue.h"
  8. #include "BsRendererObject.h"
  9. #include "BsBounds.h"
  10. #include "BsConvexVolume.h"
  11. namespace bs { namespace ct
  12. {
  13. /** @addtogroup RenderBeast
  14. * @{
  15. */
  16. BS_PARAM_BLOCK_BEGIN(PerCameraParamDef)
  17. BS_PARAM_BLOCK_ENTRY(Vector3, gViewDir)
  18. BS_PARAM_BLOCK_ENTRY(Vector3, gViewOrigin)
  19. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  20. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatView)
  21. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatProj)
  22. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvProj)
  23. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvViewProj)
  24. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatScreenToWorld)
  25. BS_PARAM_BLOCK_ENTRY(Vector2, gDeviceZToWorldZ)
  26. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToWorldZ)
  27. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToDeviceZ)
  28. BS_PARAM_BLOCK_ENTRY(Vector2, gNearFar)
  29. BS_PARAM_BLOCK_ENTRY(Vector4I, gViewportRectangle)
  30. BS_PARAM_BLOCK_ENTRY(Vector4, gClipToUVScaleOffset)
  31. BS_PARAM_BLOCK_ENTRY(float, gAmbientFactor)
  32. BS_PARAM_BLOCK_END
  33. extern PerCameraParamDef gPerCameraParamDef;
  34. BS_PARAM_BLOCK_BEGIN(SkyboxParamDef)
  35. BS_PARAM_BLOCK_ENTRY(Color, gClearColor)
  36. BS_PARAM_BLOCK_END
  37. extern SkyboxParamDef gSkyboxParamDef;
  38. /** Shader that renders a skybox using a cubemap or a solid color. */
  39. template<bool SOLID_COLOR>
  40. class SkyboxMat : public RendererMaterial<SkyboxMat<SOLID_COLOR>>
  41. {
  42. RMAT_DEF("Skybox.bsl");
  43. public:
  44. SkyboxMat();
  45. /** Binds the material for rendering and sets up any global parameters. */
  46. void bind(const SPtr<GpuParamBlockBuffer>& perCamera);
  47. /** Updates the skybox texture & solid color used by the material. */
  48. void setParams(const SPtr<Texture>& texture, const Color& solidColor);
  49. private:
  50. GpuParamTexture mSkyTextureParam;
  51. SPtr<GpuParamBlockBuffer> mParamBuffer;
  52. };
  53. /** Data shared between RENDERER_VIEW_DESC and RendererViewProperties */
  54. struct RendererViewData
  55. {
  56. Matrix4 viewTransform;
  57. Matrix4 projTransform;
  58. Vector3 viewDirection;
  59. Vector3 viewOrigin;
  60. bool flipView;
  61. float nearPlane;
  62. float farPlane;
  63. ProjectionType projType;
  64. bool isOverlay : 1;
  65. bool isHDR : 1;
  66. bool noLighting : 1;
  67. bool triggerCallbacks : 1;
  68. bool runPostProcessing : 1;
  69. bool renderingReflections : 1;
  70. UINT64 visibleLayers;
  71. ConvexVolume cullFrustum;
  72. };
  73. /** Data shared between RENDERER_VIEW_TARGET_DESC and RendererViewTargetProperties */
  74. struct RendererViewTargetData
  75. {
  76. SPtr<RenderTarget> target;
  77. Rect2I viewRect;
  78. Rect2 nrmViewRect;
  79. UINT32 targetWidth;
  80. UINT32 targetHeight;
  81. UINT32 numSamples;
  82. UINT32 clearFlags;
  83. Color clearColor;
  84. float clearDepthValue;
  85. UINT16 clearStencilValue;
  86. };
  87. /** Set of properties describing the output render target used by a renderer view. */
  88. struct RENDERER_VIEW_TARGET_DESC : RendererViewTargetData
  89. { };
  90. /** Set of properties used describing a specific view that the renderer can render. */
  91. struct RENDERER_VIEW_DESC : RendererViewData
  92. {
  93. RENDERER_VIEW_TARGET_DESC target;
  94. StateReduction stateReduction;
  95. Camera* sceneCamera;
  96. };
  97. /** Set of properties used describing a specific view that the renderer can render. */
  98. struct RendererViewProperties : RendererViewData
  99. {
  100. RendererViewProperties() {}
  101. RendererViewProperties(const RENDERER_VIEW_DESC& src);
  102. Matrix4 viewProjTransform;
  103. SPtr<RenderTarget> target;
  104. Rect2I viewRect;
  105. Rect2 nrmViewRect;
  106. UINT32 numSamples;
  107. UINT32 clearFlags;
  108. Color clearColor;
  109. float clearDepthValue;
  110. UINT16 clearStencilValue;
  111. };
  112. /** Information whether certain scene objects are visible in a view, per object type. */
  113. struct VisibilityInfo
  114. {
  115. Vector<bool> renderables;
  116. };
  117. /** Information used for culling an object against a view. */
  118. struct CullInfo
  119. {
  120. CullInfo(const Bounds& bounds, UINT64 layer = -1)
  121. :bounds(bounds), layer(layer)
  122. { }
  123. Bounds bounds;
  124. UINT64 layer;
  125. };
  126. /** Renderer information specific to a single render target. */
  127. struct RendererRenderTarget
  128. {
  129. SPtr<RenderTarget> target;
  130. Vector<Camera*> cameras;
  131. };
  132. /** Contains information about a single view into the scene, used by the renderer. */
  133. class RendererView
  134. {
  135. public:
  136. RendererView();
  137. RendererView(const RENDERER_VIEW_DESC& desc);
  138. /** Sets state reduction mode that determines how do render queues group & sort renderables. */
  139. void setStateReductionMode(StateReduction reductionMode);
  140. /** Updates the internal camera post-processing data. */
  141. void setPostProcessSettings(const SPtr<PostProcessSettings>& ppSettings);
  142. /** Updates the internal information with a new view transform. */
  143. void setTransform(const Vector3& origin, const Vector3& direction, const Matrix4& view,
  144. const Matrix4& proj, const ConvexVolume& worldFrustum);
  145. /** Updates all internal information with new view information. */
  146. void setView(const RENDERER_VIEW_DESC& desc);
  147. /** Returns a structure describing the view. */
  148. const RendererViewProperties& getProperties() const { return mProperties; }
  149. /** Returns the scene camera this object is based of. This can be null for manually constructed renderer cameras. */
  150. Camera* getSceneCamera() const { return mCamera; }
  151. /**
  152. * Prepares render targets for rendering. When done call endRendering().
  153. *
  154. * @param[in] useGBuffer Set to true if you will be rendering to internal render targets containing the
  155. * GBuffer (retrieved via getRenderTargets()).
  156. */
  157. void beginRendering(bool useGBuffer);
  158. /** Ends rendering and frees any acquired resources. */
  159. void endRendering();
  160. /** Returns the view's renderTargets. Only valid if called in-between beginRendering() and endRendering() calls. */
  161. SPtr<RenderTargets> getRenderTargets() const { return mRenderTargets; }
  162. /**
  163. * Returns a render queue containing all opaque objects. Make sure to call determineVisible() beforehand if view
  164. * or object transforms changed since the last time it was called.
  165. */
  166. const SPtr<RenderQueue>& getOpaqueQueue() const { return mOpaqueQueue; }
  167. /**
  168. * Returns a render queue containing all transparent objects. Make sure to call determineVisible() beforehand if
  169. * view or object transforms changed since the last time it was called.
  170. */
  171. const SPtr<RenderQueue>& getTransparentQueue() const { return mTransparentQueue; }
  172. /**
  173. * Populates view render queues by determining visible renderable objects.
  174. *
  175. * @param[in] renderables A set of renderable objects to iterate over and determine visibility for.
  176. * @param[in] cullInfos A set of world bounds & other information relevant for culling the provided
  177. * renderable objects. Must be the same size as the @p renderables array.
  178. * @param[out] visibility Output parameter that will have the true bit set for any visible renderable
  179. * object. If the bit for an object is already set to true, the method will never
  180. * change it to false which allows the same bitfield to be provided to multiple
  181. * renderer views. Must be the same size as the @p renderables array.
  182. *
  183. * As a side-effect, per-view visibility data is also calculated and can be
  184. * retrieved by calling getVisibilityMask().
  185. */
  186. void determineVisible(const Vector<RendererObject*>& renderables, const Vector<CullInfo>& cullInfos,
  187. Vector<bool>* visibility = nullptr);
  188. /**
  189. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  190. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  191. */
  192. void calculateVisibility(const Vector<CullInfo>& cullInfos, Vector<bool>& visibility) const;
  193. /**
  194. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  195. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  196. */
  197. void calculateVisibility(const Vector<Sphere>& bounds, Vector<bool>& visibility) const;
  198. /** Returns the visibility mask calculated with the last call to determineVisible(). */
  199. const VisibilityInfo& getVisibilityMasks() const { return mVisibility; }
  200. /**
  201. * Returns a structure containing information about post-processing effects. This structure will be modified and
  202. * maintained by the post-processing system.
  203. */
  204. PostProcessInfo& getPPInfo() { return mPostProcessInfo; }
  205. /** Updates the GPU buffer containing per-view information, with the latest internal data. */
  206. void updatePerViewBuffer();
  207. /** Returns a buffer that stores per-view parameters. */
  208. SPtr<GpuParamBlockBuffer> getPerViewBuffer() const { return mParamBuffer; }
  209. private:
  210. /**
  211. * Extracts the necessary values from the projection matrix that allow you to transform device Z value (range [0, 1]
  212. * into view Z value.
  213. *
  214. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  215. * @return Returns two values that can be used to transform device z to view z using this formula:
  216. * z = (deviceZ + y) * x.
  217. */
  218. Vector2 getDeviceZTransform(const Matrix4& projMatrix) const;
  219. /**
  220. * Extracts the necessary values from the projection matrix that allow you to transform NDC Z value (range depending
  221. * on render API) into view Z value.
  222. *
  223. * @param[in] projMatrix Projection matrix that was used to create the NDC Z value to transform.
  224. * @return Returns two values that can be used to transform NDC z to view z using this formula:
  225. * z = (NDCZ + y) * x.
  226. */
  227. Vector2 getNDCZTransform(const Matrix4& projMatrix) const;
  228. RendererViewProperties mProperties;
  229. RENDERER_VIEW_TARGET_DESC mTargetDesc;
  230. Camera* mCamera;
  231. SPtr<RenderQueue> mOpaqueQueue;
  232. SPtr<RenderQueue> mTransparentQueue;
  233. SPtr<RenderTargets> mRenderTargets;
  234. PostProcessInfo mPostProcessInfo;
  235. bool mUsingGBuffer;
  236. SPtr<GpuParamBlockBuffer> mParamBuffer;
  237. VisibilityInfo mVisibility;
  238. };
  239. /** @} */
  240. }}