Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pt_fetch.c
4565 views
/**************************************************************************1*2* Copyright 2008 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#include "util/u_memory.h"28#include "util/u_math.h"29#include "util/format/u_format.h"30#include "draw/draw_context.h"31#include "draw/draw_private.h"32#include "draw/draw_pt.h"33#include "translate/translate.h"34#include "translate/translate_cache.h"353637struct pt_fetch {38struct draw_context *draw;3940struct translate *translate;4142unsigned vertex_size;4344struct translate_cache *cache;45};464748/**49* Perform the fetch from API vertex elements & vertex buffers, to a50* contiguous set of float[4] attributes as required for the51* vertex_shader->run_linear() method.52*/53void54draw_pt_fetch_prepare(struct pt_fetch *fetch,55unsigned vs_input_count,56unsigned vertex_size,57unsigned instance_id_index)58{59struct draw_context *draw = fetch->draw;60unsigned nr_inputs;61unsigned i, nr = 0, ei = 0;62unsigned dst_offset = 0;63unsigned num_extra_inputs = 0;64struct translate_key key;6566fetch->vertex_size = vertex_size;6768/* Leave the clipmask/edgeflags/pad/vertex_id,69* clip[] and whatever else in the header untouched.70*/71dst_offset = offsetof(struct vertex_header, data);7273if (instance_id_index != ~0) {74num_extra_inputs++;75}7677assert(draw->pt.nr_vertex_elements + num_extra_inputs >= vs_input_count);7879nr_inputs = MIN2(vs_input_count, draw->pt.nr_vertex_elements + num_extra_inputs);8081for (i = 0; i < nr_inputs; i++) {82if (i == instance_id_index) {83key.element[nr].type = TRANSLATE_ELEMENT_INSTANCE_ID;84key.element[nr].input_format = PIPE_FORMAT_R32_USCALED;85key.element[nr].output_format = PIPE_FORMAT_R32_USCALED;86key.element[nr].output_offset = dst_offset;8788dst_offset += sizeof(uint);89} else if (util_format_is_pure_sint(draw->pt.vertex_element[i].src_format)) {90key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;91key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;92key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;93key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;94key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;95key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_SINT;96key.element[nr].output_offset = dst_offset;9798ei++;99dst_offset += 4 * sizeof(int);100} else if (util_format_is_pure_uint(draw->pt.vertex_element[i].src_format)) {101key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;102key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;103key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;104key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;105key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;106key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_UINT;107key.element[nr].output_offset = dst_offset;108109ei++;110dst_offset += 4 * sizeof(unsigned);111} else {112key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;113key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;114key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;115key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;116key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;117key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;118key.element[nr].output_offset = dst_offset;119120ei++;121dst_offset += 4 * sizeof(float);122}123124nr++;125}126127assert(dst_offset <= vertex_size);128129key.nr_elements = nr;130key.output_stride = vertex_size;131132if (!fetch->translate ||133translate_key_compare(&fetch->translate->key, &key) != 0)134{135translate_key_sanitize(&key);136fetch->translate = translate_cache_find(fetch->cache, &key);137}138}139140141void142draw_pt_fetch_run(struct pt_fetch *fetch,143const unsigned *elts,144unsigned count,145char *verts)146{147struct draw_context *draw = fetch->draw;148struct translate *translate = fetch->translate;149unsigned i;150151for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {152translate->set_buffer(translate,153i,154((char *)draw->pt.user.vbuffer[i].map +155draw->pt.vertex_buffer[i].buffer_offset),156draw->pt.vertex_buffer[i].stride,157draw->pt.max_index);158}159160translate->run_elts( translate,161elts,162count,163draw->start_instance,164draw->instance_id,165verts );166}167168169void170draw_pt_fetch_run_linear(struct pt_fetch *fetch,171unsigned start,172unsigned count,173char *verts)174{175struct draw_context *draw = fetch->draw;176struct translate *translate = fetch->translate;177unsigned i;178179for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {180translate->set_buffer(translate,181i,182((char *)draw->pt.user.vbuffer[i].map +183draw->pt.vertex_buffer[i].buffer_offset),184draw->pt.vertex_buffer[i].stride,185draw->pt.max_index);186}187188translate->run( translate,189start,190count,191draw->start_instance,192draw->instance_id,193verts );194}195196197struct pt_fetch *198draw_pt_fetch_create(struct draw_context *draw)199{200struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);201if (!fetch)202return NULL;203204fetch->draw = draw;205fetch->cache = translate_cache_create();206if (!fetch->cache) {207FREE(fetch);208return NULL;209}210211return fetch;212}213214215void216draw_pt_fetch_destroy(struct pt_fetch *fetch)217{218if (fetch->cache)219translate_cache_destroy(fetch->cache);220221FREE(fetch);222}223224225