Fixed conform problems with recent material tracking change.
[mesa.git] / src / mesa / tnl / t_imm_dlist.c
1 /* $Id: t_imm_dlist.c,v 1.9 2001/02/16 00:35:35 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Author:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30
31 #include "glheader.h"
32 #include "context.h"
33 #include "dlist.h"
34 #include "debug.h"
35 #include "mmath.h"
36 #include "mem.h"
37 #include "state.h"
38
39 #include "t_context.h"
40 #include "t_imm_api.h"
41 #include "t_imm_elt.h"
42 #include "t_imm_alloc.h"
43 #include "t_imm_dlist.h"
44 #include "t_imm_debug.h"
45 #include "t_imm_exec.h"
46 #include "t_imm_fixup.h"
47 #include "t_pipeline.h"
48
49 typedef struct {
50 struct immediate *IM;
51 GLuint Start;
52 GLuint Count;
53 GLuint BeginState;
54 GLuint SavedBeginState;
55 GLuint OrFlag;
56 GLuint AndFlag;
57 GLuint TexSize;
58 GLuint LastData;
59 GLuint LastPrimitive;
60 GLuint LastMaterial;
61 GLuint MaterialOrMask;
62 } TNLvertexcassette;
63
64 static void execute_compiled_cassette( GLcontext *ctx, void *data );
65
66
67 /* Insert the active immediate struct onto the display list currently
68 * being built.
69 */
70 void
71 _tnl_compile_cassette( GLcontext *ctx, struct immediate *IM )
72 {
73 struct immediate *im = TNL_CURRENT_IM(ctx);
74 TNLcontext *tnl = TNL_CONTEXT(ctx);
75 TNLvertexcassette *node;
76 GLuint new_beginstate;
77
78
79 _tnl_compute_orflag( IM );
80
81 IM->CopyStart = IM->Start;
82
83 if (IM->OrFlag & VERT_ELT) {
84 GLuint andflag = ~0;
85 GLuint i;
86 GLuint start = IM->FlushElt ? IM->LastPrimitive : IM->CopyStart;
87 _tnl_translate_array_elts( ctx, IM, start, IM->Count );
88
89 /* Need to recompute andflag.
90 */
91 if (IM->AndFlag & VERT_ELT)
92 IM->CopyAndFlag = IM->AndFlag |= ctx->Array._Enabled;
93 else {
94 for (i = IM->CopyStart ; i < IM->Count ; i++)
95 andflag &= IM->Flag[i];
96 IM->CopyAndFlag = IM->AndFlag = andflag;
97 }
98 }
99
100 _tnl_fixup_input( ctx, IM );
101
102 /* Mark the last primitive:
103 */
104 IM->PrimitiveLength[IM->LastPrimitive] = IM->Count - IM->LastPrimitive;
105 ASSERT(IM->Primitive[IM->LastPrimitive] & PRIM_LAST);
106
107
108 node = (TNLvertexcassette *)
109 _mesa_alloc_instruction(ctx,
110 tnl->opcode_vertex_cassette,
111 sizeof(TNLvertexcassette));
112 if (!node)
113 return;
114
115 node->IM = im; im->ref_count++;
116 node->Start = im->Start;
117 node->Count = im->Count;
118 node->BeginState = im->BeginState;
119 node->SavedBeginState = im->SavedBeginState;
120 node->OrFlag = im->OrFlag;
121 node->TexSize = im->TexSize;
122 node->AndFlag = im->AndFlag;
123 node->LastData = im->LastData;
124 node->LastPrimitive = im->LastPrimitive;
125 node->LastMaterial = im->LastMaterial;
126 node->MaterialOrMask = im->MaterialOrMask;
127
128 if (ctx->ExecuteFlag) {
129 execute_compiled_cassette( ctx, (void *)node );
130 }
131
132
133 /* Discard any errors raised in the last cassette.
134 */
135 new_beginstate = node->BeginState & (VERT_BEGIN_0|VERT_BEGIN_1);
136
137 /* Decide whether this immediate struct is full, or can be used for
138 * the next batch of vertices as well.
139 */
140 if (im->Count > IMM_MAXDATA - 16) {
141 /* Call it full...
142 */
143 struct immediate *new_im = _tnl_alloc_immediate(ctx);
144 if (!new_im) return;
145 new_im->ref_count++;
146 im->ref_count--; /* remove CURRENT_IM reference */
147 ASSERT(im->ref_count > 0);
148 SET_IMMEDIATE( ctx, new_im );
149 _tnl_reset_input( ctx, IMM_MAX_COPIED_VERTS,
150 new_beginstate, node->SavedBeginState );
151 } else {
152 /* Still some room in the current immediate.
153 */
154 _tnl_reset_input( ctx, im->Count+1+IMM_MAX_COPIED_VERTS,
155 new_beginstate, node->SavedBeginState);
156 }
157 }
158
159
160 static void
161 execute_compiled_cassette( GLcontext *ctx, void *data )
162 {
163 TNLcontext *tnl = TNL_CONTEXT(ctx);
164 TNLvertexcassette *node = (TNLvertexcassette *)data;
165 struct immediate *IM = node->IM;
166
167 if (ctx->NewState)
168 gl_update_state(ctx);
169
170 if (tnl->pipeline.build_state_changes)
171 _tnl_validate_pipeline( ctx );
172
173 IM->Start = node->Start;
174 IM->CopyStart = node->Start;
175 IM->Count = node->Count;
176 IM->BeginState = node->BeginState;
177 IM->SavedBeginState = node->SavedBeginState;
178 IM->OrFlag = node->OrFlag;
179 IM->TexSize = node->TexSize;
180 IM->AndFlag = node->AndFlag;
181 IM->LastData = node->LastData;
182 IM->LastPrimitive = node->LastPrimitive;
183 IM->LastMaterial = node->LastMaterial;
184 IM->MaterialOrMask = node->MaterialOrMask;
185
186 if ((MESA_VERBOSE & VERBOSE_DISPLAY_LIST) &&
187 (MESA_VERBOSE & VERBOSE_IMMEDIATE))
188 _tnl_print_cassette( IM );
189
190 if (MESA_VERBOSE & VERBOSE_DISPLAY_LIST) {
191 fprintf(stderr, "Run cassette %d, rows %d..%d, beginstate %x ",
192 IM->id,
193 IM->Start, IM->Count, IM->BeginState);
194 _tnl_print_vert_flags("orflag", IM->OrFlag);
195 }
196
197 if (IM->Count == IM->Start) {
198 _tnl_copy_to_current( ctx, IM, IM->OrFlag );
199 return;
200 }
201
202 if (IM->SavedBeginState) {
203 if (ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1)
204 tnl->ReplayHardBeginEnd = 1;
205 if (!tnl->ReplayHardBeginEnd) {
206 /* This is a user error. Whatever operation (like glRectf)
207 * decomposed to this hard begin/end pair is now being run
208 * inside a begin/end object -- illegally. Reject it and
209 * raise an error.
210 */
211 gl_error(ctx, GL_INVALID_OPERATION, "hard replay");
212 return;
213 }
214 }
215
216 _tnl_fixup_compiled_cassette( ctx, IM );
217 _tnl_get_exec_copy_verts( ctx, IM );
218 _tnl_run_cassette( ctx, IM );
219 _tnl_restore_compiled_cassette( ctx, IM );
220
221 if (ctx->Driver.CurrentExecPrimitive == GL_POLYGON+1)
222 tnl->ReplayHardBeginEnd = 0;
223 }
224
225 static void
226 destroy_compiled_cassette( GLcontext *ctx, void *data )
227 {
228 TNLvertexcassette *node = (TNLvertexcassette *)data;
229
230 if ( --node->IM->ref_count == 0 )
231 _tnl_free_immediate( node->IM );
232 }
233
234
235 static void
236 print_compiled_cassette( GLcontext *ctx, void *data )
237 {
238 TNLvertexcassette *node = (TNLvertexcassette *)data;
239 struct immediate *IM = node->IM;
240
241 fprintf(stderr, "TNL-VERTEX-CASSETTE, id %u, rows %u..%u\n",
242 node->IM->id, node->Start, node->Count);
243
244 IM->Start = node->Start;
245 IM->Count = node->Count;
246 IM->BeginState = node->BeginState;
247 IM->OrFlag = node->OrFlag;
248 IM->TexSize = node->TexSize;
249 IM->AndFlag = node->AndFlag;
250 IM->LastData = node->LastData;
251 IM->LastPrimitive = node->LastPrimitive;
252 IM->LastMaterial = node->LastMaterial;
253 IM->MaterialOrMask = node->MaterialOrMask;
254
255 _tnl_print_cassette( node->IM );
256 }
257
258 void
259 _tnl_BeginCallList( GLcontext *ctx, GLuint list )
260 {
261 (void) ctx;
262 (void) list;
263 FLUSH_CURRENT(ctx, 0);
264 }
265
266
267 /* Called at the tail of a CallList. Copy vertices out of the display
268 * list if necessary.
269 */
270 void
271 _tnl_EndCallList( GLcontext *ctx )
272 {
273 /* May have to copy vertices from a dangling begin/end inside the
274 * list to the current immediate.
275 */
276 if (ctx->CallDepth == 0) {
277 TNLcontext *tnl = TNL_CONTEXT(ctx);
278 struct immediate *IM = TNL_CURRENT_IM(ctx);
279
280 if (tnl->ExecCopySource != IM)
281 _tnl_copy_immediate_vertices( ctx, IM );
282 }
283 }
284
285
286 void
287 _tnl_EndList( GLcontext *ctx )
288 {
289 TNLcontext *tnl = TNL_CONTEXT(ctx);
290 struct immediate *IM = TNL_CURRENT_IM(ctx);
291
292 ctx->swtnl_im = 0;
293 IM->ref_count--;
294 if (IM == tnl->ExecCopySource) {
295 IM->ref_count--;
296 } else {
297 if ( --tnl->ExecCopySource->ref_count == 0 )
298 _tnl_free_immediate( tnl->ExecCopySource );
299 }
300
301 /* If this one isn't free, get a clean one. (Otherwise we'll be
302 * using one that's already half full).
303 */
304 if (IM->ref_count != 0)
305 IM = _tnl_alloc_immediate( ctx );
306
307 ASSERT(IM->ref_count == 0);
308
309 tnl->ExecCopySource = IM;
310 IM->ref_count++;
311
312 SET_IMMEDIATE( ctx, IM );
313 IM->ref_count++;
314
315 _tnl_reset_input( ctx, IMM_MAX_COPIED_VERTS, 0, 0 );
316
317 /* outside begin/end, even in COMPILE_AND_EXEC,
318 * so no vertices to copy, right?
319 */
320 ASSERT(TNL_CONTEXT(ctx)->ExecCopyCount == 0);
321 }
322
323
324 void
325 _tnl_NewList( GLcontext *ctx, GLuint list, GLenum mode )
326 {
327 struct immediate *IM = TNL_CURRENT_IM(ctx);
328
329 /* Use the installed immediate struct. No vertices in the current
330 * immediate, no copied vertices in the system.
331 */
332 ASSERT(TNL_CURRENT_IM(ctx));
333 ASSERT(TNL_CURRENT_IM(ctx)->Start == IMM_MAX_COPIED_VERTS);
334 ASSERT(TNL_CURRENT_IM(ctx)->Start == TNL_CURRENT_IM(ctx)->Count);
335 ASSERT(TNL_CONTEXT(ctx)->ExecCopyCount == 0);
336
337 /* Set current Begin/End state to unknown:
338 */
339 IM->BeginState = VERT_BEGIN_0;
340 ctx->Driver.CurrentSavePrimitive = PRIM_UNKNOWN;
341 }
342
343
344 void
345 _tnl_dlist_init( GLcontext *ctx )
346 {
347 TNLcontext *tnl = TNL_CONTEXT(ctx);
348
349 tnl->opcode_vertex_cassette =
350 _mesa_alloc_opcode( ctx,
351 sizeof(TNLvertexcassette),
352 execute_compiled_cassette,
353 destroy_compiled_cassette,
354 print_compiled_cassette );
355 }
356