revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / arch / all-hosted / libs / hostgl / hostgl_glacreatecontext.c
blob6ef54f72d35d758ab3dfcbc594b97a003dd156e6
1 /*
2 Copyright © 2011-2017, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define AROS_TAGRETURNTYPE GLAContext
8 #include <aros/debug.h>
10 #include <proto/exec.h>
11 #include <proto/oop.h>
13 #include "hostgl_ctx_manager.h"
14 #include "hostgl_funcs.h"
15 #include "hostgl_support.h"
17 #include <x11gfx_bitmapclass.h>
19 /*****************************************************************************
21 NAME */
23 GLAContext glACreateContext(
25 /* SYNOPSIS */
26 struct TagItem *tagList)
28 /* FUNCTION
30 Crates a GL rendering context. Whether the rendering will be software
31 or hardware based depends on the gallium.library returning a module
32 best suited.
34 INPUTS
36 tagList - a pointer to tags to be used during creation.
38 TAGS
40 GLA_Left - specifies the left rendering offset on the rastport.
41 Typically equals to window->BorderLeft.
43 GLA_Top - specifies the top rendering offset on the rastport.
44 Typically equals to window->BorderTop.
46 GLA_Right - specifies the right rendering offset on the rastport.
47 Typically equals to window->BorderRight.
49 GLA_Bottom - specifies the bottom rendering offset on the rastport.
50 Typically equals to window->BorderBottom.
52 GLA_Width - specifies the width of the rendering area.
53 GLA_Width + GLA_Left + GLA_Right should equal the width of
54 the rastport. The GLA_Width is interchangable at cration
55 time with GLA_Right. Later durring window resizing, width
56 is calculated from scalled left, righ and window width.
58 GLA_Height - specifies the height of the rendering area.
59 GLA_Height + GLA_Top + GLA_Bottom should equal the height
60 of the rastport. The GLA_Height is interchangable at
61 cration time with GLA_Bottom. Later durring window resizing
62 , height is calculated from scalled top, bottom and window
63 height.
65 GLA_Screen - pointer to Screen onto which scene is to be rendered. When
66 selecting RastPort has lower priority than GLA_Window.
68 GLA_Window - pointer to Window onto which scene is to be rendered. Must
69 be provided.
71 GLA_RastPort - ignored. Use GLA_Window.
73 GLA_DoubleBuf - ignored. All rendering is always double buffered.
75 GLA_RGBMode - ignored. All rendering is done in RGB. Indexed modes are
76 not supported.
78 GLA_AlphaFlag - ignored. All rendering is done with alpha channel.
80 GLA_NoDepth - disables the depth/Z buffer. Depth buffer is enabled by
81 default and is 16 or 24 bit based on rendering
82 capabilities.
84 GLA_NoStencil - disables the stencil buffer. Stencil buffer is enabled
85 by default.
87 GLA_NoAccum - disables the accumulation buffer. Accumulation buffer is
88 enabled by default.
90 RESULT
92 A valid GL context or NULL of creation was not successful.
94 BUGS
96 INTERNALS
98 HISTORY
100 *****************************************************************************/
102 LONG screen;
103 struct hostgl_context *ctx = NULL;
104 LONG numreturned;
105 Display *dsp = NULL;
106 const LONG fbattributessize = 40;
107 LONG fbattributes[fbattributessize];
108 #if defined(RENDERER_SEPARATE_X_WINDOW)
109 XVisualInfo *visinfo;
110 XSetWindowAttributes swa;
111 LONG swamask;
112 #endif
114 HostGL_Lock();
116 /* Standard glA initialization */
118 /* Allocate HostGL context struct initialized to zeros */
119 if (!(ctx = (struct hostgl_context *)AllocVec(sizeof(struct hostgl_context), MEMF_PUBLIC | MEMF_CLEAR)))
121 bug("glACreateContext: ERROR - failed to allocate Context\n");
122 goto error_out;
125 ctx->HiddX11BitMapAB = OOP_ObtainAttrBase(IID_Hidd_BitMap_X11);
127 HostGLSelectRastPort(ctx, tagList);
128 if (!ctx->visible_rp)
130 bug("glACreateContext: ERROR - failed to select visible rastport\n");
131 goto error_out;
134 HostGLStandardInit(ctx, tagList);
136 ctx->framebuffer = (struct hostgl_framebuffer *)AllocVec(sizeof(struct hostgl_framebuffer), MEMF_PUBLIC | MEMF_CLEAR);
137 if (!ctx->framebuffer)
139 bug("glACreateContext: ERROR - failed to create frame buffer\n");
140 goto error_out;
143 HostGLRecalculateBufferWidthHeight(ctx);
145 if (!HostGL_FillFBAttributes(fbattributes, fbattributessize, tagList))
147 bug("glACreateContext: ERROR - failed to fill FB attributes\n");
148 goto error_out;
151 /* X/GLX initialization */
153 /* Get connection with the server */
154 dsp = HostGL_GetGlobalX11Display();
155 screen = DefaultScreen(dsp);
157 /* Choose fb config */
158 ctx->framebuffer->fbconfigs = GLXCALL(glXChooseFBConfig, dsp, screen, fbattributes, &numreturned);
160 if (ctx->framebuffer->fbconfigs == NULL)
162 bug("glACreateContext: ERROR - failed to retrieve fbconfigs\n");
163 goto error_out;
166 #if defined(RENDERER_SEPARATE_X_WINDOW)
167 visinfo = GLXCALL(glXGetVisualFromFBConfig, dsp, ctx->framebuffer>fbconfigs[0]);
169 swa.colormap = XCALL(XCreateColormap, dsp, RootWindow(dsp, screen), visinfo->visual, AllocNone);
170 swamask = CWColormap;
172 /* Create X window */
173 ctx->XWindow = XCALL(XCreateWindow, dsp, RootWindow(dsp, screen),
174 ctx->left, ctx->top, ctx->framebuffer>width, ctx->framebuffer>height, 0,
175 visinfo->depth, InputOutput, visinfo->visual, swamask, &swa);
177 /* Create GLX window */
178 ctx->glXWindow = GLXCALL(glXCreateWindow, dsp, ctx->framebuffer>fbconfigs[0], ctx->XWindow, NULL);
180 /* Map (show) the window */
181 XCALL(XMapWindow, dsp, ctx->XWindow);
183 XCALL(XFlush, dsp);
185 /* Create GL context */
186 ctx->glXctx = GLXCALL(glXCreateNewContext, dsp, ctx->framebuffer>fbconfigs[0], GLX_RGBA_TYPE, NULL, True);
187 #endif
189 #if defined(RENDERER_PBUFFER_WPA)
190 /* Create GLX Pbuffer */
191 HostGL_AllocatePBuffer(ctx);
193 /* Create GL context */
194 ctx->glXctx = GLXCALL(glXCreateNewContext, dsp, ctx->framebuffer>fbconfigs[0], GLX_RGBA_TYPE, NULL, True);
195 #endif
197 #if defined(RENDERER_PIXMAP_BLIT)
198 ctx->visinfo = GLXCALL(glXGetVisualFromFBConfig, dsp, ctx->framebuffer->fbconfigs[0]);
200 /* Create GLX Pixmap */
201 HostGL_AllocatePixmap(ctx);
203 /* Create GL context */
204 ctx->glXctx = GLXCALL(glXCreateNewContext, dsp, ctx->framebuffer->fbconfigs[0], GLX_RGBA_TYPE, NULL, True);
205 #endif
207 if (!ctx->glXctx)
209 bug("glACreateContext: ERROR - failed to create GLX context\n");
210 goto error_out;
213 D(bug("[HostGL] TASK: 0x%x, CREATE 0x%x\n", FindTask(NULL), ctx->glXctx));
215 HostGL_UnLock();
217 return (GLAContext)ctx;
219 error_out:
220 #if defined(RENDERER_SEPARATE_X_WINDOW)
221 if (ctx && ctx->glXWindow) GLXCALL(glXDestroyWindow, dsp, ctx->glXWindow);
222 if (ctx && ctx->XWindow) XCALL(XDestroyWindow, dsp, ctx->XWindow);
223 #endif
224 #if defined(RENDERER_PBUFFER_WPA)
225 if (ctx) HostGL_DeAllocatePBuffer(ctx);
226 #endif
227 #if defined(RENDERER_PIXMAP_BLIT)
228 if (ctx) HostGL_DeAllocatePixmap(ctx);
229 #endif
231 if (ctx->HiddX11BitMapAB)
232 OOP_ReleaseAttrBase(IID_Hidd_BitMap_X11);
234 if (ctx->framebuffer)
236 if (ctx->framebuffer->fbconfigs) XCALL(XFree, ctx->framebuffer->fbconfigs);
237 FreeVec(ctx->framebuffer);
239 if (ctx) HostGLFreeContext(ctx);
241 HostGL_UnLock();
242 return (GLAContext)NULL;