1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // SamplerGL.cpp: Defines the rx::SamplerGL class, an implementation of SamplerImpl.
8 
9 #include "libANGLE/renderer/gl/SamplerGL.h"
10 
11 #include "libANGLE/renderer/gl/FunctionsGL.h"
12 #include "libANGLE/renderer/gl/StateManagerGL.h"
13 
14 namespace
15 {
16 
17 template <typename T>
SetSamplerParameter(const rx::FunctionsGL * functions,GLuint sampler,GLenum name,const T & value)18 inline void SetSamplerParameter(const rx::FunctionsGL *functions,
19                                 GLuint sampler,
20                                 GLenum name,
21                                 const T &value)
22 {
23     functions->samplerParameterf(sampler, name, static_cast<GLfloat>(value));
24 }
25 
SetSamplerParameter(const rx::FunctionsGL * functions,GLuint sampler,GLenum name,const angle::ColorGeneric & value)26 inline void SetSamplerParameter(const rx::FunctionsGL *functions,
27                                 GLuint sampler,
28                                 GLenum name,
29                                 const angle::ColorGeneric &value)
30 {
31     switch (value.type)
32     {
33         case angle::ColorGeneric::Type::Float:
34             functions->samplerParameterfv(sampler, name, &value.colorF.red);
35             break;
36         case angle::ColorGeneric::Type::Int:
37             functions->samplerParameterIiv(sampler, name, &value.colorI.red);
38             break;
39         case angle::ColorGeneric::Type::UInt:
40             functions->samplerParameterIuiv(sampler, name, &value.colorUI.red);
41             break;
42         default:
43             UNREACHABLE();
44             break;
45     }
46 }
47 
48 template <typename Getter, typename Setter>
SyncSamplerStateMember(const rx::FunctionsGL * functions,GLuint sampler,const gl::SamplerState & newState,gl::SamplerState & curState,GLenum name,Getter getter,Setter setter)49 static inline void SyncSamplerStateMember(const rx::FunctionsGL *functions,
50                                           GLuint sampler,
51                                           const gl::SamplerState &newState,
52                                           gl::SamplerState &curState,
53                                           GLenum name,
54                                           Getter getter,
55                                           Setter setter)
56 {
57     if ((curState.*getter)() != (newState.*getter)())
58     {
59         (curState.*setter)((newState.*getter)());
60         SetSamplerParameter(functions, sampler, name, (newState.*getter)());
61     }
62 }
63 }  // namespace
64 
65 namespace rx
66 {
67 
SamplerGL(const gl::SamplerState & state,const FunctionsGL * functions,StateManagerGL * stateManager)68 SamplerGL::SamplerGL(const gl::SamplerState &state,
69                      const FunctionsGL *functions,
70                      StateManagerGL *stateManager)
71     : SamplerImpl(state),
72       mFunctions(functions),
73       mStateManager(stateManager),
74       mAppliedSamplerState(),
75       mSamplerID(0)
76 {
77     mFunctions->genSamplers(1, &mSamplerID);
78 }
79 
~SamplerGL()80 SamplerGL::~SamplerGL()
81 {
82     mStateManager->deleteSampler(mSamplerID);
83     mSamplerID = 0;
84 }
85 
syncState(const gl::Context * context,const bool dirty)86 angle::Result SamplerGL::syncState(const gl::Context *context, const bool dirty)
87 {
88     if (!dirty)
89     {
90         return angle::Result::Continue;
91     }
92     // clang-format off
93     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_MIN_FILTER, &gl::SamplerState::getMinFilter, &gl::SamplerState::setMinFilter);
94     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_MAG_FILTER, &gl::SamplerState::getMagFilter, &gl::SamplerState::setMagFilter);
95     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_WRAP_S, &gl::SamplerState::getWrapS, &gl::SamplerState::setWrapS);
96     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_WRAP_T, &gl::SamplerState::getWrapT, &gl::SamplerState::setWrapT);
97     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_WRAP_R, &gl::SamplerState::getWrapR, &gl::SamplerState::setWrapR);
98     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_MAX_ANISOTROPY_EXT, &gl::SamplerState::getMaxAnisotropy, &gl::SamplerState::setMaxAnisotropy);
99     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_MIN_LOD, &gl::SamplerState::getMinLod, &gl::SamplerState::setMinLod);
100     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_MAX_LOD, &gl::SamplerState::getMaxLod, &gl::SamplerState::setMaxLod);
101     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_COMPARE_MODE, &gl::SamplerState::getCompareMode, &gl::SamplerState::setCompareMode);
102     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_COMPARE_FUNC, &gl::SamplerState::getCompareFunc, &gl::SamplerState::setCompareFunc);
103     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_SRGB_DECODE_EXT, &gl::SamplerState::getSRGBDecode, &gl::SamplerState::setSRGBDecode);
104     SyncSamplerStateMember(mFunctions, mSamplerID, mState, mAppliedSamplerState, GL_TEXTURE_BORDER_COLOR, &gl::SamplerState::getBorderColor, &gl::SamplerState::setBorderColor);
105     // clang-format on
106     return angle::Result::Continue;
107 }
108 
getSamplerID() const109 GLuint SamplerGL::getSamplerID() const
110 {
111     return mSamplerID;
112 }
113 }  // namespace rx
114