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
,
41 const int hw_front_ccw
= 0; /* hardware is always CW */
44 return SVGA3D_FACE_NONE
;
46 return front_ccw
== hw_front_ccw
? SVGA3D_FACE_FRONT
: SVGA3D_FACE_BACK
;
48 return front_ccw
== hw_front_ccw
? SVGA3D_FACE_BACK
: SVGA3D_FACE_FRONT
;
49 case PIPE_FACE_FRONT_AND_BACK
:
50 return SVGA3D_FACE_FRONT_BACK
;
53 return SVGA3D_FACE_NONE
;
57 static SVGA3dShadeMode
svga_translate_flatshade( unsigned mode
)
59 return mode
? SVGA3D_SHADEMODE_FLAT
: SVGA3D_SHADEMODE_SMOOTH
;
64 svga_create_rasterizer_state(struct pipe_context
*pipe
,
65 const struct pipe_rasterizer_state
*templ
)
67 struct svga_rasterizer_state
*rast
= CALLOC_STRUCT( svga_rasterizer_state
);
68 /* need this for draw module. */
71 /* light_twoside - XXX: need fragment shader varient */
72 /* poly_smooth - XXX: no fallback available */
73 /* poly_stipple_enable - draw module */
74 /* sprite_coord_enable - ? */
75 /* point_quad_rasterization - ? */
76 /* point_size_per_vertex - ? */
77 /* sprite_coord_mode - ??? */
78 /* bypass_vs_viewport_and_clip - handled by viewport setup */
79 /* flatshade_first - handled by index translation */
80 /* gl_rasterization_rules - XXX - viewport code */
81 /* line_width - draw module */
82 /* fill_cw, fill_ccw - draw module or index translation */
84 rast
->shademode
= svga_translate_flatshade( templ
->flatshade
);
85 rast
->cullmode
= svga_translate_cullmode( templ
->cull_face
,
87 rast
->scissortestenable
= templ
->scissor
;
88 rast
->multisampleantialias
= templ
->multisample
;
89 rast
->antialiasedlineenable
= templ
->line_smooth
;
90 rast
->lastpixel
= templ
->line_last_pixel
;
91 rast
->pointsize
= templ
->point_size
;
92 rast
->hw_unfilled
= PIPE_POLYGON_MODE_FILL
;
94 /* Use swtnl + decomposition implement these:
96 if (templ
->poly_stipple_enable
)
97 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
99 if (templ
->line_width
!= 1.0 &&
100 templ
->line_width
!= 0.0)
101 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
103 if (templ
->line_stipple_enable
) {
104 /* LinePattern not implemented on all backends.
107 SVGA3dLinePattern lp
;
108 lp
.repeat
= templ
->line_stipple_factor
+ 1;
109 lp
.pattern
= templ
->line_stipple_pattern
;
110 rast
->linepattern
= lp
.uintValue
;
113 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
117 if (templ
->point_smooth
)
118 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_POINTS
;
121 int fill_front
= templ
->fill_front
;
122 int fill_back
= templ
->fill_back
;
123 int fill
= PIPE_POLYGON_MODE_FILL
;
124 boolean offset_front
= util_get_offset(templ
, fill_front
);
125 boolean offset_back
= util_get_offset(templ
, fill_back
);
128 switch (templ
->cull_face
) {
129 case PIPE_FACE_FRONT_AND_BACK
:
131 fill
= PIPE_POLYGON_MODE_FILL
;
134 case PIPE_FACE_FRONT
:
135 offset
= offset_front
;
140 offset
= offset_back
;
145 if (fill_front
!= fill_back
|| offset_front
!= offset_back
)
147 /* Always need the draw module to work out different
148 * front/back fill modes:
150 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
153 offset
= offset_front
;
163 /* Unfilled primitive modes aren't implemented on all virtual
164 * hardware. We can do some unfilled processing with index
165 * translation, but otherwise need the draw module:
167 if (fill
!= PIPE_POLYGON_MODE_FILL
&&
169 templ
->light_twoside
||
171 templ
->cull_face
!= PIPE_FACE_NONE
))
173 fill
= PIPE_POLYGON_MODE_FILL
;
174 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
177 /* If we are decomposing to lines, and lines need the pipeline,
178 * then we also need the pipeline for tris.
180 if (fill
== PIPE_POLYGON_MODE_LINE
&&
181 (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_LINES
))
183 fill
= PIPE_POLYGON_MODE_FILL
;
184 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
187 /* Similarly for points:
189 if (fill
== PIPE_POLYGON_MODE_POINT
&&
190 (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_POINTS
))
192 fill
= PIPE_POLYGON_MODE_FILL
;
193 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
197 rast
->slopescaledepthbias
= templ
->offset_scale
;
198 rast
->depthbias
= templ
->offset_units
;
201 rast
->hw_unfilled
= fill
;
207 if (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_TRIS
) {
208 /* Turn off stuff which will get done in the draw module:
210 rast
->hw_unfilled
= PIPE_POLYGON_MODE_FILL
;
211 rast
->slopescaledepthbias
= 0;
218 static void svga_bind_rasterizer_state( struct pipe_context
*pipe
,
221 struct svga_context
*svga
= svga_context(pipe
);
222 struct svga_rasterizer_state
*raster
= (struct svga_rasterizer_state
*)state
;
224 svga
->curr
.rast
= raster
;
226 draw_set_rasterizer_state(svga
->swtnl
.draw
, raster
? &raster
->templ
: NULL
,
229 svga
->dirty
|= SVGA_NEW_RAST
;
232 static void svga_delete_rasterizer_state(struct pipe_context
*pipe
,
239 void svga_init_rasterizer_functions( struct svga_context
*svga
)
241 svga
->pipe
.create_rasterizer_state
= svga_create_rasterizer_state
;
242 svga
->pipe
.bind_rasterizer_state
= svga_bind_rasterizer_state
;
243 svga
->pipe
.delete_rasterizer_state
= svga_delete_rasterizer_state
;
247 /***********************************************************************
248 * Hardware state update