5b9dd54d75a834e7f55f888f41c814802e5f95c4
[mesa.git] / src / mesa / tnl / t_context.c
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 #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 #include "math/m_translate.h"
36 #include "math/m_xform.h"
37 #include "main/state.h"
38 #include "main/viewport.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 _math_matrix_ctr(&tnl->_WindowMap);
74
75 tnl->NeedNdcCoords = GL_TRUE;
76 tnl->AllowVertexFog = GL_TRUE;
77 tnl->AllowPixelFog = GL_TRUE;
78
79 /* Set a few default values in the driver struct.
80 */
81 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
82 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
83 tnl->Driver.NotifyMaterialChange = _tnl_validate_shine_tables;
84
85 tnl->nr_blocks = 0;
86
87 /* Lighting miscellaneous */
88 tnl->_ShineTabList = MALLOC_STRUCT( tnl_shine_tab );
89 make_empty_list( tnl->_ShineTabList );
90 /* Allocate 10 (arbitrary) shininess lookup tables */
91 for (i = 0 ; i < 10 ; i++) {
92 struct tnl_shine_tab *s = MALLOC_STRUCT( tnl_shine_tab );
93 s->shininess = -1;
94 s->refcount = 0;
95 insert_at_tail( tnl->_ShineTabList, s );
96 }
97
98 /* plug in the VBO drawing function */
99 vbo_set_draw_func(ctx, _tnl_draw_prims);
100
101 _math_init_transformation();
102 _math_init_translate();
103
104 return GL_TRUE;
105 }
106
107
108 void
109 _tnl_DestroyContext( struct gl_context *ctx )
110 {
111 struct tnl_shine_tab *s, *tmps;
112 TNLcontext *tnl = TNL_CONTEXT(ctx);
113
114 _math_matrix_dtr(&tnl->_WindowMap);
115
116 /* Free lighting shininess exponentiation table */
117 foreach_s( s, tmps, tnl->_ShineTabList ) {
118 free( s );
119 }
120 free( tnl->_ShineTabList );
121
122 _tnl_destroy_pipeline( ctx );
123
124 free(tnl);
125 ctx->swtnl_context = NULL;
126 }
127
128
129 void
130 _tnl_InvalidateState( struct gl_context *ctx, GLuint new_state )
131 {
132 TNLcontext *tnl = TNL_CONTEXT(ctx);
133 const struct gl_vertex_program *vp = ctx->VertexProgram._Current;
134 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
135 GLuint i;
136
137 if (new_state & (_NEW_HINT | _NEW_PROGRAM)) {
138 assert(tnl->AllowVertexFog || tnl->AllowPixelFog);
139 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
140 || !tnl->AllowPixelFog) && !fp;
141 }
142
143 tnl->pipeline.new_state |= new_state;
144
145 /* Calculate tnl->render_inputs. This bitmask indicates which vertex
146 * attributes need to be emitted to the rasterizer.
147 */
148 tnl->render_inputs_bitset = BITFIELD64_BIT(_TNL_ATTRIB_POS);
149
150 if (!fp || (fp->Base.InputsRead & VARYING_BIT_COL0)) {
151 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR0);
152 }
153
154 if (_mesa_need_secondary_color(ctx))
155 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_COLOR1);
156
157 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
158 if (ctx->Texture._EnabledCoordUnits & (1 << i) ||
159 (fp && fp->Base.InputsRead & VARYING_BIT_TEX(i))) {
160 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX(i));
161 }
162 }
163
164 if (ctx->Fog.Enabled
165 || (fp != NULL && (fp->Base.InputsRead & VARYING_BIT_FOGC) != 0)) {
166 /* Either fixed-function fog or a fragment program needs fog coord.
167 */
168 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_FOG);
169 }
170
171 if (ctx->Polygon.FrontMode != GL_FILL ||
172 ctx->Polygon.BackMode != GL_FILL)
173 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_EDGEFLAG);
174
175 if (ctx->RenderMode == GL_FEEDBACK)
176 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_TEX0);
177
178 if (ctx->Point._Attenuated || ctx->VertexProgram.PointSizeEnabled)
179 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE);
180
181 /* check for varying vars which are written by the vertex program */
182 if (vp) {
183 GLuint i;
184 for (i = 0; i < MAX_VARYING; i++) {
185 if (vp->Base.OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_VAR0 + i)) {
186 tnl->render_inputs_bitset |= BITFIELD64_BIT(_TNL_ATTRIB_GENERIC(i));
187 }
188 }
189 }
190
191 if (new_state & (_NEW_VIEWPORT | _NEW_BUFFERS)) {
192 double scale[3], translate[3];
193 _mesa_get_viewport_xform(ctx, 0, scale, translate);
194 _math_matrix_viewport(&tnl->_WindowMap, scale, translate,
195 ctx->DrawBuffer->_DepthMaxF);
196 }
197 }
198
199
200 void
201 _tnl_wakeup( struct gl_context *ctx )
202 {
203 /* Assume we haven't been getting state updates either:
204 */
205 _tnl_InvalidateState( ctx, ~0 );
206
207 #if 0
208 if (ctx->Light.ColorMaterialEnabled) {
209 _mesa_update_color_material( ctx,
210 ctx->Current.Attrib[VERT_ATTRIB_COLOR0] );
211 }
212 #endif
213 }
214
215
216
217
218 /**
219 * Drivers call this function to tell the TCL module whether or not
220 * it wants Normalized Device Coords (NDC) computed. I.e. whether
221 * we should "Divide-by-W". Software renders will want that.
222 */
223 void
224 _tnl_need_projected_coords( struct gl_context *ctx, GLboolean mode )
225 {
226 TNLcontext *tnl = TNL_CONTEXT(ctx);
227 tnl->NeedNdcCoords = mode;
228 }
229
230 void
231 _tnl_allow_vertex_fog( struct gl_context *ctx, GLboolean value )
232 {
233 TNLcontext *tnl = TNL_CONTEXT(ctx);
234 tnl->AllowVertexFog = value;
235 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
236 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
237
238 }
239
240 void
241 _tnl_allow_pixel_fog( struct gl_context *ctx, GLboolean value )
242 {
243 TNLcontext *tnl = TNL_CONTEXT(ctx);
244 tnl->AllowPixelFog = value;
245 tnl->_DoVertexFog = ((tnl->AllowVertexFog && (ctx->Hint.Fog != GL_NICEST))
246 || !tnl->AllowPixelFog) && !ctx->FragmentProgram._Current;
247 }
248