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