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