Renderer.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef ANKI_RENDERER_RENDERER_H
  2. #define ANKI_RENDERER_RENDERER_H
  3. #include "anki/Math.h"
  4. #include "anki/resource/TextureResource.h"
  5. #include "anki/resource/ShaderProgramResource.h"
  6. #include "anki/resource/Resource.h"
  7. #include "anki/gl/Gl.h"
  8. #include "anki/util/HighRezTimer.h"
  9. #include "anki/misc/ConfigSet.h"
  10. #include "anki/renderer/Common.h"
  11. #include "anki/renderer/Ms.h"
  12. #include "anki/renderer/Is.h"
  13. #include "anki/renderer/Pps.h"
  14. #include "anki/renderer/Bs.h"
  15. #include "anki/renderer/Dbg.h"
  16. #include "anki/renderer/Tiler.h"
  17. #include "anki/renderer/Drawer.h"
  18. namespace anki {
  19. /// A struct to initialize the renderer. It contains a few extra params for
  20. /// the MainRenderer. Open Renderer.cpp to see all the options
  21. class RendererInitializer: public ConfigSet
  22. {
  23. public:
  24. RendererInitializer();
  25. };
  26. /// Offscreen renderer. It is a class and not a namespace because we may need
  27. /// external renderers for security cameras for example
  28. class Renderer
  29. {
  30. public:
  31. Renderer();
  32. ~Renderer();
  33. /// @name Accessors
  34. /// @{
  35. const Ms& getMs() const
  36. {
  37. return ms;
  38. }
  39. Ms& getMs()
  40. {
  41. return ms;
  42. }
  43. const Is& getIs() const
  44. {
  45. return is;
  46. }
  47. Is& getIs()
  48. {
  49. return is;
  50. }
  51. const Tiler& getTiler() const
  52. {
  53. return tiler;
  54. }
  55. Tiler& getTiler()
  56. {
  57. return tiler;
  58. }
  59. const Pps& getPps() const
  60. {
  61. return pps;
  62. }
  63. Pps& getPps()
  64. {
  65. return pps;
  66. }
  67. const Dbg& getDbg() const
  68. {
  69. return dbg;
  70. }
  71. Dbg& getDbg()
  72. {
  73. return dbg;
  74. }
  75. U32 getWidth() const
  76. {
  77. return width;
  78. }
  79. U32 getHeight() const
  80. {
  81. return height;
  82. }
  83. U32 getWindowWidth() const
  84. {
  85. ANKI_ASSERT(!isOffscreen);
  86. return width / renderingQuality;
  87. }
  88. U32 getWindowHeight() const
  89. {
  90. ANKI_ASSERT(!isOffscreen);
  91. return height / renderingQuality;
  92. }
  93. F32 getAspectRatio() const
  94. {
  95. return F32(width) / height;
  96. }
  97. U32 getFramesCount() const
  98. {
  99. return framesNum;
  100. }
  101. const SceneGraph& getSceneGraph() const
  102. {
  103. return *scene;
  104. }
  105. SceneGraph& getSceneGraph()
  106. {
  107. return *scene;
  108. }
  109. const RenderableDrawer& getSceneDrawer() const
  110. {
  111. return sceneDrawer;
  112. }
  113. RenderableDrawer& getSceneDrawer()
  114. {
  115. return sceneDrawer;
  116. }
  117. const Vec2& getPlanes() const
  118. {
  119. return planes;
  120. }
  121. const Vec2& getLimitsOfNearPlane() const
  122. {
  123. return limitsOfNearPlane;
  124. }
  125. const Vec2& getLimitsOfNearPlane2() const
  126. {
  127. return limitsOfNearPlane2;
  128. }
  129. Timestamp getPlanesUpdateTimestamp() const
  130. {
  131. return planesUpdateTimestamp;
  132. }
  133. U getSamples() const
  134. {
  135. return samples;
  136. }
  137. Bool getUseMrt() const
  138. {
  139. return useMrt;
  140. }
  141. Bool getIsOffscreen() const
  142. {
  143. return isOffscreen;
  144. }
  145. Bool usesTessellation() const
  146. {
  147. return tessellation;
  148. }
  149. F32 getRenderingQuality() const
  150. {
  151. return renderingQuality;
  152. }
  153. U32 getMaxTextureSize() const
  154. {
  155. return maxTextureSize;
  156. }
  157. const UVec2& getTilesCount() const
  158. {
  159. return tilesCount;
  160. }
  161. /// Get string to pass to the material shaders
  162. const std::string& getShaderPostProcessorString() const
  163. {
  164. return shaderPostProcessorString;
  165. }
  166. /// @}
  167. /// Init the renderer given an initialization class
  168. /// @param initializer The initializer class
  169. void init(const RendererInitializer& initializer);
  170. /// This function does all the rendering stages and produces a final FAI
  171. void render(SceneGraph& scene);
  172. /// My version of gluUnproject
  173. /// @param windowCoords Window screen coords
  174. /// @param modelViewMat The modelview matrix
  175. /// @param projectionMat The projection matrix
  176. /// @param view The view vector
  177. /// @return The unprojected coords
  178. static Vec3 unproject(const Vec3& windowCoords,
  179. const Mat4& modelViewMat, const Mat4& projectionMat,
  180. const int view[4]);
  181. /// Draws a quad. Actually it draws 2 triangles because OpenGL will no
  182. /// longer support quads
  183. void drawQuad();
  184. void drawQuadInstanced(U32 primitiveCount);
  185. /// Calculate the planes needed for the calculation of the fragment
  186. /// position z in view space. Having the fragment's depth, the camera's
  187. /// zNear and zFar the z of the fragment is being calculated inside the
  188. /// fragment shader from:
  189. /// @code z = (- zFar * zNear) / (zFar - depth * (zFar - zNear))
  190. /// @endcode
  191. /// The above can be optimized and this method actually pre-calculates a
  192. /// few things in order to lift a few calculations from the fragment
  193. /// shader. So the z is:
  194. /// @code z = -planes.y / (planes.x + depth) @endcode
  195. /// @param[in] cameraRange The zNear, zFar
  196. /// @param[out] planes The planes
  197. static void calcPlanes(const Vec2& cameraRange, Vec2& planes);
  198. /// Calculates two values needed for the calculation of the fragment
  199. /// position in view space.
  200. static void calcLimitsOfNearPlane(const class PerspectiveCamera& cam,
  201. Vec2& limitsOfNearPlane);
  202. /// Get the LOD given the distance of an object from the camera
  203. F32 calculateLod(F32 distance) const
  204. {
  205. return distance / lodDistance;
  206. }
  207. /// On some GPUs its optimal to clean after binding to an FBO and there is
  208. /// no use for it's previous contents. For other GPUs the clear will be
  209. /// skipped
  210. void clearAfterBindingFbo(const GLenum cap);
  211. private:
  212. /// @name Rendering stages
  213. /// @{
  214. Ms ms; ///< Material rendering stage
  215. Is is; ///< Illumination rendering stage
  216. Pps pps; ///< Postprocessing rendering stage
  217. Bs bs; ///< Blending stage
  218. Dbg dbg; ///< Debug stage
  219. Tiler tiler;
  220. /// @}
  221. /// Width of the rendering. Don't confuse with the window width
  222. U32 width;
  223. /// Height of the rendering. Don't confuse with the window height
  224. U32 height;
  225. F32 lodDistance; ///< Distance that used to calculate the LOD
  226. U8 samples; ///< Number of sample in multisampling
  227. Bool8 useMrt; ///< Use MRT or pack things inside the G buffer
  228. Bool8 isOffscreen; ///< Is offscreen renderer?
  229. Bool8 tessellation;
  230. F32 renderingQuality; ///< Rendering quality. Relevant for offscreen
  231. U32 maxTextureSize; ///< Texture size limit. Just kept here.
  232. UVec2 tilesCount;
  233. /// @name For drawing a quad into the active framebuffer
  234. /// @{
  235. Vbo quadPositionsVbo; ///< The VBO for quad positions
  236. Vao quadVao; ///< This VAO is used everywhere except material stage
  237. /// @}
  238. /// @name Optimization vars
  239. /// Used in other stages
  240. /// @{
  241. Timestamp planesUpdateTimestamp = getGlobTimestamp();
  242. /// Used to to calculate the frag pos in view space inside a few shader
  243. /// programs
  244. Vec2 planes;
  245. /// Used to to calculate the frag pos in view space inside a few shader
  246. /// programs
  247. Vec2 limitsOfNearPlane;
  248. /// Used to to calculate the frag pos in view space inside a few shader
  249. /// programs
  250. Vec2 limitsOfNearPlane2;
  251. /// @}
  252. SceneGraph* scene; ///< Current scene
  253. RenderableDrawer sceneDrawer;
  254. U framesNum; ///< Frame number
  255. /// String to pass to the material shaders
  256. std::string shaderPostProcessorString;
  257. };
  258. } // end namespace anki
  259. #endif