Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / r300 / r300_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
32 *
33 * \author Keith Whitwell <keith@tungstengraphics.com>
34 *
35 * \author Nicolai Haehnle <prefect_@gmx.net>
36 */
37
38 #include "main/glheader.h"
39 #include "main/api_arrayelt.h"
40 #include "main/context.h"
41 #include "main/simple_list.h"
42 #include "main/imports.h"
43 #include "main/matrix.h"
44 #include "main/extensions.h"
45 #include "main/state.h"
46 #include "main/bufferobj.h"
47
48 #include "swrast/swrast.h"
49 #include "swrast_setup/swrast_setup.h"
50 #include "vbo/vbo.h"
51
52 #include "tnl/tnl.h"
53 #include "tnl/t_pipeline.h"
54 #include "tnl/t_vp_build.h"
55
56 #include "drivers/common/driverfuncs.h"
57
58 #include "radeon_ioctl.h"
59 #include "radeon_span.h"
60 #include "r300_context.h"
61 #include "r300_cmdbuf.h"
62 #include "r300_state.h"
63 #include "r300_ioctl.h"
64 #include "r300_tex.h"
65 #include "r300_emit.h"
66 #include "r300_swtcl.h"
67
68 #ifdef USER_BUFFERS
69 #include "r300_mem.h"
70 #endif
71
72 #include "vblank.h"
73 #include "utils.h"
74 #include "xmlpool.h" /* for symbolic values of enum-type options */
75
76 /* hw_tcl_on derives from future_hw_tcl_on when its safe to change it. */
77 int future_hw_tcl_on = 1;
78 int hw_tcl_on = 1;
79
80 #define need_GL_EXT_stencil_two_side
81 #define need_GL_ARB_multisample
82 #define need_GL_ARB_point_parameters
83 #define need_GL_ARB_texture_compression
84 #define need_GL_ARB_vertex_buffer_object
85 #define need_GL_ARB_vertex_program
86 #define need_GL_EXT_blend_minmax
87 //#define need_GL_EXT_fog_coord
88 #define need_GL_EXT_multi_draw_arrays
89 #define need_GL_EXT_secondary_color
90 #define need_GL_EXT_blend_equation_separate
91 #define need_GL_EXT_blend_func_separate
92 #define need_GL_EXT_gpu_program_parameters
93 #define need_GL_NV_vertex_program
94 #include "extension_helper.h"
95
96 const struct dri_extension card_extensions[] = {
97 /* *INDENT-OFF* */
98 {"GL_ARB_depth_texture", NULL},
99 {"GL_ARB_fragment_program", NULL},
100 {"GL_ARB_multisample", GL_ARB_multisample_functions},
101 {"GL_ARB_multitexture", NULL},
102 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
103 {"GL_ARB_shadow", NULL},
104 {"GL_ARB_shadow_ambient", NULL},
105 {"GL_ARB_texture_border_clamp", NULL},
106 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions},
107 {"GL_ARB_texture_cube_map", NULL},
108 {"GL_ARB_texture_env_add", NULL},
109 {"GL_ARB_texture_env_combine", NULL},
110 {"GL_ARB_texture_env_crossbar", NULL},
111 {"GL_ARB_texture_env_dot3", NULL},
112 {"GL_ARB_texture_mirrored_repeat", NULL},
113 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions},
114 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
115 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
116 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
117 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
118 {"GL_EXT_blend_subtract", NULL},
119 // {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
120 {"GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions},
121 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions},
122 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
123 {"GL_EXT_shadow_funcs", NULL},
124 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions},
125 {"GL_EXT_stencil_wrap", NULL},
126 {"GL_EXT_texture_edge_clamp", NULL},
127 {"GL_EXT_texture_env_combine", NULL},
128 {"GL_EXT_texture_env_dot3", NULL},
129 {"GL_EXT_texture_filter_anisotropic", NULL},
130 {"GL_EXT_texture_lod_bias", NULL},
131 {"GL_EXT_texture_mirror_clamp", NULL},
132 {"GL_EXT_texture_rectangle", NULL},
133 {"GL_ATI_texture_env_combine3", NULL},
134 {"GL_ATI_texture_mirror_once", NULL},
135 {"GL_MESA_pack_invert", NULL},
136 {"GL_MESA_ycbcr_texture", NULL},
137 {"GL_MESAX_texture_float", NULL},
138 {"GL_NV_blend_square", NULL},
139 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
140 {"GL_SGIS_generate_mipmap", NULL},
141 {"GL_SGIX_depth_texture", NULL},
142 {NULL, NULL}
143 /* *INDENT-ON* */
144 };
145
146 extern struct tnl_pipeline_stage _r300_render_stage;
147 extern const struct tnl_pipeline_stage _r300_tcl_stage;
148
149 static const struct tnl_pipeline_stage *r300_pipeline[] = {
150
151 /* Try and go straight to t&l
152 */
153 &_r300_tcl_stage,
154
155 /* Catch any t&l fallbacks
156 */
157 &_tnl_vertex_transform_stage,
158 &_tnl_normal_transform_stage,
159 &_tnl_lighting_stage,
160 &_tnl_fog_coordinate_stage,
161 &_tnl_texgen_stage,
162 &_tnl_texture_transform_stage,
163 &_tnl_vertex_program_stage,
164
165 /* Try again to go to tcl?
166 * - no good for asymmetric-twoside (do with multipass)
167 * - no good for asymmetric-unfilled (do with multipass)
168 * - good for material
169 * - good for texgen
170 * - need to manipulate a bit of state
171 *
172 * - worth it/not worth it?
173 */
174
175 /* Else do them here.
176 */
177 &_r300_render_stage,
178 &_tnl_render_stage, /* FALLBACK */
179 0,
180 };
181
182 /* Create the device specific rendering context.
183 */
184 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
185 __DRIcontextPrivate * driContextPriv,
186 void *sharedContextPrivate)
187 {
188 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
189 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
190 struct dd_function_table functions;
191 r300ContextPtr r300;
192 GLcontext *ctx;
193 int tcl_mode, i;
194
195 assert(glVisual);
196 assert(driContextPriv);
197 assert(screen);
198
199 /* Allocate the R300 context */
200 r300 = (r300ContextPtr) CALLOC(sizeof(*r300));
201 if (!r300)
202 return GL_FALSE;
203
204 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
205 hw_tcl_on = future_hw_tcl_on = 0;
206
207 /* Parse configuration files.
208 * Do this here so that initialMaxAnisotropy is set before we create
209 * the default textures.
210 */
211 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
212 screen->driScreen->myNum, "r300");
213 r300->initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache,
214 "def_max_anisotropy");
215
216 /* Init default driver functions then plug in our R300-specific functions
217 * (the texture functions are especially important)
218 */
219 _mesa_init_driver_functions(&functions);
220 r300InitIoctlFuncs(&functions);
221 r300InitStateFuncs(&functions);
222 r300InitTextureFuncs(&functions);
223 r300InitShaderFuncs(&functions);
224
225 #ifdef USER_BUFFERS
226 r300_mem_init(r300);
227 #endif
228
229 if (!radeonInitContext(&r300->radeon, &functions,
230 glVisual, driContextPriv,
231 sharedContextPrivate)) {
232 FREE(r300);
233 return GL_FALSE;
234 }
235
236 /* Init r300 context data */
237 r300->dma.buf0_address =
238 r300->radeon.radeonScreen->buffers->list[0].address;
239
240 (void)memset(r300->texture_heaps, 0, sizeof(r300->texture_heaps));
241 make_empty_list(&r300->swapped);
242
243 r300->nr_heaps = 1 /* screen->numTexHeaps */ ;
244 assert(r300->nr_heaps < RADEON_NR_TEX_HEAPS);
245 for (i = 0; i < r300->nr_heaps; i++) {
246 /* *INDENT-OFF* */
247 r300->texture_heaps[i] = driCreateTextureHeap(i, r300,
248 screen->
249 texSize[i], 12,
250 RADEON_NR_TEX_REGIONS,
251 (drmTextureRegionPtr)
252 r300->radeon.sarea->
253 tex_list[i],
254 &r300->radeon.sarea->
255 tex_age[i],
256 &r300->swapped,
257 sizeof
258 (r300TexObj),
259 (destroy_texture_object_t
260 *)
261 r300DestroyTexObj);
262 /* *INDENT-ON* */
263 }
264 r300->texture_depth = driQueryOptioni(&r300->radeon.optionCache,
265 "texture_depth");
266 if (r300->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
267 r300->texture_depth = (screen->cpp == 4) ?
268 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
269
270 /* Set the maximum texture size small enough that we can guarentee that
271 * all texture units can bind a maximal texture and have them both in
272 * texturable memory at once.
273 */
274
275 ctx = r300->radeon.glCtx;
276
277 ctx->Const.MaxTextureImageUnits =
278 driQueryOptioni(&r300->radeon.optionCache, "texture_image_units");
279 ctx->Const.MaxTextureCoordUnits =
280 driQueryOptioni(&r300->radeon.optionCache, "texture_coord_units");
281 ctx->Const.MaxTextureUnits =
282 MIN2(ctx->Const.MaxTextureImageUnits,
283 ctx->Const.MaxTextureCoordUnits);
284 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
285 ctx->Const.MaxTextureLodBias = 16.0;
286
287 if (screen->chip_family >= CHIP_FAMILY_RV515) {
288 ctx->Const.MaxTextureLevels = 13;
289 ctx->Const.MaxTextureRectSize = 4096;
290 }
291
292 ctx->Const.MinPointSize = 1.0;
293 ctx->Const.MinPointSizeAA = 1.0;
294 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
295 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
296
297 ctx->Const.MinLineWidth = 1.0;
298 ctx->Const.MinLineWidthAA = 1.0;
299 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
300 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
301
302 #ifdef USER_BUFFERS
303 /* Needs further modifications */
304 #if 0
305 ctx->Const.MaxArrayLockSize =
306 ( /*512 */ RADEON_BUFFER_SIZE * 16 * 1024) / (4 * 4);
307 #endif
308 #endif
309
310 /* Initialize the software rasterizer and helper modules.
311 */
312 _swrast_CreateContext(ctx);
313 _vbo_CreateContext(ctx);
314 _tnl_CreateContext(ctx);
315 _swsetup_CreateContext(ctx);
316 _swsetup_Wakeup(ctx);
317 _ae_create_context(ctx);
318
319 /* Install the customized pipeline:
320 */
321 _tnl_destroy_pipeline(ctx);
322 _tnl_install_pipeline(ctx, r300_pipeline);
323
324 /* Try and keep materials and vertices separate:
325 */
326 /* _tnl_isolate_materials(ctx, GL_TRUE); */
327
328 /* Configure swrast and TNL to match hardware characteristics:
329 */
330 _swrast_allow_pixel_fog(ctx, GL_FALSE);
331 _swrast_allow_vertex_fog(ctx, GL_TRUE);
332 _tnl_allow_pixel_fog(ctx, GL_FALSE);
333 _tnl_allow_vertex_fog(ctx, GL_TRUE);
334
335 /* currently bogus data */
336 if (screen->chip_flags & RADEON_CHIPSET_TCL) {
337 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
338 ctx->Const.VertexProgram.MaxNativeInstructions =
339 VSF_MAX_FRAGMENT_LENGTH / 4;
340 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
341 ctx->Const.VertexProgram.MaxTemps = 32;
342 ctx->Const.VertexProgram.MaxNativeTemps =
343 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
344 ctx->Const.VertexProgram.MaxNativeParameters = 256; /* r420 */
345 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
346 }
347
348 ctx->Const.FragmentProgram.MaxNativeTemps = PFS_NUM_TEMP_REGS;
349 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
350 ctx->Const.FragmentProgram.MaxNativeParameters = PFS_NUM_CONST_REGS;
351 ctx->Const.FragmentProgram.MaxNativeAluInstructions = PFS_MAX_ALU_INST;
352 ctx->Const.FragmentProgram.MaxNativeTexInstructions = PFS_MAX_TEX_INST;
353 ctx->Const.FragmentProgram.MaxNativeInstructions =
354 PFS_MAX_ALU_INST + PFS_MAX_TEX_INST;
355 ctx->Const.FragmentProgram.MaxNativeTexIndirections =
356 PFS_MAX_TEX_INDIRECT;
357 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
358 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
359 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
360
361 driInitExtensions(ctx, card_extensions, GL_TRUE);
362
363 if (driQueryOptionb
364 (&r300->radeon.optionCache, "disable_stencil_two_side"))
365 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
366
367 if (r300->radeon.glCtx->Mesa_DXTn
368 && !driQueryOptionb(&r300->radeon.optionCache, "disable_s3tc")) {
369 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
370 _mesa_enable_extension(ctx, "GL_S3_s3tc");
371 } else
372 if (driQueryOptionb(&r300->radeon.optionCache, "force_s3tc_enable"))
373 {
374 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
375 }
376
377 r300->disable_lowimpact_fallback =
378 driQueryOptionb(&r300->radeon.optionCache,
379 "disable_lowimpact_fallback");
380
381 radeonInitSpanFuncs(ctx);
382 r300InitCmdBuf(r300);
383 r300InitState(r300);
384 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
385 r300InitSwtcl(ctx);
386
387 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
388
389 tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
390 if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
391 fprintf(stderr, "disabling 3D acceleration\n");
392 #if R200_MERGED
393 FALLBACK(&r300->radeon, RADEON_FALLBACK_DISABLE, 1);
394 #endif
395 }
396 if (tcl_mode == DRI_CONF_TCL_SW ||
397 !(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL)) {
398 if (r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
399 r300->radeon.radeonScreen->chip_flags &=
400 ~RADEON_CHIPSET_TCL;
401 fprintf(stderr, "Disabling HW TCL support\n");
402 }
403 TCL_FALLBACK(r300->radeon.glCtx,
404 RADEON_TCL_FALLBACK_TCL_DISABLE, 1);
405 }
406
407 return GL_TRUE;
408 }
409
410 static void r300FreeGartAllocations(r300ContextPtr r300)
411 {
412 int i, ret, tries = 0, done_age, in_use = 0;
413 drm_radeon_mem_free_t memfree;
414
415 memfree.region = RADEON_MEM_REGION_GART;
416
417 #ifdef USER_BUFFERS
418 for (i = r300->rmm->u_last; i > 0; i--) {
419 if (r300->rmm->u_list[i].ptr == NULL) {
420 continue;
421 }
422
423 /* check whether this buffer is still in use */
424 if (r300->rmm->u_list[i].pending) {
425 in_use++;
426 }
427 }
428 /* Cannot flush/lock if no context exists. */
429 if (in_use)
430 r300FlushCmdBuf(r300, __FUNCTION__);
431
432 done_age = radeonGetAge((radeonContextPtr) r300);
433
434 for (i = r300->rmm->u_last; i > 0; i--) {
435 if (r300->rmm->u_list[i].ptr == NULL) {
436 continue;
437 }
438
439 /* check whether this buffer is still in use */
440 if (!r300->rmm->u_list[i].pending) {
441 continue;
442 }
443
444 assert(r300->rmm->u_list[i].h_pending == 0);
445
446 tries = 0;
447 while (r300->rmm->u_list[i].age > done_age && tries++ < 1000) {
448 usleep(10);
449 done_age = radeonGetAge((radeonContextPtr) r300);
450 }
451 if (tries >= 1000) {
452 WARN_ONCE("Failed to idle region!");
453 }
454
455 memfree.region_offset = (char *)r300->rmm->u_list[i].ptr -
456 (char *)r300->radeon.radeonScreen->gartTextures.map;
457
458 ret = drmCommandWrite(r300->radeon.radeonScreen->driScreen->fd,
459 DRM_RADEON_FREE, &memfree,
460 sizeof(memfree));
461 if (ret) {
462 fprintf(stderr, "Failed to free at %p\nret = %s\n",
463 r300->rmm->u_list[i].ptr, strerror(-ret));
464 } else {
465 if (i == r300->rmm->u_last)
466 r300->rmm->u_last--;
467
468 r300->rmm->u_list[i].pending = 0;
469 r300->rmm->u_list[i].ptr = NULL;
470 }
471 }
472 r300->rmm->u_head = i;
473 #endif /* USER_BUFFERS */
474 }
475
476 /* Destroy the device specific context.
477 */
478 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
479 {
480 GET_CURRENT_CONTEXT(ctx);
481 r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
482 radeonContextPtr radeon = (radeonContextPtr) r300;
483 radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
484
485 if (RADEON_DEBUG & DEBUG_DRI) {
486 fprintf(stderr, "Destroying context !\n");
487 }
488
489 /* check if we're deleting the currently bound context */
490 if (&r300->radeon == current) {
491 radeonFlush(r300->radeon.glCtx);
492 _mesa_make_current(NULL, NULL, NULL);
493 }
494
495 /* Free r300 context resources */
496 assert(r300); /* should never be null */
497
498 if (r300) {
499 GLboolean release_texture_heaps;
500
501 release_texture_heaps =
502 (r300->radeon.glCtx->Shared->RefCount == 1);
503 _swsetup_DestroyContext(r300->radeon.glCtx);
504 _tnl_DestroyContext(r300->radeon.glCtx);
505 _vbo_DestroyContext(r300->radeon.glCtx);
506 _swrast_DestroyContext(r300->radeon.glCtx);
507
508 if (r300->dma.current.buf) {
509 r300ReleaseDmaRegion(r300, &r300->dma.current,
510 __FUNCTION__);
511 #ifndef USER_BUFFERS
512 r300FlushCmdBuf(r300, __FUNCTION__);
513 #endif
514 }
515 r300FreeGartAllocations(r300);
516 r300DestroyCmdBuf(r300);
517
518 if (radeon->state.scissor.pClipRects) {
519 FREE(radeon->state.scissor.pClipRects);
520 radeon->state.scissor.pClipRects = NULL;
521 }
522
523 if (release_texture_heaps) {
524 /* This share group is about to go away, free our private
525 * texture object data.
526 */
527 int i;
528
529 for (i = 0; i < r300->nr_heaps; i++) {
530 driDestroyTextureHeap(r300->texture_heaps[i]);
531 r300->texture_heaps[i] = NULL;
532 }
533
534 assert(is_empty_list(&r300->swapped));
535 }
536
537 radeonCleanupContext(&r300->radeon);
538
539 #ifdef USER_BUFFERS
540 /* the memory manager might be accessed when Mesa frees the shared
541 * state, so don't destroy it earlier
542 */
543 r300_mem_destroy(r300);
544 #endif
545
546 /* free the option cache */
547 driDestroyOptionCache(&r300->radeon.optionCache);
548
549 FREE(r300);
550 }
551 }