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