Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_pipe_rasterizer.c
4570 views
/**********************************************************1* Copyright 2008-2009 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/2425#include "pipe/p_defines.h"26#include "draw/draw_context.h"27#include "util/u_bitmask.h"28#include "util/u_inlines.h"29#include "util/u_math.h"30#include "util/u_memory.h"3132#include "svga_cmd.h"33#include "svga_context.h"34#include "svga_hw_reg.h"35#include "svga_screen.h"363738/* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW.39*/40static SVGA3dFace41svga_translate_cullmode(unsigned mode, unsigned front_ccw)42{43const int hw_front_ccw = 0; /* hardware is always CW */44switch (mode) {45case PIPE_FACE_NONE:46return SVGA3D_FACE_NONE;47case PIPE_FACE_FRONT:48return front_ccw == hw_front_ccw ? SVGA3D_FACE_FRONT : SVGA3D_FACE_BACK;49case PIPE_FACE_BACK:50return front_ccw == hw_front_ccw ? SVGA3D_FACE_BACK : SVGA3D_FACE_FRONT;51case PIPE_FACE_FRONT_AND_BACK:52return SVGA3D_FACE_FRONT_BACK;53default:54assert(0);55return SVGA3D_FACE_NONE;56}57}5859static SVGA3dShadeMode60svga_translate_flatshade(unsigned mode)61{62return mode ? SVGA3D_SHADEMODE_FLAT : SVGA3D_SHADEMODE_SMOOTH;63}646566static unsigned67translate_fill_mode(unsigned fill)68{69switch (fill) {70case PIPE_POLYGON_MODE_POINT:71return SVGA3D_FILLMODE_POINT;72case PIPE_POLYGON_MODE_LINE:73return SVGA3D_FILLMODE_LINE;74case PIPE_POLYGON_MODE_FILL:75return SVGA3D_FILLMODE_FILL;76default:77assert(!"Bad fill mode");78return SVGA3D_FILLMODE_FILL;79}80}818283static unsigned84translate_cull_mode(unsigned cull)85{86switch (cull) {87case PIPE_FACE_NONE:88return SVGA3D_CULL_NONE;89case PIPE_FACE_FRONT:90return SVGA3D_CULL_FRONT;91case PIPE_FACE_BACK:92return SVGA3D_CULL_BACK;93case PIPE_FACE_FRONT_AND_BACK:94/* NOTE: we simply no-op polygon drawing in svga_draw_vbo() */95return SVGA3D_CULL_NONE;96default:97assert(!"Bad cull mode");98return SVGA3D_CULL_NONE;99}100}101102103static void104define_rasterizer_object(struct svga_context *svga,105struct svga_rasterizer_state *rast)106{107struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);108unsigned fill_mode = translate_fill_mode(rast->templ.fill_front);109const unsigned cull_mode = translate_cull_mode(rast->templ.cull_face);110const int depth_bias = rast->templ.offset_units;111const float slope_scaled_depth_bias = rast->templ.offset_scale;112/* PIPE_CAP_POLYGON_OFFSET_CLAMP not supported: */113const float depth_bias_clamp = 0.0;114const float line_width = rast->templ.line_width > 0.0f ?115rast->templ.line_width : 1.0f;116const uint8 line_factor = rast->templ.line_stipple_enable ?117rast->templ.line_stipple_factor : 0;118const uint16 line_pattern = rast->templ.line_stipple_enable ?119rast->templ.line_stipple_pattern : 0;120const uint8 pv_last = !rast->templ.flatshade_first &&121svgascreen->haveProvokingVertex;122123rast->id = util_bitmask_add(svga->rast_object_id_bm);124125if (rast->templ.fill_front != rast->templ.fill_back) {126/* The VGPU10 device can't handle different front/back fill modes.127* We'll handle that with a swtnl/draw fallback. But we need to128* make sure we always fill triangles in that case.129*/130fill_mode = SVGA3D_FILLMODE_FILL;131}132133SVGA_RETRY(svga, SVGA3D_vgpu10_DefineRasterizerState134(svga->swc,135rast->id,136fill_mode,137cull_mode,138rast->templ.front_ccw,139depth_bias,140depth_bias_clamp,141slope_scaled_depth_bias,142rast->templ.depth_clip_near,143rast->templ.scissor,144rast->templ.multisample,145rast->templ.line_smooth,146line_width,147rast->templ.line_stipple_enable,148line_factor,149line_pattern,150pv_last));151}152153154static void *155svga_create_rasterizer_state(struct pipe_context *pipe,156const struct pipe_rasterizer_state *templ)157{158struct svga_context *svga = svga_context(pipe);159struct svga_rasterizer_state *rast = CALLOC_STRUCT(svga_rasterizer_state);160struct svga_screen *screen = svga_screen(pipe->screen);161162if (!rast)163return NULL;164165/* need this for draw module. */166rast->templ = *templ;167168rast->shademode = svga_translate_flatshade(templ->flatshade);169rast->cullmode = svga_translate_cullmode(templ->cull_face, templ->front_ccw);170rast->scissortestenable = templ->scissor;171rast->multisampleantialias = templ->multisample;172rast->antialiasedlineenable = templ->line_smooth;173rast->lastpixel = templ->line_last_pixel;174rast->pointsprite = templ->point_quad_rasterization;175176if (rast->templ.multisample) {177/* The OpenGL 3.0 spec says points are always drawn as circles when178* MSAA is enabled. Note that our implementation isn't 100% correct,179* though. Our smooth point implementation involves drawing a square,180* computing fragment distance from point center, then attenuating181* the fragment alpha value. We should not attenuate alpha if msaa182* is enabled. We should kill fragments entirely outside the circle183* and let the GPU compute per-fragment coverage.184* But as-is, our implementation gives acceptable results and passes185* Piglit's MSAA point smooth test.186*/187rast->templ.point_smooth = TRUE;188}189190if (rast->templ.point_smooth &&191rast->templ.point_size_per_vertex == 0 &&192rast->templ.point_size <= screen->pointSmoothThreshold) {193/* If the point size is less than the threshold, disable smoothing.194* Note that this only effects point rendering when we use the195* pipe_rasterizer_state::point_size value, not when the point size196* is set in the VS.197*/198rast->templ.point_smooth = FALSE;199}200201if (rast->templ.point_smooth) {202/* For smooth points we need to generate fragments for at least203* a 2x2 region. Otherwise the quad we draw may be too small and204* we may generate no fragments at all.205*/206rast->pointsize = MAX2(2.0f, templ->point_size);207}208else {209rast->pointsize = templ->point_size;210}211212rast->hw_fillmode = PIPE_POLYGON_MODE_FILL;213214/* Use swtnl + decomposition implement these:215*/216217if (templ->line_width <= screen->maxLineWidth) {218/* pass line width to device */219rast->linewidth = MAX2(1.0F, templ->line_width);220}221else if (svga->debug.no_line_width) {222/* nothing */223}224else {225/* use 'draw' pipeline for wide line */226rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;227rast->need_pipeline_lines_str = "line width";228}229230if (templ->line_stipple_enable) {231if (screen->haveLineStipple || svga->debug.force_hw_line_stipple) {232SVGA3dLinePattern lp;233lp.repeat = templ->line_stipple_factor + 1;234lp.pattern = templ->line_stipple_pattern;235rast->linepattern = lp.uintValue;236}237else {238/* use 'draw' module to decompose into short line segments */239rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;240rast->need_pipeline_lines_str = "line stipple";241}242}243244if (!svga_have_vgpu10(svga) && rast->templ.point_smooth) {245rast->need_pipeline |= SVGA_PIPELINE_FLAG_POINTS;246rast->need_pipeline_points_str = "smooth points";247}248249if (templ->line_smooth && !screen->haveLineSmooth) {250/*251* XXX: Enabling the pipeline slows down performance immensely, so ignore252* line smooth state, where there is very little visual improvement.253* Smooth lines will still be drawn for wide lines.254*/255#if 0256rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;257rast->need_pipeline_lines_str = "smooth lines";258#endif259}260261{262int fill_front = templ->fill_front;263int fill_back = templ->fill_back;264int fill = PIPE_POLYGON_MODE_FILL;265boolean offset_front = util_get_offset(templ, fill_front);266boolean offset_back = util_get_offset(templ, fill_back);267boolean offset = FALSE;268269switch (templ->cull_face) {270case PIPE_FACE_FRONT_AND_BACK:271offset = FALSE;272fill = PIPE_POLYGON_MODE_FILL;273break;274275case PIPE_FACE_FRONT:276offset = offset_back;277fill = fill_back;278break;279280case PIPE_FACE_BACK:281offset = offset_front;282fill = fill_front;283break;284285case PIPE_FACE_NONE:286if (fill_front != fill_back || offset_front != offset_back) {287/* Always need the draw module to work out different288* front/back fill modes:289*/290rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;291rast->need_pipeline_tris_str = "different front/back fillmodes";292fill = PIPE_POLYGON_MODE_FILL;293}294else {295offset = offset_front;296fill = fill_front;297}298break;299300default:301assert(0);302break;303}304305/* Unfilled primitive modes aren't implemented on all virtual306* hardware. We can do some unfilled processing with index307* translation, but otherwise need the draw module:308*/309if (fill != PIPE_POLYGON_MODE_FILL &&310(templ->flatshade ||311templ->light_twoside ||312offset)) {313fill = PIPE_POLYGON_MODE_FILL;314rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;315rast->need_pipeline_tris_str = "unfilled primitives with no index manipulation";316}317318/* If we are decomposing to lines, and lines need the pipeline,319* then we also need the pipeline for tris.320*/321if (fill == PIPE_POLYGON_MODE_LINE &&322(rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES)) {323fill = PIPE_POLYGON_MODE_FILL;324rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;325rast->need_pipeline_tris_str = "decomposing lines";326}327328/* Similarly for points:329*/330if (fill == PIPE_POLYGON_MODE_POINT &&331(rast->need_pipeline & SVGA_PIPELINE_FLAG_POINTS)) {332fill = PIPE_POLYGON_MODE_FILL;333rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;334rast->need_pipeline_tris_str = "decomposing points";335}336337if (offset) {338rast->slopescaledepthbias = templ->offset_scale;339rast->depthbias = templ->offset_units;340}341342rast->hw_fillmode = fill;343}344345if (rast->need_pipeline & SVGA_PIPELINE_FLAG_TRIS) {346/* Turn off stuff which will get done in the draw module:347*/348rast->hw_fillmode = PIPE_POLYGON_MODE_FILL;349rast->slopescaledepthbias = 0;350rast->depthbias = 0;351}352353if (0 && rast->need_pipeline) {354debug_printf("svga: rast need_pipeline = 0x%x\n", rast->need_pipeline);355debug_printf(" pnts: %s \n", rast->need_pipeline_points_str);356debug_printf(" lins: %s \n", rast->need_pipeline_lines_str);357debug_printf(" tris: %s \n", rast->need_pipeline_tris_str);358}359360if (svga_have_vgpu10(svga)) {361define_rasterizer_object(svga, rast);362}363364if (templ->poly_smooth) {365pipe_debug_message(&svga->debug.callback, CONFORMANCE,366"GL_POLYGON_SMOOTH not supported");367}368369svga->hud.num_rasterizer_objects++;370SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,371SVGA_STATS_COUNT_RASTERIZERSTATE);372373return rast;374}375376377static void378svga_bind_rasterizer_state(struct pipe_context *pipe, void *state)379{380struct svga_context *svga = svga_context(pipe);381struct svga_rasterizer_state *raster = (struct svga_rasterizer_state *)state;382383if (!raster || !svga->curr.rast) {384svga->dirty |= SVGA_NEW_STIPPLE | SVGA_NEW_DEPTH_STENCIL_ALPHA;385}386else {387if (raster->templ.poly_stipple_enable !=388svga->curr.rast->templ.poly_stipple_enable) {389svga->dirty |= SVGA_NEW_STIPPLE;390}391if (raster->templ.rasterizer_discard !=392svga->curr.rast->templ.rasterizer_discard) {393svga->dirty |= SVGA_NEW_DEPTH_STENCIL_ALPHA;394}395}396397svga->curr.rast = raster;398399svga->dirty |= SVGA_NEW_RAST;400}401402403static void404svga_delete_rasterizer_state(struct pipe_context *pipe, void *state)405{406struct svga_context *svga = svga_context(pipe);407struct svga_rasterizer_state *raster =408(struct svga_rasterizer_state *) state;409410if (svga_have_vgpu10(svga)) {411SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyRasterizerState(svga->swc,412raster->id));413414if (raster->id == svga->state.hw_draw.rasterizer_id)415svga->state.hw_draw.rasterizer_id = SVGA3D_INVALID_ID;416417util_bitmask_clear(svga->rast_object_id_bm, raster->id);418}419420FREE(state);421svga->hud.num_rasterizer_objects--;422}423424425void426svga_init_rasterizer_functions(struct svga_context *svga)427{428svga->pipe.create_rasterizer_state = svga_create_rasterizer_state;429svga->pipe.bind_rasterizer_state = svga_bind_rasterizer_state;430svga->pipe.delete_rasterizer_state = svga_delete_rasterizer_state;431}432433434