New vbo_set_draw_func() to keep vbo context opaque to state tracker and tnl module.
[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 "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
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_install_pipeline( ctx, _tnl_vp_pipeline );
65 } else {
66 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
67 }
68
69 tnl->NeedNdcCoords = GL_TRUE;
70 tnl->AllowVertexFog = GL_TRUE;
71 tnl->AllowPixelFog = GL_TRUE;
72
73 /* Set a few default values in the driver struct.
74 */
75 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
76 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
77 tnl->Driver.NotifyMaterialChange = _mesa_validate_all_lighting_tables;
78
79 tnl->nr_blocks = 0;
80
81 /* plug in the VBO drawing function */
82 vbo_set_draw_func(ctx, _tnl_draw_prims);
83
84 return GL_TRUE;
85 }
86
87
88 void
89 _tnl_DestroyContext( GLcontext *ctx )
90 {
91 TNLcontext *tnl = TNL_CONTEXT(ctx);
92
93 _tnl_destroy_pipeline( ctx );
94
95 FREE(tnl);
96 ctx->swtnl_context = NULL;
97 }
98
99
100 void
101 _tnl_InvalidateState( GLcontext *ctx, GLuint new_state )
102 {
103 TNLcontext *tnl = TNL_CONTEXT(ctx);
104 const struct gl_vertex_program *vp = ctx->VertexProgram._Current;
105 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
106
107 if (new_state & (_NEW_HINT)) {
108 ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
109 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
110 || !tnl->AllowPixelFog;
111 }
112
113 tnl->pipeline.new_state |= new_state;
114
115 /* Calculate tnl->render_inputs:
116 */
117 if (ctx->Visual.rgbMode) {
118 GLuint i;
119
120 RENDERINPUTS_ZERO( tnl->render_inputs_bitset );
121 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
122 if (!fp || (fp->Base.InputsRead & FRAG_BIT_COL0)) {
123 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR0 );
124 }
125 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
126 if (ctx->Texture._EnabledCoordUnits & (1 << i)) {
127 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX(i) );
128 }
129 }
130
131 if (NEED_SECONDARY_COLOR(ctx))
132 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 );
133 }
134 else {
135 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POS );
136 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR_INDEX );
137 }
138
139 if (ctx->Fog.Enabled ||
140 ((ctx->FragmentProgram._Active || ctx->FragmentProgram._Current) &&
141 (ctx->FragmentProgram._Current->FogOption != GL_NONE ||
142 (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_FOGC))))
143 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_FOG );
144
145 if (ctx->Polygon.FrontMode != GL_FILL ||
146 ctx->Polygon.BackMode != GL_FILL)
147 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_EDGEFLAG );
148
149 if (ctx->RenderMode == GL_FEEDBACK)
150 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_TEX0 );
151
152 if (ctx->Point._Attenuated ||
153 (ctx->VertexProgram._Enabled && ctx->VertexProgram.PointSizeEnabled))
154 RENDERINPUTS_SET( tnl->render_inputs_bitset, _TNL_ATTRIB_POINTSIZE );
155
156 /* check for varying vars which are written by the vertex program */
157 if (vp) {
158 GLuint i;
159 for (i = 0; i < MAX_VARYING; i++) {
160 if (vp->Base.OutputsWritten & (1 << (VERT_RESULT_VAR0 + i))) {
161 RENDERINPUTS_SET(tnl->render_inputs_bitset,
162 _TNL_ATTRIB_GENERIC(i));
163 }
164 }
165 }
166 }
167
168
169 void
170 _tnl_wakeup( GLcontext *ctx )
171 {
172 /* Assume we haven't been getting state updates either:
173 */
174 _tnl_InvalidateState( ctx, ~0 );
175
176 #if 0
177 if (ctx->Light.ColorMaterialEnabled) {
178 _mesa_update_color_material( ctx,
179 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
180 }
181 #endif
182 }
183
184
185
186
187 /**
188 * Drivers call this function to tell the TCL module whether or not
189 * it wants Normalized Device Coords (NDC) computed. I.e. whether
190 * we should "Divide-by-W". Software renders will want that.
191 */
192 void
193 _tnl_need_projected_coords( GLcontext *ctx, GLboolean mode )
194 {
195 TNLcontext *tnl = TNL_CONTEXT(ctx);
196 tnl->NeedNdcCoords = mode;
197 }
198
199 void
200 _tnl_allow_vertex_fog( GLcontext *ctx, GLboolean value )
201 {
202 TNLcontext *tnl = TNL_CONTEXT(ctx);
203 tnl->AllowVertexFog = value;
204 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
205 || !tnl->AllowPixelFog;
206
207 }
208
209 void
210 _tnl_allow_pixel_fog( GLcontext *ctx, GLboolean value )
211 {
212 TNLcontext *tnl = TNL_CONTEXT(ctx);
213 tnl->AllowPixelFog = value;
214 tnl->_DoVertexFog = (tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
215 || !tnl->AllowPixelFog;
216 }
217