Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_pipe_draw.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**********************************************************/242526#include "util/u_draw.h"27#include "util/format/u_format.h"28#include "util/u_helpers.h"29#include "util/u_inlines.h"30#include "util/u_prim.h"31#include "util/u_prim_restart.h"3233#include "svga_context.h"34#include "svga_draw_private.h"35#include "svga_screen.h"36#include "svga_draw.h"37#include "svga_shader.h"38#include "svga_surface.h"39#include "svga_swtnl.h"40#include "svga_debug.h"41#include "svga_resource_buffer.h"424344static enum pipe_error45retry_draw_range_elements(struct svga_context *svga,46const struct pipe_draw_info *info,47const struct pipe_draw_start_count_bias *draw,48unsigned count)49{50SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);5152SVGA_RETRY(svga, svga_hwtnl_draw_range_elements(svga->hwtnl, info, draw, count));5354SVGA_STATS_TIME_POP(svga_sws(svga));55return PIPE_OK;56}575859static enum pipe_error60retry_draw_arrays( struct svga_context *svga,61enum pipe_prim_type prim, unsigned start, unsigned count,62unsigned start_instance, unsigned instance_count,63ubyte vertices_per_patch)64{65enum pipe_error ret;6667SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);6869SVGA_RETRY_OOM(svga, ret, svga_hwtnl_draw_arrays(svga->hwtnl, prim, start,70count, start_instance,71instance_count,72vertices_per_patch));73SVGA_STATS_TIME_POP(svga_sws(svga));74return ret;75}767778/**79* Auto draw (get vertex count from a transform feedback result).80*/81static enum pipe_error82retry_draw_auto(struct svga_context *svga,83const struct pipe_draw_info *info,84const struct pipe_draw_indirect_info *indirect)85{86assert(svga_have_sm5(svga));87assert(indirect->count_from_stream_output);88assert(info->instance_count == 1);89/* SO drawing implies core profile and none of these prim types */90assert(info->mode != PIPE_PRIM_QUADS &&91info->mode != PIPE_PRIM_QUAD_STRIP &&92info->mode != PIPE_PRIM_POLYGON);9394if (info->mode == PIPE_PRIM_LINE_LOOP) {95/* XXX need to do a fallback */96assert(!"draw auto fallback not supported yet");97return PIPE_OK;98}99else {100SVGA3dPrimitiveRange range;101unsigned hw_count;102103range.primType = svga_translate_prim(info->mode, 12, &hw_count,104info->vertices_per_patch);105range.primitiveCount = 0;106range.indexArray.surfaceId = SVGA3D_INVALID_ID;107range.indexArray.offset = 0;108range.indexArray.stride = 0;109range.indexWidth = 0;110range.indexBias = 0;111112SVGA_RETRY(svga, svga_hwtnl_prim113(svga->hwtnl, &range,1140, /* vertex count comes from SO buffer */1150, /* don't know min index */116~0u, /* don't know max index */117NULL, /* no index buffer */1180, /* start instance */1191, /* only 1 instance supported */120NULL, /* indirect drawing info */121indirect->count_from_stream_output));122123return PIPE_OK;124}125}126127128/**129* Indirect draw (get vertex count, start index, etc. from a buffer object.130*/131static enum pipe_error132retry_draw_indirect(struct svga_context *svga,133const struct pipe_draw_info *info,134const struct pipe_draw_indirect_info *indirect)135{136assert(svga_have_sm5(svga));137assert(indirect && indirect->buffer);138/* indirect drawing implies core profile and none of these prim types */139assert(info->mode != PIPE_PRIM_QUADS &&140info->mode != PIPE_PRIM_QUAD_STRIP &&141info->mode != PIPE_PRIM_POLYGON);142143if (info->mode == PIPE_PRIM_LINE_LOOP) {144/* need to do a fallback */145util_draw_indirect(&svga->pipe, info, indirect);146return PIPE_OK;147}148else {149SVGA3dPrimitiveRange range;150unsigned hw_count;151152range.primType = svga_translate_prim(info->mode, 12, &hw_count,153info->vertices_per_patch);154range.primitiveCount = 0; /* specified in indirect buffer */155range.indexArray.surfaceId = SVGA3D_INVALID_ID;156range.indexArray.offset = 0;157range.indexArray.stride = 0;158range.indexWidth = info->index_size;159range.indexBias = 0; /* specified in indirect buffer */160161SVGA_RETRY(svga, svga_hwtnl_prim162(svga->hwtnl, &range,1630, /* vertex count is in indirect buffer */1640, /* don't know min index */165~0u, /* don't know max index */166info->index.resource,167info->start_instance,1680, /* don't know instance count */169indirect,170NULL)); /* SO vertex count */171172return PIPE_OK;173}174}175176177/**178* Determine if we need to implement primitive restart with a fallback179* path which breaks the original primitive into sub-primitive at the180* restart indexes.181*/182static boolean183need_fallback_prim_restart(const struct svga_context *svga,184const struct pipe_draw_info *info)185{186if (info->primitive_restart && info->index_size) {187if (!svga_have_vgpu10(svga))188return TRUE;189else if (!svga->state.sw.need_swtnl) {190if (info->index_size == 1)191return TRUE; /* no device support for 1-byte indexes */192else if (info->index_size == 2)193return info->restart_index != 0xffff;194else195return info->restart_index != 0xffffffff;196}197}198199return FALSE;200}201202203/**204* A helper function to return the vertex count from the primitive count205* returned from the stream output statistics query for the specified stream.206*/207static unsigned208get_vcount_from_stream_output(struct svga_context *svga,209const struct pipe_draw_info *info,210unsigned stream)211{212unsigned primcount;213primcount = svga_get_primcount_from_stream_output(svga, stream);214return u_vertices_for_prims(info->mode, primcount);215}216217218static void219svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info,220unsigned drawid_offset,221const struct pipe_draw_indirect_info *indirect,222const struct pipe_draw_start_count_bias *draws,223unsigned num_draws)224{225if (num_draws > 1) {226util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);227return;228}229230if (!indirect && (!draws[0].count || !info->instance_count))231return;232233struct svga_context *svga = svga_context(pipe);234enum pipe_prim_type reduced_prim = u_reduced_prim(info->mode);235unsigned count = draws[0].count;236enum pipe_error ret = 0;237boolean needed_swtnl;238239SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);240241svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */242243if (u_reduced_prim(info->mode) == PIPE_PRIM_TRIANGLES &&244svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)245goto done;246247/*248* Mark currently bound target surfaces as dirty249* doesn't really matter if it is done before drawing.250*251* TODO If we ever normaly return something other then252* true we should not mark it as dirty then.253*/254svga_mark_surfaces_dirty(svga_context(pipe));255256if (svga->curr.reduced_prim != reduced_prim) {257svga->curr.reduced_prim = reduced_prim;258svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;259}260261/* We need to adjust the vertexID in the vertex shader since SV_VertexID262* always start from 0 for DrawArrays and does not include baseVertex for263* DrawIndexed.264*/265unsigned index_bias = info->index_size ? draws->index_bias : 0;266if (svga->curr.vertex_id_bias != (draws[0].start + index_bias)) {267svga->curr.vertex_id_bias = draws[0].start + index_bias;268svga->dirty |= SVGA_NEW_VS_CONSTS;269}270271if (svga->curr.vertices_per_patch != info->vertices_per_patch) {272svga->curr.vertices_per_patch = info->vertices_per_patch;273274/* If input patch size changes, we need to notifiy the TCS275* code to reevaluate the shader variant since the276* vertices per patch count is a constant in the control277* point count declaration.278*/279if (svga->curr.tcs || svga->curr.tes)280svga->dirty |= SVGA_NEW_TCS_PARAM;281}282283if (need_fallback_prim_restart(svga, info)) {284enum pipe_error r;285r = util_draw_vbo_without_prim_restart(pipe, info, drawid_offset, indirect, &draws[0]);286assert(r == PIPE_OK);287(void) r;288goto done;289}290291if (!indirect && !u_trim_pipe_prim(info->mode, &count))292goto done;293294needed_swtnl = svga->state.sw.need_swtnl;295296svga_update_state_retry(svga, SVGA_STATE_NEED_SWTNL);297298if (svga->state.sw.need_swtnl) {299svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */300if (!needed_swtnl) {301/*302* We're switching from HW to SW TNL. SW TNL will require mapping all303* currently bound vertex buffers, some of which may already be304* referenced in the current command buffer as result of previous HW305* TNL. So flush now, to prevent the context to flush while a referred306* vertex buffer is mapped.307*/308309svga_context_flush(svga, NULL);310}311312/* Avoid leaking the previous hwtnl bias to swtnl */313svga_hwtnl_set_index_bias(svga->hwtnl, 0);314ret = svga_swtnl_draw_vbo(svga, info, drawid_offset, indirect, &draws[0]);315}316else {317if (!svga_update_state_retry(svga, SVGA_STATE_HW_DRAW)) {318static const char *msg = "State update failed, skipping draw call";319debug_printf("%s\n", msg);320pipe_debug_message(&svga->debug.callback, INFO, "%s", msg);321goto done;322}323svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);324325svga_update_state_retry(svga, SVGA_STATE_HW_DRAW);326327/** determine if flatshade is to be used after svga_update_state()328* in case the fragment shader is changed.329*/330svga_hwtnl_set_flatshade(svga->hwtnl,331svga->curr.rast->templ.flatshade ||332svga_is_using_flat_shading(svga),333svga->curr.rast->templ.flatshade_first);334335if (indirect && indirect->count_from_stream_output) {336unsigned stream = 0;337assert(count == 0);338339/* If the vertex count is from the stream output of a non-zero stream340* or the draw info specifies instancing, we will need a workaround341* since the draw_auto command does not support stream instancing.342* The workaround requires querying the vertex count from the343* stream output statistics query for the specified stream and then344* fallback to the regular draw function.345*/346347/* Check the stream index of the specified stream output target */348for (unsigned i = 0; i < ARRAY_SIZE(svga->so_targets); i++) {349if (svga->vcount_so_targets[i] == indirect->count_from_stream_output) {350stream = (svga->vcount_buffer_stream >> (i * 4)) & 0xf;351break;352}353}354if (info->instance_count > 1 || stream > 0) {355count = get_vcount_from_stream_output(svga, info, stream);356}357}358359if (indirect && indirect->count_from_stream_output && count == 0) {360ret = retry_draw_auto(svga, info, indirect);361}362else if (indirect && indirect->buffer) {363ret = retry_draw_indirect(svga, info, indirect);364}365else if (info->index_size) {366ret = retry_draw_range_elements(svga, info, &draws[0], count);367}368else {369ret = retry_draw_arrays(svga, info->mode, draws[0].start, count,370info->start_instance, info->instance_count,371info->vertices_per_patch);372}373}374375/* XXX: Silence warnings, do something sensible here? */376(void)ret;377378if (SVGA_DEBUG & DEBUG_FLUSH) {379svga_hwtnl_flush_retry(svga);380svga_context_flush(svga, NULL);381}382383done:384SVGA_STATS_TIME_POP(svga_sws(svga));385}386387388void389svga_init_draw_functions(struct svga_context *svga)390{391svga->pipe.draw_vbo = svga_draw_vbo;392}393394395