OGLPipeline.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using OpenTK.Graphics.OpenGL;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Gal.OpenGL
  5. {
  6. class OGLPipeline : IGalPipeline
  7. {
  8. private static Dictionary<GalVertexAttribSize, int> AttribElements =
  9. new Dictionary<GalVertexAttribSize, int>()
  10. {
  11. { GalVertexAttribSize._32_32_32_32, 4 },
  12. { GalVertexAttribSize._32_32_32, 3 },
  13. { GalVertexAttribSize._16_16_16_16, 4 },
  14. { GalVertexAttribSize._32_32, 2 },
  15. { GalVertexAttribSize._16_16_16, 3 },
  16. { GalVertexAttribSize._8_8_8_8, 4 },
  17. { GalVertexAttribSize._16_16, 2 },
  18. { GalVertexAttribSize._32, 1 },
  19. { GalVertexAttribSize._8_8_8, 3 },
  20. { GalVertexAttribSize._8_8, 2 },
  21. { GalVertexAttribSize._16, 1 },
  22. { GalVertexAttribSize._8, 1 },
  23. { GalVertexAttribSize._10_10_10_2, 4 },
  24. { GalVertexAttribSize._11_11_10, 3 }
  25. };
  26. private static Dictionary<GalVertexAttribSize, VertexAttribPointerType> SignedAttribTypes =
  27. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  28. {
  29. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.Int },
  30. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.Int },
  31. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.Short },
  32. { GalVertexAttribSize._32_32, VertexAttribPointerType.Int },
  33. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.Short },
  34. { GalVertexAttribSize._8_8_8_8, VertexAttribPointerType.Byte },
  35. { GalVertexAttribSize._16_16, VertexAttribPointerType.Short },
  36. { GalVertexAttribSize._32, VertexAttribPointerType.Int },
  37. { GalVertexAttribSize._8_8_8, VertexAttribPointerType.Byte },
  38. { GalVertexAttribSize._8_8, VertexAttribPointerType.Byte },
  39. { GalVertexAttribSize._16, VertexAttribPointerType.Short },
  40. { GalVertexAttribSize._8, VertexAttribPointerType.Byte },
  41. { GalVertexAttribSize._10_10_10_2, VertexAttribPointerType.Int2101010Rev }
  42. };
  43. private static Dictionary<GalVertexAttribSize, VertexAttribPointerType> UnsignedAttribTypes =
  44. new Dictionary<GalVertexAttribSize, VertexAttribPointerType>()
  45. {
  46. { GalVertexAttribSize._32_32_32_32, VertexAttribPointerType.UnsignedInt },
  47. { GalVertexAttribSize._32_32_32, VertexAttribPointerType.UnsignedInt },
  48. { GalVertexAttribSize._16_16_16_16, VertexAttribPointerType.UnsignedShort },
  49. { GalVertexAttribSize._32_32, VertexAttribPointerType.UnsignedInt },
  50. { GalVertexAttribSize._16_16_16, VertexAttribPointerType.UnsignedShort },
  51. { GalVertexAttribSize._8_8_8_8, VertexAttribPointerType.UnsignedByte },
  52. { GalVertexAttribSize._16_16, VertexAttribPointerType.UnsignedShort },
  53. { GalVertexAttribSize._32, VertexAttribPointerType.UnsignedInt },
  54. { GalVertexAttribSize._8_8_8, VertexAttribPointerType.UnsignedByte },
  55. { GalVertexAttribSize._8_8, VertexAttribPointerType.UnsignedByte },
  56. { GalVertexAttribSize._16, VertexAttribPointerType.UnsignedShort },
  57. { GalVertexAttribSize._8, VertexAttribPointerType.UnsignedByte },
  58. { GalVertexAttribSize._10_10_10_2, VertexAttribPointerType.UnsignedInt2101010Rev },
  59. { GalVertexAttribSize._11_11_10, VertexAttribPointerType.UnsignedInt10F11F11FRev }
  60. };
  61. private GalPipelineState Old;
  62. private OGLConstBuffer Buffer;
  63. private OGLRasterizer Rasterizer;
  64. private OGLShader Shader;
  65. private int VaoHandle;
  66. public OGLPipeline(OGLConstBuffer Buffer, OGLRasterizer Rasterizer, OGLShader Shader)
  67. {
  68. this.Buffer = Buffer;
  69. this.Rasterizer = Rasterizer;
  70. this.Shader = Shader;
  71. //These values match OpenGL's defaults
  72. Old = new GalPipelineState
  73. {
  74. FrontFace = GalFrontFace.CCW,
  75. CullFaceEnabled = false,
  76. CullFace = GalCullFace.Back,
  77. DepthTestEnabled = false,
  78. DepthWriteEnabled = true,
  79. DepthFunc = GalComparisonOp.Less,
  80. StencilTestEnabled = false,
  81. StencilBackFuncFunc = GalComparisonOp.Always,
  82. StencilBackFuncRef = 0,
  83. StencilBackFuncMask = UInt32.MaxValue,
  84. StencilBackOpFail = GalStencilOp.Keep,
  85. StencilBackOpZFail = GalStencilOp.Keep,
  86. StencilBackOpZPass = GalStencilOp.Keep,
  87. StencilBackMask = UInt32.MaxValue,
  88. StencilFrontFuncFunc = GalComparisonOp.Always,
  89. StencilFrontFuncRef = 0,
  90. StencilFrontFuncMask = UInt32.MaxValue,
  91. StencilFrontOpFail = GalStencilOp.Keep,
  92. StencilFrontOpZFail = GalStencilOp.Keep,
  93. StencilFrontOpZPass = GalStencilOp.Keep,
  94. StencilFrontMask = UInt32.MaxValue,
  95. BlendEnabled = false,
  96. BlendSeparateAlpha = false,
  97. BlendEquationRgb = 0,
  98. BlendFuncSrcRgb = GalBlendFactor.One,
  99. BlendFuncDstRgb = GalBlendFactor.Zero,
  100. BlendEquationAlpha = 0,
  101. BlendFuncSrcAlpha = GalBlendFactor.One,
  102. BlendFuncDstAlpha = GalBlendFactor.Zero,
  103. PrimitiveRestartEnabled = false,
  104. PrimitiveRestartIndex = 0
  105. };
  106. }
  107. public void Bind(GalPipelineState New)
  108. {
  109. BindConstBuffers(New);
  110. BindVertexLayout(New);
  111. if (New.FramebufferSrgb != Old.FramebufferSrgb)
  112. {
  113. Enable(EnableCap.FramebufferSrgb, New.FramebufferSrgb);
  114. }
  115. if (New.FlipX != Old.FlipX || New.FlipY != Old.FlipY || New.Instance != Old.Instance)
  116. {
  117. Shader.SetExtraData(New.FlipX, New.FlipY, New.Instance);
  118. }
  119. //Note: Uncomment SetFrontFace and SetCullFace when flipping issues are solved
  120. //if (New.FrontFace != Old.FrontFace)
  121. //{
  122. // GL.FrontFace(OGLEnumConverter.GetFrontFace(New.FrontFace));
  123. //}
  124. //if (New.CullFaceEnabled != Old.CullFaceEnabled)
  125. //{
  126. // Enable(EnableCap.CullFace, New.CullFaceEnabled);
  127. //}
  128. //if (New.CullFaceEnabled)
  129. //{
  130. // if (New.CullFace != Old.CullFace)
  131. // {
  132. // GL.CullFace(OGLEnumConverter.GetCullFace(New.CullFace));
  133. // }
  134. //}
  135. if (New.DepthTestEnabled != Old.DepthTestEnabled)
  136. {
  137. Enable(EnableCap.DepthTest, New.DepthTestEnabled);
  138. }
  139. if (New.DepthWriteEnabled != Old.DepthWriteEnabled)
  140. {
  141. Rasterizer.DepthWriteEnabled = New.DepthWriteEnabled;
  142. GL.DepthMask(New.DepthWriteEnabled);
  143. }
  144. if (New.DepthTestEnabled)
  145. {
  146. if (New.DepthFunc != Old.DepthFunc)
  147. {
  148. GL.DepthFunc(OGLEnumConverter.GetDepthFunc(New.DepthFunc));
  149. }
  150. }
  151. if (New.StencilTestEnabled != Old.StencilTestEnabled)
  152. {
  153. Enable(EnableCap.StencilTest, New.StencilTestEnabled);
  154. }
  155. if (New.StencilTestEnabled)
  156. {
  157. if (New.StencilBackFuncFunc != Old.StencilBackFuncFunc ||
  158. New.StencilBackFuncRef != Old.StencilBackFuncRef ||
  159. New.StencilBackFuncMask != Old.StencilBackFuncMask)
  160. {
  161. GL.StencilFuncSeparate(
  162. StencilFace.Back,
  163. OGLEnumConverter.GetStencilFunc(New.StencilBackFuncFunc),
  164. New.StencilBackFuncRef,
  165. New.StencilBackFuncMask);
  166. }
  167. if (New.StencilBackOpFail != Old.StencilBackOpFail ||
  168. New.StencilBackOpZFail != Old.StencilBackOpZFail ||
  169. New.StencilBackOpZPass != Old.StencilBackOpZPass)
  170. {
  171. GL.StencilOpSeparate(
  172. StencilFace.Back,
  173. OGLEnumConverter.GetStencilOp(New.StencilBackOpFail),
  174. OGLEnumConverter.GetStencilOp(New.StencilBackOpZFail),
  175. OGLEnumConverter.GetStencilOp(New.StencilBackOpZPass));
  176. }
  177. if (New.StencilBackMask != Old.StencilBackMask)
  178. {
  179. GL.StencilMaskSeparate(StencilFace.Back, New.StencilBackMask);
  180. }
  181. if (New.StencilFrontFuncFunc != Old.StencilFrontFuncFunc ||
  182. New.StencilFrontFuncRef != Old.StencilFrontFuncRef ||
  183. New.StencilFrontFuncMask != Old.StencilFrontFuncMask)
  184. {
  185. GL.StencilFuncSeparate(
  186. StencilFace.Front,
  187. OGLEnumConverter.GetStencilFunc(New.StencilFrontFuncFunc),
  188. New.StencilFrontFuncRef,
  189. New.StencilFrontFuncMask);
  190. }
  191. if (New.StencilFrontOpFail != Old.StencilFrontOpFail ||
  192. New.StencilFrontOpZFail != Old.StencilFrontOpZFail ||
  193. New.StencilFrontOpZPass != Old.StencilFrontOpZPass)
  194. {
  195. GL.StencilOpSeparate(
  196. StencilFace.Front,
  197. OGLEnumConverter.GetStencilOp(New.StencilFrontOpFail),
  198. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZFail),
  199. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZPass));
  200. }
  201. if (New.StencilFrontMask != Old.StencilFrontMask)
  202. {
  203. GL.StencilMaskSeparate(StencilFace.Front, New.StencilFrontMask);
  204. }
  205. }
  206. if (New.BlendEnabled != Old.BlendEnabled)
  207. {
  208. Enable(EnableCap.Blend, New.BlendEnabled);
  209. }
  210. if (New.BlendEnabled)
  211. {
  212. if (New.BlendSeparateAlpha)
  213. {
  214. if (New.BlendEquationRgb != Old.BlendEquationRgb ||
  215. New.BlendEquationAlpha != Old.BlendEquationAlpha)
  216. {
  217. GL.BlendEquationSeparate(
  218. OGLEnumConverter.GetBlendEquation(New.BlendEquationRgb),
  219. OGLEnumConverter.GetBlendEquation(New.BlendEquationAlpha));
  220. }
  221. if (New.BlendFuncSrcRgb != Old.BlendFuncSrcRgb ||
  222. New.BlendFuncDstRgb != Old.BlendFuncDstRgb ||
  223. New.BlendFuncSrcAlpha != Old.BlendFuncSrcAlpha ||
  224. New.BlendFuncDstAlpha != Old.BlendFuncDstAlpha)
  225. {
  226. GL.BlendFuncSeparate(
  227. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcRgb),
  228. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.BlendFuncDstRgb),
  229. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcAlpha),
  230. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.BlendFuncDstAlpha));
  231. }
  232. }
  233. else
  234. {
  235. if (New.BlendEquationRgb != Old.BlendEquationRgb)
  236. {
  237. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.BlendEquationRgb));
  238. }
  239. if (New.BlendFuncSrcRgb != Old.BlendFuncSrcRgb ||
  240. New.BlendFuncDstRgb != Old.BlendFuncDstRgb)
  241. {
  242. GL.BlendFunc(
  243. OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcRgb),
  244. OGLEnumConverter.GetBlendFactor(New.BlendFuncDstRgb));
  245. }
  246. }
  247. }
  248. if (New.PrimitiveRestartEnabled != Old.PrimitiveRestartEnabled)
  249. {
  250. Enable(EnableCap.PrimitiveRestart, New.PrimitiveRestartEnabled);
  251. }
  252. if (New.PrimitiveRestartEnabled)
  253. {
  254. if (New.PrimitiveRestartIndex != Old.PrimitiveRestartIndex)
  255. {
  256. GL.PrimitiveRestartIndex(New.PrimitiveRestartIndex);
  257. }
  258. }
  259. Old = New;
  260. }
  261. private void BindConstBuffers(GalPipelineState New)
  262. {
  263. int FreeBinding = OGLShader.ReservedCbufCount;
  264. void BindIfNotNull(OGLShaderStage Stage)
  265. {
  266. if (Stage != null)
  267. {
  268. foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
  269. {
  270. long Key = New.ConstBufferKeys[(int)Stage.Type][DeclInfo.Cbuf];
  271. if (Key != 0 && Buffer.TryGetUbo(Key, out int UboHandle))
  272. {
  273. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, FreeBinding, UboHandle);
  274. }
  275. FreeBinding++;
  276. }
  277. }
  278. }
  279. BindIfNotNull(Shader.Current.Vertex);
  280. BindIfNotNull(Shader.Current.TessControl);
  281. BindIfNotNull(Shader.Current.TessEvaluation);
  282. BindIfNotNull(Shader.Current.Geometry);
  283. BindIfNotNull(Shader.Current.Fragment);
  284. }
  285. private void BindVertexLayout(GalPipelineState New)
  286. {
  287. foreach (GalVertexBinding Binding in New.VertexBindings)
  288. {
  289. if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
  290. {
  291. continue;
  292. }
  293. if (VaoHandle == 0)
  294. {
  295. VaoHandle = GL.GenVertexArray();
  296. //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
  297. //if you want to use it, move this line out of the if
  298. GL.BindVertexArray(VaoHandle);
  299. }
  300. foreach (GalVertexAttrib Attrib in Binding.Attribs)
  301. {
  302. //Skip uninitialized attributes.
  303. if (Attrib.Size == 0)
  304. {
  305. continue;
  306. }
  307. GL.EnableVertexAttribArray(Attrib.Index);
  308. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  309. bool Unsigned =
  310. Attrib.Type == GalVertexAttribType.Unorm ||
  311. Attrib.Type == GalVertexAttribType.Uint ||
  312. Attrib.Type == GalVertexAttribType.Uscaled;
  313. bool Normalize =
  314. Attrib.Type == GalVertexAttribType.Snorm ||
  315. Attrib.Type == GalVertexAttribType.Unorm;
  316. VertexAttribPointerType Type = 0;
  317. if (Attrib.Type == GalVertexAttribType.Float)
  318. {
  319. Type = VertexAttribPointerType.Float;
  320. }
  321. else
  322. {
  323. if (Unsigned)
  324. {
  325. Type = UnsignedAttribTypes[Attrib.Size];
  326. }
  327. else
  328. {
  329. Type = SignedAttribTypes[Attrib.Size];
  330. }
  331. }
  332. int Size = AttribElements[Attrib.Size];
  333. int Offset = Attrib.Offset;
  334. if (Attrib.Type == GalVertexAttribType.Sint ||
  335. Attrib.Type == GalVertexAttribType.Uint)
  336. {
  337. IntPtr Pointer = new IntPtr(Offset);
  338. VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;
  339. GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
  340. }
  341. else
  342. {
  343. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
  344. }
  345. if (Binding.Instanced && Binding.Divisor != 0)
  346. {
  347. GL.VertexAttribDivisor(Attrib.Index, 1);
  348. }
  349. else
  350. {
  351. GL.VertexAttribDivisor(Attrib.Index, 0);
  352. }
  353. }
  354. }
  355. }
  356. private void Enable(EnableCap Cap, bool Enabled)
  357. {
  358. if (Enabled)
  359. {
  360. GL.Enable(Cap);
  361. }
  362. else
  363. {
  364. GL.Disable(Cap);
  365. }
  366. }
  367. }
  368. }