Merge remote branch 'origin/master' into lp-binning
[mesa.git] / src / mesa / tnl / t_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.2
4 *
5 * Copyright (C) 1999-2008 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 "main/glheader.h"
30 #include "main/imports.h"
31 #include "main/context.h"
32 #include "main/macros.h"
33 #include "main/mtypes.h"
34 #include "main/light.h"
35 #include "math/m_translate.h"
36 #include "math/m_xform.h"
37
38 #include "tnl.h"
39 #include "t_context.h"
40 #include "t_pipeline.h"
41 #include "t_vp_build.h"
42
43 #include "vbo/vbo.h"
44
45 GLboolean
46 _tnl_CreateContext( GLcontext *ctx )
47 {
48 TNLcontext *tnl;
49
50 /* Create the TNLcontext structure
51 */
52 ctx->swtnl_context = tnl = (TNLcontext *) CALLOC( sizeof(TNLcontext) );
53
54 if (!tnl) {
55 return GL_FALSE;
56 }
57
58 /* Initialize the VB.
59 */
60 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
61
62
63 /* Initialize tnl state.
64 */
65 if (ctx->VertexProgram._MaintainTnlProgram) {
66 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
67 } else {
68 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
69 }
70
71 tnl->NeedNdcCoords = GL_TRUE;
72 tnl->AllowVertexFog = GL_TRUE;
73 tnl->AllowPixelFog = GL_TRUE;
74
75 /* Set a few default values in the driver struct.
76 */
77 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
78 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
79 tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
80
81 tnl->nr_blocks = 0;
82
83 /* plug in the VBO drawing function */
84 vbo_set_draw_func(ctx, _tnl_vbo_draw_prims);
85
86 _math_init_transformation();
87 _math_init_translate();
88
89 return GL_TRUE;
90 }
91
92
93 void
94 _tnl_DestroyContext( GLcontext *ctx )
95 {
96 TNLcontext *tnl = TNL_CONTEXT(ctx);
97
98 _tnl_destroy_pipeline( ctx );
99
100 FREE(tnl);
101 ctx->swtnl_context = NULL;
102 }
103
104
105 void
106 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
107 {
108 TNLcontext *tnl = TNL_CONTEXT(ctx);
109 const struct gl_vertex_program *vp = ctx->VertexProgram._Current;
110 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
111
112 if (new_state & (_NEW_HINT | _NEW_PROGRAM)) {
113 ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
114 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
115 || !tnl->AllowPixelFog) && !fp;
116 }
117
118 tnl->pipeline.new_state |= new_state;
119
120 /* Calculate tnl->render_inputs. This bitmask indicates which vertex
121 * attributes need to be emitted to the rasterizer.
122 */
123 if (ctx->Visual.rgbMode) {
124 GLuint i;
125
126 RENDERINPUTS_ZERO( tnl->render_inputs_bitset );
127 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
128
129 if (!fp || (fp->Base.InputsRead & FRAG_BIT_COL0)) {
130 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR0 );
131 }
132
133 if (NEED_SECONDARY_COLOR(ctx))
134 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 );
135
136 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
137 if (ctx->Texture._EnabledCoordUnits & (1 << i) ||
138 (fp && fp->Base.InputsRead & FRAG_BIT_TEX(i))) {
139 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX(i) );
140 }
141 }
142 }
143 else {
144 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
145 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR_INDEX );
146 }
147
148 if (ctx->Fog.Enabled) {
149 /* fixed-function fog */
150 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_FOG );
151 }
152 else if (fp) {
153 if (fp->FogOption != GL_NONE || (fp->Base.InputsRead & FRAG_BIT_FOGC)) {
154 /* fragment program needs fog coord */
155 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_FOG );
156 }
157 }
158
159 if (ctx->Polygon.FrontMode != GL_FILL ||
160 ctx->Polygon.BackMode != GL_FILL)
161 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_EDGEFLAG );
162
163 if (ctx->RenderMode == GL_FEEDBACK)
164 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX0 );
165
166 if (ctx->Point._Attenuated ||
167 (ctx->VertexProgram._Enabled && ctx->VertexProgram.PointSizeEnabled))
168 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POINTSIZE );
169
170 /* check for varying vars which are written by the vertex program */
171 if (vp) {
172 GLuint i;
173 for (i = 0; i < MAX_VARYING; i++) {
174 if (vp->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_VAR0 + i)) {
175 RENDERINPUTS_SET(tnl->render_inputs_bitset,
176 _TNL_ATTRIB_GENERIC(i));
177 }
178 }
179 }
180 }
181
182
183 void
184 _tnl_wakeup( GLcontext *ctx )
185 {
186 /* Assume we haven't been getting state updates either:
187 */
188 _tnl_InvalidateState( ctx, ~0 );
189
190 #if 0
191 if (ctx->Light.ColorMaterialEnabled) {
192 _mesa_update_color_material( ctx,
193 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
194 }
195 #endif
196 }
197
198
199
200
201 /**
202 * Drivers call this function to tell the TCL module whether or not
203 * it wants Normalized Device Coords (NDC) computed. I.e. whether
204 * we should "Divide-by-W". Software renders will want that.
205 */
206 void
207 _tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
208 {
209 TNLcontext *tnl = TNL_CONTEXT(ctx);
210 tnl->NeedNdcCoords = mode;
211 }
212
213 void
214 _tnl_allow_vertex_fog( GLcontext *ctx, GLboolean value )
215 {
216 TNLcontext *tnl = TNL_CONTEXT(ctx);
217 tnl->AllowVertexFog = value;
218 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
219 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
220
221 }
222
223 void
224 _tnl_allow_pixel_fog( GLcontext *ctx, GLboolean value )
225 {
226 TNLcontext *tnl = TNL_CONTEXT(ctx);
227 tnl->AllowPixelFog = value;
228 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
229 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
230 }
231