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 "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" 32 33#include "svga_cmd.h" 34#include "svga_context.h" 35#include "svga_hw_reg.h" 36#include "svga_screen.h" 37 38 39/* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW. 40 */ 41static SVGA3dFace 42svga_translate_cullmode(unsigned mode, unsigned front_ccw) 43{ 44 const int hw_front_ccw = 0; /* hardware is always CW */ 45 switch (mode) { 46 case PIPE_FACE_NONE: 47 return SVGA3D_FACE_NONE; 48 case PIPE_FACE_FRONT: 49 return front_ccw == hw_front_ccw ? SVGA3D_FACE_FRONT : SVGA3D_FACE_BACK; 50 case PIPE_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; 54 default: 55 assert(0); 56 return SVGA3D_FACE_NONE; 57 } 58} 59 60static SVGA3dShadeMode 61svga_translate_flatshade(unsigned mode) 62{ 63 return mode ? SVGA3D_SHADEMODE_FLAT : SVGA3D_SHADEMODE_SMOOTH; 64} 65 66 67static unsigned 68translate_fill_mode(unsigned fill) 69{ 70 switch (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; 77 default: 78 assert(!"Bad fill mode"); 79 return SVGA3D_FILLMODE_FILL; 80 } 81} 82 83 84static unsigned 85translate_cull_mode(unsigned cull) 86{ 87 switch (cull) { 88 case PIPE_FACE_NONE: 89 return SVGA3D_CULL_NONE; 90 case PIPE_FACE_FRONT: 91 return SVGA3D_CULL_FRONT; 92 case PIPE_FACE_BACK: 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; 97 default: 98 assert(!"Bad cull mode"); 99 return SVGA3D_CULL_NONE; 100 } 101} 102 103 104static void 105define_rasterizer_object(struct svga_context *svga, 106 struct svga_rasterizer_state *rast) 107{ 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; 123 124 rast->id = util_bitmask_add(svga->rast_object_id_bm); 125 126 if (rast->templ.fill_front != rast->templ.fill_back) { 127 /* The VGPU10 device can't handle different front/back fill modes. 128 * We'll handle that with a swtnl/draw fallback. But we need to 129 * make sure we always fill triangles in that case. 130 */ 131 fill_mode = SVGA3D_FILLMODE_FILL; 132 } 133 134 SVGA_RETRY(svga, SVGA3D_vgpu10_DefineRasterizerState 135 (svga->swc, 136 rast->id, 137 fill_mode, 138 cull_mode, 139 rast->templ.front_ccw, 140 depth_bias, 141 depth_bias_clamp, 142 slope_scaled_depth_bias, 143 rast->templ.depth_clip_near, 144 rast->templ.scissor, 145 rast->templ.multisample, 146 rast->templ.line_smooth, 147 line_width, 148 rast->templ.line_stipple_enable, 149 line_factor, 150 line_pattern, 151 pv_last)); 152} 153 154 155static void * 156svga_create_rasterizer_state(struct pipe_context *pipe, 157 const struct pipe_rasterizer_state *templ) 158{ 159 struct svga_context *svga = svga_context(pipe); 160 struct svga_rasterizer_state *rast = CALLOC_STRUCT(svga_rasterizer_state); 161 struct svga_screen *screen = svga_screen(pipe->screen); 162 163 if (!rast) 164 return NULL; 165 166 /* need this for draw module. */ 167 rast->templ = *templ; 168 169 rast->shademode = svga_translate_flatshade(templ->flatshade); 170 rast->cullmode = svga_translate_cullmode(templ->cull_face, templ->front_ccw); 171 rast->scissortestenable = templ->scissor; 172 rast->multisampleantialias = templ->multisample; 173 rast->antialiasedlineenable = templ->line_smooth; 174 rast->lastpixel = templ->line_last_pixel; 175 rast->pointsprite = templ->point_quad_rasterization; 176 177 if (rast->templ.multisample) { 178 /* The OpenGL 3.0 spec says points are always drawn as circles when 179 * MSAA is enabled. Note that our implementation isn't 100% correct, 180 * though. Our smooth point implementation involves drawing a square, 181 * computing fragment distance from point center, then attenuating 182 * the fragment alpha value. We should not attenuate alpha if msaa 183 * is enabled. We should kill fragments entirely outside the circle 184 * and let the GPU compute per-fragment coverage. 185 * But as-is, our implementation gives acceptable results and passes 186 * Piglit's MSAA point smooth test. 187 */ 188 rast->templ.point_smooth = TRUE; 189 } 190 191 if (rast->templ.point_smooth && 192 rast->templ.point_size_per_vertex == 0 && 193 rast->templ.point_size <= screen->pointSmoothThreshold) { 194 /* If the point size is less than the threshold, disable smoothing. 195 * Note that this only effects point rendering when we use the 196 * pipe_rasterizer_state::point_size value, not when the point size 197 * is set in the VS. 198 */ 199 rast->templ.point_smooth = FALSE; 200 } 201 202 if (rast->templ.point_smooth) { 203 /* For smooth points we need to generate fragments for at least 204 * a 2x2 region. Otherwise the quad we draw may be too small and 205 * we may generate no fragments at all. 206 */ 207 rast->pointsize = MAX2(2.0f, templ->point_size); 208 } 209 else { 210 rast->pointsize = templ->point_size; 211 } 212 213 rast->hw_fillmode = PIPE_POLYGON_MODE_FILL; 214 215 /* Use swtnl + decomposition implement these: 216 */ 217 218 if (templ->line_width <= screen->maxLineWidth) { 219 /* pass line width to device */ 220 rast->linewidth = MAX2(1.0F, templ->line_width); 221 } 222 else if (svga->debug.no_line_width) { 223 /* nothing */ 224 } 225 else { 226 /* use 'draw' pipeline for wide line */ 227 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES; 228 rast->need_pipeline_lines_str = "line width"; 229 } 230 231 if (templ->line_stipple_enable) { 232 if (screen->haveLineStipple || svga->debug.force_hw_line_stipple) { 233 SVGA3dLinePattern lp; 234 lp.repeat = templ->line_stipple_factor + 1; 235 lp.pattern = templ->line_stipple_pattern; 236 rast->linepattern = lp.uintValue; 237 } 238 else { 239 /* use 'draw' module to decompose into short line segments */ 240 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES; 241 rast->need_pipeline_lines_str = "line stipple"; 242 } 243 } 244 245 if (!svga_have_vgpu10(svga) && rast->templ.point_smooth) { 246 rast->need_pipeline |= SVGA_PIPELINE_FLAG_POINTS; 247 rast->need_pipeline_points_str = "smooth points"; 248 } 249 250 if (templ->line_smooth && !screen->haveLineSmooth) { 251 /* 252 * XXX: Enabling the pipeline slows down performance immensely, so ignore 253 * line smooth state, where there is very little visual improvement. 254 * Smooth lines will still be drawn for wide lines. 255 */ 256#if 0 257 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES; 258 rast->need_pipeline_lines_str = "smooth lines"; 259#endif 260 } 261 262 { 263 int fill_front = templ->fill_front; 264 int fill_back = templ->fill_back; 265 int fill = PIPE_POLYGON_MODE_FILL; 266 boolean offset_front = util_get_offset(templ, fill_front); 267 boolean offset_back = util_get_offset(templ, fill_back); 268 boolean offset = FALSE; 269 270 switch (templ->cull_face) { 271 case PIPE_FACE_FRONT_AND_BACK: 272 offset = FALSE; 273 fill = PIPE_POLYGON_MODE_FILL; 274 break; 275 276 case PIPE_FACE_FRONT: 277 offset = offset_back; 278 fill = fill_back; 279 break; 280 281 case PIPE_FACE_BACK: 282 offset = offset_front; 283 fill = fill_front; 284 break; 285 286 case PIPE_FACE_NONE: 287 if (fill_front != fill_back || offset_front != offset_back) { 288 /* Always need the draw module to work out different 289 * front/back fill modes: 290 */ 291 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS; 292 rast->need_pipeline_tris_str = "different front/back fillmodes"; 293 fill = PIPE_POLYGON_MODE_FILL; 294 } 295 else { 296 offset = offset_front; 297 fill = fill_front; 298 } 299 break; 300 301 default: 302 assert(0); 303 break; 304 } 305 306 /* Unfilled primitive modes aren't implemented on all virtual 307 * hardware. We can do some unfilled processing with index 308 * translation, but otherwise need the draw module: 309 */ 310 if (fill != PIPE_POLYGON_MODE_FILL && 311 (templ->flatshade || 312 templ->light_twoside || 313 offset)) { 314 fill = PIPE_POLYGON_MODE_FILL; 315 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS; 316 rast->need_pipeline_tris_str = "unfilled primitives with no index manipulation"; 317 } 318 319 /* If we are decomposing to lines, and lines need the pipeline, 320 * then we also need the pipeline for tris. 321 */ 322 if (fill == PIPE_POLYGON_MODE_LINE && 323 (rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES)) { 324 fill = PIPE_POLYGON_MODE_FILL; 325 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS; 326 rast->need_pipeline_tris_str = "decomposing lines"; 327 } 328 329 /* Similarly for points: 330 */ 331 if (fill == PIPE_POLYGON_MODE_POINT && 332 (rast->need_pipeline & SVGA_PIPELINE_FLAG_POINTS)) { 333 fill = PIPE_POLYGON_MODE_FILL; 334 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS; 335 rast->need_pipeline_tris_str = "decomposing points"; 336 } 337 338 if (offset) { 339 rast->slopescaledepthbias = templ->offset_scale; 340 rast->depthbias = templ->offset_units; 341 } 342 343 rast->hw_fillmode = fill; 344 } 345 346 if (rast->need_pipeline & SVGA_PIPELINE_FLAG_TRIS) { 347 /* Turn off stuff which will get done in the draw module: 348 */ 349 rast->hw_fillmode = PIPE_POLYGON_MODE_FILL; 350 rast->slopescaledepthbias = 0; 351 rast->depthbias = 0; 352 } 353 354 if (0 && rast->need_pipeline) { 355 debug_printf("svga: rast need_pipeline = 0x%x\n", rast->need_pipeline); 356 debug_printf(" pnts: %s \n", rast->need_pipeline_points_str); 357 debug_printf(" lins: %s \n", rast->need_pipeline_lines_str); 358 debug_printf(" tris: %s \n", rast->need_pipeline_tris_str); 359 } 360 361 if (svga_have_vgpu10(svga)) { 362 define_rasterizer_object(svga, rast); 363 } 364 365 if (templ->poly_smooth) { 366 pipe_debug_message(&svga->debug.callback, CONFORMANCE, 367 "GL_POLYGON_SMOOTH not supported"); 368 } 369 370 svga->hud.num_rasterizer_objects++; 371 SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws, 372 SVGA_STATS_COUNT_RASTERIZERSTATE); 373 374 return rast; 375} 376 377 378static void 379svga_bind_rasterizer_state(struct pipe_context *pipe, void *state) 380{ 381 struct svga_context *svga = svga_context(pipe); 382 struct svga_rasterizer_state *raster = (struct svga_rasterizer_state *)state; 383 384 if (!raster || !svga->curr.rast) { 385 svga->dirty |= SVGA_NEW_STIPPLE | SVGA_NEW_DEPTH_STENCIL_ALPHA; 386 } 387 else { 388 if (raster->templ.poly_stipple_enable != 389 svga->curr.rast->templ.poly_stipple_enable) { 390 svga->dirty |= SVGA_NEW_STIPPLE; 391 } 392 if (raster->templ.rasterizer_discard != 393 svga->curr.rast->templ.rasterizer_discard) { 394 svga->dirty |= SVGA_NEW_DEPTH_STENCIL_ALPHA; 395 } 396 } 397 398 svga->curr.rast = raster; 399 400 svga->dirty |= SVGA_NEW_RAST; 401} 402 403 404static void 405svga_delete_rasterizer_state(struct pipe_context *pipe, void *state) 406{ 407 struct svga_context *svga = svga_context(pipe); 408 struct svga_rasterizer_state *raster = 409 (struct svga_rasterizer_state *) state; 410 411 if (svga_have_vgpu10(svga)) { 412 SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyRasterizerState(svga->swc, 413 raster->id)); 414 415 if (raster->id == svga->state.hw_draw.rasterizer_id) 416 svga->state.hw_draw.rasterizer_id = SVGA3D_INVALID_ID; 417 418 util_bitmask_clear(svga->rast_object_id_bm, raster->id); 419 } 420 421 FREE(state); 422 svga->hud.num_rasterizer_objects--; 423} 424 425 426void 427svga_init_rasterizer_functions(struct svga_context *svga) 428{ 429 svga->pipe.create_rasterizer_state = svga_create_rasterizer_state; 430 svga->pipe.bind_rasterizer_state = svga_bind_rasterizer_state; 431 svga->pipe.delete_rasterizer_state = svga_delete_rasterizer_state; 432} 433