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