1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "st_context.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
40 static GLuint
translate_fill( GLenum mode
)
44 return PIPE_POLYGON_MODE_POINT
;
46 return PIPE_POLYGON_MODE_LINE
;
48 return PIPE_POLYGON_MODE_FILL
;
55 static GLboolean
get_offset_flag( GLuint fill_mode
,
56 const struct gl_polygon_attrib
*p
)
59 case PIPE_POLYGON_MODE_POINT
:
60 return p
->OffsetPoint
;
61 case PIPE_POLYGON_MODE_LINE
:
63 case PIPE_POLYGON_MODE_FILL
:
72 static void update_raster_state( struct st_context
*st
)
74 GLcontext
*ctx
= st
->ctx
;
75 struct pipe_rasterizer_state raster
;
76 const struct cso_rasterizer
*cso
;
79 memset(&raster
, 0, sizeof(raster
));
81 raster
.origin_lower_left
= 1; /* Always true for OpenGL */
83 /* _NEW_POLYGON, _NEW_BUFFERS
86 if (ctx
->Polygon
.FrontFace
== GL_CCW
)
87 raster
.front_winding
= PIPE_WINDING_CCW
;
89 raster
.front_winding
= PIPE_WINDING_CW
;
92 * I think the intention here is that user-created framebuffer objects
93 * use Y=0=TOP layout instead of OpenGL's normal Y=0=bottom layout.
94 * Flipping Y changes CW to CCW and vice-versa.
95 * But this is an implementation/driver-specific artifact - remove...
97 if (ctx
->DrawBuffer
&& ctx
->DrawBuffer
->Name
!= 0)
98 raster
.front_winding
^= PIPE_WINDING_BOTH
;
103 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
104 raster
.flatshade
= 1;
106 /* _NEW_LIGHT | _NEW_PROGRAM
108 * Back-face colors can come from traditional lighting (when
109 * GL_LIGHT_MODEL_TWO_SIDE is set) or from vertex programs (when
110 * GL_VERTEX_PROGRAM_TWO_SIDE is set). Note the logic here.
112 if (ctx
->VertexProgram
._Current
) {
113 raster
.light_twoside
= ctx
->VertexProgram
.TwoSideEnabled
;
115 else if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
) {
116 raster
.light_twoside
= 1;
121 if (ctx
->Polygon
.CullFlag
) {
122 if (ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
123 raster
.cull_mode
= PIPE_WINDING_BOTH
;
125 else if (ctx
->Polygon
.CullFaceMode
== GL_FRONT
) {
126 raster
.cull_mode
= raster
.front_winding
;
129 raster
.cull_mode
= raster
.front_winding
^ PIPE_WINDING_BOTH
;
136 GLuint fill_front
= translate_fill( ctx
->Polygon
.FrontMode
);
137 GLuint fill_back
= translate_fill( ctx
->Polygon
.BackMode
);
139 if (raster
.front_winding
== PIPE_WINDING_CW
) {
140 raster
.fill_cw
= fill_front
;
141 raster
.fill_ccw
= fill_back
;
144 raster
.fill_cw
= fill_back
;
145 raster
.fill_ccw
= fill_front
;
148 /* Simplify when culling is active:
150 if (raster
.cull_mode
& PIPE_WINDING_CW
) {
151 raster
.fill_cw
= raster
.fill_ccw
;
154 if (raster
.cull_mode
& PIPE_WINDING_CCW
) {
155 raster
.fill_ccw
= raster
.fill_cw
;
161 if (ctx
->Polygon
.OffsetUnits
!= 0.0 ||
162 ctx
->Polygon
.OffsetFactor
!= 0.0) {
163 raster
.offset_cw
= get_offset_flag( raster
.fill_cw
, &ctx
->Polygon
);
164 raster
.offset_ccw
= get_offset_flag( raster
.fill_ccw
, &ctx
->Polygon
);
165 raster
.offset_units
= ctx
->Polygon
.OffsetUnits
;
166 raster
.offset_scale
= ctx
->Polygon
.OffsetFactor
;
169 if (ctx
->Polygon
.SmoothFlag
)
170 raster
.poly_smooth
= 1;
172 if (ctx
->Polygon
.StippleFlag
)
173 raster
.poly_stipple_enable
= 1;
176 /* _NEW_BUFFERS, _NEW_POLYGON
178 if (raster
.fill_cw
!= PIPE_POLYGON_MODE_FILL
||
179 raster
.fill_ccw
!= PIPE_POLYGON_MODE_FILL
)
181 GLfloat mrd
= (ctx
->DrawBuffer
?
182 ctx
->DrawBuffer
->_MRD
:
185 raster
.offset_units
= ctx
->Polygon
.OffsetFactor
* mrd
;
186 raster
.offset_scale
= (ctx
->Polygon
.OffsetUnits
* mrd
*
187 st
->polygon_offset_scale
);
192 raster
.point_size
= ctx
->Point
.Size
;
193 raster
.point_smooth
= ctx
->Point
.SmoothFlag
;
194 raster
.point_sprite
= ctx
->Point
.PointSprite
;
195 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
196 if (ctx
->Point
.CoordReplace
[i
]) {
197 if (ctx
->Point
.SpriteOrigin
== GL_UPPER_LEFT
)
198 raster
.sprite_coord_mode
[i
] = PIPE_SPRITE_COORD_UPPER_LEFT
;
200 raster
.sprite_coord_mode
[i
] = PIPE_SPRITE_COORD_LOWER_LEFT
;
203 raster
.sprite_coord_mode
[i
] = PIPE_SPRITE_COORD_NONE
;
206 raster
.point_size_per_vertex
= ctx
->VertexProgram
.PointSizeEnabled
;
210 raster
.line_width
= ctx
->Line
.Width
;
211 raster
.line_smooth
= ctx
->Line
.SmoothFlag
;
212 raster
.line_stipple_enable
= ctx
->Line
.StippleFlag
;
213 raster
.line_stipple_pattern
= ctx
->Line
.StipplePattern
;
214 /* GL stipple factor is in [1,256], remap to [0, 255] here */
215 raster
.line_stipple_factor
= ctx
->Line
.StippleFactor
- 1;
217 /* _NEW_MULTISAMPLE */
218 if (ctx
->Multisample
.Enabled
)
219 raster
.multisample
= 1;
222 if (ctx
->Scissor
.Enabled
)
225 cso
= st_cached_rasterizer_state(st
, &raster
);
226 if (st
->state
.rasterizer
!= cso
) {
227 st
->state
.rasterizer
= cso
;
228 st
->pipe
->bind_rasterizer_state(st
->pipe
, cso
->data
);
232 const struct st_tracked_state st_update_rasterizer
= {
233 .name
= "st_update_rasterizer",
235 .mesa
= (_NEW_LIGHT
| _NEW_POLYGON
| _NEW_LINE
| _NEW_SCISSOR
|
236 _NEW_POINT
| _NEW_BUFFERS
| _NEW_MULTISAMPLE
),
239 .update
= update_raster_state