BsRendererCamera.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. float nearPlane;
  65. bool isOverlay : 1;
  66. bool isHDR : 1;
  67. bool triggerCallbacks : 1;
  68. bool runPostProcessing : 1;
  69. UINT64 visibleLayers;
  70. ConvexVolume cullFrustum;
  71. StateReduction stateReduction;
  72. const Camera* sceneCamera;
  73. SPtr<Texture> skyboxTexture;
  74. };
  75. /** Contains information about a Camera, used by the Renderer. */
  76. class RendererCamera
  77. {
  78. public:
  79. RendererCamera();
  80. RendererCamera(const RENDERER_VIEW_DESC& desc);
  81. /** Sets state reduction mode that determines how do render queues group & sort renderables. */
  82. void setStateReductionMode(StateReduction reductionMode);
  83. /** Updates the internal camera post-processing data. */
  84. void setPostProcessSettings(const SPtr<PostProcessSettings>& ppSettings);
  85. /** Updates the internal information with a new view transform. */
  86. void setTransform(const Vector3& origin, const Vector3& direction, const Matrix4& view,
  87. const Matrix4& proj);
  88. /** Updates all internal information with new view information. */
  89. void setView(const RENDERER_VIEW_DESC& desc);
  90. /** Returns the world position of the view. */
  91. Vector3 getViewOrigin() const { return mViewDesc.viewOrigin; }
  92. /** Returns a matrix that contains combined projection and view transforms. */
  93. Matrix4 getViewProjMatrix() const { return mViewDesc.projTransform * mViewDesc.viewTransform; }
  94. /** Returns the distance to the near clipping plane. */
  95. float getNearPlane() const { return mViewDesc.nearPlane; }
  96. /** Returns true if the view requires high dynamic range rendering. */
  97. bool isHDR() const { return mViewDesc.isHDR; }
  98. /** Returns the texture to use for the skybox (if any). */
  99. SPtr<Texture> getSkybox() const { return mViewDesc.skyboxTexture; }
  100. /** Returns the final render target the rendered contents should be output to. */
  101. SPtr<RenderTarget> getFinalTarget() const { return mViewDesc.target.target; }
  102. /** Returns normalized coordinates of the viewport area this view renders to. */
  103. Rect2 getViewportRect() const { return mViewDesc.target.nrmViewRect; }
  104. /** Returns the scene camera this object is based of. This can be null for manually constructed renderer cameras. */
  105. const Camera* getSceneCamera() const { return mViewDesc.sceneCamera; }
  106. /** Returns true if external render callbacks should trigger for this view. */
  107. bool checkTriggerCallbacks() const { return mViewDesc.triggerCallbacks; }
  108. /** Returns true if post-processing effects should be triggered for this view. */
  109. bool checkRunPostProcessing() const { return mViewDesc.runPostProcessing; }
  110. /**
  111. * Prepares render targets for rendering. When done call endRendering().
  112. *
  113. * @param[in] useRenderTargets If using the internal render targets containing the GBuffer (retrieved via
  114. * getRenderTargets()) while rendering you must set this to true.
  115. */
  116. void beginRendering(bool useRenderTargets);
  117. /** Ends rendering and frees any acquired resources. */
  118. void endRendering();
  119. /** Returns the view's renderTargets. Only valid if called in-between beginRendering() and endRendering() calls. */
  120. SPtr<RenderTargets> getRenderTargets() const { return mRenderTargets; }
  121. /**
  122. * Returns a render queue containing all opaque objects. Make sure to call determineVisible() beforehand if view
  123. * or object transforms changed since the last time it was called.
  124. */
  125. const SPtr<RenderQueue>& getOpaqueQueue() const { return mOpaqueQueue; }
  126. /**
  127. * Returns a render queue containing all transparent objects. Make sure to call determineVisible() beforehand if
  128. * view or object transforms changed since the last time it was called.
  129. */
  130. const SPtr<RenderQueue>& getTransparentQueue() const { return mTransparentQueue; }
  131. /**
  132. * Populates view render queues by determining visible renderable objects.
  133. *
  134. * @param[in] renderables A set of renderable objects to iterate over and determine visibility for.
  135. * @param[in] renderableBounds A set of world bounds for the provided renderable objects. Must be the same size
  136. * as the @p renderables array.
  137. * @param[out] visibility Output parameter that will have the true bit set for any visible renderable
  138. * object. If the bit for an object is already set to true, the method will never
  139. * change it to false which allows the same bitfield to be provided to multiple
  140. * renderer views. Must be the same size as the @p renderables array.
  141. *
  142. * As a side-effect, per-view visibility data is also calculated and can be
  143. * retrieved by calling getVisibilityMask().
  144. */
  145. void determineVisible(const Vector<RendererObject*>& renderables, const Vector<Bounds>& renderableBounds,
  146. Vector<bool>* visibility = nullptr);
  147. /** Returns the visibility mask calculated with the last call to determineVisible(). */
  148. const Vector<bool>& getVisibilityMask() const { return mVisibility; }
  149. /**
  150. * Returns a structure containing information about post-processing effects. This structure will be modified and
  151. * maintained by the post-processing system.
  152. */
  153. PostProcessInfo& getPPInfo() { return mPostProcessInfo; }
  154. /** Updates the GPU buffer containing per-view information, with the latest internal data. */
  155. void updatePerViewBuffer();
  156. /** Returns a buffer that stores per-view parameters. */
  157. SPtr<GpuParamBlockBuffer> getPerViewBuffer() const { return mParamBuffer; }
  158. private:
  159. /**
  160. * Extracts the necessary values from the projection matrix that allow you to transform device Z value into
  161. * world Z value.
  162. *
  163. * @param[in] projMatrix Projection matrix that was used to create the device Z value to transform.
  164. * @return Returns two values that can be used to transform device z to world z using this formula:
  165. * z = (deviceZ + y) * x.
  166. */
  167. Vector2 getDeviceZTransform(const Matrix4& projMatrix) const;
  168. RENDERER_VIEW_DESC mViewDesc;
  169. SPtr<RenderQueue> mOpaqueQueue;
  170. SPtr<RenderQueue> mTransparentQueue;
  171. SPtr<RenderTargets> mRenderTargets;
  172. PostProcessInfo mPostProcessInfo;
  173. bool mUsingRenderTargets;
  174. SPtr<GpuParamBlockBuffer> mParamBuffer;
  175. Vector<bool> mVisibility;
  176. };
  177. /** @} */
  178. }}