~ubuntu-branches/ubuntu/quantal/mesa-glw/quantal

« back to all changes in this revision

Viewing changes to src/mesa/swrast_setup/ss_context.c

  • Committer: Bazaar Package Importer
  • Author(s): Morten Kjeldgaard
  • Date: 2008-05-06 16:19:15 UTC
  • Revision ID: james.westby@ubuntu.com-20080506161915-uynz7nftmfixu6bq
Tags: upstream-7.0.3
ImportĀ upstreamĀ versionĀ 7.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Mesa 3-D graphics library
 
3
 * Version:  6.5.3
 
4
 *
 
5
 * Copyright (C) 1999-2007  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
 * 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.
 
23
 *
 
24
 * Authors:
 
25
 *    Keith Whitwell <keith@tungstengraphics.com>
 
26
 */
 
27
 
 
28
#include "glheader.h"
 
29
#include "imports.h"
 
30
#include "colormac.h"
 
31
#include "ss_context.h"
 
32
#include "ss_triangle.h"
 
33
#include "swrast_setup.h"
 
34
#include "tnl/tnl.h"
 
35
#include "tnl/t_context.h"
 
36
#include "tnl/t_pipeline.h"
 
37
#include "tnl/t_vertex.h"
 
38
 
 
39
/* Need to check lighting state and vertex program state to know
 
40
 * if two-sided lighting is in effect.
 
41
 */
 
42
#define _SWSETUP_NEW_RENDERINDEX (_NEW_POLYGON|_NEW_LIGHT|_NEW_PROGRAM)
 
43
 
 
44
 
 
45
#define VARYING_EMIT_STYLE  EMIT_4F
 
46
 
 
47
 
 
48
GLboolean
 
49
_swsetup_CreateContext( GLcontext *ctx )
 
50
{
 
51
   SScontext *swsetup = (SScontext *)CALLOC(sizeof(SScontext));
 
52
 
 
53
   if (!swsetup)
 
54
      return GL_FALSE;
 
55
 
 
56
   ctx->swsetup_context = swsetup;
 
57
 
 
58
   swsetup->NewState = ~0;
 
59
   _swsetup_trifuncs_init( ctx );
 
60
 
 
61
   _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12, 
 
62
                       sizeof(SWvertex) );
 
63
 
 
64
 
 
65
   return GL_TRUE;
 
66
}
 
67
 
 
68
void
 
69
_swsetup_DestroyContext( GLcontext *ctx )
 
70
{
 
71
   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
 
72
 
 
73
   if (swsetup) {
 
74
      FREE(swsetup);
 
75
      ctx->swsetup_context = 0;
 
76
   }
 
77
 
 
78
   _tnl_free_vertices( ctx );
 
79
}
 
80
 
 
81
static void
 
82
_swsetup_RenderPrimitive( GLcontext *ctx, GLenum mode )
 
83
{
 
84
   SWSETUP_CONTEXT(ctx)->render_prim = mode;
 
85
   _swrast_render_primitive( ctx, mode );
 
86
}
 
87
 
 
88
 
 
89
/**
 
90
 * Helper macros for setup_vertex_format()
 
91
 */
 
92
#define SWZ ((SWvertex *)0)
 
93
#define SWOffset(MEMBER) (((char *)&(SWZ->MEMBER)) - ((char *)SWZ))
 
94
 
 
95
#define EMIT_ATTR( ATTR, STYLE, MEMBER )        \
 
96
do {                                            \
 
97
   map[e].attrib = (ATTR);                      \
 
98
   map[e].format = (STYLE);                     \
 
99
   map[e].offset = SWOffset(MEMBER);            \
 
100
   e++;                                         \
 
101
} while (0)
 
102
 
 
103
 
 
104
/**
 
105
 * Tell the tnl module how to build SWvertex objects for swrast.
 
106
 * We'll build the map[] array with that info and pass it to
 
107
 * _tnl_install_attrs().
 
108
 */
 
109
static void
 
110
setup_vertex_format(GLcontext *ctx)
 
111
{
 
112
   TNLcontext *tnl = TNL_CONTEXT(ctx);
 
113
   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
 
114
 
 
115
   if (!RENDERINPUTS_EQUAL(tnl->render_inputs_bitset,
 
116
                           swsetup->last_index_bitset)) {
 
117
      DECLARE_RENDERINPUTS(index_bitset);
 
118
      struct tnl_attr_map map[_TNL_ATTRIB_MAX];
 
119
      int i, e = 0;
 
120
 
 
121
      RENDERINPUTS_COPY( index_bitset, tnl->render_inputs_bitset );
 
122
 
 
123
      EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, win );
 
124
 
 
125
      if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR0 ))
 
126
         EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4CHAN_4F_RGBA, color );
 
127
 
 
128
      if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 ))
 
129
         EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_4CHAN_4F_RGBA, specular);
 
130
 
 
131
      if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR_INDEX ))
 
132
         EMIT_ATTR( _TNL_ATTRIB_COLOR_INDEX, EMIT_1F, index );
 
133
 
 
134
      if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_FOG )) {
 
135
         const GLint emit = ctx->FragmentProgram._Current ? EMIT_4F : EMIT_1F;
 
136
         EMIT_ATTR( _TNL_ATTRIB_FOG, emit, attrib[FRAG_ATTRIB_FOGC]);
 
137
      }
 
138
 
 
139
      if (RENDERINPUTS_TEST_RANGE(index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX))
 
140
      {
 
141
         for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
 
142
            if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
 
143
               EMIT_ATTR( _TNL_ATTRIB_TEX(i), EMIT_4F,
 
144
                          attrib[FRAG_ATTRIB_TEX0 + i] );
 
145
            }
 
146
         }
 
147
      }
 
148
 
 
149
      /* shader varying vars */
 
150
      if (RENDERINPUTS_TEST_RANGE( index_bitset,
 
151
                                   _TNL_FIRST_GENERIC, _TNL_LAST_GENERIC )) {
 
152
         for (i = 0; i < ctx->Const.MaxVarying; i++) {
 
153
            if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_GENERIC(i) )) {
 
154
               EMIT_ATTR( _TNL_ATTRIB_GENERIC(i), VARYING_EMIT_STYLE,
 
155
                          attrib[FRAG_ATTRIB_VAR0 + i] );
 
156
            }
 
157
         }
 
158
      }
 
159
 
 
160
      if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POINTSIZE ))
 
161
         EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F, pointSize );
 
162
 
 
163
      _tnl_install_attrs( ctx, map, e,
 
164
                          ctx->Viewport._WindowMap.m,
 
165
                          sizeof(SWvertex) );
 
166
 
 
167
      RENDERINPUTS_COPY( swsetup->last_index_bitset, index_bitset );
 
168
   }
 
169
}
 
170
 
 
171
 
 
172
/**
 
173
 * Prepare to render a vertex buffer.
 
174
 * Called via tnl->Driver.Render.Start.
 
175
 */
 
176
static void
 
177
_swsetup_RenderStart( GLcontext *ctx )
 
178
{
 
179
   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
 
180
   TNLcontext *tnl = TNL_CONTEXT(ctx);
 
181
   struct vertex_buffer *VB = &tnl->vb;
 
182
 
 
183
   if (swsetup->NewState & _SWSETUP_NEW_RENDERINDEX) {
 
184
      _swsetup_choose_trifuncs(ctx);
 
185
   }
 
186
 
 
187
   swsetup->NewState = 0;
 
188
 
 
189
   /* This will change if drawing unfilled tris */
 
190
   _swrast_SetFacing(ctx, 0);
 
191
 
 
192
   _swrast_render_start(ctx);
 
193
 
 
194
   /* Important */
 
195
   VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
 
196
 
 
197
   setup_vertex_format(ctx);
 
198
}
 
199
 
 
200
 
 
201
/*
 
202
 * We patch this function into tnl->Driver.Render.Finish.
 
203
 * It's called when we finish rendering a vertex buffer.
 
204
 */
 
205
static void
 
206
_swsetup_RenderFinish( GLcontext *ctx )
 
207
{
 
208
   _swrast_render_finish( ctx );
 
209
}
 
210
 
 
211
void
 
212
_swsetup_InvalidateState( GLcontext *ctx, GLuint new_state )
 
213
{
 
214
   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
 
215
   swsetup->NewState |= new_state;
 
216
   _tnl_invalidate_vertex_state( ctx, new_state );
 
217
}
 
218
 
 
219
 
 
220
void
 
221
_swsetup_Wakeup( GLcontext *ctx )
 
222
{
 
223
   TNLcontext *tnl = TNL_CONTEXT(ctx);
 
224
   SScontext *swsetup = SWSETUP_CONTEXT(ctx);
 
225
 
 
226
   tnl->Driver.Render.Start = _swsetup_RenderStart;
 
227
   tnl->Driver.Render.Finish = _swsetup_RenderFinish;
 
228
   tnl->Driver.Render.PrimitiveNotify = _swsetup_RenderPrimitive;
 
229
   tnl->Driver.Render.Interp = _tnl_interp;
 
230
   tnl->Driver.Render.CopyPV = _tnl_copy_pv;
 
231
   tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon; /* new */
 
232
   tnl->Driver.Render.ClippedLine = _tnl_RenderClippedLine; /* new */
 
233
   /* points */
 
234
   /* line */
 
235
   /* triangle */
 
236
   /* quad */
 
237
   tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
 
238
   tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
 
239
   tnl->Driver.Render.ResetLineStipple = _swrast_ResetLineStipple;
 
240
   tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
 
241
   tnl->Driver.Render.Multipass = 0;
 
242
 
 
243
   _tnl_invalidate_vertices( ctx, ~0 );
 
244
   _tnl_need_projected_coords( ctx, GL_TRUE );
 
245
   _swsetup_InvalidateState( ctx, ~0 );
 
246
 
 
247
   swsetup->verts = (SWvertex *)tnl->clipspace.vertex_buf;
 
248
   RENDERINPUTS_ZERO( swsetup->last_index_bitset );
 
249
}
 
250
 
 
251
 
 
252
/**
 
253
 * Populate a swrast SWvertex from an attrib-style vertex.
 
254
 */
 
255
void 
 
256
_swsetup_Translate( GLcontext *ctx, const void *vertex, SWvertex *dest )
 
257
{
 
258
   const GLfloat *m = ctx->Viewport._WindowMap.m;
 
259
   GLfloat tmp[4];
 
260
   GLuint i;
 
261
 
 
262
   _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POS, tmp );
 
263
 
 
264
   dest->win[0] = m[0]  * tmp[0] + m[12];
 
265
   dest->win[1] = m[5]  * tmp[1] + m[13];
 
266
   dest->win[2] = m[10] * tmp[2] + m[14];
 
267
   dest->win[3] =         tmp[3];
 
268
 
 
269
   /** XXX try to limit these loops someday */
 
270
   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
 
271
      _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_TEX0+i,
 
272
                     dest->attrib[FRAG_ATTRIB_TEX0 + i] );
 
273
 
 
274
   for (i = 0 ; i < ctx->Const.MaxVarying ; i++)
 
275
      _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_GENERIC0+i,
 
276
                     dest->attrib[FRAG_ATTRIB_VAR0 + i] );
 
277
 
 
278
   _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR0, tmp );
 
279
   UNCLAMPED_FLOAT_TO_RGBA_CHAN( dest->color, tmp );
 
280
 
 
281
   _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR1, tmp );
 
282
   UNCLAMPED_FLOAT_TO_RGBA_CHAN( dest->specular, tmp );
 
283
 
 
284
   _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_FOG, tmp );
 
285
   dest->attrib[FRAG_ATTRIB_FOGC][0] = tmp[0];
 
286
 
 
287
   _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_COLOR_INDEX, tmp );
 
288
   dest->index = tmp[0];
 
289
 
 
290
   /* XXX See _tnl_get_attr about pointsize ... */
 
291
   _tnl_get_attr( ctx, vertex, _TNL_ATTRIB_POINTSIZE, tmp );
 
292
   dest->pointSize = tmp[0];
 
293
}
 
294