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