Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / swrast_setup / ss_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "main/glheader.h"
29 #include "main/imports.h"
30 #include "main/colormac.h"
31 #include "tnl/tnl.h"
32 #include "tnl/t_context.h"
33 #include "tnl/t_pipeline.h"
34 #include "tnl/t_vertex.h"
35 #include "swrast_setup.h"
36 #include "ss_context.h"
37 #include "ss_triangle.h"
38
39
40 /* Need to check lighting state and vertex program state to know
41 * if two-sided lighting is in effect.
42 */
43 #define _SWSETUP_NEW_RENDERINDEX (_NEW_POLYGON|_NEW_LIGHT|_NEW_PROGRAM)
44
45
46 #define VARYING_EMIT_STYLE EMIT_4F
47
48
49 GLboolean
50 _swsetup_CreateContext( GLcontext *ctx )
51 {
52 SScontext *swsetup = (SScontext *)CALLOC(sizeof(SScontext));
53
54 if (!swsetup)
55 return GL_FALSE;
56
57 ctx->swsetup_context = swsetup;
58
59 swsetup->NewState = ~0;
60 _swsetup_trifuncs_init( ctx );
61
62 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
63 sizeof(SWvertex) );
64
65
66 return GL_TRUE;
67 }
68
69 void
70 _swsetup_DestroyContext( GLcontext *ctx )
71 {
72 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
73
74 if (swsetup) {
75 FREE(swsetup);
76 ctx->swsetup_context = 0;
77 }
78
79 _tnl_free_vertices( ctx );
80 }
81
82 static void
83 _swsetup_RenderPrimitive( GLcontext *ctx, GLenum mode )
84 {
85 SWSETUP_CONTEXT(ctx)->render_prim = mode;
86 _swrast_render_primitive( ctx, mode );
87 }
88
89
90 /**
91 * Helper macros for setup_vertex_format()
92 */
93 #define SWZ ((SWvertex *)0)
94 #define SWOffset(MEMBER) (((char *)&(SWZ->MEMBER)) - ((char *)SWZ))
95
96 #define EMIT_ATTR( ATTR, STYLE, MEMBER ) \
97 do { \
98 map[e].attrib = (ATTR); \
99 map[e].format = (STYLE); \
100 map[e].offset = SWOffset(MEMBER); \
101 e++; \
102 } while (0)
103
104
105 /**
106 * Tell the tnl module how to build SWvertex objects for swrast.
107 * We'll build the map[] array with that info and pass it to
108 * _tnl_install_attrs().
109 */
110 static void
111 setup_vertex_format(GLcontext *ctx)
112 {
113 TNLcontext *tnl = TNL_CONTEXT(ctx);
114 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
115 GLboolean intColors = !ctx->FragmentProgram._Current
116 && !ctx->ATIFragmentShader._Enabled
117 && ctx->RenderMode == GL_RENDER
118 && CHAN_TYPE == GL_UNSIGNED_BYTE;
119
120 if (intColors != swsetup->intColors ||
121 !RENDERINPUTS_EQUAL(tnl->render_inputs_bitset,
122 swsetup->last_index_bitset)) {
123 DECLARE_RENDERINPUTS(index_bitset);
124 struct tnl_attr_map map[_TNL_ATTRIB_MAX];
125 int i, e = 0;
126
127 swsetup->intColors = intColors;
128
129 RENDERINPUTS_COPY( index_bitset, tnl->render_inputs_bitset );
130
131 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, attrib[FRAG_ATTRIB_WPOS] );
132
133 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR0 )) {
134 if (swsetup->intColors)
135 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4CHAN_4F_RGBA, color );
136 else
137 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4F, attrib[FRAG_ATTRIB_COL0]);
138 }
139
140 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 )) {
141 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_4F, attrib[FRAG_ATTRIB_COL1]);
142 }
143
144 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR_INDEX )) {
145 EMIT_ATTR( _TNL_ATTRIB_COLOR_INDEX, EMIT_1F,
146 attrib[FRAG_ATTRIB_CI][0] );
147 }
148
149 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_FOG )) {
150 const GLint emit = ctx->FragmentProgram._Current ? EMIT_4F : EMIT_1F;
151 EMIT_ATTR( _TNL_ATTRIB_FOG, emit, attrib[FRAG_ATTRIB_FOGC]);
152 }
153
154 if (RENDERINPUTS_TEST_RANGE(index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX))
155 {
156 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
157 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
158 EMIT_ATTR( _TNL_ATTRIB_TEX(i), EMIT_4F,
159 attrib[FRAG_ATTRIB_TEX0 + i] );
160 }
161 }
162 }
163
164 /* shader varying vars */
165 if (RENDERINPUTS_TEST_RANGE( index_bitset,
166 _TNL_FIRST_GENERIC, _TNL_LAST_GENERIC )) {
167 for (i = 0; i < ctx->Const.MaxVarying; i++) {
168 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_GENERIC(i) )) {
169 EMIT_ATTR( _TNL_ATTRIB_GENERIC(i), VARYING_EMIT_STYLE,
170 attrib[FRAG_ATTRIB_VAR0 + i] );
171 }
172 }
173 }
174
175 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POINTSIZE ))
176 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F, pointSize );
177
178 _tnl_install_attrs( ctx, map, e,
179 ctx->Viewport._WindowMap.m,
180 sizeof(SWvertex) );
181
182 RENDERINPUTS_COPY( swsetup->last_index_bitset, index_bitset );
183 }
184 }
185
186
187 /**
188 * Prepare to render a vertex buffer.
189 * Called via tnl->Driver.Render.Start.
190 */
191 static void
192 _swsetup_RenderStart( GLcontext *ctx )
193 {
194 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
195 TNLcontext *tnl = TNL_CONTEXT(ctx);
196 struct vertex_buffer *VB = &tnl->vb;
197
198 if (swsetup->NewState & _SWSETUP_NEW_RENDERINDEX) {
199 _swsetup_choose_trifuncs(ctx);
200 }
201
202 if (swsetup->NewState & _NEW_PROGRAM) {
203 RENDERINPUTS_ZERO( swsetup->last_index_bitset );
204 }
205
206 swsetup->NewState = 0;
207
208 /* This will change if drawing unfilled tris */
209 _swrast_SetFacing(ctx, 0);
210
211 _swrast_render_start(ctx);
212
213 /* Important */
214 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
215
216 setup_vertex_format(ctx);
217 }
218
219
220 /*
221 * We patch this function into tnl->Driver.Render.Finish.
222 * It's called when we finish rendering a vertex buffer.
223 */
224 static void
225 _swsetup_RenderFinish( GLcontext *ctx )
226 {
227 _swrast_render_finish( ctx );
228 }
229
230 void
231 _swsetup_InvalidateState( GLcontext *ctx, GLuint new_state )
232 {
233 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
234 swsetup->NewState |= new_state;
235 _tnl_invalidate_vertex_state( ctx, new_state );
236 }
237
238
239 void
240 _swsetup_Wakeup( GLcontext *ctx )
241 {
242 TNLcontext *tnl = TNL_CONTEXT(ctx);
243 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
244
245 tnl->Driver.Render.Start = _swsetup_RenderStart;
246 tnl->Driver.Render.Finish = _swsetup_RenderFinish;
247 tnl->Driver.Render.PrimitiveNotify = _swsetup_RenderPrimitive;
248 tnl->Driver.Render.Interp = _tnl_interp;
249 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
250 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon; /* new */
251 tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine; /* new */
252 /* points */
253 /* line */
254 /* triangle */
255 /* quad */
256 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
257 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
258 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
259 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
260 tnl->Driver.Render.Multipass = 0;
261
262 _tnl_invalidate_vertices( ctx, ~0 );
263 _tnl_need_projected_coords( ctx, GL_TRUE );
264 _swsetup_InvalidateState( ctx, ~0 );
265
266 swsetup->verts = (SWvertex *)tnl->clipspace.vertex_buf;
267 RENDERINPUTS_ZERO( swsetup->last_index_bitset );
268 }
269
270
271 /**
272 * Populate a swrast SWvertex from an attrib-style vertex.
273 */
274 void
275 _swsetup_Translate( GLcontext *ctx, const void *vertex, SWvertex *dest )
276 {
277 const GLfloat *m = ctx->Viewport._WindowMap.m;
278 GLfloat tmp[4];
279 GLuint i;
280
281 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POS, tmp );
282
283 dest->attrib[FRAG_ATTRIB_WPOS][0] = m[0] * tmp[0] + m[12];
284 dest->attrib[FRAG_ATTRIB_WPOS][1] = m[5] * tmp[1] + m[13];
285 dest->attrib[FRAG_ATTRIB_WPOS][2] = m[10] * tmp[2] + m[14];
286 dest->attrib[FRAG_ATTRIB_WPOS][3] = tmp[3];
287
288 /** XXX try to limit these loops someday */
289 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
290 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_TEX0 + i,
291 dest->attrib[FRAG_ATTRIB_TEX0 + i] );
292
293 for (i = 0 ; i < ctx->Const.MaxVarying ; i++)
294 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_GENERIC0 + i,
295 dest->attrib[FRAG_ATTRIB_VAR0 + i] );
296
297 if (ctx->Visual.rgbMode) {
298 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR0,
299 dest->attrib[FRAG_ATTRIB_COL0] );
300 UNCLAMPED_FLOAT_TO_RGBA_CHAN( dest->color, tmp );
301
302 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR1,
303 dest->attrib[FRAG_ATTRIB_COL1]);
304 }
305 else {
306 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR_INDEX, tmp );
307 dest->attrib[FRAG_ATTRIB_CI][0] = tmp[0];
308 }
309
310 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_FOG, tmp );
311 dest->attrib[FRAG_ATTRIB_FOGC][0] = tmp[0];
312
313 /* XXX See _tnl_get_attr about pointsize ... */
314 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POINTSIZE, tmp );
315 dest->pointSize = tmp[0];
316 }
317