9fbd7dc09ef625d0ae4abeba02f40cea4ad5013c
[mesa.git] / src / mesa / state_tracker / st_atom_rasterizer.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33
34 #include "st_context.h"
35 #include "st_cache.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "st_atom.h"
39
40 static GLuint translate_fill( GLenum mode )
41 {
42 switch (mode) {
43 case GL_POINT:
44 return PIPE_POLYGON_MODE_POINT;
45 case GL_LINE:
46 return PIPE_POLYGON_MODE_LINE;
47 case GL_FILL:
48 return PIPE_POLYGON_MODE_FILL;
49 default:
50 assert(0);
51 return 0;
52 }
53 }
54
55 static GLboolean get_offset_flag( GLuint fill_mode,
56 const struct gl_polygon_attrib *p )
57 {
58 switch (fill_mode) {
59 case PIPE_POLYGON_MODE_POINT:
60 return p->OffsetPoint;
61 case PIPE_POLYGON_MODE_LINE:
62 return p->OffsetLine;
63 case PIPE_POLYGON_MODE_FILL:
64 return p->OffsetFill;
65 default:
66 assert(0);
67 return 0;
68 }
69 }
70
71
72 static void update_raster_state( struct st_context *st )
73 {
74 GLcontext *ctx = st->ctx;
75 struct pipe_rasterizer_state raster;
76 const struct cso_rasterizer *cso;
77
78 memset(&raster, 0, sizeof(raster));
79
80 /* _NEW_POLYGON, _NEW_BUFFERS
81 */
82 {
83 if (ctx->Polygon.FrontFace == GL_CCW)
84 raster.front_winding = PIPE_WINDING_CCW;
85 else
86 raster.front_winding = PIPE_WINDING_CW;
87
88 /* XXX
89 * I think the intention here is that user-created framebuffer objects
90 * use Y=0=TOP layout instead of OpenGL's normal Y=0=bottom layout.
91 * Flipping Y changes CW to CCW and vice-versa.
92 * But this is an implementation/driver-specific artifact - remove...
93 */
94 if (ctx->DrawBuffer && ctx->DrawBuffer->Name != 0)
95 raster.front_winding ^= PIPE_WINDING_BOTH;
96 }
97
98 /* _NEW_LIGHT
99 */
100 if (ctx->Light.ShadeModel == GL_FLAT)
101 raster.flatshade = 1;
102
103 /* _NEW_LIGHT | _NEW_PROGRAM
104 *
105 * Back-face colors can come from traditional lighting (when
106 * GL_LIGHT_MODEL_TWO_SIDE is set) or from vertex programs (when
107 * GL_VERTEX_PROGRAM_TWO_SIDE is set). Note the logic here.
108 */
109 if (ctx->VertexProgram._Enabled) {
110 raster.light_twoside = ctx->VertexProgram.TwoSideEnabled;
111 }
112 else if (ctx->Light.Enabled && ctx->Light.Model.TwoSide) {
113 raster.light_twoside = 1;
114 }
115
116 /* _NEW_POLYGON
117 */
118 if (ctx->Polygon.CullFlag) {
119 if (ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
120 raster.cull_mode = PIPE_WINDING_BOTH;
121 }
122 else if (ctx->Polygon.CullFaceMode == GL_FRONT) {
123 raster.cull_mode = raster.front_winding;
124 }
125 else {
126 raster.cull_mode = raster.front_winding ^ PIPE_WINDING_BOTH;
127 }
128 }
129
130 /* _NEW_POLYGON
131 */
132 {
133 GLuint fill_front = translate_fill( ctx->Polygon.FrontMode );
134 GLuint fill_back = translate_fill( ctx->Polygon.BackMode );
135
136 if (raster.front_winding == PIPE_WINDING_CW) {
137 raster.fill_cw = fill_front;
138 raster.fill_ccw = fill_back;
139 }
140 else {
141 raster.fill_cw = fill_back;
142 raster.fill_ccw = fill_front;
143 }
144
145 /* Simplify when culling is active:
146 */
147 if (raster.cull_mode & PIPE_WINDING_CW) {
148 raster.fill_cw = raster.fill_ccw;
149 }
150
151 if (raster.cull_mode & PIPE_WINDING_CCW) {
152 raster.fill_ccw = raster.fill_cw;
153 }
154 }
155
156 /* _NEW_POLYGON
157 */
158 if (ctx->Polygon.OffsetUnits != 0.0 ||
159 ctx->Polygon.OffsetFactor != 0.0) {
160 raster.offset_cw = get_offset_flag( raster.fill_cw, &ctx->Polygon );
161 raster.offset_ccw = get_offset_flag( raster.fill_ccw, &ctx->Polygon );
162 raster.offset_units = ctx->Polygon.OffsetUnits;
163 raster.offset_scale = ctx->Polygon.OffsetFactor;
164 }
165
166 if (ctx->Polygon.SmoothFlag)
167 raster.poly_smooth = 1;
168
169 if (ctx->Polygon.StippleFlag)
170 raster.poly_stipple_enable = 1;
171
172
173 /* _NEW_BUFFERS, _NEW_POLYGON
174 */
175 if (raster.fill_cw != PIPE_POLYGON_MODE_FILL ||
176 raster.fill_ccw != PIPE_POLYGON_MODE_FILL)
177 {
178 GLfloat mrd = (ctx->DrawBuffer ?
179 ctx->DrawBuffer->_MRD :
180 1.0);
181
182 raster.offset_units = ctx->Polygon.OffsetFactor * mrd;
183 raster.offset_scale = (ctx->Polygon.OffsetUnits * mrd *
184 st->polygon_offset_scale);
185 }
186
187 /* _NEW_POINT
188 */
189 raster.point_size = ctx->Point.Size;
190 raster.point_smooth = ctx->Point.SmoothFlag;
191 raster.point_sprite = ctx->Point.PointSprite;
192
193 /* _NEW_LINE
194 */
195 raster.line_width = ctx->Line.Width;
196 raster.line_smooth = ctx->Line.SmoothFlag;
197 raster.line_stipple_enable = ctx->Line.StippleFlag;
198 raster.line_stipple_pattern = ctx->Line.StipplePattern;
199 /* GL stipple factor is in [1,256], remap to [0, 255] here */
200 raster.line_stipple_factor = ctx->Line.StippleFactor - 1;
201
202 /* _NEW_MULTISAMPLE */
203 if (ctx->Multisample.Enabled)
204 raster.multisample = 1;
205
206 /* _NEW_SCISSOR */
207 if (ctx->Scissor.Enabled)
208 raster.scissor = 1;
209
210 cso = st_cached_rasterizer_state(st, &raster);
211 if (st->state.rasterizer != cso) {
212 st->state.rasterizer = cso;
213 st->pipe->bind_rasterizer_state(st->pipe, cso->data);
214 }
215 }
216
217 const struct st_tracked_state st_update_rasterizer = {
218 .name = "st_update_rasterizer",
219 .dirty = {
220 .mesa = (_NEW_LIGHT | _NEW_POLYGON | _NEW_LINE | _NEW_SCISSOR |
221 _NEW_POINT | _NEW_BUFFERS | _NEW_MULTISAMPLE),
222 .st = 0,
223 },
224 .update = update_raster_state
225 };