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 "pipe/p_defines.h"
27 #include "draw/draw_context.h"
28 #include "util/u_bitmask.h"
29 #include "util/u_inlines.h"
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
34 #include "svga_context.h"
35 #include "svga_hw_reg.h"
36 #include "svga_screen.h"
39 /* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW.
42 svga_translate_cullmode(unsigned mode
, unsigned front_ccw
)
44 const int hw_front_ccw
= 0; /* hardware is always CW */
47 return SVGA3D_FACE_NONE
;
49 return front_ccw
== hw_front_ccw
? SVGA3D_FACE_FRONT
: SVGA3D_FACE_BACK
;
51 return front_ccw
== hw_front_ccw
? SVGA3D_FACE_BACK
: SVGA3D_FACE_FRONT
;
52 case PIPE_FACE_FRONT_AND_BACK
:
53 return SVGA3D_FACE_FRONT_BACK
;
56 return SVGA3D_FACE_NONE
;
60 static SVGA3dShadeMode
61 svga_translate_flatshade(unsigned mode
)
63 return mode
? SVGA3D_SHADEMODE_FLAT
: SVGA3D_SHADEMODE_SMOOTH
;
68 translate_fill_mode(unsigned fill
)
71 case PIPE_POLYGON_MODE_POINT
:
72 return SVGA3D_FILLMODE_POINT
;
73 case PIPE_POLYGON_MODE_LINE
:
74 return SVGA3D_FILLMODE_LINE
;
75 case PIPE_POLYGON_MODE_FILL
:
76 return SVGA3D_FILLMODE_FILL
;
78 assert(!"Bad fill mode");
79 return SVGA3D_FILLMODE_FILL
;
85 translate_cull_mode(unsigned cull
)
89 return SVGA3D_CULL_NONE
;
91 return SVGA3D_CULL_FRONT
;
93 return SVGA3D_CULL_BACK
;
94 case PIPE_FACE_FRONT_AND_BACK
:
95 /* NOTE: we simply no-op polygon drawing in svga_draw_vbo() */
96 return SVGA3D_CULL_NONE
;
98 assert(!"Bad cull mode");
99 return SVGA3D_CULL_NONE
;
105 define_rasterizer_object(struct svga_context
*svga
,
106 struct svga_rasterizer_state
*rast
)
108 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
109 unsigned fill_mode
= translate_fill_mode(rast
->templ
.fill_front
);
110 const unsigned cull_mode
= translate_cull_mode(rast
->templ
.cull_face
);
111 const int depth_bias
= rast
->templ
.offset_units
;
112 const float slope_scaled_depth_bias
= rast
->templ
.offset_scale
;
113 /* PIPE_CAP_POLYGON_OFFSET_CLAMP not supported: */
114 const float depth_bias_clamp
= 0.0;
115 const float line_width
= rast
->templ
.line_width
> 0.0f
?
116 rast
->templ
.line_width
: 1.0f
;
117 const uint8 line_factor
= rast
->templ
.line_stipple_enable
?
118 rast
->templ
.line_stipple_factor
: 0;
119 const uint16 line_pattern
= rast
->templ
.line_stipple_enable
?
120 rast
->templ
.line_stipple_pattern
: 0;
121 const uint8 pv_last
= !rast
->templ
.flatshade_first
&&
122 svgascreen
->haveProvokingVertex
;
126 rast
->id
= util_bitmask_add(svga
->rast_object_id_bm
);
128 if (rast
->templ
.fill_front
!= rast
->templ
.fill_back
) {
129 /* The VGPU10 device can't handle different front/back fill modes.
130 * We'll handle that with a swtnl/draw fallback. But we need to
131 * make sure we always fill triangles in that case.
133 fill_mode
= SVGA3D_FILLMODE_FILL
;
136 for (try = 0; try < 2; try++) {
137 const uint8 pv_last
= !rast
->templ
.flatshade_first
&&
138 svgascreen
->haveProvokingVertex
;
139 enum pipe_error ret
=
140 SVGA3D_vgpu10_DefineRasterizerState(svga
->swc
,
144 rast
->templ
.front_ccw
,
147 slope_scaled_depth_bias
,
148 rast
->templ
.depth_clip_near
,
150 rast
->templ
.multisample
,
151 rast
->templ
.line_smooth
,
153 rast
->templ
.line_stipple_enable
,
159 svga_context_flush(svga
, NULL
);
165 svga_create_rasterizer_state(struct pipe_context
*pipe
,
166 const struct pipe_rasterizer_state
*templ
)
168 struct svga_context
*svga
= svga_context(pipe
);
169 struct svga_rasterizer_state
*rast
= CALLOC_STRUCT(svga_rasterizer_state
);
170 struct svga_screen
*screen
= svga_screen(pipe
->screen
);
175 /* need this for draw module. */
176 rast
->templ
= *templ
;
178 rast
->shademode
= svga_translate_flatshade(templ
->flatshade
);
179 rast
->cullmode
= svga_translate_cullmode(templ
->cull_face
, templ
->front_ccw
);
180 rast
->scissortestenable
= templ
->scissor
;
181 rast
->multisampleantialias
= templ
->multisample
;
182 rast
->antialiasedlineenable
= templ
->line_smooth
;
183 rast
->lastpixel
= templ
->line_last_pixel
;
184 rast
->pointsprite
= templ
->point_quad_rasterization
;
186 if (rast
->templ
.multisample
) {
187 /* The OpenGL 3.0 spec says points are always drawn as circles when
188 * MSAA is enabled. Note that our implementation isn't 100% correct,
189 * though. Our smooth point implementation involves drawing a square,
190 * computing fragment distance from point center, then attenuating
191 * the fragment alpha value. We should not attenuate alpha if msaa
192 * is enabled. We should kill fragments entirely outside the circle
193 * and let the GPU compute per-fragment coverage.
194 * But as-is, our implementation gives acceptable results and passes
195 * Piglit's MSAA point smooth test.
197 rast
->templ
.point_smooth
= TRUE
;
200 if (rast
->templ
.point_smooth
&&
201 rast
->templ
.point_size_per_vertex
== 0 &&
202 rast
->templ
.point_size
<= screen
->pointSmoothThreshold
) {
203 /* If the point size is less than the threshold, disable smoothing.
204 * Note that this only effects point rendering when we use the
205 * pipe_rasterizer_state::point_size value, not when the point size
208 rast
->templ
.point_smooth
= FALSE
;
211 if (rast
->templ
.point_smooth
) {
212 /* For smooth points we need to generate fragments for at least
213 * a 2x2 region. Otherwise the quad we draw may be too small and
214 * we may generate no fragments at all.
216 rast
->pointsize
= MAX2(2.0f
, templ
->point_size
);
219 rast
->pointsize
= templ
->point_size
;
222 rast
->hw_fillmode
= PIPE_POLYGON_MODE_FILL
;
224 /* Use swtnl + decomposition implement these:
227 if (templ
->line_width
<= screen
->maxLineWidth
) {
228 /* pass line width to device */
229 rast
->linewidth
= MAX2(1.0F
, templ
->line_width
);
231 else if (svga
->debug
.no_line_width
) {
235 /* use 'draw' pipeline for wide line */
236 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
237 rast
->need_pipeline_lines_str
= "line width";
240 if (templ
->line_stipple_enable
) {
241 if (screen
->haveLineStipple
|| svga
->debug
.force_hw_line_stipple
) {
242 SVGA3dLinePattern lp
;
243 lp
.repeat
= templ
->line_stipple_factor
+ 1;
244 lp
.pattern
= templ
->line_stipple_pattern
;
245 rast
->linepattern
= lp
.uintValue
;
248 /* use 'draw' module to decompose into short line segments */
249 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
250 rast
->need_pipeline_lines_str
= "line stipple";
254 if (!svga_have_vgpu10(svga
) && rast
->templ
.point_smooth
) {
255 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_POINTS
;
256 rast
->need_pipeline_points_str
= "smooth points";
259 if (templ
->line_smooth
&& !screen
->haveLineSmooth
) {
261 * XXX: Enabling the pipeline slows down performance immensely, so ignore
262 * line smooth state, where there is very little visual improvement.
263 * Smooth lines will still be drawn for wide lines.
266 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_LINES
;
267 rast
->need_pipeline_lines_str
= "smooth lines";
272 int fill_front
= templ
->fill_front
;
273 int fill_back
= templ
->fill_back
;
274 int fill
= PIPE_POLYGON_MODE_FILL
;
275 boolean offset_front
= util_get_offset(templ
, fill_front
);
276 boolean offset_back
= util_get_offset(templ
, fill_back
);
277 boolean offset
= FALSE
;
279 switch (templ
->cull_face
) {
280 case PIPE_FACE_FRONT_AND_BACK
:
282 fill
= PIPE_POLYGON_MODE_FILL
;
285 case PIPE_FACE_FRONT
:
286 offset
= offset_back
;
291 offset
= offset_front
;
296 if (fill_front
!= fill_back
|| offset_front
!= offset_back
) {
297 /* Always need the draw module to work out different
298 * front/back fill modes:
300 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
301 rast
->need_pipeline_tris_str
= "different front/back fillmodes";
302 fill
= PIPE_POLYGON_MODE_FILL
;
305 offset
= offset_front
;
315 /* Unfilled primitive modes aren't implemented on all virtual
316 * hardware. We can do some unfilled processing with index
317 * translation, but otherwise need the draw module:
319 if (fill
!= PIPE_POLYGON_MODE_FILL
&&
321 templ
->light_twoside
||
323 fill
= PIPE_POLYGON_MODE_FILL
;
324 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
325 rast
->need_pipeline_tris_str
= "unfilled primitives with no index manipulation";
328 /* If we are decomposing to lines, and lines need the pipeline,
329 * then we also need the pipeline for tris.
331 if (fill
== PIPE_POLYGON_MODE_LINE
&&
332 (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_LINES
)) {
333 fill
= PIPE_POLYGON_MODE_FILL
;
334 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
335 rast
->need_pipeline_tris_str
= "decomposing lines";
338 /* Similarly for points:
340 if (fill
== PIPE_POLYGON_MODE_POINT
&&
341 (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_POINTS
)) {
342 fill
= PIPE_POLYGON_MODE_FILL
;
343 rast
->need_pipeline
|= SVGA_PIPELINE_FLAG_TRIS
;
344 rast
->need_pipeline_tris_str
= "decomposing points";
348 rast
->slopescaledepthbias
= templ
->offset_scale
;
349 rast
->depthbias
= templ
->offset_units
;
352 rast
->hw_fillmode
= fill
;
355 if (rast
->need_pipeline
& SVGA_PIPELINE_FLAG_TRIS
) {
356 /* Turn off stuff which will get done in the draw module:
358 rast
->hw_fillmode
= PIPE_POLYGON_MODE_FILL
;
359 rast
->slopescaledepthbias
= 0;
363 if (0 && rast
->need_pipeline
) {
364 debug_printf("svga: rast need_pipeline = 0x%x\n", rast
->need_pipeline
);
365 debug_printf(" pnts: %s \n", rast
->need_pipeline_points_str
);
366 debug_printf(" lins: %s \n", rast
->need_pipeline_lines_str
);
367 debug_printf(" tris: %s \n", rast
->need_pipeline_tris_str
);
370 if (svga_have_vgpu10(svga
)) {
371 define_rasterizer_object(svga
, rast
);
374 if (templ
->poly_smooth
) {
375 pipe_debug_message(&svga
->debug
.callback
, CONFORMANCE
,
376 "GL_POLYGON_SMOOTH not supported");
379 svga
->hud
.num_rasterizer_objects
++;
380 SVGA_STATS_COUNT_INC(svga_screen(svga
->pipe
.screen
)->sws
,
381 SVGA_STATS_COUNT_RASTERIZERSTATE
);
388 svga_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
390 struct svga_context
*svga
= svga_context(pipe
);
391 struct svga_rasterizer_state
*raster
= (struct svga_rasterizer_state
*)state
;
393 if (!raster
|| !svga
->curr
.rast
) {
394 svga
->dirty
|= SVGA_NEW_STIPPLE
| SVGA_NEW_DEPTH_STENCIL_ALPHA
;
397 if (raster
->templ
.poly_stipple_enable
!=
398 svga
->curr
.rast
->templ
.poly_stipple_enable
) {
399 svga
->dirty
|= SVGA_NEW_STIPPLE
;
401 if (raster
->templ
.rasterizer_discard
!=
402 svga
->curr
.rast
->templ
.rasterizer_discard
) {
403 svga
->dirty
|= SVGA_NEW_DEPTH_STENCIL_ALPHA
;
407 svga
->curr
.rast
= raster
;
409 svga
->dirty
|= SVGA_NEW_RAST
;
414 svga_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
416 struct svga_context
*svga
= svga_context(pipe
);
417 struct svga_rasterizer_state
*raster
=
418 (struct svga_rasterizer_state
*) state
;
420 if (svga_have_vgpu10(svga
)) {
421 enum pipe_error ret
=
422 SVGA3D_vgpu10_DestroyRasterizerState(svga
->swc
, raster
->id
);
423 if (ret
!= PIPE_OK
) {
424 svga_context_flush(svga
, NULL
);
425 ret
= SVGA3D_vgpu10_DestroyRasterizerState(svga
->swc
, raster
->id
);
428 if (raster
->id
== svga
->state
.hw_draw
.rasterizer_id
)
429 svga
->state
.hw_draw
.rasterizer_id
= SVGA3D_INVALID_ID
;
431 util_bitmask_clear(svga
->rast_object_id_bm
, raster
->id
);
435 svga
->hud
.num_rasterizer_objects
--;
440 svga_init_rasterizer_functions(struct svga_context
*svga
)
442 svga
->pipe
.create_rasterizer_state
= svga_create_rasterizer_state
;
443 svga
->pipe
.bind_rasterizer_state
= svga_bind_rasterizer_state
;
444 svga
->pipe
.delete_rasterizer_state
= svga_delete_rasterizer_state
;