huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*-------------------------------------------------------------------------
 * drawElements Quality Program OpenGL ES Utilities
 * ------------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Context Info Class.
 *//*--------------------------------------------------------------------*/
 
#include "gluContextInfo.hpp"
#include "gluRenderContext.hpp"
#include "gluShaderProgram.hpp"
#include "glwFunctions.hpp"
#include "glwEnums.hpp"
 
#include <iterator>
#include <algorithm>
 
using std::vector;
using std::string;
using std::set;
 
namespace glu
{
 
class TryCompileProgram
{
public:
   // \note Assumes that shader pointer can be stored as is (eg. it is static data)
   TryCompileProgram (const char* vertexSource, const char* fragmentSource)
       : m_vertexSource    (vertexSource)
       , m_fragmentSource    (fragmentSource)
   {
   }
 
   bool operator() (const RenderContext& context) const
   {
       ShaderProgram program(context,
           ProgramSources() << VertexSource(m_vertexSource)
                            << FragmentSource(m_fragmentSource));
       return program.isOk();
   }
 
private:
   const char*    m_vertexSource;
   const char*    m_fragmentSource;
};
 
typedef CachedValue<bool, TryCompileProgram> IsProgramSupported;
 
// ES2-specific context info
class ES2ContextInfo : public ContextInfo
{
public:
                                       ES2ContextInfo                        (const RenderContext& context);
                                       ~ES2ContextInfo                        (void) {}
 
   bool                                isVertexUniformLoopSupported        (void) const    { return m_vertexUniformLoopsSupported.getValue(m_context);        }
   bool                                isVertexDynamicLoopSupported        (void) const    { return m_vertexDynamicLoopsSupported.getValue(m_context);        }
   bool                                isFragmentHighPrecisionSupported    (void) const    { return m_fragmentHighPrecisionSupported.getValue(m_context);    }
   bool                                isFragmentUniformLoopSupported        (void) const    { return m_fragmentUniformLoopsSupported.getValue(m_context);    }
   bool                                isFragmentDynamicLoopSupported        (void) const    { return m_fragmentDynamicLoopsSupported.getValue(m_context);    }
 
private:
   IsProgramSupported                    m_vertexUniformLoopsSupported;
   IsProgramSupported                    m_vertexDynamicLoopsSupported;
 
   IsProgramSupported                    m_fragmentHighPrecisionSupported;
   IsProgramSupported                    m_fragmentUniformLoopsSupported;
   IsProgramSupported                    m_fragmentDynamicLoopsSupported;
};
 
static const char* s_defaultVertexShader =
   "attribute highp vec4 a_position;\n"
   "void main (void) {\n"
   "    gl_Position = a_position;\n"
   "}\n";
static const char* s_defaultFragmentShader =
   "void main (void) {\n"
   "    gl_FragColor = vec4(1.0);\n"
   "}\n";
 
static const char* s_vertexUniformLoopsSupported =
   "attribute highp vec4    a_position;\n"
   "uniform int            u_numIters;\n"
   "void main (void) {\n"
   "    gl_Position = a_position;\n"
   "    for (int i = 0; i < u_numIters; i++)\n"
   "        gl_Position += vec4(0.1);\n"
   "}\n";
static const char* s_vertexDynamicLoopsSupported =
   "attribute highp vec4    a_position;\n"
   "uniform mediump float    a, b;\n"
   "void main (void) {\n"
   "    gl_Position = a_position;\n"
   "    int numIters = a < b ? int(3.0*b) : int(a_position.x);\n"
   "    for (int i = 0; i < numIters; i++)\n"
   "        gl_Position += vec4(0.1);\n"
   "}\n";
 
static const char* s_fragmentHighPrecisionSupported =
   "varying highp vec4        v_color;\n"
   "void main (void) {\n"
   "    highp float tmp = v_color.r;\n"
   "    gl_FragColor = v_color;\n"
   "}\n";
static const char* s_fragmentUniformLoopsSupported =
   "varying mediump vec4    v_color;\n"
   "uniform int            u_numIters;\n"
   "void main (void) {\n"
   "    gl_FragColor = v_color;\n"
   "    for (int i = 0; i < u_numIters; i++)\n"
   "        gl_FragColor += vec4(0.1);\n"
   "}\n";
static const char* s_fragmentDynamicLoopsSupported =
   "varying mediump vec4    v_color;\n"
   "uniform mediump float    a, b;\n"
   "void main (void) {\n"
   "    gl_FragColor = v_color;\n"
   "    int numIters = a < b ? int(3.0*b) : int(v_color.x);\n"
   "    for (int i = 0; i < numIters; i++)\n"
   "        gl_FragColor += vec4(0.1);\n"
   "}\n";
 
ES2ContextInfo::ES2ContextInfo (const RenderContext& context)
   : glu::ContextInfo                    (context)
   , m_vertexUniformLoopsSupported        (TryCompileProgram(s_vertexUniformLoopsSupported, s_defaultFragmentShader))
   , m_vertexDynamicLoopsSupported        (TryCompileProgram(s_vertexDynamicLoopsSupported, s_defaultFragmentShader))
   , m_fragmentHighPrecisionSupported    (TryCompileProgram(s_defaultVertexShader, s_fragmentHighPrecisionSupported))
   , m_fragmentUniformLoopsSupported    (TryCompileProgram(s_defaultVertexShader, s_fragmentUniformLoopsSupported))
   , m_fragmentDynamicLoopsSupported    (TryCompileProgram(s_defaultVertexShader, s_fragmentDynamicLoopsSupported))
{
}
 
static void split (vector<string>& dst, const string& src)
{
   size_t start = 0;
   size_t end     = string::npos;
 
   while ((end = src.find(' ', start)) != string::npos)
   {
       dst.push_back(src.substr(start, end-start));
       start = end+1;
   }
 
   if (start < end)
       dst.push_back(src.substr(start, end-start));
}
 
set<int> GetCompressedTextureFormats::operator() (const RenderContext& context) const
{
   const glw::Functions& gl = context.getFunctions();
 
   int numFormats = 0;
   gl.getIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numFormats);
 
   vector<int> formats(numFormats);
   if (numFormats > 0)
       gl.getIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, &formats[0]);
 
   GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS) failed");
 
   set<int> formatSet;
   std::copy(formats.begin(), formats.end(), std::inserter(formatSet, formatSet.begin()));
 
   return formatSet;
}
 
// ContextInfo
 
ContextInfo::ContextInfo (const RenderContext& context)
   : m_context(context)
{
   const glw::Functions& gl = context.getFunctions();
 
   if (context.getType().getAPI() == ApiType::es(2,0))
   {
       const char* result = (const char*)gl.getString(GL_EXTENSIONS);
       GLU_EXPECT_NO_ERROR(gl.getError(), "glGetString(GL_EXTENSIONS) failed");
 
       split(m_extensions, string(result));
   }
   else
   {
       int                numExtensions    = 0;
 
       gl.getIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
       GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_NUM_EXTENSIONS) failed");
 
       m_extensions.resize(numExtensions);
       for (int ndx = 0; ndx < numExtensions; ndx++)
           m_extensions[ndx] = (const char*)gl.getStringi(GL_EXTENSIONS, ndx);
       GLU_EXPECT_NO_ERROR(gl.getError(), "glGetStringi(GL_EXTENSIONS, ndx) failed");
   }
}
 
ContextInfo::~ContextInfo (void)
{
}
 
int ContextInfo::getInt (int param) const
{
   int val = -1;
   m_context.getFunctions().getIntegerv(param, &val);
   GLU_EXPECT_NO_ERROR(m_context.getFunctions().getError(), "glGetIntegerv() failed");
   return val;
}
 
bool ContextInfo::getBool (int param) const
{
   glw::GLboolean val = GL_FALSE;
   m_context.getFunctions().getBooleanv(param, &val);
   GLU_EXPECT_NO_ERROR(m_context.getFunctions().getError(), "glGetBooleanv() failed");
   return val != GL_FALSE;
}
 
const char* ContextInfo::getString (int param) const
{
   const char* str = (const char*)m_context.getFunctions().getString(param);
   GLU_EXPECT_NO_ERROR(m_context.getFunctions().getError(), "glGetString() failed");
   return str;
}
 
bool ContextInfo::isCompressedTextureFormatSupported (int format) const
{
   const set<int>& formats = m_compressedTextureFormats.getValue(m_context);
   return formats.find(format) != formats.end();
}
 
bool ContextInfo::isExtensionSupported (const char* name) const
{
   const std::vector<std::string>& extensions = getExtensions();
   return std::find(extensions.begin(), extensions.end(), name) != extensions.end();
}
 
ContextInfo* ContextInfo::create (const RenderContext& context)
{
   // ES2 uses special variant that checks support for various shader features
   // by trying to compile shader programs.
   if (context.getType().getAPI() == ApiType::es(2,0))
       return new ES2ContextInfo(context);
 
   return new ContextInfo(context);
}
 
} // glu