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