Pipeline.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/Vulkan/Pipeline.h>
  6. #include <AnKi/Gr/Vulkan/GrManagerImpl.h>
  7. #include <AnKi/Gr/Utils/Functions.h>
  8. #include <AnKi/Util/Tracer.h>
  9. namespace anki {
  10. void PipelineStateTracker::reset()
  11. {
  12. m_state.reset();
  13. m_hashes = {};
  14. m_dirty = {};
  15. m_set = {};
  16. m_shaderAttributeMask.unsetAll();
  17. m_shaderColorAttachmentWritemask.unsetAll();
  18. m_fbDepth = false;
  19. m_fbStencil = false;
  20. m_defaultFb = false;
  21. m_fbColorAttachmentMask.unsetAll();
  22. }
  23. Bool PipelineStateTracker::updateHashes()
  24. {
  25. Bool stateDirty = false;
  26. // Prog
  27. if(m_dirty.m_prog)
  28. {
  29. m_dirty.m_prog = false;
  30. stateDirty = true;
  31. m_hashes.m_prog = m_state.m_prog->getUuid();
  32. }
  33. // Rpass
  34. if(m_dirty.m_rpass)
  35. {
  36. m_dirty.m_rpass = false;
  37. stateDirty = true;
  38. m_hashes.m_rpass = ptrToNumber(m_state.m_rpass);
  39. }
  40. // Vertex
  41. if(m_dirty.m_attribs.getAny() || m_dirty.m_vertBindings.getAny())
  42. {
  43. for(U i = 0; i < MAX_VERTEX_ATTRIBUTES; ++i)
  44. {
  45. if(m_shaderAttributeMask.get(i))
  46. {
  47. ANKI_ASSERT(m_set.m_attribs.get(i) && "Forgot to set the attribute");
  48. Bool dirty = false;
  49. if(m_dirty.m_attribs.get(i))
  50. {
  51. m_dirty.m_attribs.unset(i);
  52. dirty = true;
  53. }
  54. const U binding = m_state.m_vertex.m_attributes[i].m_binding;
  55. ANKI_ASSERT(m_set.m_vertBindings.get(binding) && "Forgot to set a vertex binding");
  56. if(m_dirty.m_vertBindings.get(binding))
  57. {
  58. m_dirty.m_vertBindings.unset(binding);
  59. dirty = true;
  60. }
  61. if(dirty)
  62. {
  63. m_hashes.m_vertexAttribs[i] =
  64. computeHash(&m_state.m_vertex.m_attributes[i], sizeof(m_state.m_vertex.m_attributes[i]));
  65. m_hashes.m_vertexAttribs[i] =
  66. appendHash(&m_state.m_vertex.m_bindings[i], sizeof(m_state.m_vertex.m_bindings[i]),
  67. m_hashes.m_vertexAttribs[i]);
  68. stateDirty = true;
  69. }
  70. }
  71. }
  72. }
  73. // IA
  74. if(m_dirty.m_inputAssembler)
  75. {
  76. m_dirty.m_inputAssembler = false;
  77. stateDirty = true;
  78. m_hashes.m_ia = computeHash(&m_state.m_inputAssembler, sizeof(m_state.m_inputAssembler));
  79. }
  80. // Rasterizer
  81. if(m_dirty.m_rasterizer)
  82. {
  83. m_dirty.m_rasterizer = false;
  84. stateDirty = true;
  85. m_hashes.m_raster = computeHash(&m_state.m_rasterizer, sizeof(m_state.m_rasterizer));
  86. }
  87. // Depth
  88. if(m_fbDepth && m_dirty.m_depth)
  89. {
  90. m_dirty.m_depth = false;
  91. stateDirty = true;
  92. m_hashes.m_depth = computeHash(&m_state.m_depth, sizeof(m_state.m_depth));
  93. }
  94. // Stencil
  95. if(m_fbStencil && m_dirty.m_stencil)
  96. {
  97. m_dirty.m_stencil = false;
  98. stateDirty = true;
  99. m_hashes.m_stencil = computeHash(&m_state.m_stencil, sizeof(m_state.m_stencil));
  100. }
  101. // Color
  102. if(!!m_fbColorAttachmentMask)
  103. {
  104. ANKI_ASSERT(m_fbColorAttachmentMask == m_shaderColorAttachmentWritemask
  105. && "Shader and fb should have same attachment mask");
  106. if(m_dirty.m_color)
  107. {
  108. m_dirty.m_color = false;
  109. m_hashes.m_color = m_state.m_color.m_alphaToCoverageEnabled ? 1 : 2;
  110. stateDirty = true;
  111. }
  112. if(!!(m_dirty.m_colAttachments & m_fbColorAttachmentMask))
  113. {
  114. for(U i = 0; i < MAX_COLOR_ATTACHMENTS; ++i)
  115. {
  116. if(m_fbColorAttachmentMask.get(i) && m_dirty.m_colAttachments.get(i))
  117. {
  118. m_dirty.m_colAttachments.unset(i);
  119. m_hashes.m_colAttachments[i] =
  120. computeHash(&m_state.m_color.m_attachments[i], sizeof(m_state.m_color.m_attachments[i]));
  121. stateDirty = true;
  122. }
  123. }
  124. }
  125. }
  126. return stateDirty;
  127. }
  128. void PipelineStateTracker::updateSuperHash()
  129. {
  130. Array<U64, sizeof(Hashes) / sizeof(U64)> buff;
  131. U count = 0;
  132. // Prog
  133. buff[count++] = m_hashes.m_prog;
  134. // Rpass
  135. buff[count++] = m_hashes.m_rpass;
  136. // Vertex
  137. if(!!m_shaderAttributeMask)
  138. {
  139. for(U i = 0; i < MAX_VERTEX_ATTRIBUTES; ++i)
  140. {
  141. if(m_shaderAttributeMask.get(i))
  142. {
  143. buff[count++] = m_hashes.m_vertexAttribs[i];
  144. }
  145. }
  146. }
  147. // IA
  148. buff[count++] = m_hashes.m_ia;
  149. // Rasterizer
  150. buff[count++] = m_hashes.m_raster;
  151. // Depth
  152. if(m_fbDepth)
  153. {
  154. buff[count++] = m_hashes.m_depth;
  155. }
  156. // Stencil
  157. if(m_fbStencil)
  158. {
  159. buff[count++] = m_hashes.m_stencil;
  160. }
  161. // Color
  162. if(!!m_shaderColorAttachmentWritemask)
  163. {
  164. buff[count++] = m_hashes.m_color;
  165. for(U i = 0; i < MAX_COLOR_ATTACHMENTS; ++i)
  166. {
  167. if(m_shaderColorAttachmentWritemask.get(i))
  168. {
  169. buff[count++] = m_hashes.m_colAttachments[i];
  170. }
  171. }
  172. }
  173. // Super hash
  174. m_hashes.m_superHash = computeHash(&buff[0], count * sizeof(buff[0]));
  175. }
  176. const VkGraphicsPipelineCreateInfo& PipelineStateTracker::updatePipelineCreateInfo()
  177. {
  178. VkGraphicsPipelineCreateInfo& ci = m_ci.m_ppline;
  179. ci = {};
  180. ci.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  181. if(m_pipelineStatisticsEnabled)
  182. {
  183. ci.flags |= VK_PIPELINE_CREATE_CAPTURE_STATISTICS_BIT_KHR;
  184. }
  185. // Prog
  186. ci.pStages = static_cast<const ShaderProgramImpl&>(*m_state.m_prog).getShaderCreateInfos(ci.stageCount);
  187. // Vert
  188. VkPipelineVertexInputStateCreateInfo& vertCi = m_ci.m_vert;
  189. vertCi = {};
  190. vertCi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  191. vertCi.pVertexAttributeDescriptions = &m_ci.m_attribs[0];
  192. vertCi.pVertexBindingDescriptions = &m_ci.m_vertBindings[0];
  193. BitSet<MAX_VERTEX_ATTRIBUTES, U8> bindingSet = {false};
  194. for(U32 i = 0; i < MAX_VERTEX_ATTRIBUTES; ++i)
  195. {
  196. if(m_shaderAttributeMask.get(i))
  197. {
  198. VkVertexInputAttributeDescription& attrib = m_ci.m_attribs[vertCi.vertexAttributeDescriptionCount++];
  199. attrib.binding = m_state.m_vertex.m_attributes[i].m_binding;
  200. attrib.format = convertFormat(m_state.m_vertex.m_attributes[i].m_format);
  201. attrib.location = i;
  202. attrib.offset = U32(m_state.m_vertex.m_attributes[i].m_offset);
  203. if(!bindingSet.get(attrib.binding))
  204. {
  205. bindingSet.set(attrib.binding);
  206. VkVertexInputBindingDescription& binding = m_ci.m_vertBindings[vertCi.vertexBindingDescriptionCount++];
  207. binding.binding = attrib.binding;
  208. binding.inputRate = convertVertexStepRate(m_state.m_vertex.m_bindings[attrib.binding].m_stepRate);
  209. binding.stride = m_state.m_vertex.m_bindings[attrib.binding].m_stride;
  210. }
  211. }
  212. }
  213. ci.pVertexInputState = &vertCi;
  214. // IA
  215. VkPipelineInputAssemblyStateCreateInfo& iaCi = m_ci.m_ia;
  216. iaCi = {};
  217. iaCi.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  218. iaCi.primitiveRestartEnable = m_state.m_inputAssembler.m_primitiveRestartEnabled;
  219. iaCi.topology = convertTopology(m_state.m_inputAssembler.m_topology);
  220. ci.pInputAssemblyState = &iaCi;
  221. // Viewport
  222. VkPipelineViewportStateCreateInfo& vpCi = m_ci.m_vp;
  223. vpCi = {};
  224. vpCi.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  225. vpCi.scissorCount = 1;
  226. vpCi.viewportCount = 1;
  227. ci.pViewportState = &vpCi;
  228. // Raster
  229. VkPipelineRasterizationStateCreateInfo& rastCi = m_ci.m_rast;
  230. rastCi = {};
  231. rastCi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  232. rastCi.depthClampEnable = false;
  233. rastCi.rasterizerDiscardEnable = false;
  234. rastCi.polygonMode = convertFillMode(m_state.m_rasterizer.m_fillMode);
  235. rastCi.cullMode = convertCullMode(m_state.m_rasterizer.m_cullMode);
  236. rastCi.frontFace = (!m_defaultFb) ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE; // For viewport flip
  237. rastCi.depthBiasEnable =
  238. m_state.m_rasterizer.m_depthBiasConstantFactor != 0.0 && m_state.m_rasterizer.m_depthBiasSlopeFactor != 0.0;
  239. rastCi.depthBiasConstantFactor = m_state.m_rasterizer.m_depthBiasConstantFactor;
  240. rastCi.depthBiasClamp = 0.0;
  241. rastCi.depthBiasSlopeFactor = m_state.m_rasterizer.m_depthBiasSlopeFactor;
  242. rastCi.lineWidth = 1.0;
  243. ci.pRasterizationState = &rastCi;
  244. if(m_state.m_rasterizer.m_rasterizationOrder != RasterizationOrder::ORDERED)
  245. {
  246. VkPipelineRasterizationStateRasterizationOrderAMD& rastOrderCi = m_ci.m_rasterOrder;
  247. rastOrderCi = {};
  248. rastOrderCi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD;
  249. rastOrderCi.rasterizationOrder = convertRasterizationOrder(m_state.m_rasterizer.m_rasterizationOrder);
  250. ANKI_ASSERT(rastCi.pNext == nullptr);
  251. rastCi.pNext = &rastOrderCi;
  252. }
  253. // MS
  254. VkPipelineMultisampleStateCreateInfo& msCi = m_ci.m_ms;
  255. msCi = {};
  256. msCi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  257. msCi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  258. ci.pMultisampleState = &msCi;
  259. // DS
  260. if(m_fbDepth || m_fbStencil)
  261. {
  262. VkPipelineDepthStencilStateCreateInfo& dsCi = m_ci.m_ds;
  263. dsCi = {};
  264. dsCi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  265. if(m_fbDepth)
  266. {
  267. dsCi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  268. dsCi.depthTestEnable = m_state.m_depth.m_depthCompareFunction != CompareOperation::ALWAYS
  269. || m_state.m_depth.m_depthWriteEnabled;
  270. dsCi.depthWriteEnable = m_state.m_depth.m_depthWriteEnabled;
  271. dsCi.depthCompareOp = convertCompareOp(m_state.m_depth.m_depthCompareFunction);
  272. }
  273. if(m_fbStencil)
  274. {
  275. dsCi.stencilTestEnable =
  276. !stencilTestDisabled(m_state.m_stencil.m_face[0].m_stencilFailOperation,
  277. m_state.m_stencil.m_face[0].m_stencilPassDepthFailOperation,
  278. m_state.m_stencil.m_face[0].m_stencilPassDepthPassOperation,
  279. m_state.m_stencil.m_face[0].m_compareFunction)
  280. || !stencilTestDisabled(m_state.m_stencil.m_face[1].m_stencilFailOperation,
  281. m_state.m_stencil.m_face[1].m_stencilPassDepthFailOperation,
  282. m_state.m_stencil.m_face[1].m_stencilPassDepthPassOperation,
  283. m_state.m_stencil.m_face[1].m_compareFunction);
  284. dsCi.front.failOp = convertStencilOp(m_state.m_stencil.m_face[0].m_stencilFailOperation);
  285. dsCi.front.passOp = convertStencilOp(m_state.m_stencil.m_face[0].m_stencilPassDepthPassOperation);
  286. dsCi.front.depthFailOp = convertStencilOp(m_state.m_stencil.m_face[0].m_stencilPassDepthFailOperation);
  287. dsCi.front.compareOp = convertCompareOp(m_state.m_stencil.m_face[0].m_compareFunction);
  288. dsCi.back.failOp = convertStencilOp(m_state.m_stencil.m_face[1].m_stencilFailOperation);
  289. dsCi.back.passOp = convertStencilOp(m_state.m_stencil.m_face[1].m_stencilPassDepthPassOperation);
  290. dsCi.back.depthFailOp = convertStencilOp(m_state.m_stencil.m_face[1].m_stencilPassDepthFailOperation);
  291. dsCi.back.compareOp = convertCompareOp(m_state.m_stencil.m_face[1].m_compareFunction);
  292. }
  293. ci.pDepthStencilState = &dsCi;
  294. }
  295. // Color/blend
  296. if(!!m_shaderColorAttachmentWritemask)
  297. {
  298. VkPipelineColorBlendStateCreateInfo& colCi = m_ci.m_color;
  299. colCi = {};
  300. colCi.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  301. colCi.attachmentCount = m_shaderColorAttachmentWritemask.getEnabledBitCount();
  302. colCi.pAttachments = &m_ci.m_colAttachments[0];
  303. for(U i = 0; i < colCi.attachmentCount; ++i)
  304. {
  305. ANKI_ASSERT(m_shaderColorAttachmentWritemask.get(i) && "No gaps are allowed");
  306. VkPipelineColorBlendAttachmentState& out = m_ci.m_colAttachments[i];
  307. const ColorAttachmentState& in = m_state.m_color.m_attachments[i];
  308. out.blendEnable = !blendingDisabled(in.m_srcBlendFactorRgb, in.m_dstBlendFactorRgb, in.m_srcBlendFactorA,
  309. in.m_dstBlendFactorA, in.m_blendFunctionRgb, in.m_blendFunctionA);
  310. out.srcColorBlendFactor = convertBlendFactor(in.m_srcBlendFactorRgb);
  311. out.dstColorBlendFactor = convertBlendFactor(in.m_dstBlendFactorRgb);
  312. out.srcAlphaBlendFactor = convertBlendFactor(in.m_srcBlendFactorA);
  313. out.dstAlphaBlendFactor = convertBlendFactor(in.m_dstBlendFactorA);
  314. out.colorBlendOp = convertBlendOperation(in.m_blendFunctionRgb);
  315. out.alphaBlendOp = convertBlendOperation(in.m_blendFunctionA);
  316. out.colorWriteMask = convertColorWriteMask(in.m_channelWriteMask);
  317. }
  318. ci.pColorBlendState = &colCi;
  319. }
  320. // Dyn state
  321. VkPipelineDynamicStateCreateInfo& dynCi = m_ci.m_dyn;
  322. dynCi = {};
  323. dynCi.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  324. // Almost all state is dynamic. Depth bias is static
  325. static const Array<VkDynamicState, 9> DYN = {
  326. {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_BLEND_CONSTANTS,
  327. VK_DYNAMIC_STATE_DEPTH_BOUNDS, VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK, VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
  328. VK_DYNAMIC_STATE_STENCIL_REFERENCE, VK_DYNAMIC_STATE_LINE_WIDTH, VK_DYNAMIC_STATE_FRAGMENT_SHADING_RATE_KHR}};
  329. dynCi.dynamicStateCount = DYN.getSize();
  330. dynCi.pDynamicStates = &DYN[0];
  331. ci.pDynamicState = &dynCi;
  332. // The rest
  333. ci.layout = static_cast<const ShaderProgramImpl&>(*m_state.m_prog).getPipelineLayout().getHandle();
  334. ci.renderPass = m_state.m_rpass;
  335. ci.subpass = 0;
  336. return ci;
  337. }
  338. class PipelineFactory::PipelineInternal
  339. {
  340. public:
  341. VkPipeline m_handle = VK_NULL_HANDLE;
  342. };
  343. class PipelineFactory::Hasher
  344. {
  345. public:
  346. U64 operator()(U64 h)
  347. {
  348. return h;
  349. }
  350. };
  351. void PipelineFactory::destroy()
  352. {
  353. for(auto it : m_pplines)
  354. {
  355. if(it.m_handle)
  356. {
  357. vkDestroyPipeline(m_dev, it.m_handle, nullptr);
  358. }
  359. }
  360. m_pplines.destroy(m_alloc);
  361. }
  362. void PipelineFactory::getOrCreatePipeline(PipelineStateTracker& state, Pipeline& ppline, Bool& stateDirty)
  363. {
  364. ANKI_TRACE_SCOPED_EVENT(VK_PIPELINE_GET_OR_CREATE);
  365. U64 hash;
  366. state.flush(hash, stateDirty);
  367. if(ANKI_UNLIKELY(!stateDirty))
  368. {
  369. ppline.m_handle = VK_NULL_HANDLE;
  370. return;
  371. }
  372. // Check if ppline exists
  373. {
  374. RLockGuard<RWMutex> lock(m_pplinesMtx);
  375. auto it = m_pplines.find(hash);
  376. if(it != m_pplines.getEnd())
  377. {
  378. ppline.m_handle = (*it).m_handle;
  379. ANKI_TRACE_INC_COUNTER(VK_PIPELINES_CACHE_HIT, 1);
  380. return;
  381. }
  382. }
  383. // Doesnt exist. Need to create it
  384. WLockGuard<RWMutex> lock(m_pplinesMtx);
  385. // Check again
  386. auto it = m_pplines.find(hash);
  387. if(it != m_pplines.getEnd())
  388. {
  389. ppline.m_handle = (*it).m_handle;
  390. return;
  391. }
  392. // Create it for real
  393. PipelineInternal pp;
  394. const VkGraphicsPipelineCreateInfo& ci = state.updatePipelineCreateInfo();
  395. {
  396. ANKI_TRACE_SCOPED_EVENT(VK_PIPELINE_CREATE);
  397. #if ANKI_PLATFORM_MOBILE
  398. if(m_globalCreatePipelineMtx)
  399. {
  400. m_globalCreatePipelineMtx->lock();
  401. }
  402. #endif
  403. ANKI_VK_CHECKF(vkCreateGraphicsPipelines(m_dev, m_pplineCache, 1, &ci, nullptr, &pp.m_handle));
  404. #if ANKI_PLATFORM_MOBILE
  405. if(m_globalCreatePipelineMtx)
  406. {
  407. m_globalCreatePipelineMtx->unlock();
  408. }
  409. #endif
  410. }
  411. ANKI_TRACE_INC_COUNTER(VK_PIPELINES_CACHE_MISS, 1);
  412. m_pplines.emplace(m_alloc, hash, pp);
  413. ppline.m_handle = pp.m_handle;
  414. // Print shader info
  415. state.m_state.m_prog->getGrManagerImpl().printPipelineShaderInfo(pp.m_handle, state.m_state.m_prog->getName(),
  416. state.m_state.m_prog->getStages(), hash);
  417. }
  418. } // end namespace anki