Renderer.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/renderer/Common.h>
  7. #include <anki/renderer/Drawer.h>
  8. #include <anki/renderer/Clusterer.h>
  9. #include <anki/Math.h>
  10. #include <anki/Gr.h>
  11. #include <anki/scene/Forward.h>
  12. #include <anki/resource/Forward.h>
  13. #include <anki/resource/ShaderResource.h>
  14. #include <anki/core/Timestamp.h>
  15. namespace anki {
  16. // Forward
  17. class ConfigSet;
  18. class ResourceManager;
  19. /// @addtogroup renderer
  20. /// @{
  21. /// Offscreen renderer. It is a class and not a namespace because we may need
  22. /// external renderers for security cameras for example
  23. class Renderer
  24. {
  25. public:
  26. Renderer();
  27. ~Renderer();
  28. const Ms& getMs() const
  29. {
  30. return *m_ms;
  31. }
  32. Ms& getMs()
  33. {
  34. return *m_ms;
  35. }
  36. const Is& getIs() const
  37. {
  38. return *m_is;
  39. }
  40. Is& getIs()
  41. {
  42. return *m_is;
  43. }
  44. const Pps& getPps() const
  45. {
  46. return *m_pps;
  47. }
  48. Pps& getPps()
  49. {
  50. return *m_pps;
  51. }
  52. const Dbg& getDbg() const
  53. {
  54. return *m_dbg;
  55. }
  56. Dbg& getDbg()
  57. {
  58. return *m_dbg;
  59. }
  60. U32 getWidth() const
  61. {
  62. return m_width;
  63. }
  64. U32 getHeight() const
  65. {
  66. return m_height;
  67. }
  68. F32 getAspectRatio() const
  69. {
  70. return F32(m_width) / m_height;
  71. }
  72. /// Init the renderer.
  73. ANKI_USE_RESULT Error init(
  74. ThreadPool* threadpool,
  75. ResourceManager* resources,
  76. GrManager* gr,
  77. HeapAllocator<U8> alloc,
  78. StackAllocator<U8> frameAlloc,
  79. const ConfigSet& config,
  80. const Timestamp* globalTimestamp);
  81. /// Set the output of the renderer before calling #render.
  82. void setOutputFramebuffer(FramebufferPtr outputFb, U32 width, U32 height)
  83. {
  84. m_outputFb = outputFb;
  85. m_outputFbSize = UVec2(width, height);
  86. }
  87. /// This function does all the rendering stages and produces a final result.
  88. ANKI_USE_RESULT Error render(
  89. SceneNode& frustumableNode, U frustumIdx,
  90. Array<CommandBufferPtr, RENDERER_COMMAND_BUFFERS_COUNT>& cmdBuff);
  91. anki_internal:
  92. static const U TILE_SIZE = 64;
  93. void getOutputFramebuffer(FramebufferPtr& outputFb, U32& width, U32& height)
  94. {
  95. if(m_outputFb.isCreated())
  96. {
  97. outputFb = m_outputFb;
  98. width = m_outputFbSize.x();
  99. height = m_outputFbSize.y();
  100. }
  101. }
  102. U32 getFramesCount() const
  103. {
  104. return m_framesNum;
  105. }
  106. const FrustumComponent& getActiveFrustumComponent() const
  107. {
  108. ANKI_ASSERT(m_frc);
  109. return *m_frc;
  110. }
  111. FrustumComponent& getActiveFrustumComponent()
  112. {
  113. ANKI_ASSERT(m_frc);
  114. return *m_frc;
  115. }
  116. const RenderableDrawer& getSceneDrawer() const
  117. {
  118. return m_sceneDrawer;
  119. }
  120. RenderableDrawer& getSceneDrawer()
  121. {
  122. return m_sceneDrawer;
  123. }
  124. Timestamp getProjectionParametersUpdateTimestamp() const
  125. {
  126. return m_projectionParamsUpdateTimestamp;
  127. }
  128. const Vec4& getProjectionParameters() const
  129. {
  130. return m_projectionParams;
  131. }
  132. U getSamples() const
  133. {
  134. return m_samples;
  135. }
  136. Bool getTessellationEnabled() const
  137. {
  138. return m_tessellation;
  139. }
  140. U getTileCount() const
  141. {
  142. return m_tileCount;
  143. }
  144. const UVec2& getTileCountXY() const
  145. {
  146. return m_tileCountXY;
  147. }
  148. U getClusterCount() const
  149. {
  150. return m_clusterer.getClusterCount();
  151. }
  152. const Clusterer& getClusterer() const
  153. {
  154. return m_clusterer;
  155. }
  156. const ShaderPtr& getDrawQuadVertexShader() const
  157. {
  158. return m_drawQuadVert->getGrShader();
  159. }
  160. /// My version of gluUnproject
  161. /// @param windowCoords Window screen coords
  162. /// @param modelViewMat The modelview matrix
  163. /// @param projectionMat The projection matrix
  164. /// @param view The view vector
  165. /// @return The unprojected coords
  166. static Vec3 unproject(const Vec3& windowCoords,
  167. const Mat4& modelViewMat, const Mat4& projectionMat,
  168. const int view[4]);
  169. /// Draws a quad. Actually it draws 2 triangles because OpenGL will no
  170. /// longer support quads
  171. void drawQuad(CommandBufferPtr& cmdb)
  172. {
  173. drawQuadInstanced(cmdb, 1);
  174. }
  175. void drawQuadConditional(
  176. OcclusionQueryPtr& q, CommandBufferPtr& cmdb)
  177. {
  178. cmdb->drawArraysConditional(q, 3, 1);
  179. }
  180. void drawQuadInstanced(CommandBufferPtr& cmdb, U32 primitiveCount)
  181. {
  182. cmdb->drawArrays(3, primitiveCount);
  183. }
  184. /// Get the LOD given the distance of an object from the camera
  185. F32 calculateLod(F32 distance) const
  186. {
  187. return distance / m_lodDistance;
  188. }
  189. /// Create a pipeline object that has as a vertex shader the m_drawQuadVert
  190. /// and the given fragment progam
  191. void createDrawQuadPipeline(
  192. ShaderPtr frag,
  193. const ColorStateInfo& colorState,
  194. PipelinePtr& ppline);
  195. /// Create a framebuffer attachment texture
  196. void createRenderTarget(U32 w, U32 h,
  197. const PixelFormat& format, U32 samples, SamplingFilter filter,
  198. U mipsCount, TexturePtr& rt);
  199. Bool doGpuVisibilityTest(const CollisionShape& cs, const Aabb& aabb) const;
  200. void prepareForVisibilityTests(const SceneNode& cam);
  201. GrManager& getGrManager()
  202. {
  203. return *m_gr;
  204. }
  205. HeapAllocator<U8> getAllocator() const
  206. {
  207. return m_alloc;
  208. }
  209. StackAllocator<U8> getFrameAllocator() const
  210. {
  211. return m_frameAlloc;
  212. }
  213. ResourceManager& getResourceManager()
  214. {
  215. return *m_resources;
  216. }
  217. ThreadPool& getThreadPool()
  218. {
  219. return *m_threadpool;
  220. }
  221. Timestamp getGlobalTimestamp() const
  222. {
  223. return *m_globalTimestamp;
  224. }
  225. const Timestamp* getGlobalTimestampPtr()
  226. {
  227. return m_globalTimestamp;
  228. }
  229. private:
  230. ThreadPool* m_threadpool;
  231. ResourceManager* m_resources;
  232. GrManager* m_gr;
  233. HeapAllocator<U8> m_alloc;
  234. StackAllocator<U8> m_frameAlloc;
  235. const Timestamp* m_globalTimestamp = nullptr;
  236. Clusterer m_clusterer;
  237. /// @name Rendering stages
  238. /// @{
  239. UniquePtr<Ms> m_ms; ///< Material rendering stage
  240. UniquePtr<Is> m_is; ///< Illumination rendering stage
  241. UniquePtr<Refl> m_refl; ///< Reflections.
  242. UniquePtr<Tiler> m_tiler;
  243. UniquePtr<Pps> m_pps; ///< Postprocessing rendering stage
  244. UniquePtr<Fs> m_fs; ///< Forward shading.
  245. UniquePtr<Lf> m_lf; ///< Forward shading lens flares.
  246. UniquePtr<Dbg> m_dbg; ///< Debug stage.
  247. /// @}
  248. U32 m_width;
  249. U32 m_height;
  250. F32 m_lodDistance; ///< Distance that used to calculate the LOD
  251. U8 m_samples; ///< Number of sample in multisampling
  252. Bool8 m_isOffscreen; ///< Is offscreen renderer?
  253. Bool8 m_tessellation;
  254. U32 m_tileCount;
  255. UVec2 m_tileCountXY;
  256. ShaderResourcePtr m_drawQuadVert;
  257. /// @name Optimization vars
  258. /// Used in other stages
  259. /// @{
  260. /// A vector that contains useful numbers for calculating the view space
  261. /// position from the depth
  262. Vec4 m_projectionParams = Vec4(0.0);
  263. Timestamp m_projectionParamsUpdateTimestamp = 0;
  264. /// @}
  265. FrustumComponent* m_frc = nullptr; ///< Cache current frustum component.
  266. RenderableDrawer m_sceneDrawer;
  267. U m_framesNum; ///< Frame number
  268. FramebufferPtr m_outputFb;
  269. UVec2 m_outputFbSize;
  270. TexturePtr m_reflectionsCubemapArr;
  271. ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);
  272. };
  273. /// @}
  274. } // end namespace anki