Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_draw_elements.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 "util/u_inlines.h"26#include "util/u_prim.h"27#include "util/u_upload_mgr.h"28#include "indices/u_indices.h"2930#include "svga_cmd.h"31#include "svga_draw.h"32#include "svga_draw_private.h"33#include "svga_resource_buffer.h"34#include "svga_winsys.h"35#include "svga_context.h"36#include "svga_hw_reg.h"373839/**40* Return a new index buffer which contains a translation of the original41* index buffer. An example of a translation is converting from QUAD42* primitives to TRIANGLE primitives. Each set of four indexes for a quad43* will be converted to six indices for two triangles.44*45* Before generating the new index buffer we'll check if the incoming46* buffer already has a translated buffer that can be re-used.47* This benefits demos like Cinebench R15 which has many48* glDrawElements(GL_QUADS) commands (we can't draw quads natively).49*50* \param offset offset in bytes to first index to translate in src buffer51* \param orig_prim original primitive type (like PIPE_PRIM_QUADS)52* \param gen_prim new/generated primitive type (like PIPE_PRIM_TRIANGLES)53* \param orig_nr number of indexes to translate in source buffer54* \param gen_nr number of indexes to write into new/dest buffer55* \param index_size bytes per index (2 or 4)56* \param translate the translation function from the u_translate module57* \param out_buf returns the new/translated index buffer58* \return error code to indicate success failure59*/60static enum pipe_error61translate_indices(struct svga_hwtnl *hwtnl,62const struct pipe_draw_info *info,63const struct pipe_draw_start_count_bias *draw,64enum pipe_prim_type gen_prim,65unsigned orig_nr, unsigned gen_nr,66unsigned gen_size,67u_translate_func translate,68struct pipe_resource **out_buf,69unsigned *out_offset)70{71struct pipe_context *pipe = &hwtnl->svga->pipe;72struct svga_screen *screen = svga_screen(pipe->screen);73struct svga_buffer *src_sbuf = NULL;74struct pipe_transfer *src_transfer = NULL;75struct pipe_transfer *dst_transfer = NULL;76const unsigned size = gen_size * gen_nr;77const unsigned offset = draw->start * info->index_size;78const void *src_map = NULL;79struct pipe_resource *dst = NULL;80void *dst_map = NULL;8182assert(gen_size == 2 || gen_size == 4);83if (!info->has_user_indices)84src_sbuf = svga_buffer(info->index.resource);8586/* If the draw_info provides us with a buffer rather than a87* user pointer, Check to see if we've already translated that buffer88*/89if (src_sbuf && !screen->debug.no_cache_index_buffers) {90/* Check if we already have a translated index buffer */91if (src_sbuf->translated_indices.buffer &&92src_sbuf->translated_indices.orig_prim == info->mode &&93src_sbuf->translated_indices.new_prim == gen_prim &&94src_sbuf->translated_indices.offset == offset &&95src_sbuf->translated_indices.count == orig_nr &&96src_sbuf->translated_indices.index_size == gen_size) {97pipe_resource_reference(out_buf, src_sbuf->translated_indices.buffer);98return PIPE_OK;99}100}101102/* Need to trim vertex count to make sure we don't write too much data103* to the dst buffer in the translate() call.104*/105u_trim_pipe_prim(gen_prim, &gen_nr);106107if (src_sbuf) {108/* If we have a source buffer, create a destination buffer in the109* hope that we can reuse the translated data later. If not,110* we'd probably be better off using the upload buffer.111*/112dst = pipe_buffer_create(pipe->screen,113PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_IMMUTABLE,114size);115if (!dst)116goto fail;117118dst_map = pipe_buffer_map(pipe, dst, PIPE_MAP_WRITE, &dst_transfer);119if (!dst_map)120goto fail;121122*out_offset = 0;123src_map = pipe_buffer_map(pipe, info->index.resource,124PIPE_MAP_READ |125PIPE_MAP_UNSYNCHRONIZED,126&src_transfer);127if (!src_map)128goto fail;129} else {130/* Allocate upload buffer space. Align to the index size. */131u_upload_alloc(pipe->stream_uploader, 0, size, gen_size,132out_offset, &dst, &dst_map);133if (!dst)134goto fail;135136src_map = info->index.user;137}138139translate((const char *) src_map + offset, 0, 0, gen_nr, 0, dst_map);140141if (src_transfer)142pipe_buffer_unmap(pipe, src_transfer);143144if (dst_transfer)145pipe_buffer_unmap(pipe, dst_transfer);146else147u_upload_unmap(pipe->stream_uploader);148149*out_buf = dst;150151if (src_sbuf && !screen->debug.no_cache_index_buffers) {152/* Save the new, translated index buffer in the hope we can use it153* again in the future.154*/155pipe_resource_reference(&src_sbuf->translated_indices.buffer, dst);156src_sbuf->translated_indices.orig_prim = info->mode;157src_sbuf->translated_indices.new_prim = gen_prim;158src_sbuf->translated_indices.offset = offset;159src_sbuf->translated_indices.count = orig_nr;160src_sbuf->translated_indices.index_size = gen_size;161}162163return PIPE_OK;164165fail:166if (src_transfer)167pipe_buffer_unmap(pipe, src_transfer);168169if (dst_transfer)170pipe_buffer_unmap(pipe, dst_transfer);171else if (dst_map)172u_upload_unmap(pipe->stream_uploader);173174if (dst)175pipe_resource_reference(&dst, NULL);176177return PIPE_ERROR_OUT_OF_MEMORY;178}179180181enum pipe_error182svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,183struct pipe_resource *index_buffer,184unsigned index_size, int index_bias,185unsigned min_index, unsigned max_index,186enum pipe_prim_type prim, unsigned start,187unsigned count,188unsigned start_instance,189unsigned instance_count,190ubyte vertices_per_patch)191{192SVGA3dPrimitiveRange range;193unsigned hw_prim;194unsigned hw_count;195unsigned index_offset = start * index_size;196197hw_prim = svga_translate_prim(prim, count, &hw_count, vertices_per_patch);198if (hw_count == 0)199return PIPE_OK; /* nothing to draw */200201range.primType = hw_prim;202range.primitiveCount = hw_count;203range.indexArray.offset = index_offset;204range.indexArray.stride = index_size;205range.indexWidth = index_size;206range.indexBias = index_bias;207208return svga_hwtnl_prim(hwtnl, &range, count,209min_index, max_index, index_buffer,210start_instance, instance_count,211NULL, NULL);212}213214215enum pipe_error216svga_hwtnl_draw_range_elements(struct svga_hwtnl *hwtnl,217const struct pipe_draw_info *info,218const struct pipe_draw_start_count_bias *draw,219unsigned count)220{221struct pipe_context *pipe = &hwtnl->svga->pipe;222enum pipe_prim_type gen_prim;223unsigned gen_size, gen_nr;224enum indices_mode gen_type;225u_translate_func gen_func;226enum pipe_error ret = PIPE_OK;227228SVGA_STATS_TIME_PUSH(svga_sws(hwtnl->svga),229SVGA_STATS_TIME_HWTNLDRAWELEMENTS);230231if (svga_need_unfilled_fallback(hwtnl, info->mode)) {232gen_type = u_unfilled_translator(info->mode,233info->index_size,234count,235hwtnl->api_fillmode,236&gen_prim,237&gen_size, &gen_nr, &gen_func);238}239else {240unsigned hw_pv;241242/* There is no geometry ordering with PATCH, so no need to243* consider provoking vertex mode for the translation.244* So use the same api_pv as the hw_pv.245*/246hw_pv = info->mode == PIPE_PRIM_PATCHES ? hwtnl->api_pv :247hwtnl->hw_pv;248gen_type = u_index_translator(svga_hw_prims,249info->mode,250info->index_size,251count,252hwtnl->api_pv,253hw_pv,254PR_DISABLE,255&gen_prim, &gen_size, &gen_nr, &gen_func);256}257258if ((gen_type == U_TRANSLATE_MEMCPY) && (info->index_size == gen_size)) {259/* No need for translation, just pass through to hardware:260*/261unsigned start_offset = draw->start * info->index_size;262struct pipe_resource *index_buffer = NULL;263unsigned index_offset;264265if (info->has_user_indices) {266u_upload_data(pipe->stream_uploader, 0, count * info->index_size,267info->index_size, (char *) info->index.user + start_offset,268&index_offset, &index_buffer);269u_upload_unmap(pipe->stream_uploader);270index_offset /= info->index_size;271} else {272pipe_resource_reference(&index_buffer, info->index.resource);273index_offset = draw->start;274}275276assert(index_buffer != NULL);277278ret = svga_hwtnl_simple_draw_range_elements(hwtnl, index_buffer,279info->index_size,280draw->index_bias,281info->index_bounds_valid ? info->min_index : 0,282info->index_bounds_valid ? info->max_index : ~0,283gen_prim, index_offset, count,284info->start_instance,285info->instance_count,286info->vertices_per_patch);287pipe_resource_reference(&index_buffer, NULL);288}289else {290struct pipe_resource *gen_buf = NULL;291unsigned gen_offset = 0;292293/* Need to allocate a new index buffer and run the translate294* func to populate it. Could potentially cache this translated295* index buffer with the original to avoid future296* re-translations. Not much point if we're just accelerating297* GL though, as index buffers are typically used only once298* there.299*/300ret = translate_indices(hwtnl, info, draw, gen_prim,301count, gen_nr, gen_size,302gen_func, &gen_buf, &gen_offset);303if (ret == PIPE_OK) {304gen_offset /= gen_size;305ret = svga_hwtnl_simple_draw_range_elements(hwtnl,306gen_buf,307gen_size,308draw->index_bias,309info->index_bounds_valid ? info->min_index : 0,310info->index_bounds_valid ? info->max_index : ~0,311gen_prim, gen_offset,312gen_nr,313info->start_instance,314info->instance_count,315info->vertices_per_patch);316}317318if (gen_buf) {319pipe_resource_reference(&gen_buf, NULL);320}321}322323SVGA_STATS_TIME_POP(svga_sws(hwtnl->svga));324return ret;325}326327328