Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_vbuf.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/**28* \file29* Vertex buffer drawing stage.30*31* \author Jose Fonseca <[email protected]>32* \author Keith Whitwell <[email protected]>33*/343536#include "util/u_debug.h"37#include "util/u_math.h"38#include "util/u_memory.h"39#include "draw_vbuf.h"40#include "draw_private.h"41#include "draw_vertex.h"42#include "draw_pipe.h"43#include "translate/translate.h"44#include "translate/translate_cache.h"454647/**48* Vertex buffer emit stage.49*/50struct vbuf_stage {51struct draw_stage stage; /**< This must be first (base class) */5253struct vbuf_render *render;5455const struct vertex_info *vinfo;5657/** Vertex size in bytes */58unsigned vertex_size;5960struct translate *translate;6162/* FIXME: we have no guarantee that 'unsigned' is 32bit */6364/** Vertices in hardware format */65unsigned *vertices;66unsigned *vertex_ptr;67unsigned max_vertices;68unsigned nr_vertices;6970/** Indices */71ushort *indices;72unsigned max_indices;73unsigned nr_indices;7475/* Cache point size somewhere its address won't change:76*/77float point_size;78float zero4[4];7980struct translate_cache *cache;81};828384/**85* Basically a cast wrapper.86*/87static inline struct vbuf_stage *88vbuf_stage(struct draw_stage *stage)89{90assert(stage);91return (struct vbuf_stage *)stage;92}939495static void vbuf_flush_vertices(struct vbuf_stage *vbuf);96static void vbuf_alloc_vertices(struct vbuf_stage *vbuf);979899static inline void100check_space(struct vbuf_stage *vbuf, unsigned nr)101{102if (vbuf->nr_vertices + nr > vbuf->max_vertices ||103vbuf->nr_indices + nr > vbuf->max_indices) {104vbuf_flush_vertices(vbuf);105vbuf_alloc_vertices(vbuf);106}107}108109110/**111* Extract the needed fields from post-transformed vertex and emit112* a hardware(driver) vertex.113* Recall that the vertices are constructed by the 'draw' module and114* have a couple of slots at the beginning (1-dword header, 4-dword115* clip pos) that we ignore here. We only use the vertex->data[] fields.116*/117static inline ushort118emit_vertex(struct vbuf_stage *vbuf, struct vertex_header *vertex)119{120if (vertex->vertex_id == UNDEFINED_VERTEX_ID && vbuf->vertex_ptr) {121/* Hmm - vertices are emitted one at a time - better make sure122* set_buffer is efficient. Consider a special one-shot mode for123* translate.124*/125/* Note: we really do want data[0] here, not data[pos]:126*/127vbuf->translate->set_buffer(vbuf->translate, 0, vertex->data[0], 0, ~0);128vbuf->translate->run(vbuf->translate, 0, 1, 0, 0, vbuf->vertex_ptr);129130if (0) draw_dump_emitted_vertex(vbuf->vinfo, (uint8_t *)vbuf->vertex_ptr);131132vbuf->vertex_ptr += vbuf->vertex_size/4;133vertex->vertex_id = vbuf->nr_vertices++;134}135136return (ushort)vertex->vertex_id;137}138139140static void141vbuf_tri(struct draw_stage *stage, struct prim_header *prim)142{143struct vbuf_stage *vbuf = vbuf_stage(stage);144unsigned i;145146check_space(vbuf, 3);147148for (i = 0; i < 3; i++) {149vbuf->indices[vbuf->nr_indices++] = emit_vertex(vbuf, prim->v[i]);150}151}152153154static void155vbuf_line(struct draw_stage *stage, struct prim_header *prim)156{157struct vbuf_stage *vbuf = vbuf_stage(stage);158unsigned i;159160check_space(vbuf, 2);161162for (i = 0; i < 2; i++) {163vbuf->indices[vbuf->nr_indices++] = emit_vertex(vbuf, prim->v[i]);164}165}166167168static void169vbuf_point(struct draw_stage *stage, struct prim_header *prim)170{171struct vbuf_stage *vbuf = vbuf_stage(stage);172173check_space(vbuf, 1);174175vbuf->indices[vbuf->nr_indices++] = emit_vertex(vbuf, prim->v[0]);176}177178179/**180* Set the prim type for subsequent vertices.181* This may result in a new vertex size. The existing vbuffer (if any)182* will be flushed if needed and a new one allocated.183*/184static void185vbuf_start_prim(struct vbuf_stage *vbuf, uint prim)186{187struct translate_key hw_key;188unsigned dst_offset;189unsigned i;190const struct vertex_info *vinfo;191192vbuf->render->set_primitive(vbuf->render, prim);193if (vbuf->render->set_view_index)194vbuf->render->set_view_index(vbuf->render, vbuf->stage.draw->pt.user.viewid);195196/* Must do this after set_primitive() above:197*198* XXX: need some state managment to track when this needs to be199* recalculated. The driver should tell us whether there was a200* state change.201*/202vbuf->vinfo = vbuf->render->get_vertex_info(vbuf->render);203vinfo = vbuf->vinfo;204vbuf->vertex_size = vinfo->size * sizeof(float);205206/* Translate from pipeline vertices to hw vertices.207*/208dst_offset = 0;209210for (i = 0; i < vinfo->num_attribs; i++) {211unsigned emit_sz = 0;212unsigned src_buffer = 0;213enum pipe_format output_format;214unsigned src_offset = (vinfo->attrib[i].src_index * 4 * sizeof(float));215216output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit);217emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit);218219/* doesn't handle EMIT_OMIT */220assert(emit_sz != 0);221222if (vinfo->attrib[i].emit == EMIT_1F_PSIZE) {223src_buffer = 1;224src_offset = 0;225}226else if (vinfo->attrib[i].src_index == DRAW_ATTR_NONEXIST) {227/* elements which don't exist will get assigned zeros */228src_buffer = 2;229src_offset = 0;230}231232hw_key.element[i].type = TRANSLATE_ELEMENT_NORMAL;233hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;234hw_key.element[i].input_buffer = src_buffer;235hw_key.element[i].input_offset = src_offset;236hw_key.element[i].instance_divisor = 0;237hw_key.element[i].output_format = output_format;238hw_key.element[i].output_offset = dst_offset;239240dst_offset += emit_sz;241}242243hw_key.nr_elements = vinfo->num_attribs;244hw_key.output_stride = vbuf->vertex_size;245246/* Don't bother with caching at this stage:247*/248if (!vbuf->translate ||249translate_key_compare(&vbuf->translate->key, &hw_key) != 0) {250translate_key_sanitize(&hw_key);251vbuf->translate = translate_cache_find(vbuf->cache, &hw_key);252253vbuf->translate->set_buffer(vbuf->translate, 1, &vbuf->point_size, 0, ~0);254vbuf->translate->set_buffer(vbuf->translate, 2, &vbuf->zero4[0], 0, ~0);255}256257vbuf->point_size = vbuf->stage.draw->rasterizer->point_size;258259/* Allocate new buffer?260*/261assert(vbuf->vertices == NULL);262vbuf_alloc_vertices(vbuf);263}264265266static void267vbuf_first_tri(struct draw_stage *stage, struct prim_header *prim)268{269struct vbuf_stage *vbuf = vbuf_stage(stage);270271vbuf_flush_vertices(vbuf);272vbuf_start_prim(vbuf, PIPE_PRIM_TRIANGLES);273stage->tri = vbuf_tri;274stage->tri(stage, prim);275}276277278static void279vbuf_first_line(struct draw_stage *stage, struct prim_header *prim)280{281struct vbuf_stage *vbuf = vbuf_stage(stage);282283vbuf_flush_vertices(vbuf);284vbuf_start_prim(vbuf, PIPE_PRIM_LINES);285stage->line = vbuf_line;286stage->line(stage, prim);287}288289290static void291vbuf_first_point(struct draw_stage *stage, struct prim_header *prim)292{293struct vbuf_stage *vbuf = vbuf_stage(stage);294295vbuf_flush_vertices(vbuf);296vbuf_start_prim(vbuf, PIPE_PRIM_POINTS);297stage->point = vbuf_point;298stage->point(stage, prim);299}300301302303/**304* Flush existing vertex buffer and allocate a new one.305*/306static void307vbuf_flush_vertices(struct vbuf_stage *vbuf)308{309if (vbuf->vertices) {310vbuf->render->unmap_vertices(vbuf->render, 0, vbuf->nr_vertices - 1);311312if (vbuf->nr_indices) {313vbuf->render->draw_elements(vbuf->render,314vbuf->indices,315vbuf->nr_indices);316317vbuf->nr_indices = 0;318}319320/* Reset temporary vertices ids */321if (vbuf->nr_vertices)322draw_reset_vertex_ids(vbuf->stage.draw);323324/* Free the vertex buffer */325vbuf->render->release_vertices(vbuf->render);326327vbuf->max_vertices = vbuf->nr_vertices = 0;328vbuf->vertex_ptr = vbuf->vertices = NULL;329}330331/* Reset point/line/tri function pointers.332* If (for example) we transition from points to tris and back to points333* again, we need to call the vbuf_first_point() function again to flush334* the triangles before drawing more points. This can happen when drawing335* with front polygon mode = filled and back polygon mode = line or point.336*/337vbuf->stage.point = vbuf_first_point;338vbuf->stage.line = vbuf_first_line;339vbuf->stage.tri = vbuf_first_tri;340}341342343static void344vbuf_alloc_vertices(struct vbuf_stage *vbuf)345{346if (vbuf->vertex_ptr) {347assert(!vbuf->nr_indices);348assert(!vbuf->vertices);349}350351/* Allocate a new vertex buffer */352vbuf->max_vertices =353vbuf->render->max_vertex_buffer_bytes / vbuf->vertex_size;354355if (vbuf->max_vertices >= UNDEFINED_VERTEX_ID)356vbuf->max_vertices = UNDEFINED_VERTEX_ID - 1;357358/* Must always succeed -- driver gives us a359* 'max_vertex_buffer_bytes' which it guarantees it can allocate,360* and it will flush itself if necessary to do so. If this does361* fail, we are basically without usable hardware.362*/363vbuf->render->allocate_vertices(vbuf->render,364(ushort) vbuf->vertex_size,365(ushort) vbuf->max_vertices);366367vbuf->vertices = (uint *) vbuf->render->map_vertices(vbuf->render);368369vbuf->vertex_ptr = vbuf->vertices;370}371372373static void374vbuf_flush(struct draw_stage *stage, unsigned flags)375{376struct vbuf_stage *vbuf = vbuf_stage(stage);377378vbuf_flush_vertices(vbuf);379}380381382static void383vbuf_reset_stipple_counter(struct draw_stage *stage)384{385/* XXX: Need to do something here for hardware with linestipple.386*/387(void) stage;388}389390391static void392vbuf_destroy(struct draw_stage *stage)393{394struct vbuf_stage *vbuf = vbuf_stage(stage);395396if (vbuf->indices)397align_free(vbuf->indices);398399if (vbuf->render)400vbuf->render->destroy(vbuf->render);401402if (vbuf->cache)403translate_cache_destroy(vbuf->cache);404405FREE(stage);406}407408409/**410* Create a new primitive vbuf/render stage.411*/412struct draw_stage *413draw_vbuf_stage(struct draw_context *draw, struct vbuf_render *render)414{415struct vbuf_stage *vbuf = CALLOC_STRUCT(vbuf_stage);416if (!vbuf)417goto fail;418419vbuf->stage.draw = draw;420vbuf->stage.name = "vbuf";421vbuf->stage.point = vbuf_first_point;422vbuf->stage.line = vbuf_first_line;423vbuf->stage.tri = vbuf_first_tri;424vbuf->stage.flush = vbuf_flush;425vbuf->stage.reset_stipple_counter = vbuf_reset_stipple_counter;426vbuf->stage.destroy = vbuf_destroy;427428vbuf->render = render;429vbuf->max_indices = MIN2(render->max_indices, UNDEFINED_VERTEX_ID-1);430431vbuf->indices = (ushort *) align_malloc(vbuf->max_indices *432sizeof(vbuf->indices[0]),43316);434if (!vbuf->indices)435goto fail;436437vbuf->cache = translate_cache_create();438if (!vbuf->cache)439goto fail;440441vbuf->vertices = NULL;442vbuf->vertex_ptr = vbuf->vertices;443444vbuf->zero4[0] = vbuf->zero4[1] = vbuf->zero4[2] = vbuf->zero4[3] = 0.0f;445446return &vbuf->stage;447448fail:449if (vbuf)450vbuf_destroy(&vbuf->stage);451452return NULL;453}454455456