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"
47 #include "array_cache/acache.h"
50 #include "tnl/t_pipeline.h"
52 #include "drivers/common/driverfuncs.h"
54 #include "radeon_ioctl.h"
55 #include "radeon_span.h"
56 #include "r300_context.h"
57 #include "r300_cmdbuf.h"
58 #include "r300_state.h"
59 #include "r300_ioctl.h"
64 #include "xmlpool.h" /* for symbolic values of enum-type options */
66 /* hw_tcl_on derives from future_hw_tcl_on when its safe to change it. */
67 int future_hw_tcl_on
=0;
70 #define need_GL_ARB_multisample
71 #define need_GL_ARB_texture_compression
72 #define need_GL_ARB_vertex_buffer_object
73 #define need_GL_ARB_vertex_program
74 #define need_GL_EXT_blend_minmax
75 #define need_GL_EXT_secondary_color
76 #define need_GL_EXT_blend_equation_separate
77 #define need_GL_EXT_blend_func_separate
78 #define need_GL_NV_vertex_program
79 #include "extension_helper.h"
81 const struct dri_extension card_extensions
[] = {
82 {"GL_ARB_multisample", GL_ARB_multisample_functions
},
83 {"GL_ARB_multitexture", NULL
},
84 {"GL_ARB_texture_border_clamp", NULL
},
85 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions
},
86 /* disable until we support it, fixes a few things in ut2004 */
87 /* {"GL_ARB_texture_cube_map", NULL}, */
88 {"GL_ARB_texture_env_add", NULL
},
89 {"GL_ARB_texture_env_combine", NULL
},
90 {"GL_ARB_texture_env_crossbar", NULL
},
91 {"GL_ARB_texture_env_dot3", NULL
},
92 {"GL_ARB_texture_mirrored_repeat", NULL
},
93 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions
},
94 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
96 {"GL_ARB_fragment_program", NULL
},
98 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions
},
99 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions
},
100 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions
},
101 {"GL_EXT_blend_subtract", NULL
},
102 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions
},
103 {"GL_EXT_stencil_wrap", NULL
},
104 {"GL_EXT_texture_edge_clamp", NULL
},
105 {"GL_EXT_texture_env_combine", NULL
},
106 {"GL_EXT_texture_env_dot3", NULL
},
107 {"GL_EXT_texture_filter_anisotropic", NULL
},
108 {"GL_EXT_texture_lod_bias", NULL
},
109 {"GL_EXT_texture_mirror_clamp", NULL
},
110 {"GL_EXT_texture_rectangle", NULL
},
111 {"GL_ATI_texture_env_combine3", NULL
},
112 {"GL_ATI_texture_mirror_once", NULL
},
113 {"GL_MESA_pack_invert", NULL
},
114 {"GL_MESA_ycbcr_texture", NULL
},
115 {"GL_NV_blend_square", NULL
},
116 {"GL_NV_vertex_program", GL_NV_vertex_program_functions
},
117 {"GL_SGIS_generate_mipmap", NULL
},
121 extern struct tnl_pipeline_stage _r300_render_stage
;
122 extern struct tnl_pipeline_stage _r300_tcl_stage
;
124 static const struct tnl_pipeline_stage
*r300_pipeline
[] = {
126 /* Try and go straight to t&l
130 /* Catch any t&l fallbacks
132 &_tnl_vertex_transform_stage
,
133 &_tnl_normal_transform_stage
,
134 &_tnl_lighting_stage
,
135 &_tnl_fog_coordinate_stage
,
137 &_tnl_texture_transform_stage
,
138 &_tnl_arb_vertex_program_stage
,
139 &_tnl_vertex_program_stage
,
141 /* Try again to go to tcl?
142 * - no good for asymmetric-twoside (do with multipass)
143 * - no good for asymmetric-unfilled (do with multipass)
144 * - good for material
146 * - need to manipulate a bit of state
148 * - worth it/not worth it?
151 /* Else do them here.
154 &_tnl_render_stage
, /* FALLBACK */
158 static void r300BufferData(GLcontext
*ctx
, GLenum target
, GLsizeiptrARB size
,
159 const GLvoid
*data
, GLenum usage
, struct gl_buffer_object
*obj
)
161 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
162 drm_radeon_mem_alloc_t alloc
;
165 /* Free previous buffer */
167 drm_radeon_mem_free_t memfree
;
169 memfree
.region
= RADEON_MEM_REGION_GART
;
170 memfree
.region_offset
= (char *)obj
->Data
- (char *)rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
172 ret
= drmCommandWrite(rmesa
->radeon
.radeonScreen
->driScreen
->fd
,
173 DRM_RADEON_FREE
, &memfree
, sizeof(memfree
));
176 WARN_ONCE("Failed to free GART memroy!\n");
178 obj
->OnCard
= GL_FALSE
;
181 alloc
.region
= RADEON_MEM_REGION_GART
;
184 alloc
.region_offset
= &offset
;
186 ret
= drmCommandWriteRead( rmesa
->radeon
.dri
.fd
, DRM_RADEON_ALLOC
, &alloc
, sizeof(alloc
));
188 WARN_ONCE("Ran out of GART memory!\n");
190 _mesa_buffer_data(ctx
, target
, size
, data
, usage
, obj
);
193 obj
->Data
= ((GLubyte
*)rmesa
->radeon
.radeonScreen
->gartTextures
.map
) + offset
;
196 memcpy(obj
->Data
, data
, size
);
200 obj
->OnCard
= GL_TRUE
;
202 fprintf(stderr
, "allocated %d bytes at %p, offset=%d\n", size
, obj
->Data
, offset
);
206 static void r300DeleteBuffer(GLcontext
*ctx
, struct gl_buffer_object
*obj
)
208 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
210 if(r300IsGartMemory(rmesa
, obj
->Data
, obj
->Size
)){
211 drm_radeon_mem_free_t memfree
;
214 memfree
.region
= RADEON_MEM_REGION_GART
;
215 memfree
.region_offset
= (char *)obj
->Data
- (char *)rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
217 ret
= drmCommandWrite(rmesa
->radeon
.radeonScreen
->driScreen
->fd
,
218 DRM_RADEON_FREE
, &memfree
, sizeof(memfree
));
221 WARN_ONCE("Failed to free GART memroy!\n");
225 _mesa_delete_buffer_object(ctx
, obj
);
228 /* Create the device specific rendering context.
230 GLboolean
r300CreateContext(const __GLcontextModes
* glVisual
,
231 __DRIcontextPrivate
* driContextPriv
,
232 void *sharedContextPrivate
)
234 __DRIscreenPrivate
*sPriv
= driContextPriv
->driScreenPriv
;
235 radeonScreenPtr screen
= (radeonScreenPtr
) (sPriv
->private);
236 struct dd_function_table functions
;
242 assert(driContextPriv
);
245 /* Allocate the R300 context */
246 r300
= (r300ContextPtr
)CALLOC(sizeof(*r300
));
250 /* Parse configuration files.
251 * Do this here so that initialMaxAnisotropy is set before we create
252 * the default textures.
254 driParseConfigFiles(&r300
->radeon
.optionCache
, &screen
->optionCache
,
255 screen
->driScreen
->myNum
, "r300");
257 /* Init default driver functions then plug in our R300-specific functions
258 * (the texture functions are especially important)
260 _mesa_init_driver_functions(&functions
);
261 r300InitIoctlFuncs(&functions
);
262 r300InitStateFuncs(&functions
);
263 r300InitTextureFuncs(&functions
);
264 r300InitShaderFuncs(&functions
);
266 #if 0 /* Needs various Mesa changes... */
268 functions
.BufferData
= r300BufferData
;
269 functions
.DeleteBuffer
= r300DeleteBuffer
;
273 if (!radeonInitContext(&r300
->radeon
, &functions
,
274 glVisual
, driContextPriv
, sharedContextPrivate
)) {
279 /* Init r300 context data */
280 r300
->dma
.buf0_address
= r300
->radeon
.radeonScreen
->buffers
->list
[0].address
;
282 (void)memset(r300
->texture_heaps
, 0, sizeof(r300
->texture_heaps
));
283 make_empty_list(&r300
->swapped
);
285 r300
->nr_heaps
= 1 /* screen->numTexHeaps */ ;
286 assert(r300
->nr_heaps
< R200_NR_TEX_HEAPS
);
287 for (i
= 0; i
< r300
->nr_heaps
; i
++) {
288 r300
->texture_heaps
[i
] = driCreateTextureHeap(i
, r300
,
291 RADEON_NR_TEX_REGIONS
,
292 (drmTextureRegionPtr
)
295 &r300
->radeon
.sarea
->
300 (destroy_texture_object_t
304 r300
->texture_depth
= driQueryOptioni(&r300
->radeon
.optionCache
,
306 if (r300
->texture_depth
== DRI_CONF_TEXTURE_DEPTH_FB
)
307 r300
->texture_depth
= (screen
->cpp
== 4) ?
308 DRI_CONF_TEXTURE_DEPTH_32
: DRI_CONF_TEXTURE_DEPTH_16
;
310 /* Set the maximum texture size small enough that we can guarentee that
311 * all texture units can bind a maximal texture and have them both in
312 * texturable memory at once.
315 ctx
= r300
->radeon
.glCtx
;
317 ctx
->Const
.MaxTextureImageUnits
= driQueryOptioni(&r300
->radeon
.optionCache
,
318 "texture_image_units");
319 ctx
->Const
.MaxTextureCoordUnits
= driQueryOptioni(&r300
->radeon
.optionCache
,
320 "texture_coord_units");
321 ctx
->Const
.MaxTextureUnits
= MIN2(ctx
->Const
.MaxTextureImageUnits
,
322 ctx
->Const
.MaxTextureCoordUnits
);
323 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
325 ctx
->Const
.MinPointSize
= 1.0;
326 ctx
->Const
.MinPointSizeAA
= 1.0;
327 ctx
->Const
.MaxPointSize
= R300_POINTSIZE_MAX
;
328 ctx
->Const
.MaxPointSizeAA
= R300_POINTSIZE_MAX
;
330 ctx
->Const
.MinLineWidth
= 1.0;
331 ctx
->Const
.MinLineWidthAA
= 1.0;
332 ctx
->Const
.MaxLineWidth
= R300_LINESIZE_MAX
;
333 ctx
->Const
.MaxLineWidthAA
= R300_LINESIZE_MAX
;
335 /* Initialize the software rasterizer and helper modules.
337 _swrast_CreateContext(ctx
);
338 _ac_CreateContext(ctx
);
339 _tnl_CreateContext(ctx
);
340 _swsetup_CreateContext(ctx
);
341 _swsetup_Wakeup(ctx
);
342 _ae_create_context(ctx
);
344 /* Install the customized pipeline:
346 _tnl_destroy_pipeline(ctx
);
347 _tnl_install_pipeline(ctx
, r300_pipeline
);
349 /* Try and keep materials and vertices separate:
351 _tnl_isolate_materials(ctx
, GL_TRUE
);
353 /* Configure swrast and TNL to match hardware characteristics:
355 _swrast_allow_pixel_fog(ctx
, GL_FALSE
);
356 _swrast_allow_vertex_fog(ctx
, GL_TRUE
);
357 _tnl_allow_pixel_fog(ctx
, GL_FALSE
);
358 _tnl_allow_vertex_fog(ctx
, GL_TRUE
);
360 /* currently bogus data */
361 ctx
->Const
.MaxVertexProgramInstructions
=VSF_MAX_FRAGMENT_LENGTH
;
362 ctx
->Const
.MaxVertexProgramAttribs
=16; // r420
363 ctx
->Const
.MaxVertexProgramTemps
=VSF_MAX_FRAGMENT_TEMPS
;
364 ctx
->Const
.MaxVertexProgramLocalParams
=256; // r420
365 ctx
->Const
.MaxVertexProgramEnvParams
=256; // r420
366 ctx
->Const
.MaxVertexProgramAddressRegs
=1;
369 ctx
->Const
.MaxFragmentProgramTemps
= PFS_NUM_TEMP_REGS
;
370 ctx
->Const
.MaxFragmentProgramAttribs
= 11; /* copy i915... */
371 ctx
->Const
.MaxFragmentProgramLocalParams
= PFS_NUM_CONST_REGS
;
372 ctx
->Const
.MaxFragmentProgramEnvParams
= PFS_NUM_CONST_REGS
;
373 ctx
->Const
.MaxFragmentProgramAluInstructions
= PFS_MAX_ALU_INST
;
374 ctx
->Const
.MaxFragmentProgramTexInstructions
= PFS_MAX_TEX_INST
;
375 ctx
->Const
.MaxFragmentProgramInstructions
= PFS_MAX_ALU_INST
+PFS_MAX_TEX_INST
;
376 ctx
->Const
.MaxFragmentProgramTexIndirections
= PFS_MAX_TEX_INDIRECT
;
377 ctx
->Const
.MaxFragmentProgramAddressRegs
= 0; /* and these are?? */
378 ctx
->_MaintainTexEnvProgram
= GL_TRUE
;
381 driInitExtensions(ctx
, card_extensions
, GL_TRUE
);
383 if (r300
->radeon
.glCtx
->Mesa_DXTn
) {
384 _mesa_enable_extension( ctx
, "GL_EXT_texture_compression_s3tc" );
385 _mesa_enable_extension( ctx
, "GL_S3_s3tc" );
387 else if (driQueryOptionb (&r300
->radeon
.optionCache
, "force_s3tc_enable")) {
388 _mesa_enable_extension( ctx
, "GL_EXT_texture_compression_s3tc" );
391 radeonInitSpanFuncs(ctx
);
392 r300InitCmdBuf(r300
);
396 /* plug in a few more device driver functions */
397 /* XXX these should really go right after _mesa_init_driver_functions() */
398 r300InitPixelFuncs(ctx
);
401 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
403 tcl_mode
= driQueryOptioni(&r300
->radeon
.optionCache
, "tcl_mode");
404 if (driQueryOptionb(&r300
->radeon
.optionCache
, "no_rast")) {
405 fprintf(stderr
, "disabling 3D acceleration\n");
407 FALLBACK(&r300
->radeon
, RADEON_FALLBACK_DISABLE
, 1);
410 if (tcl_mode
== DRI_CONF_TCL_SW
||
411 !(r300
->radeon
.radeonScreen
->chipset
& RADEON_CHIPSET_TCL
)) {
412 if (r300
->radeon
.radeonScreen
->chipset
& RADEON_CHIPSET_TCL
) {
413 r300
->radeon
.radeonScreen
->chipset
&= ~RADEON_CHIPSET_TCL
;
414 fprintf(stderr
, "Disabling HW TCL support\n");
416 TCL_FALLBACK(r300
->radeon
.glCtx
, RADEON_TCL_FALLBACK_TCL_DISABLE
, 1);
422 /* Destroy the device specific context.
424 void r300DestroyContext(__DRIcontextPrivate
* driContextPriv
)
426 GET_CURRENT_CONTEXT(ctx
);
427 r300ContextPtr r300
= (r300ContextPtr
) driContextPriv
->driverPrivate
;
428 radeonContextPtr current
= ctx
? RADEON_CONTEXT(ctx
) : NULL
;
430 if (RADEON_DEBUG
& DEBUG_DRI
) {
431 fprintf(stderr
, "Destroying context !\n");
434 /* check if we're deleting the currently bound context */
435 if (&r300
->radeon
== current
) {
436 radeonFlush(r300
->radeon
.glCtx
);
437 _mesa_make_current(NULL
, NULL
, NULL
);
440 /* Free r300 context resources */
441 assert(r300
); /* should never be null */
444 GLboolean release_texture_heaps
;
446 release_texture_heaps
= (r300
->radeon
.glCtx
->Shared
->RefCount
== 1);
447 _swsetup_DestroyContext(r300
->radeon
.glCtx
);
448 _tnl_DestroyContext(r300
->radeon
.glCtx
);
449 _ac_DestroyContext(r300
->radeon
.glCtx
);
450 _swrast_DestroyContext(r300
->radeon
.glCtx
);
452 r300DestroyCmdBuf(r300
);
454 radeonCleanupContext(&r300
->radeon
);
456 /* free the option cache */
457 driDestroyOptionCache(&r300
->radeon
.optionCache
);