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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
30 #include "main/glheader.h"
31 #include "main/imports.h"
32 #include "main/context.h"
33 #include "main/macros.h"
34 #include "main/mtypes.h"
35 #include "main/light.h"
36 #include "math/m_translate.h"
37 #include "math/m_xform.h"
38 #include "main/state.h"
41 #include "t_context.h"
42 #include "t_pipeline.h"
47 _tnl_CreateContext( struct gl_context *ctx )
52 /* Create the TNLcontext structure
54 ctx->swtnl_context = tnl = calloc(1, sizeof(TNLcontext));
62 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
65 /* Initialize tnl state.
67 if (ctx->VertexProgram._MaintainTnlProgram) {
68 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
70 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
73 tnl->NeedNdcCoords = GL_TRUE;
74 tnl->AllowVertexFog = GL_TRUE;
75 tnl->AllowPixelFog = GL_TRUE;
77 /* Set a few default values in the driver struct.
79 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
80 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
81 tnl->Driver.NotifyMaterialChange = _tnl_validate_shine_tables;
85 /* Lighting miscellaneous */
86 tnl->_ShineTabList = MALLOC_STRUCT( tnl_shine_tab );
87 make_empty_list( tnl->_ShineTabList );
88 /* Allocate 10 (arbitrary) shininess lookup tables */
89 for (i = 0 ; i < 10 ; i++) {
90 struct tnl_shine_tab *s = MALLOC_STRUCT( tnl_shine_tab );
93 insert_at_tail( tnl->_ShineTabList, s );
96 /* plug in the VBO drawing function */
97 vbo_set_draw_func(ctx, _tnl_vbo_draw_prims);
99 _math_init_transformation();
100 _math_init_translate();
107 _tnl_DestroyContext( struct gl_context *ctx )
109 struct tnl_shine_tab *s, *tmps;
110 TNLcontext *tnl = TNL_CONTEXT(ctx);
112 /* Free lighting shininess exponentiation table */
113 foreach_s( s, tmps, tnl->_ShineTabList ) {
116 free( tnl->_ShineTabList );
118 _tnl_destroy_pipeline( ctx );
121 ctx->swtnl_context = NULL;
126 _tnl_InvalidateState( struct gl_context *ctx, GLuint new_state )
128 TNLcontext *tnl = TNL_CONTEXT(ctx);
129 const struct gl_vertex_program *vp = ctx->VertexProgram._Current;
130 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
133 if (new_state & (_NEW_HINT | _NEW_PROGRAM)) {
134 ASSERT(tnl->AllowVertexFog || tnl->AllowPixelFog);
135 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
136 || !tnl->AllowPixelFog) && !fp;
139 tnl->pipeline.new_state |= new_state;
141 /* Calculate tnl->render_inputs. This bitmask indicates which vertex
142 * attributes need to be emitted to the rasterizer.
144 tnl->render_inputs_bitset = BITFIELD64_BIT(_TNL_ATTRIB_POS);
146 if (!fp || (fp->Base.InputsRead & VARYING_BIT_COL0)) {
147 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR0);
150 if (_mesa_need_secondary_color(ctx))
151 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR1);
153 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
154 if (ctx->Texture._EnabledCoordUnits & (1 << i) ||
155 (fp && fp->Base.InputsRead & VARYING_BIT_TEX(i))) {
156 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX(i));
161 || (fp != NULL && (fp->Base.InputsRead & VARYING_BIT_FOGC) != 0)) {
162 /* Either fixed-function fog or a fragment program needs fog coord.
164 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_FOG);
167 if (ctx->Polygon.FrontMode != GL_FILL ||
168 ctx->Polygon.BackMode != GL_FILL)
169 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_EDGEFLAG);
171 if (ctx->RenderMode == GL_FEEDBACK)
172 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX0);
174 if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled)
175 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE);
177 /* check for varying vars which are written by the vertex program */
180 for (i = 0; i < MAX_VARYING; i++) {
181 if (vp->Base.OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_VAR0 + i)) {
182 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_GENERIC(i));
190 _tnl_wakeup( struct gl_context *ctx )
192 /* Assume we haven't been getting state updates either:
194 _tnl_InvalidateState( ctx, ~0 );
197 if (ctx->Light.ColorMaterialEnabled) {
198 _mesa_update_color_material( ctx,
199 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
208 * Drivers call this function to tell the TCL module whether or not
209 * it wants Normalized Device Coords (NDC) computed. I.e. whether
210 * we should "Divide-by-W". Software renders will want that.
213 _tnl_need_projected_coords( struct gl_context *ctx, GLboolean mode )
215 TNLcontext *tnl = TNL_CONTEXT(ctx);
216 tnl->NeedNdcCoords = mode;
220 _tnl_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
222 TNLcontext *tnl = TNL_CONTEXT(ctx);
223 tnl->AllowVertexFog = value;
224 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
225 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
230 _tnl_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
232 TNLcontext *tnl = TNL_CONTEXT(ctx);
233 tnl->AllowPixelFog = value;
234 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
235 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;