varying var changes
[mesa.git] / src / mesa / swrast_setup / ss_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 "glheader.h"
29 #include "imports.h"
30 #include "colormac.h"
31 #include "ss_context.h"
32 #include "ss_triangle.h"
33 #include "swrast_setup.h"
34 #include "tnl/tnl.h"
35 #include "tnl/t_context.h"
36 #include "tnl/t_pipeline.h"
37 #include "tnl/t_vertex.h"
38
39 /* Need to check lighting state and vertex program state to know
40 * if two-sided lighting is in effect.
41 */
42 #define _SWSETUP_NEW_RENDERINDEX (_NEW_POLYGON|_NEW_LIGHT|_NEW_PROGRAM)
43
44
45 #define VARYING_EMIT_STYLE EMIT_4F
46
47
48 GLboolean
49 _swsetup_CreateContext( GLcontext *ctx )
50 {
51 SScontext *swsetup = (SScontext *)CALLOC(sizeof(SScontext));
52
53 if (!swsetup)
54 return GL_FALSE;
55
56 ctx->swsetup_context = swsetup;
57
58 swsetup->NewState = ~0;
59 _swsetup_trifuncs_init( ctx );
60
61 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
62 sizeof(SWvertex) );
63
64
65 return GL_TRUE;
66 }
67
68 void
69 _swsetup_DestroyContext( GLcontext *ctx )
70 {
71 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
72
73 if (swsetup) {
74 FREE(swsetup);
75 ctx->swsetup_context = 0;
76 }
77
78 _tnl_free_vertices( ctx );
79 }
80
81 static void
82 _swsetup_RenderPrimitive( GLcontext *ctx, GLenum mode )
83 {
84 SWSETUP_CONTEXT(ctx)->render_prim = mode;
85 _swrast_render_primitive( ctx, mode );
86 }
87
88 #define SWZ ((SWvertex *)0)
89 #define SWOffset(MEMBER) (((char *)&(SWZ->MEMBER)) - ((char *)SWZ))
90
91 #define EMIT_ATTR( ATTR, STYLE, MEMBER ) \
92 do { \
93 map[e].attrib = (ATTR); \
94 map[e].format = (STYLE); \
95 map[e].offset = SWOffset(MEMBER); \
96 e++; \
97 } while (0)
98
99 /*
100 * We patch this function into tnl->Driver.Render.Start.
101 * It's called when we start rendering a vertex buffer.
102 */
103 static void
104 _swsetup_RenderStart( GLcontext *ctx )
105 {
106 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
107 TNLcontext *tnl = TNL_CONTEXT(ctx);
108 struct vertex_buffer *VB = &tnl->vb;
109 GLuint new_state = swsetup->NewState;
110
111 if (new_state & _SWSETUP_NEW_RENDERINDEX) {
112 _swsetup_choose_trifuncs( ctx );
113 }
114
115 swsetup->NewState = 0;
116
117 _swrast_render_start( ctx );
118
119 /* Important:
120 */
121 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
122
123
124 if (!RENDERINPUTS_EQUAL(tnl->render_inputs_bitset, swsetup->last_index_bitset)) {
125 DECLARE_RENDERINPUTS(index_bitset);
126 struct tnl_attr_map map[_TNL_ATTRIB_MAX];
127 int i, e = 0;
128
129 RENDERINPUTS_COPY( index_bitset, tnl->render_inputs_bitset );
130
131 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, win );
132
133 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR0 ))
134 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4CHAN_4F_RGBA, color );
135
136 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 ))
137 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_4CHAN_4F_RGBA, specular);
138
139 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR_INDEX ))
140 EMIT_ATTR( _TNL_ATTRIB_COLOR_INDEX, EMIT_1F, index );
141
142 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_FOG ))
143 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1F, fog);
144
145 if (RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
146 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
147 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
148 EMIT_ATTR( _TNL_ATTRIB_TEX(i), EMIT_4F, texcoord[i] );
149 }
150 }
151 }
152
153 /* shader varying vars */
154 if (RENDERINPUTS_TEST_RANGE( index_bitset,
155 _TNL_FIRST_GENERIC, _TNL_LAST_GENERIC )) {
156 for (i = 0; i < ctx->Const.MaxVarying; i++) {
157 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_GENERIC(i) )) {
158 EMIT_ATTR( _TNL_ATTRIB_GENERIC(i), VARYING_EMIT_STYLE,
159 attribute[i] );
160 }
161 }
162 }
163
164 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POINTSIZE ))
165 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F, pointSize );
166
167 _tnl_install_attrs( ctx, map, e,
168 ctx->Viewport._WindowMap.m,
169 sizeof(SWvertex) );
170
171 RENDERINPUTS_COPY( swsetup->last_index_bitset, index_bitset );
172 }
173 }
174
175 /*
176 * We patch this function into tnl->Driver.Render.Finish.
177 * It's called when we finish rendering a vertex buffer.
178 */
179 static void
180 _swsetup_RenderFinish( GLcontext *ctx )
181 {
182 _swrast_render_finish( ctx );
183 }
184
185 void
186 _swsetup_InvalidateState( GLcontext *ctx, GLuint new_state )
187 {
188 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
189 swsetup->NewState |= new_state;
190 _tnl_invalidate_vertex_state( ctx, new_state );
191 }
192
193
194 void
195 _swsetup_Wakeup( GLcontext *ctx )
196 {
197 TNLcontext *tnl = TNL_CONTEXT(ctx);
198 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
199
200 tnl->Driver.Render.Start = _swsetup_RenderStart;
201 tnl->Driver.Render.Finish = _swsetup_RenderFinish;
202 tnl->Driver.Render.PrimitiveNotify = _swsetup_RenderPrimitive;
203 tnl->Driver.Render.Interp = _tnl_interp;
204 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
205 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon; /* new */
206 tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine; /* new */
207 /* points */
208 /* line */
209 /* triangle */
210 /* quad */
211 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
212 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
213 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
214 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
215 tnl->Driver.Render.Multipass = 0;
216
217 _tnl_invalidate_vertices( ctx, ~0 );
218 _tnl_need_projected_coords( ctx, GL_TRUE );
219 _swsetup_InvalidateState( ctx, ~0 );
220
221 swsetup->verts = (SWvertex *)tnl->clipspace.vertex_buf;
222 RENDERINPUTS_ZERO( swsetup->last_index_bitset );
223 }
224
225
226 /**
227 * Populate a swrast SWvertex from an attrib-style vertex.
228 */
229 void
230 _swsetup_Translate( GLcontext *ctx, const void *vertex, SWvertex *dest )
231 {
232 const GLfloat *m = ctx->Viewport._WindowMap.m;
233 GLfloat tmp[4];
234 GLuint i;
235
236 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POS, tmp );
237
238 dest->win[0] = m[0] * tmp[0] + m[12];
239 dest->win[1] = m[5] * tmp[1] + m[13];
240 dest->win[2] = m[10] * tmp[2] + m[14];
241 dest->win[3] = tmp[3];
242
243
244 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
245 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_TEX0+i, dest->texcoord[i] );
246
247 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR0, tmp );
248 UNCLAMPED_FLOAT_TO_RGBA_CHAN( dest->color, tmp );
249
250 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR1, tmp );
251 UNCLAMPED_FLOAT_TO_RGBA_CHAN( dest->specular, tmp );
252
253 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_FOG, tmp );
254 dest->fog = tmp[0];
255
256 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR_INDEX, tmp );
257 dest->index = tmp[0];
258
259 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POINTSIZE, tmp );
260 dest->pointSize = tmp[0];
261 }
262