-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sampler.cs
161 lines (140 loc) · 4.35 KB
/
Sampler.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
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.Text;
namespace GLGraphics
{
public class Sampler : GraphicsResource
{
public Sampler() : base(GLObjectType.Sampler)
{
}
float maxAniso;
private float anisotropyLevel = 16f;
public float AnisotropyLevel
{
get
{
if (maxAniso == 0)
{
maxAniso = GL.GetFloat((GetPName)All.MaxTextureMaxAnisotropy);
}
if (anisotropyLevel > maxAniso)
{
anisotropyLevel = maxAniso;
}
return anisotropyLevel;
}
set
{
if (maxAniso == 0)
{
maxAniso = GL.GetFloat((GetPName)All.MaxTextureMaxAnisotropy);
}
anisotropyLevel = MathF.Min(MathF.Max(1, value), maxAniso);
SetAnisotropy();
}
}
private TextureFilter filter = TextureFilter.Nearest;
public TextureFilter Filter
{
get => filter; set
{
filter = value;
SetTextureFilter();
}
}
private TextureWrapMode wrapModeU = TextureWrapMode.Repeat;
public TextureWrapMode WrapModeU
{
get => wrapModeU; set
{
wrapModeU = value;
SetWrapModeS();
}
}
private TextureWrapMode wrapModeV = TextureWrapMode.Repeat;
public TextureWrapMode WrapModeV
{
get => wrapModeV; set
{
wrapModeV = value;
SetWrapModeT();
}
}
private TextureWrapMode wrapModeW = TextureWrapMode.Repeat;
public TextureWrapMode WrapModeW
{
get => wrapModeW; set
{
wrapModeW = value;
SetWrapModeR();
}
}
TextureMinFilter GetMinFilter()
{
return filter switch
{
TextureFilter.Nearest => TextureMinFilter.NearestMipmapLinear,
TextureFilter.Bilinear => TextureMinFilter.LinearMipmapNearest,
TextureFilter.Trilinear => TextureMinFilter.LinearMipmapLinear,
_ => throw new Exception("Invalid filter"),
};
}
TextureMagFilter GetMagFilter()
{
return filter switch
{
TextureFilter.Nearest => TextureMagFilter.Nearest,
TextureFilter.Bilinear => TextureMagFilter.Linear,
TextureFilter.Trilinear => TextureMagFilter.Linear,
_ => throw new Exception("Invalid filter"),
};
}
void SetTextureFilter()
{
SetParameter(SamplerParameterName.TextureMinFilter, (int)GetMinFilter());
SetParameter(SamplerParameterName.TextureMagFilter, (int)GetMagFilter());
}
void SetAnisotropy()
{
SetParameter((SamplerParameterName)All.TextureMaxAnisotropy, AnisotropyLevel);
}
void SetWrapModeS()
{
SetParameter(SamplerParameterName.TextureWrapS, (int)wrapModeU);
}
void SetWrapModeT()
{
SetParameter(SamplerParameterName.TextureWrapT, (int)wrapModeV);
}
void SetWrapModeR()
{
SetParameter(SamplerParameterName.TextureWrapR, (int)wrapModeW);
}
public void Bind(int index = 0)
{
GL.BindSampler(index, Handle);
}
public static void Unbind(int index = 0)
{
GL.BindSampler(index, 0);
}
public void SetParameter(SamplerParameterName param, int value)
{
GL.SamplerParameter(Handle, param, value);
}
public void SetParameter(SamplerParameterName param, float value)
{
GL.SamplerParameter(Handle, param, value);
}
public long GetTextureSamplerHandle(TextureBase texture)
{
return GL.Arb.GetTextureSamplerHandle(texture.Handle, Handle);
}
public long GetTextureSamplerHandle(int texture)
{
return GL.Arb.GetTextureSamplerHandle(texture, Handle);
}
}
}