Path: blob/21.2-virgl/src/gallium/drivers/freedreno/a4xx/fd4_texture.c
4574 views
/*1* Copyright (C) 2014 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#include "pipe/p_state.h"27#include "util/format/u_format.h"28#include "util/u_inlines.h"29#include "util/u_memory.h"30#include "util/u_string.h"3132#include "fd4_format.h"33#include "fd4_texture.h"3435static enum a4xx_tex_clamp36tex_clamp(unsigned wrap, bool *needs_border)37{38switch (wrap) {39case PIPE_TEX_WRAP_REPEAT:40return A4XX_TEX_REPEAT;41case PIPE_TEX_WRAP_CLAMP_TO_EDGE:42return A4XX_TEX_CLAMP_TO_EDGE;43case PIPE_TEX_WRAP_CLAMP_TO_BORDER:44*needs_border = true;45return A4XX_TEX_CLAMP_TO_BORDER;46case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:47/* only works for PoT.. need to emulate otherwise! */48return A4XX_TEX_MIRROR_CLAMP;49case PIPE_TEX_WRAP_MIRROR_REPEAT:50return A4XX_TEX_MIRROR_REPEAT;51case PIPE_TEX_WRAP_MIRROR_CLAMP:52case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:53/* these two we could perhaps emulate, but we currently54* just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP55*/56default:57DBG("invalid wrap: %u", wrap);58return 0;59}60}6162static enum a4xx_tex_filter63tex_filter(unsigned filter, bool aniso)64{65switch (filter) {66case PIPE_TEX_FILTER_NEAREST:67return A4XX_TEX_NEAREST;68case PIPE_TEX_FILTER_LINEAR:69return aniso ? A4XX_TEX_ANISO : A4XX_TEX_LINEAR;70default:71DBG("invalid filter: %u", filter);72return 0;73}74}7576static void *77fd4_sampler_state_create(struct pipe_context *pctx,78const struct pipe_sampler_state *cso)79{80struct fd4_sampler_stateobj *so = CALLOC_STRUCT(fd4_sampler_stateobj);81unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));82bool miplinear = false;8384if (!so)85return NULL;8687if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)88miplinear = true;8990so->base = *cso;9192so->needs_border = false;93so->texsamp0 =94COND(miplinear, A4XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |95A4XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |96A4XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |97A4XX_TEX_SAMP_0_ANISO(aniso) |98A4XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |99A4XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |100A4XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));101102so->texsamp1 =103// COND(miplinear, A4XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |104COND(!cso->seamless_cube_map, A4XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |105COND(!cso->normalized_coords, A4XX_TEX_SAMP_1_UNNORM_COORDS);106107if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {108so->texsamp0 |= A4XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);109so->texsamp1 |= A4XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |110A4XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);111}112113if (cso->compare_mode)114so->texsamp1 |=115A4XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */116117return so;118}119120static enum a4xx_tex_type121tex_type(unsigned target)122{123switch (target) {124default:125assert(0);126case PIPE_BUFFER:127case PIPE_TEXTURE_1D:128case PIPE_TEXTURE_1D_ARRAY:129return A4XX_TEX_1D;130case PIPE_TEXTURE_RECT:131case PIPE_TEXTURE_2D:132case PIPE_TEXTURE_2D_ARRAY:133return A4XX_TEX_2D;134case PIPE_TEXTURE_3D:135return A4XX_TEX_3D;136case PIPE_TEXTURE_CUBE:137case PIPE_TEXTURE_CUBE_ARRAY:138return A4XX_TEX_CUBE;139}140}141142static bool143use_astc_srgb_workaround(struct pipe_context *pctx, enum pipe_format format)144{145return (fd_screen(pctx->screen)->gpu_id == 420) &&146(util_format_description(format)->layout == UTIL_FORMAT_LAYOUT_ASTC);147}148149static struct pipe_sampler_view *150fd4_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,151const struct pipe_sampler_view *cso)152{153struct fd4_pipe_sampler_view *so = CALLOC_STRUCT(fd4_pipe_sampler_view);154struct fd_resource *rsc = fd_resource(prsc);155enum pipe_format format = cso->format;156unsigned lvl, layers = 0;157158if (!so)159return NULL;160161if (format == PIPE_FORMAT_X32_S8X24_UINT) {162rsc = rsc->stencil;163format = rsc->b.b.format;164}165166so->base = *cso;167pipe_reference(NULL, &prsc->reference);168so->base.texture = prsc;169so->base.reference.count = 1;170so->base.context = pctx;171172so->texconst0 = A4XX_TEX_CONST_0_TYPE(tex_type(cso->target)) |173A4XX_TEX_CONST_0_FMT(fd4_pipe2tex(format)) |174fd4_tex_swiz(format, cso->swizzle_r, cso->swizzle_g,175cso->swizzle_b, cso->swizzle_a);176177if (util_format_is_srgb(format)) {178if (use_astc_srgb_workaround(pctx, format))179so->astc_srgb = true;180so->texconst0 |= A4XX_TEX_CONST_0_SRGB;181}182183if (cso->target == PIPE_BUFFER) {184unsigned elements = cso->u.buf.size / util_format_get_blocksize(format);185186lvl = 0;187so->texconst1 =188A4XX_TEX_CONST_1_WIDTH(elements) | A4XX_TEX_CONST_1_HEIGHT(1);189so->texconst2 = A4XX_TEX_CONST_2_PITCH(elements * rsc->layout.cpp);190so->offset = cso->u.buf.offset;191} else {192unsigned miplevels;193194lvl = fd_sampler_first_level(cso);195miplevels = fd_sampler_last_level(cso) - lvl;196layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1;197198so->texconst0 |= A4XX_TEX_CONST_0_MIPLVLS(miplevels);199so->texconst1 = A4XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |200A4XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));201so->texconst2 = A4XX_TEX_CONST_2_PITCHALIGN(rsc->layout.pitchalign - 5) |202A4XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));203so->offset = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);204}205206/* NOTE: since we sample z24s8 using 8888_UINT format, the swizzle207* we get isn't quite right. Use SWAP(XYZW) as a cheap and cheerful208* way to re-arrange things so stencil component is where the swiz209* expects.210*211* Note that gallium expects stencil sampler to return (s,s,s,s)212* which isn't quite true. To make that happen we'd have to massage213* the swizzle. But in practice only the .x component is used.214*/215if (format == PIPE_FORMAT_X24S8_UINT)216so->texconst2 |= A4XX_TEX_CONST_2_SWAP(XYZW);217218switch (cso->target) {219case PIPE_TEXTURE_1D_ARRAY:220case PIPE_TEXTURE_2D_ARRAY:221so->texconst3 = A4XX_TEX_CONST_3_DEPTH(layers) |222A4XX_TEX_CONST_3_LAYERSZ(rsc->layout.layer_size);223break;224case PIPE_TEXTURE_CUBE:225case PIPE_TEXTURE_CUBE_ARRAY:226so->texconst3 = A4XX_TEX_CONST_3_DEPTH(layers / 6) |227A4XX_TEX_CONST_3_LAYERSZ(rsc->layout.layer_size);228break;229case PIPE_TEXTURE_3D:230so->texconst3 =231A4XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |232A4XX_TEX_CONST_3_LAYERSZ(fd_resource_slice(rsc, lvl)->size0);233so->texconst4 = A4XX_TEX_CONST_4_LAYERSZ(234fd_resource_slice(rsc, prsc->last_level)->size0);235break;236default:237so->texconst3 = 0x00000000;238break;239}240241return &so->base;242}243244static void245fd4_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,246unsigned start, unsigned nr,247unsigned unbind_num_trailing_slots,248struct pipe_sampler_view **views)249{250struct fd_context *ctx = fd_context(pctx);251struct fd4_context *fd4_ctx = fd4_context(ctx);252uint16_t astc_srgb = 0;253unsigned i;254255for (i = 0; i < nr; i++) {256if (views[i]) {257struct fd4_pipe_sampler_view *view = fd4_pipe_sampler_view(views[i]);258if (view->astc_srgb)259astc_srgb |= (1 << i);260}261}262263fd_set_sampler_views(pctx, shader, start, nr, unbind_num_trailing_slots,264views);265266if (shader == PIPE_SHADER_FRAGMENT) {267fd4_ctx->fastc_srgb = astc_srgb;268} else if (shader == PIPE_SHADER_VERTEX) {269fd4_ctx->vastc_srgb = astc_srgb;270}271}272273void274fd4_texture_init(struct pipe_context *pctx)275{276pctx->create_sampler_state = fd4_sampler_state_create;277pctx->bind_sampler_states = fd_sampler_states_bind;278pctx->create_sampler_view = fd4_sampler_view_create;279pctx->set_sampler_views = fd4_set_sampler_views;280}281282283