Merge branch 'dri2'
[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 "framebuffer.h"
35
36 #include "tnl/tnl.h"
37 #include "tnl/t_pipeline.h"
38 #include "tnl/t_vp_build.h"
39
40 #include "drivers/common/driverfuncs.h"
41
42 #include "nouveau_context.h"
43 #include "nouveau_driver.h"
44 //#include "nouveau_state.h"
45 #include "nouveau_span.h"
46 #include "nouveau_object.h"
47 #include "nouveau_fifo.h"
48 #include "nouveau_tex.h"
49 #include "nouveau_msg.h"
50 #include "nouveau_reg.h"
51 #include "nouveau_lock.h"
52 #include "nouveau_query.h"
53 #include "nv04_swtcl.h"
54 #include "nv10_swtcl.h"
55
56 #include "vblank.h"
57 #include "utils.h"
58 #include "texmem.h"
59 #include "xmlpool.h" /* for symbolic values of enum-type options */
60
61 #ifndef NOUVEAU_DEBUG
62 int NOUVEAU_DEBUG = 0;
63 #endif
64
65 static const struct dri_debug_control debug_control[] =
66 {
67 { "shaders" , DEBUG_SHADERS },
68 { "mem" , DEBUG_MEM },
69 { "bufferobj" , DEBUG_BUFFEROBJ },
70 { NULL , 0 }
71 };
72
73 #define need_GL_ARB_vertex_program
74 #define need_GL_ARB_occlusion_query
75 #include "extension_helper.h"
76
77 const struct dri_extension common_extensions[] =
78 {
79 { NULL, 0 }
80 };
81
82 const struct dri_extension nv10_extensions[] =
83 {
84 { NULL, 0 }
85 };
86
87 const struct dri_extension nv20_extensions[] =
88 {
89 { NULL, 0 }
90 };
91
92 const struct dri_extension nv30_extensions[] =
93 {
94 { "GL_ARB_fragment_program", NULL },
95 { NULL, 0 }
96 };
97
98 const struct dri_extension nv40_extensions[] =
99 {
100 /* ARB_vp can be moved to nv20/30 once the shader backend has been
101 * written for those cards.
102 */
103 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
104 { "GL_ARB_occlusion_query", GL_ARB_occlusion_query_functions},
105 { NULL, 0 }
106 };
107
108 const struct dri_extension nv50_extensions[] =
109 {
110 { NULL, 0 }
111 };
112
113 /* Create the device specific context.
114 */
115 GLboolean nouveauCreateContext( const __GLcontextModes *glVisual,
116 __DRIcontextPrivate *driContextPriv,
117 void *sharedContextPrivate )
118 {
119 GLcontext *ctx, *shareCtx;
120 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
121 struct dd_function_table functions;
122 nouveauContextPtr nmesa;
123 nouveauScreenPtr screen;
124
125 /* Allocate the context */
126 nmesa = (nouveauContextPtr) CALLOC( sizeof(*nmesa) );
127 if ( !nmesa )
128 return GL_FALSE;
129
130 nmesa->driContext = driContextPriv;
131 nmesa->driScreen = sPriv;
132 nmesa->driDrawable = NULL;
133 nmesa->hHWContext = driContextPriv->hHWContext;
134 nmesa->driHwLock = &sPriv->pSAREA->lock;
135 nmesa->driFd = sPriv->fd;
136
137 nmesa->screen = (nouveauScreenPtr)(sPriv->private);
138 screen=nmesa->screen;
139
140 /* Create the hardware context */
141 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_FB_PHYSICAL,
142 &nmesa->vram_phys))
143 return GL_FALSE;
144 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_FB_SIZE,
145 &nmesa->vram_size))
146 return GL_FALSE;
147 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_AGP_PHYSICAL,
148 &nmesa->gart_phys))
149 return GL_FALSE;
150 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_AGP_SIZE,
151 &nmesa->gart_size))
152 return GL_FALSE;
153 if (!nouveauFifoInit(nmesa))
154 return GL_FALSE;
155 nouveauObjectInit(nmesa);
156
157
158 /* Init default driver functions then plug in our nouveau-specific functions
159 * (the texture functions are especially important)
160 */
161 _mesa_init_driver_functions( &functions );
162 nouveauDriverInitFunctions( &functions );
163 nouveauTexInitFunctions( &functions );
164
165 /* Allocate the Mesa context */
166 if (sharedContextPrivate)
167 shareCtx = ((nouveauContextPtr) sharedContextPrivate)->glCtx;
168 else
169 shareCtx = NULL;
170 nmesa->glCtx = _mesa_create_context(glVisual, shareCtx,
171 &functions, (void *) nmesa);
172 if (!nmesa->glCtx) {
173 FREE(nmesa);
174 return GL_FALSE;
175 }
176 driContextPriv->driverPrivate = nmesa;
177 ctx = nmesa->glCtx;
178
179 /* Parse configuration files */
180 driParseConfigFiles (&nmesa->optionCache, &screen->optionCache,
181 screen->driScreen->myNum, "nouveau");
182
183 nmesa->sarea = (struct drm_nouveau_sarea *)((char *)sPriv->pSAREA +
184 screen->sarea_priv_offset);
185
186 /* Enable any supported extensions */
187 driInitExtensions(ctx, common_extensions, GL_TRUE);
188 if (nmesa->screen->card->type >= NV_10)
189 driInitExtensions(ctx, nv10_extensions, GL_FALSE);
190 if (nmesa->screen->card->type >= NV_20)
191 driInitExtensions(ctx, nv20_extensions, GL_FALSE);
192 if (nmesa->screen->card->type >= NV_30)
193 driInitExtensions(ctx, nv30_extensions, GL_FALSE);
194 if (nmesa->screen->card->type >= NV_40)
195 driInitExtensions(ctx, nv40_extensions, GL_FALSE);
196 if (nmesa->screen->card->type >= NV_50)
197 driInitExtensions(ctx, nv50_extensions, GL_FALSE);
198
199 nmesa->current_primitive = -1;
200
201 nouveauShaderInitFuncs(ctx);
202 /* Install Mesa's fixed-function texenv shader support */
203 if (nmesa->screen->card->type >= NV_40)
204 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
205
206 /* Initialize the swrast */
207 _swrast_CreateContext( ctx );
208 _vbo_CreateContext( ctx );
209 _tnl_CreateContext( ctx );
210 _swsetup_CreateContext( ctx );
211
212 _math_matrix_ctr(&nmesa->viewport);
213
214 nouveauDDInitStateFuncs( ctx );
215 nouveauSpanInitFunctions( ctx );
216 nouveauDDInitState( nmesa );
217 switch(nmesa->screen->card->type)
218 {
219 case NV_04:
220 case NV_05:
221 nv04TriInitFunctions( ctx );
222 break;
223 case NV_10:
224 case NV_11:
225 case NV_17:
226 case NV_20:
227 case NV_30:
228 case NV_40:
229 case NV_44:
230 case NV_50:
231 default:
232 nv10TriInitFunctions( ctx );
233 break;
234 }
235
236 nouveauInitBufferObjects(ctx);
237 if (!nouveauSyncInitFuncs(ctx))
238 return GL_FALSE;
239 nouveauQueryInitFuncs(ctx);
240 nmesa->hw_func.InitCard(nmesa);
241 nouveauInitState(ctx);
242
243 driContextPriv->driverPrivate = (void *)nmesa;
244
245 NOUVEAU_DEBUG = driParseDebugString( getenv( "NOUVEAU_DEBUG" ),
246 debug_control );
247
248 if (driQueryOptionb(&nmesa->optionCache, "no_rast")) {
249 fprintf(stderr, "disabling 3D acceleration\n");
250 FALLBACK(nmesa, NOUVEAU_FALLBACK_DISABLE, 1);
251 }
252
253 return GL_TRUE;
254 }
255
256 /* Destroy the device specific context. */
257 void nouveauDestroyContext( __DRIcontextPrivate *driContextPriv )
258 {
259 nouveauContextPtr nmesa = (nouveauContextPtr) driContextPriv->driverPrivate;
260
261 assert(nmesa);
262 if ( nmesa ) {
263 /* free the option cache */
264 driDestroyOptionCache (&nmesa->optionCache);
265
266 FREE( nmesa );
267 }
268
269 }
270
271
272 /* Force the context `c' to be the current context and associate with it
273 * buffer `b'.
274 */
275 GLboolean nouveauMakeCurrent( __DRIcontextPrivate *driContextPriv,
276 __DRIdrawablePrivate *driDrawPriv,
277 __DRIdrawablePrivate *driReadPriv )
278 {
279 if ( driContextPriv ) {
280 nouveauContextPtr nmesa = (nouveauContextPtr) driContextPriv->driverPrivate;
281 struct gl_framebuffer *draw_fb =
282 (struct gl_framebuffer*)driDrawPriv->driverPrivate;
283 struct gl_framebuffer *read_fb =
284 (struct gl_framebuffer*)driReadPriv->driverPrivate;
285
286 driDrawableInitVBlank(driDrawPriv, nmesa->vblank_flags, &nmesa->vblank_seq );
287 nmesa->driDrawable = driDrawPriv;
288
289 _mesa_resize_framebuffer(nmesa->glCtx, draw_fb,
290 driDrawPriv->w, driDrawPriv->h);
291 if (draw_fb != read_fb) {
292 _mesa_resize_framebuffer(nmesa->glCtx, draw_fb,
293 driReadPriv->w,
294 driReadPriv->h);
295 }
296 _mesa_make_current(nmesa->glCtx, draw_fb, read_fb);
297
298 nouveau_build_framebuffer(nmesa->glCtx,
299 driDrawPriv->driverPrivate);
300 } else {
301 _mesa_make_current( NULL, NULL, NULL );
302 }
303
304 return GL_TRUE;
305 }
306
307
308 /* Force the context `c' to be unbound from its buffer.
309 */
310 GLboolean nouveauUnbindContext( __DRIcontextPrivate *driContextPriv )
311 {
312 return GL_TRUE;
313 }
314
315 void
316 nouveauDoSwapBuffers(nouveauContextPtr nmesa, __DRIdrawablePrivate *dPriv)
317 {
318 struct gl_framebuffer *fb;
319 nouveauScreenPtr screen = dPriv->driScreenPriv->private;
320 nouveau_renderbuffer_t *src;
321 drm_clip_rect_t *box;
322 int nbox, i;
323
324 fb = (struct gl_framebuffer *)dPriv->driverPrivate;
325 if (fb->_ColorDrawBufferMask[0] == BUFFER_BIT_FRONT_LEFT) {
326 src = (nouveau_renderbuffer_t *)
327 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
328 } else {
329 src = (nouveau_renderbuffer_t *)
330 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
331 }
332
333 LOCK_HARDWARE(nmesa);
334 nbox = dPriv->numClipRects;
335 box = dPriv->pClipRects;
336
337 if (nbox) {
338 BEGIN_RING_SIZE(NvSubCtxSurf2D,
339 NV10_CONTEXT_SURFACES_2D_FORMAT, 4);
340 if (src->mesa._ActualFormat == GL_RGBA8)
341 OUT_RING (6); /* X8R8G8B8 */
342 else
343 OUT_RING (4); /* R5G6B5 */
344 OUT_RING(((screen->frontPitch * screen->fbFormat) << 16) |
345 src->pitch);
346 OUT_RING(src->offset);
347 OUT_RING(screen->frontOffset);
348 }
349
350 for (i=0; i<nbox; i++, box++) {
351 BEGIN_RING_SIZE(NvSubImageBlit, NV_IMAGE_BLIT_POINT_IN, 3);
352 OUT_RING (((box->y1 - dPriv->y) << 16) |
353 (box->x1 - dPriv->x));
354 OUT_RING ((box->y1 << 16) | box->x1);
355 OUT_RING (((box->y2 - box->y1) << 16) |
356 (box->x2 - box->x1));
357 }
358 FIRE_RING();
359
360 UNLOCK_HARDWARE(nmesa);
361 }
362
363 void nouveauSwapBuffers(__DRIdrawablePrivate *dPriv)
364 {
365 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
366 nouveauContextPtr nmesa = dPriv->driContextPriv->driverPrivate;
367
368 if (nmesa->glCtx->Visual.doubleBufferMode) {
369 _mesa_notifySwapBuffers(nmesa->glCtx);
370 nouveauDoSwapBuffers(nmesa, dPriv);
371 }
372
373 }
374 }
375
376 void nouveauCopySubBuffer(__DRIdrawablePrivate *dPriv,
377 int x, int y, int w, int h)
378 {
379 }
380
381 void nouveauClearBuffer(GLcontext *ctx, nouveau_renderbuffer_t *buffer,
382 int fill, int mask)
383 {
384 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
385 int dimensions;
386
387 if (!buffer) {
388 return;
389 }
390
391 /* FIXME: only support 32 bits atm */
392
393 /* Surface that we will work on */
394 nouveauObjectOnSubchannel(nmesa, NvSubCtxSurf2D, NvCtxSurf2D);
395
396 BEGIN_RING_SIZE(NvSubCtxSurf2D, NV10_CONTEXT_SURFACES_2D_FORMAT, 4);
397 OUT_RING(0x0b); /* Y32 color format */
398 OUT_RING((buffer->pitch<<16)|buffer->pitch);
399 OUT_RING(buffer->offset);
400 OUT_RING(buffer->offset);
401
402 /* Now clear a rectangle */
403 dimensions = ((buffer->mesa.Height)<<16) | (buffer->mesa.Width);
404
405 nouveauObjectOnSubchannel(nmesa, NvSubGdiRectText, NvGdiRectText);
406
407 BEGIN_RING_SIZE(NvSubGdiRectText, NV04_GDI_RECTANGLE_TEXT_OPERATION, 1);
408 OUT_RING(3); /* SRCCOPY */
409
410 BEGIN_RING_SIZE(NvSubGdiRectText, NV04_GDI_RECTANGLE_TEXT_BLOCK_LEVEL1_TL, 5);
411 OUT_RING(0); /* top left */
412 OUT_RING(dimensions); /* bottom right */
413 OUT_RING(fill);
414 OUT_RING(0); /* top left */
415 OUT_RING(dimensions); /* bottom right */
416 }