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