r300: further cleanup
[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 #define need_GL_VERSION_2_0
76 #define need_GL_ARB_point_parameters
77 #define need_GL_ARB_vertex_program
78 #define need_GL_EXT_blend_equation_separate
79 #define need_GL_EXT_blend_func_separate
80 #define need_GL_EXT_blend_minmax
81 #define need_GL_EXT_framebuffer_object
82 #define need_GL_EXT_fog_coord
83 #define need_GL_EXT_gpu_program_parameters
84 #define need_GL_EXT_secondary_color
85 #define need_GL_EXT_stencil_two_side
86 #define need_GL_ATI_separate_stencil
87 #define need_GL_NV_vertex_program
88
89 #include "extension_helper.h"
90
91
92 const struct dri_extension card_extensions[] = {
93 /* *INDENT-OFF* */
94 {"GL_ARB_depth_texture", NULL},
95 {"GL_ARB_fragment_program", NULL},
96 {"GL_ARB_multitexture", NULL},
97 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
98 {"GL_ARB_shadow", NULL},
99 {"GL_ARB_shadow_ambient", NULL},
100 {"GL_ARB_texture_border_clamp", NULL},
101 {"GL_ARB_texture_cube_map", NULL},
102 {"GL_ARB_texture_env_add", NULL},
103 {"GL_ARB_texture_env_combine", NULL},
104 {"GL_ARB_texture_env_crossbar", NULL},
105 {"GL_ARB_texture_env_dot3", NULL},
106 {"GL_ARB_texture_mirrored_repeat", NULL},
107 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
108 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
109 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
110 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
111 {"GL_EXT_blend_subtract", NULL},
112 {"GL_EXT_packed_depth_stencil", 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 const struct dri_extension mm_extensions[] = {
141 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
142 { NULL, NULL }
143 };
144
145 /**
146 * The GL 2.0 functions are needed to make display lists work with
147 * functions added by GL_ATI_separate_stencil.
148 */
149 const struct dri_extension gl_20_extension[] = {
150 {"GL_VERSION_2_0", GL_VERSION_2_0_functions },
151 };
152
153
154 extern struct tnl_pipeline_stage _r300_render_stage;
155 extern const struct tnl_pipeline_stage _r300_tcl_stage;
156
157 static const struct tnl_pipeline_stage *r300_pipeline[] = {
158
159 /* Try and go straight to t&l
160 */
161 &_r300_tcl_stage,
162
163 /* Catch any t&l fallbacks
164 */
165 &_tnl_vertex_transform_stage,
166 &_tnl_normal_transform_stage,
167 &_tnl_lighting_stage,
168 &_tnl_fog_coordinate_stage,
169 &_tnl_texgen_stage,
170 &_tnl_texture_transform_stage,
171 &_tnl_point_attenuation_stage,
172 &_tnl_vertex_program_stage,
173
174 /* Try again to go to tcl?
175 * - no good for asymmetric-twoside (do with multipass)
176 * - no good for asymmetric-unfilled (do with multipass)
177 * - good for material
178 * - good for texgen
179 * - need to manipulate a bit of state
180 *
181 * - worth it/not worth it?
182 */
183
184 /* Else do them here.
185 */
186 &_r300_render_stage,
187 &_tnl_render_stage, /* FALLBACK */
188 0,
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_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
203 {
204 /* please flush pipe do all pending work */
205 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
206 R300_SC_SCREENDOOR, 1));
207 radeon_cs_write_dword(cs, 0x0);
208 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
209 R300_SC_SCREENDOOR, 1));
210 radeon_cs_write_dword(cs, 0x00FFFFFF);
211 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
212 R300_SC_HYPERZ, 1));
213 radeon_cs_write_dword(cs, 0x0);
214 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
215 R300_US_CONFIG, 1));
216 radeon_cs_write_dword(cs, 0x0);
217 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
218 R300_ZB_CNTL, 1));
219 radeon_cs_write_dword(cs, 0x0);
220 radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen, R300_WAIT_3D));
221 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
222 R300_RB3D_DSTCACHE_CTLSTAT, 1));
223 radeon_cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
224 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
225 R300_ZB_ZCACHE_CTLSTAT, 1));
226 radeon_cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
227 radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen,
228 R300_WAIT_3D | R300_WAIT_3D_CLEAN));
229 }
230
231 static void r300_vtbl_pre_emit_atoms(radeonContextPtr radeon)
232 {
233 r300ContextPtr r300 = (r300ContextPtr)radeon;
234 BATCH_LOCALS(radeon);
235
236 r300->vap_flush_needed = GL_TRUE;
237
238 cp_wait(radeon, R300_WAIT_3D | R300_WAIT_3D_CLEAN);
239 BEGIN_BATCH_NO_AUTOSTATE(2);
240 OUT_BATCH_REGVAL(R300_TX_INVALTAGS, R300_TX_FLUSH);
241 END_BATCH();
242 end_3d(radeon);
243 }
244
245 static void r300_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
246 {
247 r300ContextPtr r300 = R300_CONTEXT(ctx);
248 if (mode)
249 r300->radeon.Fallback |= bit;
250 else
251 r300->radeon.Fallback &= ~bit;
252 }
253
254 static void r300_init_vtbl(radeonContextPtr radeon)
255 {
256 radeon->vtbl.get_lock = r300_get_lock;
257 radeon->vtbl.update_viewport_offset = r300UpdateViewportOffset;
258 radeon->vtbl.emit_cs_header = r300_vtbl_emit_cs_header;
259 radeon->vtbl.swtcl_flush = r300_swtcl_flush;
260 radeon->vtbl.pre_emit_atoms = r300_vtbl_pre_emit_atoms;
261 radeon->vtbl.fallback = r300_fallback;
262 }
263
264 static void r300InitConstValues(GLcontext *ctx, radeonScreenPtr screen)
265 {
266 r300ContextPtr r300 = R300_CONTEXT(ctx);
267
268 ctx->Const.MaxTextureImageUnits =
269 driQueryOptioni(&r300->radeon.optionCache, "texture_image_units");
270 ctx->Const.MaxTextureCoordUnits =
271 driQueryOptioni(&r300->radeon.optionCache, "texture_coord_units");
272 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureImageUnits,
273 ctx->Const.MaxTextureCoordUnits);
274 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
275 ctx->Const.MaxTextureLodBias = 16.0;
276
277 if (screen->chip_family >= CHIP_FAMILY_RV515)
278 ctx->Const.MaxTextureLevels = 13;
279 else
280 ctx->Const.MaxTextureLevels = 12;
281
282 ctx->Const.MinPointSize = 1.0;
283 ctx->Const.MinPointSizeAA = 1.0;
284 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
285 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
286
287 ctx->Const.MinLineWidth = 1.0;
288 ctx->Const.MinLineWidthAA = 1.0;
289 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
290 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
291
292 ctx->Const.MaxDrawBuffers = 1;
293
294 /* currently bogus data */
295 if (r300->options.hw_tcl_enabled) {
296 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
297 ctx->Const.VertexProgram.MaxNativeInstructions =
298 VSF_MAX_FRAGMENT_LENGTH / 4;
299 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
300 ctx->Const.VertexProgram.MaxTemps = 32;
301 ctx->Const.VertexProgram.MaxNativeTemps =
302 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
303 ctx->Const.VertexProgram.MaxNativeParameters = 256; /* r420 */
304 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
305 }
306
307 if (screen->chip_family >= CHIP_FAMILY_RV515) {
308 ctx->Const.FragmentProgram.MaxNativeTemps = R500_PFS_NUM_TEMP_REGS;
309 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
310 ctx->Const.FragmentProgram.MaxNativeParameters = R500_PFS_NUM_CONST_REGS;
311 ctx->Const.FragmentProgram.MaxNativeAluInstructions = R500_PFS_MAX_INST;
312 ctx->Const.FragmentProgram.MaxNativeTexInstructions = R500_PFS_MAX_INST;
313 ctx->Const.FragmentProgram.MaxNativeInstructions = R500_PFS_MAX_INST;
314 ctx->Const.FragmentProgram.MaxNativeTexIndirections = R500_PFS_MAX_INST;
315 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;
316 } else {
317 ctx->Const.FragmentProgram.MaxNativeTemps = R300_PFS_NUM_TEMP_REGS;
318 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
319 ctx->Const.FragmentProgram.MaxNativeParameters = R300_PFS_NUM_CONST_REGS;
320 ctx->Const.FragmentProgram.MaxNativeAluInstructions = R300_PFS_MAX_ALU_INST;
321 ctx->Const.FragmentProgram.MaxNativeTexInstructions = R300_PFS_MAX_TEX_INST;
322 ctx->Const.FragmentProgram.MaxNativeInstructions = R300_PFS_MAX_ALU_INST + R300_PFS_MAX_TEX_INST;
323 ctx->Const.FragmentProgram.MaxNativeTexIndirections = R300_PFS_MAX_TEX_INDIRECT;
324 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;
325 }
326 }
327
328 static void r300ParseOptions(r300ContextPtr r300, radeonScreenPtr screen)
329 {
330 struct r300_options options = { 0 };
331
332 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
333 screen->driScreen->myNum, "r300");
334
335 r300->disable_lowimpact_fallback = driQueryOptionb(&r300->radeon.optionCache, "disable_lowimpact_fallback");
336 r300->radeon.initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache, "def_max_anisotropy");
337
338 options.stencil_two_side_disabled = driQueryOptionb(&r300->radeon.optionCache, "disable_stencil_two_side");
339 options.s3tc_force_enabled = driQueryOptionb(&r300->radeon.optionCache, "force_s3tc_enable");
340 options.s3tc_force_disabled = driQueryOptionb(&r300->radeon.optionCache, "disable_s3tc");
341
342 if (!(screen->chip_flags & RADEON_CHIPSET_TCL) || driQueryOptioni(&r300->radeon.optionCache, "tcl_mode") == DRI_CONF_TCL_SW)
343 options.hw_tcl_enabled = 0;
344 else
345 options.hw_tcl_enabled = 1;
346
347 r300->options = options;
348 }
349
350 static void r300InitGLExtensions(GLcontext *ctx)
351 {
352 r300ContextPtr r300 = R300_CONTEXT(ctx);
353
354 driInitExtensions(ctx, card_extensions, GL_TRUE);
355 if (r300->radeon.radeonScreen->kernel_mm)
356 driInitExtensions(ctx, mm_extensions, GL_FALSE);
357
358 if (r300->options.stencil_two_side_disabled)
359 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
360
361 if (ctx->Mesa_DXTn && !r300->options.s3tc_force_enabled) {
362 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
363 _mesa_enable_extension(ctx, "GL_S3_s3tc");
364 } else if (r300->options.s3tc_force_disabled) {
365 _mesa_disable_extension(ctx, "GL_EXT_texture_compression_s3tc");
366 }
367 }
368
369 /* Create the device specific rendering context.
370 */
371 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
372 __DRIcontextPrivate * driContextPriv,
373 void *sharedContextPrivate)
374 {
375 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
376 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
377 struct dd_function_table functions;
378 r300ContextPtr r300;
379 GLcontext *ctx;
380
381 assert(glVisual);
382 assert(driContextPriv);
383 assert(screen);
384
385 r300 = (r300ContextPtr) CALLOC(sizeof(*r300));
386 if (!r300)
387 return GL_FALSE;
388
389 r300ParseOptions(r300, screen);
390
391 r300_init_vtbl(&r300->radeon);
392
393 _mesa_init_driver_functions(&functions);
394 r300InitIoctlFuncs(&functions);
395 r300InitStateFuncs(&functions);
396 r300InitTextureFuncs(&functions);
397 r300InitShaderFuncs(&functions);
398
399 if (!radeonInitContext(&r300->radeon, &functions,
400 glVisual, driContextPriv,
401 sharedContextPrivate)) {
402 FREE(r300);
403 return GL_FALSE;
404 }
405
406 ctx = r300->radeon.glCtx;
407
408 if (r300->options.hw_tcl_enabled)
409 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
410
411 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
412
413 r300InitConstValues(ctx, screen);
414
415 /* Initialize the software rasterizer and helper modules.
416 */
417 _swrast_CreateContext(ctx);
418 _vbo_CreateContext(ctx);
419 _tnl_CreateContext(ctx);
420 _swsetup_CreateContext(ctx);
421 _swsetup_Wakeup(ctx);
422
423 /* Install the customized pipeline:
424 */
425 _tnl_destroy_pipeline(ctx);
426 _tnl_install_pipeline(ctx, r300_pipeline);
427 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
428
429 /* Configure swrast and TNL to match hardware characteristics:
430 */
431 _swrast_allow_pixel_fog(ctx, GL_FALSE);
432 _swrast_allow_vertex_fog(ctx, GL_TRUE);
433 _tnl_allow_pixel_fog(ctx, GL_FALSE);
434 _tnl_allow_vertex_fog(ctx, GL_TRUE);
435
436 if (!r300->options.hw_tcl_enabled)
437 r300InitSwtcl(ctx);
438
439 radeon_fbo_init(&r300->radeon);
440 radeonInitSpanFuncs( ctx );
441 r300InitCmdBuf(r300);
442 r300InitState(r300);
443 r300InitShaderFunctions(r300);
444
445 if (screen->chip_family == CHIP_FAMILY_RS600 || screen->chip_family == CHIP_FAMILY_RS690 ||
446 screen->chip_family == CHIP_FAMILY_RS740) {
447 r300->radeon.texture_row_align = 64;
448 }
449
450 r300InitGLExtensions(ctx);
451
452 return GL_TRUE;
453 }
454