Merge branch 'master' of git+ssh://brianp@git.freedesktop.org/git/mesa/mesa into...
[mesa.git] / src / mesa / tnl / t_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28
29 #include "glheader.h"
30 #include "imports.h"
31 #include "context.h"
32 #include "macros.h"
33 #include "mtypes.h"
34 #include "light.h"
35
36 #include "tnl.h"
37 #include "t_context.h"
38 #include "t_pipeline.h"
39 #include "t_vp_build.h"
40
41 #include "vbo/vbo.h"
42
43 GLboolean
44 _tnl_CreateContext( GLcontext *ctx )
45 {
46 TNLcontext *tnl;
47
48 /* Create the TNLcontext structure
49 */
50 ctx->swtnl_context = tnl = (TNLcontext *) CALLOC( sizeof(TNLcontext) );
51
52 if (!tnl) {
53 return GL_FALSE;
54 }
55
56 /* Initialize the VB.
57 */
58 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
59
60
61 /* Initialize tnl state.
62 */
63 if (ctx->VertexProgram._MaintainTnlProgram) {
64 _tnl_ProgramCacheInit( ctx );
65 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
66 } else {
67 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
68 }
69
70 tnl->NeedNdcCoords = GL_TRUE;
71 tnl->AllowVertexFog = GL_TRUE;
72 tnl->AllowPixelFog = GL_TRUE;
73
74 /* Set a few default values in the driver struct.
75 */
76 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
77 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
78 tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
79
80 tnl->nr_blocks = 0;
81
82 return GL_TRUE;
83 }
84
85
86 void
87 _tnl_DestroyContext( GLcontext *ctx )
88 {
89 TNLcontext *tnl = TNL_CONTEXT(ctx);
90
91 _tnl_destroy_pipeline( ctx );
92
93 if (ctx->VertexProgram._MaintainTnlProgram)
94 _tnl_ProgramCacheDestroy( ctx );
95
96 FREE(tnl);
97 ctx->swtnl_context = NULL;
98 }
99
100
101 void
102 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
103 {
104 TNLcontext *tnl = TNL_CONTEXT(ctx);
105
106 if (new_state & (_NEW_HINT)) {
107 ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
108 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
109 || !tnl->AllowPixelFog;
110 }
111
112 tnl->pipeline.new_state |= new_state;
113
114 /* Calculate tnl->render_inputs:
115 */
116 if (ctx->Visual.rgbMode) {
117 GLuint i;
118
119 RENDERINPUTS_ZERO( tnl->render_inputs_bitset );
120 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
121 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR0 );
122 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
123 if (ctx->Texture._EnabledCoordUnits & (1 << i)) {
124 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX(i) );
125 }
126 }
127
128 if (NEED_SECONDARY_COLOR(ctx))
129 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 );
130 }
131 else {
132 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
133 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR_INDEX );
134 }
135
136 if (ctx->Fog.Enabled ||
137 (ctx->FragmentProgram._Current &&
138 (ctx->FragmentProgram._Current->FogOption != GL_NONE ||
139 ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_FOGC)))
140 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_FOG );
141
142 if (ctx->Polygon.FrontMode != GL_FILL ||
143 ctx->Polygon.BackMode != GL_FILL)
144 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_EDGEFLAG );
145
146 if (ctx->RenderMode == GL_FEEDBACK)
147 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX0 );
148
149 if (ctx->Point._Attenuated ||
150 (ctx->VertexProgram._Enabled && ctx->VertexProgram.PointSizeEnabled))
151 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POINTSIZE );
152
153 #if 1 /* XXX NEW_SLANG */
154 RENDERINPUTS_SET_RANGE( tnl->render_inputs_bitset,
155 _TNL_FIRST_GENERIC, _TNL_LAST_GENERIC );
156 #else
157 if (ctx->ShaderObjects._VertexShaderPresent || ctx->ShaderObjects._FragmentShaderPresent)
158 RENDERINPUTS_SET_RANGE( tnl->render_inputs_bitset, _TNL_FIRST_GENERIC, _TNL_LAST_GENERIC );
159 #endif
160 }
161
162
163 void
164 _tnl_wakeup( GLcontext *ctx )
165 {
166 /* Assume we haven't been getting state updates either:
167 */
168 _tnl_InvalidateState( ctx, ~0 );
169
170 #if 0
171 if (ctx->Light.ColorMaterialEnabled) {
172 _mesa_update_color_material( ctx,
173 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
174 }
175 #endif
176 }
177
178
179
180
181 /**
182 * Drivers call this function to tell the TCL module whether or not
183 * it wants Normalized Device Coords (NDC) computed. I.e. whether
184 * we should "Divide-by-W". Software renders will want that.
185 */
186 void
187 _tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
188 {
189 TNLcontext *tnl = TNL_CONTEXT(ctx);
190 tnl->NeedNdcCoords = mode;
191 }
192
193 void
194 _tnl_allow_vertex_fog( GLcontext *ctx, GLboolean value )
195 {
196 TNLcontext *tnl = TNL_CONTEXT(ctx);
197 tnl->AllowVertexFog = value;
198 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
199 || !tnl->AllowPixelFog;
200
201 }
202
203 void
204 _tnl_allow_pixel_fog( GLcontext *ctx, GLboolean value )
205 {
206 TNLcontext *tnl = TNL_CONTEXT(ctx);
207 tnl->AllowPixelFog = value;
208 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
209 || !tnl->AllowPixelFog;
210 }
211