BsRendererView.h 11 KB

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