35717788677ede71482e95f5a1d5eacb03cb238d
[mesa.git] / src / gallium / drivers / svga / svga_pipe_rasterizer.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "draw/draw_context.h"
27 #include "util/u_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31
32 #include "svga_context.h"
33
34 #include "svga_hw_reg.h"
35
36 /* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW.
37 */
38 static SVGA3dFace svga_translate_cullmode( unsigned mode,
39 unsigned front_winding )
40 {
41 switch (mode) {
42 case PIPE_WINDING_NONE:
43 return SVGA3D_FACE_NONE;
44 case PIPE_WINDING_CCW:
45 return SVGA3D_FACE_BACK;
46 case PIPE_WINDING_CW:
47 return SVGA3D_FACE_FRONT;
48 case PIPE_WINDING_BOTH:
49 return SVGA3D_FACE_FRONT_BACK;
50 default:
51 assert(0);
52 return SVGA3D_FACE_NONE;
53 }
54 }
55
56 static SVGA3dShadeMode svga_translate_flatshade( unsigned mode )
57 {
58 return mode ? SVGA3D_SHADEMODE_FLAT : SVGA3D_SHADEMODE_SMOOTH;
59 }
60
61
62 static void *
63 svga_create_rasterizer_state(struct pipe_context *pipe,
64 const struct pipe_rasterizer_state *templ)
65 {
66 struct svga_rasterizer_state *rast = CALLOC_STRUCT( svga_rasterizer_state );
67 /* need this for draw module. */
68 rast->templ = *templ;
69
70 /* light_twoside - XXX: need fragment shader varient */
71 /* poly_smooth - XXX: no fallback available */
72 /* poly_stipple_enable - draw module */
73 /* sprite_coord_enable - ? */
74 /* point_quad_rasterization - ? */
75 /* point_size_per_vertex - ? */
76 /* sprite_coord_mode - ??? */
77 /* bypass_vs_viewport_and_clip - handled by viewport setup */
78 /* flatshade_first - handled by index translation */
79 /* gl_rasterization_rules - XXX - viewport code */
80 /* line_width - draw module */
81 /* fill_cw, fill_ccw - draw module or index translation */
82
83 rast->shademode = svga_translate_flatshade( templ->flatshade );
84 rast->cullmode = svga_translate_cullmode( templ->cull_mode,
85 templ->front_winding );
86 rast->scissortestenable = templ->scissor;
87 rast->multisampleantialias = templ->multisample;
88 rast->antialiasedlineenable = templ->line_smooth;
89 rast->lastpixel = templ->line_last_pixel;
90 rast->pointsize = templ->point_size;
91 rast->hw_unfilled = PIPE_POLYGON_MODE_FILL;
92
93 /* Use swtnl + decomposition implement these:
94 */
95 if (templ->poly_stipple_enable)
96 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
97
98 if (templ->line_width != 1.0 &&
99 templ->line_width != 0.0)
100 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
101
102 if (templ->line_stipple_enable) {
103 /* LinePattern not implemented on all backends.
104 */
105 if (0) {
106 SVGA3dLinePattern lp;
107 lp.repeat = templ->line_stipple_factor + 1;
108 lp.pattern = templ->line_stipple_pattern;
109 rast->linepattern = lp.uintValue;
110 }
111 else {
112 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
113 }
114 }
115
116 if (templ->point_smooth)
117 rast->need_pipeline |= SVGA_PIPELINE_FLAG_POINTS;
118
119 {
120 boolean offset_cw = templ->offset_cw;
121 boolean offset_ccw = templ->offset_ccw;
122 boolean offset = 0;
123 int fill_cw = templ->fill_cw;
124 int fill_ccw = templ->fill_ccw;
125 int fill = PIPE_POLYGON_MODE_FILL;
126
127 switch (templ->cull_mode) {
128 case PIPE_WINDING_BOTH:
129 offset = 0;
130 fill = PIPE_POLYGON_MODE_FILL;
131 break;
132
133 case PIPE_WINDING_CW:
134 offset = offset_ccw;
135 fill = fill_ccw;
136 break;
137
138 case PIPE_WINDING_CCW:
139 offset = offset_cw;
140 fill = fill_cw;
141 break;
142
143 case PIPE_WINDING_NONE:
144 if (fill_cw != fill_ccw || offset_cw != offset_ccw)
145 {
146 /* Always need the draw module to work out different
147 * front/back fill modes:
148 */
149 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
150 }
151 else {
152 offset = offset_ccw;
153 fill = fill_ccw;
154 }
155 break;
156
157 default:
158 assert(0);
159 break;
160 }
161
162 /* Unfilled primitive modes aren't implemented on all virtual
163 * hardware. We can do some unfilled processing with index
164 * translation, but otherwise need the draw module:
165 */
166 if (fill != PIPE_POLYGON_MODE_FILL &&
167 (templ->flatshade ||
168 templ->light_twoside ||
169 offset ||
170 templ->cull_mode != PIPE_WINDING_NONE))
171 {
172 fill = PIPE_POLYGON_MODE_FILL;
173 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
174 }
175
176 /* If we are decomposing to lines, and lines need the pipeline,
177 * then we also need the pipeline for tris.
178 */
179 if (fill == PIPE_POLYGON_MODE_LINE &&
180 (rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES))
181 {
182 fill = PIPE_POLYGON_MODE_FILL;
183 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
184 }
185
186 /* Similarly for points:
187 */
188 if (fill == PIPE_POLYGON_MODE_POINT &&
189 (rast->need_pipeline & SVGA_PIPELINE_FLAG_POINTS))
190 {
191 fill = PIPE_POLYGON_MODE_FILL;
192 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
193 }
194
195 if (offset) {
196 rast->slopescaledepthbias = templ->offset_scale;
197 rast->depthbias = templ->offset_units;
198 }
199
200 rast->hw_unfilled = fill;
201 }
202
203
204
205
206 if (rast->need_pipeline & SVGA_PIPELINE_FLAG_TRIS) {
207 /* Turn off stuff which will get done in the draw module:
208 */
209 rast->hw_unfilled = PIPE_POLYGON_MODE_FILL;
210 rast->slopescaledepthbias = 0;
211 rast->depthbias = 0;
212 }
213
214 return rast;
215 }
216
217 static void svga_bind_rasterizer_state( struct pipe_context *pipe,
218 void *state )
219 {
220 struct svga_context *svga = svga_context(pipe);
221 struct svga_rasterizer_state *raster = (struct svga_rasterizer_state *)state;
222
223 svga->curr.rast = raster;
224
225 draw_set_rasterizer_state(svga->swtnl.draw, raster ? &raster->templ : NULL);
226
227 svga->dirty |= SVGA_NEW_RAST;
228 }
229
230 static void svga_delete_rasterizer_state(struct pipe_context *pipe,
231 void *raster)
232 {
233 FREE(raster);
234 }
235
236
237 void svga_init_rasterizer_functions( struct svga_context *svga )
238 {
239 svga->pipe.create_rasterizer_state = svga_create_rasterizer_state;
240 svga->pipe.bind_rasterizer_state = svga_bind_rasterizer_state;
241 svga->pipe.delete_rasterizer_state = svga_delete_rasterizer_state;
242 }
243
244
245 /***********************************************************************
246 * Hardware state update
247 */
248