Start beginning of pixel shader generator..
[mesa.git] / src / mesa / drivers / dri / r300 / radeon_context.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
3
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 **************************************************************************/
29
30 /**
31 * \file radeon_context.c
32 * Common context initialization.
33 *
34 * \author Keith Whitwell <keith@tungstengraphics.com>
35 */
36
37 #include <dlfcn.h>
38
39 #include "glheader.h"
40 #include "imports.h"
41 #include "context.h"
42 #include "state.h"
43 #include "matrix.h"
44
45 #include "drivers/common/driverfuncs.h"
46 #include "swrast/swrast.h"
47
48 #include "radeon_screen.h"
49 #include "r200_context.h"
50 #include "radeon_ioctl.h"
51 #include "radeon_macros.h"
52 #include "radeon_reg.h"
53 #include "r200_state.h"
54
55 #include "utils.h"
56 #include "vblank.h"
57 #include "xmlpool.h" /* for symbolic values of enum-type options */
58
59 #define DRIVER_DATE "20040924"
60
61
62 /* Return various strings for glGetString().
63 */
64 static const GLubyte *radeonGetString(GLcontext * ctx, GLenum name)
65 {
66 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
67 static char buffer[128];
68
69 switch (name) {
70 case GL_VENDOR:
71 return (GLubyte *) "Tungsten Graphics, Inc.";
72
73 case GL_RENDERER:
74 {
75 unsigned offset;
76 GLuint agp_mode = radeon->radeonScreen->IsPCI ? 0 :
77 radeon->radeonScreen->AGPMode;
78 const char* chipname;
79
80 if (IS_FAMILY_R300(radeon))
81 chipname = "R300";
82 else
83 chipname = "R200";
84
85 offset = driGetRendererString(buffer, chipname, DRIVER_DATE,
86 agp_mode);
87
88 sprintf(&buffer[offset], " %sTCL",
89 !(radeon->TclFallback & RADEON_TCL_FALLBACK_TCL_DISABLE)
90 ? "" : "NO-");
91
92 return (GLubyte *) buffer;
93 }
94
95 default:
96 return NULL;
97 }
98 }
99
100
101 /* Return the width and height of the given buffer.
102 */
103 static void radeonGetBufferSize(GLframebuffer * buffer,
104 GLuint * width, GLuint * height)
105 {
106 GET_CURRENT_CONTEXT(ctx);
107 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
108
109 LOCK_HARDWARE(radeon);
110 *width = radeon->dri.drawable->w;
111 *height = radeon->dri.drawable->h;
112 UNLOCK_HARDWARE(radeon);
113 }
114
115
116 /* Initialize the driver's misc functions.
117 */
118 static void radeonInitDriverFuncs(struct dd_function_table *functions)
119 {
120 functions->GetBufferSize = radeonGetBufferSize;
121 functions->ResizeBuffers = _swrast_alloc_buffers;
122 functions->GetString = radeonGetString;
123 }
124
125
126 static int get_ust_nop(int64_t * ust)
127 {
128 *ust = 1;
129 return 0;
130 }
131
132 /**
133 * Create and initialize all common fields of the context,
134 * including the Mesa context itself.
135 */
136 GLboolean radeonInitContext(radeonContextPtr radeon,
137 struct dd_function_table* functions,
138 const __GLcontextModes * glVisual,
139 __DRIcontextPrivate * driContextPriv,
140 void *sharedContextPrivate)
141 {
142 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
143 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
144 GLcontext* ctx;
145 GLcontext* shareCtx;
146 int fthrottle_mode;
147
148 /* Fill in additional standard functions. */
149 radeonInitDriverFuncs(functions);
150
151 /* Allocate and initialize the Mesa context */
152 if (sharedContextPrivate)
153 shareCtx = ((radeonContextPtr)sharedContextPrivate)->glCtx;
154 else
155 shareCtx = NULL;
156 radeon->glCtx = _mesa_create_context(glVisual, shareCtx,
157 functions, (void *)radeon);
158 if (!radeon->glCtx)
159 return GL_FALSE;
160
161 ctx = radeon->glCtx;
162 driContextPriv->driverPrivate = radeon;
163
164 /* DRI fields */
165 radeon->dri.context = driContextPriv;
166 radeon->dri.screen = sPriv;
167 radeon->dri.drawable = NULL; /* Set by XMesaMakeCurrent */
168 radeon->dri.hwContext = driContextPriv->hHWContext;
169 radeon->dri.hwLock = &sPriv->pSAREA->lock;
170 radeon->dri.fd = sPriv->fd;
171 radeon->dri.drmMinor = sPriv->drmMinor;
172
173 radeon->radeonScreen = screen;
174 radeon->sarea = (drm_radeon_sarea_t *) ((GLubyte *) sPriv->pSAREA +
175 screen->sarea_priv_offset);
176
177 /* Setup IRQs */
178 fthrottle_mode = driQueryOptioni(&radeon->optionCache, "fthrottle_mode");
179 radeon->iw.irq_seq = -1;
180 radeon->irqsEmitted = 0;
181 radeon->do_irqs = (radeon->dri.drmMinor >= 6 &&
182 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
183 radeon->radeonScreen->irq);
184
185 radeon->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
186
187 if (!radeon->do_irqs)
188 fprintf(stderr,
189 "IRQ's not enabled, falling back to %s: %d %d %d\n",
190 radeon->do_usleeps ? "usleeps" : "busy waits",
191 radeon->dri.drmMinor,
192 fthrottle_mode, radeon->radeonScreen->irq);
193
194 radeon->vblank_flags = (radeon->radeonScreen->irq != 0)
195 ? driGetDefaultVBlankFlags(&radeon->optionCache) : VBLANK_FLAG_NO_IRQ;
196
197 radeon->get_ust =
198 (PFNGLXGETUSTPROC) glXGetProcAddress((const GLubyte *)
199 "__glXGetUST");
200 if (radeon->get_ust == NULL) {
201 radeon->get_ust = get_ust_nop;
202 }
203 (*radeon->get_ust) (&radeon->swap_ust);
204
205 return GL_TRUE;
206 }
207
208
209 /**
210 * Cleanup common context fields.
211 * Called by r200DestroyContext/r300DestroyContext
212 */
213 void radeonCleanupContext(radeonContextPtr radeon)
214 {
215 /* free the Mesa context */
216 radeon->glCtx->DriverCtx = NULL;
217 _mesa_destroy_context(radeon->glCtx);
218
219 if (radeon->state.scissor.pClipRects) {
220 FREE(radeon->state.scissor.pClipRects);
221 radeon->state.scissor.pClipRects = 0;
222 }
223 }
224
225
226 /**
227 * Swap front and back buffer.
228 */
229 void radeonSwapBuffers(__DRIdrawablePrivate * dPriv)
230 {
231 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
232 radeonContextPtr radeon;
233 GLcontext *ctx;
234
235 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
236 ctx = radeon->glCtx;
237
238 if (ctx->Visual.doubleBufferMode) {
239 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
240 if (radeon->doPageFlip) {
241 radeonPageFlip(dPriv);
242 } else {
243 radeonCopyBuffer(dPriv);
244 }
245 }
246 } else {
247 /* XXX this shouldn't be an error but we can't handle it for now */
248 _mesa_problem(NULL, "%s: drawable has no context!",
249 __FUNCTION__);
250 }
251 }
252
253
254 /* Force the context `c' to be the current context and associate with it
255 * buffer `b'.
256 */
257 GLboolean radeonMakeCurrent(__DRIcontextPrivate * driContextPriv,
258 __DRIdrawablePrivate * driDrawPriv,
259 __DRIdrawablePrivate * driReadPriv)
260 {
261 if (driContextPriv) {
262 radeonContextPtr radeon =
263 (radeonContextPtr) driContextPriv->driverPrivate;
264
265 if (RADEON_DEBUG & DEBUG_DRI)
266 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
267 radeon->glCtx);
268
269 if (radeon->dri.drawable != driDrawPriv) {
270 driDrawableInitVBlank(driDrawPriv,
271 radeon->vblank_flags);
272 radeon->dri.drawable = driDrawPriv;
273
274 if (IS_FAMILY_R200(radeon)) {
275 r200UpdateWindow(radeon->glCtx);
276 r200UpdateViewportOffset(radeon->glCtx);
277 }
278 }
279
280 _mesa_make_current2(radeon->glCtx,
281 (GLframebuffer *) driDrawPriv->
282 driverPrivate,
283 (GLframebuffer *) driReadPriv->
284 driverPrivate);
285
286 if (!radeon->glCtx->Viewport.Width) {
287 _mesa_set_viewport(radeon->glCtx, 0, 0,
288 driDrawPriv->w, driDrawPriv->h);
289 }
290
291 _mesa_update_state(radeon->glCtx);
292
293 if (IS_FAMILY_R200(radeon))
294 r200ValidateState(radeon->glCtx);
295
296 } else {
297 if (RADEON_DEBUG & DEBUG_DRI)
298 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
299 _mesa_make_current(0, 0);
300 }
301
302 if (RADEON_DEBUG & DEBUG_DRI)
303 fprintf(stderr, "End %s\n", __FUNCTION__);
304 return GL_TRUE;
305 }
306
307 /* Force the context `c' to be unbound from its buffer.
308 */
309 GLboolean radeonUnbindContext(__DRIcontextPrivate * driContextPriv)
310 {
311 radeonContextPtr radeon = (radeonContextPtr) driContextPriv->driverPrivate;
312
313 if (RADEON_DEBUG & DEBUG_DRI)
314 fprintf(stderr, "%s ctx %p\n", __FUNCTION__,
315 radeon->glCtx);
316
317 return GL_TRUE;
318 }
319