BsRendererView.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #include "BsLightGrid.h"
  13. #include "BsShadowRendering.h"
  14. #include "BsRenderCompositor.h"
  15. namespace bs { namespace ct
  16. {
  17. struct SceneInfo;
  18. class RendererLight;
  19. /** @addtogroup RenderBeast
  20. * @{
  21. */
  22. BS_PARAM_BLOCK_BEGIN(PerCameraParamDef)
  23. BS_PARAM_BLOCK_ENTRY(Vector3, gViewDir)
  24. BS_PARAM_BLOCK_ENTRY(Vector3, gViewOrigin)
  25. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  26. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatView)
  27. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatProj)
  28. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvProj)
  29. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatInvViewProj)
  30. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatScreenToWorld)
  31. BS_PARAM_BLOCK_ENTRY(Matrix4, gNDCToPrevNDC)
  32. BS_PARAM_BLOCK_ENTRY(Vector2, gDeviceZToWorldZ)
  33. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToWorldZ)
  34. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToDeviceZ)
  35. BS_PARAM_BLOCK_ENTRY(Vector2, gNearFar)
  36. BS_PARAM_BLOCK_ENTRY(Vector4I, gViewportRectangle)
  37. BS_PARAM_BLOCK_ENTRY(Vector4, gClipToUVScaleOffset)
  38. BS_PARAM_BLOCK_ENTRY(float, gAmbientFactor)
  39. BS_PARAM_BLOCK_END
  40. extern PerCameraParamDef gPerCameraParamDef;
  41. BS_PARAM_BLOCK_BEGIN(SkyboxParamDef)
  42. BS_PARAM_BLOCK_ENTRY(Color, gClearColor)
  43. BS_PARAM_BLOCK_END
  44. extern SkyboxParamDef gSkyboxParamDef;
  45. /** Shader that renders a skybox using a cubemap or a solid color. */
  46. class SkyboxMat : public RendererMaterial<SkyboxMat>
  47. {
  48. RMAT_DEF("Skybox.bsl");
  49. public:
  50. SkyboxMat();
  51. /** Binds the material for rendering and sets up any global parameters. */
  52. void bind(const SPtr<GpuParamBlockBuffer>& perCamera);
  53. /** Updates the skybox texture & solid color used by the material. */
  54. void setParams(const SPtr<Texture>& texture, const Color& solidColor);
  55. /**
  56. * Returns the material variation matching the provided parameters.
  57. *
  58. * @param[in] color When true the material will use a solid color to render a skybox. When false a user
  59. * provided texture will be used instead.
  60. */
  61. static SkyboxMat* getVariation(bool color);
  62. private:
  63. GpuParamTexture mSkyTextureParam;
  64. SPtr<GpuParamBlockBuffer> mParamBuffer;
  65. static ShaderVariation VAR_Texture;
  66. static ShaderVariation VAR_Color;
  67. };
  68. /** Data shared between RENDERER_VIEW_DESC and RendererViewProperties */
  69. struct RendererViewData
  70. {
  71. Matrix4 viewTransform;
  72. Matrix4 projTransform;
  73. Vector3 viewDirection;
  74. Vector3 viewOrigin;
  75. bool flipView;
  76. float nearPlane;
  77. float farPlane;
  78. ProjectionType projType;
  79. bool triggerCallbacks : 1;
  80. bool runPostProcessing : 1;
  81. bool renderingReflections : 1;
  82. UINT64 visibleLayers;
  83. ConvexVolume cullFrustum;
  84. };
  85. /** Data shared between RENDERER_VIEW_TARGET_DESC and RendererViewTargetProperties */
  86. struct RendererViewTargetData
  87. {
  88. SPtr<RenderTarget> target;
  89. Rect2I viewRect;
  90. Rect2 nrmViewRect;
  91. UINT32 targetWidth;
  92. UINT32 targetHeight;
  93. UINT32 numSamples;
  94. UINT32 clearFlags;
  95. Color clearColor;
  96. float clearDepthValue;
  97. UINT16 clearStencilValue;
  98. };
  99. /** Set of properties describing the output render target used by a renderer view. */
  100. struct RENDERER_VIEW_TARGET_DESC : RendererViewTargetData
  101. { };
  102. /** Set of properties used describing a specific view that the renderer can render. */
  103. struct RENDERER_VIEW_DESC : RendererViewData
  104. {
  105. RENDERER_VIEW_TARGET_DESC target;
  106. StateReduction stateReduction;
  107. Camera* sceneCamera;
  108. };
  109. /** Set of properties used describing a specific view that the renderer can render. */
  110. struct RendererViewProperties : RendererViewData
  111. {
  112. RendererViewProperties() {}
  113. RendererViewProperties(const RENDERER_VIEW_DESC& src);
  114. Matrix4 viewProjTransform;
  115. Matrix4 prevViewProjTransform;
  116. SPtr<RenderTarget> target;
  117. Rect2I viewRect;
  118. Rect2 nrmViewRect;
  119. UINT32 numSamples;
  120. UINT32 clearFlags;
  121. Color clearColor;
  122. float clearDepthValue;
  123. UINT16 clearStencilValue;
  124. };
  125. /** Information whether certain scene objects are visible in a view, per object type. */
  126. struct VisibilityInfo
  127. {
  128. Vector<bool> renderables;
  129. Vector<bool> radialLights;
  130. Vector<bool> spotLights;
  131. Vector<bool> reflProbes;
  132. };
  133. /** Information used for culling an object against a view. */
  134. struct CullInfo
  135. {
  136. CullInfo(const Bounds& bounds, UINT64 layer = -1)
  137. :bounds(bounds), layer(layer)
  138. { }
  139. Bounds bounds;
  140. UINT64 layer;
  141. };
  142. /** Renderer information specific to a single render target. */
  143. struct RendererRenderTarget
  144. {
  145. SPtr<RenderTarget> target;
  146. Vector<Camera*> cameras;
  147. };
  148. /** Contains information about a single view into the scene, used by the renderer. */
  149. class RendererView
  150. {
  151. public:
  152. RendererView();
  153. RendererView(const RENDERER_VIEW_DESC& desc);
  154. /** Sets state reduction mode that determines how do render queues group & sort renderables. */
  155. void setStateReductionMode(StateReduction reductionMode);
  156. /** Updates the internal camera render settings. */
  157. void setRenderSettings(const SPtr<RenderSettings>& settings);
  158. /** Updates the internal information with a new view transform. */
  159. void setTransform(const Vector3& origin, const Vector3& direction, const Matrix4& view,
  160. const Matrix4& proj, const ConvexVolume& worldFrustum);
  161. /** Updates all internal information with new view information. */
  162. void setView(const RENDERER_VIEW_DESC& desc);
  163. /** Returns a structure describing the view. */
  164. const RendererViewProperties& getProperties() const { return mProperties; }
  165. /** Returns the scene camera this object is based of. This can be null for manually constructed renderer cameras. */
  166. Camera* getSceneCamera() const { return mCamera; }
  167. /** Prepares render targets for rendering. When done call endFrame(). */
  168. void beginFrame();
  169. /** Ends rendering and frees any acquired resources. */
  170. void endFrame();
  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. /** Returns the compositor in charge of rendering for this view. */
  182. const RenderCompositor& getCompositor() const { return mCompositor; }
  183. /**
  184. * Populates view render queues by determining visible renderable objects.
  185. *
  186. * @param[in] renderables A set of renderable objects to iterate over and determine visibility for.
  187. * @param[in] cullInfos A set of world bounds & other information relevant for culling the provided
  188. * renderable objects. Must be the same size as the @p renderables array.
  189. * @param[out] visibility Output parameter that will have the true bit set for any visible renderable
  190. * object. If the bit for an object is already set to true, the method will never
  191. * change it to false which allows the same bitfield to be provided to multiple
  192. * renderer views. Must be the same size as the @p renderables array.
  193. *
  194. * As a side-effect, per-view visibility data is also calculated and can be
  195. * retrieved by calling getVisibilityMask().
  196. */
  197. void determineVisible(const Vector<RendererObject*>& renderables, const Vector<CullInfo>& cullInfos,
  198. Vector<bool>* visibility = nullptr);
  199. /**
  200. * Calculates the visibility masks for all the lights of the provided type.
  201. *
  202. * @param[in] lights A set of lights to determine visibility for.
  203. * @param[in] bounds Bounding sphere for each provided light. Must be the same size as the @p lights
  204. * array.
  205. * @param[in] type Type of all the lights in the @p lights array.
  206. * @param[out] visibility Output parameter that will have the true bit set for any visible light. If the
  207. * bit for a light is already set to true, the method will never change it to false
  208. * which allows the same bitfield to be provided to multiple renderer views. Must
  209. * be the same size as the @p lights array.
  210. *
  211. * As a side-effect, per-view visibility data is also calculated and can be
  212. * retrieved by calling getVisibilityMask().
  213. */
  214. void determineVisible(const Vector<RendererLight>& lights, const Vector<Sphere>& bounds, LightType type,
  215. Vector<bool>* visibility = nullptr);
  216. /**
  217. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  218. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  219. */
  220. void calculateVisibility(const Vector<CullInfo>& cullInfos, Vector<bool>& visibility) const;
  221. /**
  222. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  223. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  224. */
  225. void calculateVisibility(const Vector<Sphere>& bounds, Vector<bool>& visibility) const;
  226. /**
  227. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  228. * which entry is or isn't visible by this view. Both inputs must be arrays of the same size.
  229. */
  230. void calculateVisibility(const Vector<AABox>& bounds, Vector<bool>& visibility) const;
  231. /** Returns the visibility mask calculated with the last call to determineVisible(). */
  232. const VisibilityInfo& getVisibilityMasks() const { return mVisibility; }
  233. /** Returns per-view settings that control rendering. */
  234. const RenderSettings& getRenderSettings() const { return *mRenderSettings; }
  235. /**
  236. * Retrieves a hash value that is updated whenever render settings change. This can be used by external systems
  237. * to detect when they need to update.
  238. */
  239. UINT64 getRenderSettingsHash() const { return mRenderSettingsHash; }
  240. /** Updates the GPU buffer containing per-view information, with the latest internal data. */
  241. void updatePerViewBuffer();
  242. /** Returns a buffer that stores per-view parameters. */
  243. SPtr<GpuParamBlockBuffer> getPerViewBuffer() const { return mParamBuffer; }
  244. /**
  245. * Returns information about visible lights, in the form of a light grid, used for forward rendering. Only valid
  246. * after a call to updateLightGrid().
  247. */
  248. const LightGrid& getLightGrid() const { return mLightGrid; }
  249. /** Updates the light grid used for forward rendering. */
  250. void updateLightGrid(const VisibleLightData& visibleLightData, const VisibleReflProbeData& visibleReflProbeData);
  251. /**
  252. * Returns a value that can be used for transforming x, y coordinates from NDC into UV coordinates that can be used
  253. * for sampling a texture projected on the view.
  254. *
  255. * @return Returns two 2D values that can be used to transform the coordinate as such: UV = NDC * xy + zw.
  256. */
  257. Vector4 getNDCToUV() const;
  258. /**
  259. * Extracts the necessary values from the projection matrix that allow you to transform device Z value (range [0, 1]
  260. * into view Z value.
  261. *
  262. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  263. * @return Returns two values that can be used to transform device z to view z using this formula:
  264. * z = (deviceZ + y) * x.
  265. */
  266. static Vector2 getDeviceZToViewZ(const Matrix4& projMatrix);
  267. /**
  268. * Extracts the necessary values from the projection matrix that allow you to transform NDC Z value (range depending
  269. * on render API) into view Z value.
  270. *
  271. * @param[in] projMatrix Projection matrix that was used to create the NDC Z value to transform.
  272. * @return Returns two values that can be used to transform NDC z to view z using this formula:
  273. * z = (NDCZ + y) * x.
  274. */
  275. static Vector2 getNDCZToViewZ(const Matrix4& projMatrix);
  276. /**
  277. * Returns a value that can be used for tranforming a depth value in NDC, to a depth value in device Z ([0, 1]
  278. * range using this formula: (NDCZ + y) * x.
  279. */
  280. static Vector2 getNDCZToDeviceZ();
  281. private:
  282. RendererViewProperties mProperties;
  283. RENDERER_VIEW_TARGET_DESC mTargetDesc;
  284. Camera* mCamera;
  285. SPtr<RenderQueue> mOpaqueQueue;
  286. SPtr<RenderQueue> mTransparentQueue;
  287. RenderCompositor mCompositor;
  288. SPtr<RenderSettings> mRenderSettings;
  289. UINT32 mRenderSettingsHash;
  290. SPtr<GpuParamBlockBuffer> mParamBuffer;
  291. VisibilityInfo mVisibility;
  292. LightGrid mLightGrid;
  293. };
  294. /** Contains one or multiple RendererView%s that are in some way related. */
  295. class RendererViewGroup
  296. {
  297. public:
  298. RendererViewGroup();
  299. RendererViewGroup(RendererView** views, UINT32 numViews, UINT32 shadowMapSize);
  300. /**
  301. * Updates the internal list of views. This is more efficient than always constructing a new instance of this class
  302. * when views change, as internal buffers don't need to be re-allocated.
  303. */
  304. void setViews(RendererView** views, UINT32 numViews);
  305. /** Returns a view at the specified index. Index must be less than the value returned by getNumViews(). */
  306. RendererView* getView(UINT32 idx) const { return mViews[idx]; }
  307. /** Returns the total number of views in the group. */
  308. UINT32 getNumViews() const { return (UINT32)mViews.size(); }
  309. /**
  310. * Returns information about visibility of various scene objects, from the perspective of all the views in the
  311. * group (visibility will be true if the object is visible from any of the views. determineVisibility() must be
  312. * called whenever the scene or view information changes (usually every frame).
  313. */
  314. const VisibilityInfo& getVisibilityInfo() const { return mVisibility; }
  315. /**
  316. * Returns information about lights visible from this group of views. Only valid after a call to
  317. * determineVisibility().
  318. */
  319. const VisibleLightData& getVisibleLightData() const { return mVisibleLightData; }
  320. /**
  321. * Returns information about refl. probes visible from this group of views. Only valid after a call to
  322. * determineVisibility().
  323. */
  324. const VisibleReflProbeData& getVisibleReflProbeData() const { return mVisibleReflProbeData; }
  325. /** Returns the object responsible for rendering shadows for this view group. */
  326. ShadowRendering& getShadowRenderer() { return mShadowRenderer; }
  327. /** Returns the object responsible for rendering shadows for this view group. */
  328. const ShadowRendering& getShadowRenderer() const { return mShadowRenderer; }
  329. /**
  330. * Updates visibility information for the provided scene objects, from the perspective of all views in this group,
  331. * and updates the render queues of each individual view. Use getVisibilityInfo() to retrieve the calculated
  332. * visibility information.
  333. */
  334. void determineVisibility(const SceneInfo& sceneInfo);
  335. private:
  336. Vector<RendererView*> mViews;
  337. VisibilityInfo mVisibility;
  338. VisibleLightData mVisibleLightData;
  339. VisibleReflProbeData mVisibleReflProbeData;
  340. // Note: Ideally we would want to keep this global, so all views share it. This way each view group renders its
  341. // own set of shadows, but there might be shadows that are shared, and therefore we could avoid rendering them
  342. // multiple times. Since non-primary view groups are used for pre-processing tasks exclusively (at the moment)
  343. // this isn't an issue right now.
  344. ShadowRendering mShadowRenderer;
  345. };
  346. /** @} */
  347. }}