Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/mesa
[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 "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 #include "extension_helper.h"
75
76 const struct dri_extension common_extensions[] =
77 {
78 { NULL, 0 }
79 };
80
81 const struct dri_extension nv10_extensions[] =
82 {
83 { NULL, 0 }
84 };
85
86 const struct dri_extension nv20_extensions[] =
87 {
88 { NULL, 0 }
89 };
90
91 const struct dri_extension nv30_extensions[] =
92 {
93 { "GL_ARB_fragment_program", NULL },
94 { NULL, 0 }
95 };
96
97 const struct dri_extension nv40_extensions[] =
98 {
99 /* ARB_vp can be moved to nv20/30 once the shader backend has been
100 * written for those cards.
101 */
102 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions },
103 { NULL, 0 }
104 };
105
106 const struct dri_extension nv50_extensions[] =
107 {
108 { NULL, 0 }
109 };
110
111 /* Create the device specific context.
112 */
113 GLboolean nouveauCreateContext( const __GLcontextModes *glVisual,
114 __DRIcontextPrivate *driContextPriv,
115 void *sharedContextPrivate )
116 {
117 GLcontext *ctx, *shareCtx;
118 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
119 struct dd_function_table functions;
120 nouveauContextPtr nmesa;
121 nouveauScreenPtr screen;
122
123 /* Allocate the context */
124 nmesa = (nouveauContextPtr) CALLOC( sizeof(*nmesa) );
125 if ( !nmesa )
126 return GL_FALSE;
127
128 nmesa->driContext = driContextPriv;
129 nmesa->driScreen = sPriv;
130 nmesa->driDrawable = NULL;
131 nmesa->hHWContext = driContextPriv->hHWContext;
132 nmesa->driHwLock = &sPriv->pSAREA->lock;
133 nmesa->driFd = sPriv->fd;
134
135 nmesa->screen = (nouveauScreenPtr)(sPriv->private);
136 screen=nmesa->screen;
137
138 /* Create the hardware context */
139 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_FB_PHYSICAL,
140 &nmesa->vram_phys))
141 return GL_FALSE;
142 if (!nouveauDRMGetParam(nmesa, NOUVEAU_GETPARAM_AGP_PHYSICAL,
143 &nmesa->agp_phys))
144 return GL_FALSE;
145 if (!nouveauFifoInit(nmesa))
146 return GL_FALSE;
147 nouveauObjectInit(nmesa);
148
149
150 /* Init default driver functions then plug in our nouveau-specific functions
151 * (the texture functions are especially important)
152 */
153 _mesa_init_driver_functions( &functions );
154 nouveauDriverInitFunctions( &functions );
155 nouveauTexInitFunctions( &functions );
156
157 /* Allocate the Mesa context */
158 if (sharedContextPrivate)
159 shareCtx = ((nouveauContextPtr) sharedContextPrivate)->glCtx;
160 else
161 shareCtx = NULL;
162 nmesa->glCtx = _mesa_create_context(glVisual, shareCtx,
163 &functions, (void *) nmesa);
164 if (!nmesa->glCtx) {
165 FREE(nmesa);
166 return GL_FALSE;
167 }
168 driContextPriv->driverPrivate = nmesa;
169 ctx = nmesa->glCtx;
170
171 /* Parse configuration files */
172 driParseConfigFiles (&nmesa->optionCache, &screen->optionCache,
173 screen->driScreen->myNum, "nouveau");
174
175 nmesa->sarea = (drm_nouveau_sarea_t *)((char *)sPriv->pSAREA +
176 screen->sarea_priv_offset);
177
178 /* Enable any supported extensions */
179 driInitExtensions(ctx, common_extensions, GL_TRUE);
180 if (nmesa->screen->card->type >= NV_10)
181 driInitExtensions(ctx, nv10_extensions, GL_FALSE);
182 if (nmesa->screen->card->type >= NV_20)
183 driInitExtensions(ctx, nv20_extensions, GL_FALSE);
184 if (nmesa->screen->card->type >= NV_30)
185 driInitExtensions(ctx, nv30_extensions, GL_FALSE);
186 if (nmesa->screen->card->type >= NV_40)
187 driInitExtensions(ctx, nv40_extensions, GL_FALSE);
188 if (nmesa->screen->card->type >= NV_50)
189 driInitExtensions(ctx, nv50_extensions, GL_FALSE);
190
191 nmesa->current_primitive = -1;
192
193 nouveauShaderInitFuncs(ctx);
194 /* Install Mesa's fixed-function texenv shader support */
195 if (nmesa->screen->card->type >= NV_40)
196 ctx->_MaintainTexEnvProgram = GL_TRUE;
197
198 /* Initialize the swrast */
199 _swrast_CreateContext( ctx );
200 _ac_CreateContext( ctx );
201 _tnl_CreateContext( ctx );
202 _swsetup_CreateContext( ctx );
203
204 _math_matrix_ctr(&nmesa->viewport);
205
206 nouveauDDInitStateFuncs( ctx );
207 nouveauSpanInitFunctions( ctx );
208 nouveauDDInitState( nmesa );
209 switch(nmesa->screen->card->type)
210 {
211 case NV_03:
212 //nv03TriInitFunctions( ctx );
213 break;
214 case NV_04:
215 case NV_05:
216 nv04TriInitFunctions( ctx );
217 break;
218 case NV_10:
219 case NV_20:
220 case NV_30:
221 case NV_40:
222 case NV_44:
223 case NV_50:
224 default:
225 nv10TriInitFunctions( ctx );
226 break;
227 }
228
229 nouveauInitBufferObjects(ctx);
230 if (!nouveauSyncInitFuncs(ctx))
231 return GL_FALSE;
232 nmesa->hw_func.InitCard(nmesa);
233 nouveauInitState(ctx);
234
235 driContextPriv->driverPrivate = (void *)nmesa;
236
237 NOUVEAU_DEBUG = driParseDebugString( getenv( "NOUVEAU_DEBUG" ),
238 debug_control );
239
240 if (driQueryOptionb(&nmesa->optionCache, "no_rast")) {
241 fprintf(stderr, "disabling 3D acceleration\n");
242 FALLBACK(nmesa, NOUVEAU_FALLBACK_DISABLE, 1);
243 }
244
245 return GL_TRUE;
246 }
247
248 /* Destroy the device specific context. */
249 void nouveauDestroyContext( __DRIcontextPrivate *driContextPriv )
250 {
251 nouveauContextPtr nmesa = (nouveauContextPtr) driContextPriv->driverPrivate;
252
253 assert(nmesa);
254 if ( nmesa ) {
255 /* free the option cache */
256 driDestroyOptionCache (&nmesa->optionCache);
257
258 FREE( nmesa );
259 }
260
261 }
262
263
264 /* Force the context `c' to be the current context and associate with it
265 * buffer `b'.
266 */
267 GLboolean nouveauMakeCurrent( __DRIcontextPrivate *driContextPriv,
268 __DRIdrawablePrivate *driDrawPriv,
269 __DRIdrawablePrivate *driReadPriv )
270 {
271 if ( driContextPriv ) {
272 nouveauContextPtr nmesa = (nouveauContextPtr) driContextPriv->driverPrivate;
273 struct gl_framebuffer *draw_fb =
274 (struct gl_framebuffer*)driDrawPriv->driverPrivate;
275 struct gl_framebuffer *read_fb =
276 (struct gl_framebuffer*)driReadPriv->driverPrivate;
277
278 driDrawableInitVBlank(driDrawPriv, nmesa->vblank_flags, &nmesa->vblank_seq );
279 nmesa->driDrawable = driDrawPriv;
280
281 _mesa_resize_framebuffer(nmesa->glCtx, draw_fb,
282 driDrawPriv->w, driDrawPriv->h);
283 if (draw_fb != read_fb) {
284 _mesa_resize_framebuffer(nmesa->glCtx, draw_fb,
285 driReadPriv->w,
286 driReadPriv->h);
287 }
288 _mesa_make_current(nmesa->glCtx, draw_fb, read_fb);
289
290 nouveau_build_framebuffer(nmesa->glCtx,
291 driDrawPriv->driverPrivate);
292 } else {
293 _mesa_make_current( NULL, NULL, NULL );
294 }
295
296 return GL_TRUE;
297 }
298
299
300 /* Force the context `c' to be unbound from its buffer.
301 */
302 GLboolean nouveauUnbindContext( __DRIcontextPrivate *driContextPriv )
303 {
304 return GL_TRUE;
305 }
306
307 static void nouveauDoSwapBuffers(nouveauContextPtr nmesa,
308 __DRIdrawablePrivate *dPriv)
309 {
310 struct gl_framebuffer *fb;
311 nouveau_renderbuffer *src, *dst;
312 drm_clip_rect_t *box;
313 int nbox, i;
314
315 fb = (struct gl_framebuffer *)dPriv->driverPrivate;
316 dst = (nouveau_renderbuffer*)
317 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
318 src = (nouveau_renderbuffer*)
319 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
320
321 #ifdef ALLOW_MULTI_SUBCHANNEL
322 LOCK_HARDWARE(nmesa);
323 nbox = dPriv->numClipRects;
324 box = dPriv->pClipRects;
325
326 if (nbox) {
327 BEGIN_RING_SIZE(NvSubCtxSurf2D,
328 NV10_CONTEXT_SURFACES_2D_FORMAT, 4);
329 if (src->mesa._ActualFormat == GL_RGBA8)
330 OUT_RING (6); /* X8R8G8B8 */
331 else
332 OUT_RING (4); /* R5G6B5 */
333 OUT_RING ((dst->pitch << 16) | src->pitch);
334 OUT_RING (src->offset);
335 OUT_RING (dst->offset);
336 }
337
338 for (i=0; i<nbox; i++, box++) {
339 BEGIN_RING_SIZE(NvSubImageBlit, NV10_IMAGE_BLIT_SET_POINT, 3);
340 OUT_RING (((box->y1 - dPriv->y) << 16) |
341 (box->x1 - dPriv->x));
342 OUT_RING ((box->y1 << 16) | box->x1);
343 OUT_RING (((box->y2 - box->y1) << 16) |
344 (box->x2 - box->x1));
345 }
346
347 UNLOCK_HARDWARE(nmesa);
348 #endif
349 }
350
351 void nouveauSwapBuffers(__DRIdrawablePrivate *dPriv)
352 {
353 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
354 nouveauContextPtr nmesa = dPriv->driContextPriv->driverPrivate;
355
356 if (nmesa->glCtx->Visual.doubleBufferMode) {
357 _mesa_notifySwapBuffers(nmesa->glCtx);
358 nouveauDoSwapBuffers(nmesa, dPriv);
359 }
360
361 }
362 }
363
364 void nouveauCopySubBuffer(__DRIdrawablePrivate *dPriv,
365 int x, int y, int w, int h)
366 {
367 }
368