GraphicsPipeline.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.GAL.Blend;
  4. using Ryujinx.Graphics.GAL.Color;
  5. using Ryujinx.Graphics.GAL.DepthStencil;
  6. using Ryujinx.Graphics.GAL.InputAssembler;
  7. using Ryujinx.Graphics.Shader;
  8. using System;
  9. namespace Ryujinx.Graphics.OpenGL
  10. {
  11. class GraphicsPipeline : IGraphicsPipeline
  12. {
  13. private Program _program;
  14. private VertexArray _vertexArray;
  15. private Framebuffer _framebuffer;
  16. private IntPtr _indexBaseOffset;
  17. private DrawElementsType _elementsType;
  18. private PrimitiveType _primitiveType;
  19. private int _stencilFrontMask;
  20. private bool _depthMask;
  21. private bool _depthTest;
  22. private bool _hasDepthBuffer;
  23. private TextureView _unit0Texture;
  24. private ClipOrigin _clipOrigin;
  25. private uint[] _componentMasks;
  26. internal GraphicsPipeline()
  27. {
  28. _clipOrigin = ClipOrigin.LowerLeft;
  29. }
  30. public void BindBlendState(int index, BlendDescriptor blend)
  31. {
  32. if (!blend.Enable)
  33. {
  34. GL.Disable(IndexedEnableCap.Blend, index);
  35. return;
  36. }
  37. GL.BlendEquationSeparate(
  38. index,
  39. blend.ColorOp.Convert(),
  40. blend.AlphaOp.Convert());
  41. GL.BlendFuncSeparate(
  42. index,
  43. (BlendingFactorSrc) blend.ColorSrcFactor.Convert(),
  44. (BlendingFactorDest)blend.ColorDstFactor.Convert(),
  45. (BlendingFactorSrc) blend.AlphaSrcFactor.Convert(),
  46. (BlendingFactorDest)blend.AlphaDstFactor.Convert());
  47. GL.Enable(IndexedEnableCap.Blend, index);
  48. }
  49. public void BindIndexBuffer(BufferRange buffer, IndexType type)
  50. {
  51. _elementsType = type.Convert();
  52. _indexBaseOffset = (IntPtr)buffer.Offset;
  53. EnsureVertexArray();
  54. _vertexArray.SetIndexBuffer((Buffer)buffer.Buffer);
  55. }
  56. public void BindProgram(IProgram program)
  57. {
  58. _program = (Program)program;
  59. _program.Bind();
  60. }
  61. public void BindSampler(int index, ShaderStage stage, ISampler sampler)
  62. {
  63. int unit = _program.GetTextureUnit(stage, index);
  64. if (unit != -1 && sampler != null)
  65. {
  66. ((Sampler)sampler).Bind(unit);
  67. }
  68. }
  69. public void BindTexture(int index, ShaderStage stage, ITexture texture)
  70. {
  71. int unit = _program.GetTextureUnit(stage, index);
  72. if (unit != -1 && texture != null)
  73. {
  74. if (unit == 0)
  75. {
  76. _unit0Texture = ((TextureView)texture);
  77. }
  78. else
  79. {
  80. ((TextureView)texture).Bind(unit);
  81. }
  82. }
  83. }
  84. public void BindStorageBuffers(int index, ShaderStage stage, BufferRange[] buffers)
  85. {
  86. BindBuffers(index, stage, buffers, isStorage: true);
  87. }
  88. public void BindUniformBuffers(int index, ShaderStage stage, BufferRange[] buffers)
  89. {
  90. BindBuffers(index, stage, buffers, isStorage: false);
  91. }
  92. private void BindBuffers(int index, ShaderStage stage, BufferRange[] buffers, bool isStorage)
  93. {
  94. for (int bufferIndex = 0; bufferIndex < buffers.Length; bufferIndex++, index++)
  95. {
  96. int bindingPoint = isStorage
  97. ? _program.GetStorageBufferBindingPoint(stage, index)
  98. : _program.GetUniformBufferBindingPoint(stage, index);
  99. if (bindingPoint == -1)
  100. {
  101. continue;
  102. }
  103. BufferRange buffer = buffers[bufferIndex];
  104. BufferRangeTarget target = isStorage
  105. ? BufferRangeTarget.ShaderStorageBuffer
  106. : BufferRangeTarget.UniformBuffer;
  107. if (buffer.Buffer == null)
  108. {
  109. GL.BindBufferRange(target, bindingPoint, 0, IntPtr.Zero, 0);
  110. continue;
  111. }
  112. int bufferHandle = ((Buffer)buffer.Buffer).Handle;
  113. IntPtr bufferOffset = (IntPtr)buffer.Offset;
  114. GL.BindBufferRange(
  115. target,
  116. bindingPoint,
  117. bufferHandle,
  118. bufferOffset,
  119. buffer.Size);
  120. }
  121. }
  122. public void BindVertexAttribs(VertexAttribDescriptor[] vertexAttribs)
  123. {
  124. EnsureVertexArray();
  125. _vertexArray.SetVertexAttributes(vertexAttribs);
  126. }
  127. public void BindVertexBuffers(VertexBufferDescriptor[] vertexBuffers)
  128. {
  129. EnsureVertexArray();
  130. _vertexArray.SetVertexBuffers(vertexBuffers);
  131. }
  132. public void ClearRenderTargetColor(int index, uint componentMask, ColorF color)
  133. {
  134. GL.ColorMask(
  135. index,
  136. (componentMask & 1) != 0,
  137. (componentMask & 2) != 0,
  138. (componentMask & 4) != 0,
  139. (componentMask & 8) != 0);
  140. float[] colors = new float[] { color.Red, color.Green, color.Blue, color.Alpha };
  141. GL.ClearBuffer(ClearBuffer.Color, index, colors);
  142. RestoreComponentMask(index);
  143. }
  144. public void ClearRenderTargetColor(int index, uint componentMask, ColorSI color)
  145. {
  146. GL.ColorMask(
  147. index,
  148. (componentMask & 1u) != 0,
  149. (componentMask & 2u) != 0,
  150. (componentMask & 4u) != 0,
  151. (componentMask & 8u) != 0);
  152. int[] colors = new int[] { color.Red, color.Green, color.Blue, color.Alpha };
  153. GL.ClearBuffer(ClearBuffer.Color, index, colors);
  154. RestoreComponentMask(index);
  155. }
  156. public void ClearRenderTargetColor(int index, uint componentMask, ColorUI color)
  157. {
  158. GL.ColorMask(
  159. index,
  160. (componentMask & 1u) != 0,
  161. (componentMask & 2u) != 0,
  162. (componentMask & 4u) != 0,
  163. (componentMask & 8u) != 0);
  164. uint[] colors = new uint[] { color.Red, color.Green, color.Blue, color.Alpha };
  165. GL.ClearBuffer(ClearBuffer.Color, index, colors);
  166. RestoreComponentMask(index);
  167. }
  168. public void ClearRenderTargetDepthStencil(
  169. float depthValue,
  170. bool depthMask,
  171. int stencilValue,
  172. int stencilMask)
  173. {
  174. bool stencilMaskChanged =
  175. stencilMask != 0 &&
  176. stencilMask != _stencilFrontMask;
  177. bool depthMaskChanged = depthMask && depthMask != _depthMask;
  178. if (stencilMaskChanged)
  179. {
  180. GL.StencilMaskSeparate(StencilFace.Front, stencilMask);
  181. }
  182. if (depthMaskChanged)
  183. {
  184. GL.DepthMask(depthMask);
  185. }
  186. if (depthMask && stencilMask != 0)
  187. {
  188. GL.ClearBuffer(ClearBufferCombined.DepthStencil, 0, depthValue, stencilValue);
  189. }
  190. else if (depthMask)
  191. {
  192. GL.ClearBuffer(ClearBuffer.Depth, 0, ref depthValue);
  193. }
  194. else if (stencilMask != 0)
  195. {
  196. GL.ClearBuffer(ClearBuffer.Stencil, 0, ref stencilValue);
  197. }
  198. if (stencilMaskChanged)
  199. {
  200. GL.StencilMaskSeparate(StencilFace.Front, _stencilFrontMask);
  201. }
  202. if (depthMaskChanged)
  203. {
  204. GL.DepthMask(_depthMask);
  205. }
  206. }
  207. public void Draw(int vertexCount, int instanceCount, int firstVertex, int firstInstance)
  208. {
  209. if (!_program.IsLinked)
  210. {
  211. return;
  212. }
  213. PrepareForDraw();
  214. if (firstInstance == 0 && instanceCount == 1)
  215. {
  216. if (_primitiveType == PrimitiveType.Quads)
  217. {
  218. for (int offset = 0; offset < vertexCount; offset += 4)
  219. {
  220. GL.DrawArrays(PrimitiveType.TriangleFan, firstVertex + offset, 4);
  221. }
  222. }
  223. else if (_primitiveType == PrimitiveType.QuadStrip)
  224. {
  225. GL.DrawArrays(PrimitiveType.TriangleFan, firstVertex, 4);
  226. for (int offset = 2; offset < vertexCount; offset += 2)
  227. {
  228. GL.DrawArrays(PrimitiveType.TriangleFan, firstVertex + offset, 4);
  229. }
  230. }
  231. else
  232. {
  233. GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
  234. }
  235. // GL.DrawArrays(_primitiveType, firstVertex, vertexCount);
  236. }
  237. else if (firstInstance == 0)
  238. {
  239. GL.DrawArraysInstanced(_primitiveType, firstVertex, vertexCount, instanceCount);
  240. }
  241. else
  242. {
  243. GL.DrawArraysInstancedBaseInstance(
  244. _primitiveType,
  245. firstVertex,
  246. vertexCount,
  247. instanceCount,
  248. firstInstance);
  249. }
  250. }
  251. public void DrawIndexed(
  252. int indexCount,
  253. int instanceCount,
  254. int firstIndex,
  255. int firstVertex,
  256. int firstInstance)
  257. {
  258. if (!_program.IsLinked)
  259. {
  260. return;
  261. }
  262. PrepareForDraw();
  263. int firstIndexOffset = firstIndex;
  264. switch (_elementsType)
  265. {
  266. case DrawElementsType.UnsignedShort: firstIndexOffset *= 2; break;
  267. case DrawElementsType.UnsignedInt: firstIndexOffset *= 4; break;
  268. }
  269. IntPtr indexBaseOffset = _indexBaseOffset + firstIndexOffset;
  270. if (firstInstance == 0 && firstVertex == 0 && instanceCount == 1)
  271. {
  272. GL.DrawElements(_primitiveType, indexCount, _elementsType, indexBaseOffset);
  273. }
  274. else if (firstInstance == 0 && instanceCount == 1)
  275. {
  276. GL.DrawElementsBaseVertex(
  277. _primitiveType,
  278. indexCount,
  279. _elementsType,
  280. indexBaseOffset,
  281. firstVertex);
  282. }
  283. else if (firstInstance == 0 && firstVertex == 0)
  284. {
  285. GL.DrawElementsInstanced(
  286. _primitiveType,
  287. indexCount,
  288. _elementsType,
  289. indexBaseOffset,
  290. instanceCount);
  291. }
  292. else if (firstInstance == 0)
  293. {
  294. GL.DrawElementsInstancedBaseVertex(
  295. _primitiveType,
  296. indexCount,
  297. _elementsType,
  298. indexBaseOffset,
  299. instanceCount,
  300. firstVertex);
  301. }
  302. else if (firstVertex == 0)
  303. {
  304. GL.DrawElementsInstancedBaseInstance(
  305. _primitiveType,
  306. indexCount,
  307. _elementsType,
  308. indexBaseOffset,
  309. instanceCount,
  310. firstInstance);
  311. }
  312. else
  313. {
  314. GL.DrawElementsInstancedBaseVertexBaseInstance(
  315. _primitiveType,
  316. indexCount,
  317. _elementsType,
  318. indexBaseOffset,
  319. instanceCount,
  320. firstVertex,
  321. firstInstance);
  322. }
  323. }
  324. public void DrawIndirect(BufferRange buffer, ulong offset, int drawCount, int stride)
  325. {
  326. }
  327. public void DrawIndexedIndirect(BufferRange buffer, ulong offset, int drawCount, int stride)
  328. {
  329. }
  330. public void SetBlendColor(ColorF color)
  331. {
  332. GL.BlendColor(color.Red, color.Green, color.Blue, color.Alpha);
  333. }
  334. public void SetDepthBias(PolygonModeMask enables, float factor, float units, float clamp)
  335. {
  336. if ((enables & PolygonModeMask.Point) != 0)
  337. {
  338. GL.Enable(EnableCap.PolygonOffsetPoint);
  339. }
  340. else
  341. {
  342. GL.Disable(EnableCap.PolygonOffsetPoint);
  343. }
  344. if ((enables & PolygonModeMask.Line) != 0)
  345. {
  346. GL.Enable(EnableCap.PolygonOffsetLine);
  347. }
  348. else
  349. {
  350. GL.Disable(EnableCap.PolygonOffsetLine);
  351. }
  352. if ((enables & PolygonModeMask.Fill) != 0)
  353. {
  354. GL.Enable(EnableCap.PolygonOffsetFill);
  355. }
  356. else
  357. {
  358. GL.Disable(EnableCap.PolygonOffsetFill);
  359. }
  360. if (enables == 0)
  361. {
  362. return;
  363. }
  364. GL.PolygonOffset(factor, units);
  365. // GL.PolygonOffsetClamp(factor, units, clamp);
  366. }
  367. public void SetDepthTest(DepthTestDescriptor depthTest)
  368. {
  369. GL.DepthFunc((DepthFunction)depthTest.Func.Convert());
  370. _depthMask = depthTest.WriteEnable;
  371. _depthTest = depthTest.TestEnable;
  372. UpdateDepthTest();
  373. }
  374. public void SetFaceCulling(bool enable, Face face)
  375. {
  376. if (!enable)
  377. {
  378. GL.Disable(EnableCap.CullFace);
  379. return;
  380. }
  381. GL.CullFace(face.Convert());
  382. GL.Enable(EnableCap.CullFace);
  383. }
  384. public void SetFrontFace(FrontFace frontFace)
  385. {
  386. GL.FrontFace(frontFace.Convert());
  387. }
  388. public void SetPrimitiveRestart(bool enable, int index)
  389. {
  390. if (!enable)
  391. {
  392. GL.Disable(EnableCap.PrimitiveRestart);
  393. return;
  394. }
  395. GL.PrimitiveRestartIndex(index);
  396. GL.Enable(EnableCap.PrimitiveRestart);
  397. }
  398. public void SetPrimitiveTopology(PrimitiveTopology topology)
  399. {
  400. _primitiveType = topology.Convert();
  401. }
  402. public void SetRenderTargetColorMasks(uint[] componentMasks)
  403. {
  404. _componentMasks = (uint[])componentMasks.Clone();
  405. for (int index = 0; index < componentMasks.Length; index++)
  406. {
  407. RestoreComponentMask(index);
  408. }
  409. }
  410. public void SetRenderTargets(ITexture color3D, ITexture depthStencil)
  411. {
  412. EnsureFramebuffer();
  413. TextureView color = (TextureView)color3D;
  414. for (int index = 0; index < color.DepthOrLayers; index++)
  415. {
  416. _framebuffer.AttachColor(index, color, index);
  417. }
  418. TextureView depthStencilView = (TextureView)depthStencil;
  419. _framebuffer.AttachDepthStencil(depthStencilView);
  420. _framebuffer.SetDrawBuffers(color.DepthOrLayers);
  421. _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
  422. UpdateDepthTest();
  423. }
  424. public void SetRenderTargets(ITexture[] colors, ITexture depthStencil)
  425. {
  426. EnsureFramebuffer();
  427. for (int index = 0; index < colors.Length; index++)
  428. {
  429. TextureView color = (TextureView)colors[index];
  430. _framebuffer.AttachColor(index, color);
  431. }
  432. TextureView depthStencilView = (TextureView)depthStencil;
  433. _framebuffer.AttachDepthStencil(depthStencilView);
  434. _framebuffer.SetDrawBuffers(colors.Length);
  435. _hasDepthBuffer = depthStencil != null && depthStencilView.Format != Format.S8Uint;
  436. UpdateDepthTest();
  437. }
  438. public void SetStencilTest(StencilTestDescriptor stencilTest)
  439. {
  440. if (!stencilTest.TestEnable)
  441. {
  442. GL.Disable(EnableCap.StencilTest);
  443. return;
  444. }
  445. GL.StencilOpSeparate(
  446. StencilFace.Front,
  447. stencilTest.FrontSFail.Convert(),
  448. stencilTest.FrontDpFail.Convert(),
  449. stencilTest.FrontDpPass.Convert());
  450. GL.StencilFuncSeparate(
  451. StencilFace.Front,
  452. (StencilFunction)stencilTest.FrontFunc.Convert(),
  453. stencilTest.FrontFuncRef,
  454. stencilTest.FrontFuncMask);
  455. GL.StencilMaskSeparate(StencilFace.Front, stencilTest.FrontMask);
  456. GL.StencilOpSeparate(
  457. StencilFace.Back,
  458. stencilTest.BackSFail.Convert(),
  459. stencilTest.BackDpFail.Convert(),
  460. stencilTest.BackDpPass.Convert());
  461. GL.StencilFuncSeparate(
  462. StencilFace.Back,
  463. (StencilFunction)stencilTest.BackFunc.Convert(),
  464. stencilTest.BackFuncRef,
  465. stencilTest.BackFuncMask);
  466. GL.StencilMaskSeparate(StencilFace.Back, stencilTest.BackMask);
  467. GL.Enable(EnableCap.StencilTest);
  468. _stencilFrontMask = stencilTest.FrontMask;
  469. }
  470. public void SetViewports(int first, Viewport[] viewports)
  471. {
  472. bool flipY = false;
  473. float[] viewportArray = new float[viewports.Length * 4];
  474. double[] depthRangeArray = new double[viewports.Length * 2];
  475. for (int index = 0; index < viewports.Length; index++)
  476. {
  477. int viewportElemIndex = index * 4;
  478. Viewport viewport = viewports[index];
  479. viewportArray[viewportElemIndex + 0] = viewport.Region.X;
  480. viewportArray[viewportElemIndex + 1] = viewport.Region.Y;
  481. // OpenGL does not support per-viewport flipping, so
  482. // instead we decide that based on the viewport 0 value.
  483. // It will apply to all viewports.
  484. if (index == 0)
  485. {
  486. flipY = viewport.Region.Height < 0;
  487. }
  488. if (viewport.SwizzleY == ViewportSwizzle.NegativeY)
  489. {
  490. flipY = !flipY;
  491. }
  492. viewportArray[viewportElemIndex + 2] = MathF.Abs(viewport.Region.Width);
  493. viewportArray[viewportElemIndex + 3] = MathF.Abs(viewport.Region.Height);
  494. depthRangeArray[index * 2 + 0] = viewport.DepthNear;
  495. depthRangeArray[index * 2 + 1] = viewport.DepthFar;
  496. }
  497. GL.ViewportArray(first, viewports.Length, viewportArray);
  498. GL.DepthRangeArray(first, viewports.Length, depthRangeArray);
  499. SetOrigin(flipY ? ClipOrigin.UpperLeft : ClipOrigin.LowerLeft);
  500. }
  501. private void SetOrigin(ClipOrigin origin)
  502. {
  503. if (_clipOrigin != origin)
  504. {
  505. _clipOrigin = origin;
  506. GL.ClipControl(origin, ClipDepthMode.NegativeOneToOne);
  507. }
  508. }
  509. private void EnsureVertexArray()
  510. {
  511. if (_vertexArray == null)
  512. {
  513. _vertexArray = new VertexArray();
  514. _vertexArray.Bind();
  515. }
  516. }
  517. private void EnsureFramebuffer()
  518. {
  519. if (_framebuffer == null)
  520. {
  521. _framebuffer = new Framebuffer();
  522. _framebuffer.Bind();
  523. GL.Enable(EnableCap.FramebufferSrgb);
  524. }
  525. }
  526. private void UpdateDepthTest()
  527. {
  528. // Enabling depth operations is only valid when we have
  529. // a depth buffer, otherwise it's not allowed.
  530. if (_hasDepthBuffer)
  531. {
  532. if (_depthTest)
  533. {
  534. GL.Enable(EnableCap.DepthTest);
  535. }
  536. else
  537. {
  538. GL.Disable(EnableCap.DepthTest);
  539. }
  540. GL.DepthMask(_depthMask);
  541. }
  542. else
  543. {
  544. GL.Disable(EnableCap.DepthTest);
  545. GL.DepthMask(false);
  546. }
  547. }
  548. private void PrepareForDraw()
  549. {
  550. _vertexArray.Validate();
  551. if (_unit0Texture != null)
  552. {
  553. _unit0Texture.Bind(0);
  554. }
  555. }
  556. private void RestoreComponentMask(int index)
  557. {
  558. GL.ColorMask(
  559. index,
  560. (_componentMasks[index] & 1u) != 0,
  561. (_componentMasks[index] & 2u) != 0,
  562. (_componentMasks[index] & 4u) != 0,
  563. (_componentMasks[index] & 8u) != 0);
  564. }
  565. public void RebindProgram()
  566. {
  567. _program?.Bind();
  568. }
  569. }
  570. }