Merge commit 'origin/master' into glsl-pp-rework-2
[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_span.h"
63 #include "r600_cmdbuf.h"
64 #include "r600_emit.h"
65 #include "radeon_bocs_wrapper.h"
66
67 #include "r700_state.h"
68 #include "r700_ioctl.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_framebuffer_object
86 #define need_GL_EXT_fog_coord
87 #define need_GL_EXT_gpu_program_parameters
88 #define need_GL_EXT_provoking_vertex
89 #define need_GL_EXT_secondary_color
90 #define need_GL_EXT_stencil_two_side
91 #define need_GL_ATI_separate_stencil
92 #define need_GL_NV_vertex_program
93
94 #include "extension_helper.h"
95
96 extern const struct tnl_pipeline_stage *r700_pipeline[];
97
98 const struct dri_extension card_extensions[] = {
99 /* *INDENT-OFF* */
100 {"GL_ARB_depth_texture", NULL},
101 {"GL_ARB_fragment_program", NULL},
102 {"GL_ARB_multitexture", NULL},
103 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
104 {"GL_ARB_shadow", NULL},
105 {"GL_ARB_shadow_ambient", NULL},
106 {"GL_ARB_texture_border_clamp", NULL},
107 {"GL_ARB_texture_cube_map", NULL},
108 {"GL_ARB_texture_env_add", NULL},
109 {"GL_ARB_texture_env_combine", NULL},
110 {"GL_ARB_texture_env_crossbar", NULL},
111 {"GL_ARB_texture_env_dot3", NULL},
112 {"GL_ARB_texture_mirrored_repeat", NULL},
113 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
114 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
115 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
116 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
117 {"GL_EXT_blend_subtract", NULL},
118 {"GL_EXT_packed_depth_stencil", NULL},
119 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
120 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions},
121 {"GL_EXT_provoking_vertex", GL_EXT_provoking_vertex_functions },
122 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
123 {"GL_EXT_shadow_funcs", NULL},
124 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions},
125 {"GL_EXT_stencil_wrap", NULL},
126 {"GL_EXT_texture_edge_clamp", NULL},
127 {"GL_EXT_texture_env_combine", NULL},
128 {"GL_EXT_texture_env_dot3", NULL},
129 {"GL_EXT_texture_filter_anisotropic", NULL},
130 {"GL_EXT_texture_lod_bias", NULL},
131 {"GL_EXT_texture_mirror_clamp", NULL},
132 {"GL_EXT_texture_rectangle", 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 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 const struct dri_extension gl_20_extension[] = {
158 {"GL_VERSION_2_0", GL_VERSION_2_0_functions },
159 };
160
161
162 static void r600RunPipeline(GLcontext * ctx)
163 {
164 _mesa_lock_context_textures(ctx);
165
166 if (ctx->NewState)
167 _mesa_update_state_locked(ctx);
168
169 _tnl_run_pipeline(ctx);
170 _mesa_unlock_context_textures(ctx);
171 }
172
173 static void r600_get_lock(radeonContextPtr rmesa)
174 {
175 drm_radeon_sarea_t *sarea = rmesa->sarea;
176
177 if (sarea->ctx_owner != rmesa->dri.hwContext) {
178 sarea->ctx_owner = rmesa->dri.hwContext;
179 if (!rmesa->radeonScreen->kernel_mm)
180 radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
181 }
182 }
183
184 static void r600_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
185 {
186 /* please flush pipe do all pending work */
187 /* to be enabled */
188 }
189
190 static void r600_vtbl_pre_emit_atoms(radeonContextPtr radeon)
191 {
192 r700Start3D((context_t *)radeon);
193 }
194
195 static void r600_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
196 {
197 context_t *context = R700_CONTEXT(ctx);
198 if (mode)
199 context->radeon.Fallback |= bit;
200 else
201 context->radeon.Fallback &= ~bit;
202 }
203
204 static void r600_init_vtbl(radeonContextPtr radeon)
205 {
206 radeon->vtbl.get_lock = r600_get_lock;
207 radeon->vtbl.update_viewport_offset = r700UpdateViewportOffset;
208 radeon->vtbl.emit_cs_header = r600_vtbl_emit_cs_header;
209 radeon->vtbl.swtcl_flush = NULL;
210 radeon->vtbl.pre_emit_atoms = r600_vtbl_pre_emit_atoms;
211 radeon->vtbl.fallback = r600_fallback;
212 }
213
214 /* Create the device specific rendering context.
215 */
216 GLboolean r600CreateContext(const __GLcontextModes * glVisual,
217 __DRIcontextPrivate * driContextPriv,
218 void *sharedContextPrivate)
219 {
220 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
221 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
222 struct dd_function_table functions;
223 context_t *r600;
224 GLcontext *ctx;
225
226 assert(glVisual);
227 assert(driContextPriv);
228 assert(screen);
229
230 /* Allocate the R600 context */
231 r600 = (context_t*) CALLOC(sizeof(*r600));
232 if (!r600) {
233 radeon_error("Failed to allocate memory for context.\n");
234 return GL_FALSE;
235 }
236
237 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
238 hw_tcl_on = future_hw_tcl_on = 0;
239
240 r600_init_vtbl(&r600->radeon);
241 /* Parse configuration files.
242 * Do this here so that initialMaxAnisotropy is set before we create
243 * the default textures.
244 */
245 driParseConfigFiles(&r600->radeon.optionCache, &screen->optionCache,
246 screen->driScreen->myNum, "r600");
247
248 r600->radeon.initialMaxAnisotropy = driQueryOptionf(&r600->radeon.optionCache,
249 "def_max_anisotropy");
250
251 /* Init default driver functions then plug in our R600-specific functions
252 * (the texture functions are especially important)
253 */
254 _mesa_init_driver_functions(&functions);
255
256 r700InitStateFuncs(&functions);
257 r600InitTextureFuncs(&functions);
258 r700InitShaderFuncs(&functions);
259 r700InitIoctlFuncs(&functions);
260
261 if (!radeonInitContext(&r600->radeon, &functions,
262 glVisual, driContextPriv,
263 sharedContextPrivate)) {
264 radeon_error("Initializing context failed.\n");
265 FREE(r600);
266 return GL_FALSE;
267 }
268
269 /* Init r600 context data */
270 /* Set the maximum texture size small enough that we can guarentee that
271 * all texture units can bind a maximal texture and have them both in
272 * texturable memory at once.
273 */
274
275 ctx = r600->radeon.glCtx;
276
277 ctx->Const.MaxTextureImageUnits =
278 driQueryOptioni(&r600->radeon.optionCache, "texture_image_units");
279 ctx->Const.MaxTextureCoordUnits =
280 driQueryOptioni(&r600->radeon.optionCache, "texture_coord_units");
281 ctx->Const.MaxTextureUnits =
282 MIN2(ctx->Const.MaxTextureImageUnits,
283 ctx->Const.MaxTextureCoordUnits);
284 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
285 ctx->Const.MaxTextureLodBias = 16.0;
286
287 ctx->Const.MaxTextureLevels = 13;
288 ctx->Const.MaxTextureRectSize = 4096;
289
290 ctx->Const.MinPointSize = 0x0001 / 8.0;
291 ctx->Const.MinPointSizeAA = 0x0001 / 8.0;
292 ctx->Const.MaxPointSize = 0xffff / 8.0;
293 ctx->Const.MaxPointSizeAA = 0xffff / 8.0;
294
295 ctx->Const.MinLineWidth = 0x0001 / 8.0;
296 ctx->Const.MinLineWidthAA = 0x0001 / 8.0;
297 ctx->Const.MaxLineWidth = 0xffff / 8.0;
298 ctx->Const.MaxLineWidthAA = 0xffff / 8.0;
299
300 /* Needs further modifications */
301 #if 0
302 ctx->Const.MaxArrayLockSize =
303 ( /*512 */ RADEON_BUFFER_SIZE * 16 * 1024) / (4 * 4);
304 #endif
305
306 ctx->Const.MaxDrawBuffers = 1;
307
308 /* Initialize the software rasterizer and helper modules.
309 */
310 _swrast_CreateContext(ctx);
311 _vbo_CreateContext(ctx);
312 _tnl_CreateContext(ctx);
313 _swsetup_CreateContext(ctx);
314 _swsetup_Wakeup(ctx);
315 _ae_create_context(ctx);
316
317 /* Install the customized pipeline:
318 */
319 _tnl_destroy_pipeline(ctx);
320 _tnl_install_pipeline(ctx, r700_pipeline);
321
322 /* Try and keep materials and vertices separate:
323 */
324 /* _tnl_isolate_materials(ctx, GL_TRUE); */
325
326 /* Configure swrast and TNL to match hardware characteristics:
327 */
328 _swrast_allow_pixel_fog(ctx, GL_FALSE);
329 _swrast_allow_vertex_fog(ctx, GL_TRUE);
330 _tnl_allow_pixel_fog(ctx, GL_FALSE);
331 _tnl_allow_vertex_fog(ctx, GL_TRUE);
332
333 /* currently bogus data */
334 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
335 ctx->Const.VertexProgram.MaxNativeInstructions =
336 VSF_MAX_FRAGMENT_LENGTH / 4;
337 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
338 ctx->Const.VertexProgram.MaxTemps = 32;
339 ctx->Const.VertexProgram.MaxNativeTemps =
340 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
341 ctx->Const.VertexProgram.MaxNativeParameters = 256; /* r420 */
342 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
343
344 ctx->Const.FragmentProgram.MaxNativeTemps = PFS_NUM_TEMP_REGS;
345 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
346 ctx->Const.FragmentProgram.MaxNativeParameters = PFS_NUM_CONST_REGS;
347 ctx->Const.FragmentProgram.MaxNativeAluInstructions = PFS_MAX_ALU_INST;
348 ctx->Const.FragmentProgram.MaxNativeTexInstructions = PFS_MAX_TEX_INST;
349 ctx->Const.FragmentProgram.MaxNativeInstructions =
350 PFS_MAX_ALU_INST + PFS_MAX_TEX_INST;
351 ctx->Const.FragmentProgram.MaxNativeTexIndirections =
352 PFS_MAX_TEX_INDIRECT;
353 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
354 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
355 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
356
357 radeon_init_debug();
358
359 driInitExtensions(ctx, card_extensions, GL_TRUE);
360 if (r600->radeon.radeonScreen->kernel_mm)
361 driInitExtensions(ctx, mm_extensions, GL_FALSE);
362
363 if (driQueryOptionb
364 (&r600->radeon.optionCache, "disable_stencil_two_side"))
365 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
366
367 if (r600->radeon.glCtx->Mesa_DXTn
368 && !driQueryOptionb(&r600->radeon.optionCache, "disable_s3tc")) {
369 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
370 _mesa_enable_extension(ctx, "GL_S3_s3tc");
371 } else
372 if (driQueryOptionb(&r600->radeon.optionCache, "force_s3tc_enable"))
373 {
374 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
375 }
376
377 radeon_fbo_init(&r600->radeon);
378 radeonInitSpanFuncs( ctx );
379
380 r600InitCmdBuf(r600);
381
382 r700InitState(r600->radeon.glCtx);
383
384 TNL_CONTEXT(ctx)->Driver.RunPipeline = r600RunPipeline;
385
386 if (driQueryOptionb(&r600->radeon.optionCache, "no_rast")) {
387 radeon_warning("disabling 3D acceleration\n");
388 }
389
390 return GL_TRUE;
391 }
392
393