Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_validate.c
4565 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/* Authors: Keith Whitwell <[email protected]>28*/2930#include "util/u_memory.h"31#include "util/u_math.h"32#include "util/u_prim.h"33#include "pipe/p_defines.h"34#include "draw_private.h"35#include "draw_pipe.h"36#include "draw_context.h"37#include "draw_vbuf.h"383940/**41* Default version of a function to check if we need any special42* pipeline stages, or whether prims/verts can go through untouched.43* Don't test for bypass clipping or vs modes, this function is just44* about the primitive pipeline stages.45*46* This can be overridden by the driver.47*/48boolean49draw_need_pipeline(const struct draw_context *draw,50const struct pipe_rasterizer_state *rasterizer,51unsigned int prim )52{53unsigned reduced_prim = u_reduced_prim(prim);5455/* If the driver has overridden this, use that version:56*/57if (draw->render &&58draw->render->need_pipeline)59{60return draw->render->need_pipeline( draw->render,61rasterizer,62prim );63}6465/* Don't have to worry about triangles turning into lines/points66* and triggering the pipeline, because we have to trigger the67* pipeline *anyway* if unfilled mode is active.68*/69if (reduced_prim == PIPE_PRIM_LINES) {70/* line stipple */71if (rasterizer->line_stipple_enable && draw->pipeline.line_stipple)72return TRUE;7374/* wide lines */75if (roundf(rasterizer->line_width) > draw->pipeline.wide_line_threshold)76return TRUE;7778/* AA lines */79if ((!rasterizer->multisample && rasterizer->line_smooth) && draw->pipeline.aaline)80return TRUE;8182if (draw_current_shader_num_written_culldistances(draw))83return TRUE;84}85else if (reduced_prim == PIPE_PRIM_POINTS) {86/* large points */87if (rasterizer->point_size > draw->pipeline.wide_point_threshold)88return TRUE;8990/* sprite points */91if (rasterizer->point_quad_rasterization92&& draw->pipeline.wide_point_sprites)93return TRUE;9495/* AA points */96if ((!rasterizer->multisample && rasterizer->point_smooth) && draw->pipeline.aapoint)97return TRUE;9899/* point sprites */100if (rasterizer->sprite_coord_enable && draw->pipeline.point_sprite)101return TRUE;102103if (draw_current_shader_num_written_culldistances(draw))104return TRUE;105}106else if (reduced_prim == PIPE_PRIM_TRIANGLES) {107/* polygon stipple */108if (rasterizer->poly_stipple_enable && draw->pipeline.pstipple)109return TRUE;110111/* unfilled polygons */112if (rasterizer->fill_front != PIPE_POLYGON_MODE_FILL ||113rasterizer->fill_back != PIPE_POLYGON_MODE_FILL)114return TRUE;115116/* polygon offset */117if (rasterizer->offset_point ||118rasterizer->offset_line ||119rasterizer->offset_tri)120return TRUE;121122/* two-side lighting */123if (rasterizer->light_twoside)124return TRUE;125126if (draw_current_shader_num_written_culldistances(draw))127return TRUE;128}129130/* polygon cull - this is difficult - hardware can cull just fine131* most of the time (though sometimes CULL_NEITHER is unsupported.132*133* Generally this isn't a reason to require the pipeline, though.134*135if (rasterizer->cull_mode)136return TRUE;137*/138139return FALSE;140}141142143144/**145* Rebuild the rendering pipeline.146*/147static struct draw_stage *validate_pipeline( struct draw_stage *stage )148{149struct draw_context *draw = stage->draw;150struct draw_stage *next = draw->pipeline.rasterize;151boolean need_det = FALSE;152boolean precalc_flat = FALSE;153boolean wide_lines, wide_points;154const struct pipe_rasterizer_state *rast = draw->rasterizer;155156/* Set the validate's next stage to the rasterize stage, so that it157* can be found later if needed for flushing.158*/159stage->next = next;160161/* drawing wide, non-AA lines? */162wide_lines = rast->line_width != 1.0f &&163roundf(rast->line_width) > draw->pipeline.wide_line_threshold &&164(!rast->line_smooth || rast->multisample);165166/* drawing large/sprite points (but not AA points)? */167if (rast->sprite_coord_enable && draw->pipeline.point_sprite)168wide_points = TRUE;169else if ((!rast->multisample && rast->point_smooth) && draw->pipeline.aapoint)170wide_points = FALSE;171else if (rast->point_size > draw->pipeline.wide_point_threshold)172wide_points = TRUE;173else if (rast->point_quad_rasterization && draw->pipeline.wide_point_sprites)174wide_points = TRUE;175else176wide_points = FALSE;177178/*179* NOTE: we build up the pipeline in end-to-start order.180*181* TODO: make the current primitive part of the state and build182* shorter pipelines for lines & points.183*/184185if ((!rast->multisample && rast->line_smooth) && draw->pipeline.aaline) {186draw->pipeline.aaline->next = next;187next = draw->pipeline.aaline;188precalc_flat = TRUE;189}190191if ((!rast->multisample && rast->point_smooth) && draw->pipeline.aapoint) {192draw->pipeline.aapoint->next = next;193next = draw->pipeline.aapoint;194}195196if (wide_lines) {197draw->pipeline.wide_line->next = next;198next = draw->pipeline.wide_line;199precalc_flat = TRUE;200}201202if (wide_points) {203draw->pipeline.wide_point->next = next;204next = draw->pipeline.wide_point;205}206207if (rast->line_stipple_enable && draw->pipeline.line_stipple) {208draw->pipeline.stipple->next = next;209next = draw->pipeline.stipple;210precalc_flat = TRUE; /* only needed for lines really */211}212213if (rast->poly_stipple_enable214&& draw->pipeline.pstipple) {215draw->pipeline.pstipple->next = next;216next = draw->pipeline.pstipple;217}218219if (rast->fill_front != PIPE_POLYGON_MODE_FILL ||220rast->fill_back != PIPE_POLYGON_MODE_FILL) {221draw->pipeline.unfilled->next = next;222next = draw->pipeline.unfilled;223precalc_flat = TRUE; /* only needed for triangles really */224need_det = TRUE;225}226227if (precalc_flat) {228/*229* could only run the stage if either rast->flatshade is true230* or there's constant interpolated values.231*/232draw->pipeline.flatshade->next = next;233next = draw->pipeline.flatshade;234}235236if (rast->offset_point ||237rast->offset_line ||238rast->offset_tri) {239draw->pipeline.offset->next = next;240next = draw->pipeline.offset;241need_det = TRUE;242}243244if (rast->light_twoside) {245draw->pipeline.twoside->next = next;246next = draw->pipeline.twoside;247need_det = TRUE;248}249250/* Always run the cull stage as we calculate determinant there251* also.252*253* This can actually be a win as culling out the triangles can lead254* to less work emitting vertices, smaller vertex buffers, etc.255* It's difficult to say whether this will be true in general.256*/257if (need_det || rast->cull_face != PIPE_FACE_NONE) {258draw->pipeline.cull->next = next;259next = draw->pipeline.cull;260}261262/* Clip stage263*/264if (draw->clip_xy || draw->clip_z || draw->clip_user)265{266draw->pipeline.clip->next = next;267next = draw->pipeline.clip;268}269270if (draw_current_shader_num_written_culldistances(draw)) {271draw->pipeline.user_cull->next = next;272next = draw->pipeline.user_cull;273}274275draw->pipeline.first = next;276277if (0) {278debug_printf("draw pipeline:\n");279for (next = draw->pipeline.first; next ; next = next->next )280debug_printf(" %s\n", next->name);281debug_printf("\n");282}283284return draw->pipeline.first;285}286287static void validate_tri( struct draw_stage *stage,288struct prim_header *header )289{290struct draw_stage *pipeline = validate_pipeline( stage );291pipeline->tri( pipeline, header );292}293294static void validate_line( struct draw_stage *stage,295struct prim_header *header )296{297struct draw_stage *pipeline = validate_pipeline( stage );298pipeline->line( pipeline, header );299}300301static void validate_point( struct draw_stage *stage,302struct prim_header *header )303{304struct draw_stage *pipeline = validate_pipeline( stage );305pipeline->point( pipeline, header );306}307308static void validate_reset_stipple_counter( struct draw_stage *stage )309{310struct draw_stage *pipeline = validate_pipeline( stage );311pipeline->reset_stipple_counter( pipeline );312}313314static void validate_flush( struct draw_stage *stage,315unsigned flags )316{317/* May need to pass a backend flush on to the rasterize stage.318*/319if (stage->next)320stage->next->flush( stage->next, flags );321}322323324static void validate_destroy( struct draw_stage *stage )325{326FREE( stage );327}328329330/**331* Create validate pipeline stage.332*/333struct draw_stage *draw_validate_stage( struct draw_context *draw )334{335struct draw_stage *stage = CALLOC_STRUCT(draw_stage);336if (!stage)337return NULL;338339stage->draw = draw;340stage->name = "validate";341stage->next = NULL;342stage->point = validate_point;343stage->line = validate_line;344stage->tri = validate_tri;345stage->flush = validate_flush;346stage->reset_stipple_counter = validate_reset_stipple_counter;347stage->destroy = validate_destroy;348349return stage;350}351352353