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