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