1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gpu_sampler_gles.h"
17 
18 #include <render/device/gpu_resource_desc.h>
19 #include <render/namespace.h>
20 
21 #include "gles/device_gles.h"
22 #include "gles/gl_functions.h"
23 #include "util/log.h"
24 
RENDER_BEGIN_NAMESPACE()25 RENDER_BEGIN_NAMESPACE()
26 GpuSamplerGLES::GpuSamplerGLES(Device& device, const GpuSamplerDesc& desc) : device_((DeviceGLES&)device), desc_(desc)
27 {
28     PLUGIN_ASSERT(device_.IsActive());
29     // NOTE: delay Create?
30     constexpr GLint magFilter[] = {
31         GL_NEAREST, // min is CORE_FILTER_NEAREST
32         GL_LINEAR   // min is CORE_FILTER_LINEAR
33     };
34     constexpr GLint minFilter[] = {
35         GL_NEAREST_MIPMAP_NEAREST, // min = CORE_FILTER_NEAREST mip = CORE_FILTER_NEAREST
36         GL_LINEAR_MIPMAP_NEAREST,  // min = CORE_FILTER_LINEAR mip = CORE_FILTER_NEAREST
37         GL_NEAREST_MIPMAP_LINEAR,  // min = CORE_FILTER_NEAREST mip = CORE_FILTER_LINEAR
38         GL_LINEAR_MIPMAP_LINEAR,   // min = CORE_FILTER_LINEAR mip = CORE_FILTER_LINEAR
39     };
40     constexpr GLint addressMode[] = {
41         GL_REPEAT,          // CORE_SAMPLER_ADDRESS_MODE_REPEAT = 0,
42         GL_MIRRORED_REPEAT, // CORE_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT = 1,
43         GL_CLAMP_TO_EDGE,   // CORE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE = 2,
44 
45         // requires gles 3.2
46         // CORE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER = 3, requires GL_EXT_texture_border_clamp
47         // if GLES 3.1
48         GL_CLAMP_TO_BORDER,
49 
50         // Not supported in gles
51         GL_CLAMP_TO_EDGE, // GL_MIRROR_CLAMP_TO_EDGE,//CORE_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE = 4,
52     };
53     constexpr GLint compareMode[] = {
54         GL_NEVER,    // CORE_COMPARE_OP_NEVER is 0,
55         GL_LESS,     // CORE_COMPARE_OP_LESS is 1,
56         GL_EQUAL,    // CORE_COMPARE_OP_EQUAL is 2,
57         GL_LEQUAL,   // CORE_COMPARE_OP_LESS_OR_EQUAL is 3,
58         GL_GREATER,  // CORE_COMPARE_OP_GREATER is 4,
59         GL_NOTEQUAL, // CORE_COMPARE_OP_NOT_EQUAL is 5,
60         GL_GEQUAL,   // CORE_COMPARE_OP_GREATER_OR_EQUAL is 6,
61         GL_ALWAYS    // CORE_COMPARE_OP_ALWAYS is 7,
62         // CORE_COMPARE_OP_MAX_ENUM is 0x7FFFFFFF
63     };
64 
65     glGenSamplers(1, &plat_.sampler);
66     glSamplerParameteri(plat_.sampler, GL_TEXTURE_MAG_FILTER, magFilter[desc.magFilter]);
67 
68     glSamplerParameteri(plat_.sampler, GL_TEXTURE_MIN_FILTER, minFilter[desc.minFilter + desc.mipMapMode * 2]);
69 
70     glSamplerParameteri(plat_.sampler, GL_TEXTURE_WRAP_S, addressMode[desc.addressModeU]);
71     glSamplerParameteri(plat_.sampler, GL_TEXTURE_WRAP_T, addressMode[desc.addressModeV]);
72     glSamplerParameteri(plat_.sampler, GL_TEXTURE_WRAP_R, addressMode[desc.addressModeW]);
73 
74     // "desc.mipLodBias" can not be set here on GLES, needs a shader work around if needed!
75     // "desc.enableAnisotropy" is NOT SUPPORTED! it requires EXT_texture_filter_anisotropic.
76     if (desc.enableCompareOp) {
77         glSamplerParameteri(plat_.sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
78         glSamplerParameteri(plat_.sampler, GL_TEXTURE_COMPARE_FUNC, compareMode[desc.compareOp]);
79     } else {
80         glSamplerParameteri(plat_.sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE);
81         glSamplerParameteri(plat_.sampler, GL_TEXTURE_COMPARE_FUNC, GL_ALWAYS);
82     }
83 
84     glSamplerParameterf(plat_.sampler, GL_TEXTURE_MIN_LOD, desc.minLod);
85     glSamplerParameterf(plat_.sampler, GL_TEXTURE_MAX_LOD, desc.maxLod);
86 
87     float color[4] = { 0.f };
88     switch (desc.borderColor) {
89         case CORE_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
90         case CORE_BORDER_COLOR_INT_TRANSPARENT_BLACK: {
91             color[0] = color[1] = color[2] = color[3] = 0.f;
92             break;
93         }
94         case CORE_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
95         case CORE_BORDER_COLOR_INT_OPAQUE_BLACK: {
96             color[0] = color[1] = color[2] = 0.f;
97             color[3] = 1.f;
98             break;
99         }
100         case CORE_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
101         case CORE_BORDER_COLOR_INT_OPAQUE_WHITE: {
102             color[0] = color[1] = color[2] = color[3] = 1.f;
103             break;
104         }
105         default:
106             color[0] = color[1] = color[2] = color[3] = 0.f;
107             break;
108     }
109     glSamplerParameterfv(plat_.sampler, GL_TEXTURE_BORDER_COLOR, color);
110 
111     // "desc.enableUnnormalizedCoordinates" is NOT SUPPORTED!
112 }
113 
~GpuSamplerGLES()114 GpuSamplerGLES::~GpuSamplerGLES()
115 {
116     if (plat_.sampler) {
117         PLUGIN_ASSERT(device_.IsActive());
118         device_.DeleteSampler(plat_.sampler);
119     }
120 }
121 
GetDesc() const122 const GpuSamplerDesc& GpuSamplerGLES::GetDesc() const
123 {
124     return desc_;
125 }
126 
GetPlatformData() const127 const GpuSamplerPlatformDataGL& GpuSamplerGLES::GetPlatformData() const
128 {
129     return plat_;
130 }
131 RENDER_END_NAMESPACE()
132