2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
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"
37 #include "t_context.h"
38 #include "t_pipeline.h"
39 #include "t_vp_build.h"
44 _tnl_CreateContext( GLcontext
*ctx
)
48 /* Create the TNLcontext structure
50 ctx
->swtnl_context
= tnl
= (TNLcontext
*) CALLOC( sizeof(TNLcontext
) );
58 tnl
->vb
.Size
= ctx
->Const
.MaxArrayLockSize
+ MAX_CLIPPED_VERTICES
;
61 /* Initialize tnl state.
63 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
64 _tnl_install_pipeline( ctx
, _tnl_vp_pipeline
);
66 _tnl_install_pipeline( ctx
, _tnl_default_pipeline
);
69 tnl
->NeedNdcCoords
= GL_TRUE
;
70 tnl
->AllowVertexFog
= GL_TRUE
;
71 tnl
->AllowPixelFog
= GL_TRUE
;
73 /* Set a few default values in the driver struct.
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
;
81 /* plug in the VBO drawing function */
82 vbo_set_draw_func(ctx
, _tnl_draw_prims
);
89 _tnl_DestroyContext( GLcontext
*ctx
)
91 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
93 _tnl_destroy_pipeline( ctx
);
96 ctx
->swtnl_context
= NULL
;
101 _tnl_InvalidateState( GLcontext
*ctx
, GLuint new_state
)
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
;
107 if (new_state
& (_NEW_HINT
| _NEW_PROGRAM
)) {
108 ASSERT(tnl
->AllowVertexFog
|| tnl
->AllowPixelFog
);
109 tnl
->_DoVertexFog
= ((tnl
->AllowVertexFog
&& (ctx
->Hint
.Fog
!= GL_NICEST
))
110 || !tnl
->AllowPixelFog
) && !fp
;
113 tnl
->pipeline
.new_state
|= new_state
;
115 /* Calculate tnl->render_inputs. This bitmask indicates which vertex
116 * attributes need to be emitted to the rasterizer.
118 if (ctx
->Visual
.rgbMode
) {
121 RENDERINPUTS_ZERO( tnl
->render_inputs_bitset
);
122 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_POS
);
124 if (!fp
|| (fp
->Base
.InputsRead
& FRAG_BIT_COL0
)) {
125 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_COLOR0
);
128 if (NEED_SECONDARY_COLOR(ctx
))
129 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_COLOR1
);
131 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
132 if (ctx
->Texture
._EnabledCoordUnits
& (1 << i
) ||
133 (fp
&& fp
->Base
.InputsRead
& FRAG_BIT_TEX(i
))) {
134 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_TEX(i
) );
139 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_POS
);
140 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_COLOR_INDEX
);
143 if (ctx
->Fog
.Enabled
) {
144 /* fixed-function fog */
145 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_FOG
);
147 else if (ctx
->FragmentProgram
._Current
) {
148 struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
150 if (fp
->FogOption
!= GL_NONE
|| (fp
->Base
.InputsRead
& FRAG_BIT_FOGC
)) {
151 /* fragment program needs fog coord */
152 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_FOG
);
157 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
158 ctx
->Polygon
.BackMode
!= GL_FILL
)
159 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_EDGEFLAG
);
161 if (ctx
->RenderMode
== GL_FEEDBACK
)
162 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_TEX0
);
164 if (ctx
->Point
._Attenuated
||
165 (ctx
->VertexProgram
._Enabled
&& ctx
->VertexProgram
.PointSizeEnabled
))
166 RENDERINPUTS_SET( tnl
->render_inputs_bitset
, _TNL_ATTRIB_POINTSIZE
);
168 /* check for varying vars which are written by the vertex program */
171 for (i
= 0; i
< MAX_VARYING
; i
++) {
172 if (vp
->Base
.OutputsWritten
& (1 << (VERT_RESULT_VAR0
+ i
))) {
173 RENDERINPUTS_SET(tnl
->render_inputs_bitset
,
174 _TNL_ATTRIB_GENERIC(i
));
182 _tnl_wakeup( GLcontext
*ctx
)
184 /* Assume we haven't been getting state updates either:
186 _tnl_InvalidateState( ctx
, ~0 );
189 if (ctx
->Light
.ColorMaterialEnabled
) {
190 _mesa_update_color_material( ctx
,
191 ctx
->Current
.Attrib
[VERT_ATTRIB_COLOR0
] );
200 * Drivers call this function to tell the TCL module whether or not
201 * it wants Normalized Device Coords (NDC) computed. I.e. whether
202 * we should "Divide-by-W". Software renders will want that.
205 _tnl_need_projected_coords( GLcontext
*ctx
, GLboolean mode
)
207 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
208 tnl
->NeedNdcCoords
= mode
;
212 _tnl_allow_vertex_fog( GLcontext
*ctx
, GLboolean value
)
214 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
215 tnl
->AllowVertexFog
= value
;
216 tnl
->_DoVertexFog
= ((tnl
->AllowVertexFog
&& (ctx
->Hint
.Fog
!= GL_NICEST
))
217 || !tnl
->AllowPixelFog
) && !ctx
->FragmentProgram
._Current
;
222 _tnl_allow_pixel_fog( GLcontext
*ctx
, GLboolean value
)
224 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
225 tnl
->AllowPixelFog
= value
;
226 tnl
->_DoVertexFog
= ((tnl
->AllowVertexFog
&& (ctx
->Hint
.Fog
!= GL_NICEST
))
227 || !tnl
->AllowPixelFog
) && !ctx
->FragmentProgram
._Current
;