1 /**************************************************************************
3 Copyright 2006 Stephane Marchesin
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:
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
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.
25 **************************************************************************/
29 #include "simple_list.h"
32 #include "swrast/swrast.h"
33 #include "swrast_setup/swrast_setup.h"
34 #include "framebuffer.h"
37 #include "tnl/t_pipeline.h"
38 #include "tnl/t_vp_build.h"
40 #include "drivers/common/driverfuncs.h"
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"
59 #include "xmlpool.h" /* for symbolic values of enum-type options */
62 int NOUVEAU_DEBUG
= 0;
65 static const struct dri_debug_control debug_control
[] =
67 { "shaders" , DEBUG_SHADERS
},
68 { "mem" , DEBUG_MEM
},
69 { "bufferobj" , DEBUG_BUFFEROBJ
},
73 #define need_GL_ARB_vertex_program
74 #define need_GL_ARB_occlusion_query
75 #include "extension_helper.h"
77 const struct dri_extension common_extensions
[] =
82 const struct dri_extension nv10_extensions
[] =
87 const struct dri_extension nv20_extensions
[] =
92 const struct dri_extension nv30_extensions
[] =
94 { "GL_ARB_fragment_program", NULL
},
98 const struct dri_extension nv40_extensions
[] =
100 /* ARB_vp can be moved to nv20/30 once the shader backend has been
101 * written for those cards.
103 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
104 { "GL_ARB_occlusion_query", GL_ARB_occlusion_query_functions
},
108 const struct dri_extension nv50_extensions
[] =
113 /* Create the device specific context.
115 GLboolean
nouveauCreateContext( const __GLcontextModes
*glVisual
,
116 __DRIcontextPrivate
*driContextPriv
,
117 void *sharedContextPrivate
)
119 GLcontext
*ctx
, *shareCtx
;
120 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
121 struct dd_function_table functions
;
122 nouveauContextPtr nmesa
;
123 nouveauScreenPtr screen
;
125 /* Allocate the context */
126 nmesa
= (nouveauContextPtr
) CALLOC( sizeof(*nmesa
) );
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
;
137 nmesa
->screen
= (nouveauScreenPtr
)(sPriv
->private);
138 screen
=nmesa
->screen
;
140 /* Create the hardware context */
141 if (!nouveauDRMGetParam(nmesa
, NOUVEAU_GETPARAM_FB_PHYSICAL
,
144 if (!nouveauDRMGetParam(nmesa
, NOUVEAU_GETPARAM_FB_SIZE
,
147 if (!nouveauDRMGetParam(nmesa
, NOUVEAU_GETPARAM_AGP_PHYSICAL
,
150 if (!nouveauDRMGetParam(nmesa
, NOUVEAU_GETPARAM_AGP_SIZE
,
153 if (!nouveauFifoInit(nmesa
))
155 nouveauObjectInit(nmesa
);
158 /* Init default driver functions then plug in our nouveau-specific functions
159 * (the texture functions are especially important)
161 _mesa_init_driver_functions( &functions
);
162 nouveauDriverInitFunctions( &functions
);
163 nouveauTexInitFunctions( &functions
);
165 /* Allocate the Mesa context */
166 if (sharedContextPrivate
)
167 shareCtx
= ((nouveauContextPtr
) sharedContextPrivate
)->glCtx
;
170 nmesa
->glCtx
= _mesa_create_context(glVisual
, shareCtx
,
171 &functions
, (void *) nmesa
);
176 driContextPriv
->driverPrivate
= nmesa
;
179 /* Parse configuration files */
180 driParseConfigFiles (&nmesa
->optionCache
, &screen
->optionCache
,
181 screen
->driScreen
->myNum
, "nouveau");
183 nmesa
->sarea
= (struct drm_nouveau_sarea
*)((char *)sPriv
->pSAREA
+
184 screen
->sarea_priv_offset
);
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
);
199 nmesa
->current_primitive
= -1;
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
;
206 /* Initialize the swrast */
207 _swrast_CreateContext( ctx
);
208 _vbo_CreateContext( ctx
);
209 _tnl_CreateContext( ctx
);
210 _swsetup_CreateContext( ctx
);
212 _math_matrix_ctr(&nmesa
->viewport
);
214 nouveauDDInitStateFuncs( ctx
);
215 nouveauSpanInitFunctions( ctx
);
216 nouveauDDInitState( nmesa
);
217 switch(nmesa
->screen
->card
->type
)
221 nv04TriInitFunctions( ctx
);
232 nv10TriInitFunctions( ctx
);
236 nouveauInitBufferObjects(ctx
);
237 if (!nouveauSyncInitFuncs(ctx
))
239 nouveauQueryInitFuncs(ctx
);
240 nmesa
->hw_func
.InitCard(nmesa
);
241 nouveauInitState(ctx
);
243 driContextPriv
->driverPrivate
= (void *)nmesa
;
245 NOUVEAU_DEBUG
= driParseDebugString( getenv( "NOUVEAU_DEBUG" ),
248 if (driQueryOptionb(&nmesa
->optionCache
, "no_rast")) {
249 fprintf(stderr
, "disabling 3D acceleration\n");
250 FALLBACK(nmesa
, NOUVEAU_FALLBACK_DISABLE
, 1);
256 /* Destroy the device specific context. */
257 void nouveauDestroyContext( __DRIcontextPrivate
*driContextPriv
)
259 nouveauContextPtr nmesa
= (nouveauContextPtr
) driContextPriv
->driverPrivate
;
263 /* free the option cache */
264 driDestroyOptionCache (&nmesa
->optionCache
);
272 /* Force the context `c' to be the current context and associate with it
275 GLboolean
nouveauMakeCurrent( __DRIcontextPrivate
*driContextPriv
,
276 __DRIdrawablePrivate
*driDrawPriv
,
277 __DRIdrawablePrivate
*driReadPriv
)
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
;
286 if (driDrawPriv
->swap_interval
== (unsigned)-1) {
287 driDrawPriv
->vblFlags
=
288 driGetDefaultVBlankFlags(&nmesa
->optionCache
);
290 driDrawableInitVBlank(driDrawPriv
);
293 nmesa
->driDrawable
= driDrawPriv
;
295 _mesa_resize_framebuffer(nmesa
->glCtx
, draw_fb
,
296 driDrawPriv
->w
, driDrawPriv
->h
);
297 if (draw_fb
!= read_fb
) {
298 _mesa_resize_framebuffer(nmesa
->glCtx
, draw_fb
,
302 _mesa_make_current(nmesa
->glCtx
, draw_fb
, read_fb
);
304 nouveau_build_framebuffer(nmesa
->glCtx
,
305 driDrawPriv
->driverPrivate
);
307 _mesa_make_current( NULL
, NULL
, NULL
);
314 /* Force the context `c' to be unbound from its buffer.
316 GLboolean
nouveauUnbindContext( __DRIcontextPrivate
*driContextPriv
)
322 nouveauDoSwapBuffers(nouveauContextPtr nmesa
, __DRIdrawablePrivate
*dPriv
)
324 struct gl_framebuffer
*fb
;
325 nouveauScreenPtr screen
= dPriv
->driScreenPriv
->private;
326 nouveau_renderbuffer_t
*src
;
327 drm_clip_rect_t
*box
;
330 fb
= (struct gl_framebuffer
*)dPriv
->driverPrivate
;
331 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
332 src
= (nouveau_renderbuffer_t
*)
333 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
335 src
= (nouveau_renderbuffer_t
*)
336 fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
339 LOCK_HARDWARE(nmesa
);
340 nbox
= dPriv
->numClipRects
;
341 box
= dPriv
->pClipRects
;
344 BEGIN_RING_SIZE(NvSubCtxSurf2D
,
345 NV10_CONTEXT_SURFACES_2D_FORMAT
, 4);
346 if (src
->mesa
._ActualFormat
== GL_RGBA8
)
347 OUT_RING (6); /* X8R8G8B8 */
349 OUT_RING (4); /* R5G6B5 */
350 OUT_RING(((screen
->frontPitch
* screen
->fbFormat
) << 16) |
352 OUT_RING(src
->offset
);
353 OUT_RING(screen
->frontOffset
);
356 for (i
=0; i
<nbox
; i
++, box
++) {
357 BEGIN_RING_SIZE(NvSubImageBlit
, NV_IMAGE_BLIT_POINT_IN
, 3);
358 OUT_RING (((box
->y1
- dPriv
->y
) << 16) |
359 (box
->x1
- dPriv
->x
));
360 OUT_RING ((box
->y1
<< 16) | box
->x1
);
361 OUT_RING (((box
->y2
- box
->y1
) << 16) |
362 (box
->x2
- box
->x1
));
366 UNLOCK_HARDWARE(nmesa
);
369 void nouveauSwapBuffers(__DRIdrawablePrivate
*dPriv
)
371 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
372 nouveauContextPtr nmesa
= dPriv
->driContextPriv
->driverPrivate
;
374 if (nmesa
->glCtx
->Visual
.doubleBufferMode
) {
375 _mesa_notifySwapBuffers(nmesa
->glCtx
);
376 nouveauDoSwapBuffers(nmesa
, dPriv
);
382 void nouveauCopySubBuffer(__DRIdrawablePrivate
*dPriv
,
383 int x
, int y
, int w
, int h
)
387 void nouveauClearBuffer(GLcontext
*ctx
, nouveau_renderbuffer_t
*buffer
,
390 nouveauContextPtr nmesa
= NOUVEAU_CONTEXT(ctx
);
397 /* FIXME: only support 32 bits atm */
399 /* Surface that we will work on */
400 nouveauObjectOnSubchannel(nmesa
, NvSubCtxSurf2D
, NvCtxSurf2D
);
402 BEGIN_RING_SIZE(NvSubCtxSurf2D
, NV10_CONTEXT_SURFACES_2D_FORMAT
, 4);
403 OUT_RING(0x0b); /* Y32 color format */
404 OUT_RING((buffer
->pitch
<<16)|buffer
->pitch
);
405 OUT_RING(buffer
->offset
);
406 OUT_RING(buffer
->offset
);
408 /* Now clear a rectangle */
409 dimensions
= ((buffer
->mesa
.Height
)<<16) | (buffer
->mesa
.Width
);
411 nouveauObjectOnSubchannel(nmesa
, NvSubGdiRectText
, NvGdiRectText
);
413 BEGIN_RING_SIZE(NvSubGdiRectText
, NV04_GDI_RECTANGLE_TEXT_OPERATION
, 1);
414 OUT_RING(3); /* SRCCOPY */
416 BEGIN_RING_SIZE(NvSubGdiRectText
, NV04_GDI_RECTANGLE_TEXT_BLOCK_LEVEL1_TL
, 5);
417 OUT_RING(0); /* top left */
418 OUT_RING(dimensions
); /* bottom right */
420 OUT_RING(0); /* top left */
421 OUT_RING(dimensions
); /* bottom right */