/MonoGame.Framework/Graphics/States/DepthStencilState.OpenGL.cs

https://github.com/vostok4/MonoGame · C# · 244 lines · 221 code · 17 blank · 6 comment · 50 complexity · e5421f106f99290fe06ee9339864eb96 MD5 · raw file

  1. // MonoGame - Copyright (C) The MonoGame Team
  2. // This file is subject to the terms and conditions defined in
  3. // file 'LICENSE.txt', which is part of this source code package.
  4. #if MONOMAC
  5. #if PLATFORM_MACOS_LEGACY
  6. using MonoMac.OpenGL;
  7. using GLStencilFunction = MonoMac.OpenGL.StencilFunction;
  8. #else
  9. using OpenTK.Graphics.OpenGL;
  10. using GLStencilFunction = OpenTK.Graphics.OpenGL.StencilFunction;
  11. #endif
  12. #elif DESKTOPGL
  13. using OpenTK.Graphics.OpenGL;
  14. using GLStencilFunction = OpenTK.Graphics.OpenGL.StencilFunction;
  15. #elif GLES
  16. using OpenTK.Graphics.ES20;
  17. using GLStencilFunction = OpenTK.Graphics.ES20.StencilFunction;
  18. #endif
  19. namespace Microsoft.Xna.Framework.Graphics
  20. {
  21. public partial class DepthStencilState
  22. {
  23. internal void PlatformApplyState(GraphicsDevice device, bool force = false)
  24. {
  25. if (force || this.DepthBufferEnable != device._lastDepthStencilState.DepthBufferEnable)
  26. {
  27. if (!DepthBufferEnable)
  28. {
  29. GL.Disable(EnableCap.DepthTest);
  30. GraphicsExtensions.CheckGLError();
  31. }
  32. else
  33. {
  34. // enable Depth Buffer
  35. GL.Enable(EnableCap.DepthTest);
  36. GraphicsExtensions.CheckGLError();
  37. }
  38. device._lastDepthStencilState.DepthBufferEnable = this.DepthBufferEnable;
  39. }
  40. if (force || this.DepthBufferFunction != device._lastDepthStencilState.DepthBufferFunction)
  41. {
  42. GL.DepthFunc(DepthBufferFunction.GetDepthFunction());
  43. GraphicsExtensions.CheckGLError();
  44. device._lastDepthStencilState.DepthBufferFunction = this.DepthBufferFunction;
  45. }
  46. if (force || this.DepthBufferWriteEnable != device._lastDepthStencilState.DepthBufferWriteEnable)
  47. {
  48. GL.DepthMask(DepthBufferWriteEnable);
  49. GraphicsExtensions.CheckGLError();
  50. device._lastDepthStencilState.DepthBufferWriteEnable = this.DepthBufferWriteEnable;
  51. }
  52. if (force || this.StencilEnable != device._lastDepthStencilState.StencilEnable)
  53. {
  54. if (!StencilEnable)
  55. {
  56. GL.Disable(EnableCap.StencilTest);
  57. GraphicsExtensions.CheckGLError();
  58. }
  59. else
  60. {
  61. // enable Stencil
  62. GL.Enable(EnableCap.StencilTest);
  63. GraphicsExtensions.CheckGLError();
  64. }
  65. device._lastDepthStencilState.StencilEnable = this.StencilEnable;
  66. }
  67. // set function
  68. if (this.TwoSidedStencilMode)
  69. {
  70. #if GLES
  71. var cullFaceModeFront = CullFaceMode.Front;
  72. var cullFaceModeBack = CullFaceMode.Back;
  73. var stencilFaceFront = CullFaceMode.Front;
  74. var stencilFaceBack = CullFaceMode.Back;
  75. #elif MONOMAC
  76. var cullFaceModeFront = (Version20)CullFaceMode.Front;
  77. var cullFaceModeBack = (Version20)CullFaceMode.Back;
  78. var stencilFaceFront = StencilFace.Front;
  79. var stencilFaceBack = StencilFace.Back;
  80. #else
  81. var cullFaceModeFront = StencilFace.Front;
  82. var cullFaceModeBack = StencilFace.Back;
  83. var stencilFaceFront = StencilFace.Front;
  84. var stencilFaceBack = StencilFace.Back;
  85. #endif
  86. if (force ||
  87. this.TwoSidedStencilMode != device._lastDepthStencilState.TwoSidedStencilMode ||
  88. this.StencilFunction != device._lastDepthStencilState.StencilFunction ||
  89. this.ReferenceStencil != device._lastDepthStencilState.ReferenceStencil ||
  90. this.StencilMask != device._lastDepthStencilState.StencilMask)
  91. {
  92. GL.StencilFuncSeparate(cullFaceModeFront, GetStencilFunc(this.StencilFunction),
  93. this.ReferenceStencil, this.StencilMask);
  94. GraphicsExtensions.CheckGLError();
  95. device._lastDepthStencilState.StencilFunction = this.StencilFunction;
  96. device._lastDepthStencilState.ReferenceStencil = this.ReferenceStencil;
  97. device._lastDepthStencilState.StencilMask = this.StencilMask;
  98. }
  99. if (force ||
  100. this.TwoSidedStencilMode != device._lastDepthStencilState.TwoSidedStencilMode ||
  101. this.CounterClockwiseStencilFunction != device._lastDepthStencilState.CounterClockwiseStencilFunction ||
  102. this.ReferenceStencil != device._lastDepthStencilState.ReferenceStencil ||
  103. this.StencilMask != device._lastDepthStencilState.StencilMask)
  104. {
  105. GL.StencilFuncSeparate(cullFaceModeBack, GetStencilFunc(this.CounterClockwiseStencilFunction),
  106. this.ReferenceStencil, this.StencilMask);
  107. GraphicsExtensions.CheckGLError();
  108. device._lastDepthStencilState.CounterClockwiseStencilFunction = this.CounterClockwiseStencilFunction;
  109. device._lastDepthStencilState.ReferenceStencil = this.ReferenceStencil;
  110. device._lastDepthStencilState.StencilMask = this.StencilMask;
  111. }
  112. if (force ||
  113. this.TwoSidedStencilMode != device._lastDepthStencilState.TwoSidedStencilMode ||
  114. this.StencilFail != device._lastDepthStencilState.StencilFail ||
  115. this.StencilDepthBufferFail != device._lastDepthStencilState.StencilDepthBufferFail ||
  116. this.StencilPass != device._lastDepthStencilState.StencilPass)
  117. {
  118. GL.StencilOpSeparate(stencilFaceFront, GetStencilOp(this.StencilFail),
  119. GetStencilOp(this.StencilDepthBufferFail),
  120. GetStencilOp(this.StencilPass));
  121. GraphicsExtensions.CheckGLError();
  122. device._lastDepthStencilState.StencilFail = this.StencilFail;
  123. device._lastDepthStencilState.StencilDepthBufferFail = this.StencilDepthBufferFail;
  124. device._lastDepthStencilState.StencilPass = this.StencilPass;
  125. }
  126. if (force ||
  127. this.TwoSidedStencilMode != device._lastDepthStencilState.TwoSidedStencilMode ||
  128. this.CounterClockwiseStencilFail != device._lastDepthStencilState.CounterClockwiseStencilFail ||
  129. this.CounterClockwiseStencilDepthBufferFail != device._lastDepthStencilState.CounterClockwiseStencilDepthBufferFail ||
  130. this.CounterClockwiseStencilPass != device._lastDepthStencilState.CounterClockwiseStencilPass)
  131. {
  132. GL.StencilOpSeparate(stencilFaceBack, GetStencilOp(this.CounterClockwiseStencilFail),
  133. GetStencilOp(this.CounterClockwiseStencilDepthBufferFail),
  134. GetStencilOp(this.CounterClockwiseStencilPass));
  135. GraphicsExtensions.CheckGLError();
  136. device._lastDepthStencilState.CounterClockwiseStencilFail = this.CounterClockwiseStencilFail;
  137. device._lastDepthStencilState.CounterClockwiseStencilDepthBufferFail = this.CounterClockwiseStencilDepthBufferFail;
  138. device._lastDepthStencilState.CounterClockwiseStencilPass = this.CounterClockwiseStencilPass;
  139. }
  140. }
  141. else
  142. {
  143. if (force ||
  144. this.TwoSidedStencilMode != device._lastDepthStencilState.TwoSidedStencilMode ||
  145. this.StencilFunction != device._lastDepthStencilState.StencilFunction ||
  146. this.ReferenceStencil != device._lastDepthStencilState.ReferenceStencil ||
  147. this.StencilMask != device._lastDepthStencilState.StencilMask)
  148. {
  149. GL.StencilFunc(GetStencilFunc(this.StencilFunction), ReferenceStencil, StencilMask);
  150. GraphicsExtensions.CheckGLError();
  151. device._lastDepthStencilState.StencilFunction = this.StencilFunction;
  152. device._lastDepthStencilState.ReferenceStencil = this.ReferenceStencil;
  153. device._lastDepthStencilState.StencilMask = this.StencilMask;
  154. }
  155. if (force ||
  156. this.TwoSidedStencilMode != device._lastDepthStencilState.TwoSidedStencilMode ||
  157. this.StencilFail != device._lastDepthStencilState.StencilFail ||
  158. this.StencilDepthBufferFail != device._lastDepthStencilState.StencilDepthBufferFail ||
  159. this.StencilPass != device._lastDepthStencilState.StencilPass)
  160. {
  161. GL.StencilOp(GetStencilOp(StencilFail),
  162. GetStencilOp(StencilDepthBufferFail),
  163. GetStencilOp(StencilPass));
  164. GraphicsExtensions.CheckGLError();
  165. device._lastDepthStencilState.StencilFail = this.StencilFail;
  166. device._lastDepthStencilState.StencilDepthBufferFail = this.StencilDepthBufferFail;
  167. device._lastDepthStencilState.StencilPass = this.StencilPass;
  168. }
  169. }
  170. device._lastDepthStencilState.TwoSidedStencilMode = this.TwoSidedStencilMode;
  171. if (force || this.StencilWriteMask != device._lastDepthStencilState.StencilWriteMask)
  172. {
  173. GL.StencilMask(this.StencilWriteMask);
  174. GraphicsExtensions.CheckGLError();
  175. device._lastDepthStencilState.StencilWriteMask = this.StencilWriteMask;
  176. }
  177. }
  178. private static GLStencilFunction GetStencilFunc(CompareFunction function)
  179. {
  180. switch (function)
  181. {
  182. case CompareFunction.Always:
  183. return GLStencilFunction.Always;
  184. case CompareFunction.Equal:
  185. return GLStencilFunction.Equal;
  186. case CompareFunction.Greater:
  187. return GLStencilFunction.Greater;
  188. case CompareFunction.GreaterEqual:
  189. return GLStencilFunction.Gequal;
  190. case CompareFunction.Less:
  191. return GLStencilFunction.Less;
  192. case CompareFunction.LessEqual:
  193. return GLStencilFunction.Lequal;
  194. case CompareFunction.Never:
  195. return GLStencilFunction.Never;
  196. case CompareFunction.NotEqual:
  197. return GLStencilFunction.Notequal;
  198. default:
  199. return GLStencilFunction.Always;
  200. }
  201. }
  202. private static StencilOp GetStencilOp(StencilOperation operation)
  203. {
  204. switch (operation)
  205. {
  206. case StencilOperation.Keep:
  207. return StencilOp.Keep;
  208. case StencilOperation.Decrement:
  209. return StencilOp.DecrWrap;
  210. case StencilOperation.DecrementSaturation:
  211. return StencilOp.Decr;
  212. case StencilOperation.IncrementSaturation:
  213. return StencilOp.Incr;
  214. case StencilOperation.Increment:
  215. return StencilOp.IncrWrap;
  216. case StencilOperation.Invert:
  217. return StencilOp.Invert;
  218. case StencilOperation.Replace:
  219. return StencilOp.Replace;
  220. case StencilOperation.Zero:
  221. return StencilOp.Zero;
  222. default:
  223. return StencilOp.Keep;
  224. }
  225. }
  226. }
  227. }