Path: blob/21.2-virgl/src/freedreno/fdl/freedreno_layout.h
4561 views
/*1* Copyright © 2019 Google, Inc.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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef FREEDRENO_LAYOUT_H_24#define FREEDRENO_LAYOUT_H_2526#include <stdbool.h>27#include <stdint.h>2829#include "util/format/u_format.h"30#include "util/u_debug.h"31#include "util/u_math.h"3233/* Shared freedreno mipmap layout helper34*35* It does *not* attempt to track surface transitions, in particular36* about UBWC state. Possibly it should, but37* (a) I'm not sure if in all cases we can transparently do in-38* place transitions (ie. a5xx textures with interleaved39* meta and pixel data40* (b) Even if we can, we probably can't assume that we have41* figured out yet how to do in-place transition for every42* generation.43*/4445/* Texture Layout on a3xx:46* -----------------------47*48* Each mipmap-level contains all of it's layers (ie. all cubmap49* faces, all 1d/2d array elements, etc). The texture sampler is50* programmed with the start address of each mipmap level, and hw51* derives the layer offset within the level.52*53*54* Texture Layout on a4xx+:55* -----------------------56*57* For cubemap and 2d array, each layer contains all of it's mipmap58* levels (layer_first layout).59*60* 3d textures are laid out as on a3xx.61*62* In either case, the slice represents the per-miplevel information,63* but in layer_first layout it only includes the first layer, and64* an additional offset of (rsc->layer_size * layer) must be added.65*66*67* UBWC Color Compressions (a5xx+):68* -------------------------------69*70* Color compression is only supported for tiled layouts. In general71* the meta "flag" buffer (ie. what holds the compression state for72* each block) can be separate from the color data, except for textures73* on a5xx where it needs to be interleaved with layers/levels of a74* texture.75*/7677#define FDL_MAX_MIP_LEVELS 157879struct fdl_slice {80uint32_t offset; /* offset of first layer in slice */81uint32_t size0; /* size of first layer in slice */82};8384/* parameters for explicit (imported) layout */85struct fdl_explicit_layout {86uint32_t offset;87uint32_t pitch;88};8990/**91* Encapsulates the layout of a resource, including position of given 2d92* surface (layer, level) within. Or rather all the information needed93* to derive this.94*/95struct fdl_layout {96struct fdl_slice slices[FDL_MAX_MIP_LEVELS];97struct fdl_slice ubwc_slices[FDL_MAX_MIP_LEVELS];98uint32_t pitch0;99uint32_t ubwc_width0;100uint32_t layer_size;101uint32_t ubwc_layer_size; /* in bytes */102bool ubwc : 1;103bool layer_first : 1; /* see above description */104bool tile_all : 1;105106/* Note that for tiled textures, beyond a certain mipmap level (ie.107* when width is less than block size) things switch to linear. In108* general you should not directly look at fdl_layout::tile_mode,109* but instead use fdl_surface::tile_mode which will correctly take110* this into account.111*/112uint32_t tile_mode : 2;113/* Bytes per pixel (where a "pixel" is a single row of a block in the case114* of compression), including each sample in the case of multisample115* layouts.116*/117uint8_t cpp;118119/**120* Left shift necessary to multiply by cpp. Invalid for NPOT cpp, please121* use fdl_cpp_shift() to sanity check you aren't hitting that case.122*/123uint8_t cpp_shift;124125uint32_t width0, height0, depth0;126uint32_t nr_samples;127enum pipe_format format;128129uint32_t size; /* Size of the whole image, in bytes. */130uint32_t base_align; /* Alignment of the base address, in bytes. */131uint8_t pitchalign; /* log2(pitchalign) */132};133134static inline uint32_t135fdl_cpp_shift(const struct fdl_layout *layout)136{137assert(util_is_power_of_two_or_zero(layout->cpp));138return layout->cpp_shift;139}140141static inline uint32_t142fdl_pitch(const struct fdl_layout *layout, unsigned level)143{144return align(u_minify(layout->pitch0, level), 1 << layout->pitchalign);145}146147#define RGB_TILE_WIDTH_ALIGNMENT 64148#define RGB_TILE_HEIGHT_ALIGNMENT 16149#define UBWC_PLANE_SIZE_ALIGNMENT 4096150151static inline uint32_t152fdl_ubwc_pitch(const struct fdl_layout *layout, unsigned level)153{154if (!layout->ubwc)155return 0;156return align(u_minify(layout->ubwc_width0, level), RGB_TILE_WIDTH_ALIGNMENT);157}158159static inline uint32_t160fdl_layer_stride(const struct fdl_layout *layout, unsigned level)161{162if (layout->layer_first)163return layout->layer_size;164else165return layout->slices[level].size0;166}167168/* a2xx is special and needs PoT alignment for mipmaps: */169static inline uint32_t170fdl2_pitch(const struct fdl_layout *layout, unsigned level)171{172uint32_t pitch = fdl_pitch(layout, level);173if (level)174pitch = util_next_power_of_two(pitch);175return pitch;176}177178static inline uint32_t179fdl2_pitch_pixels(const struct fdl_layout *layout, unsigned level)180{181return fdl2_pitch(layout, level) >> fdl_cpp_shift(layout);182}183184static inline uint32_t185fdl_surface_offset(const struct fdl_layout *layout, unsigned level,186unsigned layer)187{188const struct fdl_slice *slice = &layout->slices[level];189return slice->offset + fdl_layer_stride(layout, level) * layer;190}191192static inline uint32_t193fdl_ubwc_offset(const struct fdl_layout *layout, unsigned level, unsigned layer)194{195const struct fdl_slice *slice = &layout->ubwc_slices[level];196return slice->offset + layer * layout->ubwc_layer_size;197}198199/* Minimum layout width to enable UBWC. */200#define FDL_MIN_UBWC_WIDTH 16201202static inline bool203fdl_level_linear(const struct fdl_layout *layout, int level)204{205if (layout->tile_all)206return false;207208unsigned w = u_minify(layout->width0, level);209if (w < FDL_MIN_UBWC_WIDTH)210return true;211212return false;213}214215static inline uint32_t216fdl_tile_mode(const struct fdl_layout *layout, int level)217{218if (layout->tile_mode && fdl_level_linear(layout, level))219return 0; /* linear */220else221return layout->tile_mode;222}223224static inline bool225fdl_ubwc_enabled(const struct fdl_layout *layout, int level)226{227return layout->ubwc;228}229230const char *fdl_tile_mode_desc(const struct fdl_layout *layout, int level);231232void fdl_layout_buffer(struct fdl_layout *layout, uint32_t size);233234void fdl5_layout(struct fdl_layout *layout, enum pipe_format format,235uint32_t nr_samples, uint32_t width0, uint32_t height0,236uint32_t depth0, uint32_t mip_levels, uint32_t array_size,237bool is_3d);238239bool fdl6_layout(struct fdl_layout *layout, enum pipe_format format,240uint32_t nr_samples, uint32_t width0, uint32_t height0,241uint32_t depth0, uint32_t mip_levels, uint32_t array_size,242bool is_3d, struct fdl_explicit_layout *plane_layout);243244static inline void245fdl_set_pitchalign(struct fdl_layout *layout, unsigned pitchalign)246{247uint32_t nblocksx = util_format_get_nblocksx(layout->format, layout->width0);248layout->pitchalign = pitchalign;249layout->pitch0 = align(nblocksx * layout->cpp, 1 << pitchalign);250}251252void fdl_dump_layout(struct fdl_layout *layout);253254void fdl6_get_ubwc_blockwidth(struct fdl_layout *layout, uint32_t *blockwidth,255uint32_t *blockheight);256257#endif /* FREEDRENO_LAYOUT_H_ */258259260