Pipeline.h 15 KB

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