Pipeline.cpp 14 KB

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