2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keithw@vmware.com>
28 #include "main/glheader.h"
29 #include "main/imports.h"
30 #include "main/macros.h"
31 #include "main/state.h"
33 #include "tnl/t_context.h"
34 #include "tnl/t_pipeline.h"
35 #include "tnl/t_vertex.h"
36 #include "swrast_setup.h"
37 #include "ss_context.h"
38 #include "ss_triangle.h"
41 /* Need to check lighting state and vertex program state to know
42 * if two-sided lighting is in effect.
44 #define _SWSETUP_NEW_RENDERINDEX (_NEW_POLYGON|_NEW_LIGHT|_NEW_PROGRAM)
47 #define VARYING_EMIT_STYLE EMIT_4F
51 _swsetup_CreateContext( struct gl_context
*ctx
)
53 SScontext
*swsetup
= calloc(1, sizeof(SScontext
));
58 ctx
->swsetup_context
= swsetup
;
60 swsetup
->NewState
= ~0;
61 _swsetup_trifuncs_init( ctx
);
63 _tnl_init_vertices( ctx
, ctx
->Const
.MaxArrayLockSize
+ 12,
71 _swsetup_DestroyContext( struct gl_context
*ctx
)
73 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
77 ctx
->swsetup_context
= 0;
80 _tnl_free_vertices( ctx
);
84 _swsetup_RenderPrimitive( struct gl_context
*ctx
, GLenum mode
)
86 SWSETUP_CONTEXT(ctx
)->render_prim
= mode
;
87 _swrast_render_primitive( ctx
, mode
);
92 * Helper macros for setup_vertex_format()
94 #define SWZ ((SWvertex *)0)
95 #define SWOffset(MEMBER) (((char *)&(SWZ->MEMBER)) - ((char *)SWZ))
97 #define EMIT_ATTR( ATTR, STYLE, MEMBER ) \
99 map[e].attrib = (ATTR); \
100 map[e].format = (STYLE); \
101 map[e].offset = SWOffset(MEMBER); \
107 * Tell the tnl module how to build SWvertex objects for swrast.
108 * We'll build the map[] array with that info and pass it to
109 * _tnl_install_attrs().
112 setup_vertex_format(struct gl_context
*ctx
)
114 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
115 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
116 GLboolean intColors
= !ctx
->FragmentProgram
._Current
117 && !_mesa_ati_fragment_shader_enabled(ctx
)
118 && ctx
->RenderMode
== GL_RENDER
119 && CHAN_TYPE
!= GL_FLOAT
;
121 if (intColors
!= swsetup
->intColors
||
122 tnl
->render_inputs_bitset
!= swsetup
->last_index_bitset
) {
123 GLbitfield64 index_bitset
= tnl
->render_inputs_bitset
;
124 struct tnl_attr_map map
[_TNL_ATTRIB_MAX
];
125 unsigned int i
, e
= 0;
127 swsetup
->intColors
= intColors
;
129 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, attrib
[VARYING_SLOT_POS
] );
131 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_COLOR0
)) {
132 if (swsetup
->intColors
)
133 EMIT_ATTR( _TNL_ATTRIB_COLOR0
, EMIT_4CHAN_4F_RGBA
, color
);
135 EMIT_ATTR( _TNL_ATTRIB_COLOR0
, EMIT_4F
, attrib
[VARYING_SLOT_COL0
]);
138 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_COLOR1
)) {
139 EMIT_ATTR( _TNL_ATTRIB_COLOR1
, EMIT_4F
, attrib
[VARYING_SLOT_COL1
]);
142 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_FOG
)) {
143 const GLint emit
= ctx
->FragmentProgram
._Current
? EMIT_4F
: EMIT_1F
;
144 EMIT_ATTR( _TNL_ATTRIB_FOG
, emit
, attrib
[VARYING_SLOT_FOGC
]);
147 if (index_bitset
& BITFIELD64_RANGE(_TNL_ATTRIB_TEX0
, _TNL_NUM_TEX
))
149 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
150 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_TEX(i
))) {
151 EMIT_ATTR( _TNL_ATTRIB_TEX(i
), EMIT_4F
,
152 attrib
[VARYING_SLOT_TEX0
+ i
] );
157 /* shader varying vars */
158 if (index_bitset
& BITFIELD64_RANGE(_TNL_ATTRIB_GENERIC0
, _TNL_NUM_GENERIC
)) {
159 for (i
= 0; i
< ctx
->Const
.MaxVarying
; i
++) {
160 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_GENERIC(i
))) {
161 EMIT_ATTR( _TNL_ATTRIB_GENERIC(i
), VARYING_EMIT_STYLE
,
162 attrib
[VARYING_SLOT_VAR0
+ i
] );
167 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE
))
168 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE
, EMIT_1F
, pointSize
);
170 _tnl_install_attrs( ctx
, map
, e
,
174 swsetup
->last_index_bitset
= index_bitset
;
180 * Prepare to render a vertex buffer.
181 * Called via tnl->Driver.Render.Start.
184 _swsetup_RenderStart( struct gl_context
*ctx
)
186 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
187 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
188 struct vertex_buffer
*VB
= &tnl
->vb
;
190 if (swsetup
->NewState
& _SWSETUP_NEW_RENDERINDEX
) {
191 _swsetup_choose_trifuncs(ctx
);
194 if (swsetup
->NewState
& _NEW_PROGRAM
) {
195 swsetup
->last_index_bitset
= 0;
198 swsetup
->NewState
= 0;
200 /* This will change if drawing unfilled tris */
201 _swrast_SetFacing(ctx
, 0);
203 _swrast_render_start(ctx
);
206 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
208 setup_vertex_format(ctx
);
213 * We patch this function into tnl->Driver.Render.Finish.
214 * It's called when we finish rendering a vertex buffer.
217 _swsetup_RenderFinish( struct gl_context
*ctx
)
219 _swrast_render_finish( ctx
);
223 _swsetup_InvalidateState( struct gl_context
*ctx
, GLuint new_state
)
225 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
226 swsetup
->NewState
|= new_state
;
227 _tnl_invalidate_vertex_state( ctx
, new_state
);
232 _swsetup_Wakeup( struct gl_context
*ctx
)
234 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
235 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
237 tnl
->Driver
.Render
.Start
= _swsetup_RenderStart
;
238 tnl
->Driver
.Render
.Finish
= _swsetup_RenderFinish
;
239 tnl
->Driver
.Render
.PrimitiveNotify
= _swsetup_RenderPrimitive
;
240 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
241 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
242 tnl
->Driver
.Render
.ClippedPolygon
= _tnl_RenderClippedPolygon
; /* new */
243 tnl
->Driver
.Render
.ClippedLine
= _tnl_RenderClippedLine
; /* new */
248 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
249 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
250 tnl
->Driver
.Render
.ResetLineStipple
= _swrast_ResetLineStipple
;
251 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
252 tnl
->Driver
.Render
.Multipass
= 0;
254 _tnl_invalidate_vertices( ctx
, ~0 );
255 _tnl_need_projected_coords( ctx
, GL_TRUE
);
256 _swsetup_InvalidateState( ctx
, ~0 );
258 swsetup
->verts
= (SWvertex
*)tnl
->clipspace
.vertex_buf
;
259 swsetup
->last_index_bitset
= 0;
264 * Populate a swrast SWvertex from an attrib-style vertex.
267 _swsetup_Translate( struct gl_context
*ctx
, const void *vertex
, SWvertex
*dest
)
269 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
270 const GLfloat
*m
= tnl
->_WindowMap
.m
;
274 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_POS
, tmp
);
276 dest
->attrib
[VARYING_SLOT_POS
][0] = m
[0] * tmp
[0] + m
[12];
277 dest
->attrib
[VARYING_SLOT_POS
][1] = m
[5] * tmp
[1] + m
[13];
278 dest
->attrib
[VARYING_SLOT_POS
][2] = m
[10] * tmp
[2] + m
[14];
279 dest
->attrib
[VARYING_SLOT_POS
][3] = tmp
[3];
281 /** XXX try to limit these loops someday */
282 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
283 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_TEX0
+ i
,
284 dest
->attrib
[VARYING_SLOT_TEX0
+ i
] );
286 for (i
= 0 ; i
< ctx
->Const
.MaxVarying
; i
++)
287 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_GENERIC0
+ i
,
288 dest
->attrib
[VARYING_SLOT_VAR0
+ i
] );
290 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_COLOR0
,
291 dest
->attrib
[VARYING_SLOT_COL0
] );
293 UNCLAMPED_FLOAT_TO_RGBA_CHAN(dest
->color
, dest
->attrib
[VARYING_SLOT_COL0
]);
295 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_COLOR1
,
296 dest
->attrib
[VARYING_SLOT_COL1
]);
298 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_FOG
, tmp
);
299 dest
->attrib
[VARYING_SLOT_FOGC
][0] = tmp
[0];
301 /* XXX See _tnl_get_attr about pointsize ... */
302 _tnl_get_attr( ctx
, vertex
, _TNL_ATTRIB_POINTSIZE
, tmp
);
303 dest
->pointSize
= tmp
[0];