comparison src/video/cybergfx/SDL_cgxgl.c @ 255:dcb5e869f8b5

Updated Amiga port by Gabriele Greco
author Sam Lantinga <slouken@libsdl.org>
date Sun, 16 Dec 2001 20:00:27 +0000
parents e8157fcb3114
children f6ffac90895c
comparison
equal deleted inserted replaced
254:4fc12b8edf74 255:dcb5e869f8b5
23 #ifdef SAVE_RCSID 23 #ifdef SAVE_RCSID
24 static char rcsid = 24 static char rcsid =
25 "@(#) $Id$"; 25 "@(#) $Id$";
26 #endif 26 #endif
27 27
28 // #include <stdlib.h> /* For getenv() prototype */ 28 /* StormMesa implementation of SDL OpenGL support */
29 // #include <string.h> 29
30
31 #include "SDL_events_c.h"
32 #include "SDL_error.h" 30 #include "SDL_error.h"
31 #include "SDL_cgxgl_c.h"
33 #include "SDL_cgxvideo.h" 32 #include "SDL_cgxvideo.h"
34 #include "SDL_cgxgl_c.h" 33
35 34 #ifdef HAVE_OPENGL
36 #define DEFAULT_OPENGL "libGL.so.1" 35 AmigaMesaContext glcont=NULL;
37 36 #endif
38 /* return the preferred visual to use for openGL graphics */ 37
39 void *CGX_GL_GetVisual(_THIS) 38 /* Init OpenGL */
40 { 39 int CGX_GL_Init(_THIS)
41 #ifdef HAVE_OPENGL 40 {
42 /* 64 seems nice. */ 41 #ifdef HAVE_OPENGL
43 int attribs[64]; 42 int i = 0;
44 int i; 43 struct TagItem attributes [ 14 ]; /* 14 should be more than enough :) */
45 44 struct Window *win = (struct Window *)SDL_Window;
46 /* load the gl driver from a default path */ 45
47 if ( ! this->gl_config.driver_loaded ) { 46 // default config. Always used...
48 /* no driver has been loaded, use default (ourselves) */ 47 attributes[i].ti_Tag = AMA_Window; attributes[i++].ti_Data = (unsigned long)win;
49 if ( X11_GL_LoadLibrary(this, NULL) < 0 ) { 48 attributes[i].ti_Tag = AMA_Left; attributes[i++].ti_Data = 0;
50 return NULL; 49 attributes[i].ti_Tag = AMA_Bottom; attributes[i++].ti_Data = 0;
51 } 50 attributes[i].ti_Tag = AMA_Width; attributes[i++].ti_Data = win->Width-win->BorderLeft-win->BorderRight;
52 } 51 attributes[i].ti_Tag = AMA_Height; attributes[i++].ti_Data = win->Height-win->BorderBottom-win->BorderTop;
53 52 attributes[i].ti_Tag = AMA_DirectRender; attributes[i++].ti_Data = GL_TRUE;
54 /* See if we already have a window which we must use */ 53
55 if ( SDL_windowid ) { 54 // double buffer ?
56 XWindowAttributes a; 55 attributes[i].ti_Tag = AMA_DoubleBuf;
57 XVisualInfo vi_in; 56 if ( this->gl_config.double_buffer ) {
58 int out_count; 57 attributes[i++].ti_Data = GL_TRUE;
59 58 }
60 XGetWindowAttributes(SDL_Display, SDL_Window, &a); 59 else {
61 vi_in.screen = SDL_Screen; 60 attributes[i++].ti_Data = GL_FALSE;
62 vi_in.visualid = XVisualIDFromVisual(a.visual); 61 }
63 glx_visualinfo = XGetVisualInfo(SDL_Display, 62 // RGB(A) Mode ?
64 VisualScreenMask|VisualIDMask, &vi_in, &out_count); 63 attributes[i].ti_Tag = AMA_RGBMode;
65 return glx_visualinfo; 64 if ( this->gl_config.red_size != 0 &&
66 } 65 this->gl_config.blue_size != 0 &&
67 66 this->gl_config.green_size != 0 ) {
68 /* Setup our GLX attributes according to the gl_config. */ 67 attributes[i++].ti_Data = GL_TRUE;
69 i = 0; 68 }
70 attribs[i++] = GLX_RGBA; 69 else {
71 attribs[i++] = GLX_RED_SIZE; 70 attributes[i++].ti_Data = GL_FALSE;
72 attribs[i++] = this->gl_config.red_size; 71 }
73 attribs[i++] = GLX_GREEN_SIZE; 72 // no depth buffer ?
74 attribs[i++] = this->gl_config.green_size; 73 if ( this->gl_config.depth_size == 0 ) {
75 attribs[i++] = GLX_BLUE_SIZE; 74 attributes[i].ti_Tag = AMA_NoDepth;
76 attribs[i++] = this->gl_config.blue_size; 75 attributes[i++].ti_Data = GL_TRUE;
77 76 }
78 if( this->gl_config.alpha_size ) { 77 // no stencil buffer ?
79 attribs[i++] = GLX_ALPHA_SIZE; 78 if ( this->gl_config.stencil_size == 0 ) {
80 attribs[i++] = this->gl_config.alpha_size; 79 attributes[i].ti_Tag = AMA_NoStencil;
81 } 80 attributes[i++].ti_Data = GL_TRUE;
82 81 }
83 if( this->gl_config.buffer_size ) { 82 // no accum buffer ?
84 attribs[i++] = GLX_BUFFER_SIZE; 83 if ( this->gl_config.accum_red_size != 0 &&
85 attribs[i++] = this->gl_config.buffer_size; 84 this->gl_config.accum_blue_size != 0 &&
86 } 85 this->gl_config.accum_green_size != 0 ) {
87 86 attributes[i].ti_Tag = AMA_NoAccum;
88 if( this->gl_config.double_buffer ) { 87 attributes[i++].ti_Data = GL_TRUE;
89 attribs[i++] = GLX_DOUBLEBUFFER; 88 }
90 } 89 // done...
91 90 attributes[i].ti_Tag = TAG_DONE;
92 attribs[i++] = GLX_DEPTH_SIZE; 91
93 attribs[i++] = this->gl_config.depth_size; 92 glcont = AmigaMesaCreateContext(attributes);
94 93 if ( glcont == NULL ) {
95 if( this->gl_config.stencil_size ) { 94 SDL_SetError("Couldn't create OpenGL context");
96 attribs[i++] = GLX_STENCIL_SIZE; 95 return(-1);
97 attribs[i++] = this->gl_config.stencil_size; 96 }
98 } 97 this->gl_data->gl_active = 1;
99 98 this->gl_config.driver_loaded = 1;
100 if( this->gl_config.accum_red_size ) { 99
101 attribs[i++] = GLX_ACCUM_RED_SIZE; 100 return(0);
102 attribs[i++] = this->gl_config.accum_red_size;
103 }
104
105 if( this->gl_config.accum_green_size ) {
106 attribs[i++] = GLX_ACCUM_GREEN_SIZE;
107 attribs[i++] = this->gl_config.accum_green_size;
108 }
109
110 if( this->gl_config.accum_blue_size ) {
111 attribs[i++] = GLX_ACCUM_BLUE_SIZE;
112 attribs[i++] = this->gl_config.accum_blue_size;
113 }
114
115 if( this->gl_config.accum_alpha_size ) {
116 attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
117 attribs[i++] = this->gl_config.accum_alpha_size;
118 }
119
120 attribs[i++] = None;
121
122 glx_visualinfo = this->gl_data->glXChooseVisual(GFX_Display,
123 SDL_Screen, attribs);
124 if( !glx_visualinfo ) {
125 SDL_SetError( "Couldn't find matching GLX visual");
126 return NULL;
127 }
128 return glx_visualinfo;
129 #else 101 #else
130 SDL_SetError("CGX driver is not yet supporting OpenGL"); 102 SDL_SetError("OpenGL support not configured");
131 return NULL; 103 return(-1);
132 #endif 104 #endif
133 } 105 }
134 106
135 int CGX_GL_CreateWindow(_THIS, int w, int h) 107 /* Quit OpenGL */
136 { 108 void CGX_GL_Quit(_THIS)
137 int retval; 109 {
138 #ifdef HAVE_OPENGL 110 #ifdef HAVE_OPENGL
139 XSetWindowAttributes attributes; 111 if ( glcont != NULL ) {
140 unsigned long mask; 112 AmigaMesaDestroyContext(glcont);
141 unsigned long black; 113 glcont = NULL;
142 114 this->gl_data->gl_active = 0;
143 black = (glx_visualinfo->visual == DefaultVisual(SDL_Display, 115 this->gl_config.driver_loaded = 0;
144 SDL_Screen)) 116 }
145 ? BlackPixel(SDL_Display, SDL_Screen) : 0; 117 #endif
146 attributes.background_pixel = black; 118 }
147 attributes.border_pixel = black; 119
148 attributes.colormap = SDL_XColorMap; 120 /* Attach context to another window */
149 mask = CWBackPixel | CWBorderPixel | CWColormap; 121 int CGX_GL_Update(_THIS)
150 122 {
151 SDL_Window = XCreateWindow(SDL_Display, WMwindow, 123 #ifdef HAVE_OPENGL
152 0, 0, w, h, 0, glx_visualinfo->depth, 124 struct TagItem tags[2];
153 InputOutput, glx_visualinfo->visual, 125 struct Window *win = (struct Window*)SDL_Window;
154 mask, &attributes); 126 if(glcont == NULL) {
155 if ( !SDL_Window ) { 127 return -1; //should never happen
156 SDL_SetError("Could not create window"); 128 }
157 return -1; 129 tags[0].ti_Tag = AMA_Window;
158 } 130 tags[0].ti_Data = (unsigned long)win;
159 retval = 0; 131 tags[1].ti_Tag = TAG_DONE;
132 AmigaMesaSetRast(glcont, tags);
133
134 return 0;
160 #else 135 #else
161 SDL_SetError("CGX driver is not yet supporting OpenGL"); 136 SDL_SetError("OpenGL support not configured");
162 retval = -1; 137 return -1;
163 #endif 138 #endif
164 return(retval);
165 }
166
167 int CGX_GL_CreateContext(_THIS)
168 {
169 int retval;
170 #ifdef HAVE_OPENGL
171 /* We do this to create a clean separation between X and GLX errors. */
172 XSync( SDL_Display, False );
173 glx_context = this->gl_data->glXCreateContext(GFX_Display,
174 glx_visualinfo, NULL, True);
175 XSync( GFX_Display, False );
176
177 if (glx_context == NULL) {
178 SDL_SetError("Could not create GL context");
179 return -1;
180 }
181
182 gl_active = 1;
183 #else
184 SDL_SetError("CGX driver is not yet supporting OpenGL");
185 #endif
186 if ( gl_active ) {
187 retval = 0;
188 } else {
189 retval = -1;
190 }
191 return(retval);
192 }
193
194 void CGX_GL_Shutdown(_THIS)
195 {
196 #ifdef HAVE_OPENGL
197 /* Clean up OpenGL */
198 if( glx_context ) {
199 this->gl_data->glXMakeCurrent(GFX_Display, None, NULL);
200
201 if (glx_context != NULL)
202 this->gl_data->glXDestroyContext(GFX_Display, glx_context);
203
204 if( this->gl_data->glXReleaseBuffersMESA ) {
205 this->gl_data->glXReleaseBuffersMESA(GFX_Display,SDL_Window);
206 }
207 glx_context = NULL;
208 }
209 gl_active = 0;
210 #endif /* HAVE_OPENGL */
211 } 139 }
212 140
213 #ifdef HAVE_OPENGL 141 #ifdef HAVE_OPENGL
214 142
215 /* Make the current context active */ 143 /* Make the current context active */
216 int CGX_GL_MakeCurrent(_THIS) 144 int CGX_GL_MakeCurrent(_THIS)
217 { 145 {
218 int retval; 146 if(glcont == NULL)
219 147 return -1;
220 retval = 0; 148
221 if ( ! this->gl_data->glXMakeCurrent(GFX_Display, 149 AmigaMesaMakeCurrent(glcont, glcont->buffer);
222 SDL_Window, glx_context) ) { 150 return 0;
223 SDL_SetError("Unable to make GL context current");
224 retval = -1;
225 }
226 XSync( GFX_Display, False );
227
228 /* More Voodoo X server workarounds... Grr... */
229 SDL_Lock_EventThread();
230 X11_CheckDGAMouse(this);
231 SDL_Unlock_EventThread();
232
233 return(retval);
234 }
235
236 /* Get attribute data from glX. */
237 int CGX_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
238 {
239 int retval;
240 int glx_attrib = None;
241
242 switch( attrib ) {
243 case SDL_GL_RED_SIZE:
244 glx_attrib = GLX_RED_SIZE;
245 break;
246 case SDL_GL_GREEN_SIZE:
247 glx_attrib = GLX_GREEN_SIZE;
248 break;
249 case SDL_GL_BLUE_SIZE:
250 glx_attrib = GLX_BLUE_SIZE;
251 break;
252 case SDL_GL_ALPHA_SIZE:
253 glx_attrib = GLX_ALPHA_SIZE;
254 break;
255 case SDL_GL_DOUBLEBUFFER:
256 glx_attrib = GLX_DOUBLEBUFFER;
257 break;
258 case SDL_GL_BUFFER_SIZE:
259 glx_attrib = GLX_BUFFER_SIZE;
260 break;
261 case SDL_GL_DEPTH_SIZE:
262 glx_attrib = GLX_DEPTH_SIZE;
263 break;
264 case SDL_GL_STENCIL_SIZE:
265 glx_attrib = GLX_STENCIL_SIZE;
266 break;
267 case SDL_GL_ACCUM_RED_SIZE:
268 glx_attrib = GLX_ACCUM_RED_SIZE;
269 break;
270 case SDL_GL_ACCUM_GREEN_SIZE:
271 glx_attrib = GLX_ACCUM_GREEN_SIZE;
272 break;
273 case SDL_GL_ACCUM_BLUE_SIZE:
274 glx_attrib = GLX_ACCUM_BLUE_SIZE;
275 break;
276 case SDL_GL_ACCUM_ALPHA_SIZE:
277 glx_attrib = GLX_ACCUM_ALPHA_SIZE;
278 break;
279 default:
280 return(-1);
281 }
282
283 retval = this->gl_data->glXGetConfig(GFX_Display, glx_visualinfo, glx_attrib, value);
284
285 return retval;
286 } 151 }
287 152
288 void CGX_GL_SwapBuffers(_THIS) 153 void CGX_GL_SwapBuffers(_THIS)
289 { 154 {
290 this->gl_data->glXSwapBuffers(GFX_Display, SDL_Window); 155 AmigaMesaSwapBuffers(glcont);
156 }
157
158 int CGX_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value) {
159 GLenum mesa_attrib;
160
161 switch(attrib) {
162 case SDL_GL_RED_SIZE:
163 mesa_attrib = GL_RED_BITS;
164 break;
165 case SDL_GL_GREEN_SIZE:
166 mesa_attrib = GL_GREEN_BITS;
167 break;
168 case SDL_GL_BLUE_SIZE:
169 mesa_attrib = GL_BLUE_BITS;
170 break;
171 case SDL_GL_ALPHA_SIZE:
172 mesa_attrib = GL_ALPHA_BITS;
173 break;
174 case SDL_GL_DOUBLEBUFFER:
175 mesa_attrib = GL_DOUBLEBUFFER;
176 break;
177 case SDL_GL_DEPTH_SIZE:
178 mesa_attrib = GL_DEPTH_BITS;
179 break;
180 case SDL_GL_STENCIL_SIZE:
181 mesa_attrib = GL_STENCIL_BITS;
182 break;
183 case SDL_GL_ACCUM_RED_SIZE:
184 mesa_attrib = GL_ACCUM_RED_BITS;
185 break;
186 case SDL_GL_ACCUM_GREEN_SIZE:
187 mesa_attrib = GL_ACCUM_GREEN_BITS;
188 break;
189 case SDL_GL_ACCUM_BLUE_SIZE:
190 mesa_attrib = GL_ACCUM_BLUE_BITS;
191 break;
192 case SDL_GL_ACCUM_ALPHA_SIZE:
193 mesa_attrib = GL_ACCUM_ALPHA_BITS;
194 break;
195 default :
196 return -1;
197 }
198
199 AmigaMesaGetConfig(glcont->visual, mesa_attrib, value);
200 return 0;
201 }
202
203 void *CGX_GL_GetProcAddress(_THIS, const char *proc) {
204 void *func = NULL;
205 func = AmiGetGLProc(proc);
206 return func;
207 }
208
209 int CGX_GL_LoadLibrary(_THIS, const char *path) {
210 /* Library is always open */
211 this->gl_config.driver_loaded = 1;
212
213 return 0;
291 } 214 }
292 215
293 #endif /* HAVE_OPENGL */ 216 #endif /* HAVE_OPENGL */
294 217
295 void CGX_GL_UnloadLibrary(_THIS)
296 {
297 #ifdef HAVE_OPENGL
298 if ( this->gl_config.driver_loaded ) {
299 dlclose(this->gl_config.dll_handle);
300
301 this->gl_data->glXChooseVisual = NULL;
302 this->gl_data->glXCreateContext = NULL;
303 this->gl_data->glXDestroyContext = NULL;
304 this->gl_data->glXMakeCurrent = NULL;
305 this->gl_data->glXSwapBuffers = NULL;
306
307 this->gl_config.dll_handle = NULL;
308 this->gl_config.driver_loaded = 0;
309 }
310 #endif
311 }
312
313 #ifdef HAVE_OPENGL
314
315 /* Passing a NULL path means load pointers from the application */
316 int CGX_GL_LoadLibrary(_THIS, const char* path)
317 {
318 void* handle;
319 int dlopen_flags;
320
321 if ( gl_active ) {
322 SDL_SetError("OpenGL context already created");
323 return -1;
324 }
325
326 #ifdef RTLD_GLOBAL
327 dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
328 #else
329 dlopen_flags = RTLD_LAZY;
330 #endif
331 handle = dlopen(path, dlopen_flags);
332 /* Catch the case where the application isn't linked with GL */
333 if ( (dlsym(handle, "glXChooseVisual") == NULL) && (path == NULL) ) {
334 dlclose(handle);
335 path = getenv("SDL_VIDEO_GL_DRIVER");
336 if ( path == NULL ) {
337 path = DEFAULT_OPENGL;
338 }
339 handle = dlopen(path, dlopen_flags);
340 }
341 if ( handle == NULL ) {
342 SDL_SetError("Could not load OpenGL library");
343 return -1;
344 }
345
346 /* Unload the old driver and reset the pointers */
347 X11_GL_UnloadLibrary(this);
348
349 /* Load new function pointers */
350 this->gl_data->glXChooseVisual = dlsym(handle, "glXChooseVisual");
351 this->gl_data->glXCreateContext = dlsym(handle, "glXCreateContext");
352 this->gl_data->glXDestroyContext = dlsym(handle, "glXDestroyContext");
353 this->gl_data->glXMakeCurrent = dlsym(handle, "glXMakeCurrent");
354 this->gl_data->glXSwapBuffers = dlsym(handle, "glXSwapBuffers");
355 this->gl_data->glXGetConfig = dlsym(handle, "glXGetConfig");
356 /* We don't compare below for this in case we're not using Mesa. */
357 this->gl_data->glXReleaseBuffersMESA = dlsym( handle, "glXReleaseBuffersMESA" );
358
359 if ( (this->gl_data->glXChooseVisual == NULL) ||
360 (this->gl_data->glXCreateContext == NULL) ||
361 (this->gl_data->glXDestroyContext == NULL) ||
362 (this->gl_data->glXMakeCurrent == NULL) ||
363 (this->gl_data->glXSwapBuffers == NULL) ||
364 (this->gl_data->glXGetConfig == NULL) ) {
365 SDL_SetError("Could not retrieve OpenGL functions");
366 return -1;
367 }
368
369 this->gl_config.dll_handle = handle;
370 this->gl_config.driver_loaded = 1;
371 if ( path ) {
372 strncpy(this->gl_config.driver_path, path,
373 sizeof(this->gl_config.driver_path)-1);
374 } else {
375 strcpy(this->gl_config.driver_path, "");
376 }
377 return 0;
378 }
379
380 void *CGX_GL_GetProcAddress(_THIS, const char* proc)
381 {
382 void* handle;
383
384 handle = this->gl_config.dll_handle;
385
386 return dlsym(handle, proc);
387 }
388
389 #endif /* HAVE_OPENGL */