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