Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SamplerState.OpenGL.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL
4 using System;
5 using SiliconStudio.Core.Mathematics;
6 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
7 using OpenTK.Graphics.ES30;
8 #else
9 using OpenTK.Graphics.OpenGL;
10 #endif
11 
12 namespace SiliconStudio.Paradox.Graphics
13 {
14  public partial class SamplerState
15  {
16 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
17  private TextureWrapMode textureWrapR;
18 #endif
19  private TextureWrapMode textureWrapS;
20  private TextureWrapMode textureWrapT;
21 
22  private TextureMinFilter minFilter;
23  private TextureMagFilter magFilter;
24 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
25  private TextureMinFilter minFilterNoMipmap;
26 #endif
27 
28  private float[] borderColor;
29 
30  private DepthFunction compareFunc;
31 
32  private SamplerState(GraphicsDevice device, SamplerStateDescription samplerStateDescription) : base(device)
33  {
34  Description = samplerStateDescription;
35 
36  textureWrapS = samplerStateDescription.AddressU.ToOpenGL();
37  textureWrapT = samplerStateDescription.AddressV.ToOpenGL();
38 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
39  textureWrapR = samplerStateDescription.AddressW.ToOpenGL();
40 #endif
41  compareFunc = samplerStateDescription.CompareFunction.ToOpenGLDepthFunction();
42  borderColor = samplerStateDescription.BorderColor.ToArray();
43  // TODO: How to do MipLinear vs MipPoint?
44  switch (samplerStateDescription.Filter)
45  {
46  case TextureFilter.ComparisonMinMagLinearMipPoint:
47  case TextureFilter.MinMagLinearMipPoint:
48  minFilter = TextureMinFilter.Linear;
49  magFilter = TextureMagFilter.Linear;
50  break;
51  case TextureFilter.Anisotropic:
52  case TextureFilter.Linear:
53  minFilter = TextureMinFilter.LinearMipmapLinear;
54  magFilter = TextureMagFilter.Linear;
55  break;
56  case TextureFilter.MinPointMagMipLinear:
57  case TextureFilter.ComparisonMinPointMagMipLinear:
58  minFilter = TextureMinFilter.NearestMipmapLinear;
59  magFilter = TextureMagFilter.Linear;
60  break;
61  case TextureFilter.Point:
62  minFilter = TextureMinFilter.Nearest;
63  magFilter = TextureMagFilter.Nearest;
64  break;
65  default:
66  throw new NotImplementedException();
67  }
68 
69 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
70  // On OpenGL ES, we need to choose the appropriate min filter ourself if the texture doesn't contain mipmaps (done at PreDraw)
71  minFilterNoMipmap = minFilter;
72  if (minFilterNoMipmap == TextureMinFilter.LinearMipmapLinear)
73  minFilterNoMipmap = TextureMinFilter.Linear;
74  else if (minFilterNoMipmap == TextureMinFilter.NearestMipmapLinear)
75  minFilterNoMipmap = TextureMinFilter.Nearest;
76 #endif
77  }
78 
79  /// <inheritdoc/>
80  protected internal override bool OnRecreate()
81  {
82  base.OnRecreate();
83  return true;
84  }
85 
86  internal void Apply(bool hasMipmap, SamplerState oldSamplerState)
87  {
88 #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES
89  if (Description.MinMipLevel != oldSamplerState.Description.MinMipLevel)
90  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinLod, Description.MinMipLevel);
91  if (Description.MaxMipLevel != oldSamplerState.Description.MaxMipLevel)
92  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMaxLod, Description.MaxMipLevel);
93  if (textureWrapR != oldSamplerState.textureWrapR)
94  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapR, (int)textureWrapR);
95  if (borderColor != oldSamplerState.borderColor)
96  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, borderColor);
97  if (compareFunc != oldSamplerState.compareFunc)
98  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareFunc, (int)compareFunc);
99  if (Description.MipMapLevelOfDetailBias != oldSamplerState.Description.MipMapLevelOfDetailBias)
100  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureLodBias, Description.MipMapLevelOfDetailBias);
101 
102  if (minFilter != oldSamplerState.minFilter)
103  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)minFilter);
104 #else
105  // On OpenGL ES, we need to choose the appropriate min filter ourself if the texture doesn't contain mipmaps (done at PreDraw)
106  if (minFilter != oldSamplerState.minFilter)
107  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, hasMipmap ? (int)minFilter : (int)minFilterNoMipmap);
108 #endif
109 
110 #if !SILICONSTUDIO_PLATFORM_IOS
111  if (Description.MaxAnisotropy != oldSamplerState.Description.MaxAnisotropy)
112  GL.TexParameter(TextureTarget.Texture2D, (TextureParameterName)OpenTK.Graphics.ES20.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, Description.MaxAnisotropy);
113 #endif
114  if (magFilter != oldSamplerState.magFilter)
115  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)magFilter);
116  if (textureWrapS != oldSamplerState.textureWrapS)
117  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)textureWrapS);
118  if (textureWrapT != oldSamplerState.textureWrapT)
119  GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)textureWrapT);
120  }
121  }
122 }
123 #endif