Pipeline.cpp 14 KB

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