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