beae36bca00441adef7e38c78687f4ff23566201
[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 uint i;
78
79 memset(&raster, 0, sizeof(raster));
80
81 raster.origin_lower_left = 1; /* Always true for OpenGL */
82
83 /* _NEW_POLYGON, _NEW_BUFFERS
84 */
85 {
86 if (ctx->Polygon.FrontFace == GL_CCW)
87 raster.front_winding = PIPE_WINDING_CCW;
88 else
89 raster.front_winding = PIPE_WINDING_CW;
90
91 /* XXX
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...
96 */
97 if (ctx->DrawBuffer && ctx->DrawBuffer->Name != 0)
98 raster.front_winding ^= PIPE_WINDING_BOTH;
99 }
100
101 /* _NEW_LIGHT
102 */
103 if (ctx->Light.ShadeModel == GL_FLAT)
104 raster.flatshade = 1;
105
106 /* _NEW_LIGHT | _NEW_PROGRAM
107 *
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.
111 */
112 if (ctx->VertexProgram._Current) {
113 if (ctx->VertexProgram._Enabled) {
114 /* user-defined program */
115 raster.light_twoside = ctx->VertexProgram.TwoSideEnabled;
116 }
117 else {
118 /* TNL-generated program */
119 raster.light_twoside = ctx->Light.Enabled && ctx->Light.Model.TwoSide;
120 }
121 }
122 else if (ctx->Light.Enabled && ctx->Light.Model.TwoSide) {
123 raster.light_twoside = 1;
124 }
125
126 /* _NEW_POLYGON
127 */
128 if (ctx->Polygon.CullFlag) {
129 if (ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
130 raster.cull_mode = PIPE_WINDING_BOTH;
131 }
132 else if (ctx->Polygon.CullFaceMode == GL_FRONT) {
133 raster.cull_mode = raster.front_winding;
134 }
135 else {
136 raster.cull_mode = raster.front_winding ^ PIPE_WINDING_BOTH;
137 }
138 }
139
140 /* _NEW_POLYGON
141 */
142 {
143 GLuint fill_front = translate_fill( ctx->Polygon.FrontMode );
144 GLuint fill_back = translate_fill( ctx->Polygon.BackMode );
145
146 if (raster.front_winding == PIPE_WINDING_CW) {
147 raster.fill_cw = fill_front;
148 raster.fill_ccw = fill_back;
149 }
150 else {
151 raster.fill_cw = fill_back;
152 raster.fill_ccw = fill_front;
153 }
154
155 /* Simplify when culling is active:
156 */
157 if (raster.cull_mode & PIPE_WINDING_CW) {
158 raster.fill_cw = raster.fill_ccw;
159 }
160
161 if (raster.cull_mode & PIPE_WINDING_CCW) {
162 raster.fill_ccw = raster.fill_cw;
163 }
164 }
165
166 /* _NEW_POLYGON
167 */
168 if (ctx->Polygon.OffsetUnits != 0.0 ||
169 ctx->Polygon.OffsetFactor != 0.0) {
170 raster.offset_cw = get_offset_flag( raster.fill_cw, &ctx->Polygon );
171 raster.offset_ccw = get_offset_flag( raster.fill_ccw, &ctx->Polygon );
172 raster.offset_units = ctx->Polygon.OffsetUnits;
173 raster.offset_scale = ctx->Polygon.OffsetFactor;
174 }
175
176 if (ctx->Polygon.SmoothFlag)
177 raster.poly_smooth = 1;
178
179 if (ctx->Polygon.StippleFlag)
180 raster.poly_stipple_enable = 1;
181
182
183 /* _NEW_BUFFERS, _NEW_POLYGON
184 */
185 if (raster.fill_cw != PIPE_POLYGON_MODE_FILL ||
186 raster.fill_ccw != PIPE_POLYGON_MODE_FILL)
187 {
188 GLfloat mrd = (ctx->DrawBuffer ?
189 ctx->DrawBuffer->_MRD :
190 1.0);
191
192 raster.offset_units = ctx->Polygon.OffsetFactor * mrd;
193 raster.offset_scale = (ctx->Polygon.OffsetUnits * mrd *
194 st->polygon_offset_scale);
195 }
196
197 /* _NEW_POINT
198 */
199 raster.point_size = ctx->Point.Size;
200 raster.point_smooth = ctx->Point.SmoothFlag;
201 raster.point_sprite = ctx->Point.PointSprite;
202 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
203 if (ctx->Point.CoordReplace[i]) {
204 if (ctx->Point.SpriteOrigin == GL_UPPER_LEFT)
205 raster.sprite_coord_mode[i] = PIPE_SPRITE_COORD_UPPER_LEFT;
206 else
207 raster.sprite_coord_mode[i] = PIPE_SPRITE_COORD_LOWER_LEFT;
208 }
209 else {
210 raster.sprite_coord_mode[i] = PIPE_SPRITE_COORD_NONE;
211 }
212 }
213 raster.point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
214
215 /* _NEW_LINE
216 */
217 raster.line_width = ctx->Line.Width;
218 raster.line_smooth = ctx->Line.SmoothFlag;
219 raster.line_stipple_enable = ctx->Line.StippleFlag;
220 raster.line_stipple_pattern = ctx->Line.StipplePattern;
221 /* GL stipple factor is in [1,256], remap to [0, 255] here */
222 raster.line_stipple_factor = ctx->Line.StippleFactor - 1;
223
224 /* _NEW_MULTISAMPLE */
225 if (ctx->Multisample.Enabled)
226 raster.multisample = 1;
227
228 /* _NEW_SCISSOR */
229 if (ctx->Scissor.Enabled)
230 raster.scissor = 1;
231
232 cso = st_cached_rasterizer_state(st, &raster);
233 if (st->state.rasterizer != cso) {
234 st->state.rasterizer = cso;
235 st->pipe->bind_rasterizer_state(st->pipe, cso->data);
236 }
237 }
238
239 const struct st_tracked_state st_update_rasterizer = {
240 .name = "st_update_rasterizer",
241 .dirty = {
242 .mesa = (_NEW_LIGHT | _NEW_POLYGON | _NEW_LINE | _NEW_SCISSOR |
243 _NEW_POINT | _NEW_BUFFERS | _NEW_MULTISAMPLE),
244 .st = 0,
245 },
246 .update = update_raster_state
247 };