1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
32 #include "svga_context.h"
34 #include "svga_hw_reg.h"
36 /* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW.
38 static SVGA3dFace
svga_translate_cullmode( unsigned mode
,
39 unsigned front_winding
)
42 case PIPE_WINDING_NONE
:
43 return SVGA3D_FACE_NONE
;
44 case PIPE_WINDING_CCW
:
45 return SVGA3D_FACE_BACK
;
47 return SVGA3D_FACE_FRONT
;
48 case PIPE_WINDING_BOTH
:
49 return SVGA3D_FACE_FRONT_BACK
;
52 return SVGA3D_FACE_NONE
;
56 static SVGA3dShadeMode
svga_translate_flatshade( unsigned mode
)
58 return mode
? SVGA3D_SHADEMODE_FLAT
: SVGA3D_SHADEMODE_SMOOTH
;
63 svga_create_rasterizer_state(struct pipe_context
*pipe
,
64 const struct pipe_rasterizer_state
*templ
)
66 struct svga_rasterizer_state
*rast
= CALLOC_STRUCT( svga_rasterizer_state
);
67 /* need this for draw module. */
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 */
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
;
93 /* Use swtnl + decomposition implement these:
95 if (templ
->poly_stipple_enable
)
96 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
98 if (templ
->line_width
!= 1.0 &&
99 templ
->line_width
!= 0.0)
100 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
102 if (templ
->line_stipple_enable
) {
103 /* LinePattern not implemented on all backends.
106 SVGA3dLinePattern lp
;
107 lp
.repeat
= templ
->line_stipple_factor
+ 1;
108 lp
.pattern
= templ
->line_stipple_pattern
;
109 rast
->linepattern
= lp
.uintValue
;
112 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
116 if (templ
->point_smooth
)
117 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_POINTS
;
120 boolean offset_cw
= templ
->offset_cw
;
121 boolean offset_ccw
= templ
->offset_ccw
;
123 int fill_cw
= templ
->fill_cw
;
124 int fill_ccw
= templ
->fill_ccw
;
125 int fill
= PIPE_POLYGON_MODE_FILL
;
127 switch (templ
->cull_mode
) {
128 case PIPE_WINDING_BOTH
:
130 fill
= PIPE_POLYGON_MODE_FILL
;
133 case PIPE_WINDING_CW
:
138 case PIPE_WINDING_CCW
:
143 case PIPE_WINDING_NONE
:
144 if (fill_cw
!= fill_ccw
|| offset_cw
!= offset_ccw
)
146 /* Always need the draw module to work out different
147 * front/back fill modes:
149 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
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:
166 if (fill
!= PIPE_POLYGON_MODE_FILL
&&
168 templ
->light_twoside
||
170 templ
->cull_mode
!= PIPE_WINDING_NONE
))
172 fill
= PIPE_POLYGON_MODE_FILL
;
173 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
176 /* If we are decomposing to lines, and lines need the pipeline,
177 * then we also need the pipeline for tris.
179 if (fill
== PIPE_POLYGON_MODE_LINE
&&
180 (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_LINES
))
182 fill
= PIPE_POLYGON_MODE_FILL
;
183 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
186 /* Similarly for points:
188 if (fill
== PIPE_POLYGON_MODE_POINT
&&
189 (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_POINTS
))
191 fill
= PIPE_POLYGON_MODE_FILL
;
192 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
196 rast
->slopescaledepthbias
= templ
->offset_scale
;
197 rast
->depthbias
= templ
->offset_units
;
200 rast
->hw_unfilled
= fill
;
206 if (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_TRIS
) {
207 /* Turn off stuff which will get done in the draw module:
209 rast
->hw_unfilled
= PIPE_POLYGON_MODE_FILL
;
210 rast
->slopescaledepthbias
= 0;
217 static void svga_bind_rasterizer_state( struct pipe_context
*pipe
,
220 struct svga_context
*svga
= svga_context(pipe
);
221 struct svga_rasterizer_state
*raster
= (struct svga_rasterizer_state
*)state
;
223 svga
->curr
.rast
= raster
;
225 draw_set_rasterizer_state(svga
->swtnl
.draw
, raster
? &raster
->templ
: NULL
,
228 svga
->dirty
|= SVGA_NEW_RAST
;
231 static void svga_delete_rasterizer_state(struct pipe_context
*pipe
,
238 void svga_init_rasterizer_functions( struct svga_context
*svga
)
240 svga
->pipe
.create_rasterizer_state
= svga_create_rasterizer_state
;
241 svga
->pipe
.bind_rasterizer_state
= svga_bind_rasterizer_state
;
242 svga
->pipe
.delete_rasterizer_state
= svga_delete_rasterizer_state
;
246 /***********************************************************************
247 * Hardware state update