BsRendererCamera.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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, gNearFar)
  28. BS_PARAM_BLOCK_ENTRY(Vector4I, gViewportRectangle)
  29. BS_PARAM_BLOCK_ENTRY(Vector4, gClipToUVScaleOffset)
  30. BS_PARAM_BLOCK_END
  31. extern PerCameraParamDef gPerCameraParamDef;
  32. /** Shader that renders a skybox using a cubemap. */
  33. class SkyboxMat : public RendererMaterial<SkyboxMat>
  34. {
  35. RMAT_DEF("Skybox.bsl");
  36. public:
  37. SkyboxMat();
  38. /** Binds the material for rendering and sets up any global parameters. */
  39. void bind(const SPtr<GpuParamBlockBuffer>& perCamera);
  40. /** Updates the skybox texture used by the material. */
  41. void setParams(const SPtr<Texture>& texture);
  42. private:
  43. GpuParamTexture mSkyTextureParam;
  44. };
  45. /** Set of properties describing the output render target used by a renderer view. */
  46. struct RENDERER_VIEW_TARGET_DESC
  47. {
  48. SPtr<RenderTarget> target;
  49. Rect2I viewRect;
  50. Rect2 nrmViewRect;
  51. UINT32 targetWidth;
  52. UINT32 targetHeight;
  53. UINT32 numSamples;
  54. UINT32 clearFlags;
  55. Color clearColor;
  56. float clearDepthValue;
  57. UINT16 clearStencilValue;
  58. };
  59. /** Set of properties used describing a specific view that the renderer can render. */
  60. struct RENDERER_VIEW_DESC
  61. {
  62. RENDERER_VIEW_TARGET_DESC target;
  63. Matrix4 viewTransform;
  64. Matrix4 projTransform;
  65. Vector3 viewDirection;
  66. Vector3 viewOrigin;
  67. bool flipView;
  68. float nearPlane;
  69. float farPlane;
  70. bool isOverlay : 1;
  71. bool isHDR : 1;
  72. bool triggerCallbacks : 1;
  73. bool runPostProcessing : 1;
  74. UINT64 visibleLayers;
  75. ConvexVolume cullFrustum;
  76. StateReduction stateReduction;
  77. const Camera* sceneCamera;
  78. SPtr<Texture> skyboxTexture;
  79. };
  80. /** Information whether certain scene objects are visible in a view, per object type. */
  81. struct VisibilityInfo
  82. {
  83. Vector<bool> renderables;
  84. };
  85. /** Information used for culling an object against a view. */
  86. struct CullInfo
  87. {
  88. CullInfo(const Bounds& bounds, UINT64 layer = -1)
  89. :bounds(bounds), layer(layer)
  90. { }
  91. Bounds bounds;
  92. UINT64 layer;
  93. };
  94. /** Contains information about a Camera, used by the Renderer. */
  95. class RendererCamera
  96. {
  97. public:
  98. RendererCamera();
  99. RendererCamera(const RENDERER_VIEW_DESC& desc);
  100. /** Sets state reduction mode that determines how do render queues group & sort renderables. */
  101. void setStateReductionMode(StateReduction reductionMode);
  102. /** Updates the internal camera post-processing data. */
  103. void setPostProcessSettings(const SPtr<PostProcessSettings>& ppSettings);
  104. /** Updates the internal information with a new view transform. */
  105. void setTransform(const Vector3& origin, const Vector3& direction, const Matrix4& view,
  106. const Matrix4& proj, const ConvexVolume& worldFrustum);
  107. /** Updates all internal information with new view information. */
  108. void setView(const RENDERER_VIEW_DESC& desc);
  109. /** Returns the world position of the view. */
  110. Vector3 getViewOrigin() const { return mViewDesc.viewOrigin; }
  111. /** Returns a matrix that contains combined projection and view transforms. */
  112. Matrix4 getViewProjMatrix() const { return mViewDesc.projTransform * mViewDesc.viewTransform; }
  113. /** Returns the distance to the near clipping plane. */
  114. float getNearPlane() const { return mViewDesc.nearPlane; }
  115. /** Returns the distance to the far clipping plane. */
  116. float getFarPlane() const { return mViewDesc.farPlane; }
  117. /** Returns true if the view requires high dynamic range rendering. */
  118. bool isHDR() const { return mViewDesc.isHDR; }
  119. /** Returns true if this view only renders overlay, and not scene objects. */
  120. bool isOverlay() const { return mViewDesc.isOverlay; }
  121. /** Returns the texture to use for the skybox (if any). */
  122. SPtr<Texture> getSkybox() const { return mViewDesc.skyboxTexture; }
  123. /** Returns the final render target the rendered contents should be output to. */
  124. SPtr<RenderTarget> getFinalTarget() const { return mViewDesc.target.target; }
  125. /** Returns normalized coordinates of the viewport area this view renders to. */
  126. Rect2 getViewportRect() const { return mViewDesc.target.nrmViewRect; }
  127. /** Returns true if the resulting render target should be flipped vertically. */
  128. bool getFlipView() const { return mViewDesc.flipView; }
  129. /** Returns the number of samples per pixel to render. */
  130. UINT32 getNumSamples() const { return mViewDesc.target.numSamples; }
  131. /** Returns the scene camera this object is based of. This can be null for manually constructed renderer cameras. */
  132. const Camera* getSceneCamera() const { return mViewDesc.sceneCamera; }
  133. /** Returns true if external render callbacks should trigger for this view. */
  134. bool checkTriggerCallbacks() const { return mViewDesc.triggerCallbacks; }
  135. /** Returns true if post-processing effects should be triggered for this view. */
  136. bool checkRunPostProcessing() const { return mViewDesc.runPostProcessing; }
  137. /**
  138. * Prepares render targets for rendering. When done call endRendering().
  139. *
  140. * @param[in] useGBuffer Set to true if you will be rendering to internal render targets containing the
  141. * GBuffer (retrieved via getRenderTargets()).
  142. */
  143. void beginRendering(bool useGBuffer);
  144. /** Ends rendering and frees any acquired resources. */
  145. void endRendering();
  146. /** Returns the view's renderTargets. Only valid if called in-between beginRendering() and endRendering() calls. */
  147. SPtr<RenderTargets> getRenderTargets() const { return mRenderTargets; }
  148. /**
  149. * Returns a render queue containing all opaque objects. Make sure to call determineVisible() beforehand if view
  150. * or object transforms changed since the last time it was called.
  151. */
  152. const SPtr<RenderQueue>& getOpaqueQueue() const { return mOpaqueQueue; }
  153. /**
  154. * Returns a render queue containing all transparent objects. Make sure to call determineVisible() beforehand if
  155. * view or object transforms changed since the last time it was called.
  156. */
  157. const SPtr<RenderQueue>& getTransparentQueue() const { return mTransparentQueue; }
  158. /**
  159. * Populates view render queues by determining visible renderable objects.
  160. *
  161. * @param[in] renderables A set of renderable objects to iterate over and determine visibility for.
  162. * @param[in] cullInfos A set of world bounds & other information relevant for culling the provided
  163. * renderable objects. Must be the same size as the @p renderables array.
  164. * @param[out] visibility Output parameter that will have the true bit set for any visible renderable
  165. * object. If the bit for an object is already set to true, the method will never
  166. * change it to false which allows the same bitfield to be provided to multiple
  167. * renderer views. Must be the same size as the @p renderables array.
  168. *
  169. * As a side-effect, per-view visibility data is also calculated and can be
  170. * retrieved by calling getVisibilityMask().
  171. */
  172. void determineVisible(const Vector<RendererObject*>& renderables, const Vector<CullInfo>& cullInfos,
  173. Vector<bool>* visibility = nullptr);
  174. /**
  175. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  176. * which entry is or ins't visible by this view. Both inputs must be arrays of the same size.
  177. */
  178. void calculateVisibility(const Vector<CullInfo>& cullInfos, Vector<bool>& visibility) const;
  179. /**
  180. * Culls the provided set of bounds against the current frustum and outputs a set of visibility flags determining
  181. * which entry is or ins't visible by this view. Both inputs must be arrays of the same size.
  182. */
  183. void calculateVisibility(const Vector<Sphere>& bounds, Vector<bool>& visibility) const;
  184. /** Returns the visibility mask calculated with the last call to determineVisible(). */
  185. const VisibilityInfo& getVisibilityMasks() const { return mVisibility; }
  186. /**
  187. * Returns a structure containing information about post-processing effects. This structure will be modified and
  188. * maintained by the post-processing system.
  189. */
  190. PostProcessInfo& getPPInfo() { return mPostProcessInfo; }
  191. /** Updates the GPU buffer containing per-view information, with the latest internal data. */
  192. void updatePerViewBuffer();
  193. /** Returns a buffer that stores per-view parameters. */
  194. SPtr<GpuParamBlockBuffer> getPerViewBuffer() const { return mParamBuffer; }
  195. private:
  196. /**
  197. * Extracts the necessary values from the projection matrix that allow you to transform device Z value (range [0, 1]
  198. * into view Z value.
  199. *
  200. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  201. * @return Returns two values that can be used to transform device z to view z using this formula:
  202. * z = (deviceZ + y) * x.
  203. */
  204. Vector2 getDeviceZTransform(const Matrix4& projMatrix) const;
  205. /**
  206. * Extracts the necessary values from the projection matrix that allow you to transform NDC Z value (range depending
  207. * on render API) into view Z value.
  208. *
  209. * @param[in] projMatrix Projection matrix that was used to create the NDC Z value to transform.
  210. * @return Returns two values that can be used to transform NDC z to view z using this formula:
  211. * z = (NDCZ + y) * x.
  212. */
  213. Vector2 getNDCZTransform(const Matrix4& projMatrix) const;
  214. RENDERER_VIEW_DESC mViewDesc;
  215. SPtr<RenderQueue> mOpaqueQueue;
  216. SPtr<RenderQueue> mTransparentQueue;
  217. SPtr<RenderTargets> mRenderTargets;
  218. PostProcessInfo mPostProcessInfo;
  219. bool mUsingGBuffer;
  220. SPtr<GpuParamBlockBuffer> mParamBuffer;
  221. VisibilityInfo mVisibility;
  222. };
  223. /** @} */
  224. }}