nouveau: add nouveau_mem_alloc/free debugging
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_context.c
1 /**************************************************************************
2
3 Copyright 2006 Stephane Marchesin
4 All Rights Reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 on the rights to use, copy, modify, merge, publish, distribute, sub
10 license, and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice (including the next
14 paragraph) shall be included in all copies or substantial portions of the
15 Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
21 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 **************************************************************************/
26
27 #include "glheader.h"
28 #include "context.h"
29 #include "simple_list.h"
30 #include "imports.h"
31 #include "matrix.h"
32 #include "swrast/swrast.h"
33 #include "swrast_setup/swrast_setup.h"
34 #include "array_cache/acache.h"
35 #include "framebuffer.h"
36
37 #include "tnl/tnl.h"
38 #include "tnl/t_pipeline.h"
39 #include "tnl/t_vp_build.h"
40
41 #include "drivers/common/driverfuncs.h"
42
43 #include "nouveau_context.h"
44 #include "nouveau_driver.h"
45 //#include "nouveau_state.h"
46 #include "nouveau_span.h"
47 #include "nouveau_object.h"
48 #include "nouveau_fifo.h"
49 #include "nouveau_tex.h"
50 #include "nouveau_msg.h"
51 #include "nouveau_reg.h"
52 #include "nouveau_lock.h"
53 #include "nv10_swtcl.h"
54
55 #include "vblank.h"
56 #include "utils.h"
57 #include "texmem.h"
58 #include "xmlpool.h" /* for symbolic values of enum-type options */
59
60 #ifndef NOUVEAU_DEBUG
61 int NOUVEAU_DEBUG = 0;
62 #endif
63
64 static const struct dri_debug_control debug_control[] =
65 {
66 { "shaders" , DEBUG_SHADERS },
67 { "mem" , DEBUG_MEM },
68 { NULL , 0 }
69 };
70
71 #define need_GL_ARB_vertex_program
72 #include "extension_helper.h"
73
74 const struct dri_extension common_extensions[] =
75 {
76 { NULL, 0 }
77 };
78
79 const struct dri_extension nv10_extensions[] =
80 {
81 { NULL, 0 }
82 };
83
84 const struct dri_extension nv20_extensions[] =
85 {
86 { NULL, 0 }
87 };
88
89 const struct dri_extension nv30_extensions[] =
90 {
91 { "GL_ARB_fragment_program", NULL },
92 { NULL, 0 }
93 };
94
95 const struct dri_extension nv40_extensions[] =
96 {
97 /* ARB_vp can be moved to nv20/30 once the shader backend has been
98 * written for those cards.
99 */
100 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
101 { NULL, 0 }
102 };
103
104 const struct dri_extension nv50_extensions[] =
105 {
106 { NULL, 0 }
107 };
108
109 /* Create the device specific context.
110 */
111 GLboolean nouveauCreateContext( const __GLcontextModes *glVisual,
112 __DRIcontextPrivate *driContextPriv,
113 void *sharedContextPrivate )
114 {
115 GLcontext *ctx, *shareCtx;
116 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
117 struct dd_function_table functions;
118 nouveauContextPtr nmesa;
119 nouveauScreenPtr screen;
120
121 /* Allocate the context */
122 nmesa = (nouveauContextPtr) CALLOC( sizeof(*nmesa) );
123 if ( !nmesa )
124 return GL_FALSE;
125
126 nmesa->driContext = driContextPriv;
127 nmesa->driScreen = sPriv;
128 nmesa->driDrawable = NULL;
129 nmesa->hHWContext = driContextPriv->hHWContext;
130 nmesa->driHwLock = &sPriv->pSAREA->lock;
131 nmesa->driFd = sPriv->fd;
132
133 nmesa->screen = (nouveauScreenPtr)(sPriv->private);
134 screen=nmesa->screen;
135
136 /* Create the hardware context */
137 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_FB_PHYSICAL,
138 &nmesa->vram_phys))
139 return GL_FALSE;
140 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_AGP_PHYSICAL,
141 &nmesa->agp_phys))
142 return GL_FALSE;
143 if (!nouveauFifoInit(nmesa))
144 return GL_FALSE;
145 nouveauObjectInit(nmesa);
146
147
148 /* Init default driver functions then plug in our nouveau-specific functions
149 * (the texture functions are especially important)
150 */
151 _mesa_init_driver_functions( &functions );
152 nouveauDriverInitFunctions( &functions );
153 nouveauTexInitFunctions( &functions );
154
155 /* Allocate the Mesa context */
156 if (sharedContextPrivate)
157 shareCtx = ((nouveauContextPtr) sharedContextPrivate)->glCtx;
158 else
159 shareCtx = NULL;
160 nmesa->glCtx = _mesa_create_context(glVisual, shareCtx,
161 &functions, (void *) nmesa);
162 if (!nmesa->glCtx) {
163 FREE(nmesa);
164 return GL_FALSE;
165 }
166 driContextPriv->driverPrivate = nmesa;
167 ctx = nmesa->glCtx;
168
169 /* Parse configuration files */
170 driParseConfigFiles (&nmesa->optionCache, &screen->optionCache,
171 screen->driScreen->myNum, "nouveau");
172
173 nmesa->sarea = (drm_nouveau_sarea_t *)((char *)sPriv->pSAREA +
174 screen->sarea_priv_offset);
175
176 /* Enable any supported extensions */
177 driInitExtensions(ctx, common_extensions, GL_TRUE);
178 if (nmesa->screen->card->type >= NV_10)
179 driInitExtensions(ctx, nv10_extensions, GL_FALSE);
180 if (nmesa->screen->card->type >= NV_20)
181 driInitExtensions(ctx, nv20_extensions, GL_FALSE);
182 if (nmesa->screen->card->type >= NV_30)
183 driInitExtensions(ctx, nv30_extensions, GL_FALSE);
184 if (nmesa->screen->card->type >= NV_40)
185 driInitExtensions(ctx, nv40_extensions, GL_FALSE);
186 if (nmesa->screen->card->type >= NV_50)
187 driInitExtensions(ctx, nv50_extensions, GL_FALSE);
188
189 nmesa->current_primitive = -1;
190
191 nouveauShaderInitFuncs(ctx);
192 /* Install Mesa's fixed-function texenv shader support */
193 if (nmesa->screen->card->type >= NV_40)
194 ctx->_MaintainTexEnvProgram = GL_TRUE;
195
196 /* Initialize the swrast */
197 _swrast_CreateContext( ctx );
198 _ac_CreateContext( ctx );
199 _tnl_CreateContext( ctx );
200 _swsetup_CreateContext( ctx );
201
202 _math_matrix_ctr(&nmesa->viewport);
203
204 nouveauDDInitStateFuncs( ctx );
205 nouveauSpanInitFunctions( ctx );
206 nouveauDDInitState( nmesa );
207 switch(nmesa->screen->card->type)
208 {
209 case NV_03:
210 //nv03TriInitFunctions( ctx );
211 break;
212 case NV_04:
213 case NV_05:
214 //nv04TriInitFunctions( ctx );
215 break;
216 case NV_10:
217 case NV_20:
218 case NV_30:
219 case NV_40:
220 case NV_44:
221 case NV_50:
222 default:
223 nv10TriInitFunctions( ctx );
224 break;
225 }
226
227 if (!nouveauSyncInitFuncs(ctx))
228 return GL_FALSE;
229 nmesa->hw_func.InitCard(nmesa);
230 nouveauInitState(ctx);
231
232 driContextPriv->driverPrivate = (void *)nmesa;
233
234 NOUVEAU_DEBUG = driParseDebugString( getenv( "NOUVEAU_DEBUG" ),
235 debug_control );
236
237 if (driQueryOptionb(&nmesa->optionCache, "no_rast")) {
238 fprintf(stderr, "disabling 3D acceleration\n");
239 FALLBACK(nmesa, NOUVEAU_FALLBACK_DISABLE, 1);
240 }
241
242 return GL_TRUE;
243 }
244
245 /* Destroy the device specific context. */
246 void nouveauDestroyContext( __DRIcontextPrivate *driContextPriv )
247 {
248 nouveauContextPtr nmesa = (nouveauContextPtr) driContextPriv->driverPrivate;
249
250 assert(nmesa);
251 if ( nmesa ) {
252 /* free the option cache */
253 driDestroyOptionCache (&nmesa->optionCache);
254
255 FREE( nmesa );
256 }
257
258 }
259
260
261 /* Force the context `c' to be the current context and associate with it
262 * buffer `b'.
263 */
264 GLboolean nouveauMakeCurrent( __DRIcontextPrivate *driContextPriv,
265 __DRIdrawablePrivate *driDrawPriv,
266 __DRIdrawablePrivate *driReadPriv )
267 {
268 if ( driContextPriv ) {
269 nouveauContextPtr nmesa = (nouveauContextPtr) driContextPriv->driverPrivate;
270 struct gl_framebuffer *draw_fb =
271 (struct gl_framebuffer*)driDrawPriv->driverPrivate;
272 struct gl_framebuffer *read_fb =
273 (struct gl_framebuffer*)driReadPriv->driverPrivate;
274
275 driDrawableInitVBlank(driDrawPriv, nmesa->vblank_flags, &nmesa->vblank_seq );
276 nmesa->driDrawable = driDrawPriv;
277
278 _mesa_resize_framebuffer(nmesa->glCtx, draw_fb,
279 driDrawPriv->w, driDrawPriv->h);
280 if (draw_fb != read_fb) {
281 _mesa_resize_framebuffer(nmesa->glCtx, draw_fb,
282 driReadPriv->w,
283 driReadPriv->h);
284 }
285 _mesa_make_current(nmesa->glCtx, draw_fb, read_fb);
286
287 nouveau_build_framebuffer(nmesa->glCtx,
288 driDrawPriv->driverPrivate);
289 } else {
290 _mesa_make_current( NULL, NULL, NULL );
291 }
292
293 return GL_TRUE;
294 }
295
296
297 /* Force the context `c' to be unbound from its buffer.
298 */
299 GLboolean nouveauUnbindContext( __DRIcontextPrivate *driContextPriv )
300 {
301 return GL_TRUE;
302 }
303
304 static void nouveauDoSwapBuffers(nouveauContextPtr nmesa,
305 __DRIdrawablePrivate *dPriv)
306 {
307 struct gl_framebuffer *fb;
308 nouveau_renderbuffer *src, *dst;
309 drm_clip_rect_t *box;
310 int nbox, i;
311
312 fb = (struct gl_framebuffer *)dPriv->driverPrivate;
313 dst = (nouveau_renderbuffer*)
314 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
315 src = (nouveau_renderbuffer*)
316 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
317
318 #ifdef ALLOW_MULTI_SUBCHANNEL
319 LOCK_HARDWARE(nmesa);
320 nbox = dPriv->numClipRects;
321 box = dPriv->pClipRects;
322
323 if (nbox) {
324 BEGIN_RING_SIZE(NvSubCtxSurf2D,
325 NV10_CONTEXT_SURFACES_2D_FORMAT, 4);
326 if (src->mesa._ActualFormat == GL_RGBA8)
327 OUT_RING (6); /* X8R8G8B8 */
328 else
329 OUT_RING (4); /* R5G6B5 */
330 OUT_RING ((dst->pitch << 16) | src->pitch);
331 OUT_RING (src->offset);
332 OUT_RING (dst->offset);
333 }
334
335 for (i=0; i<nbox; i++, box++) {
336 BEGIN_RING_SIZE(NvSubImageBlit, NV10_IMAGE_BLIT_SET_POINT, 3);
337 OUT_RING (((box->y1 - dPriv->y) << 16) |
338 (box->x1 - dPriv->x));
339 OUT_RING ((box->y1 << 16) | box->x1);
340 OUT_RING (((box->y2 - box->y1) << 16) |
341 (box->x2 - box->x1));
342 }
343
344 UNLOCK_HARDWARE(nmesa);
345 #endif
346 }
347
348 void nouveauSwapBuffers(__DRIdrawablePrivate *dPriv)
349 {
350 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
351 nouveauContextPtr nmesa = dPriv->driContextPriv->driverPrivate;
352
353 if (nmesa->glCtx->Visual.doubleBufferMode) {
354 _mesa_notifySwapBuffers(nmesa->glCtx);
355 nouveauDoSwapBuffers(nmesa, dPriv);
356 }
357
358 }
359 }
360
361 void nouveauCopySubBuffer(__DRIdrawablePrivate *dPriv,
362 int x, int y, int w, int h)
363 {
364 }
365