r200/r300: port r200 texture handling to common code
[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_state.h"
63 #include "r300_ioctl.h"
64 #include "r300_tex.h"
65 #include "r300_emit.h"
66 #include "r300_swtcl.h"
67 #include "radeon_bo_legacy.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 static void r300_get_lock(radeonContextPtr rmesa)
191 {
192 drm_radeon_sarea_t *sarea = rmesa->sarea;
193
194 if (sarea->ctx_owner != rmesa->dri.hwContext) {
195 sarea->ctx_owner = rmesa->dri.hwContext;
196 if (!rmesa->radeonScreen->kernel_mm)
197 radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
198 }
199 }
200
201 static void r300_vtbl_flush(GLcontext *ctx)
202 {
203 r300Flush(ctx);
204 }
205
206 static void r300_vtbl_set_all_dirty(GLcontext *ctx)
207 {
208 r300ContextPtr rmesa = R300_CONTEXT(ctx);
209 rmesa->hw.all_dirty = GL_TRUE;
210 }
211
212 static void r300_vtbl_emit_state(radeonContextPtr rmesa)
213 {
214 r300EmitState((r300ContextPtr)rmesa);
215 }
216
217 extern int cs_write_dword(struct radeon_cs *cs, uint32_t dword);
218
219 static void r300_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
220 {
221 /* please flush pipe do all pending work */
222 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
223 R300_SC_SCREENDOOR, 1));
224 cs_write_dword(cs, 0x0);
225 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
226 R300_SC_SCREENDOOR, 1));
227 cs_write_dword(cs, 0x00FFFFFF);
228 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
229 R300_SC_HYPERZ, 1));
230 cs_write_dword(cs, 0x0);
231 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
232 R300_US_CONFIG, 1));
233 cs_write_dword(cs, 0x0);
234 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
235 R300_ZB_CNTL, 1));
236 cs_write_dword(cs, 0x0);
237 cs_write_dword(cs, cmdwait(rmesa->radeonScreen, R300_WAIT_3D));
238 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
239 R300_RB3D_DSTCACHE_CTLSTAT, 1));
240 cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
241 cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
242 R300_ZB_ZCACHE_CTLSTAT, 1));
243 cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
244 cs_write_dword(cs, cmdwait(rmesa->radeonScreen,
245 R300_WAIT_3D | R300_WAIT_3D_CLEAN));
246 }
247
248 static void r300_vtbl_flush_vertices(radeonContextPtr rmesa)
249 {
250 R300_FIREVERTICES(((r300ContextPtr)rmesa));
251 }
252
253 static void r300_init_vtbl(radeonContextPtr radeon)
254 {
255 radeon->vtbl.get_lock = r300_get_lock;
256 radeon->vtbl.update_viewport_offset = r300UpdateViewportOffset;
257 radeon->vtbl.flush = r300_vtbl_flush;
258 radeon->vtbl.set_all_dirty = r300_vtbl_set_all_dirty;
259 radeon->vtbl.update_draw_buffer = r300UpdateDrawBuffer;
260 radeon->vtbl.emit_cs_header = r300_vtbl_emit_cs_header;
261 radeon->vtbl.emit_state = r300_vtbl_emit_state;
262 }
263
264
265 /* Create the device specific rendering context.
266 */
267 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
268 __DRIcontextPrivate * driContextPriv,
269 void *sharedContextPrivate)
270 {
271 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
272 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
273 struct dd_function_table functions;
274 r300ContextPtr r300;
275 GLcontext *ctx;
276 int tcl_mode;
277
278 assert(glVisual);
279 assert(driContextPriv);
280 assert(screen);
281
282 /* Allocate the R300 context */
283 r300 = (r300ContextPtr) CALLOC(sizeof(*r300));
284 if (!r300)
285 return GL_FALSE;
286
287 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
288 hw_tcl_on = future_hw_tcl_on = 0;
289
290 r300_init_vtbl(&r300->radeon);
291 /* Parse configuration files.
292 * Do this here so that initialMaxAnisotropy is set before we create
293 * the default textures.
294 */
295 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
296 screen->driScreen->myNum, "r300");
297 r300->radeon.initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache,
298 "def_max_anisotropy");
299
300 /* Init default driver functions then plug in our R300-specific functions
301 * (the texture functions are especially important)
302 */
303 _mesa_init_driver_functions(&functions);
304 r300InitIoctlFuncs(&functions);
305 r300InitStateFuncs(&functions);
306 r300InitTextureFuncs(&functions);
307 r300InitShaderFuncs(&functions);
308
309 if (!radeonInitContext(&r300->radeon, &functions,
310 glVisual, driContextPriv,
311 sharedContextPrivate)) {
312 FREE(r300);
313 return GL_FALSE;
314 }
315
316 /* Init r300 context data */
317 r300->radeon.texture_depth = driQueryOptioni(&r300->radeon.optionCache,
318 "texture_depth");
319 if (r300->radeon.texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
320 r300->radeon.texture_depth = (screen->cpp == 4) ?
321 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
322
323 /* Set the maximum texture size small enough that we can guarentee that
324 * all texture units can bind a maximal texture and have them both in
325 * texturable memory at once.
326 */
327
328 ctx = r300->radeon.glCtx;
329
330 ctx->Const.MaxTextureImageUnits =
331 driQueryOptioni(&r300->radeon.optionCache, "texture_image_units");
332 ctx->Const.MaxTextureCoordUnits =
333 driQueryOptioni(&r300->radeon.optionCache, "texture_coord_units");
334 ctx->Const.MaxTextureUnits =
335 MIN2(ctx->Const.MaxTextureImageUnits,
336 ctx->Const.MaxTextureCoordUnits);
337 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
338 ctx->Const.MaxTextureLodBias = 16.0;
339
340 if (screen->chip_family >= CHIP_FAMILY_RV515) {
341 ctx->Const.MaxTextureLevels = 13;
342 ctx->Const.MaxTextureRectSize = 4096;
343 }
344
345 ctx->Const.MinPointSize = 1.0;
346 ctx->Const.MinPointSizeAA = 1.0;
347 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
348 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
349
350 ctx->Const.MinLineWidth = 1.0;
351 ctx->Const.MinLineWidthAA = 1.0;
352 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
353 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
354
355 /* Needs further modifications */
356 #if 0
357 ctx->Const.MaxArrayLockSize =
358 ( /*512 */ RADEON_BUFFER_SIZE * 16 * 1024) / (4 * 4);
359 #endif
360
361 /* Initialize the software rasterizer and helper modules.
362 */
363 _swrast_CreateContext(ctx);
364 _vbo_CreateContext(ctx);
365 _tnl_CreateContext(ctx);
366 _swsetup_CreateContext(ctx);
367 _swsetup_Wakeup(ctx);
368 _ae_create_context(ctx);
369
370 /* Install the customized pipeline:
371 */
372 _tnl_destroy_pipeline(ctx);
373 _tnl_install_pipeline(ctx, r300_pipeline);
374
375 /* Try and keep materials and vertices separate:
376 */
377 /* _tnl_isolate_materials(ctx, GL_TRUE); */
378
379 /* Configure swrast and TNL to match hardware characteristics:
380 */
381 _swrast_allow_pixel_fog(ctx, GL_FALSE);
382 _swrast_allow_vertex_fog(ctx, GL_TRUE);
383 _tnl_allow_pixel_fog(ctx, GL_FALSE);
384 _tnl_allow_vertex_fog(ctx, GL_TRUE);
385
386 /* currently bogus data */
387 if (screen->chip_flags & RADEON_CHIPSET_TCL) {
388 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
389 ctx->Const.VertexProgram.MaxNativeInstructions =
390 VSF_MAX_FRAGMENT_LENGTH / 4;
391 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
392 ctx->Const.VertexProgram.MaxTemps = 32;
393 ctx->Const.VertexProgram.MaxNativeTemps =
394 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
395 ctx->Const.VertexProgram.MaxNativeParameters = 256; /* r420 */
396 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
397 }
398
399 ctx->Const.FragmentProgram.MaxNativeTemps = PFS_NUM_TEMP_REGS;
400 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
401 ctx->Const.FragmentProgram.MaxNativeParameters = PFS_NUM_CONST_REGS;
402 ctx->Const.FragmentProgram.MaxNativeAluInstructions = PFS_MAX_ALU_INST;
403 ctx->Const.FragmentProgram.MaxNativeTexInstructions = PFS_MAX_TEX_INST;
404 ctx->Const.FragmentProgram.MaxNativeInstructions =
405 PFS_MAX_ALU_INST + PFS_MAX_TEX_INST;
406 ctx->Const.FragmentProgram.MaxNativeTexIndirections =
407 PFS_MAX_TEX_INDIRECT;
408 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
409 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
410 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
411
412 driInitExtensions(ctx, card_extensions, GL_TRUE);
413
414 if (driQueryOptionb
415 (&r300->radeon.optionCache, "disable_stencil_two_side"))
416 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
417
418 if (r300->radeon.glCtx->Mesa_DXTn
419 && !driQueryOptionb(&r300->radeon.optionCache, "disable_s3tc")) {
420 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
421 _mesa_enable_extension(ctx, "GL_S3_s3tc");
422 } else
423 if (driQueryOptionb(&r300->radeon.optionCache, "force_s3tc_enable"))
424 {
425 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
426 }
427
428 r300->disable_lowimpact_fallback =
429 driQueryOptionb(&r300->radeon.optionCache,
430 "disable_lowimpact_fallback");
431
432 radeonInitSpanFuncs(ctx);
433 r300InitCmdBuf(r300);
434 r300InitState(r300);
435 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
436 r300InitSwtcl(ctx);
437
438 TNL_CONTEXT(ctx)->Driver.RunPipeline = r300RunPipeline;
439
440 tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
441 if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
442 fprintf(stderr, "disabling 3D acceleration\n");
443 #if R200_MERGED
444 FALLBACK(&r300->radeon, RADEON_FALLBACK_DISABLE, 1);
445 #endif
446 }
447 if (tcl_mode == DRI_CONF_TCL_SW ||
448 !(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL)) {
449 if (r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
450 r300->radeon.radeonScreen->chip_flags &=
451 ~RADEON_CHIPSET_TCL;
452 fprintf(stderr, "Disabling HW TCL support\n");
453 }
454 TCL_FALLBACK(r300->radeon.glCtx,
455 RADEON_TCL_FALLBACK_TCL_DISABLE, 1);
456 }
457
458 return GL_TRUE;
459 }
460
461 /* Destroy the device specific context.
462 */
463 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
464 {
465 GET_CURRENT_CONTEXT(ctx);
466 r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
467 radeonContextPtr radeon = (radeonContextPtr) r300;
468 radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
469
470 if (RADEON_DEBUG & DEBUG_DRI) {
471 fprintf(stderr, "Destroying context !\n");
472 }
473
474 /* check if we're deleting the currently bound context */
475 if (&r300->radeon == current) {
476 radeonFlush(r300->radeon.glCtx);
477 _mesa_make_current(NULL, NULL, NULL);
478 }
479
480 /* Free r300 context resources */
481 assert(r300); /* should never be null */
482
483 if (r300) {
484 _swsetup_DestroyContext(r300->radeon.glCtx);
485 _tnl_DestroyContext(r300->radeon.glCtx);
486 _vbo_DestroyContext(r300->radeon.glCtx);
487 _swrast_DestroyContext(r300->radeon.glCtx);
488
489 rcommonFlushCmdBuf(&r300->radeon, __FUNCTION__);
490 r300DestroyCmdBuf(r300);
491
492 if (radeon->state.scissor.pClipRects) {
493 FREE(radeon->state.scissor.pClipRects);
494 radeon->state.scissor.pClipRects = NULL;
495 }
496
497 radeonCleanupContext(&r300->radeon);
498
499 /* the memory manager might be accessed when Mesa frees the shared
500 * state, so don't destroy it earlier
501 */
502
503
504 FREE(r300);
505 }
506 }