forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamplerStateInfo.cs
210 lines (181 loc) · 6.4 KB
/
SamplerStateInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace TwoMGFX
{
public class SamplerStateInfo
{
private SamplerState _state;
private bool _dirty;
private TextureFilterType _minFilter;
private TextureFilterType _magFilter;
private TextureFilterType _mipFilter;
private TextureAddressMode _addressU;
private TextureAddressMode _addressV;
private TextureAddressMode _addressW;
private Color _borderColor;
private int _maxAnisotropy;
private int _maxMipLevel;
private float _mipMapLevelOfDetailBias;
public SamplerStateInfo()
{
// NOTE: These match the defaults of SamplerState.
_minFilter = TextureFilterType.Linear;
_magFilter = TextureFilterType.Linear;
_mipFilter = TextureFilterType.Linear;
_addressU = TextureAddressMode.Wrap;
_addressV = TextureAddressMode.Wrap;
_addressW = TextureAddressMode.Wrap;
_borderColor = Color.White;
_maxAnisotropy = 4;
_maxMipLevel = 0;
_mipMapLevelOfDetailBias = 0.0f;
}
public string Name { get; set; }
public string TextureName { get; set; }
public TextureFilterType MinFilter
{
set
{
_minFilter = value;
_dirty = true;
}
}
public TextureFilterType MagFilter
{
set
{
_magFilter = value;
_dirty = true;
}
}
public TextureFilterType MipFilter
{
set
{
_mipFilter = value;
_dirty = true;
}
}
public TextureFilterType Filter
{
set
{
_minFilter = _magFilter = _mipFilter = value;
_dirty = true;
}
}
public TextureAddressMode AddressU
{
set
{
_addressU = value;
_dirty = true;
}
}
public TextureAddressMode AddressV
{
set
{
_addressV = value;
_dirty = true;
}
}
public TextureAddressMode AddressW
{
set
{
_addressW = value;
_dirty = true;
}
}
public Color BorderColor
{
set
{
_borderColor = value;
_dirty = true;
}
}
public int MaxAnisotropy
{
set
{
_maxAnisotropy = value;
_dirty = true;
}
}
public int MaxMipLevel
{
set
{
_maxMipLevel = value;
_dirty = true;
}
}
public float MipMapLevelOfDetailBias
{
set
{
_mipMapLevelOfDetailBias = value;
_dirty = true;
}
}
private void UpdateSamplerState()
{
// Get the existing state or create it.
if (_state == null)
_state = new SamplerState();
_state.AddressU = _addressU;
_state.AddressV = _addressV;
_state.AddressW = _addressW;
_state.BorderColor = _borderColor;
_state.MaxAnisotropy = _maxAnisotropy;
_state.MaxMipLevel = _maxMipLevel;
_state.MipMapLevelOfDetailBias = _mipMapLevelOfDetailBias;
// Figure out what kind of filter to set based on each
// individual min, mag, and mip filter settings.
//
// NOTE: We're treating "None" and "Point" the same here
// and disabling mipmapping further below.
//
if (_minFilter == TextureFilterType.Anisotropic)
_state.Filter = TextureFilter.Anisotropic;
else if (_minFilter == TextureFilterType.Linear && _magFilter == TextureFilterType.Linear && _mipFilter == TextureFilterType.Linear)
_state.Filter = TextureFilter.Linear;
else if (_minFilter == TextureFilterType.Linear && _magFilter == TextureFilterType.Linear && _mipFilter <= TextureFilterType.Point)
_state.Filter = TextureFilter.LinearMipPoint;
else if (_minFilter == TextureFilterType.Linear && _magFilter <= TextureFilterType.Point && _mipFilter == TextureFilterType.Linear)
_state.Filter = TextureFilter.MinLinearMagPointMipLinear;
else if (_minFilter == TextureFilterType.Linear && _magFilter <= TextureFilterType.Point && _mipFilter <= TextureFilterType.Point)
_state.Filter = TextureFilter.MinLinearMagPointMipPoint;
else if (_minFilter <= TextureFilterType.Point && _magFilter == TextureFilterType.Linear && _mipFilter == TextureFilterType.Linear)
_state.Filter = TextureFilter.MinPointMagLinearMipLinear;
else if (_minFilter <= TextureFilterType.Point && _magFilter == TextureFilterType.Linear && _mipFilter <= TextureFilterType.Point)
_state.Filter = TextureFilter.MinPointMagLinearMipPoint;
else if (_minFilter <= TextureFilterType.Point && _magFilter <= TextureFilterType.Point && _mipFilter <= TextureFilterType.Point)
_state.Filter = TextureFilter.Point;
else if (_minFilter <= TextureFilterType.Point && _magFilter <= TextureFilterType.Point && _mipFilter == TextureFilterType.Linear)
_state.Filter = TextureFilter.PointMipLinear;
// Do we need to disable mipmapping?
if (_mipFilter == TextureFilterType.None)
{
// TODO: This is the only option we have right now for
// disabling mipmapping. We should add support for MinLod
// and MaxLod which potentially does a better job at this.
_state.MipMapLevelOfDetailBias = -16.0f;
_state.MaxMipLevel = 0;
}
_dirty = false;
}
public SamplerState State
{
get
{
if (_dirty)
UpdateSamplerState();
return _state;
}
}
}
}