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