-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathFFTOpenGLVersionHelper.m
More file actions
167 lines (140 loc) · 4.02 KB
/
FFTOpenGLVersionHelper.m
File metadata and controls
167 lines (140 loc) · 4.02 KB
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
//
// FFTOpenGLVersionHelper.m
// FFmpegTutorial
//
// Created by qianlongxu on 2022/1/12.
//
#import "FFTOpenGLVersionHelper.h"
#import <OpenGL/gl3ext.h>
#import <AppKit/NSOpenGL.h>
#import <OpenGL/OpenGL.h>
//https://stackoverflow.com/questions/46322280/how-to-check-opengl-and-glsl-version-without-creating-a-window
@implementation FFTOpenGLVersionHelper
+ (NSString *)glString:(const char *)name enu:(GLenum)s
{
const char *v = (const char *) glGetString(s);
#if DEBUG
printf("[GL] %s = %s\n", name, v);
#endif
const GLubyte *str = glGetString(s);
return [NSString stringWithFormat:@"[%s]=%s",name,str];
}
+ (NSString *)vendor
{
return [self glString:"Vendor" enu:GL_VENDOR];
}
+ (NSString *)renderer
{
return [self glString:"Renderer" enu:GL_RENDERER];
}
+ (NSString *)glslVersion
{
return [self glString:"GLSL Version" enu:GL_SHADING_LANGUAGE_VERSION];
}
+ (NSString *)version
{
return [self glString:"Version" enu:GL_VERSION];
}
+ (NSArray<NSString *> *)supportedExtensions:(BOOL)legacy
{
if (legacy) {
const GLubyte *ext = glGetString(GL_EXTENSIONS);
NSString *extStr = [NSString stringWithFormat:@"%s",ext];
if (extStr.length > 0) {
NSArray *r = [extStr componentsSeparatedByString:@" "];
if ([[r lastObject] length] == 0) {
return [r subarrayWithRange:NSMakeRange(0, [r count] - 1)];
} else {
return r;
}
}
} else {
GLint exts;
glGetIntegerv(GL_NUM_EXTENSIONS, &exts);
NSMutableArray *r = [NSMutableArray array];
for (int n = 0; n < exts; n++) {
const GLubyte *ext = glGetStringi(GL_EXTENSIONS, n);
if (strlen((const char *)ext) > 0) {
[r addObject:[NSString stringWithFormat:@"%s",ext]];
}
}
return r;
}
return nil;
}
//https://www.khronos.org/opengl/wiki/Legacy_OpenGL
+ (NSOpenGLContext *)glContext:(BOOL)legacy
{
NSOpenGLPixelFormat *pf = nil;
if (legacy) {
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFANoRecovery,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAAllowOfflineRenderers, 1,
0
};
pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
} else {
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFANoRecovery,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAAllowOfflineRenderers, 1,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
0
};
pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
}
if (pf)
{
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
return context;
}
return nil;
}
+ (void)prepareOpenGLContext:(void(^)(void))completion forLegacy:(BOOL)legacy
{
if (!completion) {
return;
}
NSOpenGLContext *glContext = [self glContext:legacy];
[glContext makeCurrentContext];
CGLLockContext([glContext CGLContextObj]);
completion();
CGLUnlockContext([glContext CGLContextObj]);
}
+ (NSString *)openglAllInfo:(BOOL)legacy
{
NSMutableString *txt = [NSMutableString string];
{
//版本
[txt appendFormat:@"\n【%@ OpenGL Info】\n",legacy ? @"Legacy" : @"Modern"];
}
{
//版本
[txt appendFormat:@"\n%@\n",[self version]];
}
{
//厂商
[txt appendFormat:@"\n%@\n",[self vendor]];
}
{
//渲染器
[txt appendFormat:@"\n%@\n",[self renderer]];
}
{
//GL_SHADING_LANGUAGE_VERSION
[txt appendFormat:@"\n%@\n",[self glslVersion]];
}
{
//扩展
[txt appendFormat:@"\nExtensions=%@\n",[self supportedExtensions:legacy]];
}
return [txt copy];
}
@end