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