mesa: Enable remap table in core.
[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
68 #include "r700_state.h"
69 #include "r700_ioctl.h"
70
71
72 #include "vblank.h"
73 #include "utils.h"
74 #include "xmlpool.h" /* for symbolic values of enum-type options */
75
76 /* hw_tcl_on derives from future_hw_tcl_on when its safe to change it. */
77 int future_hw_tcl_on = 1;
78 int hw_tcl_on = 1;
79
80 #define need_GL_VERSION_2_0
81 #define need_GL_ARB_point_parameters
82 #define need_GL_ARB_vertex_program
83 #define need_GL_EXT_blend_equation_separate
84 #define need_GL_EXT_blend_func_separate
85 #define need_GL_EXT_blend_minmax
86 #define need_GL_EXT_framebuffer_object
87 #define need_GL_EXT_fog_coord
88 #define need_GL_EXT_gpu_program_parameters
89 #define need_GL_EXT_provoking_vertex
90 #define need_GL_EXT_secondary_color
91 #define need_GL_EXT_stencil_two_side
92 #define need_GL_ATI_separate_stencil
93 #define need_GL_NV_vertex_program
94
95 #include "main/remap_helper.h"
96
97 static const struct dri_extension card_extensions[] = {
98 /* *INDENT-OFF* */
99 {"GL_ARB_depth_texture", NULL},
100 {"GL_ARB_fragment_program", NULL},
101 {"GL_ARB_multitexture", NULL},
102 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
103 {"GL_ARB_shadow", NULL},
104 {"GL_ARB_shadow_ambient", NULL},
105 {"GL_ARB_texture_border_clamp", NULL},
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_program", GL_ARB_vertex_program_functions},
113 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
114 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
115 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
116 {"GL_EXT_blend_subtract", NULL},
117 {"GL_EXT_packed_depth_stencil", NULL},
118 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
119 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions},
120 {"GL_EXT_provoking_vertex", GL_EXT_provoking_vertex_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_EXT_vertex_array_bgra", NULL},
133 {"GL_EXT_texture_sRGB", NULL},
134 {"GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions},
135 {"GL_ATI_texture_env_combine3", NULL},
136 {"GL_ATI_texture_mirror_once", NULL},
137 {"GL_MESA_pack_invert", NULL},
138 {"GL_MESA_ycbcr_texture", NULL},
139 {"GL_MESAX_texture_float", NULL},
140 {"GL_NV_blend_square", NULL},
141 {"GL_NV_vertex_program", GL_NV_vertex_program_functions},
142 {"GL_SGIS_generate_mipmap", NULL},
143 {NULL, NULL}
144 /* *INDENT-ON* */
145 };
146
147
148 static const struct dri_extension mm_extensions[] = {
149 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
150 { NULL, NULL }
151 };
152
153 /**
154 * The GL 2.0 functions are needed to make display lists work with
155 * functions added by GL_ATI_separate_stencil.
156 */
157 static const struct dri_extension gl_20_extension[] = {
158 {"GL_VERSION_2_0", GL_VERSION_2_0_functions },
159 };
160
161 static const struct tnl_pipeline_stage *r600_pipeline[] = {
162 /* Catch any t&l fallbacks
163 */
164 &_tnl_vertex_transform_stage,
165 &_tnl_normal_transform_stage,
166 &_tnl_lighting_stage,
167 &_tnl_fog_coordinate_stage,
168 &_tnl_texgen_stage,
169 &_tnl_texture_transform_stage,
170 &_tnl_point_attenuation_stage,
171 &_tnl_vertex_program_stage,
172 &_tnl_render_stage,
173 0,
174 };
175
176 static void r600_get_lock(radeonContextPtr rmesa)
177 {
178 drm_radeon_sarea_t *sarea = rmesa->sarea;
179
180 if (sarea->ctx_owner != rmesa->dri.hwContext) {
181 sarea->ctx_owner = rmesa->dri.hwContext;
182 if (!rmesa->radeonScreen->kernel_mm)
183 radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
184 }
185 }
186
187 static void r600_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
188 {
189 /* please flush pipe do all pending work */
190 /* to be enabled */
191 }
192
193 static void r600_vtbl_pre_emit_atoms(radeonContextPtr radeon)
194 {
195 r700Start3D((context_t *)radeon);
196 }
197
198 static void r600_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
199 {
200 context_t *context = R700_CONTEXT(ctx);
201 if (mode)
202 context->radeon.Fallback |= bit;
203 else
204 context->radeon.Fallback &= ~bit;
205 }
206
207 static void r600_init_vtbl(radeonContextPtr radeon)
208 {
209 radeon->vtbl.get_lock = r600_get_lock;
210 radeon->vtbl.update_viewport_offset = r700UpdateViewportOffset;
211 radeon->vtbl.emit_cs_header = r600_vtbl_emit_cs_header;
212 radeon->vtbl.swtcl_flush = NULL;
213 radeon->vtbl.pre_emit_atoms = r600_vtbl_pre_emit_atoms;
214 radeon->vtbl.fallback = r600_fallback;
215 }
216
217 static void r600InitConstValues(GLcontext *ctx, radeonScreenPtr screen)
218 {
219 context_t *r600 = R700_CONTEXT(ctx);
220
221 ctx->Const.MaxTextureImageUnits =
222 driQueryOptioni(&r600->radeon.optionCache, "texture_image_units");
223 ctx->Const.MaxTextureCoordUnits =
224 driQueryOptioni(&r600->radeon.optionCache, "texture_coord_units");
225 ctx->Const.MaxTextureUnits =
226 MIN2(ctx->Const.MaxTextureImageUnits,
227 ctx->Const.MaxTextureCoordUnits);
228 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
229 ctx->Const.MaxTextureLodBias = 16.0;
230
231 ctx->Const.MaxTextureLevels = 13; /* hw support 14 */
232 ctx->Const.MaxTextureRectSize = 4096; /* hw support 8192 */
233
234 ctx->Const.MinPointSize = 0x0001 / 8.0;
235 ctx->Const.MinPointSizeAA = 0x0001 / 8.0;
236 ctx->Const.MaxPointSize = 0xffff / 8.0;
237 ctx->Const.MaxPointSizeAA = 0xffff / 8.0;
238
239 ctx->Const.MinLineWidth = 0x0001 / 8.0;
240 ctx->Const.MinLineWidthAA = 0x0001 / 8.0;
241 ctx->Const.MaxLineWidth = 0xffff / 8.0;
242 ctx->Const.MaxLineWidthAA = 0xffff / 8.0;
243
244 ctx->Const.MaxDrawBuffers = 1; /* hw supports 8 */
245
246 /* 256 for reg-based consts, inline consts also supported */
247 ctx->Const.VertexProgram.MaxInstructions = 8192; /* in theory no limit */
248 ctx->Const.VertexProgram.MaxNativeInstructions = 8192;
249 ctx->Const.VertexProgram.MaxNativeAttribs = 160;
250 ctx->Const.VertexProgram.MaxTemps = 128;
251 ctx->Const.VertexProgram.MaxNativeTemps = 128;
252 ctx->Const.VertexProgram.MaxNativeParameters = 256;
253 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1; /* ??? */
254
255 ctx->Const.FragmentProgram.MaxNativeTemps = 128;
256 ctx->Const.FragmentProgram.MaxNativeAttribs = 32;
257 ctx->Const.FragmentProgram.MaxNativeParameters = 256;
258 ctx->Const.FragmentProgram.MaxNativeAluInstructions = 8192;
259 /* 8 per clause on r6xx, 16 on rv670/r7xx */
260 if ((screen->chip_family == CHIP_FAMILY_RV670) ||
261 (screen->chip_family >= CHIP_FAMILY_RV770))
262 ctx->Const.FragmentProgram.MaxNativeTexInstructions = 16;
263 else
264 ctx->Const.FragmentProgram.MaxNativeTexInstructions = 8;
265 ctx->Const.FragmentProgram.MaxNativeInstructions = 8192;
266 ctx->Const.FragmentProgram.MaxNativeTexIndirections = 8; /* ??? */
267 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
268 }
269
270 static void r600ParseOptions(context_t *r600, radeonScreenPtr screen)
271 {
272 /* Parse configuration files.
273 * Do this here so that initialMaxAnisotropy is set before we create
274 * the default textures.
275 */
276 driParseConfigFiles(&r600->radeon.optionCache, &screen->optionCache,
277 screen->driScreen->myNum, "r600");
278
279 r600->radeon.initialMaxAnisotropy = driQueryOptionf(&r600->radeon.optionCache,
280 "def_max_anisotropy");
281
282 }
283
284 static void r600InitGLExtensions(GLcontext *ctx)
285 {
286 context_t *r600 = R700_CONTEXT(ctx);
287
288 driInitExtensions(ctx, card_extensions, GL_TRUE);
289 if (r600->radeon.radeonScreen->kernel_mm)
290 driInitExtensions(ctx, mm_extensions, GL_FALSE);
291
292 if (driQueryOptionb
293 (&r600->radeon.optionCache, "disable_stencil_two_side"))
294 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
295
296 if (r600->radeon.glCtx->Mesa_DXTn
297 && !driQueryOptionb(&r600->radeon.optionCache, "disable_s3tc")) {
298 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
299 _mesa_enable_extension(ctx, "GL_S3_s3tc");
300 } else
301 if (driQueryOptionb(&r600->radeon.optionCache, "force_s3tc_enable"))
302 {
303 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
304 }
305 }
306
307 /* Create the device specific rendering context.
308 */
309 GLboolean r600CreateContext(const __GLcontextModes * glVisual,
310 __DRIcontextPrivate * driContextPriv,
311 void *sharedContextPrivate)
312 {
313 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
314 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
315 struct dd_function_table functions;
316 context_t *r600;
317 GLcontext *ctx;
318
319 assert(glVisual);
320 assert(driContextPriv);
321 assert(screen);
322
323 /* Allocate the R600 context */
324 r600 = (context_t*) CALLOC(sizeof(*r600));
325 if (!r600) {
326 radeon_error("Failed to allocate memory for context.\n");
327 return GL_FALSE;
328 }
329
330 r600ParseOptions(r600, screen);
331
332 r600->radeon.radeonScreen = screen;
333 r600_init_vtbl(&r600->radeon);
334
335 /* Init default driver functions then plug in our R600-specific functions
336 * (the texture functions are especially important)
337 */
338 _mesa_init_driver_functions(&functions);
339
340 r700InitStateFuncs(&functions);
341 r600InitTextureFuncs(&functions);
342 r700InitShaderFuncs(&functions);
343 r700InitIoctlFuncs(&functions);
344 radeonInitBufferObjectFuncs(&functions);
345
346 if (!radeonInitContext(&r600->radeon, &functions,
347 glVisual, driContextPriv,
348 sharedContextPrivate)) {
349 radeon_error("Initializing context failed.\n");
350 FREE(r600);
351 return GL_FALSE;
352 }
353
354 ctx = r600->radeon.glCtx;
355
356 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
357 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
358
359 r600InitConstValues(ctx, screen);
360
361 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
362
363 /* Initialize the software rasterizer and helper modules.
364 */
365 _swrast_CreateContext(ctx);
366 _vbo_CreateContext(ctx);
367 _tnl_CreateContext(ctx);
368 _swsetup_CreateContext(ctx);
369 _swsetup_Wakeup(ctx);
370
371 /* Install the customized pipeline:
372 */
373 _tnl_destroy_pipeline(ctx);
374 _tnl_install_pipeline(ctx, r600_pipeline);
375 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
376
377 /* Configure swrast and TNL to match hardware characteristics:
378 */
379 _swrast_allow_pixel_fog(ctx, GL_FALSE);
380 _swrast_allow_vertex_fog(ctx, GL_TRUE);
381 _tnl_allow_pixel_fog(ctx, GL_FALSE);
382 _tnl_allow_vertex_fog(ctx, GL_TRUE);
383
384 radeon_init_debug();
385
386 r700InitDraw(ctx);
387
388 radeon_fbo_init(&r600->radeon);
389 radeonInitSpanFuncs( ctx );
390 r600InitCmdBuf(r600);
391 r700InitState(r600->radeon.glCtx);
392
393 r600InitGLExtensions(ctx);
394
395 return GL_TRUE;
396 }
397
398