summaryrefslogtreecommitdiff
path: root/src/mesa/tnl/t_pipeline.c
blob: 1e9b686789b4a2d8db48ef9c00fc5074d57c4e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

/*
 * Mesa 3-D graphics library
 * Version:  5.1
 *
 * Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Keith Whitwell <keith@tungstengraphics.com>
 */

#include "glheader.h"
#include "context.h"
#include "imports.h"
#include "state.h"
#include "mtypes.h"

#include "math/m_translate.h"
#include "math/m_xform.h"

#include "t_context.h"
#include "t_pipeline.h"


void _tnl_install_pipeline( GLcontext *ctx,
			    const struct tnl_pipeline_stage **stages )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_pipeline *pipe = &tnl->pipeline;
   GLuint i;

   ASSERT(pipe->nr_stages == 0);

   pipe->run_state_changes = ~0;
   pipe->run_input_changes = ~0;
   pipe->build_state_changes = ~0;
   pipe->build_state_trigger = 0;
   pipe->inputs = 0;

   /* Create a writeable copy of each stage.
    */
   for (i = 0 ; i < MAX_PIPELINE_STAGES && stages[i] ; i++) {
      MEMCPY( &pipe->stages[i], stages[i], sizeof( **stages ));
      pipe->build_state_trigger |= pipe->stages[i].check_state;
   }

   MEMSET( &pipe->stages[i], 0, sizeof( **stages ));

   pipe->nr_stages = i;
}

void _tnl_destroy_pipeline( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   GLuint i;

   for (i = 0 ; i < tnl->pipeline.nr_stages ; i++)
      tnl->pipeline.stages[i].destroy( &tnl->pipeline.stages[i] );

   tnl->pipeline.nr_stages = 0;
}

/* TODO: merge validate with run.
 */
void _tnl_validate_pipeline( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_pipeline *pipe = &tnl->pipeline;
   struct tnl_pipeline_stage *s = pipe->stages;
   GLuint newstate = pipe->build_state_changes;
   GLuint generated = 0;
   GLuint changed_inputs = 0;

   pipe->inputs = 0;
   pipe->build_state_changes = 0;

   for ( ; s->check ; s++) {

      s->changed_inputs |= s->inputs & changed_inputs;

      if (s->check_state & newstate) {
	 if (s->active) {
	    GLuint old_outputs = s->outputs;
	    s->check(ctx, s);
	    if (!s->active)
	       changed_inputs |= old_outputs;
	 }
	 else
	    s->check(ctx, s);
      }

      if (s->active) {
	 pipe->inputs |= s->inputs & ~generated;
	 generated |= s->outputs;
      }
   }
}



void _tnl_run_pipeline( GLcontext *ctx )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct tnl_pipeline *pipe = &tnl->pipeline;
   struct tnl_pipeline_stage *s = pipe->stages;
   GLuint changed_state = pipe->run_state_changes;
   GLuint changed_inputs = pipe->run_input_changes;
   GLboolean running = GL_TRUE;
#ifdef HAVE_FAST_MATH
   unsigned short __tmp;
#endif

   if (!tnl->vb.Count)
      return;

   pipe->run_state_changes = 0;
   pipe->run_input_changes = 0;

   /* Done elsewhere.
    */
   ASSERT(pipe->build_state_changes == 0);

#ifdef HAVE_FAST_MATH
   START_FAST_MATH(__tmp);
#endif

   /* If something changes in the pipeline, tag all subsequent stages
    * using this value for recalculation.  Inactive stages have their
    * state and inputs examined to try to keep cached data alive over
    * state-changes.
    */
   for ( ; s->run ; s++) {
      s->changed_inputs |= s->inputs & changed_inputs;

      if (s->run_state & changed_state)
	 s->changed_inputs = s->inputs;

      if (s->active && running) {
	 if (s->changed_inputs)
	    changed_inputs |= s->outputs;

	 running = s->run( ctx, s );

	 s->changed_inputs = 0;
      }
   }

#ifdef HAVE_FAST_MATH
   END_FAST_MATH(__tmp);
#endif
}



/* The default pipeline.  This is useful for software rasterizers, and
 * simple hardware rasterizers.  For customization, I don't recommend
 * tampering with the internals of these stages in the way that
 * drivers did in Mesa 3.4.  These stages are basically black boxes,
 * and should be left intact.
 *
 * To customize the pipeline, consider:
 *
 * - removing redundant stages (making sure that the software rasterizer
 *   can cope with this on fallback paths).  An example is fog
 *   coordinate generation, which is not required in the FX driver.
 *
 * - replacing general-purpose machine-independent stages with
 *   general-purpose machine-specific stages.  There is no example of
 *   this to date, though it must be borne in mind that all subsequent
 *   stages that reference the output of the new stage must cope with
 *   any machine-specific data introduced.  This may not be easy
 *   unless there are no such stages (ie the new stage is the last in
 *   the pipe).
 *
 * - inserting optimized (but specialized) stages ahead of the
 *   general-purpose fallback implementation.  For example, the old
 *   fastpath mechanism, which only works when the VB->Elts input is
 *   available, can be duplicated by placing the fastpath stage at the
 *   head of this pipeline.  Such specialized stages are currently
 *   constrained to have no outputs (ie. they must either finish the *
 *   pipeline by returning GL_FALSE from run(), or do nothing).
 *
 * Some work can be done to lift some of the restrictions in the final
 * case, if it becomes necessary to do so.
 */
const struct tnl_pipeline_stage *_tnl_default_pipeline[] = {
   &_tnl_vertex_transform_stage,
   &_tnl_normal_transform_stage,
   &_tnl_lighting_stage,
   &_tnl_fog_coordinate_stage,
   &_tnl_texgen_stage,
   &_tnl_texture_transform_stage,
   &_tnl_point_attenuation_stage,
#if defined(FEATURE_NV_vertex_program) || defined(FEATURE_ARB_vertex_program)
   &_tnl_vertex_program_stage,
#endif
   &_tnl_render_stage,
   NULL 
};