GrGLUtil.h revision 0a94e4cc1073daf3bb3cd8d92fc3d8c62bdb35f3
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrGLUtil_DEFINED
9#define GrGLUtil_DEFINED
10
11#include "gl/GrGLInterface.h"
12#include "GrGLDefines.h"
13#include "GrStencilSettings.h"
14
15class SkMatrix;
16
17////////////////////////////////////////////////////////////////////////////////
18
19typedef uint32_t GrGLVersion;
20typedef uint32_t GrGLSLVersion;
21typedef uint32_t GrGLDriverVersion;
22
23#define GR_GL_VER(major, minor) ((static_cast<int>(major) << 16) | \
24                                 static_cast<int>(minor))
25#define GR_GLSL_VER(major, minor) ((static_cast<int>(major) << 16) | \
26                                   static_cast<int>(minor))
27#define GR_GL_DRIVER_VER(major, minor) ((static_cast<int>(major) << 16) | \
28                                        static_cast<int>(minor))
29
30#define GR_GL_INVALID_VER GR_GL_VER(0, 0)
31#define GR_GLSL_INVALID_VER GR_GLSL_VER(0, 0)
32#define GR_GL_DRIVER_UNKNOWN_VER GR_GL_DRIVER_VER(0, 0)
33
34/**
35 * The Vendor and Renderer enum values are lazily updated as required.
36 */
37enum GrGLVendor {
38    kARM_GrGLVendor,
39    kImagination_GrGLVendor,
40    kIntel_GrGLVendor,
41    kQualcomm_GrGLVendor,
42    kNVIDIA_GrGLVendor,
43    kATI_GrGLVendor,
44
45    kOther_GrGLVendor
46};
47
48enum GrGLRenderer {
49    kTegra2_GrGLRenderer,
50    kTegra3_GrGLRenderer,
51    kPowerVR54x_GrGLRenderer,
52    kPowerVRRogue_GrGLRenderer,
53    kAdreno3xx_GrGLRenderer,
54    kAdreno4xx_GrGLRenderer,
55    kAdreno5xx_GrGLRenderer,
56    kOSMesa_GrGLRenderer,
57    kIntelIrisPro_GrGLRenderer,
58    /** Either HD 4xxx or Iris 4xxx */
59    kIntel4xxx_GrGLRenderer,
60    /** Either HD 6xxx or Iris 6xxx */
61    kIntel6xxx_GrGLRenderer,
62    kGalliumLLVM_GrGLRenderer,
63    /** T-6xx, T-7xx, or T-8xx */
64    kMaliT_GrGLRenderer,
65    kANGLE_GrGLRenderer,
66
67    kAMDRadeonHD7xxx_GrGLRenderer, // AMD Radeon HD 7000 Series
68    kAMDRadeonR9M4xx_GrGLRenderer, // AMD Radeon R9 M400 Series
69
70    kOther_GrGLRenderer
71};
72
73enum GrGLDriver {
74    kMesa_GrGLDriver,
75    kChromium_GrGLDriver,
76    kNVIDIA_GrGLDriver,
77    kIntel_GrGLDriver,
78    kANGLE_GrGLDriver,
79    kQualcomm_GrGLDriver,
80    kUnknown_GrGLDriver
81};
82
83enum class GrGLANGLEBackend {
84    kUnknown,
85    kD3D9,
86    kD3D11,
87    kOpenGL
88};
89
90enum class GrGLANGLEVendor {
91    kUnknown,
92    kIntel
93};
94
95enum class GrGLANGLERenderer {
96    kUnknown,
97    kIvyBridge,
98    kSkylake
99};
100
101////////////////////////////////////////////////////////////////////////////////
102
103/**
104 *  Some drivers want the var-int arg to be zero-initialized on input.
105 */
106#define GR_GL_INIT_ZERO     0
107#define GR_GL_GetIntegerv(gl, e, p)                                            \
108    do {                                                                       \
109        *(p) = GR_GL_INIT_ZERO;                                                \
110        GR_GL_CALL(gl, GetIntegerv(e, p));                                     \
111    } while (0)
112
113#define GR_GL_GetFramebufferAttachmentParameteriv(gl, t, a, pname, p)          \
114    do {                                                                       \
115        *(p) = GR_GL_INIT_ZERO;                                                \
116        GR_GL_CALL(gl, GetFramebufferAttachmentParameteriv(t, a, pname, p));   \
117    } while (0)
118
119#define GR_GL_GetInternalformativ(gl, t, f, n, s, p)                           \
120    do {                                                                       \
121        *(p) = GR_GL_INIT_ZERO;                                                \
122        GR_GL_CALL(gl, GetInternalformativ(t, f, n, s, p));                    \
123    } while (0)
124
125#define GR_GL_GetNamedFramebufferAttachmentParameteriv(gl, fb, a, pname, p)          \
126    do {                                                                             \
127        *(p) = GR_GL_INIT_ZERO;                                                      \
128        GR_GL_CALL(gl, GetNamedFramebufferAttachmentParameteriv(fb, a, pname, p));   \
129    } while (0)
130
131#define GR_GL_GetRenderbufferParameteriv(gl, t, pname, p)                      \
132    do {                                                                       \
133        *(p) = GR_GL_INIT_ZERO;                                                \
134        GR_GL_CALL(gl, GetRenderbufferParameteriv(t, pname, p));               \
135    } while (0)
136
137#define GR_GL_GetTexLevelParameteriv(gl, t, l, pname, p)                       \
138    do {                                                                       \
139        *(p) = GR_GL_INIT_ZERO;                                                \
140        GR_GL_CALL(gl, GetTexLevelParameteriv(t, l, pname, p));                \
141    } while (0)
142
143#define GR_GL_GetShaderPrecisionFormat(gl, st, pt, range, precision)           \
144    do {                                                                       \
145        (range)[0] = GR_GL_INIT_ZERO;                                          \
146        (range)[1] = GR_GL_INIT_ZERO;                                          \
147        (*precision) = GR_GL_INIT_ZERO;                                        \
148        GR_GL_CALL(gl, GetShaderPrecisionFormat(st, pt, range, precision));    \
149    } while (0)
150
151////////////////////////////////////////////////////////////////////////////////
152
153/**
154 * Helpers for glGetString()
155 */
156
157// these variants assume caller already has a string from glGetString()
158GrGLVersion GrGLGetVersionFromString(const char* versionString);
159GrGLStandard GrGLGetStandardInUseFromString(const char* versionString);
160GrGLSLVersion GrGLGetGLSLVersionFromString(const char* versionString);
161GrGLVendor GrGLGetVendorFromString(const char* vendorString);
162GrGLRenderer GrGLGetRendererFromString(const char* rendererString);
163void GrGLGetANGLEInfoFromString(const char* rendererString, GrGLANGLEBackend*,
164                                GrGLANGLEVendor*, GrGLANGLERenderer*);
165
166void GrGLGetDriverInfo(GrGLStandard standard,
167                       GrGLVendor vendor,
168                       const char* rendererString,
169                       const char* versionString,
170                       GrGLDriver* outDriver,
171                       GrGLDriverVersion* outVersion);
172
173// these variants call glGetString()
174GrGLVersion GrGLGetVersion(const GrGLInterface*);
175GrGLSLVersion GrGLGetGLSLVersion(const GrGLInterface*);
176GrGLVendor GrGLGetVendor(const GrGLInterface*);
177GrGLRenderer GrGLGetRenderer(const GrGLInterface*);
178
179/**
180 * Helpers for glGetError()
181 */
182
183void GrGLCheckErr(const GrGLInterface* gl,
184                  const char* location,
185                  const char* call);
186
187void GrGLClearErr(const GrGLInterface* gl);
188
189////////////////////////////////////////////////////////////////////////////////
190
191/**
192 * Macros for using GrGLInterface to make GL calls
193 */
194
195// internal macro to conditionally call glGetError based on compile-time and
196// run-time flags.
197#if GR_GL_CHECK_ERROR
198    extern bool gCheckErrorGL;
199    #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)                    \
200        if (gCheckErrorGL)                                      \
201            GrGLCheckErr(IFACE, GR_FILE_AND_LINE_STR, #X)
202#else
203    #define GR_GL_CHECK_ERROR_IMPL(IFACE, X)
204#endif
205
206// internal macro to conditionally log the gl call using SkDebugf based on
207// compile-time and run-time flags.
208#if GR_GL_LOG_CALLS
209    extern bool gLogCallsGL;
210    #define GR_GL_LOG_CALLS_IMPL(X)                             \
211        if (gLogCallsGL)                                        \
212            SkDebugf(GR_FILE_AND_LINE_STR "GL: " #X "\n")
213#else
214    #define GR_GL_LOG_CALLS_IMPL(X)
215#endif
216
217// makes a GL call on the interface and does any error checking and logging
218#define GR_GL_CALL(IFACE, X)                                    \
219    do {                                                        \
220        GR_GL_CALL_NOERRCHECK(IFACE, X);                        \
221        GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
222    } while (false)
223
224// Variant of above that always skips the error check. This is useful when
225// the caller wants to do its own glGetError() call and examine the error value.
226#define GR_GL_CALL_NOERRCHECK(IFACE, X)                         \
227    do {                                                        \
228        (IFACE)->fFunctions.f##X;                               \
229        GR_GL_LOG_CALLS_IMPL(X);                                \
230    } while (false)
231
232// same as GR_GL_CALL but stores the return value of the gl call in RET
233#define GR_GL_CALL_RET(IFACE, RET, X)                           \
234    do {                                                        \
235        GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X);               \
236        GR_GL_CHECK_ERROR_IMPL(IFACE, X);                       \
237    } while (false)
238
239// same as GR_GL_CALL_RET but always skips the error check.
240#define GR_GL_CALL_RET_NOERRCHECK(IFACE, RET, X)                \
241    do {                                                        \
242        (RET) = (IFACE)->fFunctions.f##X;                       \
243        GR_GL_LOG_CALLS_IMPL(X);                                \
244    } while (false)
245
246// call glGetError without doing a redundant error check or logging.
247#define GR_GL_GET_ERROR(IFACE) (IFACE)->fFunctions.fGetError()
248
249GrGLenum GrToGLStencilFunc(GrStencilTest test);
250
251GrPixelConfig GrGLSizedFormatToPixelConfig(GrGLenum sizedFormat);
252
253#endif
254