2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "test-graphics-sampler.h"
21 std::map<uint32_t, GLint> TestGraphicsSampler::mParamCache;
23 TestGraphicsSampler::TestGraphicsSampler(TestGlAbstraction& glAbstraction, const Graphics::SamplerCreateInfo& createInfo)
24 : mGlAbstraction(glAbstraction),
25 mCreateInfo(createInfo)
29 GLint TestGraphicsSampler::FilterModeToGL(Graphics::SamplerFilter filterMode)
33 case Graphics::SamplerFilter::NEAREST:
37 case Graphics::SamplerFilter::LINEAR:
45 GLint TestGraphicsSampler::FilterModeToGL(Graphics::SamplerFilter filterMode, Graphics::SamplerMipmapMode mipmapMode)
47 if(filterMode == Graphics::SamplerFilter::NEAREST)
51 case Graphics::SamplerMipmapMode::NONE:
53 case Graphics::SamplerMipmapMode::NEAREST:
54 return GL_NEAREST_MIPMAP_NEAREST;
55 case Graphics::SamplerMipmapMode::LINEAR:
56 return GL_NEAREST_MIPMAP_LINEAR;
63 case Graphics::SamplerMipmapMode::NONE:
65 case Graphics::SamplerMipmapMode::NEAREST:
66 return GL_LINEAR_MIPMAP_NEAREST;
67 case Graphics::SamplerMipmapMode::LINEAR:
68 return GL_LINEAR_MIPMAP_LINEAR;
75 * @brief Convert from a WrapMode to its corresponding GL enumeration
76 * @param[in] wrapMode The wrap mode
77 * @param[in] defaultWrapMode The mode to use if WrapMode is Default
78 * @return The equivalent GL wrap mode
80 GLint TestGraphicsSampler::WrapModeToGL(Graphics::SamplerAddressMode wrapMode)
84 case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
86 return GL_CLAMP_TO_EDGE;
88 case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
90 return GL_CLAMP_TO_EDGE;
92 case Graphics::SamplerAddressMode::REPEAT:
96 case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
98 return GL_MIRRORED_REPEAT;
100 case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
102 return GL_MIRRORED_REPEAT;
108 void TestGraphicsSampler::Apply(GLuint target)
110 SetTexParameter(mGlAbstraction, target, GL_TEXTURE_MIN_FILTER, FilterModeToGL(mCreateInfo.minFilter, mCreateInfo.mipMapMode));
111 SetTexParameter(mGlAbstraction, target, GL_TEXTURE_MAG_FILTER, FilterModeToGL(mCreateInfo.magFilter));
112 SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_S, WrapModeToGL(mCreateInfo.addressModeU));
113 SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_T, WrapModeToGL(mCreateInfo.addressModeV));
114 if(target == GL_TEXTURE_CUBE_MAP)
116 TestGraphicsSampler::SetTexParameter(mGlAbstraction, target, GL_TEXTURE_WRAP_R, WrapModeToGL(mCreateInfo.addressModeW));
120 uint32_t TestGraphicsSampler::GetTexParamHash(TestGlAbstraction& glAbstraction, GLuint target, GLenum pname)
122 uint32_t targetFlags = 0;
128 case GL_TEXTURE_CUBE_MAP:
137 case GL_TEXTURE_WRAP_S:
138 targetFlags |= (0x01) << 2;
140 case GL_TEXTURE_WRAP_T:
141 targetFlags |= (0x02) << 2;
143 case GL_TEXTURE_WRAP_R:
144 targetFlags |= (0x03) << 2;
146 case GL_TEXTURE_MAG_FILTER:
147 targetFlags |= (0x04) << 2;
149 case GL_TEXTURE_MIN_FILTER:
150 targetFlags |= (0x05) << 2;
153 targetFlags |= (0x07) << 2;
156 auto& textures = glAbstraction.GetBoundTextures(glAbstraction.GetActiveTextureUnit());
157 targetFlags |= (textures.back() << 5);
162 void TestGraphicsSampler::SetTexParameter(TestGlAbstraction& glAbstraction, GLuint target, GLenum pname, GLint value)
164 // Works on the currently active texture
166 uint32_t hash = GetTexParamHash(glAbstraction, target, pname);
168 if(mParamCache.find(hash) != mParamCache.end())
170 if(mParamCache[hash] != value)
172 mParamCache[hash] = value;
173 glAbstraction.TexParameteri(target, pname, value);
178 mParamCache[hash] = value;
179 glAbstraction.TexParameteri(target, pname, value);