Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
glSamplerOject.cpp
Go to the documentation of this file.
1
2
4
7
8namespace Divide {
9
10namespace {
11 [[nodiscard]] inline gl46core::GLenum getMagFilterMode(const TextureFilter magFilter)
12 {
13 assert(magFilter != TextureFilter::COUNT);
14
15 return magFilter == TextureFilter::LINEAR ? gl46core::GL_LINEAR : gl46core::GL_NEAREST;
16 }
17
18 [[nodiscard]] inline gl46core::GLenum getMinFilterMode(const TextureFilter minFilter, const TextureMipSampling mipSampling)
19 {
20 assert(minFilter != TextureFilter::COUNT);
21 assert(mipSampling != TextureMipSampling::COUNT);
22
23 if (mipSampling == TextureMipSampling::NONE)
24 {
25 return getMagFilterMode(minFilter);
26 }
27
28 if (minFilter == TextureFilter::NEAREST)
29 {
30 return mipSampling == TextureMipSampling::LINEAR ? gl46core::GL_NEAREST_MIPMAP_LINEAR : gl46core::GL_NEAREST_MIPMAP_NEAREST;
31 }
32
33 return mipSampling == TextureMipSampling::LINEAR ? gl46core::GL_LINEAR_MIPMAP_LINEAR : gl46core::GL_LINEAR_MIPMAP_NEAREST;
34 }
35};
36
37gl46core::GLuint glSamplerObject::Construct(const SamplerDescriptor& descriptor)
38{
39 gl46core::GLuint samplerID = GL_NULL_HANDLE;
40
41 gl46core::glCreateSamplers(1, &samplerID);
42 gl46core::glSamplerParameterf(samplerID, gl46core::GL_TEXTURE_LOD_BIAS, descriptor._lod._bias);
43 gl46core::glSamplerParameterf(samplerID, gl46core::GL_TEXTURE_MIN_LOD, descriptor._lod._min);
44 gl46core::glSamplerParameterf(samplerID, gl46core::GL_TEXTURE_MAX_LOD, descriptor._lod._max);
45 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_MIN_FILTER, getMinFilterMode(descriptor._minFilter, descriptor._mipSampling));
46 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_MAG_FILTER, getMagFilterMode(descriptor._magFilter));
47 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_WRAP_S, to_U32(GLUtil::glWrapTable[to_U32(descriptor._wrapU)]));
48 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_WRAP_T, to_U32(GLUtil::glWrapTable[to_U32(descriptor._wrapV)]));
49 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_WRAP_R, to_U32(GLUtil::glWrapTable[to_U32(descriptor._wrapW)]));
50
51 constexpr gl46core::GLint black_transparent_int[] = { 0, 0, 0, 0 };
52 constexpr gl46core::GLint black_opaque_int[] = { 0, 0, 0, 1 };
53 constexpr gl46core::GLint white_opaque_int[] = { 1, 1, 1, 1 };
54 constexpr gl46core::GLfloat black_transparent_float[] = { 0.f, 0.f, 0.f, 0.f };
55 constexpr gl46core::GLfloat black_opaque_float[] = { 0.f, 0.f, 0.f, 1.f };
56 constexpr gl46core::GLfloat white_opaque_float[] = { 1.f, 1.f, 1.f, 1.f };
57
58 switch (descriptor._borderColour)
59 {
60 default:
61 case TextureBorderColour::TRANSPARENT_BLACK_INT: gl46core::glSamplerParameteriv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, black_transparent_int); break;
62 case TextureBorderColour::TRANSPARENT_BLACK_F32: gl46core::glSamplerParameterfv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, black_transparent_float); break;
63 case TextureBorderColour::OPAQUE_BLACK_INT : gl46core::glSamplerParameteriv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, black_opaque_int); break;
64 case TextureBorderColour::OPAQUE_BLACK_F32 : gl46core::glSamplerParameterfv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, black_opaque_float); break;
65 case TextureBorderColour::OPAQUE_WHITE_INT : gl46core::glSamplerParameteriv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, white_opaque_int); break;
66 case TextureBorderColour::OPAQUE_WHITE_F32 : gl46core::glSamplerParameterfv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, white_opaque_float); break;
68 {
69 gl46core::GLuint customColour[] =
70 {
71 descriptor._customBorderColour.r,
72 descriptor._customBorderColour.g,
73 descriptor._customBorderColour.b,
74 descriptor._customBorderColour.a
75 };
76 gl46core::glSamplerParameterIuiv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, customColour);
77 } break;
79 {
80 const FColour4 fColour = Util::ToFloatColour(descriptor._customBorderColour);
81 gl46core::glSamplerParameterfv(samplerID, gl46core::GL_TEXTURE_BORDER_COLOR, fColour._v);
82 } break;
83 }
84
86 {
87 gl46core::glSamplerParameteri( samplerID, gl46core::GL_TEXTURE_COMPARE_FUNC, to_base( GLUtil::glCompareFuncTable[to_base( descriptor._depthCompareFunc )] ) );
88 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_COMPARE_MODE, to_base( gl46core::GL_COMPARE_REF_TO_TEXTURE));
89 }
90 else
91 {
92 gl46core::glSamplerParameteri(samplerID, gl46core::GL_TEXTURE_COMPARE_MODE, to_base( gl46core::GL_NONE));
93 }
94
95 const F32 maxAnisotropy = std::min(to_F32(descriptor._anisotropyLevel), to_F32(GFXDevice::GetDeviceInformation()._maxAnisotropy));
96 if ( descriptor._mipSampling != TextureMipSampling::NONE && maxAnisotropy > 0.f )
97 {
98 gl46core::glSamplerParameterf(samplerID, gl46core::GL_TEXTURE_MAX_ANISOTROPY, maxAnisotropy);
99 }
100 else
101 {
102 gl46core::glSamplerParameterf( samplerID, gl46core::GL_TEXTURE_MAX_ANISOTROPY, 1.f );
103 }
104
105 if constexpr ( Config::ENABLE_GPU_VALIDATION )
106 {
107 gl46core::glObjectLabel( gl46core::GL_SAMPLER,
108 samplerID,
109 -1,
110 Util::StringFormat("SAMPLER_{}", GetHash(descriptor)).c_str() );
111 }
112
113 return samplerID;
114}
115
116void glSamplerObject::Destruct( gl46core::GLuint& handle)
117{
118 if (handle != GL_NULL_HANDLE )
119 {
120 gl46core::glDeleteSamplers(1, &handle);
121 handle = GL_NULL_HANDLE;
122 }
123}
124
125} //namespace Divide
static const DeviceInformation & GetDeviceInformation() noexcept
Definition: GFXDevice.inl:158
static void Destruct(gl46core::GLuint &handle)
static gl46core::GLuint Construct(const SamplerDescriptor &descriptor)
constexpr bool ENABLE_GPU_VALIDATION
Error callbacks, validations, buffer checks, etc. are controlled by this flag. Heavy performance impa...
Definition: config.h:192
std::array< gl46core::GLenum, to_base(TextureWrap::COUNT)> glWrapTable
Definition: glResources.cpp:95
std::array< gl46core::GLenum, to_base(ComparisonFunction::COUNT)> glCompareFuncTable
Definition: glResources.cpp:88
Str StringFormat(const char *fmt, Args &&...args)
FColour4 ToFloatColour(const UColour4 &byteColour) noexcept
Definition: MathHelper.cpp:268
gl46core::GLenum getMinFilterMode(const TextureFilter minFilter, const TextureMipSampling mipSampling)
gl46core::GLenum getMagFilterMode(const TextureFilter magFilter)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
constexpr F32 to_F32(const T value)
size_t GetHash(const PropertyDescriptor< T > &descriptor) noexcept
Definition: Resource.inl:40
constexpr gl46core::GLuint GL_NULL_HANDLE
Invalid object value. Used to compare handles and determine if they were properly created.
Definition: glResources.h:105
@ COUNT
Place all properties above this.
constexpr auto to_base(const Type value) -> Type
TextureFilter _minFilter
Texture filtering mode.
TextureWrap _wrapU
Texture wrap mode (Or S-R-T)
U8 _anisotropyLevel
The value must be in the range [0...255] and is automatically clamped by the max HW supported level.
ComparisonFunction _depthCompareFunc
Used for depth comparison (COUNT = disabled)
TextureBorderColour _borderColour
Used with CLAMP_TO_BORDER as the background colour outside of the texture border.
TextureMipSampling _mipSampling
UColour4 _customBorderColour
Used with custom border colours.
F32 _min
OpenGL eg: used by TEXTURE_MIN_LOD and TEXTURE_MAX_LOD.
F32 _bias
OpenGL eg: used by TEXTURE_LOD_BIAS.