1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@vmware.com>
26 */
27
28
29 #include "main/glheader.h"
30
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 #include "main/state.h"
38 #include "main/viewport.h"
39 #include "util/simple_list.h"
40 #include "util/u_memory.h"
41
42 #include "tnl.h"
43 #include "t_context.h"
44 #include "t_pipeline.h"
45
46 #include "vbo/vbo.h"
47
48 GLboolean
_tnl_CreateContext(struct gl_context * ctx)49 _tnl_CreateContext( struct gl_context *ctx )
50 {
51 TNLcontext *tnl;
52 GLuint i;
53
54 /* Create the TNLcontext structure
55 */
56 ctx->swtnl_context = tnl = calloc(1, sizeof(TNLcontext));
57
58 if (!tnl) {
59 return GL_FALSE;
60 }
61
62 /* Initialize the VB.
63 */
64 tnl->vb.Size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
65
66
67 /* Initialize tnl state.
68 */
69 if (ctx->VertexProgram._MaintainTnlProgram) {
70 _tnl_install_pipeline( ctx, _tnl_vp_pipeline );
71 } else {
72 _tnl_install_pipeline( ctx, _tnl_default_pipeline );
73 }
74
75 _math_matrix_ctr(&tnl->_WindowMap);
76
77 tnl->NeedNdcCoords = GL_TRUE;
78 tnl->AllowVertexFog = GL_TRUE;
79 tnl->AllowPixelFog = GL_TRUE;
80
81 /* Set a few default values in the driver struct.
82 */
83 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
84 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
85 tnl->Driver.NotifyMaterialChange = _tnl_validate_shine_tables;
86
87 tnl->nr_blocks = 0;
88
89 /* Lighting miscellaneous */
90 tnl->_ShineTabList = MALLOC_STRUCT( tnl_shine_tab );
91 make_empty_list( tnl->_ShineTabList );
92 /* Allocate 10 (arbitrary) shininess lookup tables */
93 for (i = 0 ; i < 10 ; i++) {
94 struct tnl_shine_tab *s = MALLOC_STRUCT( tnl_shine_tab );
95 s->shininess = -1;
96 s->refcount = 0;
97 insert_at_tail( tnl->_ShineTabList, s );
98 }
99
100 _math_init_transformation();
101 _math_init_translate();
102
103 /* Keep our list of tnl_vertex_array inputs */
104 _tnl_init_inputs(&tnl->draw_arrays);
105
106 return GL_TRUE;
107 }
108
109
110 void
_tnl_DestroyContext(struct gl_context * ctx)111 _tnl_DestroyContext( struct gl_context *ctx )
112 {
113 struct tnl_shine_tab *s, *tmps;
114 TNLcontext *tnl = TNL_CONTEXT(ctx);
115
116 _math_matrix_dtr(&tnl->_WindowMap);
117
118 /* Free lighting shininess exponentiation table */
119 foreach_s( s, tmps, tnl->_ShineTabList ) {
120 free( s );
121 }
122 free( tnl->_ShineTabList );
123
124 _tnl_destroy_pipeline( ctx );
125
126 free(tnl);
127 ctx->swtnl_context = NULL;
128 }
129
130
131 void
_tnl_InvalidateState(struct gl_context * ctx,GLuint new_state)132 _tnl_InvalidateState( struct gl_context *ctx, GLuint new_state )
133 {
134 TNLcontext *tnl = TNL_CONTEXT(ctx);
135 const struct gl_program *vp = ctx->VertexProgram._Current;
136 const struct gl_program *fp = ctx->FragmentProgram._Current;
137 GLuint i;
138
139 if (new_state & (_NEW_HINT | _NEW_PROGRAM)) {
140 assert(tnl->AllowVertexFog || tnl->AllowPixelFog);
141 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
142 || !tnl->AllowPixelFog) && !fp;
143 }
144
145 tnl->pipeline.new_state |= new_state;
146
147 /* Calculate tnl->render_inputs. This bitmask indicates which vertex
148 * attributes need to be emitted to the rasterizer.
149 */
150 tnl->render_inputs_bitset = BITFIELD64_BIT(_TNL_ATTRIB_POS);
151
152 if (!fp || (fp->info.inputs_read & VARYING_BIT_COL0)) {
153 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR0);
154 }
155
156 if (_mesa_need_secondary_color(ctx))
157 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR1);
158
159 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
160 if (ctx->Texture._EnabledCoordUnits & (1 << i) ||
161 (fp && fp->info.inputs_read & VARYING_BIT_TEX(i)) ||
162 _mesa_ati_fragment_shader_enabled(ctx)) {
163 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX(i));
164 }
165 }
166
167 if (ctx->Fog.Enabled
168 || (fp != NULL && (fp->info.inputs_read & VARYING_BIT_FOGC) != 0)) {
169 /* Either fixed-function fog or a fragment program needs fog coord.
170 */
171 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_FOG);
172 }
173
174 if (ctx->Polygon.FrontMode != GL_FILL ||
175 ctx->Polygon.BackMode != GL_FILL)
176 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_EDGEFLAG);
177
178 if (ctx->RenderMode == GL_FEEDBACK)
179 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX0);
180
181 if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled)
182 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE);
183
184 /* check for varying vars which are written by the vertex program */
185 if (vp) {
186 GLuint i;
187 for (i = 0; i < MAX_VARYING; i++) {
188 if (vp->info.outputs_written &
189 BITFIELD64_BIT(VARYING_SLOT_VAR0 + i)) {
190 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_GENERIC(i));
191 }
192 }
193 }
194
195 if (new_state & (_NEW_VIEWPORT | _NEW_BUFFERS)) {
196 float scale[3], translate[3];
197 _mesa_get_viewport_xform(ctx, 0, scale, translate);
198 _math_matrix_viewport(&tnl->_WindowMap, scale, translate,
199 ctx->DrawBuffer->_DepthMaxF);
200 }
201 }
202
203
204 void
_tnl_wakeup(struct gl_context * ctx)205 _tnl_wakeup( struct gl_context *ctx )
206 {
207 /* Assume we haven't been getting state updates either:
208 */
209 _tnl_InvalidateState( ctx, ~0 );
210
211 #if 0
212 if (ctx->Light.ColorMaterialEnabled) {
213 _mesa_update_color_material( ctx,
214 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
215 }
216 #endif
217 }
218
219
220
221
222 /**
223 * Drivers call this function to tell the TCL module whether or not
224 * it wants Normalized Device Coords (NDC) computed. I.e. whether
225 * we should "Divide-by-W". Software renders will want that.
226 */
227 void
_tnl_need_projected_coords(struct gl_context * ctx,GLboolean mode)228 _tnl_need_projected_coords( struct gl_context *ctx, GLboolean mode )
229 {
230 TNLcontext *tnl = TNL_CONTEXT(ctx);
231 tnl->NeedNdcCoords = mode;
232 }
233
234 void
_tnl_allow_vertex_fog(struct gl_context * ctx,GLboolean value)235 _tnl_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
236 {
237 TNLcontext *tnl = TNL_CONTEXT(ctx);
238 tnl->AllowVertexFog = value;
239 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
240 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
241
242 }
243
244 void
_tnl_allow_pixel_fog(struct gl_context * ctx,GLboolean value)245 _tnl_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
246 {
247 TNLcontext *tnl = TNL_CONTEXT(ctx);
248 tnl->AllowPixelFog = value;
249 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
250 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
251 }
252
253