Re-commit t_vertex.[ch] changes to fd.o server.
[mesa.git] / src / mesa / swrast_setup / ss_context.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 4.1
5 *
6 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29 #include "glheader.h"
30 #include "imports.h"
31 #include "colormac.h"
32 #include "ss_context.h"
33 #include "ss_triangle.h"
34 #include "ss_vb.h"
35 #include "swrast_setup.h"
36 #include "tnl/tnl.h"
37 #include "tnl/t_context.h"
38 #include "tnl/t_pipeline.h"
39 #include "tnl/t_vertex.h"
40
41
42 #define _SWSETUP_NEW_VERTS (_NEW_RENDERMODE| \
43 _NEW_POLYGON| \
44 _NEW_LIGHT| \
45 _NEW_TEXTURE| \
46 _NEW_COLOR| \
47 _NEW_FOG| \
48 _NEW_POINT)
49
50 #define _SWSETUP_NEW_RENDERINDEX (_NEW_POLYGON|_NEW_LIGHT)
51
52
53 GLboolean
54 _swsetup_CreateContext( GLcontext *ctx )
55 {
56 TNLcontext *tnl = TNL_CONTEXT(ctx);
57 SScontext *swsetup = (SScontext *)CALLOC(sizeof(SScontext));
58
59 if (!swsetup)
60 return GL_FALSE;
61
62 swsetup->verts = (SWvertex *) ALIGN_CALLOC( sizeof(SWvertex) * tnl->vb.Size,
63 32);
64 if (!swsetup->verts) {
65 FREE(swsetup);
66 return GL_FALSE;
67 }
68
69 ctx->swsetup_context = swsetup;
70
71 swsetup->NewState = ~0;
72 _swsetup_vb_init( ctx );
73 _swsetup_trifuncs_init( ctx );
74
75 return GL_TRUE;
76 }
77
78 void
79 _swsetup_DestroyContext( GLcontext *ctx )
80 {
81 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
82
83 if (swsetup) {
84 if (swsetup->verts)
85 ALIGN_FREE(swsetup->verts);
86
87 if (swsetup->ChanSecondaryColor.Ptr)
88 ALIGN_FREE((void *) swsetup->ChanSecondaryColor.Ptr);
89
90 if (swsetup->ChanColor.Ptr)
91 ALIGN_FREE((void *) swsetup->ChanColor.Ptr);
92
93 FREE(swsetup);
94 ctx->swsetup_context = 0;
95 }
96 }
97
98 static void
99 _swsetup_RenderPrimitive( GLcontext *ctx, GLenum mode )
100 {
101 SWSETUP_CONTEXT(ctx)->render_prim = mode;
102 _swrast_render_primitive( ctx, mode );
103 }
104
105 /*
106 * We patch this function into tnl->Driver.Render.Start.
107 * It's called when we start rendering a vertex buffer.
108 */
109 static void
110 _swsetup_RenderStart( GLcontext *ctx )
111 {
112 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
113 GLuint new_state = swsetup->NewState;
114
115 if (new_state & _SWSETUP_NEW_RENDERINDEX) {
116 _swsetup_choose_trifuncs( ctx );
117 }
118
119 if (new_state & _SWSETUP_NEW_VERTS) {
120 _swsetup_choose_rastersetup_func( ctx );
121 }
122
123 swsetup->NewState = 0;
124
125 _swrast_render_start( ctx );
126 }
127
128 /*
129 * We patch this function into tnl->Driver.Render.Finish.
130 * It's called when we finish rendering a vertex buffer.
131 */
132 static void
133 _swsetup_RenderFinish( GLcontext *ctx )
134 {
135 _swrast_render_finish( ctx );
136 }
137
138 void
139 _swsetup_InvalidateState( GLcontext *ctx, GLuint new_state )
140 {
141 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
142 swsetup->NewState |= new_state;
143 }
144
145
146 void
147 _swsetup_Wakeup( GLcontext *ctx )
148 {
149 TNLcontext *tnl = TNL_CONTEXT(ctx);
150 tnl->Driver.Render.Start = _swsetup_RenderStart;
151 tnl->Driver.Render.Finish = _swsetup_RenderFinish;
152 tnl->Driver.Render.PrimitiveNotify = _swsetup_RenderPrimitive;
153 /* interp */
154 /* copypv */
155 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon; /* new */
156 tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine; /* new */
157 /* points */
158 /* line */
159 /* triangle */
160 /* quad */
161 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
162 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
163 tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
164 /* buildvertices */
165 tnl->Driver.Render.Multipass = 0;
166 _tnl_need_projected_coords( ctx, GL_TRUE );
167 _swsetup_InvalidateState( ctx, ~0 );
168 }
169
170
171
172
173
174 /* Populate a swrast SWvertex from an attrib-style vertex.
175 */
176 void
177 _swsetup_Translate( GLcontext *ctx, const void *vertex, SWvertex *dest )
178 {
179 GLfloat tmp[4];
180 GLint i;
181
182 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POS, dest->win );
183
184 for (i = 0 ; i < ctx->Const.MaxTextureUnits ; i++)
185 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_TEX0+i, dest->texcoord[i] );
186
187 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR0, tmp );
188 UNCLAMPED_FLOAT_TO_RGBA_CHAN( dest->color, tmp );
189
190 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR1, tmp );
191 UNCLAMPED_FLOAT_TO_RGB_CHAN( dest->specular, tmp );
192
193 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_FOG, tmp );
194 dest->fog = tmp[0];
195
196 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_INDEX, tmp );
197 dest->index = (GLuint) tmp[0];
198
199 /*
200 Need to check how pointsize is related to vertex program attributes:
201
202 _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POINTSIZE, tmp );
203 dest->pointSize = tmp[0];
204 */
205 }
206