Simplify the pipeline_stage structure
[mesa.git] / src / mesa / tnl / t_save_playback.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.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
25 /* Author:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29 #include "glheader.h"
30 #include "context.h"
31 #include "imports.h"
32 #include "mtypes.h"
33 #include "macros.h"
34 #include "light.h"
35 #include "state.h"
36 #include "t_pipeline.h"
37 #include "t_save_api.h"
38 #include "t_vtx_api.h"
39
40 static INLINE GLint get_size( const GLfloat *f )
41 {
42 if (f[3] != 1.0) return 4;
43 if (f[2] != 0.0) return 3;
44 return 2;
45 }
46
47
48 /* Some nasty stuff still hanging on here.
49 *
50 * TODO - remove VB->ColorPtr, etc and just use the AttrPtr's.
51 */
52 static void _tnl_bind_vertex_list( GLcontext *ctx,
53 const struct tnl_vertex_list *node )
54 {
55 TNLcontext *tnl = TNL_CONTEXT(ctx);
56 struct vertex_buffer *VB = &tnl->vb;
57 struct tnl_vertex_arrays *tmp = &tnl->save_inputs;
58 GLfloat *data = node->buffer;
59 GLuint attr, i;
60
61 /* Setup constant data in the VB.
62 */
63 VB->Count = node->count;
64 VB->Primitive = node->prim;
65 VB->PrimitiveCount = node->prim_count;
66 VB->Elts = NULL;
67 VB->NormalLengthPtr = node->normal_lengths;
68
69 for (attr = 0; attr <= _TNL_ATTRIB_INDEX; attr++) {
70 if (node->attrsz[attr]) {
71 tmp->Attribs[attr].count = node->count;
72 tmp->Attribs[attr].data = (GLfloat (*)[4]) data;
73 tmp->Attribs[attr].start = data;
74 tmp->Attribs[attr].size = node->attrsz[attr];
75 tmp->Attribs[attr].stride = node->vertex_size * sizeof(GLfloat);
76 VB->AttribPtr[attr] = &tmp->Attribs[attr];
77 data += node->attrsz[attr];
78 }
79 else {
80 tmp->Attribs[attr].count = 1;
81 tmp->Attribs[attr].data = (GLfloat (*)[4]) tnl->vtx.current[attr];
82 tmp->Attribs[attr].start = tnl->vtx.current[attr];
83 tmp->Attribs[attr].size = get_size( tnl->vtx.current[attr] );
84 tmp->Attribs[attr].stride = 0;
85 VB->AttribPtr[attr] = &tmp->Attribs[attr];
86 }
87 }
88
89
90 /* Copy edgeflag to a contiguous array
91 */
92 if (ctx->Polygon.FrontMode != GL_FILL || ctx->Polygon.BackMode != GL_FILL) {
93 if (node->attrsz[_TNL_ATTRIB_EDGEFLAG]) {
94 VB->EdgeFlag = _tnl_translate_edgeflag( ctx, data,
95 node->count,
96 node->vertex_size );
97 data++;
98 }
99 else
100 VB->EdgeFlag = _tnl_import_current_edgeflag( ctx, node->count );
101 }
102
103 /* Legacy pointers -- remove one day.
104 */
105 VB->ObjPtr = VB->AttribPtr[_TNL_ATTRIB_POS];
106 VB->NormalPtr = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
107 VB->ColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
108 VB->ColorPtr[1] = NULL;
109 VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_INDEX];
110 VB->IndexPtr[1] = NULL;
111 VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
112 VB->SecondaryColorPtr[1] = NULL;
113 VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG];
114
115 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
116 VB->TexCoordPtr[i] = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i];
117 }
118 }
119
120 static void _playback_copy_to_current( GLcontext *ctx,
121 const struct tnl_vertex_list *node )
122 {
123 TNLcontext *tnl = TNL_CONTEXT(ctx);
124 const GLfloat *data;
125 GLuint i;
126
127 if (node->count)
128 data = node->buffer + (node->count-1) * node->vertex_size;
129 else
130 data = node->buffer;
131
132 for (i = _TNL_ATTRIB_POS+1 ; i <= _TNL_ATTRIB_INDEX ; i++) {
133 if (node->attrsz[i]) {
134 COPY_CLEAN_4V(tnl->vtx.current[i], node->attrsz[i], data);
135 data += node->attrsz[i];
136 }
137 }
138
139 /* Edgeflag requires special treatment:
140 */
141 if (node->attrsz[_TNL_ATTRIB_EDGEFLAG]) {
142 ctx->Current.EdgeFlag = (data[0] == 1.0);
143 }
144
145 /* Colormaterial -- this kindof sucks.
146 */
147 if (ctx->Light.ColorMaterialEnabled) {
148 _mesa_update_color_material(ctx, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
149 }
150
151 if (node->have_materials) {
152 tnl->Driver.NotifyMaterialChange( ctx );
153 }
154
155 /* CurrentExecPrimitive
156 */
157 if (node->prim_count) {
158 GLenum mode = node->prim[node->prim_count - 1].mode;
159 if (mode & PRIM_END)
160 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
161 else
162 ctx->Driver.CurrentExecPrimitive = (mode & PRIM_MODE_MASK);
163 }
164 }
165
166
167 /**
168 * Execute the buffer and save copied verts.
169 */
170 void _tnl_playback_vertex_list( GLcontext *ctx, void *data )
171 {
172 const struct tnl_vertex_list *node = (const struct tnl_vertex_list *) data;
173 TNLcontext *tnl = TNL_CONTEXT(ctx);
174
175 FLUSH_CURRENT(ctx, 0);
176
177 if (node->prim_count > 0 && node->count > 0) {
178
179 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
180 (node->prim[0].mode & PRIM_BEGIN)) {
181
182 /* Degenerate case: list is called inside begin/end pair and
183 * includes operations such as glBegin or glDrawArrays.
184 */
185 _mesa_error( ctx, GL_INVALID_OPERATION, "displaylist recursive begin");
186 _tnl_loopback_vertex_list( ctx, node );
187 return;
188 }
189 else if (tnl->save.replay_flags) {
190 /* Various degnerate cases: translate into immediate mode
191 * calls rather than trying to execute in place.
192 */
193 _tnl_loopback_vertex_list( ctx, node );
194 return;
195 }
196
197 if (ctx->NewState)
198 _mesa_update_state( ctx );
199
200 if ((ctx->VertexProgram.Enabled && !ctx->VertexProgram._Enabled) ||
201 (ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled)) {
202 _mesa_error(ctx, GL_INVALID_OPERATION,
203 "glBegin (invalid vertex/fragment program)");
204 return;
205 }
206
207 _tnl_bind_vertex_list( ctx, node );
208
209 tnl->Driver.RunPipeline( ctx );
210 }
211
212 /* Copy to current?
213 */
214 _playback_copy_to_current( ctx, node );
215 }