r300: add occlusion queries support
[platform/upstream/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_swtcl.h"
68 #include "radeon_bocs_wrapper.h"
69 #include "radeon_buffer_objects.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_occlusion_query
78 #define need_GL_ARB_point_parameters
79 #define need_GL_ARB_vertex_program
80 #define need_GL_EXT_blend_equation_separate
81 #define need_GL_EXT_blend_func_separate
82 #define need_GL_EXT_blend_minmax
83 #define need_GL_EXT_framebuffer_object
84 #define need_GL_EXT_fog_coord
85 #define need_GL_EXT_gpu_program_parameters
86 #define need_GL_EXT_secondary_color
87 #define need_GL_EXT_stencil_two_side
88 #define need_GL_ATI_separate_stencil
89 #define need_GL_NV_vertex_program
90
91 #include "extension_helper.h"
92
93
94 const struct dri_extension card_extensions[] = {
95   /* *INDENT-OFF* */
96   {"GL_ARB_depth_texture",              NULL},
97   {"GL_ARB_fragment_program",           NULL},
98   {"GL_ARB_multitexture",               NULL},
99   {"GL_ARB_point_parameters",           GL_ARB_point_parameters_functions},
100   {"GL_ARB_shadow",                     NULL},
101   {"GL_ARB_shadow_ambient",             NULL},
102   {"GL_ARB_texture_border_clamp",       NULL},
103   {"GL_ARB_texture_cube_map",           NULL},
104   {"GL_ARB_texture_env_add",            NULL},
105   {"GL_ARB_texture_env_combine",        NULL},
106   {"GL_ARB_texture_env_crossbar",       NULL},
107   {"GL_ARB_texture_env_dot3",           NULL},
108   {"GL_ARB_texture_mirrored_repeat",    NULL},
109   {"GL_ARB_vertex_program",             GL_ARB_vertex_program_functions},
110   {"GL_EXT_blend_equation_separate",    GL_EXT_blend_equation_separate_functions},
111   {"GL_EXT_blend_func_separate",        GL_EXT_blend_func_separate_functions},
112   {"GL_EXT_blend_minmax",               GL_EXT_blend_minmax_functions},
113   {"GL_EXT_blend_subtract",             NULL},
114   {"GL_EXT_packed_depth_stencil",       NULL},
115   {"GL_EXT_fog_coord",                  GL_EXT_fog_coord_functions },
116   {"GL_EXT_gpu_program_parameters",     GL_EXT_gpu_program_parameters_functions},
117   {"GL_EXT_secondary_color",            GL_EXT_secondary_color_functions},
118   {"GL_EXT_shadow_funcs",               NULL},
119   {"GL_EXT_stencil_two_side",           GL_EXT_stencil_two_side_functions},
120   {"GL_EXT_stencil_wrap",               NULL},
121   {"GL_EXT_texture_edge_clamp",         NULL},
122   {"GL_EXT_texture_env_combine",        NULL},
123   {"GL_EXT_texture_env_dot3",           NULL},
124   {"GL_EXT_texture_filter_anisotropic", NULL},
125   {"GL_EXT_texture_lod_bias",           NULL},
126   {"GL_EXT_texture_mirror_clamp",       NULL},
127   {"GL_EXT_texture_rectangle",          NULL},
128   {"GL_EXT_texture_sRGB",               NULL},
129   {"GL_EXT_vertex_array_bgra",          NULL},
130   {"GL_ATI_separate_stencil",           GL_ATI_separate_stencil_functions},
131   {"GL_ATI_texture_env_combine3",       NULL},
132   {"GL_ATI_texture_mirror_once",        NULL},
133   {"GL_MESA_pack_invert",               NULL},
134   {"GL_MESA_ycbcr_texture",             NULL},
135   {"GL_MESAX_texture_float",            NULL},
136   {"GL_NV_blend_square",                NULL},
137   {"GL_NV_vertex_program",              GL_NV_vertex_program_functions},
138   {"GL_SGIS_generate_mipmap",           NULL},
139   {NULL,                                NULL}
140   /* *INDENT-ON* */
141 };
142
143
144 const struct dri_extension mm_extensions[] = {
145   { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
146   { NULL, NULL }
147 };
148
149 /**
150  * The GL 2.0 functions are needed to make display lists work with
151  * functions added by GL_ATI_separate_stencil.
152  */
153 const struct dri_extension gl_20_extension[] = {
154   {"GL_VERSION_2_0",                    GL_VERSION_2_0_functions },
155 };
156
157 static const struct tnl_pipeline_stage *r300_pipeline[] = {
158         /* Catch any t&l fallbacks
159          */
160         &_tnl_vertex_transform_stage,
161         &_tnl_normal_transform_stage,
162         &_tnl_lighting_stage,
163         &_tnl_fog_coordinate_stage,
164         &_tnl_texgen_stage,
165         &_tnl_texture_transform_stage,
166         &_tnl_point_attenuation_stage,
167         &_tnl_vertex_program_stage,
168         &_tnl_render_stage,
169         0,
170 };
171
172 static void r300_get_lock(radeonContextPtr rmesa)
173 {
174         drm_radeon_sarea_t *sarea = rmesa->sarea;
175
176         if (sarea->ctx_owner != rmesa->dri.hwContext) {
177                 sarea->ctx_owner = rmesa->dri.hwContext;
178                 if (!rmesa->radeonScreen->kernel_mm)
179                         radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
180         }
181 }
182
183 static void r300_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
184 {
185     /* please flush pipe do all pending work */
186     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
187                                   R300_SC_SCREENDOOR, 1));
188     radeon_cs_write_dword(cs, 0x0);
189     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
190                                   R300_SC_SCREENDOOR, 1));
191     radeon_cs_write_dword(cs, 0x00FFFFFF);
192     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
193                                   R300_SC_HYPERZ, 1));
194     radeon_cs_write_dword(cs, 0x0);
195     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
196                                   R300_US_CONFIG, 1));
197     radeon_cs_write_dword(cs, 0x0);
198     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
199                                   R300_ZB_CNTL, 1));
200     radeon_cs_write_dword(cs, 0x0);
201     radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen, R300_WAIT_3D));
202     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
203                                   R300_RB3D_DSTCACHE_CTLSTAT, 1));
204     radeon_cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
205     radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
206                                   R300_ZB_ZCACHE_CTLSTAT, 1));
207     radeon_cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
208     radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen,
209                                R300_WAIT_3D | R300_WAIT_3D_CLEAN));
210 }
211
212 static void r300_vtbl_pre_emit_atoms(radeonContextPtr radeon)
213 {
214         r300ContextPtr r300 = (r300ContextPtr)radeon;
215         BATCH_LOCALS(radeon);
216
217         r300->vap_flush_needed = GL_TRUE;
218
219         cp_wait(radeon, R300_WAIT_3D | R300_WAIT_3D_CLEAN);
220         BEGIN_BATCH_NO_AUTOSTATE(2);
221         OUT_BATCH_REGVAL(R300_TX_INVALTAGS, R300_TX_FLUSH);
222         END_BATCH();
223         end_3d(radeon);
224 }
225
226 static void r300_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
227 {
228         r300ContextPtr r300 = R300_CONTEXT(ctx);
229         if (mode)
230                 r300->radeon.Fallback |= bit;
231         else
232                 r300->radeon.Fallback &= ~bit;
233 }
234
235 static void r300_init_vtbl(radeonContextPtr radeon)
236 {
237         radeon->vtbl.get_lock = r300_get_lock;
238         radeon->vtbl.update_viewport_offset = r300UpdateViewportOffset;
239         radeon->vtbl.emit_cs_header = r300_vtbl_emit_cs_header;
240         radeon->vtbl.swtcl_flush = r300_swtcl_flush;
241         radeon->vtbl.pre_emit_atoms = r300_vtbl_pre_emit_atoms;
242         radeon->vtbl.fallback = r300_fallback;
243 }
244
245 static void r300InitConstValues(GLcontext *ctx, radeonScreenPtr screen)
246 {
247         r300ContextPtr r300 = R300_CONTEXT(ctx);
248
249         ctx->Const.MaxTextureImageUnits =
250             driQueryOptioni(&r300->radeon.optionCache, "texture_image_units");
251         ctx->Const.MaxTextureCoordUnits =
252             driQueryOptioni(&r300->radeon.optionCache, "texture_coord_units");
253         ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureImageUnits,
254                  ctx->Const.MaxTextureCoordUnits);
255
256         ctx->Const.MaxTextureMaxAnisotropy = 16.0;
257         ctx->Const.MaxTextureLodBias = 16.0;
258
259         if (screen->chip_family >= CHIP_FAMILY_RV515) {
260                 ctx->Const.MaxTextureLevels = 13;
261                 ctx->Const.MaxCubeTextureLevels = 13;
262                 ctx->Const.MaxTextureRectSize = 4096;
263         }
264         else {
265                 ctx->Const.MaxTextureLevels = 12;
266                 ctx->Const.MaxCubeTextureLevels = 12;
267                 ctx->Const.MaxTextureRectSize = 2048;
268         }
269
270         ctx->Const.MinPointSize = 1.0;
271         ctx->Const.MinPointSizeAA = 1.0;
272         ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
273         ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
274
275         ctx->Const.MinLineWidth = 1.0;
276         ctx->Const.MinLineWidthAA = 1.0;
277         ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
278         ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
279
280         ctx->Const.MaxDrawBuffers = 1;
281
282         /* currently bogus data */
283         if (r300->options.hw_tcl_enabled) {
284                 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
285                 ctx->Const.VertexProgram.MaxNativeInstructions =
286                   VSF_MAX_FRAGMENT_LENGTH / 4;
287                 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
288                 ctx->Const.VertexProgram.MaxTemps = 32;
289                 ctx->Const.VertexProgram.MaxNativeTemps =
290                   /*VSF_MAX_FRAGMENT_TEMPS */ 32;
291                 ctx->Const.VertexProgram.MaxNativeParameters = 256;     /* r420 */
292                 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
293         }
294
295         if (screen->chip_family >= CHIP_FAMILY_RV515) {
296                 ctx->Const.FragmentProgram.MaxNativeTemps = R500_PFS_NUM_TEMP_REGS;
297                 ctx->Const.FragmentProgram.MaxNativeAttribs = 11;       /* copy i915... */
298                 ctx->Const.FragmentProgram.MaxNativeParameters = R500_PFS_NUM_CONST_REGS;
299                 ctx->Const.FragmentProgram.MaxNativeAluInstructions = R500_PFS_MAX_INST;
300                 ctx->Const.FragmentProgram.MaxNativeTexInstructions = R500_PFS_MAX_INST;
301                 ctx->Const.FragmentProgram.MaxNativeInstructions = R500_PFS_MAX_INST;
302                 ctx->Const.FragmentProgram.MaxNativeTexIndirections = R500_PFS_MAX_INST;
303                 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;
304         } else {
305                 ctx->Const.FragmentProgram.MaxNativeTemps = R300_PFS_NUM_TEMP_REGS;
306                 ctx->Const.FragmentProgram.MaxNativeAttribs = 11;       /* copy i915... */
307                 ctx->Const.FragmentProgram.MaxNativeParameters = R300_PFS_NUM_CONST_REGS;
308                 ctx->Const.FragmentProgram.MaxNativeAluInstructions = R300_PFS_MAX_ALU_INST;
309                 ctx->Const.FragmentProgram.MaxNativeTexInstructions = R300_PFS_MAX_TEX_INST;
310                 ctx->Const.FragmentProgram.MaxNativeInstructions = R300_PFS_MAX_ALU_INST + R300_PFS_MAX_TEX_INST;
311                 ctx->Const.FragmentProgram.MaxNativeTexIndirections = R300_PFS_MAX_TEX_INDIRECT;
312                 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0;
313         }
314
315         if (r300->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV530)
316                 r300->num_z_pipes = 2;
317         else
318                 r300->num_z_pipes = r300->radeon.radeonScreen->num_gb_pipes;
319 }
320
321 static void r300ParseOptions(r300ContextPtr r300, radeonScreenPtr screen)
322 {
323         struct r300_options options = { 0 };
324
325         driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
326                             screen->driScreen->myNum, "r300");
327
328         r300->radeon.initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache, "def_max_anisotropy");
329
330         options.stencil_two_side_disabled = driQueryOptionb(&r300->radeon.optionCache, "disable_stencil_two_side");
331         options.s3tc_force_enabled = driQueryOptionb(&r300->radeon.optionCache, "force_s3tc_enable");
332         options.s3tc_force_disabled = driQueryOptionb(&r300->radeon.optionCache, "disable_s3tc");
333
334         if (!(screen->chip_flags & RADEON_CHIPSET_TCL) || driQueryOptioni(&r300->radeon.optionCache, "tcl_mode") == DRI_CONF_TCL_SW)
335                 options.hw_tcl_enabled = 0;
336         else
337                 options.hw_tcl_enabled = 1;
338
339         options.conformance_mode = !driQueryOptionb(&r300->radeon.optionCache, "disable_lowimpact_fallback");
340
341         r300->options = options;
342 }
343
344 static void r300InitGLExtensions(GLcontext *ctx)
345 {
346         r300ContextPtr r300 = R300_CONTEXT(ctx);
347
348         driInitExtensions(ctx, card_extensions, GL_TRUE);
349         if (r300->radeon.radeonScreen->kernel_mm)
350                 driInitExtensions(ctx, mm_extensions, GL_FALSE);
351
352         if (r300->options.stencil_two_side_disabled)
353                 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
354
355         if (ctx->Mesa_DXTn && !r300->options.s3tc_force_enabled) {
356                 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
357                 _mesa_enable_extension(ctx, "GL_S3_s3tc");
358         } else if (r300->options.s3tc_force_disabled) {
359                 _mesa_disable_extension(ctx, "GL_EXT_texture_compression_s3tc");
360         }
361 }
362
363 /* Create the device specific rendering context.
364  */
365 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
366                             __DRIcontextPrivate * driContextPriv,
367                             void *sharedContextPrivate)
368 {
369         __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
370         radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
371         struct dd_function_table functions;
372         r300ContextPtr r300;
373         GLcontext *ctx;
374
375         assert(glVisual);
376         assert(driContextPriv);
377         assert(screen);
378
379         r300 = (r300ContextPtr) CALLOC(sizeof(*r300));
380         if (!r300)
381                 return GL_FALSE;
382
383         r300ParseOptions(r300, screen);
384
385         r300_init_vtbl(&r300->radeon);
386
387         _mesa_init_driver_functions(&functions);
388         r300InitIoctlFuncs(&functions);
389         r300InitStateFuncs(&functions);
390         r300InitTextureFuncs(&functions);
391         r300InitShaderFuncs(&functions);
392         radeonInitBufferObjectFuncs(&functions);
393
394         if (!radeonInitContext(&r300->radeon, &functions,
395                                glVisual, driContextPriv,
396                                sharedContextPrivate)) {
397                 FREE(r300);
398                 return GL_FALSE;
399         }
400
401         ctx = r300->radeon.glCtx;
402
403         r300->fallback = 0;
404         if (r300->options.hw_tcl_enabled)
405                 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
406
407         ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
408
409         r300InitConstValues(ctx, screen);
410
411         _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
412
413         /* Initialize the software rasterizer and helper modules.
414          */
415         _swrast_CreateContext(ctx);
416         _vbo_CreateContext(ctx);
417         _tnl_CreateContext(ctx);
418         _swsetup_CreateContext(ctx);
419         _swsetup_Wakeup(ctx);
420
421         /* Install the customized pipeline:
422          */
423         _tnl_destroy_pipeline(ctx);
424         _tnl_install_pipeline(ctx, r300_pipeline);
425         TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
426
427         /* Configure swrast and TNL to match hardware characteristics:
428          */
429         _swrast_allow_pixel_fog(ctx, GL_FALSE);
430         _swrast_allow_vertex_fog(ctx, GL_TRUE);
431         _tnl_allow_pixel_fog(ctx, GL_FALSE);
432         _tnl_allow_vertex_fog(ctx, GL_TRUE);
433
434         if (r300->options.hw_tcl_enabled) {
435                 r300InitDraw(ctx);
436         } else {
437                 r300InitSwtcl(ctx);
438         }
439
440         radeon_fbo_init(&r300->radeon);
441         radeonInitSpanFuncs( ctx );
442         r300InitCmdBuf(r300);
443         r300InitState(r300);
444         r300InitShaderFunctions(r300);
445
446         r300InitGLExtensions(ctx);
447
448         make_empty_list(&r300->query.not_flushed_head);
449
450         return GL_TRUE;
451 }
452