blob: 5ac7539d6566406e0477acce7aadd003ae6dfaac [file] [log] [blame]
Brian Paul27558a12003-03-01 01:50:20 +00001/* $Id: t_pipeline.c,v 1.26 2003/03/01 01:50:27 brianp Exp $ */
Keith Whitwell23caf202000-11-16 21:05:34 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul27558a12003-03-01 01:50:20 +00005 * Version: 5.1
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00006 *
Brian Paul27558a12003-03-01 01:50:20 +00007 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +00008 *
Keith Whitwell23caf202000-11-16 21:05:34 +00009 * 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:
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000015 *
Keith Whitwell23caf202000-11-16 21:05:34 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000018 *
Keith Whitwell23caf202000-11-16 21:05:34 +000019 * 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.
Keith Whitwellcab974c2000-12-26 05:09:27 +000025 *
Gareth Hughes22144ab2001-03-12 00:48:37 +000026 * Authors:
Brian Paul05a4b372002-10-29 20:28:36 +000027 * Keith Whitwell <keith@tungstengraphics.com>
Keith Whitwell23caf202000-11-16 21:05:34 +000028 */
29
30#include "glheader.h"
31#include "context.h"
Brian Paul3c634522002-10-24 23:57:19 +000032#include "imports.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000033#include "state.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000034#include "mtypes.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000035
36#include "math/m_translate.h"
37#include "math/m_xform.h"
38
Keith Whitwellcab974c2000-12-26 05:09:27 +000039#include "t_context.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000040#include "t_pipeline.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000041
42
Gareth Hughes22144ab2001-03-12 00:48:37 +000043void _tnl_install_pipeline( GLcontext *ctx,
Keith Whitwellcab974c2000-12-26 05:09:27 +000044 const struct gl_pipeline_stage **stages )
Keith Whitwell23caf202000-11-16 21:05:34 +000045{
Keith Whitwell23caf202000-11-16 21:05:34 +000046 TNLcontext *tnl = TNL_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +000047 struct gl_pipeline *pipe = &tnl->pipeline;
Keith Whitwell23caf202000-11-16 21:05:34 +000048 GLuint i;
Keith Whitwell23caf202000-11-16 21:05:34 +000049
Keith Whitwellcab974c2000-12-26 05:09:27 +000050 ASSERT(pipe->nr_stages == 0);
Keith Whitwell23caf202000-11-16 21:05:34 +000051
Keith Whitwellcab974c2000-12-26 05:09:27 +000052 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;
Keith Whitwell23caf202000-11-16 21:05:34 +000057
Keith Whitwellcab974c2000-12-26 05:09:27 +000058 /* Create a writeable copy of each stage.
Keith Whitwell23caf202000-11-16 21:05:34 +000059 */
Keith Whitwellcab974c2000-12-26 05:09:27 +000060 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;
Keith Whitwell23caf202000-11-16 21:05:34 +000063 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000064
Keith Whitwell7954a0c2001-05-10 12:18:38 +000065 MEMSET( &pipe->stages[i], 0, sizeof( **stages ));
66
Keith Whitwellcab974c2000-12-26 05:09:27 +000067 pipe->nr_stages = i;
68}
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000069
Keith Whitwellcab974c2000-12-26 05:09:27 +000070void _tnl_destroy_pipeline( GLcontext *ctx )
71{
72 TNLcontext *tnl = TNL_CONTEXT(ctx);
73 GLuint i;
74
Gareth Hughes22144ab2001-03-12 00:48:37 +000075 for (i = 0 ; i < tnl->pipeline.nr_stages ; i++)
Keith Whitwellcab974c2000-12-26 05:09:27 +000076 tnl->pipeline.stages[i].destroy( &tnl->pipeline.stages[i] );
77
78 tnl->pipeline.nr_stages = 0;
79}
80
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000081/* TODO: merge validate with run.
82 */
Keith Whitwellcab974c2000-12-26 05:09:27 +000083void _tnl_validate_pipeline( GLcontext *ctx )
84{
85 TNLcontext *tnl = TNL_CONTEXT(ctx);
86 struct gl_pipeline *pipe = &tnl->pipeline;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000087 struct gl_pipeline_stage *s = pipe->stages;
Keith Whitwellcab974c2000-12-26 05:09:27 +000088 GLuint newstate = pipe->build_state_changes;
89 GLuint generated = 0;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000090 GLuint changed_inputs = 0;
Keith Whitwellcab974c2000-12-26 05:09:27 +000091
92 pipe->inputs = 0;
93 pipe->build_state_changes = 0;
94
Keith Whitwell7954a0c2001-05-10 12:18:38 +000095 for ( ; s->check ; s++) {
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +000096
97 s->changed_inputs |= s->inputs & changed_inputs;
Gareth Hughes22144ab2001-03-12 00:48:37 +000098
99 if (s->check_state & newstate) {
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000100 if (s->active) {
101 GLuint old_outputs = s->outputs;
102 s->check(ctx, s);
103 if (!s->active)
104 changed_inputs |= old_outputs;
105 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000106 else
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000107 s->check(ctx, s);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000108 }
109
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000110 if (s->active) {
111 pipe->inputs |= s->inputs & ~generated;
112 generated |= s->outputs;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000113 }
Keith Whitwellcab974c2000-12-26 05:09:27 +0000114 }
115}
116
117
118
Keith Whitwellcab974c2000-12-26 05:09:27 +0000119void _tnl_run_pipeline( GLcontext *ctx )
120{
121 TNLcontext *tnl = TNL_CONTEXT(ctx);
Keith Whitwell3004bf82001-04-19 12:23:07 +0000122 struct vertex_buffer *VB = &tnl->vb;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000123 struct gl_pipeline *pipe = &tnl->pipeline;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000124 struct gl_pipeline_stage *s = pipe->stages;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000125 GLuint changed_state = pipe->run_state_changes;
126 GLuint changed_inputs = pipe->run_input_changes;
127 GLboolean running = GL_TRUE;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000128 unsigned short __tmp;
129
Keith Whitwell7954a0c2001-05-10 12:18:38 +0000130 pipe->run_state_changes = 0;
131 pipe->run_input_changes = 0;
132
Keith Whitwellcab974c2000-12-26 05:09:27 +0000133 /* Done elsewhere.
134 */
135 ASSERT(pipe->build_state_changes == 0);
Gareth Hughese7e38a42001-05-21 16:33:41 +0000136
Keith Whitwellcab974c2000-12-26 05:09:27 +0000137 START_FAST_MATH(__tmp);
Keith Whitwell23caf202000-11-16 21:05:34 +0000138
139 /* If something changes in the pipeline, tag all subsequent stages
Keith Whitwell7954a0c2001-05-10 12:18:38 +0000140 * using this value for recalculation. Inactive stages have their
141 * state and inputs examined to try to keep cached data alive over
Gareth Hughese7e38a42001-05-21 16:33:41 +0000142 * state-changes.
Keith Whitwell23caf202000-11-16 21:05:34 +0000143 */
Keith Whitwell7954a0c2001-05-10 12:18:38 +0000144 for ( ; s->run ; s++) {
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000145 s->changed_inputs |= s->inputs & changed_inputs;
Keith Whitwell23caf202000-11-16 21:05:34 +0000146
Gareth Hughese7e38a42001-05-21 16:33:41 +0000147 if (s->run_state & changed_state)
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000148 s->changed_inputs = s->inputs;
Keith Whitwell23caf202000-11-16 21:05:34 +0000149
Keith Whitwell7954a0c2001-05-10 12:18:38 +0000150 if (s->active && running) {
151 if (s->changed_inputs)
152 changed_inputs |= s->outputs;
Keith Whitwell5c1e7fa2001-01-29 20:47:39 +0000153
Keith Whitwell7954a0c2001-05-10 12:18:38 +0000154 running = s->run( ctx, s );
Keith Whitwell16837e42001-04-30 09:04:00 +0000155
Keith Whitwell7954a0c2001-05-10 12:18:38 +0000156 s->changed_inputs = 0;
157 VB->importable_data &= ~s->outputs;
Keith Whitwell23caf202000-11-16 21:05:34 +0000158 }
159 }
Keith Whitwellc6b2a922001-02-15 01:33:52 +0000160
Keith Whitwellcab974c2000-12-26 05:09:27 +0000161 END_FAST_MATH(__tmp);
Keith Whitwell23caf202000-11-16 21:05:34 +0000162}
163
164
165
Keith Whitwellcab974c2000-12-26 05:09:27 +0000166/* 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,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000170 * and should be left intact.
Keith Whitwellcab974c2000-12-26 05:09:27 +0000171 *
Gareth Hughes22144ab2001-03-12 00:48:37 +0000172 * To customize the pipeline, consider:
Keith Whitwellcab974c2000-12-26 05:09:27 +0000173 *
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
Brian Paul4c8fadc62002-01-22 14:35:16 +0000188 * fastpath mechanism, which only works when the VERT_BIT_ELT input is
Keith Whitwellcab974c2000-12-26 05:09:27 +0000189 * 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
Gareth Hughes22144ab2001-03-12 00:48:37 +0000195 * case, if it becomes necessary to do so.
Keith Whitwellcab974c2000-12-26 05:09:27 +0000196 */
197const struct gl_pipeline_stage *_tnl_default_pipeline[] = {
Gareth Hughes22144ab2001-03-12 00:48:37 +0000198 &_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,
Brian Paul8dfc5b92002-10-16 17:57:51 +0000205#if FEATURE_NV_vertex_program
Brian Paul86b84272001-12-14 02:50:01 +0000206 &_tnl_vertex_program_stage,
Brian Paul8dfc5b92002-10-16 17:57:51 +0000207#endif
Keith Whitwellcab974c2000-12-26 05:09:27 +0000208 &_tnl_render_stage,
209 0
210};