6ba1eabbf241c3dbfdb8441f784f9274d365fe89
[mesa.git] / src / mesa / drivers / dri / ffb / ffb_vb.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/ffb/ffb_vb.c,v 1.4 2002/02/22 21:32:59 dawes Exp $
2 *
3 * GLX Hardware Device Driver for Sun Creator/Creator3D
4 * Copyright (C) 2000, 2001 David S. Miller
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * DAVID MILLER, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 *
25 * David S. Miller <davem@redhat.com>
26 */
27
28 #include "ffb_xmesa.h"
29 #include "ffb_context.h"
30 #include "ffb_vb.h"
31 #include "imports.h"
32 #include "tnl/t_context.h"
33 #include "swrast_setup/swrast_setup.h"
34 #include "math/m_translate.h"
35
36 #undef VB_DEBUG
37
38 static void ffb_copy_pv_oneside(GLcontext *ctx, GLuint edst, GLuint esrc)
39 {
40 ffbContextPtr fmesa = FFB_CONTEXT(ctx);
41 ffb_vertex *dst = &fmesa->verts[edst];
42 ffb_vertex *src = &fmesa->verts[esrc];
43
44 #ifdef VB_DEBUG
45 fprintf(stderr, "ffb_copy_pv_oneside: edst(%d) esrc(%d)\n", edst, esrc);
46 #endif
47 dst->color[0].alpha = src->color[0].alpha;
48 dst->color[0].red = src->color[0].red;
49 dst->color[0].green = src->color[0].green;
50 dst->color[0].blue = src->color[0].blue;
51 }
52
53 static void ffb_copy_pv_twoside(GLcontext *ctx, GLuint edst, GLuint esrc)
54 {
55 ffbContextPtr fmesa = FFB_CONTEXT(ctx);
56 ffb_vertex *dst = &fmesa->verts[edst];
57 ffb_vertex *src = &fmesa->verts[esrc];
58
59 #ifdef VB_DEBUG
60 fprintf(stderr, "ffb_copy_pv_twoside: edst(%d) esrc(%d)\n", edst, esrc);
61 #endif
62 dst->color[0].alpha = src->color[0].alpha;
63 dst->color[0].red = src->color[0].red;
64 dst->color[0].green = src->color[0].green;
65 dst->color[0].blue = src->color[0].blue;
66 dst->color[1].alpha = src->color[1].alpha;
67 dst->color[1].red = src->color[1].red;
68 dst->color[1].green = src->color[1].green;
69 dst->color[1].blue = src->color[1].blue;
70 }
71
72 #define FFB_VB_RGBA_BIT 0x01
73 #define FFB_VB_XYZ_BIT 0x02
74 #define FFB_VB_TWOSIDE_BIT 0x04
75 #define FFB_VB_MAX 0x08
76
77 typedef void (*ffb_emit_func)(GLcontext *, GLuint, GLuint);
78
79 static struct {
80 ffb_emit_func emit;
81 tnl_interp_func interp;
82 } setup_tab[FFB_VB_MAX];
83
84
85 #define IND (FFB_VB_XYZ_BIT)
86 #define TAG(x) x##_w
87 #include "ffb_vbtmp.h"
88
89 #define IND (FFB_VB_RGBA_BIT)
90 #define TAG(x) x##_g
91 #include "ffb_vbtmp.h"
92
93 #define IND (FFB_VB_XYZ_BIT | FFB_VB_RGBA_BIT)
94 #define TAG(x) x##_wg
95 #include "ffb_vbtmp.h"
96
97 #define IND (FFB_VB_TWOSIDE_BIT)
98 #define TAG(x) x##_t
99 #include "ffb_vbtmp.h"
100
101 #define IND (FFB_VB_XYZ_BIT | FFB_VB_TWOSIDE_BIT)
102 #define TAG(x) x##_wt
103 #include "ffb_vbtmp.h"
104
105 #define IND (FFB_VB_RGBA_BIT | FFB_VB_TWOSIDE_BIT)
106 #define TAG(x) x##_gt
107 #include "ffb_vbtmp.h"
108
109 #define IND (FFB_VB_XYZ_BIT | FFB_VB_RGBA_BIT | FFB_VB_TWOSIDE_BIT)
110 #define TAG(x) x##_wgt
111 #include "ffb_vbtmp.h"
112
113 static void init_setup_tab( void )
114 {
115 init_w();
116 init_g();
117 init_wg();
118 init_t();
119 init_wt();
120 init_gt();
121 init_wgt();
122 }
123
124 #ifdef VB_DEBUG
125 static void ffbPrintSetupFlags(char *msg, GLuint flags)
126 {
127 fprintf(stderr, "%s(%x): %s%s%s\n",
128 msg,
129 (int)flags,
130 (flags & FFB_VB_XYZ_BIT) ? " xyz," : "",
131 (flags & FFB_VB_RGBA_BIT) ? " rgba," : "",
132 (flags & FFB_VB_TWOSIDE_BIT) ? " twoside," : "");
133 }
134 #endif
135
136 static void ffbDDBuildVertices(GLcontext *ctx, GLuint start, GLuint count,
137 GLuint newinputs)
138 {
139 ffbContextPtr fmesa = FFB_CONTEXT(ctx);
140
141 newinputs |= fmesa->setupnewinputs;
142 fmesa->setupnewinputs = 0;
143
144 if (!newinputs)
145 return;
146
147 if (newinputs & VERT_BIT_POS) {
148 setup_tab[fmesa->setupindex].emit(ctx, start, count);
149 } else {
150 GLuint ind = 0;
151
152 if (newinputs & VERT_BIT_COLOR0)
153 ind |= (FFB_VB_RGBA_BIT | FFB_VB_TWOSIDE_BIT);
154
155 ind &= fmesa->setupindex;
156
157 if (ind)
158 setup_tab[ind].emit(ctx, start, count);
159 }
160 }
161
162 void ffbChooseVertexState( GLcontext *ctx )
163 {
164 TNLcontext *tnl = TNL_CONTEXT(ctx);
165 ffbContextPtr fmesa = FFB_CONTEXT(ctx);
166 int ind = FFB_VB_XYZ_BIT | FFB_VB_RGBA_BIT;
167
168 if (ctx->_TriangleCaps & DD_TRI_LIGHT_TWOSIDE)
169 ind |= FFB_VB_TWOSIDE_BIT;
170
171 #ifdef VB_DEBUG
172 ffbPrintSetupFlags("ffb: full setup function", ind);
173 #endif
174
175 fmesa->setupindex = ind;
176
177 tnl->Driver.Render.BuildVertices = ffbDDBuildVertices;
178 tnl->Driver.Render.Interp = setup_tab[ind].interp;
179 if (ind & FFB_VB_TWOSIDE_BIT)
180 tnl->Driver.Render.CopyPV = ffb_copy_pv_twoside;
181 else
182 tnl->Driver.Render.CopyPV = ffb_copy_pv_oneside;
183 }
184
185 void ffbInitVB( GLcontext *ctx )
186 {
187 ffbContextPtr fmesa = FFB_CONTEXT(ctx);
188 GLuint size = TNL_CONTEXT(ctx)->vb.Size;
189
190 fmesa->verts = (ffb_vertex *)ALIGN_MALLOC(size * sizeof(ffb_vertex), 32);
191
192 {
193 static int firsttime = 1;
194 if (firsttime) {
195 init_setup_tab();
196 firsttime = 0;
197 }
198 }
199 }
200
201
202 void ffbFreeVB( GLcontext *ctx )
203 {
204 ffbContextPtr fmesa = FFB_CONTEXT(ctx);
205 if (fmesa->verts) {
206 ALIGN_FREE(fmesa->verts);
207 fmesa->verts = 0;
208 }
209 }