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