Pipeline.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/Vulkan/DescriptorSet.h>
  7. #include <AnKi/Gr/ShaderProgram.h>
  8. #include <AnKi/Gr/Vulkan/ShaderProgramImpl.h>
  9. #include <AnKi/Gr/Framebuffer.h>
  10. #include <AnKi/Gr/Vulkan/FramebufferImpl.h>
  11. #include <AnKi/Util/HashMap.h>
  12. namespace anki {
  13. /// @addtogroup vulkan
  14. /// @{
  15. class VertexBufferBindingPipelineState
  16. {
  17. public:
  18. U32 m_stride = kMaxU32; ///< Vertex stride.
  19. VertexStepRate m_stepRate = VertexStepRate::kVertex;
  20. Array<U8, 3> m_padding = {};
  21. Bool operator==(const VertexBufferBindingPipelineState& b) const
  22. {
  23. return m_stride == b.m_stride && m_stepRate == b.m_stepRate;
  24. }
  25. Bool operator!=(const VertexBufferBindingPipelineState& b) const
  26. {
  27. return !(*this == b);
  28. }
  29. };
  30. static_assert(sizeof(VertexBufferBindingPipelineState) == 2 * sizeof(U32), "Packed because it will be hashed");
  31. class VertexAttributeBindingPipelineState
  32. {
  33. public:
  34. PtrSize m_offset = 0;
  35. Format m_format = Format::kNone;
  36. U8 m_binding = 0;
  37. Array<U8, 3> m_padding = {};
  38. Bool operator==(const VertexAttributeBindingPipelineState& b) const
  39. {
  40. return m_format == b.m_format && m_offset == b.m_offset && m_binding == b.m_binding;
  41. }
  42. Bool operator!=(const VertexAttributeBindingPipelineState& b) const
  43. {
  44. return !(*this == b);
  45. }
  46. };
  47. static_assert(sizeof(VertexAttributeBindingPipelineState) == 2 * sizeof(PtrSize), "Packed because it will be hashed");
  48. class VertexPipelineState
  49. {
  50. public:
  51. Array<VertexBufferBindingPipelineState, kMaxVertexAttributes> m_bindings;
  52. Array<VertexAttributeBindingPipelineState, kMaxVertexAttributes> m_attributes;
  53. };
  54. static_assert(sizeof(VertexPipelineState)
  55. == sizeof(VertexBufferBindingPipelineState) * kMaxVertexAttributes
  56. + sizeof(VertexAttributeBindingPipelineState) * kMaxVertexAttributes,
  57. "Packed because it will be hashed");
  58. class InputAssemblerPipelineState
  59. {
  60. public:
  61. PrimitiveTopology m_topology = PrimitiveTopology::kTriangles;
  62. Bool m_primitiveRestartEnabled = false;
  63. };
  64. static_assert(sizeof(InputAssemblerPipelineState) == sizeof(U8) * 2, "Packed because it will be hashed");
  65. class RasterizerPipelineState
  66. {
  67. public:
  68. FillMode m_fillMode = FillMode::kSolid;
  69. FaceSelectionBit m_cullMode = FaceSelectionBit::kBack;
  70. RasterizationOrder m_rasterizationOrder = RasterizationOrder::kOrdered;
  71. Bool m_depthBiasEnabled = false;
  72. };
  73. static_assert(sizeof(RasterizerPipelineState) == sizeof(U32), "Packed because it will be hashed");
  74. class DepthPipelineState
  75. {
  76. public:
  77. Bool m_depthWriteEnabled = true;
  78. CompareOperation m_depthCompareFunction = CompareOperation::kLess;
  79. };
  80. static_assert(sizeof(DepthPipelineState) == sizeof(U8) * 2, "Packed because it will be hashed");
  81. class StencilPipelineState
  82. {
  83. public:
  84. class S
  85. {
  86. public:
  87. StencilOperation m_stencilFailOperation = StencilOperation::kKeep;
  88. StencilOperation m_stencilPassDepthFailOperation = StencilOperation::kKeep;
  89. StencilOperation m_stencilPassDepthPassOperation = StencilOperation::kKeep;
  90. CompareOperation m_compareFunction = CompareOperation::kAlways;
  91. };
  92. Array<S, 2> m_face;
  93. };
  94. static_assert(sizeof(StencilPipelineState) == sizeof(U32) * 2, "Packed because it will be hashed");
  95. class ColorAttachmentState
  96. {
  97. public:
  98. BlendFactor m_srcBlendFactorRgb = BlendFactor::kOne;
  99. BlendFactor m_srcBlendFactorA = BlendFactor::kOne;
  100. BlendFactor m_dstBlendFactorRgb = BlendFactor::kZero;
  101. BlendFactor m_dstBlendFactorA = BlendFactor::kZero;
  102. BlendOperation m_blendFunctionRgb = BlendOperation::kAdd;
  103. BlendOperation m_blendFunctionA = BlendOperation::kAdd;
  104. ColorBit m_channelWriteMask = ColorBit::kAll;
  105. };
  106. static_assert(sizeof(ColorAttachmentState) == sizeof(U8) * 7, "Packed because it will be hashed");
  107. class ColorPipelineState
  108. {
  109. public:
  110. Bool m_alphaToCoverageEnabled = false;
  111. Array<ColorAttachmentState, kMaxColorRenderTargets> m_attachments;
  112. };
  113. static_assert(sizeof(ColorPipelineState) == sizeof(ColorAttachmentState) * kMaxColorRenderTargets + sizeof(U8), "Packed because it will be hashed");
  114. class AllPipelineState
  115. {
  116. public:
  117. const ShaderProgramImpl* m_prog = nullptr;
  118. VkRenderPass m_rpass = VK_NULL_HANDLE;
  119. VertexPipelineState m_vertex;
  120. InputAssemblerPipelineState m_inputAssembler;
  121. RasterizerPipelineState m_rasterizer;
  122. DepthPipelineState m_depth;
  123. StencilPipelineState m_stencil;
  124. ColorPipelineState m_color;
  125. void reset()
  126. {
  127. ::new(this) AllPipelineState();
  128. }
  129. };
  130. /// Track changes in the static state.
  131. class PipelineStateTracker
  132. {
  133. friend class PipelineFactory;
  134. public:
  135. PipelineStateTracker()
  136. {
  137. }
  138. PipelineStateTracker(const PipelineStateTracker&) = delete; // Non-copyable
  139. PipelineStateTracker& operator=(const PipelineStateTracker&) = delete; // Non-copyable
  140. void bindVertexBuffer(U32 binding, PtrSize stride, VertexStepRate stepRate)
  141. {
  142. VertexBufferBindingPipelineState b;
  143. b.m_stride = U32(stride);
  144. b.m_stepRate = stepRate;
  145. if(m_state.m_vertex.m_bindings[binding] != b)
  146. {
  147. m_state.m_vertex.m_bindings[binding] = b;
  148. m_dirty.m_vertBindings.set(binding);
  149. }
  150. m_set.m_vertBindings.set(binding);
  151. }
  152. void setVertexAttribute(U32 location, U32 buffBinding, const Format fmt, PtrSize relativeOffset)
  153. {
  154. VertexAttributeBindingPipelineState b;
  155. b.m_binding = U8(buffBinding);
  156. b.m_format = fmt;
  157. b.m_offset = relativeOffset;
  158. if(m_state.m_vertex.m_attributes[location] != b)
  159. {
  160. m_state.m_vertex.m_attributes[location] = b;
  161. m_dirty.m_attribs.set(location);
  162. }
  163. m_set.m_attribs.set(location);
  164. }
  165. void setPrimitiveRestart(Bool enable)
  166. {
  167. if(m_state.m_inputAssembler.m_primitiveRestartEnabled != enable)
  168. {
  169. m_state.m_inputAssembler.m_primitiveRestartEnabled = enable;
  170. m_dirty.m_inputAssembler = true;
  171. }
  172. }
  173. void setFillMode(FillMode mode)
  174. {
  175. if(m_state.m_rasterizer.m_fillMode != mode)
  176. {
  177. m_state.m_rasterizer.m_fillMode = mode;
  178. m_dirty.m_rasterizer = true;
  179. }
  180. }
  181. void setCullMode(FaceSelectionBit mode)
  182. {
  183. if(m_state.m_rasterizer.m_cullMode != mode)
  184. {
  185. m_state.m_rasterizer.m_cullMode = mode;
  186. m_dirty.m_rasterizer = true;
  187. }
  188. }
  189. void setPolygonOffset(F32 factor, F32 units)
  190. {
  191. const Bool depthBiasEnabled = factor != 0.0f || units != 0.0f;
  192. if(depthBiasEnabled != m_state.m_rasterizer.m_depthBiasEnabled)
  193. {
  194. m_state.m_rasterizer.m_depthBiasEnabled = depthBiasEnabled;
  195. m_dirty.m_rasterizer = true;
  196. }
  197. }
  198. void setRasterizationOrder(RasterizationOrder order)
  199. {
  200. if(m_state.m_rasterizer.m_rasterizationOrder != order)
  201. {
  202. m_state.m_rasterizer.m_rasterizationOrder = order;
  203. m_dirty.m_rasterizer = true;
  204. }
  205. }
  206. void setStencilOperations(FaceSelectionBit face, StencilOperation stencilFail, StencilOperation stencilPassDepthFail,
  207. StencilOperation stencilPassDepthPass)
  208. {
  209. if(!!(face & FaceSelectionBit::kFront)
  210. && (m_state.m_stencil.m_face[0].m_stencilFailOperation != stencilFail
  211. || m_state.m_stencil.m_face[0].m_stencilPassDepthFailOperation != stencilPassDepthFail
  212. || m_state.m_stencil.m_face[0].m_stencilPassDepthPassOperation != stencilPassDepthPass))
  213. {
  214. m_state.m_stencil.m_face[0].m_stencilFailOperation = stencilFail;
  215. m_state.m_stencil.m_face[0].m_stencilPassDepthFailOperation = stencilPassDepthFail;
  216. m_state.m_stencil.m_face[0].m_stencilPassDepthPassOperation = stencilPassDepthPass;
  217. m_dirty.m_stencil = true;
  218. }
  219. if(!!(face & FaceSelectionBit::kBack)
  220. && (m_state.m_stencil.m_face[1].m_stencilFailOperation != stencilFail
  221. || m_state.m_stencil.m_face[1].m_stencilPassDepthFailOperation != stencilPassDepthFail
  222. || m_state.m_stencil.m_face[1].m_stencilPassDepthPassOperation != stencilPassDepthPass))
  223. {
  224. m_state.m_stencil.m_face[1].m_stencilFailOperation = stencilFail;
  225. m_state.m_stencil.m_face[1].m_stencilPassDepthFailOperation = stencilPassDepthFail;
  226. m_state.m_stencil.m_face[1].m_stencilPassDepthPassOperation = stencilPassDepthPass;
  227. m_dirty.m_stencil = true;
  228. }
  229. }
  230. void setStencilCompareOperation(FaceSelectionBit face, CompareOperation comp)
  231. {
  232. if(!!(face & FaceSelectionBit::kFront) && m_state.m_stencil.m_face[0].m_compareFunction != comp)
  233. {
  234. m_state.m_stencil.m_face[0].m_compareFunction = comp;
  235. m_dirty.m_stencil = true;
  236. }
  237. if(!!(face & FaceSelectionBit::kBack) && m_state.m_stencil.m_face[1].m_compareFunction != comp)
  238. {
  239. m_state.m_stencil.m_face[1].m_compareFunction = comp;
  240. m_dirty.m_stencil = true;
  241. }
  242. }
  243. void setDepthWrite(Bool enable)
  244. {
  245. if(m_state.m_depth.m_depthWriteEnabled != enable)
  246. {
  247. m_state.m_depth.m_depthWriteEnabled = enable;
  248. m_dirty.m_depth = true;
  249. }
  250. }
  251. void setDepthCompareOperation(CompareOperation op)
  252. {
  253. if(m_state.m_depth.m_depthCompareFunction != op)
  254. {
  255. m_state.m_depth.m_depthCompareFunction = op;
  256. m_dirty.m_depth = true;
  257. }
  258. }
  259. void setAlphaToCoverage(Bool enable)
  260. {
  261. if(m_state.m_color.m_alphaToCoverageEnabled != enable)
  262. {
  263. m_state.m_color.m_alphaToCoverageEnabled = enable;
  264. m_dirty.m_color = true;
  265. }
  266. }
  267. void setColorChannelWriteMask(U32 attachment, ColorBit mask)
  268. {
  269. if(m_state.m_color.m_attachments[attachment].m_channelWriteMask != mask)
  270. {
  271. m_state.m_color.m_attachments[attachment].m_channelWriteMask = mask;
  272. m_dirty.m_colAttachments.set(attachment);
  273. }
  274. }
  275. void setBlendFactors(U32 attachment, BlendFactor srcRgb, BlendFactor dstRgb, BlendFactor srcA, BlendFactor dstA)
  276. {
  277. ColorAttachmentState& c = m_state.m_color.m_attachments[attachment];
  278. if(c.m_srcBlendFactorRgb != srcRgb || c.m_dstBlendFactorRgb != dstRgb || c.m_srcBlendFactorA != srcA || c.m_dstBlendFactorA != dstA)
  279. {
  280. c.m_srcBlendFactorRgb = srcRgb;
  281. c.m_dstBlendFactorRgb = dstRgb;
  282. c.m_srcBlendFactorA = srcA;
  283. c.m_dstBlendFactorA = dstA;
  284. m_dirty.m_colAttachments.set(attachment);
  285. }
  286. }
  287. void setBlendOperation(U32 attachment, BlendOperation funcRgb, BlendOperation funcA)
  288. {
  289. ColorAttachmentState& c = m_state.m_color.m_attachments[attachment];
  290. if(c.m_blendFunctionRgb != funcRgb || c.m_blendFunctionA != funcA)
  291. {
  292. c.m_blendFunctionRgb = funcRgb;
  293. c.m_blendFunctionA = funcA;
  294. m_dirty.m_colAttachments.set(attachment);
  295. }
  296. }
  297. void bindShaderProgram(const ShaderProgramImpl* prog)
  298. {
  299. if(prog != m_state.m_prog)
  300. {
  301. m_shaderColorAttachmentWritemask = prog->getReflectionInfo().m_colorAttachmentWritemask;
  302. m_shaderAttributeMask = prog->getReflectionInfo().m_attributeMask;
  303. m_state.m_prog = prog;
  304. m_dirty.m_prog = true;
  305. }
  306. }
  307. void beginRenderPass(const FramebufferImpl* fb)
  308. {
  309. ANKI_ASSERT(m_state.m_rpass == VK_NULL_HANDLE);
  310. Bool d, s;
  311. fb->getAttachmentInfo(m_fbColorAttachmentMask, d, s);
  312. m_fbDepth = d;
  313. m_fbStencil = s;
  314. m_defaultFb = fb->hasPresentableTexture();
  315. m_state.m_rpass = fb->getCompatibleRenderPass();
  316. m_dirty.m_rpass = true;
  317. }
  318. void endRenderPass()
  319. {
  320. ANKI_ASSERT(m_state.m_rpass);
  321. m_state.m_rpass = VK_NULL_HANDLE;
  322. }
  323. void setPrimitiveTopology(PrimitiveTopology topology)
  324. {
  325. if(m_state.m_inputAssembler.m_topology != topology)
  326. {
  327. m_state.m_inputAssembler.m_topology = topology;
  328. m_dirty.m_inputAssembler = true;
  329. }
  330. }
  331. PrimitiveTopology getPrimitiveTopology() const
  332. {
  333. return m_state.m_inputAssembler.m_topology;
  334. }
  335. Bool getEnablePipelineStatistics() const
  336. {
  337. return m_pipelineStatisticsEnabled;
  338. }
  339. void setEnablePipelineStatistics(Bool enable)
  340. {
  341. m_pipelineStatisticsEnabled = enable;
  342. }
  343. void setVrsCapable(Bool capable)
  344. {
  345. m_vrsCapable = capable;
  346. }
  347. /// Flush state
  348. void flush(U64& pipelineHash, Bool& stateDirty)
  349. {
  350. const Bool dirtyHashes = updateHashes();
  351. if(dirtyHashes)
  352. {
  353. updateSuperHash();
  354. }
  355. if(m_hashes.m_superHash != m_hashes.m_lastSuperHash)
  356. {
  357. m_hashes.m_lastSuperHash = m_hashes.m_superHash;
  358. stateDirty = true;
  359. }
  360. else
  361. {
  362. stateDirty = false;
  363. }
  364. pipelineHash = m_hashes.m_superHash;
  365. ANKI_ASSERT(pipelineHash);
  366. }
  367. /// Populate the internal pipeline create info structure.
  368. const VkGraphicsPipelineCreateInfo& updatePipelineCreateInfo();
  369. void reset();
  370. private:
  371. AllPipelineState m_state;
  372. class DirtyBits
  373. {
  374. public:
  375. Bool m_prog : 1 = true;
  376. Bool m_rpass : 1 = true;
  377. Bool m_inputAssembler : 1 = true;
  378. Bool m_rasterizer : 1 = true;
  379. Bool m_depth : 1 = true;
  380. Bool m_stencil : 1 = true;
  381. Bool m_color : 1 = true;
  382. // Vertex
  383. BitSet<kMaxVertexAttributes, U8> m_attribs = {true};
  384. BitSet<kMaxVertexAttributes, U8> m_vertBindings = {true};
  385. BitSet<kMaxColorRenderTargets, U8> m_colAttachments = {true};
  386. } m_dirty;
  387. class SetBits
  388. {
  389. public:
  390. BitSet<kMaxVertexAttributes, U8> m_attribs = {false};
  391. BitSet<kMaxVertexAttributes, U8> m_vertBindings = {false};
  392. } m_set;
  393. // Shader info
  394. BitSet<kMaxVertexAttributes, U8> m_shaderAttributeMask = {false};
  395. BitSet<kMaxColorRenderTargets, U8> m_shaderColorAttachmentWritemask = {false};
  396. // Renderpass info
  397. Bool m_fbDepth : 1 = false;
  398. Bool m_fbStencil : 1 = false;
  399. Bool m_defaultFb : 1 = false;
  400. BitSet<kMaxColorRenderTargets, U8> m_fbColorAttachmentMask = {false};
  401. class Hashes
  402. {
  403. public:
  404. U64 m_prog;
  405. U64 m_rpass;
  406. Array<U64, kMaxVertexAttributes> m_vertexAttribs;
  407. U64 m_ia;
  408. U64 m_raster;
  409. U64 m_depth;
  410. U64 m_stencil;
  411. U64 m_color;
  412. Array<U64, kMaxColorRenderTargets> m_colAttachments;
  413. U64 m_superHash;
  414. U64 m_lastSuperHash;
  415. Hashes()
  416. {
  417. zeroMemory(*this);
  418. }
  419. } m_hashes;
  420. // Create info
  421. class CreateInfo
  422. {
  423. public:
  424. Array<VkVertexInputBindingDescription, kMaxVertexAttributes> m_vertBindings;
  425. Array<VkVertexInputAttributeDescription, kMaxVertexAttributes> m_attribs;
  426. VkPipelineVertexInputStateCreateInfo m_vert;
  427. VkPipelineInputAssemblyStateCreateInfo m_ia;
  428. VkPipelineViewportStateCreateInfo m_vp;
  429. VkPipelineTessellationStateCreateInfo m_tess;
  430. VkPipelineRasterizationStateCreateInfo m_rast;
  431. VkPipelineMultisampleStateCreateInfo m_ms;
  432. VkPipelineDepthStencilStateCreateInfo m_ds;
  433. Array<VkPipelineColorBlendAttachmentState, kMaxColorRenderTargets> m_colAttachments;
  434. VkPipelineColorBlendStateCreateInfo m_color;
  435. VkPipelineDynamicStateCreateInfo m_dyn;
  436. VkGraphicsPipelineCreateInfo m_ppline;
  437. VkPipelineRasterizationStateRasterizationOrderAMD m_rasterOrder;
  438. } m_ci;
  439. Bool m_pipelineStatisticsEnabled : 1 = false;
  440. Bool m_vrsCapable : 1 = false;
  441. Bool updateHashes();
  442. void updateSuperHash();
  443. };
  444. /// Small wrapper on top of the pipeline.
  445. class Pipeline
  446. {
  447. friend class PipelineFactory;
  448. public:
  449. VkPipeline getHandle() const
  450. {
  451. ANKI_ASSERT(m_handle);
  452. return m_handle;
  453. }
  454. private:
  455. VkPipeline m_handle ANKI_DEBUG_CODE(= 0);
  456. };
  457. /// Given some state it creates/hashes pipelines.
  458. class PipelineFactory
  459. {
  460. public:
  461. PipelineFactory();
  462. ~PipelineFactory();
  463. void init(VkPipelineCache pplineCache
  464. #if ANKI_PLATFORM_MOBILE
  465. ,
  466. Mutex* globalCreatePipelineMtx
  467. #endif
  468. )
  469. {
  470. m_pplineCache = pplineCache;
  471. #if ANKI_PLATFORM_MOBILE
  472. m_globalCreatePipelineMtx = globalCreatePipelineMtx;
  473. #endif
  474. }
  475. void destroy();
  476. /// @note Thread-safe.
  477. void getOrCreatePipeline(PipelineStateTracker& state, Pipeline& ppline, Bool& stateDirty);
  478. private:
  479. class PipelineInternal;
  480. class Hasher;
  481. VkPipelineCache m_pplineCache = VK_NULL_HANDLE;
  482. GrHashMap<U64, PipelineInternal, Hasher> m_pplines;
  483. RWMutex m_pplinesMtx;
  484. #if ANKI_PLATFORM_MOBILE
  485. Mutex* m_globalCreatePipelineMtx = nullptr;
  486. #endif
  487. };
  488. /// @}
  489. } // end namespace anki