Merge remote branch 'origin/master' into gallium_draw_llvm
[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 #include "main/macros.h"
34 #include "st_context.h"
35 #include "st_atom.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "cso_cache/cso_context.h"
39
40
41 static GLuint translate_fill( GLenum mode )
42 {
43 switch (mode) {
44 case GL_POINT:
45 return PIPE_POLYGON_MODE_POINT;
46 case GL_LINE:
47 return PIPE_POLYGON_MODE_LINE;
48 case GL_FILL:
49 return PIPE_POLYGON_MODE_FILL;
50 default:
51 assert(0);
52 return 0;
53 }
54 }
55
56 static GLboolean get_offset_flag( GLuint fill_mode,
57 const struct gl_polygon_attrib *p )
58 {
59 switch (fill_mode) {
60 case PIPE_POLYGON_MODE_POINT:
61 return p->OffsetPoint;
62 case PIPE_POLYGON_MODE_LINE:
63 return p->OffsetLine;
64 case PIPE_POLYGON_MODE_FILL:
65 return p->OffsetFill;
66 default:
67 assert(0);
68 return 0;
69 }
70 }
71
72
73 static void update_raster_state( struct st_context *st )
74 {
75 GLcontext *ctx = st->ctx;
76 struct pipe_rasterizer_state *raster = &st->state.rasterizer;
77 const struct gl_vertex_program *vertProg = ctx->VertexProgram._Current;
78 uint i;
79
80 memset(raster, 0, sizeof(*raster));
81
82 /* _NEW_POLYGON, _NEW_BUFFERS
83 */
84 {
85 if (ctx->Polygon.FrontFace == GL_CCW)
86 raster->front_winding = PIPE_WINDING_CCW;
87 else
88 raster->front_winding = PIPE_WINDING_CW;
89
90 /* XXX
91 * I think the intention here is that user-created framebuffer objects
92 * use Y=0=TOP layout instead of OpenGL's normal Y=0=bottom layout.
93 * Flipping Y changes CW to CCW and vice-versa.
94 * But this is an implementation/driver-specific artifact - remove...
95 */
96 if (ctx->DrawBuffer && ctx->DrawBuffer->Name != 0)
97 raster->front_winding ^= PIPE_WINDING_BOTH;
98 }
99
100 /* _NEW_LIGHT
101 */
102 if (ctx->Light.ShadeModel == GL_FLAT)
103 raster->flatshade = 1;
104
105 if (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION_EXT)
106 raster->flatshade_first = 1;
107
108 /* _NEW_LIGHT | _NEW_PROGRAM
109 *
110 * Back-face colors can come from traditional lighting (when
111 * GL_LIGHT_MODEL_TWO_SIDE is set) or from vertex programs/shaders (when
112 * GL_VERTEX_PROGRAM_TWO_SIDE is set). Note the logic here.
113 */
114 if (ctx->VertexProgram._Current) {
115 if (ctx->VertexProgram._Enabled ||
116 (ctx->Shader.CurrentProgram &&
117 ctx->Shader.CurrentProgram->VertexProgram &&
118 ctx->Shader.CurrentProgram->LinkStatus)) {
119 /* user-defined vertex program or shader */
120 raster->light_twoside = ctx->VertexProgram.TwoSideEnabled;
121 }
122 else {
123 /* TNL-generated program */
124 raster->light_twoside = ctx->Light.Enabled && ctx->Light.Model.TwoSide;
125 }
126 }
127 else if (ctx->Light.Enabled && ctx->Light.Model.TwoSide) {
128 raster->light_twoside = 1;
129 }
130
131 /* _NEW_POLYGON
132 */
133 if (ctx->Polygon.CullFlag) {
134 if (ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
135 raster->cull_mode = PIPE_WINDING_BOTH;
136 }
137 else if (ctx->Polygon.CullFaceMode == GL_FRONT) {
138 raster->cull_mode = raster->front_winding;
139 }
140 else {
141 raster->cull_mode = raster->front_winding ^ PIPE_WINDING_BOTH;
142 }
143 }
144
145 /* _NEW_POLYGON
146 */
147 {
148 GLuint fill_front = translate_fill( ctx->Polygon.FrontMode );
149 GLuint fill_back = translate_fill( ctx->Polygon.BackMode );
150
151 if (raster->front_winding == PIPE_WINDING_CW) {
152 raster->fill_cw = fill_front;
153 raster->fill_ccw = fill_back;
154 }
155 else {
156 raster->fill_cw = fill_back;
157 raster->fill_ccw = fill_front;
158 }
159
160 /* Simplify when culling is active:
161 */
162 if (raster->cull_mode & PIPE_WINDING_CW) {
163 raster->fill_cw = raster->fill_ccw;
164 }
165
166 if (raster->cull_mode & PIPE_WINDING_CCW) {
167 raster->fill_ccw = raster->fill_cw;
168 }
169 }
170
171 /* _NEW_POLYGON
172 */
173 if (ctx->Polygon.OffsetUnits != 0.0 ||
174 ctx->Polygon.OffsetFactor != 0.0) {
175 raster->offset_cw = get_offset_flag( raster->fill_cw, &ctx->Polygon );
176 raster->offset_ccw = get_offset_flag( raster->fill_ccw, &ctx->Polygon );
177 raster->offset_units = ctx->Polygon.OffsetUnits;
178 raster->offset_scale = ctx->Polygon.OffsetFactor;
179 }
180
181 if (ctx->Polygon.SmoothFlag)
182 raster->poly_smooth = 1;
183
184 if (ctx->Polygon.StippleFlag)
185 raster->poly_stipple_enable = 1;
186
187 /* _NEW_POINT
188 */
189 raster->point_size = ctx->Point.Size;
190
191 if (!ctx->Point.PointSprite && ctx->Point.SmoothFlag)
192 raster->point_smooth = 1;
193
194 if (ctx->Point.PointSprite) {
195 if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
196 (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM))
197 raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
198 else
199 raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
200 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
201 if (ctx->Point.CoordReplace[i]) {
202 raster->sprite_coord_enable |= 1 << i;
203 }
204 }
205 raster->point_quad_rasterization = 1;
206 }
207
208 /* ST_NEW_VERTEX_PROGRAM
209 */
210 if (vertProg) {
211 if (vertProg->Base.Id == 0) {
212 if (vertProg->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_PSIZ)) {
213 /* generated program which emits point size */
214 raster->point_size_per_vertex = TRUE;
215 }
216 }
217 else if (ctx->VertexProgram.PointSizeEnabled) {
218 /* user-defined program and GL_VERTEX_PROGRAM_POINT_SIZE set */
219 raster->point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
220 }
221 }
222 if (!raster->point_size_per_vertex) {
223 /* clamp size now */
224 raster->point_size = CLAMP(ctx->Point.Size,
225 ctx->Point.MinSize,
226 ctx->Point.MaxSize);
227 }
228
229 /* _NEW_LINE
230 */
231 raster->line_smooth = ctx->Line.SmoothFlag;
232 if (ctx->Line.SmoothFlag) {
233 raster->line_width = CLAMP(ctx->Line.Width,
234 ctx->Const.MinLineWidthAA,
235 ctx->Const.MaxLineWidthAA);
236 }
237 else {
238 raster->line_width = CLAMP(ctx->Line.Width,
239 ctx->Const.MinLineWidth,
240 ctx->Const.MaxLineWidth);
241 }
242
243 raster->line_stipple_enable = ctx->Line.StippleFlag;
244 raster->line_stipple_pattern = ctx->Line.StipplePattern;
245 /* GL stipple factor is in [1,256], remap to [0, 255] here */
246 raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
247
248 /* _NEW_MULTISAMPLE */
249 if (ctx->Multisample._Enabled || st->force_msaa)
250 raster->multisample = 1;
251
252 /* _NEW_SCISSOR */
253 if (ctx->Scissor.Enabled)
254 raster->scissor = 1;
255
256 raster->gl_rasterization_rules = 1;
257
258 cso_set_rasterizer(st->cso_context, raster);
259 }
260
261 const struct st_tracked_state st_update_rasterizer = {
262 "st_update_rasterizer", /* name */
263 {
264 (_NEW_BUFFERS |
265 _NEW_LIGHT |
266 _NEW_LINE |
267 _NEW_MULTISAMPLE |
268 _NEW_POINT |
269 _NEW_POLYGON |
270 _NEW_PROGRAM |
271 _NEW_SCISSOR), /* mesa state dependencies*/
272 ST_NEW_VERTEX_PROGRAM, /* state tracker dependencies */
273 },
274 update_raster_state /* update function */
275 };