2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 * Keith Whitwell <keith@tungstengraphics.com>
28 #include "main/glheader.h"
29 #include "main/imports.h"
30 #include "main/colormac.h"
32 #include "tnl/t_context.h"
33 #include "tnl/t_pipeline.h"
34 #include "tnl/t_vertex.h"
35 #include "swrast_setup.h"
36 #include "ss_context.h"
37 #include "ss_triangle.h"
40 /* Need to check lighting state and vertex program state to know
41 * if two-sided lighting is in effect.
43 #define _SWSETUP_NEW_RENDERINDEX (_NEW_POLYGON|_NEW_LIGHT)
46 #define VARYING_EMIT_STYLE EMIT_4F
50 _swsetup_CreateContext( struct gl_context
*ctx
)
52 SScontext
*swsetup
= (SScontext
*)CALLOC(sizeof(SScontext
));
57 ctx
->swsetup_context
= swsetup
;
59 swsetup
->NewState
= ~0;
60 _swsetup_trifuncs_init( ctx
);
62 _tnl_init_vertices( ctx
, ctx
->Const
.MaxArrayLockSize
+ 12,
70 _swsetup_DestroyContext( struct gl_context
*ctx
)
72 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
76 ctx
->swsetup_context
= 0;
79 _tnl_free_vertices( ctx
);
83 _swsetup_RenderPrimitive( struct gl_context
*ctx
, GLenum mode
)
85 SWSETUP_CONTEXT(ctx
)->render_prim
= mode
;
86 _swrast_render_primitive( ctx
, mode
);
91 * Helper macros for setup_vertex_format()
93 #define SWZ ((SWvertex *)0)
94 #define SWOffset(MEMBER) (((char *)&(SWZ->MEMBER)) - ((char *)SWZ))
96 #define EMIT_ATTR( ATTR, STYLE, MEMBER ) \
98 map[e].attrib = (ATTR); \
99 map[e].format = (STYLE); \
100 map[e].offset = SWOffset(MEMBER); \
106 * Tell the tnl module how to build SWvertex objects for swrast.
107 * We'll build the map[] array with that info and pass it to
108 * _tnl_install_attrs().
111 setup_vertex_format(struct gl_context
*ctx
)
113 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
114 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
115 GLboolean intColors
= ctx
->RenderMode
== GL_RENDER
116 && CHAN_TYPE
!= GL_FLOAT
;
118 if (intColors
!= swsetup
->intColors
||
119 tnl
->render_inputs_bitset
!= swsetup
->last_index_bitset
) {
120 GLbitfield64 index_bitset
= tnl
->render_inputs_bitset
;
121 struct tnl_attr_map map
[_TNL_ATTRIB_MAX
];
124 swsetup
->intColors
= intColors
;
126 EMIT_ATTR( _TNL_ATTRIB_POS
, EMIT_4F_VIEWPORT
, attrib
[FRAG_ATTRIB_WPOS
] );
128 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_COLOR
)) {
129 if (swsetup
->intColors
)
130 EMIT_ATTR( _TNL_ATTRIB_COLOR
, EMIT_4CHAN_4F_RGBA
, color
);
132 EMIT_ATTR( _TNL_ATTRIB_COLOR
, EMIT_4F
, attrib
[FRAG_ATTRIB_COL
]);
135 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_FOG
)) {
136 EMIT_ATTR( _TNL_ATTRIB_FOG
, EMIT_1F
, attrib
[FRAG_ATTRIB_FOGC
]);
139 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_TEX
)) {
140 EMIT_ATTR( _TNL_ATTRIB_TEX
, EMIT_4F
,
141 attrib
[FRAG_ATTRIB_TEX
] );
144 if (index_bitset
& BITFIELD64_BIT(_TNL_ATTRIB_POINTSIZE
))
145 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE
, EMIT_1F
, pointSize
);
147 _tnl_install_attrs( ctx
, map
, e
,
148 ctx
->Viewport
._WindowMap
.m
,
151 swsetup
->last_index_bitset
= index_bitset
;
157 * Prepare to render a vertex buffer.
158 * Called via tnl->Driver.Render.Start.
161 _swsetup_RenderStart( struct gl_context
*ctx
)
163 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
164 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
165 struct vertex_buffer
*VB
= &tnl
->vb
;
167 if (swsetup
->NewState
& _SWSETUP_NEW_RENDERINDEX
) {
168 _swsetup_choose_trifuncs(ctx
);
171 swsetup
->NewState
= 0;
173 _swrast_render_start(ctx
);
176 VB
->AttribPtr
[VERT_ATTRIB_POS
] = VB
->NdcPtr
;
178 setup_vertex_format(ctx
);
183 * We patch this function into tnl->Driver.Render.Finish.
184 * It's called when we finish rendering a vertex buffer.
187 _swsetup_RenderFinish( struct gl_context
*ctx
)
189 _swrast_render_finish( ctx
);
193 _swsetup_InvalidateState( struct gl_context
*ctx
, GLuint new_state
)
195 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
196 swsetup
->NewState
|= new_state
;
197 _tnl_invalidate_vertex_state( ctx
, new_state
);
202 _swsetup_Wakeup( struct gl_context
*ctx
)
204 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
205 SScontext
*swsetup
= SWSETUP_CONTEXT(ctx
);
207 tnl
->Driver
.Render
.Start
= _swsetup_RenderStart
;
208 tnl
->Driver
.Render
.Finish
= _swsetup_RenderFinish
;
209 tnl
->Driver
.Render
.PrimitiveNotify
= _swsetup_RenderPrimitive
;
210 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
211 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
212 tnl
->Driver
.Render
.ClippedPolygon
= _tnl_RenderClippedPolygon
; /* new */
213 tnl
->Driver
.Render
.ClippedLine
= _tnl_RenderClippedLine
; /* new */
218 tnl
->Driver
.Render
.PrimTabVerts
= _tnl_render_tab_verts
;
219 tnl
->Driver
.Render
.PrimTabElts
= _tnl_render_tab_elts
;
220 tnl
->Driver
.Render
.ResetLineStipple
= _swrast_ResetLineStipple
;
221 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
222 tnl
->Driver
.Render
.Multipass
= 0;
224 _tnl_invalidate_vertices( ctx
, ~0 );
225 _tnl_need_projected_coords( ctx
, GL_TRUE
);
226 _swsetup_InvalidateState( ctx
, ~0 );
228 swsetup
->verts
= (SWvertex
*)tnl
->clipspace
.vertex_buf
;
229 swsetup
->last_index_bitset
= 0;