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