Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / drivers / dri / r600 / r600_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 "radeon_debug.h"
60 #include "r600_context.h"
61 #include "radeon_common_context.h"
62 #include "radeon_buffer_objects.h"
63 #include "radeon_span.h"
64 #include "r600_cmdbuf.h"
65 #include "r600_emit.h"
66 #include "radeon_bocs_wrapper.h"
67 #include "radeon_queryobj.h"
68 #include "r600_blit.h"
69
70 #include "r700_state.h"
71 #include "r700_ioctl.h"
72
73
74 #include "vblank.h"
75 #include "utils.h"
76 #include "xmlpool.h" /* for symbolic values of enum-type options */
77
78 #define R600_ENABLE_GLSL_TEST 1
79
80 #define need_GL_VERSION_2_0
81 #define need_GL_ARB_occlusion_query
82 #define need_GL_ARB_point_parameters
83 #define need_GL_ARB_vertex_program
84 #define need_GL_EXT_blend_equation_separate
85 #define need_GL_EXT_blend_func_separate
86 #define need_GL_EXT_blend_minmax
87 #define need_GL_EXT_framebuffer_object
88 #define need_GL_EXT_fog_coord
89 #define need_GL_EXT_gpu_program_parameters
90 #define need_GL_EXT_provoking_vertex
91 #define need_GL_EXT_secondary_color
92 #define need_GL_EXT_stencil_two_side
93 #define need_GL_ATI_separate_stencil
94 #define need_GL_NV_vertex_program
95
96 #include "main/remap_helper.h"
97
98 static const struct dri_extension card_extensions[] = {
99 /* *INDENT-OFF* */
100 {"GL_ARB_depth_clamp", NULL},
101 {"GL_ARB_depth_texture", NULL},
102 {"GL_ARB_fragment_program", NULL},
103 {"GL_ARB_fragment_program_shadow", NULL},
104 {"GL_ARB_occlusion_query", GL_ARB_occlusion_query_functions},
105 {"GL_ARB_multitexture", NULL},
106 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
107 {"GL_ARB_shadow", NULL},
108 {"GL_ARB_shadow_ambient", NULL},
109 {"GL_ARB_texture_border_clamp", NULL},
110 {"GL_ARB_texture_cube_map", NULL},
111 {"GL_ARB_texture_env_add", NULL},
112 {"GL_ARB_texture_env_combine", NULL},
113 {"GL_ARB_texture_env_crossbar", NULL},
114 {"GL_ARB_texture_env_dot3", NULL},
115 {"GL_ARB_texture_mirrored_repeat", NULL},
116 {"GL_ARB_texture_non_power_of_two", NULL},
117 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
118 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
119 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
120 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
121 {"GL_EXT_blend_subtract", NULL},
122 {"GL_EXT_packed_depth_stencil", NULL},
123 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
124 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions},
125 {"GL_EXT_provoking_vertex", GL_EXT_provoking_vertex_functions },
126 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
127 {"GL_EXT_shadow_funcs", NULL},
128 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions},
129 {"GL_EXT_stencil_wrap", NULL},
130 {"GL_EXT_texture_edge_clamp", NULL},
131 {"GL_EXT_texture_env_combine", NULL},
132 {"GL_EXT_texture_env_dot3", NULL},
133 {"GL_EXT_texture_filter_anisotropic", NULL},
134 {"GL_EXT_texture_lod_bias", NULL},
135 {"GL_EXT_texture_mirror_clamp", NULL},
136 {"GL_EXT_texture_rectangle", NULL},
137 {"GL_EXT_vertex_array_bgra", NULL},
138 {"GL_EXT_texture_sRGB", NULL},
139 {"GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions},
140 {"GL_ATI_texture_env_combine3", NULL},
141 {"GL_ATI_texture_mirror_once", NULL},
142 {"GL_MESA_pack_invert", NULL},
143 {"GL_MESA_ycbcr_texture", NULL},
144 {"GL_MESAX_texture_float", NULL},
145 {"GL_NV_blend_square", NULL},
146 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
147 {"GL_SGIS_generate_mipmap", NULL},
148 {NULL, NULL}
149 /* *INDENT-ON* */
150 };
151
152
153 static const struct dri_extension mm_extensions[] = {
154 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
155 { NULL, NULL }
156 };
157
158 /**
159 * The GL 2.0 functions are needed to make display lists work with
160 * functions added by GL_ATI_separate_stencil.
161 */
162 static const struct dri_extension gl_20_extension[] = {
163 #ifdef R600_ENABLE_GLSL_TEST
164 {"GL_ARB_shading_language_100", GL_VERSION_2_0_functions },
165 #else
166 {"GL_VERSION_2_0", GL_VERSION_2_0_functions },
167 #endif /* R600_ENABLE_GLSL_TEST */
168 {NULL, NULL}
169 };
170
171 static const struct tnl_pipeline_stage *r600_pipeline[] = {
172 /* Catch any t&l fallbacks
173 */
174 &_tnl_vertex_transform_stage,
175 &_tnl_normal_transform_stage,
176 &_tnl_lighting_stage,
177 &_tnl_fog_coordinate_stage,
178 &_tnl_texgen_stage,
179 &_tnl_texture_transform_stage,
180 &_tnl_point_attenuation_stage,
181 &_tnl_vertex_program_stage,
182 &_tnl_render_stage,
183 0,
184 };
185
186 static void r600_get_lock(radeonContextPtr rmesa)
187 {
188 drm_radeon_sarea_t *sarea = rmesa->sarea;
189
190 if (sarea->ctx_owner != rmesa->dri.hwContext) {
191 sarea->ctx_owner = rmesa->dri.hwContext;
192 if (!rmesa->radeonScreen->kernel_mm)
193 radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
194 }
195 }
196
197 static void r600_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
198 {
199 /* please flush pipe do all pending work */
200 /* to be enabled */
201 }
202
203 static void r600_vtbl_pre_emit_atoms(radeonContextPtr radeon)
204 {
205 r700Start3D((context_t *)radeon);
206 }
207
208 static void r600_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
209 {
210 context_t *context = R700_CONTEXT(ctx);
211 if (mode)
212 context->radeon.Fallback |= bit;
213 else
214 context->radeon.Fallback &= ~bit;
215 }
216
217 static void r600_emit_query_finish(radeonContextPtr radeon)
218 {
219 context_t *context = (context_t*) radeon;
220 BATCH_LOCALS(&context->radeon);
221
222 struct radeon_query_object *query = radeon->query.current;
223
224 BEGIN_BATCH_NO_AUTOSTATE(4 + 2);
225 R600_OUT_BATCH(CP_PACKET3(R600_IT_EVENT_WRITE, 2));
226 R600_OUT_BATCH(ZPASS_DONE);
227 R600_OUT_BATCH(query->curr_offset + 8); /* hw writes qwords */
228 R600_OUT_BATCH(0x00000000);
229 R600_OUT_BATCH_RELOC(VGT_EVENT_INITIATOR, query->bo, 0, 0, RADEON_GEM_DOMAIN_GTT, 0);
230 END_BATCH();
231 assert(query->curr_offset < RADEON_QUERY_PAGE_SIZE);
232 query->emitted_begin = GL_FALSE;
233 }
234
235 static void r600_init_vtbl(radeonContextPtr radeon)
236 {
237 radeon->vtbl.get_lock = r600_get_lock;
238 radeon->vtbl.update_viewport_offset = r700UpdateViewportOffset;
239 radeon->vtbl.emit_cs_header = r600_vtbl_emit_cs_header;
240 radeon->vtbl.swtcl_flush = NULL;
241 radeon->vtbl.pre_emit_atoms = r600_vtbl_pre_emit_atoms;
242 radeon->vtbl.fallback = r600_fallback;
243 radeon->vtbl.emit_query_finish = r600_emit_query_finish;
244 radeon->vtbl.blit = r600_blit;
245 }
246
247 static void r600InitConstValues(GLcontext *ctx, radeonScreenPtr screen)
248 {
249 context_t *r600 = R700_CONTEXT(ctx);
250
251 ctx->Const.MaxTextureImageUnits =
252 driQueryOptioni(&r600->radeon.optionCache, "texture_image_units");
253 ctx->Const.MaxTextureCoordUnits =
254 driQueryOptioni(&r600->radeon.optionCache, "texture_coord_units");
255 ctx->Const.MaxTextureUnits =
256 MIN2(ctx->Const.MaxTextureImageUnits,
257 ctx->Const.MaxTextureCoordUnits);
258 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
259 ctx->Const.MaxTextureLodBias = 16.0;
260
261 ctx->Const.MaxTextureLevels = 13; /* hw support 14 */
262 ctx->Const.MaxTextureRectSize = 4096; /* hw support 8192 */
263
264 ctx->Const.MinPointSize = 0x0001 / 8.0;
265 ctx->Const.MinPointSizeAA = 0x0001 / 8.0;
266 ctx->Const.MaxPointSize = 0xffff / 8.0;
267 ctx->Const.MaxPointSizeAA = 0xffff / 8.0;
268
269 ctx->Const.MinLineWidth = 0x0001 / 8.0;
270 ctx->Const.MinLineWidthAA = 0x0001 / 8.0;
271 ctx->Const.MaxLineWidth = 0xffff / 8.0;
272 ctx->Const.MaxLineWidthAA = 0xffff / 8.0;
273
274 ctx->Const.MaxDrawBuffers = 1; /* hw supports 8 */
275
276 /* 256 for reg-based consts, inline consts also supported */
277 ctx->Const.VertexProgram.MaxInstructions = 8192; /* in theory no limit */
278 ctx->Const.VertexProgram.MaxNativeInstructions = 8192;
279 ctx->Const.VertexProgram.MaxNativeAttribs = 160;
280 ctx->Const.VertexProgram.MaxTemps = 128;
281 ctx->Const.VertexProgram.MaxNativeTemps = 128;
282 ctx->Const.VertexProgram.MaxNativeParameters = 256;
283 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1; /* ??? */
284
285 ctx->Const.FragmentProgram.MaxNativeTemps = 128;
286 ctx->Const.FragmentProgram.MaxNativeAttribs = 32;
287 ctx->Const.FragmentProgram.MaxNativeParameters = 256;
288 ctx->Const.FragmentProgram.MaxNativeAluInstructions = 8192;
289 /* 8 per clause on r6xx, 16 on rv670/r7xx */
290 if ((screen->chip_family == CHIP_FAMILY_RV670) ||
291 (screen->chip_family >= CHIP_FAMILY_RV770))
292 ctx->Const.FragmentProgram.MaxNativeTexInstructions = 16;
293 else
294 ctx->Const.FragmentProgram.MaxNativeTexInstructions = 8;
295 ctx->Const.FragmentProgram.MaxNativeInstructions = 8192;
296 ctx->Const.FragmentProgram.MaxNativeTexIndirections = 8; /* ??? */
297 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
298 }
299
300 static void r600ParseOptions(context_t *r600, radeonScreenPtr screen)
301 {
302 /* Parse configuration files.
303 * Do this here so that initialMaxAnisotropy is set before we create
304 * the default textures.
305 */
306 driParseConfigFiles(&r600->radeon.optionCache, &screen->optionCache,
307 screen->driScreen->myNum, "r600");
308
309 r600->radeon.initialMaxAnisotropy = driQueryOptionf(&r600->radeon.optionCache,
310 "def_max_anisotropy");
311
312 }
313
314 static void r600InitGLExtensions(GLcontext *ctx)
315 {
316 context_t *r600 = R700_CONTEXT(ctx);
317
318 driInitExtensions(ctx, card_extensions, GL_TRUE);
319 if (r600->radeon.radeonScreen->kernel_mm)
320 driInitExtensions(ctx, mm_extensions, GL_FALSE);
321
322 #ifdef R600_ENABLE_GLSL_TEST
323 driInitExtensions(ctx, gl_20_extension, GL_TRUE);
324 _mesa_enable_2_0_extensions(ctx);
325
326 /* glsl compiler has problem if this is not GL_TRUE */
327 ctx->Shader.EmitCondCodes = GL_TRUE;
328 #endif /* R600_ENABLE_GLSL_TEST */
329
330 if (driQueryOptionb
331 (&r600->radeon.optionCache, "disable_stencil_two_side"))
332 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
333
334 if (r600->radeon.glCtx->Mesa_DXTn
335 && !driQueryOptionb(&r600->radeon.optionCache, "disable_s3tc")) {
336 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
337 _mesa_enable_extension(ctx, "GL_S3_s3tc");
338 } else
339 if (driQueryOptionb(&r600->radeon.optionCache, "force_s3tc_enable"))
340 {
341 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
342 }
343
344 /* XXX: RV740 only seems to report results from half of its DBs */
345 if (r600->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV740)
346 _mesa_disable_extension(ctx, "GL_ARB_occlusion_query");
347 }
348
349 /* Create the device specific rendering context.
350 */
351 GLboolean r600CreateContext(const __GLcontextModes * glVisual,
352 __DRIcontext * driContextPriv,
353 void *sharedContextPrivate)
354 {
355 __DRIscreen *sPriv = driContextPriv->driScreenPriv;
356 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
357 struct dd_function_table functions;
358 context_t *r600;
359 GLcontext *ctx;
360
361 assert(glVisual);
362 assert(driContextPriv);
363 assert(screen);
364
365 /* Allocate the R600 context */
366 r600 = (context_t*) CALLOC(sizeof(*r600));
367 if (!r600) {
368 radeon_error("Failed to allocate memory for context.\n");
369 return GL_FALSE;
370 }
371
372 r600ParseOptions(r600, screen);
373
374 r600->radeon.radeonScreen = screen;
375 r600_init_vtbl(&r600->radeon);
376
377 /* Init default driver functions then plug in our R600-specific functions
378 * (the texture functions are especially important)
379 */
380 _mesa_init_driver_functions(&functions);
381
382 r700InitStateFuncs(&functions);
383 r600InitTextureFuncs(&r600->radeon, &functions);
384 r700InitShaderFuncs(&functions);
385 radeonInitQueryObjFunctions(&functions);
386 r700InitIoctlFuncs(&functions);
387 radeonInitBufferObjectFuncs(&functions);
388
389 if (!radeonInitContext(&r600->radeon, &functions,
390 glVisual, driContextPriv,
391 sharedContextPrivate)) {
392 radeon_error("Initializing context failed.\n");
393 FREE(r600);
394 return GL_FALSE;
395 }
396
397 ctx = r600->radeon.glCtx;
398
399 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
400 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
401
402 r600InitConstValues(ctx, screen);
403
404 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
405
406 /* Initialize the software rasterizer and helper modules.
407 */
408 _swrast_CreateContext(ctx);
409 _vbo_CreateContext(ctx);
410 _tnl_CreateContext(ctx);
411 _swsetup_CreateContext(ctx);
412 _swsetup_Wakeup(ctx);
413
414 /* Install the customized pipeline:
415 */
416 _tnl_destroy_pipeline(ctx);
417 _tnl_install_pipeline(ctx, r600_pipeline);
418 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
419
420 /* Configure swrast and TNL to match hardware characteristics:
421 */
422 _swrast_allow_pixel_fog(ctx, GL_FALSE);
423 _swrast_allow_vertex_fog(ctx, GL_TRUE);
424 _tnl_allow_pixel_fog(ctx, GL_FALSE);
425 _tnl_allow_vertex_fog(ctx, GL_TRUE);
426
427 radeon_init_debug();
428
429 r700InitDraw(ctx);
430
431 radeon_fbo_init(&r600->radeon);
432 radeonInitSpanFuncs( ctx );
433 r600InitCmdBuf(r600);
434 r700InitState(r600->radeon.glCtx);
435
436 r600InitGLExtensions(ctx);
437
438 return GL_TRUE;
439 }
440
441