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