BsRendererCamera.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRendererCamera.h"
  4. #include "BsCamera.h"
  5. #include "BsRenderable.h"
  6. #include "BsMaterial.h"
  7. #include "BsShader.h"
  8. #include "BsRenderTargets.h"
  9. namespace BansheeEngine
  10. {
  11. RendererCamera::RendererCamera()
  12. :mCamera(nullptr), mUsingRenderTargets(false)
  13. { }
  14. RendererCamera::RendererCamera(const CameraCore* camera, StateReduction reductionMode)
  15. :mCamera(camera), mUsingRenderTargets(false)
  16. {
  17. update(reductionMode);
  18. }
  19. void RendererCamera::update(StateReduction reductionMode)
  20. {
  21. mOpaqueQueue = bs_shared_ptr_new<RenderQueue>(reductionMode);
  22. StateReduction transparentStateReduction = reductionMode;
  23. if (transparentStateReduction == StateReduction::Material)
  24. transparentStateReduction = StateReduction::Distance; // Transparent object MUST be sorted by distance
  25. mTransparentQueue = bs_shared_ptr_new<RenderQueue>(transparentStateReduction);
  26. updatePP();
  27. }
  28. void RendererCamera::updatePP()
  29. {
  30. if (mPostProcessInfo.settings == nullptr)
  31. mPostProcessInfo.settings = bs_shared_ptr_new<StandardPostProcessSettings>();
  32. SPtr<StandardPostProcessSettings> ppSettings = std::static_pointer_cast<StandardPostProcessSettings>(mCamera->getPostProcessSettings());
  33. if (ppSettings != nullptr)
  34. *mPostProcessInfo.settings = *ppSettings;
  35. else
  36. *mPostProcessInfo.settings = StandardPostProcessSettings();
  37. mPostProcessInfo.settingDirty = true;
  38. }
  39. void RendererCamera::beginRendering(bool useGBuffer)
  40. {
  41. if (useGBuffer)
  42. {
  43. SPtr<ViewportCore> viewport = mCamera->getViewport();
  44. bool useHDR = mCamera->getFlags().isSet(CameraFlag::HDR);
  45. UINT32 msaaCount = mCamera->getMSAACount();
  46. // Render scene objects to g-buffer
  47. bool createGBuffer = mRenderTargets == nullptr ||
  48. mRenderTargets->getHDR() != useHDR ||
  49. mRenderTargets->getNumSamples() != msaaCount;
  50. if (createGBuffer)
  51. mRenderTargets = RenderTargets::create(viewport, useHDR, msaaCount);
  52. mRenderTargets->allocate();
  53. mUsingRenderTargets = true;
  54. }
  55. }
  56. void RendererCamera::endRendering()
  57. {
  58. mOpaqueQueue->clear();
  59. mTransparentQueue->clear();
  60. if(mUsingRenderTargets)
  61. {
  62. mRenderTargets->release();
  63. mUsingRenderTargets = false;
  64. }
  65. }
  66. void RendererCamera::determineVisible(Vector<RendererObject>& renderables, const Vector<Bounds>& renderableBounds,
  67. Vector<bool>& visibility)
  68. {
  69. bool isOverlayCamera = mCamera->getFlags().isSet(CameraFlag::Overlay);
  70. if (isOverlayCamera)
  71. return;
  72. UINT64 cameraLayers = mCamera->getLayers();
  73. ConvexVolume worldFrustum = mCamera->getWorldFrustum();
  74. // Update per-object param buffers and queue render elements
  75. for(UINT32 i = 0; i < (UINT32)renderables.size(); i++)
  76. {
  77. RenderableCore* renderable = renderables[i].renderable;
  78. UINT32 rendererId = renderable->getRendererId();
  79. if ((renderable->getLayer() & cameraLayers) == 0)
  80. continue;
  81. // Do frustum culling
  82. // Note: This is bound to be a bottleneck at some point. When it is ensure that intersect methods use vector
  83. // operations, as it is trivial to update them. Also consider spatial partitioning.
  84. const Sphere& boundingSphere = renderableBounds[rendererId].getSphere();
  85. if (worldFrustum.intersects(boundingSphere))
  86. {
  87. // More precise with the box
  88. const AABox& boundingBox = renderableBounds[rendererId].getBox();
  89. if (worldFrustum.intersects(boundingBox))
  90. {
  91. visibility[i] = true;
  92. float distanceToCamera = (mCamera->getPosition() - boundingBox.getCenter()).length();
  93. for (auto& renderElem : renderables[i].elements)
  94. {
  95. bool isTransparent = (renderElem.material->getShader()->getFlags() & (UINT32)ShaderFlags::Transparent) != 0;
  96. if (isTransparent)
  97. mTransparentQueue->add(&renderElem, distanceToCamera);
  98. else
  99. mOpaqueQueue->add(&renderElem, distanceToCamera);
  100. }
  101. }
  102. }
  103. }
  104. mOpaqueQueue->sort();
  105. mTransparentQueue->sort();
  106. }
  107. Vector2 RendererCamera::getDeviceZTransform(const Matrix4& projMatrix) const
  108. {
  109. // Returns a set of values that will transform depth buffer values (e.g. [0, 1] in DX, [-1, 1] in GL) to a distance
  110. // in world space. This involes applying the inverse projection transform to the depth value. When you multiply
  111. // a vector with the projection matrix you get [clipX, clipY, Az + B, C * z], where we don't care about clipX/clipY.
  112. // A is [2, 2], B is [2, 3] and C is [3, 2] elements of the projection matrix (only ones that matter for our depth
  113. // value). The hardware will also automatically divide the z value with w to get the depth, therefore the final
  114. // formula is:
  115. // depth = (Az + B) / (C * z)
  116. // To get the z coordinate back we simply do the opposite:
  117. // z = B / (depth * C - A)
  118. // However some APIs will also do a transformation on the depth values before storing them to the texture
  119. // (e.g. OpenGL will transform from [-1, 1] to [0, 1]). And we need to reverse that as well. Therefore the final
  120. // formula is:
  121. // z = B / ((depth * (maxDepth - minDepth) + minDepth) * C - A)
  122. // Are we reorganize it because it needs to fit the "(1.0f / (depth + y)) * x" format used in the shader:
  123. // z = 1.0f / (depth + minDepth/(maxDepth - minDepth) - A/((maxDepth - minDepth) * C)) * B/((maxDepth - minDepth) * C)
  124. RenderAPICore& rapi = RenderAPICore::instance();
  125. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  126. float depthRange = rapiInfo.getMaximumDepthInputValue() - rapiInfo.getMinimumDepthInputValue();
  127. float minDepth = rapiInfo.getMinimumDepthInputValue();
  128. float a = projMatrix[2][2];
  129. float b = projMatrix[2][3];
  130. float c = projMatrix[3][2];
  131. Vector2 output;
  132. output.x = b / (depthRange * c);
  133. output.y = minDepth / depthRange - a / (depthRange * c);
  134. return output;
  135. }
  136. CameraShaderData RendererCamera::getShaderData()
  137. {
  138. CameraShaderData data;
  139. data.proj = mCamera->getProjectionMatrixRS();
  140. data.view = mCamera->getViewMatrix();
  141. data.viewProj = data.proj * data.view;
  142. data.invProj = data.proj.inverse();
  143. data.invViewProj = data.viewProj.inverse(); // Note: Calculate inverses separately (better precision possibly)
  144. // Construct a special inverse view-projection matrix that had projection entries that affect z and w eliminated.
  145. // Used to transform a vector(clip_x, clip_y, view_z, view_w), where clip_x/clip_y are in clip space, and
  146. // view_z/view_w in view space, into world space.
  147. // Only projects z/w coordinates
  148. Matrix4 projZ = Matrix4::IDENTITY;
  149. projZ[2][2] = data.proj[2][2];
  150. projZ[2][3] = data.proj[2][3];
  151. projZ[3][2] = data.proj[3][2];
  152. projZ[3][3] = 0.0f;
  153. data.screenToWorld = data.invViewProj * projZ;
  154. data.viewDir = mCamera->getForward();
  155. data.viewOrigin = mCamera->getPosition();
  156. data.deviceZToWorldZ = getDeviceZTransform(data.proj);
  157. SPtr<ViewportCore> viewport = mCamera->getViewport();
  158. SPtr<RenderTargetCore> rt = viewport->getTarget();
  159. float halfWidth = viewport->getWidth() * 0.5f;
  160. float halfHeight = viewport->getHeight() * 0.5f;
  161. float rtWidth = (float)rt->getProperties().getWidth();
  162. float rtHeight = (float)rt->getProperties().getHeight();
  163. RenderAPICore& rapi = RenderAPICore::instance();
  164. const RenderAPIInfo& rapiInfo = rapi.getAPIInfo();
  165. data.clipToUVScaleOffset.x = halfWidth / rtWidth;
  166. data.clipToUVScaleOffset.y = -halfHeight / rtHeight;
  167. data.clipToUVScaleOffset.z = viewport->getX() / rtWidth + (halfWidth + rapiInfo.getHorizontalTexelOffset()) / rtWidth;
  168. data.clipToUVScaleOffset.w = viewport->getY() / rtHeight + (halfHeight + rapiInfo.getVerticalTexelOffset()) / rtHeight;
  169. if (!rapiInfo.getNDCYAxisDown())
  170. data.clipToUVScaleOffset.y = -data.clipToUVScaleOffset.y;
  171. return data;
  172. }
  173. }