glewinfo_tail.c 11.4 KB
Newer Older
mem's avatar
mem committed
1
2
3
4
5
6
}

#endif /* _WIN32 */

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
7
int main (int argc, char** argv)
mem's avatar
mem committed
8
9
{
  GLuint err;
10
11
12
  struct createParams params = {
#if defined(_WIN32)
    -1,  /* pixelformat */
13
#elif !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
14
15
16
17
18
19
20
21
    "",  /* display */
    -1,  /* visual */
#endif
    0,   /* major */
    0,   /* minor */
    0,   /* profile mask */
    0    /* flags */
  };
ikits's avatar
ikits committed
22

23
  if (glewParseArgs(argc-1, argv+1, &params))
ikits's avatar
ikits committed
24
  {
25
    fprintf(stderr, "Usage: glewinfo "
ikits's avatar
ikits committed
26
#if defined(_WIN32)
27
	    "[-pf <pixelformat>] "
28
#elif !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
29
30
	    "[-display <display>] "
	    "[-visual <visual id>] "
ikits's avatar
ikits committed
31
#endif
32
33
34
35
	    "[-version <OpenGL version>] "
	    "[-profiles <OpenGL profile mask>] "
	    "[-flags <OpenGL flags>]"
	    "\n");
ikits's avatar
ikits committed
36
37
    return 1;
  }
ikits's avatar
ikits committed
38

39
  if (GL_TRUE == glewCreateContext(&params))
ikits's avatar
ikits committed
40
41
42
43
44
  {
    fprintf(stderr, "Error: glewCreateContext failed\n");
    glewDestroyContext();
    return 1;
  }
mem's avatar
mem committed
45
  glewExperimental = GL_TRUE;
ikits's avatar
ikits committed
46
#ifdef GLEW_MX
ikits's avatar
ikits committed
47
48
49
  err = glewContextInit(glewGetContext());
#ifdef _WIN32
  err = err || wglewContextInit(wglewGetContext());
50
#elif !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
51
52
53
  err = err || glxewContextInit(glxewGetContext());
#endif

ikits's avatar
ikits committed
54
#else
mem's avatar
mem committed
55
  err = glewInit();
ikits's avatar
ikits committed
56
#endif
mem's avatar
mem committed
57
58
59
  if (GLEW_OK != err)
  {
    fprintf(stderr, "Error [main]: glewInit failed: %s\n", glewGetErrorString(err));
ikits's avatar
ikits committed
60
    glewDestroyContext();
mem's avatar
mem committed
61
62
    return 1;
  }
ikits's avatar
ikits committed
63
#if defined(_WIN32)
64
65
66
67
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
  if (fopen_s(&f, "glewinfo.txt", "w") != 0)
    f = stdout;
#else
mem's avatar
mem committed
68
  f = fopen("glewinfo.txt", "w");
69
#endif
mem's avatar
mem committed
70
  if (f == NULL) f = stdout;
71
72
73
#else
  f = stdout;
#endif
mem's avatar
mem committed
74
75
76
  fprintf(f, "---------------------------\n");
  fprintf(f, "    GLEW Extension Info\n");
  fprintf(f, "---------------------------\n\n");
ikits's avatar
ikits committed
77
  fprintf(f, "GLEW version %s\n", glewGetString(GLEW_VERSION));
ikits's avatar
ikits committed
78
#if defined(_WIN32)
79
  fprintf(f, "Reporting capabilities of pixelformat %d\n", params.pixelformat);
ikits's avatar
ikits committed
80
#elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
81
  fprintf(f, "Reporting capabilities of display %s, visual 0x%x\n", 
82
    params.display == NULL ? getenv("DISPLAY") : params.display, params.visual);
ikits's avatar
ikits committed
83
#endif
mem's avatar
mem committed
84
85
86
87
  fprintf(f, "Running on a %s from %s\n", 
	  glGetString(GL_RENDERER), glGetString(GL_VENDOR));
  fprintf(f, "OpenGL version %s is supported\n", glGetString(GL_VERSION));
  glewInfo();
ikits's avatar
ikits committed
88
#if defined(_WIN32)
mem's avatar
mem committed
89
  wglewInfo();
ikits's avatar
ikits committed
90
#else
mem's avatar
mem committed
91
92
93
  glxewInfo();
#endif
  if (f != stdout) fclose(f);
ikits's avatar
ikits committed
94
  glewDestroyContext();
mem's avatar
mem committed
95
96
  return 0;
}
ikits's avatar
ikits committed
97
98
99

/* ------------------------------------------------------------------------ */

100
GLboolean glewParseArgs (int argc, char** argv, struct createParams *params)
ikits's avatar
ikits committed
101
102
103
104
{
  int p = 0;
  while (p < argc)
  {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    if (!strcmp(argv[p], "-version"))
    {
      if (++p >= argc) return GL_TRUE;
      if (sscanf(argv[p++], "%d.%d", &params->major, &params->minor) != 2) return GL_TRUE;
    }
    else if (!strcmp(argv[p], "-profiles"))
    {
      if (++p >= argc) return GL_TRUE;
      params->profile_mask = (int)strtol(argv[p++], NULL, 0);
    }
    else if (!strcmp(argv[p], "-flags"))
    {
      if (++p >= argc) return GL_TRUE;
      params->flags = (int)strtol(argv[p++], NULL, 0);
    }
ikits's avatar
ikits committed
120
#if defined(_WIN32)
121
    else if (!strcmp(argv[p], "-pf") || !strcmp(argv[p], "-pixelformat"))
ikits's avatar
ikits committed
122
123
    {
      if (++p >= argc) return GL_TRUE;
124
      params->pixelformat = strtol(argv[p++], NULL, 0);
ikits's avatar
ikits committed
125
    }
126
#elif !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
127
    else if (!strcmp(argv[p], "-display"))
ikits's avatar
ikits committed
128
129
    {
      if (++p >= argc) return GL_TRUE;
130
      params->display = argv[p++];
ikits's avatar
ikits committed
131
    }
ikits's avatar
ikits committed
132
    else if (!strcmp(argv[p], "-visual"))
ikits's avatar
ikits committed
133
134
    {
      if (++p >= argc) return GL_TRUE;
135
      params->visual = (int)strtol(argv[p++], NULL, 0);
ikits's avatar
ikits committed
136
    }
137
#endif
ikits's avatar
ikits committed
138
139
140
141
142
143
144
145
    else
      return GL_TRUE;
  }
  return GL_FALSE;
}

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
146
#if defined(_WIN32)
ikits's avatar
ikits committed
147

ikits's avatar
ikits committed
148
149
150
151
HWND wnd = NULL;
HDC dc = NULL;
HGLRC rc = NULL;

152
GLboolean glewCreateContext (struct createParams* params)
ikits's avatar
ikits committed
153
154
155
{
  WNDCLASS wc;
  PIXELFORMATDESCRIPTOR pfd;
ikits's avatar
ikits committed
156
  /* register window class */
ikits's avatar
ikits committed
157
158
159
160
161
  ZeroMemory(&wc, sizeof(WNDCLASS));
  wc.hInstance = GetModuleHandle(NULL);
  wc.lpfnWndProc = DefWindowProc;
  wc.lpszClassName = "GLEW";
  if (0 == RegisterClass(&wc)) return GL_TRUE;
ikits's avatar
ikits committed
162
  /* create window */
ikits's avatar
ikits committed
163
164
  wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
                     CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL);
ikits's avatar
ikits committed
165
  if (NULL == wnd) return GL_TRUE;
ikits's avatar
ikits committed
166
  /* get the device context */
ikits's avatar
ikits committed
167
168
  dc = GetDC(wnd);
  if (NULL == dc) return GL_TRUE;
ikits's avatar
ikits committed
169
  /* find pixel format */
ikits's avatar
ikits committed
170
  ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
171
  if (params->pixelformat == -1) /* find default */
ikits's avatar
ikits committed
172
173
174
175
  {
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
176
177
    params->pixelformat = ChoosePixelFormat(dc, &pfd);
    if (params->pixelformat == 0) return GL_TRUE;
ikits's avatar
ikits committed
178
  }
ikits's avatar
ikits committed
179
  /* set the pixel format for the dc */
180
  if (FALSE == SetPixelFormat(dc, params->pixelformat, &pfd)) return GL_TRUE;
ikits's avatar
ikits committed
181
  /* create rendering context */
ikits's avatar
ikits committed
182
183
184
  rc = wglCreateContext(dc);
  if (NULL == rc) return GL_TRUE;
  if (FALSE == wglMakeCurrent(dc, rc)) return GL_TRUE;
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
  if (params->major || params->profile_mask || params->flags)
  {
    HGLRC oldRC = rc;
    int contextAttrs[20];
    int i;

    extern GLenum GLEWAPIENTRY wglewContextInit();
    wglewContextInit();

    if (!wglewGetExtension("WGL_ARB_create_context_profile"))
      return GL_TRUE;

    i = 0;
    if( params->major )
    {
      contextAttrs[i++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
      contextAttrs[i++] = params->major;
      contextAttrs[i++] = WGL_CONTEXT_MINOR_VERSION_ARB;
      contextAttrs[i++] = params->minor;
    }
    if( params->profile_mask )
    {
      contextAttrs[i++] = WGL_CONTEXT_PROFILE_MASK_ARB;
      contextAttrs[i++] = params->profile_mask;
    }
    if( params->flags )
    {
      contextAttrs[i++] = WGL_CONTEXT_FLAGS_ARB;
      contextAttrs[i++] = params->flags;
    }
    contextAttrs[i++] = 0;
    rc = wglCreateContextAttribsARB(dc, 0, contextAttrs);

    if (NULL == rc) return GL_TRUE;
    if (!wglMakeCurrent(dc, rc)) return GL_TRUE;

    wglDeleteContext(oldRC);
  }
ikits's avatar
ikits committed
223
224
225
226
227
228
  return GL_FALSE;
}

void glewDestroyContext ()
{
  if (NULL != rc) wglMakeCurrent(NULL, NULL);
nigels's avatar
nigels committed
229
  if (NULL != rc) wglDeleteContext(rc);
ikits's avatar
ikits committed
230
231
232
233
234
235
236
  if (NULL != wnd && NULL != dc) ReleaseDC(wnd, dc);
  if (NULL != wnd) DestroyWindow(wnd);
  UnregisterClass("GLEW", GetModuleHandle(NULL));
}

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
237
#elif defined(__APPLE__) && !defined(GLEW_APPLE_GLX)
ikits's avatar
ikits committed
238

239
240
#include <OpenGL/OpenGL.h>
#include <OpenGL/CGLTypes.h>
ikits's avatar
ikits committed
241

242
CGLContextObj ctx, octx;
ikits's avatar
ikits committed
243

244
GLboolean glewCreateContext (struct createParams *params)
ikits's avatar
ikits committed
245
{
246
247
  CGLPixelFormatAttribute contextAttrs[20];
  int i;
248
249
250
251
  CGLPixelFormatObj pf;
  GLint npix;
  CGLError error;

252
253
254
255
256
257
258
259
260
261
262
  i = 0;
  contextAttrs[i++] = kCGLPFAAccelerated; /* No software rendering */

  #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
  if (params->profile_mask & GL_CONTEXT_CORE_PROFILE_BIT)
  {
    if (params->major==3 && params->minor>=2)
    {
      contextAttrs[i++] = kCGLPFAOpenGLProfile;                                /* OSX 10.7 Lion onwards */
      contextAttrs[i++] = (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core;  /* 3.2 Core Context      */
    }
263
  }
264
265
266
  #endif

  contextAttrs[i++] = 0;
267

268
  error = CGLChoosePixelFormat(contextAttrs, &pf, &npix);
269
270
271
272
273
274
275
  if (error) return GL_TRUE;
  error = CGLCreateContext(pf, NULL, &ctx);
  if (error) return GL_TRUE;
  CGLReleasePixelFormat(pf);
  octx = CGLGetCurrentContext();
  error = CGLSetCurrentContext(ctx);
  if (error) return GL_TRUE;
276
277
  /* Needed for Regal on the Mac */
  #if defined(GLEW_REGAL) && defined(__APPLE__)
278
  RegalMakeCurrent(ctx);
279
  #endif
ikits's avatar
ikits committed
280
  return GL_FALSE;
ikits's avatar
ikits committed
281
282
283
284
}

void glewDestroyContext ()
{
285
286
  CGLSetCurrentContext(octx);
  CGLReleaseContext(ctx);
ikits's avatar
ikits committed
287
}
288
289
290
291
292

/* ------------------------------------------------------------------------ */

#elif defined(__HAIKU__)

293
GLboolean glewCreateContext (struct createParams *params)
294
295
296
297
298
299
300
301
302
{
  /* TODO: Haiku: We need to call C++ code here */
  return GL_FALSE;
}

void glewDestroyContext ()
{
  /* TODO: Haiku: We need to call C++ code here */
}
ikits's avatar
ikits committed
303
304
305

/* ------------------------------------------------------------------------ */

ikits's avatar
ikits committed
306
307
#else /* __UNIX || (__APPLE__ && GLEW_APPLE_GLX) */

ikits's avatar
ikits committed
308
309
Display* dpy = NULL;
XVisualInfo* vi = NULL;
ikits's avatar
ikits committed
310
XVisualInfo* vis = NULL;
ikits's avatar
ikits committed
311
GLXContext ctx = NULL;
ikits's avatar
ikits committed
312
313
Window wnd = 0;
Colormap cmap = 0;
ikits's avatar
ikits committed
314

315
GLboolean glewCreateContext (struct createParams *params)
ikits's avatar
ikits committed
316
{
ikits's avatar
ikits committed
317
  int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
ikits's avatar
ikits committed
318
  int erb, evb;
ikits's avatar
ikits committed
319
  XSetWindowAttributes swa;
ikits's avatar
ikits committed
320
  /* open display */
321
  dpy = XOpenDisplay(params->display);
ikits's avatar
ikits committed
322
323
  if (NULL == dpy) return GL_TRUE;
  /* query for glx */
ikits's avatar
ikits committed
324
  if (!glXQueryExtension(dpy, &erb, &evb)) return GL_TRUE;
ikits's avatar
ikits committed
325
  /* choose visual */
326
  if (params->visual == -1)
ikits's avatar
ikits committed
327
328
329
  {
    vi = glXChooseVisual(dpy, DefaultScreen(dpy), attrib);
    if (NULL == vi) return GL_TRUE;
330
    params->visual = (int)XVisualIDFromVisual(vi->visual);
ikits's avatar
ikits committed
331
332
333
334
335
336
337
  }
  else
  {
    int n_vis, i;
    vis = XGetVisualInfo(dpy, 0, NULL, &n_vis);
    for (i=0; i<n_vis; i++)
    {
338
      if ((int)XVisualIDFromVisual(vis[i].visual) == params->visual)
ikits's avatar
ikits committed
339
340
341
342
        vi = &vis[i];
    }
    if (vi == NULL) return GL_TRUE;
  }
ikits's avatar
ikits committed
343
344
345
346
  /* create context */
  ctx = glXCreateContext(dpy, vi, None, True);
  if (NULL == ctx) return GL_TRUE;
  /* create window */
ikits's avatar
ikits committed
347
  /*wnd = XCreateSimpleWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 1, 1, 1, 0, 0);*/
ikits's avatar
ikits committed
348
349
350
  cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
  swa.border_pixel = 0;
  swa.colormap = cmap;
ikits's avatar
ikits committed
351
352
353
  wnd = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 
                      0, 0, 1, 1, 0, vi->depth, InputOutput, vi->visual, 
                      CWBorderPixel | CWColormap, &swa);
ikits's avatar
ikits committed
354
  /* make context current */
ikits's avatar
ikits committed
355
  if (!glXMakeCurrent(dpy, wnd, ctx)) return GL_TRUE;
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
  if (params->major || params->profile_mask || params->flags)
  {
    GLXContext oldCtx = ctx;
    GLXFBConfig *FBConfigs;
    int FBConfigAttrs[] = { GLX_FBCONFIG_ID, 0, None };
    int contextAttrs[20];
    int nelems, i;

    glxewContextInit();

    if (!glxewGetExtension("GLX_ARB_create_context_profile"))
      return GL_TRUE;

    if (glXQueryContext(dpy, oldCtx, GLX_FBCONFIG_ID, &FBConfigAttrs[1]))
      return GL_TRUE;
    FBConfigs = glXChooseFBConfig(dpy, vi->screen, FBConfigAttrs, &nelems);

    if (nelems < 1)
      return GL_TRUE;

    i = 0;
    if( params->major )
    {
      contextAttrs[i++] = GLX_CONTEXT_MAJOR_VERSION_ARB;
      contextAttrs[i++] = params->major;
      contextAttrs[i++] = GLX_CONTEXT_MINOR_VERSION_ARB;
      contextAttrs[i++] = params->minor;
    }
    if( params->profile_mask )
    {
      contextAttrs[i++] = GLX_CONTEXT_PROFILE_MASK_ARB;
      contextAttrs[i++] = params->profile_mask;
    }
    if( params->flags )
    {
      contextAttrs[i++] = GLX_CONTEXT_FLAGS_ARB;
      contextAttrs[i++] = params->flags;
    }
    contextAttrs[i++] = None;
    ctx = glXCreateContextAttribsARB(dpy, *FBConfigs, NULL, True, contextAttrs);

    if (NULL == ctx) return GL_TRUE;
    if (!glXMakeCurrent(dpy, wnd, ctx)) return GL_TRUE;

    glXDestroyContext(dpy, oldCtx);

    XFree(FBConfigs);
  }
ikits's avatar
ikits committed
404
  return GL_FALSE;
ikits's avatar
ikits committed
405
406
407
408
}

void glewDestroyContext ()
{
ikits's avatar
ikits committed
409
  if (NULL != dpy && NULL != ctx) glXDestroyContext(dpy, ctx);
ikits's avatar
ikits committed
410
  if (NULL != dpy && 0 != wnd) XDestroyWindow(dpy, wnd);
ikits's avatar
ikits committed
411
  if (NULL != dpy && 0 != cmap) XFreeColormap(dpy, cmap);
ikits's avatar
ikits committed
412
413
414
415
  if (NULL != vis)
    XFree(vis);
  else if (NULL != vi)
    XFree(vi);
ikits's avatar
ikits committed
416
  if (NULL != dpy) XCloseDisplay(dpy);
ikits's avatar
ikits committed
417
418
}

ikits's avatar
ikits committed
419
#endif /* __UNIX || (__APPLE__ && GLEW_APPLE_GLX) */