Pipeline.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. 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. // Prog
  182. ci.pStages = static_cast<const ShaderProgramImpl&>(*m_state.m_prog).getShaderCreateInfos(ci.stageCount);
  183. // Vert
  184. VkPipelineVertexInputStateCreateInfo& vertCi = m_ci.m_vert;
  185. vertCi = {};
  186. vertCi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  187. vertCi.pVertexAttributeDescriptions = &m_ci.m_attribs[0];
  188. vertCi.pVertexBindingDescriptions = &m_ci.m_vertBindings[0];
  189. BitSet<MAX_VERTEX_ATTRIBUTES, U8> bindingSet = {false};
  190. for(U32 i = 0; i < MAX_VERTEX_ATTRIBUTES; ++i)
  191. {
  192. if(m_shaderAttributeMask.get(i))
  193. {
  194. VkVertexInputAttributeDescription& attrib = m_ci.m_attribs[vertCi.vertexAttributeDescriptionCount++];
  195. attrib.binding = m_state.m_vertex.m_attributes[i].m_binding;
  196. attrib.format = convertFormat(m_state.m_vertex.m_attributes[i].m_format);
  197. attrib.location = i;
  198. attrib.offset = U32(m_state.m_vertex.m_attributes[i].m_offset);
  199. if(!bindingSet.get(attrib.binding))
  200. {
  201. bindingSet.set(attrib.binding);
  202. VkVertexInputBindingDescription& binding = m_ci.m_vertBindings[vertCi.vertexBindingDescriptionCount++];
  203. binding.binding = attrib.binding;
  204. binding.inputRate = convertVertexStepRate(m_state.m_vertex.m_bindings[attrib.binding].m_stepRate);
  205. binding.stride = m_state.m_vertex.m_bindings[attrib.binding].m_stride;
  206. }
  207. }
  208. }
  209. ci.pVertexInputState = &vertCi;
  210. // IA
  211. VkPipelineInputAssemblyStateCreateInfo& iaCi = m_ci.m_ia;
  212. iaCi = {};
  213. iaCi.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  214. iaCi.primitiveRestartEnable = m_state.m_inputAssembler.m_primitiveRestartEnabled;
  215. iaCi.topology = convertTopology(m_state.m_inputAssembler.m_topology);
  216. ci.pInputAssemblyState = &iaCi;
  217. // Viewport
  218. VkPipelineViewportStateCreateInfo& vpCi = m_ci.m_vp;
  219. vpCi = {};
  220. vpCi.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  221. vpCi.scissorCount = 1;
  222. vpCi.viewportCount = 1;
  223. ci.pViewportState = &vpCi;
  224. // Raster
  225. VkPipelineRasterizationStateCreateInfo& rastCi = m_ci.m_rast;
  226. rastCi = {};
  227. rastCi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  228. rastCi.depthClampEnable = false;
  229. rastCi.rasterizerDiscardEnable = false;
  230. rastCi.polygonMode = convertFillMode(m_state.m_rasterizer.m_fillMode);
  231. rastCi.cullMode = convertCullMode(m_state.m_rasterizer.m_cullMode);
  232. rastCi.frontFace = (!m_defaultFb) ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE; // For viewport flip
  233. rastCi.depthBiasEnable =
  234. m_state.m_rasterizer.m_depthBiasConstantFactor != 0.0 && m_state.m_rasterizer.m_depthBiasSlopeFactor != 0.0;
  235. rastCi.depthBiasConstantFactor = m_state.m_rasterizer.m_depthBiasConstantFactor;
  236. rastCi.depthBiasClamp = 0.0;
  237. rastCi.depthBiasSlopeFactor = m_state.m_rasterizer.m_depthBiasSlopeFactor;
  238. rastCi.lineWidth = 1.0;
  239. ci.pRasterizationState = &rastCi;
  240. if(m_state.m_rasterizer.m_rasterizationOrder != RasterizationOrder::ORDERED)
  241. {
  242. VkPipelineRasterizationStateRasterizationOrderAMD& rastOrderCi = m_ci.m_rasterOrder;
  243. rastOrderCi = {};
  244. rastOrderCi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_RASTERIZATION_ORDER_AMD;
  245. rastOrderCi.rasterizationOrder = convertRasterizationOrder(m_state.m_rasterizer.m_rasterizationOrder);
  246. ANKI_ASSERT(rastCi.pNext == nullptr);
  247. rastCi.pNext = &rastOrderCi;
  248. }
  249. // MS
  250. VkPipelineMultisampleStateCreateInfo& msCi = m_ci.m_ms;
  251. msCi = {};
  252. msCi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  253. msCi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  254. ci.pMultisampleState = &msCi;
  255. // DS
  256. if(m_fbDepth || m_fbStencil)
  257. {
  258. VkPipelineDepthStencilStateCreateInfo& dsCi = m_ci.m_ds;
  259. dsCi = {};
  260. dsCi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  261. if(m_fbDepth)
  262. {
  263. dsCi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  264. dsCi.depthTestEnable = m_state.m_depth.m_depthCompareFunction != CompareOperation::ALWAYS
  265. || m_state.m_depth.m_depthWriteEnabled;
  266. dsCi.depthWriteEnable = m_state.m_depth.m_depthWriteEnabled;
  267. dsCi.depthCompareOp = convertCompareOp(m_state.m_depth.m_depthCompareFunction);
  268. }
  269. if(m_fbStencil)
  270. {
  271. dsCi.stencilTestEnable =
  272. !stencilTestDisabled(m_state.m_stencil.m_face[0].m_stencilFailOperation,
  273. m_state.m_stencil.m_face[0].m_stencilPassDepthFailOperation,
  274. m_state.m_stencil.m_face[0].m_stencilPassDepthPassOperation,
  275. m_state.m_stencil.m_face[0].m_compareFunction)
  276. || !stencilTestDisabled(m_state.m_stencil.m_face[1].m_stencilFailOperation,
  277. m_state.m_stencil.m_face[1].m_stencilPassDepthFailOperation,
  278. m_state.m_stencil.m_face[1].m_stencilPassDepthPassOperation,
  279. m_state.m_stencil.m_face[1].m_compareFunction);
  280. dsCi.front.failOp = convertStencilOp(m_state.m_stencil.m_face[0].m_stencilFailOperation);
  281. dsCi.front.passOp = convertStencilOp(m_state.m_stencil.m_face[0].m_stencilPassDepthPassOperation);
  282. dsCi.front.depthFailOp = convertStencilOp(m_state.m_stencil.m_face[0].m_stencilPassDepthFailOperation);
  283. dsCi.front.compareOp = convertCompareOp(m_state.m_stencil.m_face[0].m_compareFunction);
  284. dsCi.back.failOp = convertStencilOp(m_state.m_stencil.m_face[1].m_stencilFailOperation);
  285. dsCi.back.passOp = convertStencilOp(m_state.m_stencil.m_face[1].m_stencilPassDepthPassOperation);
  286. dsCi.back.depthFailOp = convertStencilOp(m_state.m_stencil.m_face[1].m_stencilPassDepthFailOperation);
  287. dsCi.back.compareOp = convertCompareOp(m_state.m_stencil.m_face[1].m_compareFunction);
  288. }
  289. ci.pDepthStencilState = &dsCi;
  290. }
  291. // Color/blend
  292. if(!!m_shaderColorAttachmentWritemask)
  293. {
  294. VkPipelineColorBlendStateCreateInfo& colCi = m_ci.m_color;
  295. colCi = {};
  296. colCi.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  297. colCi.attachmentCount = m_shaderColorAttachmentWritemask.getEnabledBitCount();
  298. colCi.pAttachments = &m_ci.m_colAttachments[0];
  299. for(U i = 0; i < colCi.attachmentCount; ++i)
  300. {
  301. ANKI_ASSERT(m_shaderColorAttachmentWritemask.get(i) && "No gaps are allowed");
  302. VkPipelineColorBlendAttachmentState& out = m_ci.m_colAttachments[i];
  303. const ColorAttachmentState& in = m_state.m_color.m_attachments[i];
  304. out.blendEnable = !blendingDisabled(in.m_srcBlendFactorRgb, in.m_dstBlendFactorRgb, in.m_srcBlendFactorA,
  305. in.m_dstBlendFactorA, in.m_blendFunctionRgb, in.m_blendFunctionA);
  306. out.srcColorBlendFactor = convertBlendFactor(in.m_srcBlendFactorRgb);
  307. out.dstColorBlendFactor = convertBlendFactor(in.m_dstBlendFactorRgb);
  308. out.srcAlphaBlendFactor = convertBlendFactor(in.m_srcBlendFactorA);
  309. out.dstAlphaBlendFactor = convertBlendFactor(in.m_dstBlendFactorA);
  310. out.colorBlendOp = convertBlendOperation(in.m_blendFunctionRgb);
  311. out.alphaBlendOp = convertBlendOperation(in.m_blendFunctionA);
  312. out.colorWriteMask = convertColorWriteMask(in.m_channelWriteMask);
  313. }
  314. ci.pColorBlendState = &colCi;
  315. }
  316. // Dyn state
  317. VkPipelineDynamicStateCreateInfo& dynCi = m_ci.m_dyn;
  318. dynCi = {};
  319. dynCi.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  320. // Almost all state is dynamic. Depth bias is static
  321. static const Array<VkDynamicState, 8> DYN = {
  322. {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_BLEND_CONSTANTS,
  323. VK_DYNAMIC_STATE_DEPTH_BOUNDS, VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK, VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
  324. VK_DYNAMIC_STATE_STENCIL_REFERENCE, VK_DYNAMIC_STATE_LINE_WIDTH}};
  325. dynCi.dynamicStateCount = DYN.getSize();
  326. dynCi.pDynamicStates = &DYN[0];
  327. ci.pDynamicState = &dynCi;
  328. // The rest
  329. ci.layout = static_cast<const ShaderProgramImpl&>(*m_state.m_prog).getPipelineLayout().getHandle();
  330. ci.renderPass = m_state.m_rpass;
  331. ci.subpass = 0;
  332. return ci;
  333. }
  334. class PipelineFactory::PipelineInternal
  335. {
  336. public:
  337. VkPipeline m_handle = VK_NULL_HANDLE;
  338. };
  339. class PipelineFactory::Hasher
  340. {
  341. public:
  342. U64 operator()(U64 h)
  343. {
  344. return h;
  345. }
  346. };
  347. void PipelineFactory::destroy()
  348. {
  349. for(auto it : m_pplines)
  350. {
  351. if(it.m_handle)
  352. {
  353. vkDestroyPipeline(m_dev, it.m_handle, nullptr);
  354. }
  355. }
  356. m_pplines.destroy(m_alloc);
  357. }
  358. void PipelineFactory::getOrCreatePipeline(PipelineStateTracker& state, Pipeline& ppline, Bool& stateDirty)
  359. {
  360. ANKI_TRACE_SCOPED_EVENT(VK_PIPELINE_GET_OR_CREATE);
  361. U64 hash;
  362. state.flush(hash, stateDirty);
  363. if(ANKI_UNLIKELY(!stateDirty))
  364. {
  365. ppline.m_handle = VK_NULL_HANDLE;
  366. return;
  367. }
  368. // Check if ppline exists
  369. {
  370. RLockGuard<RWMutex> lock(m_pplinesMtx);
  371. auto it = m_pplines.find(hash);
  372. if(it != m_pplines.getEnd())
  373. {
  374. ppline.m_handle = (*it).m_handle;
  375. ANKI_TRACE_INC_COUNTER(VK_PIPELINES_CACHE_HIT, 1);
  376. return;
  377. }
  378. }
  379. // Doesnt exist. Need to create it
  380. WLockGuard<RWMutex> lock(m_pplinesMtx);
  381. // Check again
  382. auto it = m_pplines.find(hash);
  383. if(it != m_pplines.getEnd())
  384. {
  385. ppline.m_handle = (*it).m_handle;
  386. return;
  387. }
  388. // Create it for real
  389. PipelineInternal pp;
  390. const VkGraphicsPipelineCreateInfo& ci = state.updatePipelineCreateInfo();
  391. {
  392. ANKI_TRACE_SCOPED_EVENT(VK_PIPELINE_CREATE);
  393. ANKI_VK_CHECKF(vkCreateGraphicsPipelines(m_dev, m_pplineCache, 1, &ci, nullptr, &pp.m_handle));
  394. }
  395. ANKI_TRACE_INC_COUNTER(VK_PIPELINES_CACHE_MISS, 1);
  396. m_pplines.emplace(m_alloc, hash, pp);
  397. ppline.m_handle = pp.m_handle;
  398. // Print shader info
  399. state.m_state.m_prog->getGrManagerImpl().printPipelineShaderInfo(pp.m_handle, state.m_state.m_prog->getName(),
  400. state.m_state.m_prog->getStages(), hash);
  401. }
  402. } // end namespace anki