OGLPipeline.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. DepthFunc = GalComparisonOp.Less,
  79. StencilTestEnabled = false,
  80. StencilBackFuncFunc = GalComparisonOp.Always,
  81. StencilBackFuncRef = 0,
  82. StencilBackFuncMask = UInt32.MaxValue,
  83. StencilBackOpFail = GalStencilOp.Keep,
  84. StencilBackOpZFail = GalStencilOp.Keep,
  85. StencilBackOpZPass = GalStencilOp.Keep,
  86. StencilBackMask = UInt32.MaxValue,
  87. StencilFrontFuncFunc = GalComparisonOp.Always,
  88. StencilFrontFuncRef = 0,
  89. StencilFrontFuncMask = UInt32.MaxValue,
  90. StencilFrontOpFail = GalStencilOp.Keep,
  91. StencilFrontOpZFail = GalStencilOp.Keep,
  92. StencilFrontOpZPass = GalStencilOp.Keep,
  93. StencilFrontMask = UInt32.MaxValue,
  94. BlendEnabled = false,
  95. BlendSeparateAlpha = false,
  96. BlendEquationRgb = 0,
  97. BlendFuncSrcRgb = GalBlendFactor.One,
  98. BlendFuncDstRgb = GalBlendFactor.Zero,
  99. BlendEquationAlpha = 0,
  100. BlendFuncSrcAlpha = GalBlendFactor.One,
  101. BlendFuncDstAlpha = GalBlendFactor.Zero,
  102. PrimitiveRestartEnabled = false,
  103. PrimitiveRestartIndex = 0
  104. };
  105. }
  106. public void Bind(GalPipelineState New)
  107. {
  108. BindConstBuffers(New);
  109. BindVertexLayout(New);
  110. if (New.FlipX != Old.FlipX || New.FlipY != Old.FlipY || New.Instance != Old.Instance)
  111. {
  112. Shader.SetExtraData(New.FlipX, New.FlipY, New.Instance);
  113. }
  114. //Note: Uncomment SetFrontFace and SetCullFace when flipping issues are solved
  115. //if (New.FrontFace != O.FrontFace)
  116. //{
  117. // GL.FrontFace(OGLEnumConverter.GetFrontFace(New.FrontFace));
  118. //}
  119. //if (New.CullFaceEnabled != O.CullFaceEnabled)
  120. //{
  121. // Enable(EnableCap.CullFace, New.CullFaceEnabled);
  122. //}
  123. //if (New.CullFaceEnabled)
  124. //{
  125. // if (New.CullFace != O.CullFace)
  126. // {
  127. // GL.CullFace(OGLEnumConverter.GetCullFace(New.CullFace));
  128. // }
  129. //}
  130. if (New.DepthTestEnabled != Old.DepthTestEnabled)
  131. {
  132. Enable(EnableCap.DepthTest, New.DepthTestEnabled);
  133. }
  134. if (New.DepthTestEnabled)
  135. {
  136. if (New.DepthFunc != Old.DepthFunc)
  137. {
  138. GL.DepthFunc(OGLEnumConverter.GetDepthFunc(New.DepthFunc));
  139. }
  140. }
  141. if (New.StencilTestEnabled != Old.StencilTestEnabled)
  142. {
  143. Enable(EnableCap.StencilTest, New.StencilTestEnabled);
  144. }
  145. if (New.StencilTestEnabled)
  146. {
  147. if (New.StencilBackFuncFunc != Old.StencilBackFuncFunc ||
  148. New.StencilBackFuncRef != Old.StencilBackFuncRef ||
  149. New.StencilBackFuncMask != Old.StencilBackFuncMask)
  150. {
  151. GL.StencilFuncSeparate(
  152. StencilFace.Back,
  153. OGLEnumConverter.GetStencilFunc(New.StencilBackFuncFunc),
  154. New.StencilBackFuncRef,
  155. New.StencilBackFuncMask);
  156. }
  157. if (New.StencilBackOpFail != Old.StencilBackOpFail ||
  158. New.StencilBackOpZFail != Old.StencilBackOpZFail ||
  159. New.StencilBackOpZPass != Old.StencilBackOpZPass)
  160. {
  161. GL.StencilOpSeparate(
  162. StencilFace.Back,
  163. OGLEnumConverter.GetStencilOp(New.StencilBackOpFail),
  164. OGLEnumConverter.GetStencilOp(New.StencilBackOpZFail),
  165. OGLEnumConverter.GetStencilOp(New.StencilBackOpZPass));
  166. }
  167. if (New.StencilBackMask != Old.StencilBackMask)
  168. {
  169. GL.StencilMaskSeparate(StencilFace.Back, New.StencilBackMask);
  170. }
  171. if (New.StencilFrontFuncFunc != Old.StencilFrontFuncFunc ||
  172. New.StencilFrontFuncRef != Old.StencilFrontFuncRef ||
  173. New.StencilFrontFuncMask != Old.StencilFrontFuncMask)
  174. {
  175. GL.StencilFuncSeparate(
  176. StencilFace.Front,
  177. OGLEnumConverter.GetStencilFunc(New.StencilFrontFuncFunc),
  178. New.StencilFrontFuncRef,
  179. New.StencilFrontFuncMask);
  180. }
  181. if (New.StencilFrontOpFail != Old.StencilFrontOpFail ||
  182. New.StencilFrontOpZFail != Old.StencilFrontOpZFail ||
  183. New.StencilFrontOpZPass != Old.StencilFrontOpZPass)
  184. {
  185. GL.StencilOpSeparate(
  186. StencilFace.Front,
  187. OGLEnumConverter.GetStencilOp(New.StencilFrontOpFail),
  188. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZFail),
  189. OGLEnumConverter.GetStencilOp(New.StencilFrontOpZPass));
  190. }
  191. if (New.StencilFrontMask != Old.StencilFrontMask)
  192. {
  193. GL.StencilMaskSeparate(StencilFace.Front, New.StencilFrontMask);
  194. }
  195. }
  196. if (New.BlendEnabled != Old.BlendEnabled)
  197. {
  198. Enable(EnableCap.Blend, New.BlendEnabled);
  199. }
  200. if (New.BlendEnabled)
  201. {
  202. if (New.BlendSeparateAlpha)
  203. {
  204. if (New.BlendEquationRgb != Old.BlendEquationRgb ||
  205. New.BlendEquationAlpha != Old.BlendEquationAlpha)
  206. {
  207. GL.BlendEquationSeparate(
  208. OGLEnumConverter.GetBlendEquation(New.BlendEquationRgb),
  209. OGLEnumConverter.GetBlendEquation(New.BlendEquationAlpha));
  210. }
  211. if (New.BlendFuncSrcRgb != Old.BlendFuncSrcRgb ||
  212. New.BlendFuncDstRgb != Old.BlendFuncDstRgb ||
  213. New.BlendFuncSrcAlpha != Old.BlendFuncSrcAlpha ||
  214. New.BlendFuncDstAlpha != Old.BlendFuncDstAlpha)
  215. {
  216. GL.BlendFuncSeparate(
  217. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcRgb),
  218. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.BlendFuncDstRgb),
  219. (BlendingFactorSrc) OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcAlpha),
  220. (BlendingFactorDest)OGLEnumConverter.GetBlendFactor(New.BlendFuncDstAlpha));
  221. }
  222. }
  223. else
  224. {
  225. if (New.BlendEquationRgb != Old.BlendEquationRgb)
  226. {
  227. GL.BlendEquation(OGLEnumConverter.GetBlendEquation(New.BlendEquationRgb));
  228. }
  229. if (New.BlendFuncSrcRgb != Old.BlendFuncSrcRgb ||
  230. New.BlendFuncDstRgb != Old.BlendFuncDstRgb)
  231. {
  232. GL.BlendFunc(
  233. OGLEnumConverter.GetBlendFactor(New.BlendFuncSrcRgb),
  234. OGLEnumConverter.GetBlendFactor(New.BlendFuncDstRgb));
  235. }
  236. }
  237. }
  238. if (New.PrimitiveRestartEnabled != Old.PrimitiveRestartEnabled)
  239. {
  240. Enable(EnableCap.PrimitiveRestart, New.PrimitiveRestartEnabled);
  241. }
  242. if (New.PrimitiveRestartEnabled)
  243. {
  244. if (New.PrimitiveRestartIndex != Old.PrimitiveRestartIndex)
  245. {
  246. GL.PrimitiveRestartIndex(New.PrimitiveRestartIndex);
  247. }
  248. }
  249. Old = New;
  250. }
  251. private void BindConstBuffers(GalPipelineState New)
  252. {
  253. int FreeBinding = OGLShader.ReservedCbufCount;
  254. void BindIfNotNull(OGLShaderStage Stage)
  255. {
  256. if (Stage != null)
  257. {
  258. foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
  259. {
  260. long Key = New.ConstBufferKeys[(int)Stage.Type][DeclInfo.Cbuf];
  261. if (Key != 0 && Buffer.TryGetUbo(Key, out int UboHandle))
  262. {
  263. GL.BindBufferBase(BufferRangeTarget.UniformBuffer, FreeBinding, UboHandle);
  264. }
  265. FreeBinding++;
  266. }
  267. }
  268. }
  269. BindIfNotNull(Shader.Current.Vertex);
  270. BindIfNotNull(Shader.Current.TessControl);
  271. BindIfNotNull(Shader.Current.TessEvaluation);
  272. BindIfNotNull(Shader.Current.Geometry);
  273. BindIfNotNull(Shader.Current.Fragment);
  274. }
  275. private void BindVertexLayout(GalPipelineState New)
  276. {
  277. foreach (GalVertexBinding Binding in New.VertexBindings)
  278. {
  279. if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
  280. {
  281. continue;
  282. }
  283. if (VaoHandle == 0)
  284. {
  285. VaoHandle = GL.GenVertexArray();
  286. //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
  287. //if you want to use it, move this line out of the if
  288. GL.BindVertexArray(VaoHandle);
  289. }
  290. foreach (GalVertexAttrib Attrib in Binding.Attribs)
  291. {
  292. GL.EnableVertexAttribArray(Attrib.Index);
  293. GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);
  294. bool Unsigned =
  295. Attrib.Type == GalVertexAttribType.Unorm ||
  296. Attrib.Type == GalVertexAttribType.Uint ||
  297. Attrib.Type == GalVertexAttribType.Uscaled;
  298. bool Normalize =
  299. Attrib.Type == GalVertexAttribType.Snorm ||
  300. Attrib.Type == GalVertexAttribType.Unorm;
  301. VertexAttribPointerType Type = 0;
  302. if (Attrib.Type == GalVertexAttribType.Float)
  303. {
  304. Type = VertexAttribPointerType.Float;
  305. }
  306. else
  307. {
  308. if (Unsigned)
  309. {
  310. Type = UnsignedAttribTypes[Attrib.Size];
  311. }
  312. else
  313. {
  314. Type = SignedAttribTypes[Attrib.Size];
  315. }
  316. }
  317. int Size = AttribElements[Attrib.Size];
  318. int Offset = Attrib.Offset;
  319. if (Attrib.Type == GalVertexAttribType.Sint ||
  320. Attrib.Type == GalVertexAttribType.Uint)
  321. {
  322. IntPtr Pointer = new IntPtr(Offset);
  323. VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;
  324. GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
  325. }
  326. else
  327. {
  328. GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
  329. }
  330. if (Binding.Instanced && Binding.Divisor != 0)
  331. {
  332. GL.VertexAttribDivisor(Attrib.Index, 1);
  333. }
  334. else
  335. {
  336. GL.VertexAttribDivisor(Attrib.Index, 0);
  337. }
  338. }
  339. }
  340. }
  341. private void Enable(EnableCap Cap, bool Enabled)
  342. {
  343. if (Enabled)
  344. {
  345. GL.Enable(Cap);
  346. }
  347. else
  348. {
  349. GL.Disable(Cap);
  350. }
  351. }
  352. }
  353. }