st/mesa: fix setting of point_size_per_vertex in ES contexts
[mesa.git] / src / mesa / state_tracker / st_atom_rasterizer.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
31 */
32
33 #include "main/macros.h"
34 #include "main/framebuffer.h"
35 #include "st_context.h"
36 #include "st_atom.h"
37 #include "st_debug.h"
38 #include "st_program.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "cso_cache/cso_context.h"
42
43
44 static GLuint translate_fill( GLenum mode )
45 {
46 switch (mode) {
47 case GL_POINT:
48 return PIPE_POLYGON_MODE_POINT;
49 case GL_LINE:
50 return PIPE_POLYGON_MODE_LINE;
51 case GL_FILL:
52 return PIPE_POLYGON_MODE_FILL;
53 default:
54 assert(0);
55 return 0;
56 }
57 }
58
59
60
61 static void update_raster_state( struct st_context *st )
62 {
63 struct gl_context *ctx = st->ctx;
64 struct pipe_rasterizer_state *raster = &st->state.rasterizer;
65 const struct gl_vertex_program *vertProg = ctx->VertexProgram._Current;
66 const struct gl_fragment_program *fragProg = ctx->FragmentProgram._Current;
67 uint i;
68
69 memset(raster, 0, sizeof(*raster));
70
71 /* _NEW_POLYGON, _NEW_BUFFERS
72 */
73 {
74 raster->front_ccw = (ctx->Polygon.FrontFace == GL_CCW);
75
76 /* _NEW_TRANSFORM */
77 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
78 raster->front_ccw ^= 1;
79 }
80
81 /*
82 * Gallium's surfaces are Y=0=TOP orientation. OpenGL is the
83 * opposite. Window system surfaces are Y=0=TOP. Mesa's FBOs
84 * must match OpenGL conventions so FBOs use Y=0=BOTTOM. In that
85 * case, we must invert Y and flip the notion of front vs. back.
86 */
87 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
88 /* Drawing to an FBO. The viewport will be inverted. */
89 raster->front_ccw ^= 1;
90 }
91 }
92
93 /* _NEW_LIGHT
94 */
95 raster->flatshade = ctx->Light.ShadeModel == GL_FLAT;
96
97 raster->flatshade_first = ctx->Light.ProvokingVertex ==
98 GL_FIRST_VERTEX_CONVENTION_EXT;
99
100 /* _NEW_LIGHT | _NEW_PROGRAM */
101 raster->light_twoside = ctx->VertexProgram._TwoSideEnabled;
102
103 /*_NEW_LIGHT | _NEW_BUFFERS */
104 raster->clamp_vertex_color = !st->clamp_vert_color_in_shader &&
105 ctx->Light._ClampVertexColor;
106
107 /* _NEW_POLYGON
108 */
109 if (ctx->Polygon.CullFlag) {
110 switch (ctx->Polygon.CullFaceMode) {
111 case GL_FRONT:
112 raster->cull_face = PIPE_FACE_FRONT;
113 break;
114 case GL_BACK:
115 raster->cull_face = PIPE_FACE_BACK;
116 break;
117 case GL_FRONT_AND_BACK:
118 raster->cull_face = PIPE_FACE_FRONT_AND_BACK;
119 break;
120 }
121 }
122 else {
123 raster->cull_face = PIPE_FACE_NONE;
124 }
125
126 /* _NEW_POLYGON
127 */
128 {
129 if (ST_DEBUG & DEBUG_WIREFRAME) {
130 raster->fill_front = PIPE_POLYGON_MODE_LINE;
131 raster->fill_back = PIPE_POLYGON_MODE_LINE;
132 }
133 else {
134 raster->fill_front = translate_fill( ctx->Polygon.FrontMode );
135 raster->fill_back = translate_fill( ctx->Polygon.BackMode );
136 }
137
138 /* Simplify when culling is active:
139 */
140 if (raster->cull_face & PIPE_FACE_FRONT) {
141 raster->fill_front = raster->fill_back;
142 }
143
144 if (raster->cull_face & PIPE_FACE_BACK) {
145 raster->fill_back = raster->fill_front;
146 }
147 }
148
149 /* _NEW_POLYGON
150 */
151 if (ctx->Polygon.OffsetPoint ||
152 ctx->Polygon.OffsetLine ||
153 ctx->Polygon.OffsetFill) {
154 raster->offset_point = ctx->Polygon.OffsetPoint;
155 raster->offset_line = ctx->Polygon.OffsetLine;
156 raster->offset_tri = ctx->Polygon.OffsetFill;
157 raster->offset_units = ctx->Polygon.OffsetUnits;
158 raster->offset_scale = ctx->Polygon.OffsetFactor;
159 raster->offset_clamp = ctx->Polygon.OffsetClamp;
160 }
161
162 raster->poly_smooth = ctx->Polygon.SmoothFlag;
163 raster->poly_stipple_enable = ctx->Polygon.StippleFlag;
164
165 /* _NEW_POINT
166 */
167 raster->point_size = ctx->Point.Size;
168 raster->point_smooth = !ctx->Point.PointSprite && ctx->Point.SmoothFlag;
169
170 /* _NEW_POINT | _NEW_PROGRAM
171 */
172 if (ctx->Point.PointSprite) {
173 /* origin */
174 if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
175 (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM))
176 raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
177 else
178 raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
179
180 /* Coord replacement flags. If bit 'k' is set that means
181 * that we need to replace GENERIC[k] attrib with an automatically
182 * computed texture coord.
183 */
184 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
185 if (ctx->Point.CoordReplace[i]) {
186 raster->sprite_coord_enable |= 1 << i;
187 }
188 }
189 if (!st->needs_texcoord_semantic &&
190 fragProg->Base.InputsRead & VARYING_BIT_PNTC) {
191 raster->sprite_coord_enable |=
192 1 << st_get_generic_varying_index(st, VARYING_SLOT_PNTC);
193 }
194
195 raster->point_quad_rasterization = 1;
196 }
197
198 /* ST_NEW_VERTEX_PROGRAM
199 */
200 if (vertProg) {
201 if (vertProg->Base.Id == 0) {
202 if (vertProg->Base.OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_PSIZ)) {
203 /* generated program which emits point size */
204 raster->point_size_per_vertex = TRUE;
205 }
206 }
207 else if (ctx->API != API_OPENGLES2) {
208 /* PointSizeEnabled is always set in ES2 contexts */
209 raster->point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
210 }
211 else {
212 /* ST_NEW_TESSEVAL_PROGRAM | ST_NEW_GEOMETRY_PROGRAM */
213 /* We have to check the last bound stage and see if it writes psize */
214 struct gl_program *last = NULL;
215 if (ctx->GeometryProgram._Current)
216 last = &ctx->GeometryProgram._Current->Base;
217 else if (ctx->TessEvalProgram._Current)
218 last = &ctx->TessEvalProgram._Current->Base;
219 else if (ctx->VertexProgram._Current)
220 last = &ctx->VertexProgram._Current->Base;
221 if (last)
222 raster->point_size_per_vertex =
223 !!(last->OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_PSIZ));
224 }
225 }
226 if (!raster->point_size_per_vertex) {
227 /* clamp size now */
228 raster->point_size = CLAMP(ctx->Point.Size,
229 ctx->Point.MinSize,
230 ctx->Point.MaxSize);
231 }
232
233 /* _NEW_LINE
234 */
235 raster->line_smooth = ctx->Line.SmoothFlag;
236 if (ctx->Line.SmoothFlag) {
237 raster->line_width = CLAMP(ctx->Line.Width,
238 ctx->Const.MinLineWidthAA,
239 ctx->Const.MaxLineWidthAA);
240 }
241 else {
242 raster->line_width = CLAMP(ctx->Line.Width,
243 ctx->Const.MinLineWidth,
244 ctx->Const.MaxLineWidth);
245 }
246
247 raster->line_stipple_enable = ctx->Line.StippleFlag;
248 raster->line_stipple_pattern = ctx->Line.StipplePattern;
249 /* GL stipple factor is in [1,256], remap to [0, 255] here */
250 raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
251
252 /* _NEW_MULTISAMPLE */
253 raster->multisample = _mesa_is_multisample_enabled(ctx);
254
255 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
256 raster->force_persample_interp =
257 !st->force_persample_in_shader &&
258 _mesa_is_multisample_enabled(ctx) &&
259 ctx->Multisample.SampleShading &&
260 ctx->Multisample.MinSampleShadingValue *
261 _mesa_geometric_samples(ctx->DrawBuffer) > 1;
262
263 /* _NEW_SCISSOR */
264 raster->scissor = ctx->Scissor.EnableFlags;
265
266 /* _NEW_FRAG_CLAMP */
267 raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
268 ctx->Color._ClampFragmentColor;
269
270 raster->half_pixel_center = 1;
271 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
272 raster->bottom_edge_rule = 1;
273 /* _NEW_TRANSFORM */
274 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT)
275 raster->bottom_edge_rule ^= 1;
276
277 /* ST_NEW_RASTERIZER */
278 raster->rasterizer_discard = ctx->RasterDiscard;
279
280 if (st->edgeflag_culls_prims) {
281 /* All edge flags are FALSE. Cull the affected faces. */
282 if (raster->fill_front != PIPE_POLYGON_MODE_FILL)
283 raster->cull_face |= PIPE_FACE_FRONT;
284 if (raster->fill_back != PIPE_POLYGON_MODE_FILL)
285 raster->cull_face |= PIPE_FACE_BACK;
286 }
287
288 /* _NEW_TRANSFORM */
289 raster->depth_clip = !ctx->Transform.DepthClamp;
290 raster->clip_plane_enable = ctx->Transform.ClipPlanesEnabled;
291 raster->clip_halfz = (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE);
292
293 cso_set_rasterizer(st->cso_context, raster);
294 }
295
296 const struct st_tracked_state st_update_rasterizer = {
297 "st_update_rasterizer", /* name */
298 {
299 (_NEW_BUFFERS |
300 _NEW_LIGHT |
301 _NEW_LINE |
302 _NEW_MULTISAMPLE |
303 _NEW_POINT |
304 _NEW_POLYGON |
305 _NEW_PROGRAM |
306 _NEW_SCISSOR |
307 _NEW_FRAG_CLAMP |
308 _NEW_TRANSFORM), /* mesa state dependencies*/
309 (ST_NEW_VERTEX_PROGRAM |
310 ST_NEW_TESSEVAL_PROGRAM |
311 ST_NEW_GEOMETRY_PROGRAM |
312 ST_NEW_RASTERIZER), /* state tracker dependencies */
313 },
314 update_raster_state /* update function */
315 };