Porting fix.
[wine/multimedia.git] / graphics / x11drv / opengl.c
blob2142d589776ebe18fb6b15ac4395062bdf34dcc8
1 /*
2 * X11DRV OpenGL functions
4 * Copyright 2000 Lionel Ulmer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include "ts_xlib.h"
25 #include <stdlib.h>
26 #include <string.h>
28 #include "gdi.h"
29 #include "x11drv.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
34 #ifdef HAVE_OPENGL
36 #undef APIENTRY
37 #undef CALLBACK
38 #undef WINAPI
40 #define XMD_H /* This is to prevent the Xmd.h inclusion bug :-/ */
41 #ifdef HAVE_GL_GL_H
42 # include <GL/gl.h>
43 #endif
44 #ifdef HAVE_GL_GLX_H
45 # include <GL/glx.h>
46 #endif
47 #ifdef HAVE_GL_GLEXT_H
48 # include <GL/glext.h>
49 #endif
50 #undef XMD_H
52 #undef APIENTRY
53 #undef CALLBACK
54 #undef WINAPI
56 /* Redefines the constants */
57 #define CALLBACK __stdcall
58 #define WINAPI __stdcall
59 #define APIENTRY WINAPI
62 static void dump_PIXELFORMATDESCRIPTOR(PIXELFORMATDESCRIPTOR *ppfd) {
63 DPRINTF(" - size / version : %d / %d\n", ppfd->nSize, ppfd->nVersion);
64 DPRINTF(" - dwFlags : ");
65 #define TEST_AND_DUMP(t,tv) if ((t) & (tv)) DPRINTF(#tv " ")
66 TEST_AND_DUMP(ppfd->dwFlags, PFD_DEPTH_DONTCARE);
67 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER);
68 TEST_AND_DUMP(ppfd->dwFlags, PFD_DOUBLEBUFFER_DONTCARE);
69 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_WINDOW);
70 TEST_AND_DUMP(ppfd->dwFlags, PFD_DRAW_TO_BITMAP);
71 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_ACCELERATED);
72 TEST_AND_DUMP(ppfd->dwFlags, PFD_GENERIC_FORMAT);
73 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_PALETTE);
74 TEST_AND_DUMP(ppfd->dwFlags, PFD_NEED_SYSTEM_PALETTE);
75 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO);
76 TEST_AND_DUMP(ppfd->dwFlags, PFD_STEREO_DONTCARE);
77 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_GDI);
78 TEST_AND_DUMP(ppfd->dwFlags, PFD_SUPPORT_OPENGL);
79 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_COPY);
80 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_EXCHANGE);
81 TEST_AND_DUMP(ppfd->dwFlags, PFD_SWAP_LAYER_BUFFERS);
82 #undef TEST_AND_DUMP
83 DPRINTF("\n");
85 DPRINTF(" - iPixelType : ");
86 switch (ppfd->iPixelType) {
87 case PFD_TYPE_RGBA: DPRINTF("PFD_TYPE_RGBA"); break;
88 case PFD_TYPE_COLORINDEX: DPRINTF("PFD_TYPE_COLORINDEX"); break;
90 DPRINTF("\n");
92 DPRINTF(" - Color : %d\n", ppfd->cColorBits);
93 DPRINTF(" - Alpha : %d\n", ppfd->cAlphaBits);
94 DPRINTF(" - Accum : %d\n", ppfd->cAccumBits);
95 DPRINTF(" - Depth : %d\n", ppfd->cDepthBits);
96 DPRINTF(" - Stencil : %d\n", ppfd->cStencilBits);
97 DPRINTF(" - Aux : %d\n", ppfd->cAuxBuffers);
99 DPRINTF(" - iLayerType : ");
100 switch (ppfd->iLayerType) {
101 case PFD_MAIN_PLANE: DPRINTF("PFD_MAIN_PLANE"); break;
102 case PFD_OVERLAY_PLANE: DPRINTF("PFD_OVERLAY_PLANE"); break;
103 case PFD_UNDERLAY_PLANE: DPRINTF("PFD_UNDERLAY_PLANE"); break;
105 DPRINTF("\n");
108 /* X11DRV_ChoosePixelFormat
110 Equivalent of glXChooseVisual
112 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
113 const PIXELFORMATDESCRIPTOR *ppfd) {
114 #define TEST_AND_ADD1(t,a) if (t) att_list[att_pos++] = a
115 #define TEST_AND_ADD2(t,a,b) if (t) { att_list[att_pos++] = a; att_list[att_pos++] = b; }
116 #define NULL_TEST_AND_ADD2(tv,a,b) att_list[att_pos++] = a; att_list[att_pos++] = ((tv) == 0 ? 0 : b)
117 #define ADD2(a,b) att_list[att_pos++] = a; att_list[att_pos++] = b
119 int att_list[64];
120 int att_pos = 0;
121 XVisualInfo *vis;
122 int i;
124 if (TRACE_ON(opengl)) {
125 TRACE("(%p,%p)\n", physDev, ppfd);
127 dump_PIXELFORMATDESCRIPTOR((PIXELFORMATDESCRIPTOR *) ppfd);
130 if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP) {
131 ERR("Flag not supported !\n");
132 /* Should SetError here... */
133 return 0;
136 /* Now, build the request to GLX */
137 TEST_AND_ADD1(ppfd->dwFlags & PFD_DOUBLEBUFFER, GLX_DOUBLEBUFFER);
138 TEST_AND_ADD1(ppfd->dwFlags & PFD_STEREO, GLX_STEREO);
139 TEST_AND_ADD1(ppfd->iPixelType == PFD_TYPE_RGBA, GLX_RGBA);
140 TEST_AND_ADD2(ppfd->iPixelType == PFD_TYPE_COLORINDEX, GLX_BUFFER_SIZE, ppfd->cColorBits);
142 NULL_TEST_AND_ADD2(ppfd->cDepthBits, GLX_DEPTH_SIZE, 8);
143 /* These flags are not supported yet...
145 NULL_TEST_AND_ADD2(ppfd->cAlphaBits, GLX_ALPHA_SIZE, 8);
146 ADD2(GLX_ACCUM_SIZE, ppfd->cAccumBits); */
147 ADD2(GLX_STENCIL_SIZE, ppfd->cStencilBits); /* now suported */
148 /* ADD2(GLX_AUX_BUFFERS, ppfd->cAuxBuffers); */
149 att_list[att_pos] = None;
151 wine_tsx11_lock(); {
153 This command cannot be used as we need to use the default visual...
154 Let's hope it at least contains some OpenGL functionnalities
156 vis = glXChooseVisual(gdi_display, DefaultScreen(gdi_display), att_list);
158 int num;
159 XVisualInfo template;
161 template.visualid = XVisualIDFromVisual(visual);
162 vis = XGetVisualInfo(gdi_display, VisualIDMask, &template, &num);
164 TRACE("Found visual : %p - returns %d\n", vis, physDev->used_visuals + 1);
166 wine_tsx11_unlock();
168 if (vis == NULL) {
169 ERR("No visual found !\n");
170 /* Should SetError here... */
171 return 0;
173 /* try to find the visualid in the already created visuals */
174 for( i=0; i<physDev->used_visuals; i++ ) {
175 if ( vis->visualid == physDev->visuals[i]->visualid ) {
176 XFree(vis);
177 return i+1;
180 /* now give up, if the maximum is reached */
181 if (physDev->used_visuals == MAX_PIXELFORMATS) {
182 ERR("Maximum number of visuals reached !\n");
183 /* Should SetError here... */
184 return 0;
186 physDev->visuals[physDev->used_visuals++] = vis;
188 return physDev->used_visuals;
191 /* X11DRV_DescribePixelFormat
193 Get the pixel-format descriptor associated to the given id
195 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
196 int iPixelFormat,
197 UINT nBytes,
198 PIXELFORMATDESCRIPTOR *ppfd) {
199 XVisualInfo *vis;
200 int value;
201 int rb,gb,bb,ab;
203 TRACE("(%p,%d,%d,%p)\n", physDev, iPixelFormat, nBytes, ppfd);
205 if (ppfd == NULL) {
206 /* The application is only querying the number of visuals */
207 return MAX_PIXELFORMATS;
210 if (nBytes < sizeof(PIXELFORMATDESCRIPTOR)) {
211 ERR("Wrong structure size !\n");
212 /* Should set error */
213 return 0;
215 if ((iPixelFormat > MAX_PIXELFORMATS) ||
216 (iPixelFormat > physDev->used_visuals + 1) ||
217 (iPixelFormat <= 0)) {
218 ERR("Wrong pixel format !\n");
219 /* Should set error */
220 return 0;
223 if (iPixelFormat == physDev->used_visuals + 1) {
224 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
226 /* Create a 'standard' X Visual */
227 wine_tsx11_lock();
228 vis = glXChooseVisual(gdi_display, DefaultScreen(gdi_display), dblBuf);
229 wine_tsx11_unlock();
231 WARN("Uninitialized Visual. Creating standard (%p) !\n", vis);
233 if (vis == NULL) {
234 ERR("Could not create standard visual !\n");
235 /* Should set error */
236 return 0;
239 physDev->visuals[physDev->used_visuals++] = vis;
241 vis = physDev->visuals[iPixelFormat - 1];
243 memset(ppfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
244 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
245 ppfd->nVersion = 1;
247 /* These flags are always the same... */
248 ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED;
249 /* Now the flags extraced from the Visual */
250 wine_tsx11_lock();
251 glXGetConfig(gdi_display, vis, GLX_DOUBLEBUFFER, &value); if (value) ppfd->dwFlags |= PFD_DOUBLEBUFFER;
252 glXGetConfig(gdi_display, vis, GLX_STEREO, &value); if (value) ppfd->dwFlags |= PFD_STEREO;
254 /* Pixel type */
255 glXGetConfig(gdi_display, vis, GLX_RGBA, &value);
256 if (value)
257 ppfd->iPixelType = PFD_TYPE_RGBA;
258 else
259 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
261 /* Color bits */
262 glXGetConfig(gdi_display, vis, GLX_BUFFER_SIZE, &value);
263 ppfd->cColorBits = value;
265 /* Red, green, blue and alpha bits / shifts */
266 if (ppfd->iPixelType == PFD_TYPE_RGBA) {
267 glXGetConfig(gdi_display, vis, GLX_RED_SIZE, &rb);
268 glXGetConfig(gdi_display, vis, GLX_GREEN_SIZE, &gb);
269 glXGetConfig(gdi_display, vis, GLX_BLUE_SIZE, &bb);
270 glXGetConfig(gdi_display, vis, GLX_ALPHA_SIZE, &ab);
272 ppfd->cRedBits = rb;
273 ppfd->cRedShift = gb + bb + ab;
274 ppfd->cBlueBits = bb;
275 ppfd->cBlueShift = ab;
276 ppfd->cGreenBits = gb;
277 ppfd->cGreenShift = bb + ab;
278 ppfd->cAlphaBits = ab;
279 ppfd->cAlphaShift = 0;
280 } else {
281 ppfd->cRedBits = 0;
282 ppfd->cRedShift = 0;
283 ppfd->cBlueBits = 0;
284 ppfd->cBlueShift = 0;
285 ppfd->cGreenBits = 0;
286 ppfd->cGreenShift = 0;
287 ppfd->cAlphaBits = 0;
288 ppfd->cAlphaShift = 0;
290 /* Accums : to do ... */
292 /* Depth bits */
293 glXGetConfig(gdi_display, vis, GLX_DEPTH_SIZE, &value);
294 ppfd->cDepthBits = value;
296 /* stencil bits */
297 glXGetConfig( gdi_display, vis, GLX_STENCIL_SIZE, &value );
298 ppfd->cStencilBits = value;
300 wine_tsx11_unlock();
302 /* Aux : to do ... */
304 ppfd->iLayerType = PFD_MAIN_PLANE;
306 if (TRACE_ON(opengl)) {
307 dump_PIXELFORMATDESCRIPTOR(ppfd);
310 return MAX_PIXELFORMATS;
313 /* X11DRV_GetPixelFormat
315 Get the pixel-format id used by this DC
317 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
318 TRACE("(%p): returns %d\n", physDev, physDev->current_pf);
320 return physDev->current_pf;
323 /* X11DRV_SetPixelFormat
325 Set the pixel-format id used by this DC
327 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
328 int iPixelFormat,
329 const PIXELFORMATDESCRIPTOR *ppfd) {
330 TRACE("(%p,%d,%p)\n", physDev, iPixelFormat, ppfd);
332 physDev->current_pf = iPixelFormat;
334 return TRUE;
337 /* X11DRV_SwapBuffers
339 Swap the buffers of this DC
341 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
342 TRACE("(%p)\n", physDev);
344 wine_tsx11_lock();
345 glXSwapBuffers(gdi_display, physDev->drawable);
346 wine_tsx11_unlock();
348 return TRUE;
351 /***********************************************************************
352 * X11DRV_setup_opengl_visual
354 * Setup the default visual used for OpenGL and Direct3D, and the desktop
355 * window (if it exists). If OpenGL isn't available, the visual is simply
356 * set to the default visual for the display
358 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
360 int err_base, evt_base;
361 XVisualInfo *visual = NULL;
363 /* In order to support OpenGL or D3D, we require a double-buffered
364 * visual */
365 wine_tsx11_lock();
366 if (glXQueryExtension(display, &err_base, &evt_base) == True)
368 int dblBuf[]={GLX_RGBA,GLX_DEPTH_SIZE,16,GLX_DOUBLEBUFFER,None};
369 visual = glXChooseVisual(display, DefaultScreen(display), dblBuf);
371 wine_tsx11_unlock();
372 return visual;
375 #else /* defined(HAVE_OPENGL) */
377 int X11DRV_ChoosePixelFormat(X11DRV_PDEVICE *physDev,
378 const PIXELFORMATDESCRIPTOR *ppfd) {
379 ERR("No OpenGL support compiled in.\n");
381 return 0;
384 int X11DRV_DescribePixelFormat(X11DRV_PDEVICE *physDev,
385 int iPixelFormat,
386 UINT nBytes,
387 PIXELFORMATDESCRIPTOR *ppfd) {
388 ERR("No OpenGL support compiled in.\n");
390 return 0;
393 int X11DRV_GetPixelFormat(X11DRV_PDEVICE *physDev) {
394 ERR("No OpenGL support compiled in.\n");
396 return 0;
399 BOOL X11DRV_SetPixelFormat(X11DRV_PDEVICE *physDev,
400 int iPixelFormat,
401 const PIXELFORMATDESCRIPTOR *ppfd) {
402 ERR("No OpenGL support compiled in.\n");
404 return FALSE;
407 BOOL X11DRV_SwapBuffers(X11DRV_PDEVICE *physDev) {
408 ERR("No OpenGL support compiled in.\n");
410 return FALSE;
413 XVisualInfo *X11DRV_setup_opengl_visual( Display *display )
415 return NULL;
418 #endif /* defined(HAVE_OPENGL) */