Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_draw_arrays.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 "svga_cmd.h"2627#include "util/u_inlines.h"28#include "util/u_prim.h"29#include "indices/u_indices.h"3031#include "svga_hw_reg.h"32#include "svga_draw.h"33#include "svga_draw_private.h"34#include "svga_context.h"35#include "svga_shader.h"363738#define DBG 0394041static enum pipe_error42generate_indices(struct svga_hwtnl *hwtnl,43unsigned nr,44unsigned index_size,45u_generate_func generate, struct pipe_resource **out_buf)46{47struct pipe_context *pipe = &hwtnl->svga->pipe;48struct pipe_transfer *transfer;49unsigned size = index_size * nr;50struct pipe_resource *dst = NULL;51void *dst_map = NULL;5253dst = pipe_buffer_create(pipe->screen, PIPE_BIND_INDEX_BUFFER,54PIPE_USAGE_IMMUTABLE, size);55if (!dst)56goto fail;5758dst_map = pipe_buffer_map(pipe, dst, PIPE_MAP_WRITE, &transfer);59if (!dst_map)60goto fail;6162generate(0, nr, dst_map);6364pipe_buffer_unmap(pipe, transfer);6566*out_buf = dst;67return PIPE_OK;6869fail:70if (dst_map)71pipe_buffer_unmap(pipe, transfer);7273if (dst)74pipe->screen->resource_destroy(pipe->screen, dst);7576return PIPE_ERROR_OUT_OF_MEMORY;77}787980static boolean81compare(unsigned cached_nr, unsigned nr, unsigned type)82{83if (type == U_GENERATE_REUSABLE)84return cached_nr >= nr;85else86return cached_nr == nr;87}888990static enum pipe_error91retrieve_or_generate_indices(struct svga_hwtnl *hwtnl,92enum pipe_prim_type prim,93unsigned gen_type,94unsigned gen_nr,95unsigned gen_size,96u_generate_func generate,97struct pipe_resource **out_buf)98{99enum pipe_error ret = PIPE_OK;100int i;101102SVGA_STATS_TIME_PUSH(svga_sws(hwtnl->svga), SVGA_STATS_TIME_GENERATEINDICES);103104for (i = 0; i < IDX_CACHE_MAX; i++) {105if (hwtnl->index_cache[prim][i].buffer != NULL &&106hwtnl->index_cache[prim][i].generate == generate) {107if (compare(hwtnl->index_cache[prim][i].gen_nr, gen_nr, gen_type)) {108pipe_resource_reference(out_buf,109hwtnl->index_cache[prim][i].buffer);110111if (DBG)112debug_printf("%s retrieve %d/%d\n", __FUNCTION__, i, gen_nr);113114goto done;115}116else if (gen_type == U_GENERATE_REUSABLE) {117pipe_resource_reference(&hwtnl->index_cache[prim][i].buffer,118NULL);119120if (DBG)121debug_printf("%s discard %d/%d\n", __FUNCTION__,122i, hwtnl->index_cache[prim][i].gen_nr);123124break;125}126}127}128129if (i == IDX_CACHE_MAX) {130unsigned smallest = 0;131unsigned smallest_size = ~0;132133for (i = 0; i < IDX_CACHE_MAX && smallest_size; i++) {134if (hwtnl->index_cache[prim][i].buffer == NULL) {135smallest = i;136smallest_size = 0;137}138else if (hwtnl->index_cache[prim][i].gen_nr < smallest) {139smallest = i;140smallest_size = hwtnl->index_cache[prim][i].gen_nr;141}142}143144assert(smallest != IDX_CACHE_MAX);145146pipe_resource_reference(&hwtnl->index_cache[prim][smallest].buffer,147NULL);148149if (DBG)150debug_printf("%s discard smallest %d/%d\n", __FUNCTION__,151smallest, smallest_size);152153i = smallest;154}155156ret = generate_indices(hwtnl, gen_nr, gen_size, generate, out_buf);157if (ret != PIPE_OK)158goto done;159160hwtnl->index_cache[prim][i].generate = generate;161hwtnl->index_cache[prim][i].gen_nr = gen_nr;162pipe_resource_reference(&hwtnl->index_cache[prim][i].buffer, *out_buf);163164if (DBG)165debug_printf("%s cache %d/%d\n", __FUNCTION__,166i, hwtnl->index_cache[prim][i].gen_nr);167168done:169SVGA_STATS_TIME_POP(svga_sws(hwtnl->svga));170return ret;171}172173174static enum pipe_error175simple_draw_arrays(struct svga_hwtnl *hwtnl,176enum pipe_prim_type prim, unsigned start, unsigned count,177unsigned start_instance, unsigned instance_count,178ubyte vertices_per_patch)179{180SVGA3dPrimitiveRange range;181unsigned hw_prim;182unsigned hw_count;183184hw_prim = svga_translate_prim(prim, count, &hw_count, vertices_per_patch);185if (hw_count == 0)186return PIPE_ERROR_BAD_INPUT;187188range.primType = hw_prim;189range.primitiveCount = hw_count;190range.indexArray.surfaceId = SVGA3D_INVALID_ID;191range.indexArray.offset = 0;192range.indexArray.stride = 0;193range.indexWidth = 0;194range.indexBias = start;195196/* Min/max index should be calculated prior to applying bias, so we197* end up with min_index = 0, max_index = count - 1 and everybody198* looking at those numbers knows to adjust them by199* range.indexBias.200*/201return svga_hwtnl_prim(hwtnl, &range, count,2020, count - 1, NULL,203start_instance, instance_count,204NULL, NULL);205}206207208enum pipe_error209svga_hwtnl_draw_arrays(struct svga_hwtnl *hwtnl,210enum pipe_prim_type prim, unsigned start, unsigned count,211unsigned start_instance, unsigned instance_count,212ubyte vertices_per_patch)213{214enum pipe_prim_type gen_prim;215unsigned gen_size, gen_nr;216enum indices_mode gen_type;217u_generate_func gen_func;218enum pipe_error ret = PIPE_OK;219unsigned api_pv = hwtnl->api_pv;220struct svga_context *svga = hwtnl->svga;221222SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_HWTNLDRAWARRAYS);223224if (svga->curr.rast->templ.fill_front !=225svga->curr.rast->templ.fill_back) {226assert(hwtnl->api_fillmode == PIPE_POLYGON_MODE_FILL);227}228229if (svga->curr.rast->templ.flatshade &&230svga_fs_variant(svga->state.hw_draw.fs)->constant_color_output) {231/* The fragment color is a constant, not per-vertex so the whole232* primitive will be the same color (except for possible blending).233* We can ignore the current provoking vertex state and use whatever234* the hardware wants.235*/236api_pv = hwtnl->hw_pv;237238if (hwtnl->api_fillmode == PIPE_POLYGON_MODE_FILL) {239/* Do some simple primitive conversions to avoid index buffer240* generation below. Note that polygons and quads are not directly241* supported by the svga device. Also note, we can only do this242* for flat/constant-colored rendering because of provoking vertex.243*/244if (prim == PIPE_PRIM_POLYGON) {245prim = PIPE_PRIM_TRIANGLE_FAN;246}247else if (prim == PIPE_PRIM_QUADS && count == 4) {248prim = PIPE_PRIM_TRIANGLE_FAN;249}250}251}252253if (svga_need_unfilled_fallback(hwtnl, prim)) {254/* Convert unfilled polygons into points, lines, triangles */255gen_type = u_unfilled_generator(prim,256start,257count,258hwtnl->api_fillmode,259&gen_prim,260&gen_size, &gen_nr, &gen_func);261}262else {263/* Convert PIPE_PRIM_LINE_LOOP to PIPE_PRIM_LINESTRIP,264* convert PIPE_PRIM_POLYGON to PIPE_PRIM_TRIANGLE_FAN,265* etc, if needed (as determined by svga_hw_prims mask).266*/267gen_type = u_index_generator(svga_hw_prims,268prim,269start,270count,271api_pv,272hwtnl->hw_pv,273&gen_prim, &gen_size, &gen_nr, &gen_func);274}275276if (gen_type == U_GENERATE_LINEAR) {277ret = simple_draw_arrays(hwtnl, gen_prim, start, count,278start_instance, instance_count,279vertices_per_patch);280}281else {282struct pipe_resource *gen_buf = NULL;283284/* Need to draw as indexed primitive.285* Potentially need to run the gen func to build an index buffer.286*/287ret = retrieve_or_generate_indices(hwtnl,288prim,289gen_type,290gen_nr,291gen_size, gen_func, &gen_buf);292if (ret == PIPE_OK) {293pipe_debug_message(&svga->debug.callback, PERF_INFO,294"generating temporary index buffer for drawing %s",295u_prim_name(prim));296297ret = svga_hwtnl_simple_draw_range_elements(hwtnl,298gen_buf,299gen_size,300start,3010,302count - 1,303gen_prim, 0, gen_nr,304start_instance,305instance_count,306vertices_per_patch);307}308309if (gen_buf) {310pipe_resource_reference(&gen_buf, NULL);311}312}313314SVGA_STATS_TIME_POP(svga_sws(svga));315return ret;316}317318319