Renderer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "anki/renderer/Renderer.h"
  6. #include "anki/scene/Camera.h"
  7. #include "anki/scene/SceneGraph.h"
  8. #include "anki/core/Counters.h"
  9. #include "anki/misc/ConfigSet.h"
  10. #include "anki/renderer/Ms.h"
  11. #include "anki/renderer/Is.h"
  12. #include "anki/renderer/Pps.h"
  13. #include "anki/renderer/Fs.h"
  14. #include "anki/renderer/Lf.h"
  15. #include "anki/renderer/Dbg.h"
  16. #include "anki/renderer/Tiler.h"
  17. namespace anki {
  18. //==============================================================================
  19. Renderer::Renderer()
  20. : m_sceneDrawer(this)
  21. {}
  22. //==============================================================================
  23. Renderer::~Renderer()
  24. {}
  25. //==============================================================================
  26. Error Renderer::init(
  27. ThreadPool* threadpool,
  28. ResourceManager* resources,
  29. GrManager* gl,
  30. HeapAllocator<U8> alloc,
  31. StackAllocator<U8> frameAlloc,
  32. const ConfigSet& config,
  33. const Timestamp* globalTimestamp,
  34. TexturePtr reflections)
  35. {
  36. m_globalTimestamp = globalTimestamp;
  37. m_threadpool = threadpool;
  38. m_resources = resources;
  39. m_gr = gl;
  40. m_alloc = alloc;
  41. m_frameAlloc = frameAlloc;
  42. m_reflectionsCubemapArr = reflections;
  43. Error err = initInternal(config);
  44. if(err)
  45. {
  46. ANKI_LOGE("Failed to initialize the renderer");
  47. }
  48. return err;
  49. }
  50. //==============================================================================
  51. Error Renderer::initInternal(const ConfigSet& config)
  52. {
  53. // Set from the config
  54. m_width = getAlignedRoundDown(TILE_SIZE, U(config.getNumber("width")));
  55. m_height = getAlignedRoundDown(TILE_SIZE, U(config.getNumber("height")));
  56. m_lodDistance = config.getNumber("lodDistance");
  57. m_framesNum = 0;
  58. m_samples = config.getNumber("samples");
  59. m_tileCountXY.x() = m_width / TILE_SIZE;
  60. m_tileCountXY.y() = m_height / TILE_SIZE;
  61. m_tileCount = m_tileCountXY.x() * m_tileCountXY.y();
  62. m_clusterer.init(getAllocator(), m_tileCountXY.x(), m_tileCountXY.y(),
  63. config.getNumber("clusterSizeZ"));
  64. m_tessellation = config.getNumber("tessellation");
  65. // A few sanity checks
  66. if(m_samples != 1 && m_samples != 4 && m_samples != 8 && m_samples != 16
  67. && m_samples != 32)
  68. {
  69. ANKI_LOGE("Incorrect samples");
  70. return ErrorCode::USER_DATA;
  71. }
  72. if(m_width < 10 || m_height < 10)
  73. {
  74. ANKI_LOGE("Incorrect sizes");
  75. return ErrorCode::USER_DATA;
  76. }
  77. if(m_width % m_tileCountXY.x() != 0)
  78. {
  79. ANKI_LOGE("Width is not multiple of tile width");
  80. return ErrorCode::USER_DATA;
  81. }
  82. if(m_height % m_tileCountXY.y() != 0)
  83. {
  84. ANKI_LOGE("Height is not multiple of tile height");
  85. return ErrorCode::USER_DATA;
  86. }
  87. // quad setup
  88. ANKI_CHECK(
  89. m_resources->loadResource("shaders/Quad.vert.glsl", m_drawQuadVert));
  90. // Init the stages. Careful with the order!!!!!!!!!!
  91. m_ms.reset(m_alloc.newInstance<Ms>(this));
  92. ANKI_CHECK(m_ms->init(config));
  93. m_tiler.reset(m_alloc.newInstance<Tiler>(this));
  94. ANKI_CHECK(m_tiler->init());
  95. m_is.reset(m_alloc.newInstance<Is>(this));
  96. ANKI_CHECK(m_is->init(config));
  97. m_fs.reset(m_alloc.newInstance<Fs>(this));
  98. ANKI_CHECK(m_fs->init(config));
  99. m_lf.reset(m_alloc.newInstance<Lf>(this));
  100. ANKI_CHECK(m_lf->init(config));
  101. m_pps.reset(m_alloc.newInstance<Pps>(this));
  102. ANKI_CHECK(m_pps->init(config));
  103. m_dbg.reset(m_alloc.newInstance<Dbg>(this));
  104. ANKI_CHECK(m_dbg->init(config));
  105. return ErrorCode::NONE;
  106. }
  107. //==============================================================================
  108. Error Renderer::render(SceneNode& frustumableNode, U frustumIdx,
  109. Array<CommandBufferPtr, RENDERER_COMMAND_BUFFERS_COUNT>& cmdb)
  110. {
  111. m_frc = nullptr;
  112. Error err = frustumableNode.iterateComponentsOfType<FrustumComponent>(
  113. [&](FrustumComponent& frc) -> Error
  114. {
  115. if(frustumIdx == 0)
  116. {
  117. m_frc = &frc;
  118. }
  119. else
  120. {
  121. --frustumIdx;
  122. }
  123. return ErrorCode::NONE;
  124. });
  125. (void)err;
  126. ANKI_ASSERT(m_frc && "Not enough frustum components");
  127. m_frameAlloc.getMemoryPool().reset();
  128. // Calc a few vars
  129. //
  130. if(m_frc->getProjectionParameters() != m_projectionParams)
  131. {
  132. m_projectionParams = m_frc->getProjectionParameters();
  133. m_projectionParamsUpdateTimestamp = getGlobalTimestamp();
  134. }
  135. ANKI_ASSERT(m_frc->getFrustum().getType() == Frustum::Type::PERSPECTIVE);
  136. m_clusterer.prepare(getThreadPool(), frustumableNode);
  137. ANKI_COUNTER_START_TIMER(RENDERER_MS_TIME);
  138. ANKI_CHECK(m_ms->run(cmdb[0]));
  139. ANKI_COUNTER_STOP_TIMER_INC(RENDERER_MS_TIME);
  140. m_lf->runOcclusionTests(cmdb[0]);
  141. m_ms->generateMipmaps(cmdb[0]);
  142. m_tiler->run(cmdb[0]);
  143. ANKI_COUNTER_START_TIMER(RENDERER_IS_TIME);
  144. ANKI_CHECK(m_is->run(cmdb[1]));
  145. ANKI_COUNTER_STOP_TIMER_INC(RENDERER_IS_TIME);
  146. ANKI_CHECK(m_fs->run(cmdb[1]));
  147. m_lf->run(cmdb[1]);
  148. ANKI_COUNTER_START_TIMER(RENDERER_PPS_TIME);
  149. if(m_pps->getEnabled())
  150. {
  151. m_pps->run(cmdb[1]);
  152. }
  153. ANKI_COUNTER_STOP_TIMER_INC(RENDERER_PPS_TIME);
  154. if(m_dbg->getEnabled())
  155. {
  156. ANKI_CHECK(m_dbg->run(cmdb[1]));
  157. }
  158. ++m_framesNum;
  159. return ErrorCode::NONE;
  160. }
  161. //==============================================================================
  162. Vec3 Renderer::unproject(const Vec3& windowCoords, const Mat4& modelViewMat,
  163. const Mat4& projectionMat, const int view[4])
  164. {
  165. Mat4 invPm = projectionMat * modelViewMat;
  166. invPm.invert();
  167. // the vec is in NDC space meaning: -1<=vec.x<=1 -1<=vec.y<=1 -1<=vec.z<=1
  168. Vec4 vec;
  169. vec.x() = (2.0 * (windowCoords.x() - view[0])) / view[2] - 1.0;
  170. vec.y() = (2.0 * (windowCoords.y() - view[1])) / view[3] - 1.0;
  171. vec.z() = 2.0 * windowCoords.z() - 1.0;
  172. vec.w() = 1.0;
  173. Vec4 out = invPm * vec;
  174. out /= out.w();
  175. return out.xyz();
  176. }
  177. //==============================================================================
  178. void Renderer::createRenderTarget(U32 w, U32 h, const PixelFormat& format,
  179. U32 samples, SamplingFilter filter, U mipsCount, TexturePtr& rt)
  180. {
  181. // Not very important but keep the resolution of render targets aligned to
  182. // 16
  183. if(0)
  184. {
  185. ANKI_ASSERT(isAligned(16, w));
  186. ANKI_ASSERT(isAligned(16, h));
  187. }
  188. TextureInitializer init;
  189. init.m_width = w;
  190. init.m_height = h;
  191. init.m_depth = 0;
  192. init.m_type = TextureType::_2D;
  193. init.m_format = format;
  194. init.m_mipmapsCount = mipsCount;
  195. init.m_samples = samples;
  196. init.m_sampling.m_minMagFilter = filter;
  197. if(mipsCount > 1)
  198. {
  199. init.m_sampling.m_mipmapFilter = filter;
  200. }
  201. else
  202. {
  203. init.m_sampling.m_mipmapFilter = SamplingFilter::BASE;
  204. }
  205. init.m_sampling.m_repeat = false;
  206. init.m_sampling.m_anisotropyLevel = 0;
  207. rt = m_gr->newInstance<Texture>(init);
  208. }
  209. //==============================================================================
  210. void Renderer::createDrawQuadPipeline(
  211. ShaderPtr frag, const ColorStateInfo& colorState,
  212. PipelinePtr& ppline)
  213. {
  214. PipelineInitializer init;
  215. init.m_inputAssembler.m_topology = PrimitiveTopology::TRIANGLE_STRIP;
  216. init.m_depthStencil.m_depthWriteEnabled = false;
  217. init.m_depthStencil.m_depthCompareFunction = CompareOperation::ALWAYS;
  218. init.m_color = colorState;
  219. init.m_shaders[U(ShaderType::VERTEX)] = m_drawQuadVert->getGrShader();
  220. init.m_shaders[U(ShaderType::FRAGMENT)] = frag;
  221. ppline = m_gr->newInstance<Pipeline>(init);
  222. }
  223. //==============================================================================
  224. void Renderer::prepareForVisibilityTests(const SceneNode& cam)
  225. {
  226. m_tiler->prepareForVisibilityTests(cam);
  227. }
  228. //==============================================================================
  229. Bool Renderer::doGpuVisibilityTest(const CollisionShape& cs,
  230. const Aabb& aabb) const
  231. {
  232. return m_tiler->test(cs, aabb);
  233. }
  234. } // end namespace anki