Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / mga / mgavb.c
1 /*
2  * Copyright 2000-2001 VA Linux Systems, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Keith Whitwell <keith@tungstengraphics.com>
26  */
27
28 #include <stdlib.h>
29 #include "mgacontext.h"
30 #include "mgavb.h"
31 #include "mgatris.h"
32 #include "mgaioctl.h"
33 #include "mga_xmesa.h"
34
35 #include "main/glheader.h"
36 #include "main/mtypes.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/colormac.h"
40
41 #include "tnl/t_context.h"
42 #include "swrast/swrast.h"
43
44
45 #define MGA_TEX1_BIT       0x1
46 #define MGA_TEX0_BIT       0x2  
47 #define MGA_RGBA_BIT       0x4
48 #define MGA_SPEC_BIT       0x8
49 #define MGA_FOG_BIT        0x10
50 #define MGA_XYZW_BIT       0x20
51 #define MGA_PTEX_BIT       0x40
52 #define MGA_MAX_SETUP      0x80
53
54 static struct {
55    void                (*emit)( struct gl_context *, GLuint, GLuint, void *, GLuint );
56    tnl_interp_func              interp;
57    tnl_copy_pv_func             copy_pv;
58    GLboolean           (*check_tex_sizes)( struct gl_context *ctx );
59    GLuint               vertex_size;
60    GLuint               vertex_format;
61 } setup_tab[MGA_MAX_SETUP];
62
63
64 #define TINY_VERTEX_FORMAT      0
65 #define NOTEX_VERTEX_FORMAT     0
66 #define TEX0_VERTEX_FORMAT      (MGA_A|MGA_S|MGA_F)
67 #define TEX1_VERTEX_FORMAT      (MGA_A|MGA_S|MGA_F|MGA_T2)
68 #define PROJ_TEX1_VERTEX_FORMAT 0
69 #define TEX2_VERTEX_FORMAT      0
70 #define TEX3_VERTEX_FORMAT      0
71 #define PROJ_TEX3_VERTEX_FORMAT 0
72
73 #define DO_XYZW (IND & MGA_XYZW_BIT)
74 #define DO_RGBA (IND & MGA_RGBA_BIT)
75 #define DO_SPEC (IND & MGA_SPEC_BIT)
76 #define DO_FOG  (IND & MGA_FOG_BIT)
77 #define DO_TEX0 (IND & MGA_TEX0_BIT)
78 #define DO_TEX1 (IND & MGA_TEX1_BIT)
79 #define DO_TEX2 0
80 #define DO_TEX3 0
81 #define DO_PTEX (IND & MGA_PTEX_BIT)
82
83                                
84 #define VERTEX mgaVertex
85 #define VERTEX_COLOR mga_color_t
86 #define LOCALVARS mgaContextPtr mmesa = MGA_CONTEXT(ctx);
87 #define GET_VIEWPORT_MAT() mmesa->hw_viewport
88 #define GET_TEXSOURCE(n)  mmesa->tmu_source[n]
89 #define GET_VERTEX_FORMAT() mmesa->vertex_format
90 #define GET_VERTEX_STORE() mmesa->verts
91 #define GET_VERTEX_SIZE() mmesa->vertex_size * sizeof(GLuint)
92
93 #define HAVE_HW_VIEWPORT    0
94 #define HAVE_HW_DIVIDE      0
95 #define HAVE_RGBA_COLOR     0
96 #define HAVE_TINY_VERTICES  0
97 #define HAVE_NOTEX_VERTICES 0
98 #define HAVE_TEX0_VERTICES  1
99 #define HAVE_TEX1_VERTICES  1
100 #define HAVE_TEX2_VERTICES  0
101 #define HAVE_TEX3_VERTICES  0
102 #define HAVE_PTEX_VERTICES  0
103
104 #define UNVIEWPORT_VARS                                 \
105    const GLfloat dx = - mmesa->drawX - SUBPIXEL_X;      \
106    const GLfloat dy = (mmesa->driDrawable->h +          \
107                        mmesa->drawY + SUBPIXEL_Y);      \
108    const GLfloat sz = 1.0 / mmesa->depth_scale
109
110 #define UNVIEWPORT_X(x)    x      + dx;
111 #define UNVIEWPORT_Y(y)  - y      + dy;
112 #define UNVIEWPORT_Z(z)    z * sz;
113
114 #define PTEX_FALLBACK() FALLBACK(ctx, MGA_FALLBACK_TEXTURE, 1)
115
116 #define INTERP_VERTEX setup_tab[mmesa->SetupIndex].interp
117 #define COPY_PV_VERTEX setup_tab[mmesa->SetupIndex].copy_pv
118
119
120 /***********************************************************************
121  *         Generate  pv-copying and translation functions              *
122  ***********************************************************************/
123
124 #define TAG(x) mga_##x
125 #include "tnl_dd/t_dd_vb.c"
126
127 /***********************************************************************
128  *             Generate vertex emit and interp functions               *
129  ***********************************************************************/
130
131
132 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT)
133 #define TAG(x) x##_wg
134 #include "tnl_dd/t_dd_vbtmp.h"
135
136 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT)
137 #define TAG(x) x##_wgs
138 #include "tnl_dd/t_dd_vbtmp.h"
139
140 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT)
141 #define TAG(x) x##_wgt0
142 #include "tnl_dd/t_dd_vbtmp.h"
143
144 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
145 #define TAG(x) x##_wgt0t1
146 #include "tnl_dd/t_dd_vbtmp.h"
147
148 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
149 #define TAG(x) x##_wgpt0
150 #include "tnl_dd/t_dd_vbtmp.h"
151
152 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
153 #define TAG(x) x##_wgst0
154 #include "tnl_dd/t_dd_vbtmp.h"
155
156 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
157 #define TAG(x) x##_wgst0t1
158 #include "tnl_dd/t_dd_vbtmp.h"
159
160 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
161 #define TAG(x) x##_wgspt0
162 #include "tnl_dd/t_dd_vbtmp.h"
163
164 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT)
165 #define TAG(x) x##_wgf
166 #include "tnl_dd/t_dd_vbtmp.h"
167
168 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT)
169 #define TAG(x) x##_wgfs
170 #include "tnl_dd/t_dd_vbtmp.h"
171
172 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT)
173 #define TAG(x) x##_wgft0
174 #include "tnl_dd/t_dd_vbtmp.h"
175
176 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
177 #define TAG(x) x##_wgft0t1
178 #include "tnl_dd/t_dd_vbtmp.h"
179
180 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
181 #define TAG(x) x##_wgfpt0
182 #include "tnl_dd/t_dd_vbtmp.h"
183
184 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
185 #define TAG(x) x##_wgfst0
186 #include "tnl_dd/t_dd_vbtmp.h"
187
188 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
189 #define TAG(x) x##_wgfst0t1
190 #include "tnl_dd/t_dd_vbtmp.h"
191
192 #define IND (MGA_XYZW_BIT|MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_PTEX_BIT)
193 #define TAG(x) x##_wgfspt0
194 #include "tnl_dd/t_dd_vbtmp.h"
195
196 #define IND (MGA_TEX0_BIT)
197 #define TAG(x) x##_t0
198 #include "tnl_dd/t_dd_vbtmp.h"
199
200 #define IND (MGA_TEX0_BIT|MGA_TEX1_BIT)
201 #define TAG(x) x##_t0t1
202 #include "tnl_dd/t_dd_vbtmp.h"
203
204 #define IND (MGA_FOG_BIT)
205 #define TAG(x) x##_f
206 #include "tnl_dd/t_dd_vbtmp.h"
207
208 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT)
209 #define TAG(x) x##_ft0
210 #include "tnl_dd/t_dd_vbtmp.h"
211
212 #define IND (MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
213 #define TAG(x) x##_ft0t1
214 #include "tnl_dd/t_dd_vbtmp.h"
215
216 #define IND (MGA_RGBA_BIT)
217 #define TAG(x) x##_g
218 #include "tnl_dd/t_dd_vbtmp.h"
219
220 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT)
221 #define TAG(x) x##_gs
222 #include "tnl_dd/t_dd_vbtmp.h"
223
224 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT)
225 #define TAG(x) x##_gt0
226 #include "tnl_dd/t_dd_vbtmp.h"
227
228 #define IND (MGA_RGBA_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
229 #define TAG(x) x##_gt0t1
230 #include "tnl_dd/t_dd_vbtmp.h"
231
232 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
233 #define TAG(x) x##_gst0
234 #include "tnl_dd/t_dd_vbtmp.h"
235
236 #define IND (MGA_RGBA_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
237 #define TAG(x) x##_gst0t1
238 #include "tnl_dd/t_dd_vbtmp.h"
239
240 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT)
241 #define TAG(x) x##_gf
242 #include "tnl_dd/t_dd_vbtmp.h"
243
244 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT)
245 #define TAG(x) x##_gfs
246 #include "tnl_dd/t_dd_vbtmp.h"
247
248 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT)
249 #define TAG(x) x##_gft0
250 #include "tnl_dd/t_dd_vbtmp.h"
251
252 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
253 #define TAG(x) x##_gft0t1
254 #include "tnl_dd/t_dd_vbtmp.h"
255
256 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT)
257 #define TAG(x) x##_gfst0
258 #include "tnl_dd/t_dd_vbtmp.h"
259
260 #define IND (MGA_RGBA_BIT|MGA_FOG_BIT|MGA_SPEC_BIT|MGA_TEX0_BIT|MGA_TEX1_BIT)
261 #define TAG(x) x##_gfst0t1
262 #include "tnl_dd/t_dd_vbtmp.h"
263
264
265 static void init_setup_tab( void )
266 {
267    init_wg();
268    init_wgs();
269    init_wgt0();
270    init_wgt0t1();
271    init_wgpt0();
272    init_wgst0();
273    init_wgst0t1();
274    init_wgspt0();
275    init_wgf();
276    init_wgfs();
277    init_wgft0();
278    init_wgft0t1();
279    init_wgfpt0();
280    init_wgfst0();
281    init_wgfst0t1();
282    init_wgfspt0();
283    init_t0();
284    init_t0t1();
285    init_f();
286    init_ft0();
287    init_ft0t1();
288    init_g();
289    init_gs();
290    init_gt0();
291    init_gt0t1();
292    init_gst0();
293    init_gst0t1();
294    init_gf();
295    init_gfs();
296    init_gft0();
297    init_gft0t1();
298    init_gfst0();
299    init_gfst0t1();
300 }
301
302
303
304
305 void mgaPrintSetupFlags(char *msg, GLuint flags )
306 {
307    fprintf(stderr, "%s: %d %s%s%s%s%s%s\n",
308            msg,
309            (int)flags,
310            (flags & MGA_XYZW_BIT)      ? " xyzw," : "", 
311            (flags & MGA_RGBA_BIT)     ? " rgba," : "",
312            (flags & MGA_SPEC_BIT)     ? " spec," : "",
313            (flags & MGA_FOG_BIT)      ? " fog," : "",
314            (flags & MGA_TEX0_BIT)     ? " tex-0," : "",
315            (flags & MGA_TEX1_BIT)     ? " tex-1," : "");
316 }
317
318
319 void mgaCheckTexSizes( struct gl_context *ctx )
320 {
321    mgaContextPtr mmesa = MGA_CONTEXT( ctx );
322    TNLcontext *tnl = TNL_CONTEXT(ctx);
323
324    /*fprintf(stderr, "%s\n", __FUNCTION__);*/
325
326    if (!setup_tab[mmesa->SetupIndex].check_tex_sizes(ctx)) {
327       mmesa->SetupIndex |= MGA_PTEX_BIT;
328       mmesa->SetupNewInputs = ~0;
329
330       if (!mmesa->Fallback &&
331           !(ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED))) {
332          tnl->Driver.Render.Interp = setup_tab[mmesa->SetupIndex].interp;
333          tnl->Driver.Render.CopyPV = setup_tab[mmesa->SetupIndex].copy_pv;
334       }
335       if (mmesa->Fallback) {
336          tnl->Driver.Render.Start(ctx);
337       }
338    }
339 }
340
341
342 void mgaBuildVertices( struct gl_context *ctx, 
343                        GLuint start, 
344                        GLuint count,
345                        GLuint newinputs )
346 {
347    mgaContextPtr mmesa = MGA_CONTEXT( ctx );
348    GLuint stride = mmesa->vertex_size * sizeof(int);
349    GLubyte *v = ((GLubyte *)mmesa->verts + (start * stride));
350
351    newinputs |= mmesa->SetupNewInputs;
352    mmesa->SetupNewInputs = 0;
353
354    if (!newinputs)
355       return;
356
357    if (newinputs & VERT_BIT_POS) {
358       setup_tab[mmesa->SetupIndex].emit( ctx, start, count, v, stride );   
359    } else {
360       GLuint ind = 0;
361
362       if (newinputs & VERT_BIT_COLOR0)
363          ind |= MGA_RGBA_BIT;
364       
365       if (newinputs & VERT_BIT_COLOR1)
366          ind |= MGA_SPEC_BIT;
367
368       if (newinputs & VERT_BIT_TEX0) 
369          ind |= MGA_TEX0_BIT;
370
371       if (newinputs & VERT_BIT_TEX1)
372          ind |= MGA_TEX0_BIT|MGA_TEX1_BIT;
373
374       if (newinputs & VERT_BIT_FOG)
375          ind |= MGA_FOG_BIT;
376
377       if (mmesa->SetupIndex & MGA_PTEX_BIT)
378          ind = ~0;
379
380       ind &= mmesa->SetupIndex;
381
382       if (ind) {
383          setup_tab[ind].emit( ctx, start, count, v, stride );   
384       }
385    }
386 }
387
388
389 void mgaChooseVertexState( struct gl_context *ctx )
390 {
391    mgaContextPtr mmesa = MGA_CONTEXT( ctx );
392    TNLcontext *tnl = TNL_CONTEXT(ctx);
393    GLuint ind = MGA_XYZW_BIT|MGA_RGBA_BIT;
394
395    if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) 
396       ind |= MGA_SPEC_BIT;
397
398    if (ctx->Fog.Enabled) 
399       ind |= MGA_FOG_BIT;
400    
401    if (ctx->Texture._EnabledUnits & 0x2) {
402       /* unit 1 enabled */
403       if (ctx->Texture._EnabledUnits & 0x1) {
404          /* unit 0 enabled */
405          ind |= MGA_TEX1_BIT|MGA_TEX0_BIT;
406       }
407       else {
408          ind |= MGA_TEX0_BIT;
409       }
410    }
411    else if (ctx->Texture._EnabledUnits & 0x1) {
412       /* unit 0 enabled */
413       ind |= MGA_TEX0_BIT;
414    }
415    
416    mmesa->SetupIndex = ind;
417
418    if (ctx->_TriangleCaps & (DD_TRI_LIGHT_TWOSIDE|DD_TRI_UNFILLED)) {
419       tnl->Driver.Render.Interp = mga_interp_extras;
420       tnl->Driver.Render.CopyPV = mga_copy_pv_extras;
421    } else {
422       tnl->Driver.Render.Interp = setup_tab[ind].interp;
423       tnl->Driver.Render.CopyPV = setup_tab[ind].copy_pv;
424    }
425
426    if (setup_tab[ind].vertex_format != mmesa->vertex_format) {
427       FLUSH_BATCH(mmesa);      
428       mmesa->dirty |= MGA_UPLOAD_PIPE;
429       mmesa->vertex_format = setup_tab[ind].vertex_format;
430       mmesa->vertex_size = setup_tab[ind].vertex_size;
431    }
432 }
433
434
435
436 void *mga_emit_contiguous_verts( struct gl_context *ctx,
437                                  GLuint start,
438                                  GLuint count,
439                                  void *dest)
440 {
441    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
442    GLuint stride = mmesa->vertex_size * 4;
443    setup_tab[mmesa->SetupIndex].emit( ctx, start, count, dest, stride );
444    return (void *)((char *)dest + stride * (count - start));
445 }
446                                    
447
448
449 void mgaInitVB( struct gl_context *ctx )
450 {
451    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
452    GLuint size = TNL_CONTEXT(ctx)->vb.Size;
453
454    mmesa->verts = (GLubyte *)_mesa_align_malloc(size * sizeof(mgaVertex), 32);
455
456    {
457       static int firsttime = 1;
458       if (firsttime) {
459          init_setup_tab();
460          firsttime = 0;
461       }
462    }
463
464    mmesa->dirty |= MGA_UPLOAD_PIPE;
465    mmesa->vertex_format = setup_tab[0].vertex_format;
466    mmesa->vertex_size = setup_tab[0].vertex_size;
467 }
468
469
470 void mgaFreeVB( struct gl_context *ctx )
471 {
472    mgaContextPtr mmesa = MGA_CONTEXT(ctx);
473    if (mmesa->verts) {
474       _mesa_align_free(mmesa->verts);
475       mmesa->verts = 0;
476    }
477 }
478