BsRendererView.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include "BsLight.h"
  12. namespace bs { namespace ct
  13. {
  14. struct SceneInfo;
  15. class RendererLight;
  16. /** @addtogroup RenderBeast
  17. * @{
  18. */
  19. BS_PARAM_BLOCK_BEGIN(PerCameraParamDef)
  20. BS_PARAM_BLOCK_ENTRY(Vector3, gViewDir)
  21. BS_PARAM_BLOCK_ENTRY(Vector3, gViewOrigin)
  22. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  23. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatView)
  24. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatProj)
  25. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvProj)
  26. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvViewProj)
  27. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatScreenToWorld)
  28. BS_PARAM_BLOCK_ENTRY(Matrix4, gNDCToPrevNDC)
  29. BS_PARAM_BLOCK_ENTRY(Vector2, gDeviceZToWorldZ)
  30. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToWorldZ)
  31. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToDeviceZ)
  32. BS_PARAM_BLOCK_ENTRY(Vector2, gNearFar)
  33. BS_PARAM_BLOCK_ENTRY(Vector4I, gViewportRectangle)
  34. BS_PARAM_BLOCK_ENTRY(Vector4, gClipToUVScaleOffset)
  35. BS_PARAM_BLOCK_ENTRY(float, gAmbientFactor)
  36. BS_PARAM_BLOCK_END
  37. extern PerCameraParamDef gPerCameraParamDef;
  38. BS_PARAM_BLOCK_BEGIN(SkyboxParamDef)
  39. BS_PARAM_BLOCK_ENTRY(Color, gClearColor)
  40. BS_PARAM_BLOCK_END
  41. extern SkyboxParamDef gSkyboxParamDef;
  42. /** Shader that renders a skybox using a cubemap or a solid color. */
  43. template<bool SOLID_COLOR>
  44. class SkyboxMat : public RendererMaterial<SkyboxMat<SOLID_COLOR>>
  45. {
  46. RMAT_DEF("Skybox.bsl");
  47. public:
  48. SkyboxMat();
  49. /** Binds the material for rendering and sets up any global parameters. */
  50. void bind(const SPtr<GpuParamBlockBuffer>& perCamera);
  51. /** Updates the skybox texture & solid color used by the material. */
  52. void setParams(const SPtr<Texture>& texture, const Color& solidColor);
  53. private:
  54. GpuParamTexture mSkyTextureParam;
  55. SPtr<GpuParamBlockBuffer> mParamBuffer;
  56. };
  57. /** Data shared between RENDERER_VIEW_DESC and RendererViewProperties */
  58. struct RendererViewData
  59. {
  60. Matrix4 viewTransform;
  61. Matrix4 projTransform;
  62. Vector3 viewDirection;
  63. Vector3 viewOrigin;
  64. bool flipView;
  65. float nearPlane;
  66. float farPlane;
  67. ProjectionType projType;
  68. bool isOverlay : 1;
  69. bool isHDR : 1;
  70. bool noLighting : 1;
  71. bool noShadows : 1;
  72. bool triggerCallbacks : 1;
  73. bool runPostProcessing : 1;
  74. bool renderingReflections : 1;
  75. UINT64 visibleLayers;
  76. ConvexVolume cullFrustum;
  77. };
  78. /** Data shared between RENDERER_VIEW_TARGET_DESC and RendererViewTargetProperties */
  79. struct RendererViewTargetData
  80. {
  81. SPtr<RenderTarget> target;
  82. Rect2I viewRect;
  83. Rect2 nrmViewRect;
  84. UINT32 targetWidth;
  85. UINT32 targetHeight;
  86. UINT32 numSamples;
  87. UINT32 clearFlags;
  88. Color clearColor;
  89. float clearDepthValue;
  90. UINT16 clearStencilValue;
  91. };
  92. /** Set of properties describing the output render target used by a renderer view. */
  93. struct RENDERER_VIEW_TARGET_DESC : RendererViewTargetData
  94. { };
  95. /** Set of properties used describing a specific view that the renderer can render. */
  96. struct RENDERER_VIEW_DESC : RendererViewData
  97. {
  98. RENDERER_VIEW_TARGET_DESC target;
  99. StateReduction stateReduction;
  100. Camera* sceneCamera;
  101. };
  102. /** Set of properties used describing a specific view that the renderer can render. */
  103. struct RendererViewProperties : RendererViewData
  104. {
  105. RendererViewProperties() {}
  106. RendererViewProperties(const RENDERER_VIEW_DESC& src);
  107. Matrix4 viewProjTransform;
  108. Matrix4 prevViewProjTransform;
  109. SPtr<RenderTarget> target;
  110. Rect2I viewRect;
  111. Rect2 nrmViewRect;
  112. UINT32 numSamples;
  113. UINT32 clearFlags;
  114. Color clearColor;
  115. float clearDepthValue;
  116. UINT16 clearStencilValue;
  117. };
  118. /** Information whether certain scene objects are visible in a view, per object type. */
  119. struct VisibilityInfo
  120. {
  121. Vector<bool> renderables;
  122. Vector<bool> radialLights;
  123. Vector<bool> spotLights;
  124. Vector<bool> reflProbes;
  125. };
  126. /** Information used for culling an object against a view. */
  127. struct CullInfo
  128. {
  129. CullInfo(const Bounds& bounds, UINT64 layer = -1)
  130. :bounds(bounds), layer(layer)
  131. { }
  132. Bounds bounds;
  133. UINT64 layer;
  134. };
  135. /** Renderer information specific to a single render target. */
  136. struct RendererRenderTarget
  137. {
  138. SPtr<RenderTarget> target;
  139. Vector<Camera*> cameras;
  140. };
  141. /** Contains information about a single view into the scene, used by the renderer. */
  142. class RendererView
  143. {
  144. public:
  145. RendererView();
  146. RendererView(const RENDERER_VIEW_DESC& desc);
  147. /** Sets state reduction mode that determines how do render queues group & sort renderables. */
  148. void setStateReductionMode(StateReduction reductionMode);
  149. /** Updates the internal camera post-processing data. */
  150. void setPostProcessSettings(const SPtr<PostProcessSettings>& ppSettings);
  151. /** Updates the internal information with a new view transform. */
  152. void setTransform(const Vector3& origin, const Vector3& direction, const Matrix4& view,
  153. const Matrix4& proj, const ConvexVolume& worldFrustum);
  154. /** Updates all internal information with new view information. */
  155. void setView(const RENDERER_VIEW_DESC& desc);
  156. /** Returns a structure describing the view. */
  157. const RendererViewProperties& getProperties() const { return mProperties; }
  158. /** Returns the scene camera this object is based of. This can be null for manually constructed renderer cameras. */
  159. Camera* getSceneCamera() const { return mCamera; }
  160. /**
  161. * Prepares render targets for rendering. When done call endFrame().
  162. *
  163. * @param[in] useGBuffer Set to true if you will be rendering to internal render targets containing the
  164. * GBuffer (retrieved via getRenderTargets()).
  165. */
  166. void beginFrame(bool useGBuffer);
  167. /** Ends rendering and frees any acquired resources. */
  168. void endFrame();
  169. /** Returns the view's renderTargets. Only valid if called in-between beginRendering() and endRendering() calls. */
  170. SPtr<RenderTargets> getRenderTargets() const { return mRenderTargets; }
  171. /**
  172. * Returns a render queue containing all opaque objects. Make sure to call determineVisible() beforehand if view
  173. * or object transforms changed since the last time it was called.
  174. */
  175. const SPtr<RenderQueue>& getOpaqueQueue() const { return mOpaqueQueue; }
  176. /**
  177. * Returns a render queue containing all transparent objects. Make sure to call determineVisible() beforehand if
  178. * view or object transforms changed since the last time it was called.
  179. */
  180. const SPtr<RenderQueue>& getTransparentQueue() const { return mTransparentQueue; }
  181. /**
  182. * Populates view render queues by determining visible renderable objects.
  183. *
  184. * @param[in] renderables A set of renderable objects to iterate over and determine visibility for.
  185. * @param[in] cullInfos A set of world bounds & other information relevant for culling the provided
  186. * renderable objects. Must be the same size as the @p renderables array.
  187. * @param[out] visibility Output parameter that will have the true bit set for any visible renderable
  188. * object. If the bit for an object is already set to true, the method will never
  189. * change it to false which allows the same bitfield to be provided to multiple
  190. * renderer views. Must be the same size as the @p renderables array.
  191. *
  192. * As a side-effect, per-view visibility data is also calculated and can be
  193. * retrieved by calling getVisibilityMask().
  194. */
  195. void determineVisible(const Vector<RendererObject*>& renderables, const Vector<CullInfo>& cullInfos,
  196. Vector<bool>* visibility = nullptr);
  197. /**
  198. * Calculates the visibility masks for all the lights of the provided type.
  199. *
  200. * @param[in] lights A set of lights to determine visibility for.
  201. * @param[in] bounds Bounding sphere for each provided light. Must be the same size as the @p lights
  202. * array.
  203. * @param[in] type Type of all the lights in the @p lights array.
  204. * @param[out] visibility Output parameter that will have the true bit set for any visible light. If the
  205. * bit for a light is already set to true, the method will never change it to false
  206. * which allows the same bitfield to be provided to multiple renderer views. Must
  207. * be the same size as the @p lights array.
  208. *
  209. * As a side-effect, per-view visibility data is also calculated and can be
  210. * retrieved by calling getVisibilityMask().
  211. */
  212. void determineVisible(const Vector<RendererLight>& lights, const Vector<Sphere>& bounds, LightType type,
  213. Vector<bool>* visibility = nullptr);
  214. /**
  215. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  216. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  217. */
  218. void calculateVisibility(const Vector<CullInfo>& cullInfos, Vector<bool>& visibility) const;
  219. /**
  220. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  221. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  222. */
  223. void calculateVisibility(const Vector<Sphere>& bounds, Vector<bool>& visibility) const;
  224. /**
  225. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  226. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  227. */
  228. void calculateVisibility(const Vector<AABox>& bounds, Vector<bool>& visibility) const;
  229. /** Returns the visibility mask calculated with the last call to determineVisible(). */
  230. const VisibilityInfo& getVisibilityMasks() const { return mVisibility; }
  231. /**
  232. * Returns a structure containing information about post-processing effects. This structure will be modified and
  233. * maintained by the post-processing system.
  234. */
  235. PostProcessInfo& getPPInfo() { return mPostProcessInfo; }
  236. /** @copydoc getPPInfo() */
  237. const PostProcessInfo& getPPInfo() const { return mPostProcessInfo; }
  238. /** Updates the GPU buffer containing per-view information, with the latest internal data. */
  239. void updatePerViewBuffer();
  240. /** Returns a buffer that stores per-view parameters. */
  241. SPtr<GpuParamBlockBuffer> getPerViewBuffer() const { return mParamBuffer; }
  242. /**
  243. * Extracts the necessary values from the projection matrix that allow you to transform device Z value (range [0, 1]
  244. * into view Z value.
  245. *
  246. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  247. * @return Returns two values that can be used to transform device z to view z using this formula:
  248. * z = (deviceZ + y) * x.
  249. */
  250. static Vector2 getDeviceZToViewZ(const Matrix4& projMatrix);
  251. /**
  252. * Extracts the necessary values from the projection matrix that allow you to transform NDC Z value (range depending
  253. * on render API) into view Z value.
  254. *
  255. * @param[in] projMatrix Projection matrix that was used to create the NDC Z value to transform.
  256. * @return Returns two values that can be used to transform NDC z to view z using this formula:
  257. * z = (NDCZ + y) * x.
  258. */
  259. static Vector2 getNDCZToViewZ(const Matrix4& projMatrix);
  260. /**
  261. * Returns a value that can be used for tranforming a depth value in NDC, to a depth value in device Z ([0, 1]
  262. * range using this formula: (NDCZ + y) * x.
  263. */
  264. static Vector2 getNDCZToDeviceZ();
  265. private:
  266. RendererViewProperties mProperties;
  267. RENDERER_VIEW_TARGET_DESC mTargetDesc;
  268. Camera* mCamera;
  269. SPtr<RenderQueue> mOpaqueQueue;
  270. SPtr<RenderQueue> mTransparentQueue;
  271. SPtr<RenderTargets> mRenderTargets;
  272. PostProcessInfo mPostProcessInfo;
  273. bool mUsingGBuffer;
  274. SPtr<GpuParamBlockBuffer> mParamBuffer;
  275. VisibilityInfo mVisibility;
  276. };
  277. /** Contains one or multiple RendererView%s that are in some way related. */
  278. class RendererViewGroup
  279. {
  280. public:
  281. RendererViewGroup() {}
  282. RendererViewGroup(RendererView** views, UINT32 numViews);
  283. /**
  284. * Updates the internal list of views. This is more efficient than always constructing a new instance of this class
  285. * when views change, as internal buffers don't need to be re-allocated.
  286. */
  287. void setViews(RendererView** views, UINT32 numViews);
  288. /** Returns a view at the specified index. Index must be less than the value returned by getNumViews(). */
  289. RendererView* getView(UINT32 idx) const { return mViews[idx]; }
  290. /** Returns the total number of views in the group. */
  291. UINT32 getNumViews() const { return (UINT32)mViews.size(); }
  292. /**
  293. * Returns information about visibility of various scene objects, from the perspective of all the views in the
  294. * group (visibility will be true if the object is visible from any of the views. determineVisibility() must be
  295. * called whenever the scene or view information changes (usually every frame).
  296. */
  297. const VisibilityInfo& getVisibilityInfo() const { return mVisibility; }
  298. /**
  299. * Updates visibility information for the provided scene objects, from the perspective of all views in this group,
  300. * and updates the render queues of each individual view. Use getVisibilityInfo() to retrieve the calculated
  301. * visibility information.
  302. */
  303. void determineVisibility(const SceneInfo& sceneInfo);
  304. private:
  305. Vector<RendererView*> mViews;
  306. VisibilityInfo mVisibility;
  307. };
  308. /** @} */
  309. }}