Path: blob/21.2-virgl/src/gallium/drivers/svga/svga_resource_buffer.h
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#ifndef SVGA_BUFFER_H26#define SVGA_BUFFER_H272829#include "pipe/p_compiler.h"30#include "pipe/p_state.h"31#include "util/u_transfer.h"3233#include "svga_screen_cache.h"34#include "svga_screen.h"35#include "svga_cmd.h"36#include "svga_context.h"373839/**40* Maximum number of discontiguous ranges41*/42#define SVGA_BUFFER_MAX_RANGES 32434445struct svga_context;46struct svga_winsys_buffer;47struct svga_winsys_surface;4849struct svga_buffer_range50{51unsigned start;52unsigned end;53};5455struct svga_3d_update_gb_image;5657/**58* This structure describes the bind flags and cache key associated59* with the host surface.60*/61struct svga_buffer_surface62{63struct list_head list;64unsigned bind_flags;65struct svga_host_surface_cache_key key;66struct svga_winsys_surface *handle;67};6869/**70* SVGA pipe buffer.71*/72struct svga_buffer73{74struct pipe_resource b;7576/** This is a superset of b.b.bind */77unsigned bind_flags;7879/**80* Regular (non DMA'able) memory.81*82* Used for user buffers or for buffers which we know before hand that can83* never be used by the virtual hardware directly, such as constant buffers.84*/85void *swbuf;8687/**88* Whether swbuf was created by the user or not.89*/90boolean user;9192/**93* Whether swbuf is used for this buffer.94*/95boolean use_swbuf;9697/**98* Creation key for the host surface handle.99*100* This structure describes all the host surface characteristics so that it101* can be looked up in cache, since creating a host surface is often a slow102* operation.103*/104struct svga_host_surface_cache_key key;105106/**107* Host surface handle.108*109* This is a platform independent abstraction for host SID. We create when110* trying to bind.111*112* Only set for non-user buffers.113*/114struct svga_winsys_surface *handle;115116/**117* List of surfaces created for this buffer resource to support118* incompatible bind flags.119*/120struct list_head surfaces;121122/**123* Information about ongoing and past map operations.124*/125struct {126/**127* Number of concurrent mappings.128*/129unsigned count;130131/**132* Dirty ranges.133*134* Ranges that were touched by the application and need to be uploaded to135* the host.136*137* This information will be copied into dma.boxes, when emiting the138* SVGA3dCmdSurfaceDMA command.139*/140struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES];141unsigned num_ranges;142} map;143144/**145* Information about uploaded version of user buffers.146*/147struct {148struct pipe_resource *buffer;149150/**151* We combine multiple user buffers into the same hardware buffer. This152* is the relative offset within that buffer.153*/154unsigned offset;155156/**157* Range of user buffer that is uploaded in @buffer at @offset.158*/159unsigned start;160unsigned end;161} uploaded;162163/**164* DMA'ble memory.165*166* A piece of GMR memory, with the same size of the buffer. It is created167* when mapping the buffer, and will be used to upload vertex data to the168* host.169*170* Only set for non-user buffers.171*/172struct svga_winsys_buffer *hwbuf;173174/**175* Information about pending DMA uploads.176*177*/178struct {179/**180* Whether this buffer has an unfinished DMA upload command.181*182* If not set then the rest of the information is null.183*/184boolean pending;185186SVGA3dSurfaceDMAFlags flags;187188/**189* Pointer to the DMA copy box *inside* the command buffer.190*/191SVGA3dCopyBox *boxes;192193/**194* Pointer to the sequence of update commands195* *inside* the command buffer.196*/197struct svga_3d_update_gb_image *updates;198199/**200* Context that has the pending DMA to this buffer.201*/202struct svga_context *svga;203} dma;204205/**206* Linked list head, used to gather all buffers with pending dma uploads on207* a context. It is only valid if the dma.pending is set above.208*/209struct list_head head;210211unsigned size; /**< Approximate size in bytes */212213boolean dirty; /**< Need to do a readback before mapping? */214215/** In some cases we try to keep the results of the translate_indices()216* function from svga_draw_elements.c217*/218struct {219enum pipe_prim_type orig_prim, new_prim;220struct pipe_resource *buffer;221unsigned index_size;222unsigned offset; /**< first index */223unsigned count; /**< num indices */224} translated_indices;225};226227228static inline struct svga_buffer *229svga_buffer(struct pipe_resource *resource)230{231struct svga_buffer *buf = (struct svga_buffer *) resource;232assert(buf == NULL || buf->b.target == PIPE_BUFFER);233return buf;234}235236237/**238* Returns TRUE for user buffers. We may239* decide to use an alternate upload path for these buffers.240*/241static inline boolean242svga_buffer_is_user_buffer(struct pipe_resource *buffer)243{244if (buffer) {245return svga_buffer(buffer)->user;246} else {247return FALSE;248}249}250251/**252* Returns a pointer to a struct svga_winsys_screen given a253* struct svga_buffer.254*/255static inline struct svga_winsys_screen *256svga_buffer_winsys_screen(struct svga_buffer *sbuf)257{258return svga_screen(sbuf->b.screen)->sws;259}260261262/**263* Returns whether a buffer has hardware storage that is264* visible to the GPU.265*/266static inline boolean267svga_buffer_has_hw_storage(struct svga_buffer *sbuf)268{269if (svga_buffer_winsys_screen(sbuf)->have_gb_objects)270return (sbuf->handle ? TRUE : FALSE);271else272return (sbuf->hwbuf ? TRUE : FALSE);273}274275/**276* Map the hardware storage of a buffer.277* \param flags bitmask of PIPE_MAP_* flags278*/279static inline void *280svga_buffer_hw_storage_map(struct svga_context *svga,281struct svga_buffer *sbuf,282unsigned flags, boolean *retry)283{284struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);285286svga->hud.num_buffers_mapped++;287288if (sws->have_gb_objects) {289struct svga_winsys_context *swc = svga->swc;290boolean rebind;291void *map;292293if (swc->force_coherent) {294flags |= PIPE_MAP_PERSISTENT | PIPE_MAP_COHERENT;295}296map = swc->surface_map(swc, sbuf->handle, flags, retry, &rebind);297if (map && rebind) {298enum pipe_error ret;299300ret = SVGA3D_BindGBSurface(swc, sbuf->handle);301if (ret != PIPE_OK) {302svga_context_flush(svga, NULL);303ret = SVGA3D_BindGBSurface(swc, sbuf->handle);304assert(ret == PIPE_OK);305}306svga_context_flush(svga, NULL);307}308return map;309} else {310*retry = FALSE;311return sws->buffer_map(sws, sbuf->hwbuf, flags);312}313}314315/**316* Unmap the hardware storage of a buffer.317*/318static inline void319svga_buffer_hw_storage_unmap(struct svga_context *svga,320struct svga_buffer *sbuf)321{322struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);323324if (sws->have_gb_objects) {325struct svga_winsys_context *swc = svga->swc;326boolean rebind;327328swc->surface_unmap(swc, sbuf->handle, &rebind);329if (rebind) {330SVGA_RETRY(svga, SVGA3D_BindGBSurface(swc, sbuf->handle));331}332} else333sws->buffer_unmap(sws, sbuf->hwbuf);334}335336337struct pipe_resource *338svga_user_buffer_create(struct pipe_screen *screen,339void *ptr,340unsigned bytes,341unsigned usage);342343struct pipe_resource *344svga_buffer_create(struct pipe_screen *screen,345const struct pipe_resource *template);346347348349/**350* Get the host surface handle for this buffer.351*352* This will ensure the host surface is updated, issuing DMAs as needed.353*354* NOTE: This may insert new commands in the context, so it *must* be called355* before reserving command buffer space. And, in order to insert commands356* it may need to call svga_context_flush().357*/358struct svga_winsys_surface *359svga_buffer_handle(struct svga_context *svga,360struct pipe_resource *buf,361unsigned tobind_flags);362363void364svga_context_flush_buffers(struct svga_context *svga);365366struct svga_winsys_buffer *367svga_winsys_buffer_create(struct svga_context *svga,368unsigned alignment,369unsigned usage,370unsigned size);371372void373svga_buffer_transfer_flush_region(struct pipe_context *pipe,374struct pipe_transfer *transfer,375const struct pipe_box *box);376377void378svga_resource_destroy(struct pipe_screen *screen,379struct pipe_resource *buf);380381void *382svga_buffer_transfer_map(struct pipe_context *pipe,383struct pipe_resource *resource,384unsigned level,385unsigned usage,386const struct pipe_box *box,387struct pipe_transfer **ptransfer);388389void390svga_buffer_transfer_unmap(struct pipe_context *pipe,391struct pipe_transfer *transfer);392393#endif /* SVGA_BUFFER_H */394395396