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