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