r300: init fbos
[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
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_secondary_color
89 #define need_GL_EXT_stencil_two_side
90 #define need_GL_ATI_separate_stencil
91 #define need_GL_NV_vertex_program
92
93 #include "extension_helper.h"
94
95
96 const struct dri_extension card_extensions[] = {
97 /* *INDENT-OFF* */
98 {"GL_ARB_depth_texture", NULL},
99 {"GL_ARB_fragment_program", NULL},
100 {"GL_ARB_multitexture", NULL},
101 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions},
102 {"GL_ARB_shadow", NULL},
103 {"GL_ARB_shadow_ambient", NULL},
104 {"GL_ARB_texture_border_clamp", NULL},
105 {"GL_ARB_texture_cube_map", NULL},
106 {"GL_ARB_texture_env_add", NULL},
107 {"GL_ARB_texture_env_combine", NULL},
108 {"GL_ARB_texture_env_crossbar", NULL},
109 {"GL_ARB_texture_env_dot3", NULL},
110 {"GL_ARB_texture_mirrored_repeat", NULL},
111 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions},
112 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions},
113 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions},
114 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions},
115 {"GL_EXT_blend_subtract", NULL},
116 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions },
117 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
118 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions},
119 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions},
120 {"GL_EXT_shadow_funcs", NULL},
121 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions},
122 {"GL_EXT_stencil_wrap", NULL},
123 {"GL_EXT_texture_edge_clamp", NULL},
124 {"GL_EXT_texture_env_combine", NULL},
125 {"GL_EXT_texture_env_dot3", NULL},
126 {"GL_EXT_texture_filter_anisotropic", NULL},
127 {"GL_EXT_texture_lod_bias", NULL},
128 {"GL_EXT_texture_mirror_clamp", NULL},
129 {"GL_EXT_texture_rectangle", 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 /**
145 * The GL 2.0 functions are needed to make display lists work with
146 * functions added by GL_ATI_separate_stencil.
147 */
148 const struct dri_extension gl_20_extension[] = {
149 {"GL_VERSION_2_0", GL_VERSION_2_0_functions },
150 };
151
152
153 extern struct tnl_pipeline_stage _r300_render_stage;
154 extern const struct tnl_pipeline_stage _r300_tcl_stage;
155
156 static const struct tnl_pipeline_stage *r300_pipeline[] = {
157
158 /* Try and go straight to t&l
159 */
160 &_r300_tcl_stage,
161
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_vertex_program_stage,
171
172 /* Try again to go to tcl?
173 * - no good for asymmetric-twoside (do with multipass)
174 * - no good for asymmetric-unfilled (do with multipass)
175 * - good for material
176 * - good for texgen
177 * - need to manipulate a bit of state
178 *
179 * - worth it/not worth it?
180 */
181
182 /* Else do them here.
183 */
184 &_r300_render_stage,
185 &_tnl_render_stage, /* FALLBACK */
186 0,
187 };
188
189 static void r300RunPipeline(GLcontext * ctx)
190 {
191 _mesa_lock_context_textures(ctx);
192
193 if (ctx->NewState)
194 _mesa_update_state_locked(ctx);
195
196 _tnl_run_pipeline(ctx);
197 _mesa_unlock_context_textures(ctx);
198 }
199
200 static void r300_get_lock(radeonContextPtr rmesa)
201 {
202 drm_radeon_sarea_t *sarea = rmesa->sarea;
203
204 if (sarea->ctx_owner != rmesa->dri.hwContext) {
205 sarea->ctx_owner = rmesa->dri.hwContext;
206 if (!rmesa->radeonScreen->kernel_mm)
207 radeon_bo_legacy_texture_age(rmesa->radeonScreen->bom);
208 }
209 }
210
211 static void r300_vtbl_emit_cs_header(struct radeon_cs *cs, radeonContextPtr rmesa)
212 {
213 /* please flush pipe do all pending work */
214 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
215 R300_SC_SCREENDOOR, 1));
216 radeon_cs_write_dword(cs, 0x0);
217 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
218 R300_SC_SCREENDOOR, 1));
219 radeon_cs_write_dword(cs, 0x00FFFFFF);
220 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
221 R300_SC_HYPERZ, 1));
222 radeon_cs_write_dword(cs, 0x0);
223 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
224 R300_US_CONFIG, 1));
225 radeon_cs_write_dword(cs, 0x0);
226 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
227 R300_ZB_CNTL, 1));
228 radeon_cs_write_dword(cs, 0x0);
229 radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen, R300_WAIT_3D));
230 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
231 R300_RB3D_DSTCACHE_CTLSTAT, 1));
232 radeon_cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
233 radeon_cs_write_dword(cs, cmdpacket0(rmesa->radeonScreen,
234 R300_ZB_ZCACHE_CTLSTAT, 1));
235 radeon_cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
236 radeon_cs_write_dword(cs, cmdwait(rmesa->radeonScreen,
237 R300_WAIT_3D | R300_WAIT_3D_CLEAN));
238 }
239
240 static void r300_vtbl_pre_emit_atoms(radeonContextPtr radeon)
241 {
242 r300ContextPtr r300 = (r300ContextPtr)radeon;
243 BATCH_LOCALS(radeon);
244
245 r300->vap_flush_needed = GL_TRUE;
246
247 cp_wait(radeon, R300_WAIT_3D | R300_WAIT_3D_CLEAN);
248 BEGIN_BATCH_NO_AUTOSTATE(2);
249 OUT_BATCH_REGVAL(R300_TX_INVALTAGS, R300_TX_FLUSH);
250 END_BATCH();
251 end_3d(radeon);
252 }
253
254 static void r300_fallback(GLcontext *ctx, GLuint bit, GLboolean mode)
255 {
256 r300ContextPtr r300 = R300_CONTEXT(ctx);
257 if (mode)
258 r300->radeon.Fallback |= bit;
259 else
260 r300->radeon.Fallback &= ~bit;
261 }
262
263 static void r300_init_vtbl(radeonContextPtr radeon)
264 {
265 radeon->vtbl.get_lock = r300_get_lock;
266 radeon->vtbl.update_viewport_offset = r300UpdateViewportOffset;
267 radeon->vtbl.emit_cs_header = r300_vtbl_emit_cs_header;
268 radeon->vtbl.swtcl_flush = r300_swtcl_flush;
269 radeon->vtbl.pre_emit_atoms = r300_vtbl_pre_emit_atoms;
270 radeon->vtbl.fallback = r300_fallback;
271 }
272
273
274 /* Create the device specific rendering context.
275 */
276 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
277 __DRIcontextPrivate * driContextPriv,
278 void *sharedContextPrivate)
279 {
280 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
281 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
282 struct dd_function_table functions;
283 r300ContextPtr r300;
284 GLcontext *ctx;
285 int tcl_mode;
286
287 assert(glVisual);
288 assert(driContextPriv);
289 assert(screen);
290
291 /* Allocate the R300 context */
292 r300 = (r300ContextPtr) CALLOC(sizeof(*r300));
293 if (!r300)
294 return GL_FALSE;
295
296 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
297 hw_tcl_on = future_hw_tcl_on = 0;
298
299 r300_init_vtbl(&r300->radeon);
300 /* Parse configuration files.
301 * Do this here so that initialMaxAnisotropy is set before we create
302 * the default textures.
303 */
304 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
305 screen->driScreen->myNum, "r300");
306 r300->radeon.initialMaxAnisotropy = driQueryOptionf(&r300->radeon.optionCache,
307 "def_max_anisotropy");
308
309 /* Init default driver functions then plug in our R300-specific functions
310 * (the texture functions are especially important)
311 */
312 _mesa_init_driver_functions(&functions);
313 r300InitIoctlFuncs(&functions);
314 r300InitStateFuncs(&functions);
315 r300InitTextureFuncs(&functions);
316 r300InitShaderFuncs(&functions);
317
318 if (!radeonInitContext(&r300->radeon, &functions,
319 glVisual, driContextPriv,
320 sharedContextPrivate)) {
321 FREE(r300);
322 return GL_FALSE;
323 }
324
325 /* Init r300 context data */
326 r300->radeon.texture_depth = driQueryOptioni(&r300->radeon.optionCache,
327 "texture_depth");
328 if (r300->radeon.texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
329 r300->radeon.texture_depth = (screen->cpp == 4) ?
330 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
331
332 /* Set the maximum texture size small enough that we can guarentee that
333 * all texture units can bind a maximal texture and have them both in
334 * texturable memory at once.
335 */
336
337 ctx = r300->radeon.glCtx;
338
339 ctx->Const.MaxTextureImageUnits =
340 driQueryOptioni(&r300->radeon.optionCache, "texture_image_units");
341 ctx->Const.MaxTextureCoordUnits =
342 driQueryOptioni(&r300->radeon.optionCache, "texture_coord_units");
343 ctx->Const.MaxTextureUnits =
344 MIN2(ctx->Const.MaxTextureImageUnits,
345 ctx->Const.MaxTextureCoordUnits);
346 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
347 ctx->Const.MaxTextureLodBias = 16.0;
348
349 if (screen->chip_family >= CHIP_FAMILY_RV515) {
350 ctx->Const.MaxTextureLevels = 13;
351 ctx->Const.MaxTextureRectSize = 4096;
352 }
353
354 ctx->Const.MinPointSize = 1.0;
355 ctx->Const.MinPointSizeAA = 1.0;
356 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
357 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
358
359 ctx->Const.MinLineWidth = 1.0;
360 ctx->Const.MinLineWidthAA = 1.0;
361 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
362 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
363
364 /* Needs further modifications */
365 #if 0
366 ctx->Const.MaxArrayLockSize =
367 ( /*512 */ RADEON_BUFFER_SIZE * 16 * 1024) / (4 * 4);
368 #endif
369
370 ctx->Const.MaxDrawBuffers = 1;
371
372 /* Initialize the software rasterizer and helper modules.
373 */
374 _swrast_CreateContext(ctx);
375 _vbo_CreateContext(ctx);
376 _tnl_CreateContext(ctx);
377 _swsetup_CreateContext(ctx);
378 _swsetup_Wakeup(ctx);
379 _ae_create_context(ctx);
380
381 /* Install the customized pipeline:
382 */
383 _tnl_destroy_pipeline(ctx);
384 _tnl_install_pipeline(ctx, r300_pipeline);
385
386 /* Try and keep materials and vertices separate:
387 */
388 /* _tnl_isolate_materials(ctx, GL_TRUE); */
389
390 /* Configure swrast and TNL to match hardware characteristics:
391 */
392 _swrast_allow_pixel_fog(ctx, GL_FALSE);
393 _swrast_allow_vertex_fog(ctx, GL_TRUE);
394 _tnl_allow_pixel_fog(ctx, GL_FALSE);
395 _tnl_allow_vertex_fog(ctx, GL_TRUE);
396
397 /* currently bogus data */
398 if (screen->chip_flags & RADEON_CHIPSET_TCL) {
399 ctx->Const.VertexProgram.MaxInstructions = VSF_MAX_FRAGMENT_LENGTH / 4;
400 ctx->Const.VertexProgram.MaxNativeInstructions =
401 VSF_MAX_FRAGMENT_LENGTH / 4;
402 ctx->Const.VertexProgram.MaxNativeAttribs = 16; /* r420 */
403 ctx->Const.VertexProgram.MaxTemps = 32;
404 ctx->Const.VertexProgram.MaxNativeTemps =
405 /*VSF_MAX_FRAGMENT_TEMPS */ 32;
406 ctx->Const.VertexProgram.MaxNativeParameters = 256; /* r420 */
407 ctx->Const.VertexProgram.MaxNativeAddressRegs = 1;
408 }
409
410 ctx->Const.FragmentProgram.MaxNativeTemps = PFS_NUM_TEMP_REGS;
411 ctx->Const.FragmentProgram.MaxNativeAttribs = 11; /* copy i915... */
412 ctx->Const.FragmentProgram.MaxNativeParameters = PFS_NUM_CONST_REGS;
413 ctx->Const.FragmentProgram.MaxNativeAluInstructions = PFS_MAX_ALU_INST;
414 ctx->Const.FragmentProgram.MaxNativeTexInstructions = PFS_MAX_TEX_INST;
415 ctx->Const.FragmentProgram.MaxNativeInstructions =
416 PFS_MAX_ALU_INST + PFS_MAX_TEX_INST;
417 ctx->Const.FragmentProgram.MaxNativeTexIndirections =
418 PFS_MAX_TEX_INDIRECT;
419 ctx->Const.FragmentProgram.MaxNativeAddressRegs = 0; /* and these are?? */
420 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
421 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
422
423 driInitExtensions(ctx, card_extensions, GL_TRUE);
424
425 if (driQueryOptionb
426 (&r300->radeon.optionCache, "disable_stencil_two_side"))
427 _mesa_disable_extension(ctx, "GL_EXT_stencil_two_side");
428
429 if (r300->radeon.glCtx->Mesa_DXTn
430 && !driQueryOptionb(&r300->radeon.optionCache, "disable_s3tc")) {
431 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
432 _mesa_enable_extension(ctx, "GL_S3_s3tc");
433 } else
434 if (driQueryOptionb(&r300->radeon.optionCache, "force_s3tc_enable"))
435 {
436 _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
437 }
438
439 r300->disable_lowimpact_fallback =
440 driQueryOptionb(&r300->radeon.optionCache,
441 "disable_lowimpact_fallback");
442 radeon_fbo_init(&r300->radeon);
443 radeonInitSpanFuncs( ctx );
444 r300InitCmdBuf(r300);
445 r300InitState(r300);
446 if (!(screen->chip_flags & RADEON_CHIPSET_TCL))
447 r300InitSwtcl(ctx);
448
449 TNL_CONTEXT(ctx)->Driver.RunPipeline = r300RunPipeline;
450
451 tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
452 if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
453 fprintf(stderr, "disabling 3D acceleration\n");
454 #if R200_MERGED
455 FALLBACK(&r300->radeon, RADEON_FALLBACK_DISABLE, 1);
456 #endif
457 }
458 if (tcl_mode == DRI_CONF_TCL_SW ||
459 !(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL)) {
460 if (r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
461 r300->radeon.radeonScreen->chip_flags &=
462 ~RADEON_CHIPSET_TCL;
463 fprintf(stderr, "Disabling HW TCL support\n");
464 }
465 TCL_FALLBACK(r300->radeon.glCtx,
466 RADEON_TCL_FALLBACK_TCL_DISABLE, 1);
467 }
468
469 return GL_TRUE;
470 }
471
472 /* Destroy the device specific context.
473 */
474 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
475 {
476 GET_CURRENT_CONTEXT(ctx);
477 r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
478 radeonContextPtr radeon = (radeonContextPtr) r300;
479 radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
480
481 if (RADEON_DEBUG & DEBUG_DRI) {
482 fprintf(stderr, "Destroying context !\n");
483 }
484
485 /* check if we're deleting the currently bound context */
486 if (&r300->radeon == current) {
487 radeonFlush(r300->radeon.glCtx);
488 _mesa_make_current(NULL, NULL, NULL);
489 }
490
491 /* Free r300 context resources */
492 assert(r300); /* should never be null */
493
494 if (r300) {
495 _swsetup_DestroyContext(r300->radeon.glCtx);
496 _tnl_DestroyContext(r300->radeon.glCtx);
497 _vbo_DestroyContext(r300->radeon.glCtx);
498 _swrast_DestroyContext(r300->radeon.glCtx);
499
500 rcommonFlushCmdBuf(&r300->radeon, __FUNCTION__);
501
502 if (radeon->state.scissor.pClipRects) {
503 FREE(radeon->state.scissor.pClipRects);
504 radeon->state.scissor.pClipRects = NULL;
505 }
506
507 r300DestroyCmdBuf(r300);
508
509 radeonCleanupContext(&r300->radeon);
510
511
512 /* the memory manager might be accessed when Mesa frees the shared
513 * state, so don't destroy it earlier
514 */
515
516
517 FREE(r300);
518 }
519 }