mesa: Restore 78-column wrapping of license text in C-style comments.
[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 * 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.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29
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"
39
40 #include "tnl.h"
41 #include "t_context.h"
42 #include "t_pipeline.h"
43
44 #include "vbo/vbo.h"
45
46 GLboolean
47 _tnl_CreateContext( struct gl_context *ctx )
48 {
49 TNLcontext *tnl;
50 GLuint i;
51
52 /* Create the TNLcontext structure
53 */
54 ctx->swtnl_context = tnl = calloc(1, sizeof(TNLcontext));
55
56 if (!tnl) {
57 return GL_FALSE;
58 }
59
60 /* Initialize the VB.
61 */
62 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
63
64
65 /* Initialize tnl state.
66 */
67 if (ctx->VertexProgram._MaintainTnlProgram) {
68 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
69 } else {
70 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
71 }
72
73 tnl->NeedNdcCoords = GL_TRUE;
74 tnl->AllowVertexFog = GL_TRUE;
75 tnl->AllowPixelFog = GL_TRUE;
76
77 /* Set a few default values in the driver struct.
78 */
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;
82
83 tnl->nr_blocks = 0;
84
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 );
91 s->shininess = -1;
92 s->refcount = 0;
93 insert_at_tail( tnl->_ShineTabList, s );
94 }
95
96 /* plug in the VBO drawing function */
97 vbo_set_draw_func(ctx, _tnl_vbo_draw_prims);
98
99 _math_init_transformation();
100 _math_init_translate();
101
102 return GL_TRUE;
103 }
104
105
106 void
107 _tnl_DestroyContext( struct gl_context *ctx )
108 {
109 struct tnl_shine_tab *s, *tmps;
110 TNLcontext *tnl = TNL_CONTEXT(ctx);
111
112 /* Free lighting shininess exponentiation table */
113 foreach_s( s, tmps, tnl->_ShineTabList ) {
114 free( s );
115 }
116 free( tnl->_ShineTabList );
117
118 _tnl_destroy_pipeline( ctx );
119
120 free(tnl);
121 ctx->swtnl_context = NULL;
122 }
123
124
125 void
126 _tnl_InvalidateState( struct gl_context *ctx, GLuint new_state )
127 {
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;
131 GLuint i;
132
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;
137 }
138
139 tnl->pipeline.new_state |= new_state;
140
141 /* Calculate tnl->render_inputs. This bitmask indicates which vertex
142 * attributes need to be emitted to the rasterizer.
143 */
144 tnl->render_inputs_bitset = BITFIELD64_BIT(_TNL_ATTRIB_POS);
145
146 if (!fp || (fp->Base.InputsRead & VARYING_BIT_COL0)) {
147 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR0);
148 }
149
150 if (_mesa_need_secondary_color(ctx))
151 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR1);
152
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));
157 }
158 }
159
160 if (ctx->Fog.Enabled
161 || (fp != NULL && (fp->Base.InputsRead & VARYING_BIT_FOGC) != 0)) {
162 /* Either fixed-function fog or a fragment program needs fog coord.
163 */
164 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_FOG);
165 }
166
167 if (ctx->Polygon.FrontMode != GL_FILL ||
168 ctx->Polygon.BackMode != GL_FILL)
169 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_EDGEFLAG);
170
171 if (ctx->RenderMode == GL_FEEDBACK)
172 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX0);
173
174 if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled)
175 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE);
176
177 /* check for varying vars which are written by the vertex program */
178 if (vp) {
179 GLuint i;
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));
183 }
184 }
185 }
186 }
187
188
189 void
190 _tnl_wakeup( struct gl_context *ctx )
191 {
192 /* Assume we haven't been getting state updates either:
193 */
194 _tnl_InvalidateState( ctx, ~0 );
195
196 #if 0
197 if (ctx->Light.ColorMaterialEnabled) {
198 _mesa_update_color_material( ctx,
199 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
200 }
201 #endif
202 }
203
204
205
206
207 /**
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.
211 */
212 void
213 _tnl_need_projected_coords( struct gl_context *ctx, GLboolean mode )
214 {
215 TNLcontext *tnl = TNL_CONTEXT(ctx);
216 tnl->NeedNdcCoords = mode;
217 }
218
219 void
220 _tnl_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
221 {
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;
226
227 }
228
229 void
230 _tnl_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
231 {
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;
236 }
237