Fix broken tnl stage.
[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 * Authors:
32 * Keith Whitwell <keith@tungstengraphics.com>
33 * Nicolai Haehnle <prefect_@gmx.net>
34 */
35 #include "glheader.h"
36 #include "api_arrayelt.h"
37 #include "context.h"
38 #include "simple_list.h"
39 #include "imports.h"
40 #include "matrix.h"
41 #include "extensions.h"
42 #include "state.h"
43 #include "bufferobj.h"
44
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "array_cache/acache.h"
48
49 #include "tnl/tnl.h"
50 #include "tnl/t_pipeline.h"
51
52 #include "drivers/common/driverfuncs.h"
53
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"
60 #include "r300_tex.h"
61
62 #ifdef USER_BUFFERS
63 #include "radeon_mm.h"
64 #endif
65
66 #include "vblank.h"
67 #include "utils.h"
68 #include "xmlpool.h" /* for symbolic values of enum-type options */
69
70 /* hw_tcl_on derives from future_hw_tcl_on when its safe to change it. */
71 int future_hw_tcl_on=0;
72 int hw_tcl_on=0;
73
74 #define need_GL_ARB_multisample
75 #define need_GL_ARB_texture_compression
76 #define need_GL_ARB_vertex_buffer_object
77 #define need_GL_ARB_vertex_program
78 #define need_GL_EXT_blend_minmax
79 #define need_GL_EXT_secondary_color
80 #define need_GL_EXT_blend_equation_separate
81 #define need_GL_EXT_blend_func_separate
82 #define need_GL_NV_vertex_program
83 #include "extension_helper.h"
84
85 const struct dri_extension card_extensions[] = {
86 {"GL_ARB_multisample", GL_ARB_multisample_functions},
87 {"GL_ARB_multitexture", NULL},
88 {"GL_ARB_texture_border_clamp", NULL},
89 {"GL_ARB_texture_compression", GL_ARB_texture_compression_functions},
90 {"GL_ARB_texture_cube_map", NULL},
91 {"GL_ARB_texture_env_add", NULL},
92 {"GL_ARB_texture_env_combine", NULL},
93 {"GL_ARB_texture_env_crossbar", NULL},
94 {"GL_ARB_texture_env_dot3", NULL},
95 {"GL_ARB_texture_mirrored_repeat", NULL},
96 {"GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions},
97 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
98 #if USE_ARB_F_P == 1
99 {"GL_ARB_fragment_program", NULL},
100 #endif
101 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
102 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
103 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
104 {"GL_EXT_blend_subtract", NULL},
105 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
106 {"GL_EXT_stencil_wrap", NULL},
107 {"GL_EXT_texture_edge_clamp", NULL},
108 {"GL_EXT_texture_env_combine", NULL},
109 {"GL_EXT_texture_env_dot3", NULL},
110 {"GL_EXT_texture_filter_anisotropic", NULL},
111 {"GL_EXT_texture_lod_bias", NULL},
112 {"GL_EXT_texture_mirror_clamp", NULL},
113 {"GL_EXT_texture_rectangle", NULL},
114 {"GL_ATI_texture_env_combine3", NULL},
115 {"GL_ATI_texture_mirror_once", NULL},
116 {"GL_MESA_pack_invert", NULL},
117 {"GL_MESA_ycbcr_texture", NULL},
118 {"GL_NV_blend_square", NULL},
119 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
120 {"GL_SGIS_generate_mipmap", NULL},
121 {NULL, NULL}
122 };
123
124 extern struct tnl_pipeline_stage _r300_render_stage;
125 extern struct tnl_pipeline_stage _r300_tcl_stage;
126
127 static const struct tnl_pipeline_stage *r300_pipeline[] = {
128
129 /* Try and go straight to t&l
130 */
131 &_r300_tcl_stage,
132
133 /* Catch any t&l fallbacks
134 */
135 &_tnl_vertex_transform_stage,
136 &_tnl_normal_transform_stage,
137 &_tnl_lighting_stage,
138 &_tnl_fog_coordinate_stage,
139 &_tnl_texgen_stage,
140 &_tnl_texture_transform_stage,
141 &_tnl_arb_vertex_program_stage,
142 &_tnl_vertex_program_stage,
143
144 /* Try again to go to tcl?
145 * - no good for asymmetric-twoside (do with multipass)
146 * - no good for asymmetric-unfilled (do with multipass)
147 * - good for material
148 * - good for texgen
149 * - need to manipulate a bit of state
150 *
151 * - worth it/not worth it?
152 */
153
154 /* Else do them here.
155 */
156 &_r300_render_stage,
157 &_tnl_render_stage, /* FALLBACK */
158 0,
159 };
160
161
162 /* Create the device specific rendering context.
163 */
164 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
165 __DRIcontextPrivate * driContextPriv,
166 void *sharedContextPrivate)
167 {
168 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
169 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
170 struct dd_function_table functions;
171 r300ContextPtr r300;
172 GLcontext *ctx;
173 int tcl_mode, i;
174
175 assert(glVisual);
176 assert(driContextPriv);
177 assert(screen);
178
179 /* Allocate the R300 context */
180 r300 = (r300ContextPtr)CALLOC(sizeof(*r300));
181 if (!r300)
182 return GL_FALSE;
183
184 /* Parse configuration files.
185 * Do this here so that initialMaxAnisotropy is set before we create
186 * the default textures.
187 */
188 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
189 screen->driScreen->myNum, "r300");
190
191 /* Init default driver functions then plug in our R300-specific functions
192 * (the texture functions are especially important)
193 */
194 _mesa_init_driver_functions(&functions);
195 r300InitIoctlFuncs(&functions);
196 r300InitStateFuncs(&functions);
197 r300InitTextureFuncs(&functions);
198 r300InitShaderFuncs(&functions);
199
200 #ifdef USER_BUFFERS
201 radeon_mm_init(r300);
202 #endif
203 #ifdef HW_VBOS
204 if (hw_tcl_on) {
205 r300_init_vbo_funcs(&functions);
206 }
207 #endif
208 if (!radeonInitContext(&r300->radeon, &functions,
209 glVisual, driContextPriv, sharedContextPrivate)) {
210 FREE(r300);
211 return GL_FALSE;
212 }
213
214 /* Init r300 context data */
215 r300->dma.buf0_address = r300->radeon.radeonScreen->buffers->list[0].address;
216
217 (void)memset(r300->texture_heaps, 0, sizeof(r300->texture_heaps));
218 make_empty_list(&r300->swapped);
219
220 r300->nr_heaps = 1 /* screen->numTexHeaps */ ;
221 assert(r300->nr_heaps < R200_NR_TEX_HEAPS);
222 for (i = 0; i < r300->nr_heaps; i++) {
223 r300->texture_heaps[i] = driCreateTextureHeap(i, r300,
224 screen->
225 texSize[i], 12,
226 RADEON_NR_TEX_REGIONS,
227 (drmTextureRegionPtr)
228 r300->radeon.sarea->
229 tex_list[i],
230 &r300->radeon.sarea->
231 tex_age[i],
232 &r300->swapped,
233 sizeof
234 (r300TexObj),
235 (destroy_texture_object_t
236 *)
237 r300DestroyTexObj);
238 }
239 r300->texture_depth = driQueryOptioni(&r300->radeon.optionCache,
240 "texture_depth");
241 if (r300->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
242 r300->texture_depth = (screen->cpp == 4) ?
243 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
244
245 /* Set the maximum texture size small enough that we can guarentee that
246 * all texture units can bind a maximal texture and have them both in
247 * texturable memory at once.
248 */
249
250 ctx = r300->radeon.glCtx;
251
252 ctx->Const.MaxTextureImageUnits = driQueryOptioni(&r300->radeon.optionCache,
253 "texture_image_units");
254 ctx->Const.MaxTextureCoordUnits = driQueryOptioni(&r300->radeon.optionCache,
255 "texture_coord_units");
256 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureImageUnits,
257 ctx->Const.MaxTextureCoordUnits);
258 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
259
260 ctx->Const.MinPointSize = 1.0;
261 ctx->Const.MinPointSizeAA = 1.0;
262 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
263 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
264
265 ctx->Const.MinLineWidth = 1.0;
266 ctx->Const.MinLineWidthAA = 1.0;
267 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
268 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
269
270 if (hw_tcl_on)
271 ctx->_MaintainTnlProgram = GL_TRUE;
272
273 #ifdef USER_BUFFERS
274 /* Needs further modifications */
275 #if 0
276 ctx->Const.MaxArrayLockSize = (/*512*/RADEON_BUFFER_SIZE*16*1024) / (4*4);
277 #endif
278 #endif
279
280 /* Initialize the software rasterizer and helper modules.
281 */
282 _swrast_CreateContext(ctx);
283 _ac_CreateContext(ctx);
284 _tnl_CreateContext(ctx);
285 _swsetup_CreateContext(ctx);
286 _swsetup_Wakeup(ctx);
287 _ae_create_context(ctx);
288
289 /* Install the customized pipeline:
290 */
291 _tnl_destroy_pipeline(ctx);
292 _tnl_install_pipeline(ctx, r300_pipeline);
293
294 /* Try and keep materials and vertices separate:
295 */
296 _tnl_isolate_materials(ctx, GL_TRUE);
297
298 /* Configure swrast and TNL to match hardware characteristics:
299 */
300 _swrast_allow_pixel_fog(ctx, GL_FALSE);
301 _swrast_allow_vertex_fog(ctx, GL_TRUE);
302 _tnl_allow_pixel_fog(ctx, GL_FALSE);
303 _tnl_allow_vertex_fog(ctx, GL_TRUE);
304
305 /* currently bogus data */
306 ctx->Const.VertexProgram.MaxNativeInstructions=VSF_MAX_FRAGMENT_LENGTH;
307 ctx->Const.VertexProgram.MaxNativeAttribs=16; /* r420 */
308 ctx->Const.VertexProgram.MaxNativeTemps=VSF_MAX_FRAGMENT_TEMPS;
309 ctx->Const.VertexProgram.MaxNativeParameters=256; /* r420 */
310 ctx->Const.VertexProgram.MaxNativeAddressRegs=1;
311
312 #if USE_ARB_F_P
313 ctx->Const.FragmentProgram.MaxNativeTemps = PFS_NUM_TEMP_REGS;
314 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
315 ctx->Const.FragmentProgram.MaxNativeParameters = PFS_NUM_CONST_REGS;
316 ctx->Const.FragmentProgram.MaxNativeAluInstructions = PFS_MAX_ALU_INST;
317 ctx->Const.FragmentProgram.MaxNativeTexInstructions = PFS_MAX_TEX_INST;
318 ctx->Const.FragmentProgram.MaxNativeInstructions = PFS_MAX_ALU_INST+PFS_MAX_TEX_INST;
319 ctx->Const.FragmentProgram.MaxNativeTexIndirections = PFS_MAX_TEX_INDIRECT;
320 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
321 ctx->_MaintainTexEnvProgram = GL_TRUE;
322 #endif
323
324 driInitExtensions(ctx, card_extensions, GL_TRUE);
325
326 if (r300->radeon.glCtx->Mesa_DXTn) {
327 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
328 _mesa_enable_extension( ctx, "GL_S3_s3tc" );
329 }
330 else if (driQueryOptionb (&r300->radeon.optionCache, "force_s3tc_enable")) {
331 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
332 }
333
334 radeonInitSpanFuncs(ctx);
335 r300InitCmdBuf(r300);
336 r300InitState(r300);
337
338 #ifdef RADEON_VTXFMT_A
339 radeon_init_vtxfmt_a(r300);
340 #endif
341
342 #if 0
343 /* plug in a few more device driver functions */
344 /* XXX these should really go right after _mesa_init_driver_functions() */
345 r300InitPixelFuncs(ctx);
346 r300InitSwtcl(ctx);
347 #endif
348 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
349
350 tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
351 if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
352 fprintf(stderr, "disabling 3D acceleration\n");
353 #if R200_MERGED
354 FALLBACK(&r300->radeon, RADEON_FALLBACK_DISABLE, 1);
355 #endif
356 }
357 if (tcl_mode == DRI_CONF_TCL_SW ||
358 !(r300->radeon.radeonScreen->chipset & RADEON_CHIPSET_TCL)) {
359 if (r300->radeon.radeonScreen->chipset & RADEON_CHIPSET_TCL) {
360 r300->radeon.radeonScreen->chipset &= ~RADEON_CHIPSET_TCL;
361 fprintf(stderr, "Disabling HW TCL support\n");
362 }
363 TCL_FALLBACK(r300->radeon.glCtx, RADEON_TCL_FALLBACK_TCL_DISABLE, 1);
364 }
365
366 return GL_TRUE;
367 }
368
369 /* Destroy the device specific context.
370 */
371 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
372 {
373 GET_CURRENT_CONTEXT(ctx);
374 r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
375 radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
376
377 if (RADEON_DEBUG & DEBUG_DRI) {
378 fprintf(stderr, "Destroying context !\n");
379 }
380
381 /* check if we're deleting the currently bound context */
382 if (&r300->radeon == current) {
383 radeonFlush(r300->radeon.glCtx);
384 _mesa_make_current(NULL, NULL, NULL);
385 }
386
387 /* Free r300 context resources */
388 assert(r300); /* should never be null */
389
390 if (r300) {
391 GLboolean release_texture_heaps;
392
393 release_texture_heaps = (r300->radeon.glCtx->Shared->RefCount == 1);
394 _swsetup_DestroyContext(r300->radeon.glCtx);
395 _tnl_DestroyContext(r300->radeon.glCtx);
396 _ac_DestroyContext(r300->radeon.glCtx);
397 _swrast_DestroyContext(r300->radeon.glCtx);
398
399 r300DestroyCmdBuf(r300);
400
401 radeonCleanupContext(&r300->radeon);
402
403 /* free the option cache */
404 driDestroyOptionCache(&r300->radeon.optionCache);
405
406 FREE(r300);
407 }
408 }