Added ctx->Texture._EnabledCoordUnits bitfield.
[mesa.git] / src / mesa / tnl / t_pipeline.c
1 /* $Id: t_pipeline.c,v 1.26 2003/03/01 01:50:27 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2003 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 * Authors:
27 * Keith Whitwell <keith@tungstengraphics.com>
28 */
29
30 #include "glheader.h"
31 #include "context.h"
32 #include "imports.h"
33 #include "state.h"
34 #include "mtypes.h"
35
36 #include "math/m_translate.h"
37 #include "math/m_xform.h"
38
39 #include "t_context.h"
40 #include "t_pipeline.h"
41
42
43 void _tnl_install_pipeline( GLcontext *ctx,
44 const struct gl_pipeline_stage **stages )
45 {
46 TNLcontext *tnl = TNL_CONTEXT(ctx);
47 struct gl_pipeline *pipe = &tnl->pipeline;
48 GLuint i;
49
50 ASSERT(pipe->nr_stages == 0);
51
52 pipe->run_state_changes = ~0;
53 pipe->run_input_changes = ~0;
54 pipe->build_state_changes = ~0;
55 pipe->build_state_trigger = 0;
56 pipe->inputs = 0;
57
58 /* Create a writeable copy of each stage.
59 */
60 for (i = 0 ; i < MAX_PIPELINE_STAGES && stages[i] ; i++) {
61 MEMCPY( &pipe->stages[i], stages[i], sizeof( **stages ));
62 pipe->build_state_trigger |= pipe->stages[i].check_state;
63 }
64
65 MEMSET( &pipe->stages[i], 0, sizeof( **stages ));
66
67 pipe->nr_stages = i;
68 }
69
70 void _tnl_destroy_pipeline( GLcontext *ctx )
71 {
72 TNLcontext *tnl = TNL_CONTEXT(ctx);
73 GLuint i;
74
75 for (i = 0 ; i < tnl->pipeline.nr_stages ; i++)
76 tnl->pipeline.stages[i].destroy( &tnl->pipeline.stages[i] );
77
78 tnl->pipeline.nr_stages = 0;
79 }
80
81 /* TODO: merge validate with run.
82 */
83 void _tnl_validate_pipeline( GLcontext *ctx )
84 {
85 TNLcontext *tnl = TNL_CONTEXT(ctx);
86 struct gl_pipeline *pipe = &tnl->pipeline;
87 struct gl_pipeline_stage *s = pipe->stages;
88 GLuint newstate = pipe->build_state_changes;
89 GLuint generated = 0;
90 GLuint changed_inputs = 0;
91
92 pipe->inputs = 0;
93 pipe->build_state_changes = 0;
94
95 for ( ; s->check ; s++) {
96
97 s->changed_inputs |= s->inputs & changed_inputs;
98
99 if (s->check_state & newstate) {
100 if (s->active) {
101 GLuint old_outputs = s->outputs;
102 s->check(ctx, s);
103 if (!s->active)
104 changed_inputs |= old_outputs;
105 }
106 else
107 s->check(ctx, s);
108 }
109
110 if (s->active) {
111 pipe->inputs |= s->inputs & ~generated;
112 generated |= s->outputs;
113 }
114 }
115 }
116
117
118
119 void _tnl_run_pipeline( GLcontext *ctx )
120 {
121 TNLcontext *tnl = TNL_CONTEXT(ctx);
122 struct vertex_buffer *VB = &tnl->vb;
123 struct gl_pipeline *pipe = &tnl->pipeline;
124 struct gl_pipeline_stage *s = pipe->stages;
125 GLuint changed_state = pipe->run_state_changes;
126 GLuint changed_inputs = pipe->run_input_changes;
127 GLboolean running = GL_TRUE;
128 unsigned short __tmp;
129
130 pipe->run_state_changes = 0;
131 pipe->run_input_changes = 0;
132
133 /* Done elsewhere.
134 */
135 ASSERT(pipe->build_state_changes == 0);
136
137 START_FAST_MATH(__tmp);
138
139 /* If something changes in the pipeline, tag all subsequent stages
140 * using this value for recalculation. Inactive stages have their
141 * state and inputs examined to try to keep cached data alive over
142 * state-changes.
143 */
144 for ( ; s->run ; s++) {
145 s->changed_inputs |= s->inputs & changed_inputs;
146
147 if (s->run_state & changed_state)
148 s->changed_inputs = s->inputs;
149
150 if (s->active && running) {
151 if (s->changed_inputs)
152 changed_inputs |= s->outputs;
153
154 running = s->run( ctx, s );
155
156 s->changed_inputs = 0;
157 VB->importable_data &= ~s->outputs;
158 }
159 }
160
161 END_FAST_MATH(__tmp);
162 }
163
164
165
166 /* The default pipeline. This is useful for software rasterizers, and
167 * simple hardware rasterizers. For customization, I don't recommend
168 * tampering with the internals of these stages in the way that
169 * drivers did in Mesa 3.4. These stages are basically black boxes,
170 * and should be left intact.
171 *
172 * To customize the pipeline, consider:
173 *
174 * - removing redundant stages (making sure that the software rasterizer
175 * can cope with this on fallback paths). An example is fog
176 * coordinate generation, which is not required in the FX driver.
177 *
178 * - replacing general-purpose machine-independent stages with
179 * general-purpose machine-specific stages. There is no example of
180 * this to date, though it must be borne in mind that all subsequent
181 * stages that reference the output of the new stage must cope with
182 * any machine-specific data introduced. This may not be easy
183 * unless there are no such stages (ie the new stage is the last in
184 * the pipe).
185 *
186 * - inserting optimized (but specialized) stages ahead of the
187 * general-purpose fallback implementation. For example, the old
188 * fastpath mechanism, which only works when the VERT_BIT_ELT input is
189 * available, can be duplicated by placing the fastpath stage at the
190 * head of this pipeline. Such specialized stages are currently
191 * constrained to have no outputs (ie. they must either finish the *
192 * pipeline by returning GL_FALSE from run(), or do nothing).
193 *
194 * Some work can be done to lift some of the restrictions in the final
195 * case, if it becomes necessary to do so.
196 */
197 const struct gl_pipeline_stage *_tnl_default_pipeline[] = {
198 &_tnl_vertex_transform_stage,
199 &_tnl_normal_transform_stage,
200 &_tnl_lighting_stage,
201 &_tnl_fog_coordinate_stage,
202 &_tnl_texgen_stage,
203 &_tnl_texture_transform_stage,
204 &_tnl_point_attenuation_stage,
205 #if FEATURE_NV_vertex_program
206 &_tnl_vertex_program_stage,
207 #endif
208 &_tnl_render_stage,
209 0
210 };