2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
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.
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:
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.
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.
28 **************************************************************************/
32 * Keith Whitwell <keith@tungstengraphics.com>
33 * Nicolai Haehnle <prefect_@gmx.net>
36 #include "api_arrayelt.h"
38 #include "simple_list.h"
41 #include "extensions.h"
43 #include "bufferobj.h"
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
50 #include "tnl/t_pipeline.h"
51 #include "tnl/t_vp_build.h"
53 #include "drivers/common/driverfuncs.h"
55 #include "radeon_ioctl.h"
56 #include "radeon_span.h"
57 #include "r300_context.h"
58 #include "r300_cmdbuf.h"
59 #include "r300_state.h"
60 #include "r300_ioctl.h"
62 #include "r300_maos.h"
70 #include "xmlpool.h" /* for symbolic values of enum-type options */
72 /* hw_tcl_on derives from future_hw_tcl_on when its safe to change it. */
73 int future_hw_tcl_on
= 1;
76 #define need_GL_EXT_stencil_two_side
77 #define need_GL_ARB_multisample
78 #define need_GL_ARB_texture_compression
79 #define need_GL_ARB_vertex_buffer_object
80 #define need_GL_ARB_vertex_program
81 #define need_GL_EXT_blend_minmax
82 //#define need_GL_EXT_fog_coord
83 #define need_GL_EXT_secondary_color
84 #define need_GL_EXT_blend_equation_separate
85 #define need_GL_EXT_blend_func_separate
86 #define need_GL_EXT_gpu_program_parameters
87 #define need_GL_NV_vertex_program
88 #include "extension_helper.h"
90 const struct dri_extension card_extensions
[] = {
92 {"GL_ARB_multisample", GL_ARB_multisample_functions
},
93 {"GL_ARB_multitexture", NULL
},
94 {"GL_ARB_texture_border_clamp", NULL
},
95 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions
},
96 {"GL_ARB_texture_cube_map", NULL
},
97 {"GL_ARB_texture_env_add", NULL
},
98 {"GL_ARB_texture_env_combine", NULL
},
99 {"GL_ARB_texture_env_crossbar", NULL
},
100 {"GL_ARB_texture_env_dot3", NULL
},
101 {"GL_ARB_texture_mirrored_repeat", NULL
},
102 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions
},
103 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
104 {"GL_ARB_fragment_program", NULL
},
105 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions
},
106 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions
},
107 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions
},
108 {"GL_EXT_blend_subtract", NULL
},
109 // {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
110 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions
},
111 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions
},
112 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions
},
113 {"GL_EXT_stencil_wrap", NULL
},
114 {"GL_EXT_texture_edge_clamp", NULL
},
115 {"GL_EXT_texture_env_combine", NULL
},
116 {"GL_EXT_texture_env_dot3", NULL
},
117 {"GL_EXT_texture_filter_anisotropic", NULL
},
118 {"GL_EXT_texture_lod_bias", NULL
},
119 {"GL_EXT_texture_mirror_clamp", NULL
},
120 {"GL_EXT_texture_rectangle", NULL
},
121 {"GL_ATI_texture_env_combine3", NULL
},
122 {"GL_ATI_texture_mirror_once", NULL
},
123 {"GL_MESA_pack_invert", NULL
},
124 {"GL_MESA_ycbcr_texture", NULL
},
125 {"GL_MESAX_texture_float", NULL
},
126 {"GL_NV_blend_square", NULL
},
127 {"GL_NV_vertex_program", GL_NV_vertex_program_functions
},
128 {"GL_SGIS_generate_mipmap", NULL
},
133 extern struct tnl_pipeline_stage _r300_render_stage
;
134 extern const struct tnl_pipeline_stage _r300_tcl_stage
;
136 static const struct tnl_pipeline_stage
*r300_pipeline
[] = {
138 /* Try and go straight to t&l
142 /* Catch any t&l fallbacks
144 &_tnl_vertex_transform_stage
,
145 &_tnl_normal_transform_stage
,
146 &_tnl_lighting_stage
,
147 &_tnl_fog_coordinate_stage
,
149 &_tnl_texture_transform_stage
,
150 &_tnl_vertex_program_stage
,
152 /* Try again to go to tcl?
153 * - no good for asymmetric-twoside (do with multipass)
154 * - no good for asymmetric-unfilled (do with multipass)
155 * - good for material
157 * - need to manipulate a bit of state
159 * - worth it/not worth it?
162 /* Else do them here.
165 &_tnl_render_stage
, /* FALLBACK */
169 /* Create the device specific rendering context.
171 GLboolean
r300CreateContext(const __GLcontextModes
* glVisual
,
172 __DRIcontextPrivate
* driContextPriv
,
173 void *sharedContextPrivate
)
175 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
176 radeonScreenPtr screen
= (radeonScreenPtr
) (sPriv
->private);
177 struct dd_function_table functions
;
183 assert(driContextPriv
);
186 /* Allocate the R300 context */
187 r300
= (r300ContextPtr
) CALLOC(sizeof(*r300
));
191 if (!(screen
->chip_flags
& RADEON_CHIPSET_TCL
))
192 hw_tcl_on
= future_hw_tcl_on
= 0;
194 /* Parse configuration files.
195 * Do this here so that initialMaxAnisotropy is set before we create
196 * the default textures.
198 driParseConfigFiles(&r300
->radeon
.optionCache
, &screen
->optionCache
,
199 screen
->driScreen
->myNum
, "r300");
200 r300
->initialMaxAnisotropy
= driQueryOptionf(&r300
->radeon
.optionCache
,
201 "def_max_anisotropy");
203 //r300->texmicrotile = GL_TRUE;
205 /* Init default driver functions then plug in our R300-specific functions
206 * (the texture functions are especially important)
208 _mesa_init_driver_functions(&functions
);
209 r300InitIoctlFuncs(&functions
);
210 r300InitStateFuncs(&functions
);
211 r300InitTextureFuncs(&functions
);
212 r300InitShaderFuncs(&functions
);
218 if (!radeonInitContext(&r300
->radeon
, &functions
,
219 glVisual
, driContextPriv
,
220 sharedContextPrivate
)) {
225 /* Init r300 context data */
226 r300
->dma
.buf0_address
=
227 r300
->radeon
.radeonScreen
->buffers
->list
[0].address
;
229 (void)memset(r300
->texture_heaps
, 0, sizeof(r300
->texture_heaps
));
230 make_empty_list(&r300
->swapped
);
232 r300
->nr_heaps
= 1 /* screen->numTexHeaps */ ;
233 assert(r300
->nr_heaps
< RADEON_NR_TEX_HEAPS
);
234 for (i
= 0; i
< r300
->nr_heaps
; i
++) {
236 r300
->texture_heaps
[i
] = driCreateTextureHeap(i
, r300
,
239 RADEON_NR_TEX_REGIONS
,
240 (drmTextureRegionPtr
)
243 &r300
->radeon
.sarea
->
248 (destroy_texture_object_t
253 r300
->texture_depth
= driQueryOptioni(&r300
->radeon
.optionCache
,
255 if (r300
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
256 r300
->texture_depth
= (screen
->cpp
== 4) ?
257 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
259 /* Set the maximum texture size small enough that we can guarentee that
260 * all texture units can bind a maximal texture and have them both in
261 * texturable memory at once.
264 ctx
= r300
->radeon
.glCtx
;
266 ctx
->Const
.MaxTextureImageUnits
=
267 driQueryOptioni(&r300
->radeon
.optionCache
, "texture_image_units");
268 ctx
->Const
.MaxTextureCoordUnits
=
269 driQueryOptioni(&r300
->radeon
.optionCache
, "texture_coord_units");
270 ctx
->Const
.MaxTextureUnits
=
271 MIN2(ctx
->Const
.MaxTextureImageUnits
,
272 ctx
->Const
.MaxTextureCoordUnits
);
273 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
275 ctx
->Const
.MinPointSize
= 1.0;
276 ctx
->Const
.MinPointSizeAA
= 1.0;
277 ctx
->Const
.MaxPointSize
= R300_POINTSIZE_MAX
;
278 ctx
->Const
.MaxPointSizeAA
= R300_POINTSIZE_MAX
;
280 ctx
->Const
.MinLineWidth
= 1.0;
281 ctx
->Const
.MinLineWidthAA
= 1.0;
282 ctx
->Const
.MaxLineWidth
= R300_LINESIZE_MAX
;
283 ctx
->Const
.MaxLineWidthAA
= R300_LINESIZE_MAX
;
286 /* Needs further modifications */
288 ctx
->Const
.MaxArrayLockSize
=
289 ( /*512 */ RADEON_BUFFER_SIZE
* 16 * 1024) / (4 * 4);
293 /* Initialize the software rasterizer and helper modules.
295 _swrast_CreateContext(ctx
);
296 _vbo_CreateContext(ctx
);
297 _tnl_CreateContext(ctx
);
298 _swsetup_CreateContext(ctx
);
299 _swsetup_Wakeup(ctx
);
300 _ae_create_context(ctx
);
302 /* Install the customized pipeline:
304 _tnl_destroy_pipeline(ctx
);
305 _tnl_install_pipeline(ctx
, r300_pipeline
);
307 /* Try and keep materials and vertices separate:
309 /* _tnl_isolate_materials(ctx, GL_TRUE); */
311 /* Configure swrast and TNL to match hardware characteristics:
313 _swrast_allow_pixel_fog(ctx
, GL_FALSE
);
314 _swrast_allow_vertex_fog(ctx
, GL_TRUE
);
315 _tnl_allow_pixel_fog(ctx
, GL_FALSE
);
316 _tnl_allow_vertex_fog(ctx
, GL_TRUE
);
318 /* currently bogus data */
319 ctx
->Const
.VertexProgram
.MaxInstructions
= VSF_MAX_FRAGMENT_LENGTH
/ 4;
320 ctx
->Const
.VertexProgram
.MaxNativeInstructions
=
321 VSF_MAX_FRAGMENT_LENGTH
/ 4;
322 ctx
->Const
.VertexProgram
.MaxNativeAttribs
= 16; /* r420 */
323 ctx
->Const
.VertexProgram
.MaxTemps
= 32;
324 ctx
->Const
.VertexProgram
.MaxNativeTemps
=
325 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
326 ctx
->Const
.VertexProgram
.MaxNativeParameters
= 256; /* r420 */
327 ctx
->Const
.VertexProgram
.MaxNativeAddressRegs
= 1;
329 ctx
->Const
.FragmentProgram
.MaxNativeTemps
= PFS_NUM_TEMP_REGS
;
330 ctx
->Const
.FragmentProgram
.MaxNativeAttribs
= 11; /* copy i915... */
331 ctx
->Const
.FragmentProgram
.MaxNativeParameters
= PFS_NUM_CONST_REGS
;
332 ctx
->Const
.FragmentProgram
.MaxNativeAluInstructions
= PFS_MAX_ALU_INST
;
333 ctx
->Const
.FragmentProgram
.MaxNativeTexInstructions
= PFS_MAX_TEX_INST
;
334 ctx
->Const
.FragmentProgram
.MaxNativeInstructions
=
335 PFS_MAX_ALU_INST
+ PFS_MAX_TEX_INST
;
336 ctx
->Const
.FragmentProgram
.MaxNativeTexIndirections
=
337 PFS_MAX_TEX_INDIRECT
;
338 ctx
->Const
.FragmentProgram
.MaxNativeAddressRegs
= 0; /* and these are?? */
339 _tnl_ProgramCacheInit(ctx
);
340 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
342 driInitExtensions(ctx
, card_extensions
, GL_TRUE
);
345 (&r300
->radeon
.optionCache
, "disable_stencil_two_side"))
346 _mesa_disable_extension(ctx
, "GL_EXT_stencil_two_side");
348 if (r300
->radeon
.glCtx
->Mesa_DXTn
349 && !driQueryOptionb(&r300
->radeon
.optionCache
, "disable_s3tc")) {
350 _mesa_enable_extension(ctx
, "GL_EXT_texture_compression_s3tc");
351 _mesa_enable_extension(ctx
, "GL_S3_s3tc");
353 if (driQueryOptionb(&r300
->radeon
.optionCache
, "force_s3tc_enable"))
355 _mesa_enable_extension(ctx
, "GL_EXT_texture_compression_s3tc");
358 r300
->disable_lowimpact_fallback
=
359 driQueryOptionb(&r300
->radeon
.optionCache
,
360 "disable_lowimpact_fallback");
362 radeonInitSpanFuncs(ctx
);
363 r300InitCmdBuf(r300
);
366 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
368 tcl_mode
= driQueryOptioni(&r300
->radeon
.optionCache
, "tcl_mode");
369 if (driQueryOptionb(&r300
->radeon
.optionCache
, "no_rast")) {
370 fprintf(stderr
, "disabling 3D acceleration\n");
372 FALLBACK(&r300
->radeon
, RADEON_FALLBACK_DISABLE
, 1);
375 if (tcl_mode
== DRI_CONF_TCL_SW
||
376 !(r300
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
)) {
377 if (r300
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
) {
378 r300
->radeon
.radeonScreen
->chip_flags
&=
380 fprintf(stderr
, "Disabling HW TCL support\n");
382 TCL_FALLBACK(r300
->radeon
.glCtx
,
383 RADEON_TCL_FALLBACK_TCL_DISABLE
, 1);
389 static void r300FreeGartAllocations(r300ContextPtr r300
)
391 int i
, ret
, tries
= 0, done_age
, in_use
= 0;
392 drm_radeon_mem_free_t memfree
;
394 memfree
.region
= RADEON_MEM_REGION_GART
;
397 for (i
= r300
->rmm
->u_last
; i
> 0; i
--) {
398 if (r300
->rmm
->u_list
[i
].ptr
== NULL
) {
402 /* check whether this buffer is still in use */
403 if (r300
->rmm
->u_list
[i
].pending
) {
407 /* Cannot flush/lock if no context exists. */
409 r300FlushCmdBuf(r300
, __FUNCTION__
);
411 done_age
= radeonGetAge((radeonContextPtr
) r300
);
413 for (i
= r300
->rmm
->u_last
; i
> 0; i
--) {
414 if (r300
->rmm
->u_list
[i
].ptr
== NULL
) {
418 /* check whether this buffer is still in use */
419 if (!r300
->rmm
->u_list
[i
].pending
) {
423 assert(r300
->rmm
->u_list
[i
].h_pending
== 0);
426 while (r300
->rmm
->u_list
[i
].age
> done_age
&& tries
++ < 1000) {
428 done_age
= radeonGetAge((radeonContextPtr
) r300
);
431 WARN_ONCE("Failed to idle region!");
434 memfree
.region_offset
= (char *)r300
->rmm
->u_list
[i
].ptr
-
435 (char *)r300
->radeon
.radeonScreen
->gartTextures
.map
;
437 ret
= drmCommandWrite(r300
->radeon
.radeonScreen
->driScreen
->fd
,
438 DRM_RADEON_FREE
, &memfree
,
441 fprintf(stderr
, "Failed to free at %p\nret = %s\n",
442 r300
->rmm
->u_list
[i
].ptr
, strerror(-ret
));
444 if (i
== r300
->rmm
->u_last
)
447 r300
->rmm
->u_list
[i
].pending
= 0;
448 r300
->rmm
->u_list
[i
].ptr
= NULL
;
449 if (r300
->rmm
->u_list
[i
].fb
) {
450 LOCK_HARDWARE(&(r300
->radeon
));
451 ret
= mmFreeMem(r300
->rmm
->u_list
[i
].fb
);
452 UNLOCK_HARDWARE(&(r300
->radeon
));
454 fprintf(stderr
, "failed to free!\n");
455 r300
->rmm
->u_list
[i
].fb
= NULL
;
457 r300
->rmm
->u_list
[i
].ref_count
= 0;
460 r300
->rmm
->u_head
= i
;
461 #endif /* USER_BUFFERS */
464 /* Destroy the device specific context.
466 void r300DestroyContext(__DRIcontextPrivate
* driContextPriv
)
468 GET_CURRENT_CONTEXT(ctx
);
469 r300ContextPtr r300
= (r300ContextPtr
) driContextPriv
->driverPrivate
;
470 radeonContextPtr radeon
= (radeonContextPtr
) r300
;
471 radeonContextPtr current
= ctx
? RADEON_CONTEXT(ctx
) : NULL
;
473 if (RADEON_DEBUG
& DEBUG_DRI
) {
474 fprintf(stderr
, "Destroying context !\n");
477 /* check if we're deleting the currently bound context */
478 if (&r300
->radeon
== current
) {
479 radeonFlush(r300
->radeon
.glCtx
);
480 _mesa_make_current(NULL
, NULL
, NULL
);
483 /* Free r300 context resources */
484 assert(r300
); /* should never be null */
487 GLboolean release_texture_heaps
;
489 release_texture_heaps
=
490 (r300
->radeon
.glCtx
->Shared
->RefCount
== 1);
491 _swsetup_DestroyContext(r300
->radeon
.glCtx
);
492 _tnl_ProgramCacheDestroy(r300
->radeon
.glCtx
);
493 _tnl_DestroyContext(r300
->radeon
.glCtx
);
494 _vbo_DestroyContext(r300
->radeon
.glCtx
);
495 _swrast_DestroyContext(r300
->radeon
.glCtx
);
497 if (r300
->dma
.current
.buf
) {
498 r300ReleaseDmaRegion(r300
, &r300
->dma
.current
,
501 r300FlushCmdBuf(r300
, __FUNCTION__
);
504 r300FreeGartAllocations(r300
);
505 r300DestroyCmdBuf(r300
);
507 if (radeon
->state
.scissor
.pClipRects
) {
508 FREE(radeon
->state
.scissor
.pClipRects
);
509 radeon
->state
.scissor
.pClipRects
= NULL
;
512 if (release_texture_heaps
) {
513 /* This share group is about to go away, free our private
514 * texture object data.
518 for (i
= 0; i
< r300
->nr_heaps
; i
++) {
519 driDestroyTextureHeap(r300
->texture_heaps
[i
]);
520 r300
->texture_heaps
[i
] = NULL
;
523 assert(is_empty_list(&r300
->swapped
));
526 radeonCleanupContext(&r300
->radeon
);
529 /* the memory manager might be accessed when Mesa frees the shared
530 * state, so don't destroy it earlier
532 r300_mem_destroy(r300
);
535 /* free the option cache */
536 driDestroyOptionCache(&r300
->radeon
.optionCache
);