Slight cleanup
[mesa.git] / src / mesa / tnl / t_save_playback.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 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 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 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 = node->count;
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] = 0;
109 VB->IndexPtr[0] = VB->AttribPtr[_TNL_ATTRIB_INDEX];
110 VB->IndexPtr[1] = 0;
111 VB->SecondaryColorPtr[0] = VB->AttribPtr[_TNL_ATTRIB_COLOR1];
112 VB->SecondaryColorPtr[1] = 0;
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 struct tnl_vertex_list *node )
122 {
123 TNLcontext *tnl = TNL_CONTEXT(ctx);
124 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 ASSIGN_4V(tnl->vtx.current[i], 0, 0, 0, 1);
135 COPY_SZ_4V(tnl->vtx.current[i], node->attrsz[i], data);
136 data += node->attrsz[i];
137 }
138 }
139
140 /* Edgeflag requires special treatment:
141 */
142 if (node->attrsz[_TNL_ATTRIB_EDGEFLAG]) {
143 ctx->Current.EdgeFlag = (data[0] == 1.0);
144 }
145
146 /* Colormaterial -- this kindof sucks.
147 */
148 if (ctx->Light.ColorMaterialEnabled) {
149 _mesa_update_color_material(ctx, ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
150 }
151
152 if (node->have_materials) {
153 tnl->Driver.NotifyMaterialChange( ctx );
154 }
155
156 /* CurrentExecPrimitive
157 */
158 if (node->prim_count) {
159 GLenum mode = node->prim[node->prim_count - 1].mode;
160 if (mode & PRIM_END)
161 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
162 else
163 ctx->Driver.CurrentExecPrimitive = (mode & PRIM_MODE_MASK);
164 }
165 }
166
167
168 /**
169 * Execute the buffer and save copied verts.
170 */
171 void _tnl_playback_vertex_list( GLcontext *ctx, void *data )
172 {
173 struct tnl_vertex_list *node = (struct tnl_vertex_list *)data;
174 TNLcontext *tnl = TNL_CONTEXT(ctx);
175
176 FLUSH_CURRENT(ctx, 0);
177
178 if (node->prim_count &&
179 node->count) {
180
181 if (ctx->Driver.CurrentExecPrimitive != PRIM_OUTSIDE_BEGIN_END &&
182 (node->prim[0].mode & PRIM_BEGIN)) {
183
184 /* Degenerate case: list is called inside begin/end pair and
185 * includes operations such as glBegin or glDrawArrays.
186 */
187 _mesa_error( ctx, GL_INVALID_OPERATION, "displaylist recursive begin");
188 _tnl_loopback_vertex_list( ctx, (struct tnl_vertex_list *) data );
189 return;
190 }
191 else if (tnl->LoopbackDListCassettes ||
192 node->dangling_attr_ref) {
193 /* Degenerate case: list references current data and would
194 * require fixup. Take the easier option & loop it back.
195 */
196 _tnl_loopback_vertex_list( ctx, (struct tnl_vertex_list *) data );
197 return;
198 }
199
200 if (ctx->NewState)
201 _mesa_update_state( ctx );
202
203 if (tnl->pipeline.build_state_changes)
204 _tnl_validate_pipeline( ctx );
205
206 _tnl_bind_vertex_list( ctx, node );
207
208 /* Invalidate all stored data before and after run:
209 */
210 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
211 tnl->Driver.RunPipeline( ctx );
212 tnl->pipeline.run_input_changes |= tnl->pipeline.inputs;
213 }
214
215 /* Copy to current?
216 */
217 _playback_copy_to_current( ctx, node );
218 }
219
220
221
222
223