Pipeline.h 15 KB

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