BsRendererCamera.h 9.2 KB

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