gpucapabilities.h 15.4 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
/**********************************************************************
 *                                                                    *
3
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
4
5
6
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
schultezub's avatar
schultezub committed
7
 *                                                                    *
8
9
10
11
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
schultezub's avatar
schultezub committed
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
schultezub's avatar
schultezub committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29
30
#ifndef CGT_GPUCAPABILITIES_H
#define CGT_GPUCAPABILITIES_H
schultezub's avatar
schultezub committed
31
32

#include <string>
33
#include <vector>
schultezub's avatar
schultezub committed
34

35
#include "cgt/singleton.h"
36
#include "cgt/cgt_gl.h"
37
#include "cgt/types.h"
schultezub's avatar
schultezub committed
38

39
namespace cgt {
schultezub's avatar
schultezub committed
40
41
42

class GpuCapabilities;
#ifdef DLL_TEMPLATE_INST
43
template class CGT_API Singleton<GpuCapabilities>;
schultezub's avatar
schultezub committed
44
45
46
#endif

/**
47
 * This cgt-Singleton provides information about the graphics system.
schultezub's avatar
schultezub committed
48
49
50
51
52
53
54
55
56
57
58
59
 * This information includes:
 *  - Operating system
 *  - OpenGL version
 *  - Supported OpenGL extensions
 *  - GPU vendor
 *  - Texturing and shader capabilities
 *
 * All data except the operating system information is exclusively retrieved
 * through the OpenGL API and can thus be regarded as reliable.
 *
 * The global identifier of this class' singleton is <tt>GpuCaps</tt>.
 */
60
class CGT_API GpuCapabilities : public Singleton<GpuCapabilities> {
schultezub's avatar
schultezub committed
61
62
63
64
65
public:

    /**
     * Specifies the version
     * of the OpenGL/glsl implementation.
66
     * CGT_GL_VERSION_x_y denotes OpenGL version x.y.
schultezub's avatar
schultezub committed
67
     *
68
     * CGT prefix is necessary due to name clashes with glew.
schultezub's avatar
schultezub committed
69
     */
70
    class CGT_API GlVersion {
schultezub's avatar
schultezub committed
71
        public:
72
        explicit GlVersion(int major = 0, int minor = 0, int release = 0);
schultezub's avatar
schultezub committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

        /**
         * Parse OpenGL version string as specified:
         *
         * The GL_VERSION and GL_SHADING_LANGUAGE_VERSION strings begin with a version number.
         * The version number uses one of these forms:
         *      major_number.minor_number
         *      major_number.minor_number.release_number
         *
         * Vendor-specific information may follow the version number.
         * Its format depends on the implementation, but a space always separates the version number and the vendor-specific information.
         */
        bool parseVersionString(const std::string& st);

        int major_;
        int minor_;
        int release_;

        int major() const { return major_; }
        int minor() const { return minor_; }
        int release() const { return release_; }

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        CGT_API friend bool operator==(const GlVersion& x, const GlVersion& y);
        CGT_API friend bool operator!=(const GlVersion& x, const GlVersion& y);
        CGT_API friend bool operator<(const GlVersion& x, const GlVersion& y);
        CGT_API friend bool operator<=(const GlVersion& x, const GlVersion& y);
        CGT_API friend bool operator>(const GlVersion& x, const GlVersion& y);
        CGT_API friend bool operator>=(const GlVersion& x, const GlVersion& y);
        CGT_API friend std::ostream& operator<<(std::ostream& s, const GlVersion& v);

        static const GlVersion CGT_GL_VERSION_1_0;
        static const GlVersion CGT_GL_VERSION_1_1;
        static const GlVersion CGT_GL_VERSION_1_2;
        static const GlVersion CGT_GL_VERSION_1_3;
        static const GlVersion CGT_GL_VERSION_1_4;
        static const GlVersion CGT_GL_VERSION_1_5;
        static const GlVersion CGT_GL_VERSION_2_0;
        static const GlVersion CGT_GL_VERSION_2_1;
        static const GlVersion CGT_GL_VERSION_3_0;
        static const GlVersion CGT_GL_VERSION_3_1;
        static const GlVersion CGT_GL_VERSION_3_2;
        static const GlVersion CGT_GL_VERSION_3_3;
        static const GlVersion CGT_GL_VERSION_4_0;
        static const GlVersion CGT_GL_VERSION_4_1;
        static const GlVersion CGT_GL_VERSION_4_2;
118
119
120
        static const GlVersion CGT_GL_VERSION_4_3;
        static const GlVersion CGT_GL_VERSION_4_4;
        static const GlVersion CGT_GL_VERSION_4_5;
schultezub's avatar
schultezub committed
121
122
123
124
125
126
127
128
129
130

        static const GlVersion SHADER_VERSION_110; ///< GLSL version 1.10
        static const GlVersion SHADER_VERSION_120; ///< GLSL version 1.20
        static const GlVersion SHADER_VERSION_130; ///< GLSL version 1.30
        static const GlVersion SHADER_VERSION_140; ///< GLSL version 1.40
        static const GlVersion SHADER_VERSION_150; ///< GLSL version 1.50
        static const GlVersion SHADER_VERSION_330; ///< GLSL version 3.30
        static const GlVersion SHADER_VERSION_400; ///< GLSL version 4.00
        static const GlVersion SHADER_VERSION_410; ///< GLSL version 4.10
        static const GlVersion SHADER_VERSION_420; ///< GLSL version 4.20
131
132
133
        static const GlVersion SHADER_VERSION_430; ///< GLSL version 4.30
        static const GlVersion SHADER_VERSION_440; ///< GLSL version 4.40
        static const GlVersion SHADER_VERSION_450; ///< GLSL version 4.50
schultezub's avatar
schultezub committed
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
    };


    /**
     * Identifies the vendor of the GPU
     * (not the graphics board's vendor).
     */
    enum GpuVendor {
        GPU_VENDOR_NVIDIA,
        GPU_VENDOR_ATI,
        GPU_VENDOR_INTEL,
        GPU_VENDOR_UNKNOWN
    };

    /**
     * Defines the DirectX shader model
     * supported by the GPU. This value
     * can be used to derive information
     * about the GPU's GLSL shader capabilities.
     * The shader model is fully downwards compatible.
     */
    enum ShaderModel {
        SHADER_MODEL_UNKNOWN,
        SHADER_MODEL_2,     ///< implied by OpenGL version 2.0
        SHADER_MODEL_3,     ///< extension NV_vertex_program3 or ATI_shader_texture_lod
        SHADER_MODEL_4,     ///< extension ARB_geometry_shader4
        SHADER_MODEL_5      ///< extension ARB_tessellation_shader
    };

    /**
     * Identifies the used operating system.
     */
    enum OSVersion {
        OS_UNKNOWN,
        OS_WIN_2000,
        OS_WIN_XP,
        OS_WIN_VISTA,
        OS_WIN_SERVER_2003,
        OS_WIN_SERVER_2008,
        OS_WIN_7,
174
175
176
        OS_WIN_8,
        OS_WIN_8_1,
        OS_WIN_10,
schultezub's avatar
schultezub committed
177
178
179
180
181
182
183
184
185
186
        OS_POSIX     ///< For Linux and other POSIX-like OS. Have a look at getOSVersionString for details.
    };

    /**
     * Creates an object for the detection of graphics system properties. If detectCapabilities
     * is set to false, the capabilities of the graphics card aren't detected right away in the
     * constructor. This way you can use GpuCapabilitiesWindows to detect the amount of memory
     * on the graphics card before initGL() is called. Otherwise GpuCapabilities tries to
     * detect GL values while initGL() isn't called yet and produces a crash.
     */
187
    explicit GpuCapabilities(bool detectCaps = true);
schultezub's avatar
schultezub committed
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

    virtual ~GpuCapabilities() {}

    /**
     * Returns the OpenGL version implemented by the driver.
     *
     * @see GlVersion
     */
    GlVersion getGlVersion();

    /**
     * Returns whether a certain OpenGL version is supported.
     *
     * @param version the OpenGL version to check
     *
     * @see GlVersion
     */
    bool isOpenGlVersionSupported(GlVersion version);

    /**
     * Returns the vendor of the GPU.
     *
     * @see GpuVendor
     */
    GpuVendor getVendor();

    /**
     * Returns the vendor of the GPU.
     *
     * @see GpuVendor
     */
    std::string getVendorAsString();

    /**
     * Returns wether a certain OpenGL extension
     * is supported by this implementation. The
     * check is done by parsing the OpenGL
     * extensions-string provided by the graphics driver.
     *
     * @param extension the exact name string of the extension
     *      as found in http://www.opengl.org/registry/
     */
    bool isExtensionSupported(std::string extension);

    /**
     * Returns the complete OpenGL version string
     * retrieved by <tt>glGetString(GL_VERSION)</tt>.
     */
    std::string getGlVersionString();

    /**
     * Returns the complete OpenGL vendor string
     * retrieved by <tt>glGetString(GL_VENDOR)</tt>.
     */
    std::string getGlVendorString();

    /**
     * Returns the complete OpenGL renderer string
     * retrieved by <tt>glGetString(GL_RENDERER)</tt>.
     */
    std::string getGlRendererString();

    /**
     * Returns the complete OpenGL extensions-string
252
     * retrieved by <tt>glGetStringi(GL_EXTENSIONS)</tt>.
schultezub's avatar
schultezub committed
253
     * This strings contains all OpenGL extensions supported
254
     * by this OpenGL implementation
schultezub's avatar
schultezub committed
255
     */
256
    const std::vector<std::string>& getGlExtensions();
schultezub's avatar
schultezub committed
257
258
259
260
261
262
263
264

    /**
    * Returns the complete Shading Language Version string
    * retrieved by <tt>glGetString(GL_SHADING_LANGUAGE_VERSION)</tt>.
    */
    std::string getShadingLanguageVersionString();

    /**
265
     * Returns whether shaders are supported, which
schultezub's avatar
schultezub committed
266
267
268
269
270
     * is true for OpenGL version 2.0 or later.
     */
    bool areShadersSupported();

    /**
271
272
273
274
    * Returns whether shaders are supported, which
    * is true for OpenGL version 4.3 or later.
    */
    bool areComputeShadersSupported();
275

276
277
    /**
     * Returns whether the ARB shader extensions
schultezub's avatar
schultezub committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
     * are supported (GL_ARB_vertex_program and
     * GL_ARB_fragment_program).
     *
     * \warning If you want to use shaders based on these
     * extensions, you have call the ARB variants of the
     * shader functions, e.g. <tt>glCompileShaderARB</tt> instead of
     * <tt>glCompileShader</tt>
     */
    bool areShadersSupportedARB();

    /**
    * Returns the GLSL shading language version
    * supported by the GPU.
    *
    * @see GlVersion
    */
    GlVersion getShaderVersion();

    /**
     * Returns the DirectX shader model
     * supported by the GPU.
     *
     * @see ShaderModel
     */
    ShaderModel getShaderModel();

    /**
     * Returns wether a certain shader model
     * is supported.
     *
     * @param shaderModel the shader model to check
     */
    bool isShaderModelSupported(ShaderModel shaderModel);

    /**
     * Returns the maximal side length of 1D and 2D textures.
     *
     * @see getMax3DTextureSize
     */
    int getMaxTextureSize();

    /**
     * Returns wether 3D textures are supported.
     * This is the case for OpenGL version 1.2
     * and later.
     */
    bool is3DTexturingSupported();

    /**
     * Returns the maximal side length
     * of 3D textures. If 3D texturing
     * is not supported, 0 is returned.
     */
    int getMax3DTextureSize();

    /**
     * Returns the number of texture units
     * provided by the GPU.
     */
    int getNumTextureUnits();

339
340
341
342
343
344
    /**
    * Returns the number of image units
    * provided by the GPU.
    */
    int getNumImageUnits();

schultezub's avatar
schultezub committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    /**
     * Returns wether non-power-of-two textures
     * are supported (extension GL_ARB_texture_non_power_of_two).
     */
    bool isNpotSupported();

    /**
     * Returns wether texture rectangles
     * are supported (extension GL_ARB_texture_rectangle).
     */
    bool areTextureRectanglesSupported();

    /**
     * Returns wether anisotropic texture filtering
     * is supported (extension GL_EXT_texture_filter_anisotropic).
     */
    bool isAnisotropicFilteringSupported();

    /**
     * Returns the maximum anisotropy. If
     * anisotropic texture filtering is not
     * supported, 1.0 is returned.
     */
    float getMaxTextureAnisotropy();

    /**
     * Returns wether texture compression
     * is supported (extension GL_ARB_texture_compression).
     */
    bool isTextureCompressionSupported();

    /**
     * Returns wether FramebufferObjects (FBOs)
     * are supported (extension GL_EXT_framebuffer_object).
     */
    bool areFramebufferObjectsSupported();

    ///Returns the maximal number of color attachments for a FBO
    int getMaxColorAttachments();

    /**
     * Overrides the detected GLSL language version.
     *
     * @return false, if the passed version string could not be parsed.
     *      In this case, the detected GLSL version is kept.
     */
    bool overrideGLSLVersion(const std::string& versionString);

    /**
     * Writes the most important GPU features to the
     * log (debug-level info).
     *
     * @param extensionsString determines wether the
     *      extensions string is also written. This is disabled by default
     *      since the extensions string is usually very long.
     * @param osString determines wether the operating system describing string
     *      is also written
     */
    virtual void logCapabilities(bool extensionsString = false, bool osString = true);

    /**
     * Get the OS version.
407
408
409
     * Note: On windows > 8, still windows 8 will be reported unless the application is 
     * manifested properly (not easily possible with cmake).
     * See https://msdn.microsoft.com/de-de/library/windows/desktop/ms724451 for more info.
schultezub's avatar
schultezub committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
     */
    OSVersion getOSVersion();

    /**
     * Get the OS version as string.
     */
    std::string getOSVersionString();

protected:
    /**
     * Is called by the constructor and performs the
     * complete hardware detection. The results
     * are internally stored.
     */
    virtual void detectCapabilities();

426
427
428
429
430
431
    /**
     * Is called by the constructor to query all extensions and
     * populate \a glExtensions_
     */
    void queryExtensions();

schultezub's avatar
schultezub committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
    /**
     * Is called by the constructor and performs the
     * operating system detection. The results
     * are internally stored.
     */
    virtual void detectOS();

    static const std::string loggerCat_;

private:
    // detection results are stored in the following members

    OSVersion osVersion_;
    std::string osVersionString_;

    GlVersion glVersion_;
    std::string glVersionString_;
449
    std::vector<std::string> glExtensions_;
schultezub's avatar
schultezub committed
450
451
452
453
454
455
456
    std::string glVendorString_;
    std::string glRendererString_;
    std::string glslVersionString_;
    GpuVendor vendor_;

    bool shaderSupport_;
    bool shaderSupportARB_;
457
    bool computeShaderSupport_;
schultezub's avatar
schultezub committed
458
459
460
461
462
463
464
    GlVersion shaderVersion_;
    ShaderModel shaderModel_;

    int maxTexSize_;
    bool texturing3D_;
    int max3DTexSize_;
    int numTextureUnits_;
465
    int numImageUnits_;
schultezub's avatar
schultezub committed
466
467
468
469
470
471
472
473
474
    bool npotTextures_;
    bool textureRectangles_;
    bool anisotropicFiltering_;
    float maxTextureAnisotropy_;
    bool textureCompression_;
    bool framebufferObjects_;
    int maxColorAttachments_;
};

475
} // namespace cgt
schultezub's avatar
schultezub committed
476

477
#define GpuCaps cgt::Singleton<cgt::GpuCapabilities>::getRef()
schultezub's avatar
schultezub committed
478

479
#endif // CGT_GPUCAPABILITIES_H