9004a3ffab53d496d19fc44484c70feed340f54e
[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_render.h"
68 #include "r300_swtcl.h"
69 #include "radeon_bocs_wrapper.h"
70
71
72 #include "vblank.h"
73 #include "utils.h"
74 #include "xmlpool.h" /* for symbolic values of enum-type options */
75
76 #define need_GL_VERSION_2_0
77 #define need_GL_ARB_point_parameters
78 #define need_GL_ARB_vertex_program
79 #define need_GL_EXT_blend_equation_separate
80 #define need_GL_EXT_blend_func_separate
81 #define need_GL_EXT_blend_minmax
82 #define need_GL_EXT_framebuffer_object
83 #define need_GL_EXT_fog_coord
84 #define need_GL_EXT_gpu_program_parameters
85 #define need_GL_EXT_secondary_color
86 #define need_GL_EXT_stencil_two_side
87 #define need_GL_ATI_separate_stencil
88 #define need_GL_NV_vertex_program
89
90 #include "extension_helper.h"
91
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_packed_depth_stencil", NULL},
114 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
115 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions},
116 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
117 {"GL_EXT_shadow_funcs", NULL},
118 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions},
119 {"GL_EXT_stencil_wrap", NULL},
120 {"GL_EXT_texture_edge_clamp", NULL},
121 {"GL_EXT_texture_env_combine", NULL},
122 {"GL_EXT_texture_env_dot3", NULL},
123 {"GL_EXT_texture_filter_anisotropic", NULL},
124 {"GL_EXT_texture_lod_bias", NULL},
125 {"GL_EXT_texture_mirror_clamp", NULL},
126 {"GL_EXT_texture_rectangle", NULL},
127 {"GL_EXT_vertex_array_bgra", NULL},
128 {"GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions},
129 {"GL_ATI_texture_env_combine3", NULL},
130 {"GL_ATI_texture_mirror_once", NULL},
131 {"GL_MESA_pack_invert", NULL},
132 {"GL_MESA_ycbcr_texture", NULL},
133 {"GL_MESAX_texture_float", NULL},
134 {"GL_NV_blend_square", NULL},
135 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
136 {"GL_SGIS_generate_mipmap", NULL},
137 {NULL, NULL}
138 /* *INDENT-ON* */
139 };
140
141
142 const struct dri_extension mm_extensions[] = {
143 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
144 { NULL, NULL }
145 };
146
147 /**
148 * The GL 2.0 functions are needed to make display lists work with
149 * functions added by GL_ATI_separate_stencil.
150 */
151 const struct dri_extension gl_20_extension[] = {
152 {"GL_VERSION_2_0", GL_VERSION_2_0_functions },
153 };
154
155 static const struct tnl_pipeline_stage *r300_pipeline[] = {
156
157 /* Catch any t&l fallbacks
158 */
159 &_tnl_vertex_transform_stage,
160 &_tnl_normal_transform_stage,
161 &_tnl_lighting_stage,
162 &_tnl_fog_coordinate_stage,
163 &_tnl_texgen_stage,
164 &_tnl_texture_transform_stage,
165 &_tnl_point_attenuation_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 r300_get_lock(radeonContextPtr rmesa)
186 {
187 drm_radeon_sarea_t *sarea = rmesa->sarea;
188
189 if (sarea->ctx_owner != rmesa->dri.hwContext) {
190 sarea->ctx_owner = rmesa->dri.hwContext;
191 if (!rmesa->radeonScreen->kernel_mm)
192 radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
193 }
194 }
195
196 static void r300_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
197 {
198 /* please flush pipe do all pending work */
199 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
200 R300_SC_SCREENDOOR, 1));
201 radeon_cs_write_dword(cs, 0x0);
202 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
203 R300_SC_SCREENDOOR, 1));
204 radeon_cs_write_dword(cs, 0x00FFFFFF);
205 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
206 R300_SC_HYPERZ, 1));
207 radeon_cs_write_dword(cs, 0x0);
208 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
209 R300_US_CONFIG, 1));
210 radeon_cs_write_dword(cs, 0x0);
211 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
212 R300_ZB_CNTL, 1));
213 radeon_cs_write_dword(cs, 0x0);
214 radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen, R300_WAIT_3D));
215 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
216 R300_RB3D_DSTCACHE_CTLSTAT, 1));
217 radeon_cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
218 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
219 R300_ZB_ZCACHE_CTLSTAT, 1));
220 radeon_cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
221 radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen,
222 R300_WAIT_3D | R300_WAIT_3D_CLEAN));
223 }
224
225 static void r300_vtbl_pre_emit_atoms(radeonContextPtr radeon)
226 {
227 r300ContextPtr r300 = (r300ContextPtr)radeon;
228 BATCH_LOCALS(radeon);
229
230 r300->vap_flush_needed = GL_TRUE;
231
232 cp_wait(radeon, R300_WAIT_3D | R300_WAIT_3D_CLEAN);
233 BEGIN_BATCH_NO_AUTOSTATE(2);
234 OUT_BATCH_REGVAL(R300_TX_INVALTAGS, R300_TX_FLUSH);
235 END_BATCH();
236 end_3d(radeon);
237 }
238
239 static void r300_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
240 {
241 r300ContextPtr r300 = R300_CONTEXT(ctx);
242 if (mode)
243 r300->radeon.Fallback |= bit;
244 else
245 r300->radeon.Fallback &= ~bit;
246 }
247
248 static void r300_init_vtbl(radeonContextPtr radeon)
249 {
250 radeon->vtbl.get_lock = r300_get_lock;
251 radeon->vtbl.update_viewport_offset = r300UpdateViewportOffset;
252 radeon->vtbl.emit_cs_header = r300_vtbl_emit_cs_header;
253 radeon->vtbl.swtcl_flush = r300_swtcl_flush;
254 radeon->vtbl.pre_emit_atoms = r300_vtbl_pre_emit_atoms;
255 radeon->vtbl.fallback = r300_fallback;
256 }
257
258 static void r300InitConstValues(GLcontext *ctx, radeonScreenPtr screen)
259 {
260 r300ContextPtr r300 = R300_CONTEXT(ctx);
261
262 ctx->Const.MaxTextureImageUnits =
263 driQueryOptioni(&r300->radeon.optionCache, "texture_image_units");
264 ctx->Const.MaxTextureCoordUnits =
265 driQueryOptioni(&r300->radeon.optionCache, "texture_coord_units");
266 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureImageUnits,
267 ctx->Const.MaxTextureCoordUnits);
268 /* FIXME: When no memory manager is available we should set this
269 * to some reasonable value based on texture memory pool size */
270 /* FIXME: r5xx limit is 4096 */
271 ctx->Const.MaxTextureLevels = 12;
272 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
273 ctx->Const.MaxTextureLodBias = 16.0;
274
275 if (screen->chip_family >= CHIP_FAMILY_RV515)
276 ctx->Const.MaxTextureLevels = 13;
277 else
278 ctx->Const.MaxTextureLevels = 12;
279
280 ctx->Const.MinPointSize = 1.0;
281 ctx->Const.MinPointSizeAA = 1.0;
282 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
283 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
284
285 ctx->Const.MinLineWidth = 1.0;
286 ctx->Const.MinLineWidthAA = 1.0;
287 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
288 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
289
290 ctx->Const.MaxDrawBuffers = 1;
291
292 /* currently bogus data */
293 if (r300->options.hw_tcl_enabled) {
294 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
295 ctx->Const.VertexProgram.MaxNativeInstructions =
296 VSF_MAX_FRAGMENT_LENGTH / 4;
297 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
298 ctx->Const.VertexProgram.MaxTemps = 32;
299 ctx->Const.VertexProgram.MaxNativeTemps =
300 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
301 ctx->Const.VertexProgram.MaxNativeParameters = 256; /* r420 */
302 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
303 }
304
305 if (screen->chip_family >= CHIP_FAMILY_RV515) {
306 ctx->Const.FragmentProgram.MaxNativeTemps = R500_PFS_NUM_TEMP_REGS;
307 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
308 ctx->Const.FragmentProgram.MaxNativeParameters = R500_PFS_NUM_CONST_REGS;
309 ctx->Const.FragmentProgram.MaxNativeAluInstructions = R500_PFS_MAX_INST;
310 ctx->Const.FragmentProgram.MaxNativeTexInstructions = R500_PFS_MAX_INST;
311 ctx->Const.FragmentProgram.MaxNativeInstructions = R500_PFS_MAX_INST;
312 ctx->Const.FragmentProgram.MaxNativeTexIndirections = R500_PFS_MAX_INST;
313 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;
314 } else {
315 ctx->Const.FragmentProgram.MaxNativeTemps = R300_PFS_NUM_TEMP_REGS;
316 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
317 ctx->Const.FragmentProgram.MaxNativeParameters = R300_PFS_NUM_CONST_REGS;
318 ctx->Const.FragmentProgram.MaxNativeAluInstructions = R300_PFS_MAX_ALU_INST;
319 ctx->Const.FragmentProgram.MaxNativeTexInstructions = R300_PFS_MAX_TEX_INST;
320 ctx->Const.FragmentProgram.MaxNativeInstructions = R300_PFS_MAX_ALU_INST + R300_PFS_MAX_TEX_INST;
321 ctx->Const.FragmentProgram.MaxNativeTexIndirections = R300_PFS_MAX_TEX_INDIRECT;
322 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;
323 }
324 }
325
326 static void r300ParseOptions(r300ContextPtr r300, radeonScreenPtr screen)
327 {
328 struct r300_options options = { 0 };
329
330 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
331 screen->driScreen->myNum, "r300");
332
333 r300->radeon.initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache, "def_max_anisotropy");
334
335 options.stencil_two_side_disabled = driQueryOptionb(&r300->radeon.optionCache, "disable_stencil_two_side");
336 options.s3tc_force_enabled = driQueryOptionb(&r300->radeon.optionCache, "force_s3tc_enable");
337 options.s3tc_force_disabled = driQueryOptionb(&r300->radeon.optionCache, "disable_s3tc");
338
339 if (!(screen->chip_flags & RADEON_CHIPSET_TCL) || driQueryOptioni(&r300->radeon.optionCache, "tcl_mode") == DRI_CONF_TCL_SW)
340 options.hw_tcl_enabled = 0;
341 else
342 options.hw_tcl_enabled = 1;
343
344 options.conformance_mode = !driQueryOptionb(&r300->radeon.optionCache, "disable_lowimpact_fallback");
345
346 r300->options = options;
347 }
348
349 static void r300InitGLExtensions(GLcontext *ctx)
350 {
351 r300ContextPtr r300 = R300_CONTEXT(ctx);
352
353 driInitExtensions(ctx, card_extensions, GL_TRUE);
354 if (r300->radeon.radeonScreen->kernel_mm)
355 driInitExtensions(ctx, mm_extensions, GL_FALSE);
356
357 if (r300->options.stencil_two_side_disabled)
358 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
359
360 if (ctx->Mesa_DXTn && !r300->options.s3tc_force_enabled) {
361 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
362 _mesa_enable_extension(ctx, "GL_S3_s3tc");
363 } else if (r300->options.s3tc_force_disabled) {
364 _mesa_disable_extension(ctx, "GL_EXT_texture_compression_s3tc");
365 }
366 }
367
368 /* Create the device specific rendering context.
369 */
370 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
371 __DRIcontextPrivate * driContextPriv,
372 void *sharedContextPrivate)
373 {
374 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
375 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
376 struct dd_function_table functions;
377 r300ContextPtr r300;
378 GLcontext *ctx;
379
380 assert(glVisual);
381 assert(driContextPriv);
382 assert(screen);
383
384 r300 = (r300ContextPtr) CALLOC(sizeof(*r300));
385 if (!r300)
386 return GL_FALSE;
387
388 r300ParseOptions(r300, screen);
389
390 r300_init_vtbl(&r300->radeon);
391
392 _mesa_init_driver_functions(&functions);
393 r300InitIoctlFuncs(&functions);
394 r300InitStateFuncs(&functions);
395 r300InitTextureFuncs(&functions);
396 r300InitShaderFuncs(&functions);
397
398 if (!radeonInitContext(&r300->radeon, &functions,
399 glVisual, driContextPriv,
400 sharedContextPrivate)) {
401 FREE(r300);
402 return GL_FALSE;
403 }
404
405 ctx = r300->radeon.glCtx;
406
407 r300->fallback = 0;
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 r300InitDraw(ctx);
438 } else {
439 r300InitSwtcl(ctx);
440 }
441
442 radeon_fbo_init(&r300->radeon);
443 radeonInitSpanFuncs( ctx );
444 r300InitCmdBuf(r300);
445 r300InitState(r300);
446 r300InitShaderFunctions(r300);
447
448 if (screen->chip_family == CHIP_FAMILY_RS600 || screen->chip_family == CHIP_FAMILY_RS690 ||
449 screen->chip_family == CHIP_FAMILY_RS740) {
450 r300->radeon.texture_row_align = 64;
451 }
452
453 r300InitGLExtensions(ctx);
454
455 return GL_TRUE;
456 }
457