forked from eclipse-efx/efxclipse-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriftGL_cgl.cpp
166 lines (133 loc) · 4.74 KB
/
DriftGL_cgl.cpp
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
#ifdef DRIFTGL_CGL
#include <DriftGL.h>
#include <DriftGL_cgl.h>
#include <iostream>
#include <OpenGL/OpenGL.h>
#include <OpenGL/CGLContext.h>
#include <OpenGL/CGLTypes.h>
#include <OpenGL/CGLDevice.h>
#include <CoreFoundation/CoreFoundation.h>
namespace driftgl {
struct CGLContext : Context {
CGLContextObj contextObj;
};
void* cglGetProcAddress(const char* procName)
{
static CFBundleRef openGLBundle = NULL;
if (openGLBundle == NULL)
{
openGLBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
}
CFStringRef functionName = CFStringCreateWithCString(NULL, procName, kCFStringEncodingASCII);
void* function = CFBundleGetFunctionPointerForName(openGLBundle, functionName);
CFRelease(functionName);
return function;
}
bool initialized = false;
bool Initialize() {
procs::Initialize([](const char* name) {
void* proc = cglGetProcAddress(name);
if (proc == 0) {
//std::cout << " ! Could not acquire " << name << std::endl;
}
return (void*) proc;
});
return true;
}
bool Destroy() {
return true;
}
CGLContextObj getNative(Context* ctx) {
if (ctx == nullptr) {
return nullptr;
}
return ((CGLContext*) ctx)->contextObj;
}
Context* CreateContext(Context* sharedContext, int majorHint, int minorHint) {
auto ctx = new CGLContext();
auto sharedCtxObj = getNative(sharedContext);
CGLPixelFormatAttribute profile = (CGLPixelFormatAttribute) kCGLOGLPVersion_Legacy;
if (majorHint == 4) {
profile = (CGLPixelFormatAttribute) kCGLOGLPVersion_GL4_Core;
}
if (majorHint == 3) {
profile = (CGLPixelFormatAttribute) kCGLOGLPVersion_GL3_Core;
}
if (majorHint == 3 && minorHint == 2) {
profile = (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core;
}
CGLPixelFormatAttribute attributes[5] = {
//kCGLPFAPBuffer,
kCGLPFAAccelerated,
kCGLPFAOpenGLProfile,
profile,
(CGLPixelFormatAttribute) 0
};
GLint num;
CGLPixelFormatObj pix;
if (sharedCtxObj != 0) {
pix = CGLGetPixelFormat(sharedCtxObj);
}
else {
CGLChoosePixelFormat( attributes, &pix, &num );
}
CGLError error = CGLCreateContext( pix, sharedCtxObj, &ctx->contextObj );
if (error) {
std::cout << "CreateContext Error: " << error << std::endl;
}
CGLError r = CGLDestroyPixelFormat( pix );
return ctx;
}
Context* CreateSharedCompatContext(Context* sharedContext) {
auto ctx = new CGLContext();
auto sharedCtxObj = getNative(sharedContext);
CGLPixelFormatAttribute attributes[5] = {
//kCGLPFAPBuffer,
kCGLPFAAccelerated,
kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) kCGLOGLPVersion_Legacy,
(CGLPixelFormatAttribute) 0
};
GLint num;
CGLPixelFormatObj pix;
CGLChoosePixelFormat( attributes, &pix, &num );
//std::cout << "calling CGLCreateContext(" << pix << ", " << sharedCtxObj << ")" << std::endl << std::flush;
CGLError error = CGLCreateContext( pix, sharedCtxObj, &ctx->contextObj );
if (error) {
std::cout << "CreateSharedContext Error: " << error << std::endl;
}
//std::cout << " => " << ctx->contextObj << std::endl << std::flush;
CGLError r = CGLDestroyPixelFormat( pix );
return ctx;
}
Context* WrapContext(long long nativeContextHandle) {
auto ctx = new CGLContext();
ctx->contextObj = (CGLContextObj) nativeContextHandle;
return ctx;
}
void DestroyContext(Context* context) {
auto ctx = (CGLContext*) context;
CGLError result = CGLDestroyContext(ctx->contextObj);
delete ctx;
}
bool MakeContextCurrent(Context* context) {
auto ctxObj = getNative(context);
CGLError result = CGLSetCurrentContext(ctxObj);
//std::cout << "currentContext = " << CGLGetCurrentContext() << std::endl;
//std::cout << "CGLSetCurrentContext("<< ctxObj <<") returned " << result << std::endl;
//std::cout << "currentContext = " << CGLGetCurrentContext() << std::endl;
return true;
}
bool IsContextCurrent(Context* context) {
CGLContext* ctx = (CGLContext*) context;
return CGLGetCurrentContext() == ctx->contextObj;
}
void* GetNativeContextHandle(Context* context) {
CGLContext* ctx = (CGLContext*) context;
return (void*) ctx->contextObj;
}
Context* GetCurrentContext() {
return 0;
}
}
#endif