Don't print debug messages unless the user explicitly requested them
[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 * Authors:
32 * Keith Whitwell <keith@tungstengraphics.com>
33 * Nicolai Haehnle <prefect_@gmx.net>
34 */
35 #include "glheader.h"
36 #include "api_arrayelt.h"
37 #include "context.h"
38 #include "simple_list.h"
39 #include "imports.h"
40 #include "matrix.h"
41 #include "extensions.h"
42 #include "state.h"
43
44 #include "swrast/swrast.h"
45 #include "swrast_setup/swrast_setup.h"
46 #include "array_cache/acache.h"
47
48 #include "tnl/tnl.h"
49 #include "tnl/t_pipeline.h"
50
51 #include "drivers/common/driverfuncs.h"
52
53 #include "radeon_ioctl.h"
54 #include "radeon_span.h"
55 #include "r300_context.h"
56 #include "r300_cmdbuf.h"
57 #include "r300_state.h"
58 #include "r300_ioctl.h"
59 #include "r300_tex.h"
60
61 #include "vblank.h"
62 #include "utils.h"
63 #include "xmlpool.h" /* for symbolic values of enum-type options */
64
65 /* hw_tcl_on derives from future_hw_tcl_on when its safe to change it. */
66 int future_hw_tcl_on=0;
67 int hw_tcl_on=0;
68
69 /* Extension strings exported by the R300 driver.
70 */
71 static const char *const card_extensions[] = {
72 "GL_ARB_multisample",
73 "GL_ARB_multitexture",
74 "GL_ARB_texture_border_clamp",
75 "GL_ARB_texture_compression",
76 "GL_ARB_texture_cube_map",
77 "GL_ARB_texture_env_add",
78 "GL_ARB_texture_env_combine",
79 "GL_ARB_texture_env_dot3",
80 "GL_ARB_texture_mirrored_repeat",
81 "GL_ARB_vertex_buffer_object",
82 "GL_ARB_vertex_program",
83 //"GL_ARB_fragment_program",
84 "GL_EXT_blend_equation_separate",
85 "GL_EXT_blend_func_separate",
86 "GL_EXT_blend_minmax",
87 "GL_EXT_blend_subtract",
88 "GL_EXT_secondary_color",
89 "GL_EXT_stencil_wrap",
90 "GL_EXT_texture_edge_clamp",
91 "GL_EXT_texture_env_combine",
92 "GL_EXT_texture_env_dot3",
93 "GL_EXT_texture_filter_anisotropic",
94 "GL_EXT_texture_lod_bias",
95 "GL_EXT_texture_mirror_clamp",
96 "GL_EXT_texture_rectangle",
97 "GL_ATI_texture_env_combine3",
98 "GL_ATI_texture_mirror_once",
99 "GL_MESA_pack_invert",
100 "GL_MESA_ycbcr_texture",
101 "GL_NV_blend_square",
102 "GL_NV_vertex_program",
103 "GL_SGIS_generate_mipmap",
104 NULL
105 };
106
107 extern struct tnl_pipeline_stage _r300_render_stage;
108 extern struct tnl_pipeline_stage _r300_tcl_stage;
109
110 static const struct tnl_pipeline_stage *r300_pipeline[] = {
111
112 /* Try and go straight to t&l
113 */
114 &_r300_tcl_stage,
115
116 /* Catch any t&l fallbacks
117 */
118 &_tnl_vertex_transform_stage,
119 &_tnl_normal_transform_stage,
120 &_tnl_lighting_stage,
121 &_tnl_fog_coordinate_stage,
122 &_tnl_texgen_stage,
123 &_tnl_texture_transform_stage,
124 &_tnl_vertex_program_stage,
125
126 /* Try again to go to tcl?
127 * - no good for asymmetric-twoside (do with multipass)
128 * - no good for asymmetric-unfilled (do with multipass)
129 * - good for material
130 * - good for texgen
131 * - need to manipulate a bit of state
132 *
133 * - worth it/not worth it?
134 */
135
136 /* Else do them here.
137 */
138 &_r300_render_stage,
139 &_tnl_render_stage, /* FALLBACK */
140 0,
141 };
142
143 void r300BufferData(GLcontext *ctx, GLenum target, GLsizeiptrARB size,
144 const GLvoid *data, GLenum usage, struct gl_buffer_object *obj)
145 {
146 r300ContextPtr rmesa = R300_CONTEXT(ctx);
147 drm_radeon_mem_alloc_t alloc;
148 int offset, ret;
149
150 alloc.region = RADEON_MEM_REGION_GART;
151 alloc.alignment = 4;
152 alloc.size = size;
153 alloc.region_offset = &offset;
154
155 ret = drmCommandWriteRead( rmesa->radeon.dri.fd, DRM_RADEON_ALLOC, &alloc, sizeof(alloc));
156 if(ret){
157 WARN_ONCE("Ran out of GART memory!\n");
158 _mesa_buffer_data(ctx, target, size, data, usage, obj);
159 return ;
160 }
161 obj->Data = ((char *)rmesa->radeon.radeonScreen->gartTextures.map) + offset;
162 memcpy(obj->Data, data, size);
163 obj->Size = size;
164 obj->Usage = usage;
165 #if 0
166 fprintf(stderr, "allocated %d bytes at %p, offset=%d\n", size, obj->Data, offset);
167 #endif
168 }
169
170 void r300DeleteBuffer(GLcontext *ctx, struct gl_buffer_object *obj)
171 {
172 r300ContextPtr rmesa = R300_CONTEXT(ctx);
173
174 if(r300IsGartMemory(rmesa, obj->Data, obj->Size)){
175 drm_radeon_mem_free_t memfree;
176 int ret;
177
178 memfree.region = RADEON_MEM_REGION_GART;
179 memfree.region_offset = (char *)obj->Data - (char *)rmesa->radeon.radeonScreen->gartTextures.map;
180
181 ret = drmCommandWrite(rmesa->radeon.radeonScreen->driScreen->fd,
182 DRM_RADEON_FREE, &memfree, sizeof(memfree));
183
184 if(ret){
185 WARN_ONCE("Failed to free GART memroy!\n");
186 }
187 obj->Data = NULL;
188 }
189 _mesa_delete_buffer_object(ctx, obj);
190 }
191
192 /* Create the device specific rendering context.
193 */
194 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
195 __DRIcontextPrivate * driContextPriv,
196 void *sharedContextPrivate)
197 {
198 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
199 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
200 struct dd_function_table functions;
201 r300ContextPtr r300;
202 GLcontext *ctx;
203 int tcl_mode, i;
204
205 assert(glVisual);
206 assert(driContextPriv);
207 assert(screen);
208
209 /* Allocate the R300 context */
210 r300 = (r300ContextPtr)CALLOC(sizeof(*r300));
211 if (!r300)
212 return GL_FALSE;
213
214 /* Parse configuration files.
215 * Do this here so that initialMaxAnisotropy is set before we create
216 * the default textures.
217 */
218 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
219 screen->driScreen->myNum, "r300");
220
221 /* Init default driver functions then plug in our R300-specific functions
222 * (the texture functions are especially important)
223 */
224 _mesa_init_driver_functions(&functions);
225 r300InitIoctlFuncs(&functions);
226 r300InitStateFuncs(&functions);
227 r300InitTextureFuncs(&functions);
228 r300InitShaderFuncs(&functions);
229 if(hw_tcl_on){
230 functions.BufferData = r300BufferData;
231 functions.DeleteBuffer = r300DeleteBuffer;
232 }
233
234 if (!radeonInitContext(&r300->radeon, &functions,
235 glVisual, driContextPriv, sharedContextPrivate)) {
236 FREE(r300);
237 return GL_FALSE;
238 }
239
240 /* Init r300 context data */
241 r300->dma.buf0_address = r300->radeon.radeonScreen->buffers->list[0].address;
242
243 (void)memset(r300->texture_heaps, 0, sizeof(r300->texture_heaps));
244 make_empty_list(&r300->swapped);
245
246 r300->nr_heaps = 1 /* screen->numTexHeaps */ ;
247 assert(r300->nr_heaps < R200_NR_TEX_HEAPS);
248 for (i = 0; i < r300->nr_heaps; i++) {
249 r300->texture_heaps[i] = driCreateTextureHeap(i, r300,
250 screen->
251 texSize[i], 12,
252 RADEON_NR_TEX_REGIONS,
253 (drmTextureRegionPtr)
254 r300->radeon.sarea->
255 tex_list[i],
256 &r300->radeon.sarea->
257 tex_age[i],
258 &r300->swapped,
259 sizeof
260 (r300TexObj),
261 (destroy_texture_object_t
262 *)
263 r300DestroyTexObj);
264 }
265 r300->texture_depth = driQueryOptioni(&r300->radeon.optionCache,
266 "texture_depth");
267 if (r300->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
268 r300->texture_depth = (screen->cpp == 4) ?
269 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
270
271 /* Set the maximum texture size small enough that we can guarentee that
272 * all texture units can bind a maximal texture and have them both in
273 * texturable memory at once.
274 */
275
276 ctx = r300->radeon.glCtx;
277
278 ctx->Const.MaxTextureImageUnits = driQueryOptioni(&r300->radeon.optionCache,
279 "texture_image_units");
280 ctx->Const.MaxTextureCoordUnits = driQueryOptioni(&r300->radeon.optionCache,
281 "texture_coord_units");
282 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureImageUnits,
283 ctx->Const.MaxTextureCoordUnits);
284 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
285
286 ctx->Const.MinPointSize = 1.0;
287 ctx->Const.MinPointSizeAA = 1.0;
288 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
289 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
290
291 ctx->Const.MinLineWidth = 1.0;
292 ctx->Const.MinLineWidthAA = 1.0;
293 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
294 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
295
296 /* Initialize the software rasterizer and helper modules.
297 */
298 _swrast_CreateContext(ctx);
299 _ac_CreateContext(ctx);
300 _tnl_CreateContext(ctx);
301 _swsetup_CreateContext(ctx);
302 _swsetup_Wakeup(ctx);
303 _ae_create_context(ctx);
304
305 /* Install the customized pipeline:
306 */
307 _tnl_destroy_pipeline(ctx);
308 _tnl_install_pipeline(ctx, r300_pipeline);
309
310 /* Try and keep materials and vertices separate:
311 */
312 _tnl_isolate_materials(ctx, GL_TRUE);
313
314 /* Configure swrast and TNL to match hardware characteristics:
315 */
316 _swrast_allow_pixel_fog(ctx, GL_FALSE);
317 _swrast_allow_vertex_fog(ctx, GL_TRUE);
318 _tnl_allow_pixel_fog(ctx, GL_FALSE);
319 _tnl_allow_vertex_fog(ctx, GL_TRUE);
320
321 #if 0
322 //if(driQueryOptionb(&rmesa->optionCache, "arb_vertex_program"))
323 _mesa_enable_extension( ctx, "GL_ARB_vertex_program");
324 //if(driQueryOptionb(&rmesa->optionCache, "nv_vertex_program"))
325 _mesa_enable_extension( ctx, "GL_NV_vertex_program");
326 #endif
327 /* currently bogus data */
328 ctx->Const.MaxVertexProgramInstructions=VSF_MAX_FRAGMENT_LENGTH;
329 ctx->Const.MaxVertexProgramAttribs=16; // r420
330 ctx->Const.MaxVertexProgramTemps=VSF_MAX_FRAGMENT_TEMPS;
331 ctx->Const.MaxVertexProgramLocalParams=256; // r420
332 ctx->Const.MaxVertexProgramEnvParams=256; // r420
333 ctx->Const.MaxVertexProgramAddressRegs=1;
334
335 driInitExtensions(ctx, card_extensions, GL_TRUE);
336
337 radeonInitSpanFuncs(ctx);
338 r300InitCmdBuf(r300);
339 r300InitState(r300);
340
341 #if 0
342 /* plug in a few more device driver functions */
343 /* XXX these should really go right after _mesa_init_driver_functions() */
344 r300InitPixelFuncs(ctx);
345 r300InitSwtcl(ctx);
346 #endif
347 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
348
349 tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
350 if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
351 fprintf(stderr, "disabling 3D acceleration\n");
352 #if R200_MERGED
353 FALLBACK(&r300->radeon, RADEON_FALLBACK_DISABLE, 1);
354 #endif
355 }
356 if (tcl_mode == DRI_CONF_TCL_SW ||
357 !(r300->radeon.radeonScreen->chipset & RADEON_CHIPSET_TCL)) {
358 if (r300->radeon.radeonScreen->chipset & RADEON_CHIPSET_TCL) {
359 r300->radeon.radeonScreen->chipset &= ~RADEON_CHIPSET_TCL;
360 fprintf(stderr, "Disabling HW TCL support\n");
361 }
362 TCL_FALLBACK(r300->radeon.glCtx, RADEON_TCL_FALLBACK_TCL_DISABLE, 1);
363 }
364
365 return GL_TRUE;
366 }
367
368 /* Destroy the device specific context.
369 */
370 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
371 {
372 GET_CURRENT_CONTEXT(ctx);
373 r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
374 radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
375
376 if (RADEON_DEBUG & DEBUG_DRI) {
377 fprintf(stderr, "Destroying context !\n");
378 }
379
380 /* check if we're deleting the currently bound context */
381 if (&r300->radeon == current) {
382 radeonFlush(r300->radeon.glCtx);
383 _mesa_make_current(NULL, NULL, NULL);
384 }
385
386 /* Free r300 context resources */
387 assert(r300); /* should never be null */
388
389 if (r300) {
390 GLboolean release_texture_heaps;
391
392 release_texture_heaps = (r300->radeon.glCtx->Shared->RefCount == 1);
393 _swsetup_DestroyContext(r300->radeon.glCtx);
394 _tnl_DestroyContext(r300->radeon.glCtx);
395 _ac_DestroyContext(r300->radeon.glCtx);
396 _swrast_DestroyContext(r300->radeon.glCtx);
397
398 r300DestroyCmdBuf(r300);
399
400 radeonCleanupContext(&r300->radeon);
401
402 /* free the option cache */
403 driDestroyOptionCache(&r300->radeon.optionCache);
404
405 FREE(r300);
406 }
407 }