Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a6xx/fd6_texture.c
4574 views
/*1* Copyright (C) 2016 Rob Clark <[email protected]>2* Copyright © 2018 Google, Inc.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23* Authors:24* Rob Clark <[email protected]>25*/2627#include "pipe/p_state.h"28#include "util/format/u_format.h"29#include "util/hash_table.h"30#include "util/u_inlines.h"31#include "util/u_memory.h"32#include "util/u_string.h"3334#include "fd6_emit.h"35#include "fd6_format.h"36#include "fd6_resource.h"37#include "fd6_texture.h"3839static void40remove_tex_entry(struct fd6_context *fd6_ctx, struct hash_entry *entry)41{42struct fd6_texture_state *tex = entry->data;43_mesa_hash_table_remove(fd6_ctx->tex_cache, entry);44fd6_texture_state_reference(&tex, NULL);45}4647static enum a6xx_tex_clamp48tex_clamp(unsigned wrap, bool *needs_border)49{50switch (wrap) {51case PIPE_TEX_WRAP_REPEAT:52return A6XX_TEX_REPEAT;53case PIPE_TEX_WRAP_CLAMP_TO_EDGE:54return A6XX_TEX_CLAMP_TO_EDGE;55case PIPE_TEX_WRAP_CLAMP_TO_BORDER:56*needs_border = true;57return A6XX_TEX_CLAMP_TO_BORDER;58case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:59/* only works for PoT.. need to emulate otherwise! */60return A6XX_TEX_MIRROR_CLAMP;61case PIPE_TEX_WRAP_MIRROR_REPEAT:62return A6XX_TEX_MIRROR_REPEAT;63case PIPE_TEX_WRAP_MIRROR_CLAMP:64case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:65/* these two we could perhaps emulate, but we currently66* just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP67*/68default:69DBG("invalid wrap: %u", wrap);70return 0;71}72}7374static enum a6xx_tex_filter75tex_filter(unsigned filter, bool aniso)76{77switch (filter) {78case PIPE_TEX_FILTER_NEAREST:79return A6XX_TEX_NEAREST;80case PIPE_TEX_FILTER_LINEAR:81return aniso ? A6XX_TEX_ANISO : A6XX_TEX_LINEAR;82default:83DBG("invalid filter: %u", filter);84return 0;85}86}8788static void *89fd6_sampler_state_create(struct pipe_context *pctx,90const struct pipe_sampler_state *cso)91{92struct fd6_sampler_stateobj *so = CALLOC_STRUCT(fd6_sampler_stateobj);93unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));94bool miplinear = false;9596if (!so)97return NULL;9899so->base = *cso;100so->seqno = ++fd6_context(fd_context(pctx))->tex_seqno;101102if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)103miplinear = true;104105so->needs_border = false;106so->texsamp0 =107COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |108A6XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |109A6XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |110A6XX_TEX_SAMP_0_ANISO(aniso) |111A6XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |112A6XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |113A6XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));114115so->texsamp1 =116COND(cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE,117A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |118COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |119COND(!cso->normalized_coords, A6XX_TEX_SAMP_1_UNNORM_COORDS);120121so->texsamp0 |= A6XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);122so->texsamp1 |= A6XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |123A6XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);124125if (cso->compare_mode)126so->texsamp1 |=127A6XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */128129return so;130}131132static void133fd6_sampler_state_delete(struct pipe_context *pctx, void *hwcso)134{135struct fd_context *ctx = fd_context(pctx);136struct fd6_context *fd6_ctx = fd6_context(ctx);137struct fd6_sampler_stateobj *samp = hwcso;138139fd_screen_lock(ctx->screen);140141hash_table_foreach (fd6_ctx->tex_cache, entry) {142struct fd6_texture_state *state = entry->data;143144for (unsigned i = 0; i < ARRAY_SIZE(state->key.samp); i++) {145if (samp->seqno == state->key.samp[i].seqno) {146remove_tex_entry(fd6_ctx, entry);147break;148}149}150}151152fd_screen_unlock(ctx->screen);153154free(hwcso);155}156157static struct pipe_sampler_view *158fd6_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,159const struct pipe_sampler_view *cso)160{161struct fd6_pipe_sampler_view *so = CALLOC_STRUCT(fd6_pipe_sampler_view);162163if (!so)164return NULL;165166so->base = *cso;167pipe_reference(NULL, &prsc->reference);168so->base.texture = prsc;169so->base.reference.count = 1;170so->base.context = pctx;171so->needs_validate = true;172173return &so->base;174}175176static void177fd6_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,178unsigned start, unsigned nr,179unsigned unbind_num_trailing_slots,180struct pipe_sampler_view **views) in_dt181{182struct fd_context *ctx = fd_context(pctx);183184fd_set_sampler_views(pctx, shader, start, nr, unbind_num_trailing_slots,185views);186187if (!views)188return;189190for (unsigned i = 0; i < nr; i++) {191struct fd6_pipe_sampler_view *so = fd6_pipe_sampler_view(views[i]);192193if (!(so && so->needs_validate))194continue;195196struct fd_resource *rsc = fd_resource(so->base.texture);197198fd6_validate_format(ctx, rsc, so->base.format);199fd6_sampler_view_update(ctx, so);200201so->needs_validate = false;202}203}204205void206fd6_sampler_view_update(struct fd_context *ctx,207struct fd6_pipe_sampler_view *so)208{209const struct pipe_sampler_view *cso = &so->base;210struct pipe_resource *prsc = cso->texture;211struct fd_resource *rsc = fd_resource(prsc);212enum pipe_format format = cso->format;213bool ubwc_enabled = false;214unsigned lvl, layers = 0;215216fd6_validate_format(ctx, rsc, cso->format);217218if (format == PIPE_FORMAT_X32_S8X24_UINT) {219rsc = rsc->stencil;220format = rsc->b.b.format;221}222223so->seqno = ++fd6_context(ctx)->tex_seqno;224so->ptr1 = rsc;225so->rsc_seqno = rsc->seqno;226227if (cso->target == PIPE_BUFFER) {228unsigned elements = cso->u.buf.size / util_format_get_blocksize(format);229230lvl = 0;231so->texconst1 = A6XX_TEX_CONST_1_WIDTH(elements & MASK(15)) |232A6XX_TEX_CONST_1_HEIGHT(elements >> 15);233so->texconst2 = A6XX_TEX_CONST_2_UNK4 | A6XX_TEX_CONST_2_UNK31;234so->offset1 = cso->u.buf.offset;235} else {236unsigned miplevels;237238lvl = fd_sampler_first_level(cso);239miplevels = fd_sampler_last_level(cso) - lvl;240layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1;241242so->texconst0 |= A6XX_TEX_CONST_0_MIPLVLS(miplevels);243so->texconst1 = A6XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |244A6XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));245so->texconst2 = A6XX_TEX_CONST_2_PITCHALIGN(rsc->layout.pitchalign - 6) |246A6XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));247248ubwc_enabled = fd_resource_ubwc_enabled(rsc, lvl);249250if (rsc->b.b.format == PIPE_FORMAT_R8_G8B8_420_UNORM) {251struct fd_resource *next = fd_resource(rsc->b.b.next);252253/* In case of biplanar R8_G8B8, the UBWC metadata address in254* dwords 7 and 8, is instead the pointer to the second plane.255*/256so->ptr2 = next;257so->texconst6 =258A6XX_TEX_CONST_6_PLANE_PITCH(fd_resource_pitch(next, lvl));259260if (ubwc_enabled) {261/* Further, if using UBWC with R8_G8B8, we only point to the262* UBWC header and the color data is expected to follow immediately.263*/264so->offset1 =265fd_resource_ubwc_offset(rsc, lvl, cso->u.tex.first_layer);266so->offset2 =267fd_resource_ubwc_offset(next, lvl, cso->u.tex.first_layer);268} else {269so->offset1 = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);270so->offset2 = fd_resource_offset(next, lvl, cso->u.tex.first_layer);271}272} else {273so->offset1 = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);274if (ubwc_enabled) {275so->ptr2 = rsc;276so->offset2 =277fd_resource_ubwc_offset(rsc, lvl, cso->u.tex.first_layer);278}279}280}281282so->texconst0 |=283fd6_tex_const_0(prsc, lvl, cso->format, cso->swizzle_r, cso->swizzle_g,284cso->swizzle_b, cso->swizzle_a);285286so->texconst2 |= A6XX_TEX_CONST_2_TYPE(fd6_tex_type(cso->target));287288switch (cso->target) {289case PIPE_TEXTURE_RECT:290case PIPE_TEXTURE_1D:291case PIPE_TEXTURE_2D:292so->texconst3 = A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);293so->texconst5 = A6XX_TEX_CONST_5_DEPTH(1);294break;295case PIPE_TEXTURE_1D_ARRAY:296case PIPE_TEXTURE_2D_ARRAY:297so->texconst3 = A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);298so->texconst5 = A6XX_TEX_CONST_5_DEPTH(layers);299break;300case PIPE_TEXTURE_CUBE:301case PIPE_TEXTURE_CUBE_ARRAY:302so->texconst3 = A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);303so->texconst5 = A6XX_TEX_CONST_5_DEPTH(layers / 6);304break;305case PIPE_TEXTURE_3D:306so->texconst3 =307A6XX_TEX_CONST_3_MIN_LAYERSZ(308fd_resource_slice(rsc, prsc->last_level)->size0) |309A6XX_TEX_CONST_3_ARRAY_PITCH(fd_resource_slice(rsc, lvl)->size0);310so->texconst5 = A6XX_TEX_CONST_5_DEPTH(u_minify(prsc->depth0, lvl));311break;312default:313break;314}315316if (rsc->layout.tile_all)317so->texconst3 |= A6XX_TEX_CONST_3_TILE_ALL;318319if (ubwc_enabled) {320uint32_t block_width, block_height;321fdl6_get_ubwc_blockwidth(&rsc->layout, &block_width, &block_height);322323so->texconst3 |= A6XX_TEX_CONST_3_FLAG;324so->texconst9 |= A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(325rsc->layout.ubwc_layer_size >> 2);326so->texconst10 |=327A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(328fdl_ubwc_pitch(&rsc->layout, lvl)) |329A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(330DIV_ROUND_UP(u_minify(prsc->width0, lvl), block_width))) |331A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(332DIV_ROUND_UP(u_minify(prsc->height0, lvl), block_height)));333}334}335336/* NOTE this can be called in either driver thread or frontend thread337* depending on where the last unref comes from338*/339static void340fd6_sampler_view_destroy(struct pipe_context *pctx,341struct pipe_sampler_view *_view)342{343struct fd_context *ctx = fd_context(pctx);344struct fd6_context *fd6_ctx = fd6_context(ctx);345struct fd6_pipe_sampler_view *view = fd6_pipe_sampler_view(_view);346347fd_screen_lock(ctx->screen);348349hash_table_foreach (fd6_ctx->tex_cache, entry) {350struct fd6_texture_state *state = entry->data;351352for (unsigned i = 0; i < ARRAY_SIZE(state->key.view); i++) {353if (view->seqno == state->key.view[i].seqno) {354remove_tex_entry(fd6_ctx, entry);355break;356}357}358}359360fd_screen_unlock(ctx->screen);361362pipe_resource_reference(&view->base.texture, NULL);363364free(view);365}366367static uint32_t368key_hash(const void *_key)369{370const struct fd6_texture_key *key = _key;371return XXH32(key, sizeof(*key), 0);372}373374static bool375key_equals(const void *_a, const void *_b)376{377const struct fd6_texture_key *a = _a;378const struct fd6_texture_key *b = _b;379return memcmp(a, b, sizeof(struct fd6_texture_key)) == 0;380}381382struct fd6_texture_state *383fd6_texture_state(struct fd_context *ctx, enum pipe_shader_type type,384struct fd_texture_stateobj *tex)385{386struct fd6_context *fd6_ctx = fd6_context(ctx);387struct fd6_texture_state *state = NULL;388struct fd6_texture_key key;389bool needs_border = false;390391memset(&key, 0, sizeof(key));392393for (unsigned i = 0; i < tex->num_textures; i++) {394if (!tex->textures[i])395continue;396397struct fd6_pipe_sampler_view *view =398fd6_pipe_sampler_view(tex->textures[i]);399400/* NOTE that if the backing rsc was uncompressed between the401* time that the CSO was originally created and now, the rsc402* seqno would have changed, so we don't have to worry about403* getting a bogus cache hit.404*/405key.view[i].rsc_seqno = fd_resource(view->base.texture)->seqno;406key.view[i].seqno = view->seqno;407}408409for (unsigned i = 0; i < tex->num_samplers; i++) {410if (!tex->samplers[i])411continue;412413struct fd6_sampler_stateobj *sampler =414fd6_sampler_stateobj(tex->samplers[i]);415416key.samp[i].seqno = sampler->seqno;417418needs_border |= sampler->needs_border;419}420421key.type = type;422key.bcolor_offset = fd6_border_color_offset(ctx, type, tex);423424uint32_t hash = key_hash(&key);425fd_screen_lock(ctx->screen);426struct hash_entry *entry =427_mesa_hash_table_search_pre_hashed(fd6_ctx->tex_cache, hash, &key);428429if (entry) {430fd6_texture_state_reference(&state, entry->data);431goto out_unlock;432}433434state = CALLOC_STRUCT(fd6_texture_state);435436/* NOTE: one ref for tex_cache, and second ref for returned state: */437pipe_reference_init(&state->reference, 2);438state->key = key;439state->stateobj = fd_ringbuffer_new_object(ctx->pipe, 32 * 4);440state->needs_border = needs_border;441442fd6_emit_textures(ctx, state->stateobj, type, tex, key.bcolor_offset, NULL);443444/* NOTE: uses copy of key in state obj, because pointer passed by caller445* is probably on the stack446*/447_mesa_hash_table_insert_pre_hashed(fd6_ctx->tex_cache, hash, &state->key,448state);449450out_unlock:451fd_screen_unlock(ctx->screen);452return state;453}454455void456__fd6_texture_state_describe(char *buf, const struct fd6_texture_state *tex)457{458sprintf(buf, "fd6_texture_state<%p>", tex);459}460461void462__fd6_texture_state_destroy(struct fd6_texture_state *state)463{464fd_ringbuffer_del(state->stateobj);465free(state);466}467468static void469fd6_rebind_resource(struct fd_context *ctx, struct fd_resource *rsc) assert_dt470{471fd_screen_assert_locked(ctx->screen);472473if (!(rsc->dirty & FD_DIRTY_TEX))474return;475476struct fd6_context *fd6_ctx = fd6_context(ctx);477478hash_table_foreach (fd6_ctx->tex_cache, entry) {479struct fd6_texture_state *state = entry->data;480481for (unsigned i = 0; i < ARRAY_SIZE(state->key.view); i++) {482if (rsc->seqno == state->key.view[i].rsc_seqno) {483remove_tex_entry(fd6_ctx, entry);484break;485}486}487}488}489490void491fd6_texture_init(struct pipe_context *pctx) disable_thread_safety_analysis492{493struct fd_context *ctx = fd_context(pctx);494struct fd6_context *fd6_ctx = fd6_context(ctx);495496pctx->create_sampler_state = fd6_sampler_state_create;497pctx->delete_sampler_state = fd6_sampler_state_delete;498pctx->bind_sampler_states = fd_sampler_states_bind;499500pctx->create_sampler_view = fd6_sampler_view_create;501pctx->sampler_view_destroy = fd6_sampler_view_destroy;502pctx->set_sampler_views = fd6_set_sampler_views;503504ctx->rebind_resource = fd6_rebind_resource;505506fd6_ctx->tex_cache = _mesa_hash_table_create(NULL, key_hash, key_equals);507}508509void510fd6_texture_fini(struct pipe_context *pctx)511{512struct fd_context *ctx = fd_context(pctx);513struct fd6_context *fd6_ctx = fd6_context(ctx);514515fd_screen_lock(ctx->screen);516517hash_table_foreach (fd6_ctx->tex_cache, entry) {518remove_tex_entry(fd6_ctx, entry);519}520521fd_screen_unlock(ctx->screen);522523ralloc_free(fd6_ctx->tex_cache);524}525526527