Reorganized shader stuff.
[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 int hw_vertprog_on=1;
66
67 /* Extension strings exported by the R300 driver.
68 */
69 static const char *const card_extensions[] = {
70 "GL_ARB_multisample",
71 "GL_ARB_multitexture",
72 "GL_ARB_texture_border_clamp",
73 "GL_ARB_texture_compression",
74 "GL_ARB_texture_cube_map",
75 "GL_ARB_texture_env_add",
76 "GL_ARB_texture_env_combine",
77 "GL_ARB_texture_env_dot3",
78 "GL_ARB_texture_mirrored_repeat",
79 "GL_ARB_vertex_buffer_object",
80 "GL_ARB_vertex_program",
81 //"GL_ARB_fragment_program",
82 "GL_EXT_blend_equation_separate",
83 "GL_EXT_blend_func_separate",
84 "GL_EXT_blend_minmax",
85 "GL_EXT_blend_subtract",
86 "GL_EXT_secondary_color",
87 "GL_EXT_stencil_wrap",
88 "GL_EXT_texture_edge_clamp",
89 "GL_EXT_texture_env_combine",
90 "GL_EXT_texture_env_dot3",
91 "GL_EXT_texture_filter_anisotropic",
92 "GL_EXT_texture_lod_bias",
93 "GL_EXT_texture_mirror_clamp",
94 "GL_EXT_texture_rectangle",
95 "GL_ATI_texture_env_combine3",
96 "GL_ATI_texture_mirror_once",
97 "GL_MESA_pack_invert",
98 "GL_MESA_ycbcr_texture",
99 "GL_NV_blend_square",
100 "GL_NV_vertex_program",
101 "GL_SGIS_generate_mipmap",
102 NULL
103 };
104
105 extern struct tnl_pipeline_stage _r300_render_stage;
106 extern struct tnl_pipeline_stage _r300_tcl_stage;
107
108 static const struct tnl_pipeline_stage *r300_pipeline[] = {
109
110 /* Try and go straight to t&l
111 */
112 &_r300_tcl_stage,
113
114 /* Catch any t&l fallbacks
115 */
116 &_tnl_vertex_transform_stage,
117 &_tnl_normal_transform_stage,
118 &_tnl_lighting_stage,
119 &_tnl_fog_coordinate_stage,
120 &_tnl_texgen_stage,
121 &_tnl_texture_transform_stage,
122 &_tnl_vertex_program_stage,
123
124 /* Try again to go to tcl?
125 * - no good for asymmetric-twoside (do with multipass)
126 * - no good for asymmetric-unfilled (do with multipass)
127 * - good for material
128 * - good for texgen
129 * - need to manipulate a bit of state
130 *
131 * - worth it/not worth it?
132 */
133
134 /* Else do them here.
135 */
136 &_r300_render_stage,
137 &_tnl_render_stage, /* FALLBACK */
138 0,
139 };
140
141 /* Create the device specific rendering context.
142 */
143 GLboolean r300CreateContext(const __GLcontextModes * glVisual,
144 __DRIcontextPrivate * driContextPriv,
145 void *sharedContextPrivate)
146 {
147 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
148 radeonScreenPtr screen = (radeonScreenPtr) (sPriv->private);
149 struct dd_function_table functions;
150 r300ContextPtr r300;
151 GLcontext *ctx;
152 int tcl_mode, i;
153
154 assert(glVisual);
155 assert(driContextPriv);
156 assert(screen);
157
158 /* Allocate the R300 context */
159 r300 = (r300ContextPtr)CALLOC(sizeof(*r300));
160 if (!r300)
161 return GL_FALSE;
162
163 /* Parse configuration files.
164 * Do this here so that initialMaxAnisotropy is set before we create
165 * the default textures.
166 */
167 driParseConfigFiles(&r300->radeon.optionCache, &screen->optionCache,
168 screen->driScreen->myNum, "r300");
169
170 /* Init default driver functions then plug in our R300-specific functions
171 * (the texture functions are especially important)
172 */
173 _mesa_init_driver_functions(&functions);
174 r300InitIoctlFuncs(&functions);
175 r300InitStateFuncs(&functions);
176 r300InitTextureFuncs(&functions);
177 r300InitShaderFuncs(&functions);
178
179 if (!radeonInitContext(&r300->radeon, &functions,
180 glVisual, driContextPriv, sharedContextPrivate)) {
181 FREE(r300);
182 return GL_FALSE;
183 }
184
185 /* Init r300 context data */
186 r300->dma.buf0_address = r300->radeon.radeonScreen->buffers->list[0].address;
187
188 (void)memset(r300->texture_heaps, 0, sizeof(r300->texture_heaps));
189 make_empty_list(&r300->swapped);
190
191 r300->nr_heaps = 1 /* screen->numTexHeaps */ ;
192 assert(r300->nr_heaps < R200_NR_TEX_HEAPS);
193 for (i = 0; i < r300->nr_heaps; i++) {
194 r300->texture_heaps[i] = driCreateTextureHeap(i, r300,
195 screen->
196 texSize[i], 12,
197 RADEON_NR_TEX_REGIONS,
198 (drmTextureRegionPtr)
199 r300->radeon.sarea->
200 tex_list[i],
201 &r300->radeon.sarea->
202 tex_age[i],
203 &r300->swapped,
204 sizeof
205 (r300TexObj),
206 (destroy_texture_object_t
207 *)
208 r300DestroyTexObj);
209 }
210 r300->texture_depth = driQueryOptioni(&r300->radeon.optionCache,
211 "texture_depth");
212 if (r300->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
213 r300->texture_depth = (screen->cpp == 4) ?
214 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
215
216 /* Set the maximum texture size small enough that we can guarentee that
217 * all texture units can bind a maximal texture and have them both in
218 * texturable memory at once.
219 */
220
221 ctx = r300->radeon.glCtx;
222
223 ctx->Const.MaxTextureImageUnits = driQueryOptioni(&r300->radeon.optionCache,
224 "texture_image_units");
225 ctx->Const.MaxTextureCoordUnits = driQueryOptioni(&r300->radeon.optionCache,
226 "texture_coord_units");
227 ctx->Const.MaxTextureUnits = MIN2(ctx->Const.MaxTextureImageUnits,
228 ctx->Const.MaxTextureCoordUnits);
229 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
230
231 ctx->Const.MinPointSize = 1.0;
232 ctx->Const.MinPointSizeAA = 1.0;
233 ctx->Const.MaxPointSize = R300_POINTSIZE_MAX;
234 ctx->Const.MaxPointSizeAA = R300_POINTSIZE_MAX;
235
236 ctx->Const.MinLineWidth = 1.0;
237 ctx->Const.MinLineWidthAA = 1.0;
238 ctx->Const.MaxLineWidth = R300_LINESIZE_MAX;
239 ctx->Const.MaxLineWidthAA = R300_LINESIZE_MAX;
240
241 /* Initialize the software rasterizer and helper modules.
242 */
243 _swrast_CreateContext(ctx);
244 _ac_CreateContext(ctx);
245 _tnl_CreateContext(ctx);
246 _swsetup_CreateContext(ctx);
247 _ae_create_context(ctx);
248
249 /* Install the customized pipeline:
250 */
251 _tnl_destroy_pipeline(ctx);
252 _tnl_install_pipeline(ctx, r300_pipeline);
253
254 /* Try and keep materials and vertices separate:
255 */
256 _tnl_isolate_materials(ctx, GL_TRUE);
257
258 /* Configure swrast and TNL to match hardware characteristics:
259 */
260 _swrast_allow_pixel_fog(ctx, GL_FALSE);
261 _swrast_allow_vertex_fog(ctx, GL_TRUE);
262 _tnl_allow_pixel_fog(ctx, GL_FALSE);
263 _tnl_allow_vertex_fog(ctx, GL_TRUE);
264
265 #if 0
266 //if(driQueryOptionb(&rmesa->optionCache, "arb_vertex_program"))
267 _mesa_enable_extension( ctx, "GL_ARB_vertex_program");
268 //if(driQueryOptionb(&rmesa->optionCache, "nv_vertex_program"))
269 _mesa_enable_extension( ctx, "GL_NV_vertex_program");
270 #endif
271 /* currently bogus data */
272 ctx->Const.MaxVertexProgramInstructions=VSF_MAX_FRAGMENT_LENGTH;
273 ctx->Const.MaxVertexProgramAttribs=16; // r420
274 ctx->Const.MaxVertexProgramTemps=VSF_MAX_FRAGMENT_TEMPS;
275 ctx->Const.MaxVertexProgramLocalParams=256; // r420
276 ctx->Const.MaxVertexProgramEnvParams=256; // r420
277 ctx->Const.MaxVertexProgramAddressRegs=1;
278
279 driInitExtensions(ctx, card_extensions, GL_TRUE);
280
281 radeonInitSpanFuncs(ctx);
282 r300InitCmdBuf(r300);
283 r300InitState(r300);
284
285 #if 0
286 /* plug in a few more device driver functions */
287 /* XXX these should really go right after _mesa_init_driver_functions() */
288 r300InitPixelFuncs(ctx);
289 r300InitSwtcl(ctx);
290 #endif
291 TNL_CONTEXT(ctx)->Driver.RunPipeline = _tnl_run_pipeline;
292
293 tcl_mode = driQueryOptioni(&r300->radeon.optionCache, "tcl_mode");
294 if (driQueryOptionb(&r300->radeon.optionCache, "no_rast")) {
295 fprintf(stderr, "disabling 3D acceleration\n");
296 FALLBACK(&r300->radeon, RADEON_FALLBACK_DISABLE, 1);
297 }
298 if (tcl_mode == DRI_CONF_TCL_SW ||
299 !(r300->radeon.radeonScreen->chipset & RADEON_CHIPSET_TCL)) {
300 if (r300->radeon.radeonScreen->chipset & RADEON_CHIPSET_TCL) {
301 r300->radeon.radeonScreen->chipset &= ~RADEON_CHIPSET_TCL;
302 fprintf(stderr, "Disabling HW TCL support\n");
303 }
304 TCL_FALLBACK(r300->radeon.glCtx, RADEON_TCL_FALLBACK_TCL_DISABLE, 1);
305 }
306
307 return GL_TRUE;
308 }
309
310 /* Destroy the device specific context.
311 */
312 void r300DestroyContext(__DRIcontextPrivate * driContextPriv)
313 {
314 GET_CURRENT_CONTEXT(ctx);
315 r300ContextPtr r300 = (r300ContextPtr) driContextPriv->driverPrivate;
316 radeonContextPtr current = ctx ? RADEON_CONTEXT(ctx) : NULL;
317
318 fprintf(stderr, "Destroying context !\n");
319 sleep(1);
320 /* check if we're deleting the currently bound context */
321 if (&r300->radeon == current) {
322 radeonFlush(r300->radeon.glCtx);
323 _mesa_make_current2(NULL, NULL, NULL);
324 }
325
326 /* Free r300 context resources */
327 assert(r300); /* should never be null */
328
329 if (r300) {
330 GLboolean release_texture_heaps;
331
332 release_texture_heaps = (r300->radeon.glCtx->Shared->RefCount == 1);
333 _swsetup_DestroyContext(r300->radeon.glCtx);
334 _tnl_DestroyContext(r300->radeon.glCtx);
335 _ac_DestroyContext(r300->radeon.glCtx);
336 _swrast_DestroyContext(r300->radeon.glCtx);
337
338 r300DestroyCmdBuf(r300);
339
340 radeonCleanupContext(&r300->radeon);
341
342 /* free the option cache */
343 driDestroyOptionCache(&r300->radeon.optionCache);
344
345 FREE(r300);
346 }
347 }