Path: blob/21.2-virgl/src/gallium/drivers/freedreno/freedreno_resource.h
4570 views
/*1* Copyright (C) 2012 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Rob Clark <[email protected]>24*/2526#ifndef FREEDRENO_RESOURCE_H_27#define FREEDRENO_RESOURCE_H_2829#include "util/list.h"30#include "util/simple_mtx.h"31#include "util/u_dump.h"32#include "util/u_range.h"33#include "util/u_transfer_helper.h"3435#include "freedreno/fdl/freedreno_layout.h"36#include "freedreno_batch.h"37#include "freedreno_util.h"3839#define PRSC_FMT \40"p: target=%s, format=%s, %ux%ux%u, " \41"array_size=%u, last_level=%u, " \42"nr_samples=%u, usage=%u, bind=%x, flags=%x"43#define PRSC_ARGS(p) \44(p), util_str_tex_target((p)->target, true), \45util_format_short_name((p)->format), (p)->width0, (p)->height0, \46(p)->depth0, (p)->array_size, (p)->last_level, (p)->nr_samples, \47(p)->usage, (p)->bind, (p)->flags4849enum fd_lrz_direction {50FD_LRZ_UNKNOWN,51/* Depth func less/less-than: */52FD_LRZ_LESS,53/* Depth func greater/greater-than: */54FD_LRZ_GREATER,55};5657/**58* State related to batch/resource tracking.59*60* With threaded_context we need to support replace_buffer_storage, in61* which case we can end up in transfer_map with tres->latest, but other62* pipe_context APIs using the original prsc pointer. This allows TC to63* not have to synchronize the front-end thread with the buffer storage64* replacement called on driver thread. But it complicates the batch/65* resource tracking.66*67* To handle this, we need to split the tracking out into it's own ref-68* counted structure, so as needed both "versions" of the resource can69* point to the same tracking.70*71* We could *almost* just push this down to fd_bo, except for a3xx/a4xx72* hw queries, where we don't know up-front the size to allocate for73* per-tile query results.74*/75struct fd_resource_tracking {76struct pipe_reference reference;7778/* bitmask of in-flight batches which reference this resource. Note79* that the batch doesn't hold reference to resources (but instead80* the fd_ringbuffer holds refs to the underlying fd_bo), but in case81* the resource is destroyed we need to clean up the batch's weak82* references to us.83*/84uint32_t batch_mask;8586/* reference to batch that writes this resource: */87struct fd_batch *write_batch;8889/* Set of batches whose batch-cache key references this resource.90* We need to track this to know which batch-cache entries to91* invalidate if, for example, the resource is invalidated or92* shadowed.93*/94uint32_t bc_batch_mask;95};9697void __fd_resource_tracking_destroy(struct fd_resource_tracking *track);9899static inline void100fd_resource_tracking_reference(struct fd_resource_tracking **ptr,101struct fd_resource_tracking *track)102{103struct fd_resource_tracking *old_track = *ptr;104105if (pipe_reference(&(*ptr)->reference, &track->reference)) {106assert(!old_track->write_batch);107free(old_track);108}109110*ptr = track;111}112113/**114* A resource (any buffer/texture/image/etc)115*/116struct fd_resource {117struct threaded_resource b;118struct fd_bo *bo; /* use fd_resource_set_bo() to write */119enum pipe_format internal_format;120struct fdl_layout layout;121122/* buffer range that has been initialized */123struct util_range valid_buffer_range;124bool valid;125struct renderonly_scanout *scanout;126127/* reference to the resource holding stencil data for a z32_s8 texture */128/* TODO rename to secondary or auxiliary? */129struct fd_resource *stencil;130131struct fd_resource_tracking *track;132133simple_mtx_t lock;134135/* bitmask of state this resource could potentially dirty when rebound,136* see rebind_resource()137*/138enum fd_dirty_3d_state dirty;139140/* Sequence # incremented each time bo changes: */141uint16_t seqno;142143/* Is this buffer a replacement created by threaded_context to avoid144* a stall in PIPE_MAP_DISCARD_WHOLE_RESOURCE|PIPE_MAP_WRITE case?145* If so, it no longer "owns" it's rsc->track, and so should not146* invalidate when the rsc is destroyed.147*/148bool is_replacement : 1;149150/* Uninitialized resources with UBWC format need their UBWC flag data151* cleared before writes, as the UBWC state is read and used during152* writes, so undefined UBWC flag data results in undefined results.153*/154bool needs_ubwc_clear : 1;155156/*157* LRZ158*159* TODO lrz width/height/pitch should probably also move to160* fdl_layout161*/162bool lrz_valid : 1;163enum fd_lrz_direction lrz_direction : 2;164uint16_t lrz_width; // for lrz clear, does this differ from lrz_pitch?165uint16_t lrz_height;166uint16_t lrz_pitch;167struct fd_bo *lrz;168};169170struct fd_memory_object {171struct pipe_memory_object b;172struct fd_bo *bo;173};174175static inline struct fd_resource *176fd_resource(struct pipe_resource *ptex)177{178return (struct fd_resource *)ptex;179}180181static inline const struct fd_resource *182fd_resource_const(const struct pipe_resource *ptex)183{184return (const struct fd_resource *)ptex;185}186187static inline struct fd_memory_object *188fd_memory_object(struct pipe_memory_object *pmemobj)189{190return (struct fd_memory_object *)pmemobj;191}192193static inline bool194pending(struct fd_resource *rsc, bool write)195{196/* if we have a pending GPU write, we are busy in any case: */197if (rsc->track->write_batch)198return true;199200/* if CPU wants to write, but we are pending a GPU read, we are busy: */201if (write && rsc->track->batch_mask)202return true;203204if (rsc->stencil && pending(rsc->stencil, write))205return true;206207return false;208}209210static inline bool211resource_busy(struct fd_resource *rsc, unsigned op)212{213return fd_bo_cpu_prep(rsc->bo, NULL, op | FD_BO_PREP_NOSYNC) != 0;214}215216int __fd_resource_wait(struct fd_context *ctx, struct fd_resource *rsc,217unsigned op, const char *func);218#define fd_resource_wait(ctx, rsc, op) \219__fd_resource_wait(ctx, rsc, op, __func__)220221static inline void222fd_resource_lock(struct fd_resource *rsc)223{224simple_mtx_lock(&rsc->lock);225}226227static inline void228fd_resource_unlock(struct fd_resource *rsc)229{230simple_mtx_unlock(&rsc->lock);231}232233static inline void234fd_resource_set_usage(struct pipe_resource *prsc, enum fd_dirty_3d_state usage)235{236if (!prsc)237return;238struct fd_resource *rsc = fd_resource(prsc);239/* Bits are only ever ORed in, and we expect many set_usage() per240* resource, so do the quick check outside of the lock.241*/242if (likely(rsc->dirty & usage))243return;244fd_resource_lock(rsc);245rsc->dirty |= usage;246fd_resource_unlock(rsc);247}248249static inline bool250has_depth(enum pipe_format format)251{252const struct util_format_description *desc = util_format_description(format);253return util_format_has_depth(desc);254}255256struct fd_transfer {257struct threaded_transfer b;258struct pipe_resource *staging_prsc;259struct pipe_box staging_box;260};261262static inline struct fd_transfer *263fd_transfer(struct pipe_transfer *ptrans)264{265return (struct fd_transfer *)ptrans;266}267268static inline struct fdl_slice *269fd_resource_slice(struct fd_resource *rsc, unsigned level)270{271assert(level <= rsc->b.b.last_level);272return &rsc->layout.slices[level];273}274275static inline uint32_t276fd_resource_layer_stride(struct fd_resource *rsc, unsigned level)277{278return fdl_layer_stride(&rsc->layout, level);279}280281/* get pitch (in bytes) for specified mipmap level */282static inline uint32_t283fd_resource_pitch(struct fd_resource *rsc, unsigned level)284{285if (is_a2xx(fd_screen(rsc->b.b.screen)))286return fdl2_pitch(&rsc->layout, level);287288return fdl_pitch(&rsc->layout, level);289}290291/* get offset for specified mipmap level and texture/array layer */292static inline uint32_t293fd_resource_offset(struct fd_resource *rsc, unsigned level, unsigned layer)294{295uint32_t offset = fdl_surface_offset(&rsc->layout, level, layer);296debug_assert(offset < fd_bo_size(rsc->bo));297return offset;298}299300static inline uint32_t301fd_resource_ubwc_offset(struct fd_resource *rsc, unsigned level, unsigned layer)302{303uint32_t offset = fdl_ubwc_offset(&rsc->layout, level, layer);304debug_assert(offset < fd_bo_size(rsc->bo));305return offset;306}307308/* This might be a5xx specific, but higher mipmap levels are always linear: */309static inline bool310fd_resource_level_linear(const struct pipe_resource *prsc, int level)311{312struct fd_screen *screen = fd_screen(prsc->screen);313debug_assert(!is_a3xx(screen));314315return fdl_level_linear(&fd_resource_const(prsc)->layout, level);316}317318static inline uint32_t319fd_resource_tile_mode(struct pipe_resource *prsc, int level)320{321return fdl_tile_mode(&fd_resource(prsc)->layout, level);322}323324static inline const char *325fd_resource_tile_mode_desc(const struct fd_resource *rsc, int level)326{327return fdl_tile_mode_desc(&rsc->layout, level);328}329330static inline bool331fd_resource_ubwc_enabled(struct fd_resource *rsc, int level)332{333return fdl_ubwc_enabled(&rsc->layout, level);334}335336/* access # of samples, with 0 normalized to 1 (which is what we care about337* most of the time)338*/339static inline unsigned340fd_resource_nr_samples(struct pipe_resource *prsc)341{342return MAX2(1, prsc->nr_samples);343}344345void fd_resource_screen_init(struct pipe_screen *pscreen);346void fd_resource_context_init(struct pipe_context *pctx);347348uint32_t fd_setup_slices(struct fd_resource *rsc);349void fd_resource_resize(struct pipe_resource *prsc, uint32_t sz);350void fd_replace_buffer_storage(struct pipe_context *ctx,351struct pipe_resource *dst,352struct pipe_resource *src,353unsigned num_rebinds,354uint32_t rebind_mask,355uint32_t delete_buffer_id) in_dt;356bool fd_resource_busy(struct pipe_screen *pscreen, struct pipe_resource *prsc,357unsigned usage);358359void fd_resource_uncompress(struct fd_context *ctx,360struct fd_resource *rsc,361bool linear) assert_dt;362void fd_resource_dump(struct fd_resource *rsc, const char *name);363364bool fd_render_condition_check(struct pipe_context *pctx) assert_dt;365366static inline bool367fd_batch_references_resource(struct fd_batch *batch, struct fd_resource *rsc)368{369return rsc->track->batch_mask & (1 << batch->idx);370}371372static inline void373fd_batch_write_prep(struct fd_batch *batch, struct fd_resource *rsc) assert_dt374{375if (unlikely(rsc->needs_ubwc_clear)) {376batch->ctx->clear_ubwc(batch, rsc);377rsc->needs_ubwc_clear = false;378}379}380381static inline void382fd_batch_resource_read(struct fd_batch *batch,383struct fd_resource *rsc) assert_dt384{385/* Fast path: if we hit this then we know we don't have anyone else386* writing to it (since both _write and _read flush other writers), and387* that we've already recursed for stencil.388*/389if (unlikely(!fd_batch_references_resource(batch, rsc)))390fd_batch_resource_read_slowpath(batch, rsc);391}392393#endif /* FREEDRENO_RESOURCE_H_ */394395396