forked from BitcoderCZ/BuildPlate_Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLGetVersion.cs
270 lines (229 loc) · 10.9 KB
/
GLGetVersion.cs
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
261
262
263
264
265
266
267
268
269
270
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace BuildPlate_Editor
{
internal static class Imports
{
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeLibrary(IntPtr hModule);
[DllImport("user32.dll", CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procName);
}
internal sealed class UnmanagedLibrary : IDisposable
{
private bool disposed = false;
public UnmanagedLibrary(string path)
{
Handle = Imports.LoadLibrary(path);
if (Handle == IntPtr.Zero) {
throw new Exception($"Failed to load library \"{path}\" ({Marshal.GetLastWin32Error()}).");
}
}
~UnmanagedLibrary()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!disposed) {
Imports.FreeLibrary(Handle);
disposed = true;
}
}
public IntPtr Handle
{
get;
private set;
}
}
internal static class OpenGLHelper
{
[StructLayout(LayoutKind.Explicit)]
private struct PIXELFORMATDESCRIPTOR
{
[FieldOffset(0)]
public UInt16 nSize;
[FieldOffset(2)]
public UInt16 nVersion;
[FieldOffset(4)]
public UInt32 dwFlags;
[FieldOffset(8)]
public Byte iPixelType;
[FieldOffset(9)]
public Byte cColorBits;
[FieldOffset(10)]
public Byte cRedBits;
[FieldOffset(11)]
public Byte cRedShift;
[FieldOffset(12)]
public Byte cGreenBits;
[FieldOffset(13)]
public Byte cGreenShift;
[FieldOffset(14)]
public Byte cBlueBits;
[FieldOffset(15)]
public Byte cBlueShift;
[FieldOffset(16)]
public Byte cAlphaBits;
[FieldOffset(17)]
public Byte cAlphaShift;
[FieldOffset(18)]
public Byte cAccumBits;
[FieldOffset(19)]
public Byte cAccumRedBits;
[FieldOffset(20)]
public Byte cAccumGreenBits;
[FieldOffset(21)]
public Byte cAccumBlueBits;
[FieldOffset(22)]
public Byte cAccumAlphaBits;
[FieldOffset(23)]
public Byte cDepthBits;
[FieldOffset(24)]
public Byte cStencilBits;
[FieldOffset(25)]
public Byte cAuxBuffers;
[FieldOffset(26)]
public SByte iLayerType;
[FieldOffset(27)]
public Byte bReserved;
[FieldOffset(28)]
public UInt32 dwLayerMask;
[FieldOffset(32)]
public UInt32 dwVisibleMask;
[FieldOffset(36)]
public UInt32 dwDamageMask;
}
private const byte PFD_TYPE_RGBA = 0;
private const sbyte PFD_MAIN_PLANE = 0;
private const uint PFD_DOUBLEBUFFER = 1;
private const uint PFD_DRAW_TO_WINDOW = 4;
private const uint PFD_SUPPORT_OPENGL = 32;
private const int GL_VERSION = 0x1F02;
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate int ChoosePixelFormatDelegate(IntPtr hdc, IntPtr ppfd);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate int SetPixelFormatDelegate(IntPtr hdc, int format, IntPtr ppfd);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate IntPtr wglCreateContextDelegate(IntPtr arg1);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate int wglDeleteContextDelegate(IntPtr arg1);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate int wglMakeCurrentDelegate(IntPtr arg1, IntPtr arg2);
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate IntPtr glGetStringDelegate(int name);
public static string GetVersion()
{
using (UnmanagedLibrary openGLLib = new UnmanagedLibrary("opengl32.dll"))
using (UnmanagedLibrary gdi32Lib = new UnmanagedLibrary("Gdi32.dll")) {
IntPtr deviceContextHandle = Imports.GetDC(Process.GetCurrentProcess().MainWindowHandle);
if (deviceContextHandle == IntPtr.Zero) {
throw new Exception("Failed to get device context from the main window.");
}
IntPtr choosePixelFormatAddress = Imports.GetProcAddress(gdi32Lib.Handle, "ChoosePixelFormat");
if (choosePixelFormatAddress == IntPtr.Zero) {
throw new Exception($"Failed to get ChoosePixelFormat address ({Marshal.GetLastWin32Error()}).");
}
ChoosePixelFormatDelegate choosePixelFormat = Marshal.GetDelegateForFunctionPointer<ChoosePixelFormatDelegate>(choosePixelFormatAddress);
PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR
{
nSize = (UInt16)Marshal.SizeOf(typeof(PIXELFORMATDESCRIPTOR)),
nVersion = 1,
dwFlags = (PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER),
iPixelType = PFD_TYPE_RGBA,
cColorBits = 32,
cRedBits = 0,
cRedShift = 0,
cGreenBits = 0,
cGreenShift = 0,
cBlueBits = 0,
cBlueShift = 0,
cAlphaBits = 0,
cAlphaShift = 0,
cAccumBits = 0,
cAccumRedBits = 0,
cAccumGreenBits = 0,
cAccumBlueBits = 0,
cAccumAlphaBits = 0,
cDepthBits = 24,
cStencilBits = 8,
cAuxBuffers = 0,
iLayerType = PFD_MAIN_PLANE,
bReserved = 0,
dwLayerMask = 0,
dwVisibleMask = 0,
dwDamageMask = 0
};
IntPtr pfdPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PIXELFORMATDESCRIPTOR)));
try {
Marshal.StructureToPtr(pfd, pfdPtr, false);
int pixelFormat = choosePixelFormat(deviceContextHandle, pfdPtr);
if (pixelFormat == 0) {
throw new Exception($"Failed to choose pixel format ({Marshal.GetLastWin32Error()}).");
}
IntPtr setPixelFormatAddress = Imports.GetProcAddress(gdi32Lib.Handle, "SetPixelFormat");
if (setPixelFormatAddress == IntPtr.Zero) {
throw new Exception($"Failed to get SetPixelFormat address ({Marshal.GetLastWin32Error()}).");
}
SetPixelFormatDelegate setPixelFormat = Marshal.GetDelegateForFunctionPointer<SetPixelFormatDelegate>(setPixelFormatAddress);
if (setPixelFormat(deviceContextHandle, pixelFormat, pfdPtr) <= 0) {
throw new Exception($"Failed to set pixel format ({Marshal.GetLastWin32Error()}).");
}
IntPtr wglCreateContextAddress = Imports.GetProcAddress(openGLLib.Handle, "wglCreateContext");
if (wglCreateContextAddress == IntPtr.Zero) {
throw new Exception($"Failed to get wglCreateContext address ({Marshal.GetLastWin32Error()}).");
}
wglCreateContextDelegate wglCreateContext = Marshal.GetDelegateForFunctionPointer<wglCreateContextDelegate>(wglCreateContextAddress);
IntPtr wglDeleteContextAddress = Imports.GetProcAddress(openGLLib.Handle, "wglDeleteContext");
if (wglDeleteContextAddress == IntPtr.Zero) {
throw new Exception($"Failed to get wglDeleteContext address ({Marshal.GetLastWin32Error()}).");
}
wglDeleteContextDelegate wglDeleteContext = Marshal.GetDelegateForFunctionPointer<wglDeleteContextDelegate>(wglDeleteContextAddress);
IntPtr openGLRenderingContext = wglCreateContext(deviceContextHandle);
if (openGLRenderingContext == IntPtr.Zero) {
throw new Exception($"Failed to create OpenGL rendering context ({Marshal.GetLastWin32Error()}).");
}
try {
IntPtr wglMakeCurrentAddress = Imports.GetProcAddress(openGLLib.Handle, "wglMakeCurrent");
if (wglMakeCurrentAddress == IntPtr.Zero) {
throw new Exception($"Failed to get wglMakeCurrent address ({Marshal.GetLastWin32Error()}).");
}
Program.Window.MakeCurrent();
IntPtr glGetStringAddress = Imports.GetProcAddress(openGLLib.Handle, "glGetString");
if (glGetStringAddress == IntPtr.Zero) {
throw new Exception($"Failed to get glGetString address ({Marshal.GetLastWin32Error()}).");
}
glGetStringDelegate glGetString = Marshal.GetDelegateForFunctionPointer<glGetStringDelegate>(glGetStringAddress);
IntPtr versionStrPtr = glGetString(GL_VERSION);
if (versionStrPtr == IntPtr.Zero) {
// I don't think this ever goes wrong, in the context of OP's question and considering the current code.
throw new Exception("Failed to get OpenGL version string.");
}
return Marshal.PtrToStringAnsi(versionStrPtr);
}
finally {
wglDeleteContext(openGLRenderingContext);
}
}
finally {
Marshal.FreeHGlobal(pfdPtr);
}
}
}
}
}