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