bugfixes wrt texture compression
[mesa.git] / src / mesa / drivers / glide / fxvb.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
23 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /* Authors:
27 * Keith Whitwell
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "conf.h"
32 #endif
33
34 #ifdef FX
35
36 #include "glheader.h"
37 #include "mtypes.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "colormac.h"
41
42 #include "math/m_translate.h"
43 #include "swrast_setup/swrast_setup.h"
44
45 #include "tnl/tnl.h"
46 #include "tnl/t_context.h"
47
48 #include "fxdrv.h"
49
50
51 static void copy_pv( GLcontext *ctx, GLuint edst, GLuint esrc )
52 {
53 fxMesaContext fxMesa = FX_CONTEXT( ctx );
54 GrVertex *dst = fxMesa->verts + edst;
55 GrVertex *src = fxMesa->verts + esrc;
56
57 *(GLuint *)&dst->pargb = *(GLuint *)&src->pargb;
58 }
59
60 typedef void (*emit_func)( GLcontext *, GLuint, GLuint, void * );
61
62 static struct {
63 emit_func emit;
64 interp_func interp;
65 GLboolean (*check_tex_sizes)( GLcontext *ctx );
66 GLuint vertex_format;
67 } setup_tab[MAX_SETUP];
68
69
70 static void import_float_colors( GLcontext *ctx )
71 {
72 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
73 struct gl_client_array *from = VB->ColorPtr[0];
74 struct gl_client_array *to = &FX_CONTEXT(ctx)->UbyteColor;
75 GLuint count = VB->Count;
76
77 if (!to->Ptr) {
78 to->Ptr = ALIGN_MALLOC( VB->Size * 4 * sizeof(GLubyte), 32 );
79 to->Type = GL_UNSIGNED_BYTE;
80 }
81
82 /* No need to transform the same value 3000 times.
83 */
84 if (!from->StrideB) {
85 to->StrideB = 0;
86 count = 1;
87 }
88 else
89 to->StrideB = 4 * sizeof(GLubyte);
90
91 _math_trans_4ub( (GLubyte (*)[4]) to->Ptr,
92 from->Ptr,
93 from->StrideB,
94 from->Type,
95 from->Size,
96 0,
97 count);
98
99 VB->ColorPtr[0] = to;
100 }
101
102
103 /* Hack alert: assume chan is 8 bits */
104 #define GET_COLOR(ptr, idx) (((GLchan (*)[4])((ptr)->Ptr))[idx])
105
106
107 static void interp_extras( GLcontext *ctx,
108 GLfloat t,
109 GLuint dst, GLuint out, GLuint in,
110 GLboolean force_boundary )
111 {
112 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
113
114 if (VB->ColorPtr[1]) {
115 INTERP_4CHAN( t,
116 GET_COLOR(VB->ColorPtr[1], dst),
117 GET_COLOR(VB->ColorPtr[1], out),
118 GET_COLOR(VB->ColorPtr[1], in) );
119 #if 1 /* [dBorca] GL_EXT_separate_specular_color */
120 if (VB->SecondaryColorPtr[1]) {
121 INTERP_3CHAN( t,
122 GET_COLOR(VB->SecondaryColorPtr[1], dst),
123 GET_COLOR(VB->SecondaryColorPtr[1], out),
124 GET_COLOR(VB->SecondaryColorPtr[1], in) );
125 }
126 #endif
127 }
128
129 if (VB->EdgeFlag) {
130 VB->EdgeFlag[dst] = VB->EdgeFlag[out] || force_boundary;
131 }
132
133 setup_tab[FX_CONTEXT(ctx)->SetupIndex].interp(ctx, t, dst, out, in,
134 force_boundary);
135 }
136
137 static void copy_pv_extras( GLcontext *ctx, GLuint dst, GLuint src )
138 {
139 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
140
141 if (VB->ColorPtr[1]) {
142 COPY_CHAN4( GET_COLOR(VB->ColorPtr[1], dst),
143 GET_COLOR(VB->ColorPtr[1], src) );
144 #if 1 /* [dBorca] GL_EXT_separate_specular_color */
145 if (VB->SecondaryColorPtr[1]) {
146 COPY_CHAN4( GET_COLOR(VB->SecondaryColorPtr[1], dst),
147 GET_COLOR(VB->SecondaryColorPtr[1], src) );
148 }
149 #endif
150 }
151
152 copy_pv(ctx, dst, src);
153 }
154
155
156 #define IND (SETUP_XYZW|SETUP_RGBA)
157 #define TAG(x) x##_wg
158 #include "fxvbtmp.h"
159
160 #define IND (SETUP_XYZW|SETUP_RGBA|SETUP_TMU0)
161 #define TAG(x) x##_wgt0
162 #include "fxvbtmp.h"
163
164 #define IND (SETUP_XYZW|SETUP_RGBA|SETUP_TMU0|SETUP_TMU1)
165 #define TAG(x) x##_wgt0t1
166 #include "fxvbtmp.h"
167
168 #define IND (SETUP_XYZW|SETUP_RGBA|SETUP_TMU0|SETUP_PTEX)
169 #define TAG(x) x##_wgpt0
170 #include "fxvbtmp.h"
171
172 #define IND (SETUP_XYZW|SETUP_RGBA|SETUP_TMU0|SETUP_TMU1|\
173 SETUP_PTEX)
174 #define TAG(x) x##_wgpt0t1
175 #include "fxvbtmp.h"
176
177
178 /* Snapping for voodoo-1
179 */
180 #define IND (SETUP_XYZW|SETUP_SNAP|SETUP_RGBA)
181 #define TAG(x) x##_wsg
182 #include "fxvbtmp.h"
183
184 #define IND (SETUP_XYZW|SETUP_SNAP|SETUP_RGBA|SETUP_TMU0)
185 #define TAG(x) x##_wsgt0
186 #include "fxvbtmp.h"
187
188 #define IND (SETUP_XYZW|SETUP_SNAP|SETUP_RGBA|SETUP_TMU0|\
189 SETUP_TMU1)
190 #define TAG(x) x##_wsgt0t1
191 #include "fxvbtmp.h"
192
193 #define IND (SETUP_XYZW|SETUP_SNAP|SETUP_RGBA|SETUP_TMU0|\
194 SETUP_PTEX)
195 #define TAG(x) x##_wsgpt0
196 #include "fxvbtmp.h"
197
198 #define IND (SETUP_XYZW|SETUP_SNAP|SETUP_RGBA|SETUP_TMU0|\
199 SETUP_TMU1|SETUP_PTEX)
200 #define TAG(x) x##_wsgpt0t1
201 #include "fxvbtmp.h"
202
203
204 /* Vertex repair (multipass rendering)
205 */
206 #define IND (SETUP_RGBA)
207 #define TAG(x) x##_g
208 #include "fxvbtmp.h"
209
210 #define IND (SETUP_TMU0)
211 #define TAG(x) x##_t0
212 #include "fxvbtmp.h"
213
214 #define IND (SETUP_TMU0|SETUP_TMU1)
215 #define TAG(x) x##_t0t1
216 #include "fxvbtmp.h"
217
218 #define IND (SETUP_RGBA|SETUP_TMU0)
219 #define TAG(x) x##_gt0
220 #include "fxvbtmp.h"
221
222 #define IND (SETUP_RGBA|SETUP_TMU0|SETUP_TMU1)
223 #define TAG(x) x##_gt0t1
224 #include "fxvbtmp.h"
225
226
227
228 static void init_setup_tab( void )
229 {
230 init_wg();
231 init_wgt0();
232 init_wgt0t1();
233 init_wgpt0();
234 init_wgpt0t1();
235
236 init_wsg();
237 init_wsgt0();
238 init_wsgt0t1();
239 init_wsgpt0();
240 init_wsgpt0t1();
241
242 init_g();
243 init_t0();
244 init_t0t1();
245 init_gt0();
246 init_gt0t1();
247 }
248
249
250 void fxPrintSetupFlags(char *msg, GLuint flags )
251 {
252 fprintf(stderr, "%s(%x): %s%s%s%s%s\n",
253 msg,
254 (int)flags,
255 (flags & SETUP_XYZW) ? " xyzw," : "",
256 (flags & SETUP_SNAP) ? " snap," : "",
257 (flags & SETUP_RGBA) ? " rgba," : "",
258 (flags & SETUP_TMU0) ? " tex-0," : "",
259 (flags & SETUP_TMU1) ? " tex-1," : "");
260 }
261
262
263
264 void fxCheckTexSizes( GLcontext *ctx )
265 {
266 TNLcontext *tnl = TNL_CONTEXT(ctx);
267 fxMesaContext fxMesa = FX_CONTEXT( ctx );
268
269 if (!setup_tab[fxMesa->SetupIndex].check_tex_sizes(ctx)) {
270 GLuint ind = fxMesa->SetupIndex |= (SETUP_PTEX|SETUP_RGBA);
271
272 /* Tdfx handles projective textures nicely; just have to change
273 * up to the new vertex format.
274 */
275 if (setup_tab[ind].vertex_format != fxMesa->stw_hint_state) {
276
277 fxMesa->stw_hint_state = setup_tab[ind].vertex_format;
278 FX_grHints(GR_HINT_STWHINT, fxMesa->stw_hint_state);
279
280 /* This is required as we have just changed the vertex
281 * format, so the interp routines must also change.
282 * In the unfilled and twosided cases we are using the
283 * Extras ones anyway, so leave them in place.
284 */
285 if (!(ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
286 tnl->Driver.Render.Interp = setup_tab[fxMesa->SetupIndex].interp;
287 }
288 }
289 }
290 }
291
292
293 void fxBuildVertices( GLcontext *ctx, GLuint start, GLuint count,
294 GLuint newinputs )
295 {
296 fxMesaContext fxMesa = FX_CONTEXT( ctx );
297 GrVertex *v = (fxMesa->verts + start);
298
299 if (!newinputs)
300 return;
301
302 if (newinputs & VERT_BIT_CLIP) {
303 setup_tab[fxMesa->SetupIndex].emit( ctx, start, count, v );
304 } else {
305 GLuint ind = 0;
306
307 if (newinputs & VERT_BIT_COLOR0)
308 ind |= SETUP_RGBA;
309
310 if (newinputs & VERT_BIT_TEX0)
311 ind |= SETUP_TMU0;
312
313 if (newinputs & VERT_BIT_TEX1)
314 ind |= SETUP_TMU0|SETUP_TMU1;
315
316 if (fxMesa->SetupIndex & SETUP_PTEX)
317 ind = ~0;
318
319 ind &= fxMesa->SetupIndex;
320
321 if (ind) {
322 setup_tab[ind].emit( ctx, start, count, v );
323 }
324 }
325 }
326
327
328 void fxChooseVertexState( GLcontext *ctx )
329 {
330 TNLcontext *tnl = TNL_CONTEXT(ctx);
331 fxMesaContext fxMesa = FX_CONTEXT( ctx );
332 GLuint ind = SETUP_XYZW|SETUP_RGBA;
333
334 if (fxMesa->snapVertices)
335 ind |= SETUP_SNAP;
336
337 fxMesa->tmu_source[0] = 0;
338 fxMesa->tmu_source[1] = 1;
339
340 if (ctx->Texture._EnabledUnits & 0x2) {
341 if (ctx->Texture._EnabledUnits & 0x1) {
342 ind |= SETUP_TMU1|SETUP_TMU0;
343 }
344 else {
345 fxMesa->tmu_source[0] = 1;
346 fxMesa->tmu_source[1] = 0;
347 ind |= SETUP_TMU0;
348 }
349 }
350 else if (ctx->Texture._EnabledUnits & 0x1) {
351 ind |= SETUP_TMU0;
352 }
353
354 fxMesa->SetupIndex = ind;
355
356 if (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED)) {
357 tnl->Driver.Render.Interp = interp_extras;
358 tnl->Driver.Render.CopyPV = copy_pv_extras;
359 } else {
360 tnl->Driver.Render.Interp = setup_tab[ind].interp;
361 tnl->Driver.Render.CopyPV = copy_pv;
362 }
363
364 if (setup_tab[ind].vertex_format != fxMesa->stw_hint_state) {
365 fxMesa->stw_hint_state = setup_tab[ind].vertex_format;
366 FX_grHints(GR_HINT_STWHINT, fxMesa->stw_hint_state);
367 }
368 }
369
370
371
372 void fxAllocVB( GLcontext *ctx )
373 {
374 fxMesaContext fxMesa = FX_CONTEXT(ctx);
375 GLuint size = TNL_CONTEXT(ctx)->vb.Size;
376 static int firsttime = 1;
377 if (firsttime) {
378 init_setup_tab();
379 firsttime = 0;
380 }
381
382 fxMesa->verts = (GrVertex *)ALIGN_MALLOC(size * sizeof(GrVertex), 32);
383 fxMesa->SetupIndex = SETUP_XYZW|SETUP_RGBA;
384 }
385
386
387 void fxFreeVB( GLcontext *ctx )
388 {
389 fxMesaContext fxMesa = FX_CONTEXT(ctx);
390 if (fxMesa->verts) {
391 ALIGN_FREE(fxMesa->verts);
392 fxMesa->verts = 0;
393 }
394
395 if (fxMesa->UbyteColor.Ptr) {
396 ALIGN_FREE((void *)fxMesa->UbyteColor.Ptr);
397 fxMesa->UbyteColor.Ptr = 0;
398 }
399 }
400 #else
401
402
403 /*
404 * Need this to provide at least one external definition.
405 */
406
407 extern int gl_fx_dummy_function_vb(void);
408 int
409 gl_fx_dummy_function_vb(void)
410 {
411 return 0;
412 }
413
414 #endif /* FX */