Path: blob/21.2-virgl/src/gallium/drivers/softpipe/sp_tex_sample.c
4570 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4* Copyright 2008-2010 VMware, Inc. All rights reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/2728/**29* Texture sampling30*31* Authors:32* Brian Paul33* Keith Whitwell34*/3536#include "pipe/p_context.h"37#include "pipe/p_defines.h"38#include "pipe/p_shader_tokens.h"39#include "util/u_math.h"40#include "util/format/u_format.h"41#include "util/u_memory.h"42#include "util/u_inlines.h"43#include "sp_quad.h" /* only for #define QUAD_* tokens */44#include "sp_tex_sample.h"45#include "sp_texture.h"46#include "sp_tex_tile_cache.h"474849/** Set to one to help debug texture sampling */50#define DEBUG_TEX 0515253/*54* Return fractional part of 'f'. Used for computing interpolation weights.55* Need to be careful with negative values.56* Note, if this function isn't perfect you'll sometimes see 1-pixel bands57* of improperly weighted linear-filtered textures.58* The tests/texwrap.c demo is a good test.59*/60static inline float61frac(float f)62{63return f - floorf(f);64}65666768/**69* Linear interpolation macro70*/71static inline float72lerp(float a, float v0, float v1)73{74return v0 + a * (v1 - v0);75}767778/**79* Do 2D/bilinear interpolation of float values.80* v00, v10, v01 and v11 are typically four texture samples in a square/box.81* a and b are the horizontal and vertical interpolants.82* It's important that this function is inlined when compiled with83* optimization! If we find that's not true on some systems, convert84* to a macro.85*/86static inline float87lerp_2d(float a, float b,88float v00, float v10, float v01, float v11)89{90const float temp0 = lerp(a, v00, v10);91const float temp1 = lerp(a, v01, v11);92return lerp(b, temp0, temp1);93}949596/**97* As above, but 3D interpolation of 8 values.98*/99static inline float100lerp_3d(float a, float b, float c,101float v000, float v100, float v010, float v110,102float v001, float v101, float v011, float v111)103{104const float temp0 = lerp_2d(a, b, v000, v100, v010, v110);105const float temp1 = lerp_2d(a, b, v001, v101, v011, v111);106return lerp(c, temp0, temp1);107}108109110111/**112* Compute coord % size for repeat wrap modes.113* Note that if coord is negative, coord % size doesn't give the right114* value. To avoid that problem we add a large multiple of the size115* (rather than using a conditional).116*/117static inline int118repeat(int coord, unsigned size)119{120return (coord + size * 1024) % size;121}122123124/**125* Apply texture coord wrapping mode and return integer texture indexes126* for a vector of four texcoords (S or T or P).127* \param wrapMode PIPE_TEX_WRAP_x128* \param s the incoming texcoords129* \param size the texture image size130* \param icoord returns the integer texcoords131*/132static void133wrap_nearest_repeat(float s, unsigned size, int offset, int *icoord)134{135/* s limited to [0,1) */136/* i limited to [0,size-1] */137const int i = util_ifloor(s * size);138*icoord = repeat(i + offset, size);139}140141142static void143wrap_nearest_clamp(float s, unsigned size, int offset, int *icoord)144{145/* s limited to [0,1] */146/* i limited to [0,size-1] */147s *= size;148s += offset;149if (s <= 0.0F)150*icoord = 0;151else if (s >= size)152*icoord = size - 1;153else154*icoord = util_ifloor(s);155}156157158static void159wrap_nearest_clamp_to_edge(float s, unsigned size, int offset, int *icoord)160{161/* s limited to [min,max] */162/* i limited to [0, size-1] */163const float min = 0.5F;164const float max = (float)size - 0.5F;165166s *= size;167s += offset;168169if (s < min)170*icoord = 0;171else if (s > max)172*icoord = size - 1;173else174*icoord = util_ifloor(s);175}176177178static void179wrap_nearest_clamp_to_border(float s, unsigned size, int offset, int *icoord)180{181/* s limited to [min,max] */182/* i limited to [-1, size] */183const float min = -0.5F;184const float max = size + 0.5F;185186s *= size;187s += offset;188if (s <= min)189*icoord = -1;190else if (s >= max)191*icoord = size;192else193*icoord = util_ifloor(s);194}195196static void197wrap_nearest_mirror_repeat(float s, unsigned size, int offset, int *icoord)198{199const float min = 1.0F / (2.0F * size);200const float max = 1.0F - min;201int flr;202float u;203204s += (float)offset / size;205flr = util_ifloor(s);206u = frac(s);207if (flr & 1)208u = 1.0F - u;209if (u < min)210*icoord = 0;211else if (u > max)212*icoord = size - 1;213else214*icoord = util_ifloor(u * size);215}216217218static void219wrap_nearest_mirror_clamp(float s, unsigned size, int offset, int *icoord)220{221/* s limited to [0,1] */222/* i limited to [0,size-1] */223const float u = fabsf(s * size + offset);224if (u <= 0.0F)225*icoord = 0;226else if (u >= size)227*icoord = size - 1;228else229*icoord = util_ifloor(u);230}231232233static void234wrap_nearest_mirror_clamp_to_edge(float s, unsigned size, int offset, int *icoord)235{236/* s limited to [min,max] */237/* i limited to [0, size-1] */238const float min = 0.5F;239const float max = (float)size - 0.5F;240const float u = fabsf(s * size + offset);241242if (u < min)243*icoord = 0;244else if (u > max)245*icoord = size - 1;246else247*icoord = util_ifloor(u);248}249250251static void252wrap_nearest_mirror_clamp_to_border(float s, unsigned size, int offset, int *icoord)253{254/* u limited to [-0.5, size-0.5] */255const float min = -0.5F;256const float max = (float)size + 0.5F;257const float u = fabsf(s * size + offset);258259if (u < min)260*icoord = -1;261else if (u > max)262*icoord = size;263else264*icoord = util_ifloor(u);265}266267268/**269* Used to compute texel locations for linear sampling270* \param wrapMode PIPE_TEX_WRAP_x271* \param s the texcoord272* \param size the texture image size273* \param icoord0 returns first texture index274* \param icoord1 returns second texture index (usually icoord0 + 1)275* \param w returns blend factor/weight between texture indices276* \param icoord returns the computed integer texture coord277*/278static void279wrap_linear_repeat(float s, unsigned size, int offset,280int *icoord0, int *icoord1, float *w)281{282const float u = s * size - 0.5F;283*icoord0 = repeat(util_ifloor(u) + offset, size);284*icoord1 = repeat(*icoord0 + 1, size);285*w = frac(u);286}287288289static void290wrap_linear_clamp(float s, unsigned size, int offset,291int *icoord0, int *icoord1, float *w)292{293const float u = CLAMP(s * size + offset, 0.0F, (float)size) - 0.5f;294295*icoord0 = util_ifloor(u);296*icoord1 = *icoord0 + 1;297*w = frac(u);298}299300301static void302wrap_linear_clamp_to_edge(float s, unsigned size, int offset,303int *icoord0, int *icoord1, float *w)304{305const float u = CLAMP(s * size + offset, 0.0F, (float)size) - 0.5f;306*icoord0 = util_ifloor(u);307*icoord1 = *icoord0 + 1;308if (*icoord0 < 0)309*icoord0 = 0;310if (*icoord1 >= (int) size)311*icoord1 = size - 1;312*w = frac(u);313}314315316static void317wrap_linear_clamp_to_border(float s, unsigned size, int offset,318int *icoord0, int *icoord1, float *w)319{320const float min = -1.0F;321const float max = (float)size + 0.5F;322const float u = CLAMP(s * size + offset, min, max) - 0.5f;323*icoord0 = util_ifloor(u);324*icoord1 = *icoord0 + 1;325*w = frac(u);326}327328329static void330wrap_linear_mirror_repeat(float s, unsigned size, int offset,331int *icoord0, int *icoord1, float *w)332{333int flr;334float u;335bool no_mirror;336337s += (float)offset / size;338flr = util_ifloor(s);339no_mirror = !(flr & 1);340341u = frac(s);342if (no_mirror) {343u = u * size - 0.5F;344} else {345u = 1.0F - u;346u = u * size + 0.5F;347}348349*icoord0 = util_ifloor(u);350*icoord1 = (no_mirror) ? *icoord0 + 1 : *icoord0 - 1;351352if (*icoord0 < 0)353*icoord0 = 1 + *icoord0;354if (*icoord0 >= (int) size)355*icoord0 = size - 1;356357if (*icoord1 >= (int) size)358*icoord1 = size - 1;359if (*icoord1 < 0)360*icoord1 = 1 + *icoord1;361362*w = (no_mirror) ? frac(u) : frac(1.0f - u);363}364365366static void367wrap_linear_mirror_clamp(float s, unsigned size, int offset,368int *icoord0, int *icoord1, float *w)369{370float u = fabsf(s * size + offset);371if (u >= size)372u = (float) size;373u -= 0.5F;374*icoord0 = util_ifloor(u);375*icoord1 = *icoord0 + 1;376*w = frac(u);377}378379380static void381wrap_linear_mirror_clamp_to_edge(float s, unsigned size, int offset,382int *icoord0, int *icoord1, float *w)383{384float u = fabsf(s * size + offset);385if (u >= size)386u = (float) size;387u -= 0.5F;388*icoord0 = util_ifloor(u);389*icoord1 = *icoord0 + 1;390if (*icoord0 < 0)391*icoord0 = 0;392if (*icoord1 >= (int) size)393*icoord1 = size - 1;394*w = frac(u);395}396397398static void399wrap_linear_mirror_clamp_to_border(float s, unsigned size, int offset,400int *icoord0, int *icoord1, float *w)401{402const float min = -0.5F;403const float max = size + 0.5F;404const float t = fabsf(s * size + offset);405const float u = CLAMP(t, min, max) - 0.5F;406*icoord0 = util_ifloor(u);407*icoord1 = *icoord0 + 1;408*w = frac(u);409}410411412/**413* PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.414*/415static void416wrap_nearest_unorm_clamp(float s, unsigned size, int offset, int *icoord)417{418const int i = util_ifloor(s);419*icoord = CLAMP(i + offset, 0, (int) size-1);420}421422423/**424* PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.425*/426static void427wrap_nearest_unorm_clamp_to_border(float s, unsigned size, int offset, int *icoord)428{429*icoord = util_ifloor( CLAMP(s + offset, -0.5F, (float) size + 0.5F) );430}431432433/**434* PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.435*/436static void437wrap_nearest_unorm_clamp_to_edge(float s, unsigned size, int offset, int *icoord)438{439*icoord = util_ifloor( CLAMP(s + offset, 0.5F, (float) size - 0.5F) );440}441442443/**444* PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.445*/446static void447wrap_linear_unorm_clamp(float s, unsigned size, int offset,448int *icoord0, int *icoord1, float *w)449{450/* Not exactly what the spec says, but it matches NVIDIA output */451const float u = CLAMP(s + offset - 0.5F, 0.0f, (float) size - 1.0f);452*icoord0 = util_ifloor(u);453*icoord1 = *icoord0 + 1;454*w = frac(u);455}456457458/**459* PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.460*/461static void462wrap_linear_unorm_clamp_to_border(float s, unsigned size, int offset,463int *icoord0, int *icoord1, float *w)464{465const float u = CLAMP(s + offset, -0.5F, (float) size + 0.5F) - 0.5F;466*icoord0 = util_ifloor(u);467*icoord1 = *icoord0 + 1;468if (*icoord1 > (int) size - 1)469*icoord1 = size - 1;470*w = frac(u);471}472473474/**475* PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.476*/477static void478wrap_linear_unorm_clamp_to_edge(float s, unsigned size, int offset,479int *icoord0, int *icoord1, float *w)480{481const float u = CLAMP(s + offset, +0.5F, (float) size - 0.5F) - 0.5F;482*icoord0 = util_ifloor(u);483*icoord1 = *icoord0 + 1;484if (*icoord1 > (int) size - 1)485*icoord1 = size - 1;486*w = frac(u);487}488489490/**491* Do coordinate to array index conversion. For array textures.492*/493static inline int494coord_to_layer(float coord, unsigned first_layer, unsigned last_layer)495{496const int c = util_ifloor(coord + 0.5F);497return CLAMP(c, (int)first_layer, (int)last_layer);498}499500static void501compute_gradient_1d(const float s[TGSI_QUAD_SIZE],502const float t[TGSI_QUAD_SIZE],503const float p[TGSI_QUAD_SIZE],504float derivs[3][2][TGSI_QUAD_SIZE])505{506memset(derivs, 0, 6 * TGSI_QUAD_SIZE * sizeof(float));507derivs[0][0][0] = s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT];508derivs[0][1][0] = s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT];509}510511static float512compute_lambda_1d_explicit_gradients(const struct sp_sampler_view *sview,513const float derivs[3][2][TGSI_QUAD_SIZE],514uint quad)515{516const struct pipe_resource *texture = sview->base.texture;517const float dsdx = fabsf(derivs[0][0][quad]);518const float dsdy = fabsf(derivs[0][1][quad]);519const float rho = MAX2(dsdx, dsdy) * u_minify(texture->width0, sview->base.u.tex.first_level);520return util_fast_log2(rho);521}522523524/**525* Examine the quad's texture coordinates to compute the partial526* derivatives w.r.t X and Y, then compute lambda (level of detail).527*/528static float529compute_lambda_1d(const struct sp_sampler_view *sview,530const float s[TGSI_QUAD_SIZE],531const float t[TGSI_QUAD_SIZE],532const float p[TGSI_QUAD_SIZE])533{534float derivs[3][2][TGSI_QUAD_SIZE];535compute_gradient_1d(s, t, p, derivs);536return compute_lambda_1d_explicit_gradients(sview, derivs, 0);537}538539540static void541compute_gradient_2d(const float s[TGSI_QUAD_SIZE],542const float t[TGSI_QUAD_SIZE],543const float p[TGSI_QUAD_SIZE],544float derivs[3][2][TGSI_QUAD_SIZE])545{546memset(derivs, 0, 6 * TGSI_QUAD_SIZE * sizeof(float));547derivs[0][0][0] = s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT];548derivs[0][1][0] = s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT];549derivs[1][0][0] = t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT];550derivs[1][1][0] = t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT];551}552553static float554compute_lambda_2d_explicit_gradients(const struct sp_sampler_view *sview,555const float derivs[3][2][TGSI_QUAD_SIZE],556uint quad)557{558const struct pipe_resource *texture = sview->base.texture;559const float dsdx = fabsf(derivs[0][0][quad]);560const float dsdy = fabsf(derivs[0][1][quad]);561const float dtdx = fabsf(derivs[1][0][quad]);562const float dtdy = fabsf(derivs[1][1][quad]);563const float maxx = MAX2(dsdx, dsdy) * u_minify(texture->width0, sview->base.u.tex.first_level);564const float maxy = MAX2(dtdx, dtdy) * u_minify(texture->height0, sview->base.u.tex.first_level);565const float rho = MAX2(maxx, maxy);566return util_fast_log2(rho);567}568569570static float571compute_lambda_2d(const struct sp_sampler_view *sview,572const float s[TGSI_QUAD_SIZE],573const float t[TGSI_QUAD_SIZE],574const float p[TGSI_QUAD_SIZE])575{576float derivs[3][2][TGSI_QUAD_SIZE];577compute_gradient_2d(s, t, p, derivs);578return compute_lambda_2d_explicit_gradients(sview, derivs, 0);579}580581582static void583compute_gradient_3d(const float s[TGSI_QUAD_SIZE],584const float t[TGSI_QUAD_SIZE],585const float p[TGSI_QUAD_SIZE],586float derivs[3][2][TGSI_QUAD_SIZE])587{588memset(derivs, 0, 6 * TGSI_QUAD_SIZE * sizeof(float));589derivs[0][0][0] = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);590derivs[0][1][0] = fabsf(s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]);591derivs[1][0][0] = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);592derivs[1][1][0] = fabsf(t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]);593derivs[2][0][0] = fabsf(p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]);594derivs[2][1][0] = fabsf(p[QUAD_TOP_LEFT] - p[QUAD_BOTTOM_LEFT]);595}596597static float598compute_lambda_3d_explicit_gradients(const struct sp_sampler_view *sview,599const float derivs[3][2][TGSI_QUAD_SIZE],600uint quad)601{602const struct pipe_resource *texture = sview->base.texture;603const float dsdx = fabsf(derivs[0][0][quad]);604const float dsdy = fabsf(derivs[0][1][quad]);605const float dtdx = fabsf(derivs[1][0][quad]);606const float dtdy = fabsf(derivs[1][1][quad]);607const float dpdx = fabsf(derivs[2][0][quad]);608const float dpdy = fabsf(derivs[2][1][quad]);609const float maxx = MAX2(dsdx, dsdy) * u_minify(texture->width0, sview->base.u.tex.first_level);610const float maxy = MAX2(dtdx, dtdy) * u_minify(texture->height0, sview->base.u.tex.first_level);611const float maxz = MAX2(dpdx, dpdy) * u_minify(texture->depth0, sview->base.u.tex.first_level);612const float rho = MAX3(maxx, maxy, maxz);613614return util_fast_log2(rho);615}616617618static float619compute_lambda_3d(const struct sp_sampler_view *sview,620const float s[TGSI_QUAD_SIZE],621const float t[TGSI_QUAD_SIZE],622const float p[TGSI_QUAD_SIZE])623{624float derivs[3][2][TGSI_QUAD_SIZE];625compute_gradient_3d(s, t, p, derivs);626return compute_lambda_3d_explicit_gradients(sview, derivs, 0);627}628629630static float631compute_lambda_cube_explicit_gradients(const struct sp_sampler_view *sview,632const float derivs[3][2][TGSI_QUAD_SIZE],633uint quad)634{635const struct pipe_resource *texture = sview->base.texture;636const float dsdx = fabsf(derivs[0][0][quad]);637const float dsdy = fabsf(derivs[0][1][quad]);638const float dtdx = fabsf(derivs[1][0][quad]);639const float dtdy = fabsf(derivs[1][1][quad]);640const float dpdx = fabsf(derivs[2][0][quad]);641const float dpdy = fabsf(derivs[2][1][quad]);642const float maxx = MAX2(dsdx, dsdy);643const float maxy = MAX2(dtdx, dtdy);644const float maxz = MAX2(dpdx, dpdy);645const float rho = MAX3(maxx, maxy, maxz) * u_minify(texture->width0, sview->base.u.tex.first_level) / 2.0f;646647return util_fast_log2(rho);648}649650static float651compute_lambda_cube(const struct sp_sampler_view *sview,652const float s[TGSI_QUAD_SIZE],653const float t[TGSI_QUAD_SIZE],654const float p[TGSI_QUAD_SIZE])655{656float derivs[3][2][TGSI_QUAD_SIZE];657compute_gradient_3d(s, t, p, derivs);658return compute_lambda_cube_explicit_gradients(sview, derivs, 0);659}660661/**662* Compute lambda for a vertex texture sampler.663* Since there aren't derivatives to use, just return 0.664*/665static float666compute_lambda_vert(const struct sp_sampler_view *sview,667const float s[TGSI_QUAD_SIZE],668const float t[TGSI_QUAD_SIZE],669const float p[TGSI_QUAD_SIZE])670{671return 0.0f;672}673674675compute_lambda_from_grad_func676softpipe_get_lambda_from_grad_func(const struct pipe_sampler_view *view,677enum pipe_shader_type shader)678{679switch (view->target) {680case PIPE_BUFFER:681case PIPE_TEXTURE_1D:682case PIPE_TEXTURE_1D_ARRAY:683return compute_lambda_1d_explicit_gradients;684case PIPE_TEXTURE_2D:685case PIPE_TEXTURE_2D_ARRAY:686case PIPE_TEXTURE_RECT:687return compute_lambda_2d_explicit_gradients;688case PIPE_TEXTURE_CUBE:689case PIPE_TEXTURE_CUBE_ARRAY:690return compute_lambda_cube_explicit_gradients;691case PIPE_TEXTURE_3D:692return compute_lambda_3d_explicit_gradients;693default:694assert(0);695return compute_lambda_1d_explicit_gradients;696}697}698699700/**701* Get a texel from a texture, using the texture tile cache.702*703* \param addr the template tex address containing cube, z, face info.704* \param x the x coord of texel within 2D image705* \param y the y coord of texel within 2D image706* \param rgba the quad to put the texel/color into707*708* XXX maybe move this into sp_tex_tile_cache.c and merge with the709* sp_get_cached_tile_tex() function.710*/711712713714static inline const float *715get_texel_buffer_no_border(const struct sp_sampler_view *sp_sview,716union tex_tile_address addr, int x, unsigned elmsize)717{718const struct softpipe_tex_cached_tile *tile;719addr.bits.x = x * elmsize / TEX_TILE_SIZE;720assert(x * elmsize / TEX_TILE_SIZE == addr.bits.x);721722x %= TEX_TILE_SIZE / elmsize;723724tile = sp_get_cached_tile_tex(sp_sview->cache, addr);725726return &tile->data.color[0][x][0];727}728729730static inline const float *731get_texel_2d_no_border(const struct sp_sampler_view *sp_sview,732union tex_tile_address addr, int x, int y)733{734const struct softpipe_tex_cached_tile *tile;735addr.bits.x = x / TEX_TILE_SIZE;736addr.bits.y = y / TEX_TILE_SIZE;737y %= TEX_TILE_SIZE;738x %= TEX_TILE_SIZE;739740tile = sp_get_cached_tile_tex(sp_sview->cache, addr);741742return &tile->data.color[y][x][0];743}744745746static inline const float *747get_texel_2d(const struct sp_sampler_view *sp_sview,748const struct sp_sampler *sp_samp,749union tex_tile_address addr, int x, int y)750{751const struct pipe_resource *texture = sp_sview->base.texture;752const unsigned level = addr.bits.level;753754if (x < 0 || x >= (int) u_minify(texture->width0, level) ||755y < 0 || y >= (int) u_minify(texture->height0, level)) {756return sp_sview->border_color.f;757}758else {759return get_texel_2d_no_border( sp_sview, addr, x, y );760}761}762763764/*765* Here's the complete logic (HOLY CRAP) for finding next face and doing the766* corresponding coord wrapping, implemented by get_next_face,767* get_next_xcoord, get_next_ycoord.768* Read like that (first line):769* If face is +x and s coord is below zero, then770* new face is +z, new s is max , new t is old t771* (max is always cube size - 1).772*773* +x s- -> +z: s = max, t = t774* +x s+ -> -z: s = 0, t = t775* +x t- -> +y: s = max, t = max-s776* +x t+ -> -y: s = max, t = s777*778* -x s- -> -z: s = max, t = t779* -x s+ -> +z: s = 0, t = t780* -x t- -> +y: s = 0, t = s781* -x t+ -> -y: s = 0, t = max-s782*783* +y s- -> -x: s = t, t = 0784* +y s+ -> +x: s = max-t, t = 0785* +y t- -> -z: s = max-s, t = 0786* +y t+ -> +z: s = s, t = 0787*788* -y s- -> -x: s = max-t, t = max789* -y s+ -> +x: s = t, t = max790* -y t- -> +z: s = s, t = max791* -y t+ -> -z: s = max-s, t = max792793* +z s- -> -x: s = max, t = t794* +z s+ -> +x: s = 0, t = t795* +z t- -> +y: s = s, t = max796* +z t+ -> -y: s = s, t = 0797798* -z s- -> +x: s = max, t = t799* -z s+ -> -x: s = 0, t = t800* -z t- -> +y: s = max-s, t = 0801* -z t+ -> -y: s = max-s, t = max802*/803804805/*806* seamless cubemap neighbour array.807* this array is used to find the adjacent face in each of 4 directions,808* left, right, up, down. (or -x, +x, -y, +y).809*/810static const unsigned face_array[PIPE_TEX_FACE_MAX][4] = {811/* pos X first then neg X is Z different, Y the same */812/* PIPE_TEX_FACE_POS_X,*/813{ PIPE_TEX_FACE_POS_Z, PIPE_TEX_FACE_NEG_Z,814PIPE_TEX_FACE_POS_Y, PIPE_TEX_FACE_NEG_Y },815/* PIPE_TEX_FACE_NEG_X */816{ PIPE_TEX_FACE_NEG_Z, PIPE_TEX_FACE_POS_Z,817PIPE_TEX_FACE_POS_Y, PIPE_TEX_FACE_NEG_Y },818819/* pos Y first then neg Y is X different, X the same */820/* PIPE_TEX_FACE_POS_Y */821{ PIPE_TEX_FACE_NEG_X, PIPE_TEX_FACE_POS_X,822PIPE_TEX_FACE_NEG_Z, PIPE_TEX_FACE_POS_Z },823824/* PIPE_TEX_FACE_NEG_Y */825{ PIPE_TEX_FACE_NEG_X, PIPE_TEX_FACE_POS_X,826PIPE_TEX_FACE_POS_Z, PIPE_TEX_FACE_NEG_Z },827828/* pos Z first then neg Y is X different, X the same */829/* PIPE_TEX_FACE_POS_Z */830{ PIPE_TEX_FACE_NEG_X, PIPE_TEX_FACE_POS_X,831PIPE_TEX_FACE_POS_Y, PIPE_TEX_FACE_NEG_Y },832833/* PIPE_TEX_FACE_NEG_Z */834{ PIPE_TEX_FACE_POS_X, PIPE_TEX_FACE_NEG_X,835PIPE_TEX_FACE_POS_Y, PIPE_TEX_FACE_NEG_Y }836};837838static inline unsigned839get_next_face(unsigned face, int idx)840{841return face_array[face][idx];842}843844/*845* return a new xcoord based on old face, old coords, cube size846* and fall_off_index (0 for x-, 1 for x+, 2 for y-, 3 for y+)847*/848static inline int849get_next_xcoord(unsigned face, unsigned fall_off_index, int max, int xc, int yc)850{851if ((face == 0 && fall_off_index != 1) ||852(face == 1 && fall_off_index == 0) ||853(face == 4 && fall_off_index == 0) ||854(face == 5 && fall_off_index == 0)) {855return max;856}857if ((face == 1 && fall_off_index != 0) ||858(face == 0 && fall_off_index == 1) ||859(face == 4 && fall_off_index == 1) ||860(face == 5 && fall_off_index == 1)) {861return 0;862}863if ((face == 4 && fall_off_index >= 2) ||864(face == 2 && fall_off_index == 3) ||865(face == 3 && fall_off_index == 2)) {866return xc;867}868if ((face == 5 && fall_off_index >= 2) ||869(face == 2 && fall_off_index == 2) ||870(face == 3 && fall_off_index == 3)) {871return max - xc;872}873if ((face == 2 && fall_off_index == 0) ||874(face == 3 && fall_off_index == 1)) {875return yc;876}877/* (face == 2 && fall_off_index == 1) ||878(face == 3 && fall_off_index == 0)) */879return max - yc;880}881882/*883* return a new ycoord based on old face, old coords, cube size884* and fall_off_index (0 for x-, 1 for x+, 2 for y-, 3 for y+)885*/886static inline int887get_next_ycoord(unsigned face, unsigned fall_off_index, int max, int xc, int yc)888{889if ((fall_off_index <= 1) && (face <= 1 || face >= 4)) {890return yc;891}892if (face == 2 ||893(face == 4 && fall_off_index == 3) ||894(face == 5 && fall_off_index == 2)) {895return 0;896}897if (face == 3 ||898(face == 4 && fall_off_index == 2) ||899(face == 5 && fall_off_index == 3)) {900return max;901}902if ((face == 0 && fall_off_index == 3) ||903(face == 1 && fall_off_index == 2)) {904return xc;905}906/* (face == 0 && fall_off_index == 2) ||907(face == 1 && fall_off_index == 3) */908return max - xc;909}910911912/* Gather a quad of adjacent texels within a tile:913*/914static inline void915get_texel_quad_2d_no_border_single_tile(const struct sp_sampler_view *sp_sview,916union tex_tile_address addr,917unsigned x, unsigned y,918const float *out[4])919{920const struct softpipe_tex_cached_tile *tile;921922addr.bits.x = x / TEX_TILE_SIZE;923addr.bits.y = y / TEX_TILE_SIZE;924y %= TEX_TILE_SIZE;925x %= TEX_TILE_SIZE;926927tile = sp_get_cached_tile_tex(sp_sview->cache, addr);928929out[0] = &tile->data.color[y ][x ][0];930out[1] = &tile->data.color[y ][x+1][0];931out[2] = &tile->data.color[y+1][x ][0];932out[3] = &tile->data.color[y+1][x+1][0];933}934935936/* Gather a quad of potentially non-adjacent texels:937*/938static inline void939get_texel_quad_2d_no_border(const struct sp_sampler_view *sp_sview,940union tex_tile_address addr,941int x0, int y0,942int x1, int y1,943const float *out[4])944{945out[0] = get_texel_2d_no_border( sp_sview, addr, x0, y0 );946out[1] = get_texel_2d_no_border( sp_sview, addr, x1, y0 );947out[2] = get_texel_2d_no_border( sp_sview, addr, x0, y1 );948out[3] = get_texel_2d_no_border( sp_sview, addr, x1, y1 );949}950951952/* 3d variants:953*/954static inline const float *955get_texel_3d_no_border(const struct sp_sampler_view *sp_sview,956union tex_tile_address addr, int x, int y, int z)957{958const struct softpipe_tex_cached_tile *tile;959960addr.bits.x = x / TEX_TILE_SIZE;961addr.bits.y = y / TEX_TILE_SIZE;962addr.bits.z = z;963y %= TEX_TILE_SIZE;964x %= TEX_TILE_SIZE;965966tile = sp_get_cached_tile_tex(sp_sview->cache, addr);967968return &tile->data.color[y][x][0];969}970971972static inline const float *973get_texel_3d(const struct sp_sampler_view *sp_sview,974const struct sp_sampler *sp_samp,975union tex_tile_address addr, int x, int y, int z)976{977const struct pipe_resource *texture = sp_sview->base.texture;978const unsigned level = addr.bits.level;979980if (x < 0 || x >= (int) u_minify(texture->width0, level) ||981y < 0 || y >= (int) u_minify(texture->height0, level) ||982z < 0 || z >= (int) u_minify(texture->depth0, level)) {983return sp_sview->border_color.f;984}985else {986return get_texel_3d_no_border( sp_sview, addr, x, y, z );987}988}989990991/* Get texel pointer for 1D array texture */992static inline const float *993get_texel_1d_array(const struct sp_sampler_view *sp_sview,994const struct sp_sampler *sp_samp,995union tex_tile_address addr, int x, int y)996{997const struct pipe_resource *texture = sp_sview->base.texture;998const unsigned level = addr.bits.level;9991000if (x < 0 || x >= (int) u_minify(texture->width0, level)) {1001return sp_sview->border_color.f;1002}1003else {1004return get_texel_2d_no_border(sp_sview, addr, x, y);1005}1006}100710081009/* Get texel pointer for 2D array texture */1010static inline const float *1011get_texel_2d_array(const struct sp_sampler_view *sp_sview,1012const struct sp_sampler *sp_samp,1013union tex_tile_address addr, int x, int y, int layer)1014{1015const struct pipe_resource *texture = sp_sview->base.texture;1016const unsigned level = addr.bits.level;10171018assert(layer < (int) texture->array_size);1019assert(layer >= 0);10201021if (x < 0 || x >= (int) u_minify(texture->width0, level) ||1022y < 0 || y >= (int) u_minify(texture->height0, level)) {1023return sp_sview->border_color.f;1024}1025else {1026return get_texel_3d_no_border(sp_sview, addr, x, y, layer);1027}1028}102910301031static inline const float *1032get_texel_cube_seamless(const struct sp_sampler_view *sp_sview,1033union tex_tile_address addr, int x, int y,1034float *corner, int layer, unsigned face)1035{1036const struct pipe_resource *texture = sp_sview->base.texture;1037const unsigned level = addr.bits.level;1038int new_x, new_y, max_x;10391040max_x = (int) u_minify(texture->width0, level);10411042assert(texture->width0 == texture->height0);1043new_x = x;1044new_y = y;10451046/* change the face */1047if (x < 0) {1048/*1049* Cheat with corners. They are difficult and I believe because we don't get1050* per-pixel faces we can actually have multiple corner texels per pixel,1051* which screws things up majorly in any case (as the per spec behavior is1052* to average the 3 remaining texels, which we might not have).1053* Hence just make sure that the 2nd coord is clamped, will simply pick the1054* sample which would have fallen off the x coord, but not y coord.1055* So the filter weight of the samples will be wrong, but at least this1056* ensures that only valid texels near the corner are used.1057*/1058if (y < 0 || y >= max_x) {1059y = CLAMP(y, 0, max_x - 1);1060}1061new_x = get_next_xcoord(face, 0, max_x -1, x, y);1062new_y = get_next_ycoord(face, 0, max_x -1, x, y);1063face = get_next_face(face, 0);1064} else if (x >= max_x) {1065if (y < 0 || y >= max_x) {1066y = CLAMP(y, 0, max_x - 1);1067}1068new_x = get_next_xcoord(face, 1, max_x -1, x, y);1069new_y = get_next_ycoord(face, 1, max_x -1, x, y);1070face = get_next_face(face, 1);1071} else if (y < 0) {1072new_x = get_next_xcoord(face, 2, max_x -1, x, y);1073new_y = get_next_ycoord(face, 2, max_x -1, x, y);1074face = get_next_face(face, 2);1075} else if (y >= max_x) {1076new_x = get_next_xcoord(face, 3, max_x -1, x, y);1077new_y = get_next_ycoord(face, 3, max_x -1, x, y);1078face = get_next_face(face, 3);1079}10801081return get_texel_3d_no_border(sp_sview, addr, new_x, new_y, layer + face);1082}108310841085/* Get texel pointer for cube array texture */1086static inline const float *1087get_texel_cube_array(const struct sp_sampler_view *sp_sview,1088const struct sp_sampler *sp_samp,1089union tex_tile_address addr, int x, int y, int layer)1090{1091const struct pipe_resource *texture = sp_sview->base.texture;1092const unsigned level = addr.bits.level;10931094assert(layer < (int) texture->array_size);1095assert(layer >= 0);10961097if (x < 0 || x >= (int) u_minify(texture->width0, level) ||1098y < 0 || y >= (int) u_minify(texture->height0, level)) {1099return sp_sview->border_color.f;1100}1101else {1102return get_texel_3d_no_border(sp_sview, addr, x, y, layer);1103}1104}1105/**1106* Given the logbase2 of a mipmap's base level size and a mipmap level,1107* return the size (in texels) of that mipmap level.1108* For example, if level[0].width = 256 then base_pot will be 8.1109* If level = 2, then we'll return 64 (the width at level=2).1110* Return 1 if level > base_pot.1111*/1112static inline unsigned1113pot_level_size(unsigned base_pot, unsigned level)1114{1115return (base_pot >= level) ? (1 << (base_pot - level)) : 1;1116}111711181119static void1120print_sample(const char *function, const float *rgba)1121{1122debug_printf("%s %g %g %g %g\n",1123function,1124rgba[0], rgba[TGSI_NUM_CHANNELS], rgba[2*TGSI_NUM_CHANNELS], rgba[3*TGSI_NUM_CHANNELS]);1125}112611271128static void1129print_sample_4(const char *function, float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])1130{1131debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",1132function,1133rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],1134rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],1135rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],1136rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);1137}113811391140/* Some image-filter fastpaths:1141*/1142static inline void1143img_filter_2d_linear_repeat_POT(const struct sp_sampler_view *sp_sview,1144const struct sp_sampler *sp_samp,1145const struct img_filter_args *args,1146float *rgba)1147{1148const unsigned xpot = pot_level_size(sp_sview->xpot, args->level);1149const unsigned ypot = pot_level_size(sp_sview->ypot, args->level);1150const int xmax = (xpot - 1) & (TEX_TILE_SIZE - 1); /* MIN2(TEX_TILE_SIZE, xpot) - 1; */1151const int ymax = (ypot - 1) & (TEX_TILE_SIZE - 1); /* MIN2(TEX_TILE_SIZE, ypot) - 1; */1152union tex_tile_address addr;1153int c;11541155const float u = (args->s * xpot - 0.5F) + args->offset[0];1156const float v = (args->t * ypot - 0.5F) + args->offset[1];11571158const int uflr = util_ifloor(u);1159const int vflr = util_ifloor(v);11601161const float xw = u - (float)uflr;1162const float yw = v - (float)vflr;11631164const int x0 = uflr & (xpot - 1);1165const int y0 = vflr & (ypot - 1);11661167const float *tx[4];11681169addr.value = 0;1170addr.bits.level = args->level;1171addr.bits.z = sp_sview->base.u.tex.first_layer;11721173/* Can we fetch all four at once:1174*/1175if (x0 < xmax && y0 < ymax) {1176get_texel_quad_2d_no_border_single_tile(sp_sview, addr, x0, y0, tx);1177}1178else {1179const unsigned x1 = (x0 + 1) & (xpot - 1);1180const unsigned y1 = (y0 + 1) & (ypot - 1);1181get_texel_quad_2d_no_border(sp_sview, addr, x0, y0, x1, y1, tx);1182}11831184/* interpolate R, G, B, A */1185for (c = 0; c < TGSI_NUM_CHANNELS; c++) {1186rgba[TGSI_NUM_CHANNELS*c] = lerp_2d(xw, yw,1187tx[0][c], tx[1][c],1188tx[2][c], tx[3][c]);1189}11901191if (DEBUG_TEX) {1192print_sample(__FUNCTION__, rgba);1193}1194}119511961197static inline void1198img_filter_2d_nearest_repeat_POT(const struct sp_sampler_view *sp_sview,1199const struct sp_sampler *sp_samp,1200const struct img_filter_args *args,1201float *rgba)1202{1203const unsigned xpot = pot_level_size(sp_sview->xpot, args->level);1204const unsigned ypot = pot_level_size(sp_sview->ypot, args->level);1205const float *out;1206union tex_tile_address addr;1207int c;12081209const float u = args->s * xpot + args->offset[0];1210const float v = args->t * ypot + args->offset[1];12111212const int uflr = util_ifloor(u);1213const int vflr = util_ifloor(v);12141215const int x0 = uflr & (xpot - 1);1216const int y0 = vflr & (ypot - 1);12171218addr.value = 0;1219addr.bits.level = args->level;1220addr.bits.z = sp_sview->base.u.tex.first_layer;12211222out = get_texel_2d_no_border(sp_sview, addr, x0, y0);1223for (c = 0; c < TGSI_NUM_CHANNELS; c++)1224rgba[TGSI_NUM_CHANNELS*c] = out[c];12251226if (DEBUG_TEX) {1227print_sample(__FUNCTION__, rgba);1228}1229}123012311232static inline void1233img_filter_2d_nearest_clamp_POT(const struct sp_sampler_view *sp_sview,1234const struct sp_sampler *sp_samp,1235const struct img_filter_args *args,1236float *rgba)1237{1238const unsigned xpot = pot_level_size(sp_sview->xpot, args->level);1239const unsigned ypot = pot_level_size(sp_sview->ypot, args->level);1240union tex_tile_address addr;1241int c;12421243const float u = args->s * xpot + args->offset[0];1244const float v = args->t * ypot + args->offset[1];12451246int x0, y0;1247const float *out;12481249addr.value = 0;1250addr.bits.level = args->level;1251addr.bits.z = sp_sview->base.u.tex.first_layer;12521253x0 = util_ifloor(u);1254if (x0 < 0)1255x0 = 0;1256else if (x0 > (int) xpot - 1)1257x0 = xpot - 1;12581259y0 = util_ifloor(v);1260if (y0 < 0)1261y0 = 0;1262else if (y0 > (int) ypot - 1)1263y0 = ypot - 1;12641265out = get_texel_2d_no_border(sp_sview, addr, x0, y0);1266for (c = 0; c < TGSI_NUM_CHANNELS; c++)1267rgba[TGSI_NUM_CHANNELS*c] = out[c];12681269if (DEBUG_TEX) {1270print_sample(__FUNCTION__, rgba);1271}1272}127312741275static void1276img_filter_1d_nearest(const struct sp_sampler_view *sp_sview,1277const struct sp_sampler *sp_samp,1278const struct img_filter_args *args,1279float *rgba)1280{1281const struct pipe_resource *texture = sp_sview->base.texture;1282const int width = u_minify(texture->width0, args->level);1283int x;1284union tex_tile_address addr;1285const float *out;1286int c;12871288assert(width > 0);12891290addr.value = 0;1291addr.bits.level = args->level;12921293sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);12941295out = get_texel_1d_array(sp_sview, sp_samp, addr, x,1296sp_sview->base.u.tex.first_layer);1297for (c = 0; c < TGSI_NUM_CHANNELS; c++)1298rgba[TGSI_NUM_CHANNELS*c] = out[c];12991300if (DEBUG_TEX) {1301print_sample(__FUNCTION__, rgba);1302}1303}130413051306static void1307img_filter_1d_array_nearest(const struct sp_sampler_view *sp_sview,1308const struct sp_sampler *sp_samp,1309const struct img_filter_args *args,1310float *rgba)1311{1312const struct pipe_resource *texture = sp_sview->base.texture;1313const int width = u_minify(texture->width0, args->level);1314const int layer = coord_to_layer(args->t, sp_sview->base.u.tex.first_layer,1315sp_sview->base.u.tex.last_layer);1316int x;1317union tex_tile_address addr;1318const float *out;1319int c;13201321assert(width > 0);13221323addr.value = 0;1324addr.bits.level = args->level;13251326sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);13271328out = get_texel_1d_array(sp_sview, sp_samp, addr, x, layer);1329for (c = 0; c < TGSI_NUM_CHANNELS; c++)1330rgba[TGSI_NUM_CHANNELS*c] = out[c];13311332if (DEBUG_TEX) {1333print_sample(__FUNCTION__, rgba);1334}1335}133613371338static void1339img_filter_2d_nearest(const struct sp_sampler_view *sp_sview,1340const struct sp_sampler *sp_samp,1341const struct img_filter_args *args,1342float *rgba)1343{1344const struct pipe_resource *texture = sp_sview->base.texture;1345const int width = u_minify(texture->width0, args->level);1346const int height = u_minify(texture->height0, args->level);1347int x, y;1348union tex_tile_address addr;1349const float *out;1350int c;13511352assert(width > 0);1353assert(height > 0);13541355addr.value = 0;1356addr.bits.level = args->level;1357addr.bits.z = sp_sview->base.u.tex.first_layer;13581359sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);1360sp_samp->nearest_texcoord_t(args->t, height, args->offset[1], &y);13611362out = get_texel_2d(sp_sview, sp_samp, addr, x, y);1363for (c = 0; c < TGSI_NUM_CHANNELS; c++)1364rgba[TGSI_NUM_CHANNELS*c] = out[c];13651366if (DEBUG_TEX) {1367print_sample(__FUNCTION__, rgba);1368}1369}137013711372static void1373img_filter_2d_array_nearest(const struct sp_sampler_view *sp_sview,1374const struct sp_sampler *sp_samp,1375const struct img_filter_args *args,1376float *rgba)1377{1378const struct pipe_resource *texture = sp_sview->base.texture;1379const int width = u_minify(texture->width0, args->level);1380const int height = u_minify(texture->height0, args->level);1381const int layer = coord_to_layer(args->p, sp_sview->base.u.tex.first_layer,1382sp_sview->base.u.tex.last_layer);1383int x, y;1384union tex_tile_address addr;1385const float *out;1386int c;13871388assert(width > 0);1389assert(height > 0);13901391addr.value = 0;1392addr.bits.level = args->level;13931394sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);1395sp_samp->nearest_texcoord_t(args->t, height, args->offset[1], &y);13961397out = get_texel_2d_array(sp_sview, sp_samp, addr, x, y, layer);1398for (c = 0; c < TGSI_NUM_CHANNELS; c++)1399rgba[TGSI_NUM_CHANNELS*c] = out[c];14001401if (DEBUG_TEX) {1402print_sample(__FUNCTION__, rgba);1403}1404}140514061407static void1408img_filter_cube_nearest(const struct sp_sampler_view *sp_sview,1409const struct sp_sampler *sp_samp,1410const struct img_filter_args *args,1411float *rgba)1412{1413const struct pipe_resource *texture = sp_sview->base.texture;1414const int width = u_minify(texture->width0, args->level);1415const int height = u_minify(texture->height0, args->level);1416const int layerface = args->face_id + sp_sview->base.u.tex.first_layer;1417int x, y;1418union tex_tile_address addr;1419const float *out;1420int c;14211422assert(width > 0);1423assert(height > 0);14241425addr.value = 0;1426addr.bits.level = args->level;14271428/*1429* If NEAREST filtering is done within a miplevel, always apply wrap1430* mode CLAMP_TO_EDGE.1431*/1432if (sp_samp->base.seamless_cube_map) {1433wrap_nearest_clamp_to_edge(args->s, width, args->offset[0], &x);1434wrap_nearest_clamp_to_edge(args->t, height, args->offset[1], &y);1435} else {1436/* Would probably make sense to ignore mode and just do edge clamp */1437sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);1438sp_samp->nearest_texcoord_t(args->t, height, args->offset[1], &y);1439}14401441out = get_texel_cube_array(sp_sview, sp_samp, addr, x, y, layerface);1442for (c = 0; c < TGSI_NUM_CHANNELS; c++)1443rgba[TGSI_NUM_CHANNELS*c] = out[c];14441445if (DEBUG_TEX) {1446print_sample(__FUNCTION__, rgba);1447}1448}14491450static void1451img_filter_cube_array_nearest(const struct sp_sampler_view *sp_sview,1452const struct sp_sampler *sp_samp,1453const struct img_filter_args *args,1454float *rgba)1455{1456const struct pipe_resource *texture = sp_sview->base.texture;1457const int width = u_minify(texture->width0, args->level);1458const int height = u_minify(texture->height0, args->level);1459const int layerface = CLAMP(6 * util_ifloor(args->p + 0.5f) + sp_sview->base.u.tex.first_layer,1460sp_sview->base.u.tex.first_layer,1461sp_sview->base.u.tex.last_layer - 5) + args->face_id;1462int x, y;1463union tex_tile_address addr;1464const float *out;1465int c;14661467assert(width > 0);1468assert(height > 0);14691470addr.value = 0;1471addr.bits.level = args->level;14721473sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);1474sp_samp->nearest_texcoord_t(args->t, height, args->offset[1], &y);14751476out = get_texel_cube_array(sp_sview, sp_samp, addr, x, y, layerface);1477for (c = 0; c < TGSI_NUM_CHANNELS; c++)1478rgba[TGSI_NUM_CHANNELS*c] = out[c];14791480if (DEBUG_TEX) {1481print_sample(__FUNCTION__, rgba);1482}1483}14841485static void1486img_filter_3d_nearest(const struct sp_sampler_view *sp_sview,1487const struct sp_sampler *sp_samp,1488const struct img_filter_args *args,1489float *rgba)1490{1491const struct pipe_resource *texture = sp_sview->base.texture;1492const int width = u_minify(texture->width0, args->level);1493const int height = u_minify(texture->height0, args->level);1494const int depth = u_minify(texture->depth0, args->level);1495int x, y, z;1496union tex_tile_address addr;1497const float *out;1498int c;14991500assert(width > 0);1501assert(height > 0);1502assert(depth > 0);15031504sp_samp->nearest_texcoord_s(args->s, width, args->offset[0], &x);1505sp_samp->nearest_texcoord_t(args->t, height, args->offset[1], &y);1506sp_samp->nearest_texcoord_p(args->p, depth, args->offset[2], &z);15071508addr.value = 0;1509addr.bits.level = args->level;15101511out = get_texel_3d(sp_sview, sp_samp, addr, x, y, z);1512for (c = 0; c < TGSI_NUM_CHANNELS; c++)1513rgba[TGSI_NUM_CHANNELS*c] = out[c];1514}151515161517static void1518img_filter_1d_linear(const struct sp_sampler_view *sp_sview,1519const struct sp_sampler *sp_samp,1520const struct img_filter_args *args,1521float *rgba)1522{1523const struct pipe_resource *texture = sp_sview->base.texture;1524const int width = u_minify(texture->width0, args->level);1525int x0, x1;1526float xw; /* weights */1527union tex_tile_address addr;1528const float *tx0, *tx1;1529int c;15301531assert(width > 0);15321533addr.value = 0;1534addr.bits.level = args->level;15351536sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);15371538tx0 = get_texel_1d_array(sp_sview, sp_samp, addr, x0,1539sp_sview->base.u.tex.first_layer);1540tx1 = get_texel_1d_array(sp_sview, sp_samp, addr, x1,1541sp_sview->base.u.tex.first_layer);15421543/* interpolate R, G, B, A */1544for (c = 0; c < TGSI_NUM_CHANNELS; c++)1545rgba[TGSI_NUM_CHANNELS*c] = lerp(xw, tx0[c], tx1[c]);1546}154715481549static void1550img_filter_1d_array_linear(const struct sp_sampler_view *sp_sview,1551const struct sp_sampler *sp_samp,1552const struct img_filter_args *args,1553float *rgba)1554{1555const struct pipe_resource *texture = sp_sview->base.texture;1556const int width = u_minify(texture->width0, args->level);1557const int layer = coord_to_layer(args->t, sp_sview->base.u.tex.first_layer,1558sp_sview->base.u.tex.last_layer);1559int x0, x1;1560float xw; /* weights */1561union tex_tile_address addr;1562const float *tx0, *tx1;1563int c;15641565assert(width > 0);15661567addr.value = 0;1568addr.bits.level = args->level;15691570sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);15711572tx0 = get_texel_1d_array(sp_sview, sp_samp, addr, x0, layer);1573tx1 = get_texel_1d_array(sp_sview, sp_samp, addr, x1, layer);15741575/* interpolate R, G, B, A */1576for (c = 0; c < TGSI_NUM_CHANNELS; c++)1577rgba[TGSI_NUM_CHANNELS*c] = lerp(xw, tx0[c], tx1[c]);1578}15791580/*1581* Retrieve the gathered value, need to convert to the1582* TGSI expected interface, and take component select1583* and swizzling into account.1584*/1585static float1586get_gather_value(const struct sp_sampler_view *sp_sview,1587int chan_in, int comp_sel,1588const float *tx[4])1589{1590int chan;1591unsigned swizzle;15921593/*1594* softpipe samples in a different order1595* to TGSI expects, so we need to swizzle,1596* the samples into the correct slots.1597*/1598switch (chan_in) {1599case 0:1600chan = 2;1601break;1602case 1:1603chan = 3;1604break;1605case 2:1606chan = 1;1607break;1608case 3:1609chan = 0;1610break;1611default:1612assert(0);1613return 0.0;1614}16151616/* pick which component to use for the swizzle */1617switch (comp_sel) {1618case 0:1619swizzle = sp_sview->base.swizzle_r;1620break;1621case 1:1622swizzle = sp_sview->base.swizzle_g;1623break;1624case 2:1625swizzle = sp_sview->base.swizzle_b;1626break;1627case 3:1628swizzle = sp_sview->base.swizzle_a;1629break;1630default:1631assert(0);1632return 0.0;1633}16341635/* get correct result using the channel and swizzle */1636switch (swizzle) {1637case PIPE_SWIZZLE_0:1638return 0.0;1639case PIPE_SWIZZLE_1:1640return sp_sview->oneval;1641default:1642return tx[chan][swizzle];1643}1644}164516461647static void1648img_filter_2d_linear(const struct sp_sampler_view *sp_sview,1649const struct sp_sampler *sp_samp,1650const struct img_filter_args *args,1651float *rgba)1652{1653const struct pipe_resource *texture = sp_sview->base.texture;1654const int width = u_minify(texture->width0, args->level);1655const int height = u_minify(texture->height0, args->level);1656int x0, y0, x1, y1;1657float xw, yw; /* weights */1658union tex_tile_address addr;1659const float *tx[4];1660int c;16611662assert(width > 0);1663assert(height > 0);16641665addr.value = 0;1666addr.bits.level = args->level;1667addr.bits.z = sp_sview->base.u.tex.first_layer;16681669sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);1670sp_samp->linear_texcoord_t(args->t, height, args->offset[1], &y0, &y1, &yw);16711672tx[0] = get_texel_2d(sp_sview, sp_samp, addr, x0, y0);1673tx[1] = get_texel_2d(sp_sview, sp_samp, addr, x1, y0);1674tx[2] = get_texel_2d(sp_sview, sp_samp, addr, x0, y1);1675tx[3] = get_texel_2d(sp_sview, sp_samp, addr, x1, y1);16761677if (args->gather_only) {1678for (c = 0; c < TGSI_NUM_CHANNELS; c++)1679rgba[TGSI_NUM_CHANNELS*c] = get_gather_value(sp_sview, c,1680args->gather_comp,1681tx);1682} else {1683/* interpolate R, G, B, A */1684for (c = 0; c < TGSI_NUM_CHANNELS; c++)1685rgba[TGSI_NUM_CHANNELS*c] = lerp_2d(xw, yw,1686tx[0][c], tx[1][c],1687tx[2][c], tx[3][c]);1688}1689}169016911692static void1693img_filter_2d_array_linear(const struct sp_sampler_view *sp_sview,1694const struct sp_sampler *sp_samp,1695const struct img_filter_args *args,1696float *rgba)1697{1698const struct pipe_resource *texture = sp_sview->base.texture;1699const int width = u_minify(texture->width0, args->level);1700const int height = u_minify(texture->height0, args->level);1701const int layer = coord_to_layer(args->p, sp_sview->base.u.tex.first_layer,1702sp_sview->base.u.tex.last_layer);1703int x0, y0, x1, y1;1704float xw, yw; /* weights */1705union tex_tile_address addr;1706const float *tx[4];1707int c;17081709assert(width > 0);1710assert(height > 0);17111712addr.value = 0;1713addr.bits.level = args->level;17141715sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);1716sp_samp->linear_texcoord_t(args->t, height, args->offset[1], &y0, &y1, &yw);17171718tx[0] = get_texel_2d_array(sp_sview, sp_samp, addr, x0, y0, layer);1719tx[1] = get_texel_2d_array(sp_sview, sp_samp, addr, x1, y0, layer);1720tx[2] = get_texel_2d_array(sp_sview, sp_samp, addr, x0, y1, layer);1721tx[3] = get_texel_2d_array(sp_sview, sp_samp, addr, x1, y1, layer);17221723if (args->gather_only) {1724for (c = 0; c < TGSI_NUM_CHANNELS; c++)1725rgba[TGSI_NUM_CHANNELS*c] = get_gather_value(sp_sview, c,1726args->gather_comp,1727tx);1728} else {1729/* interpolate R, G, B, A */1730for (c = 0; c < TGSI_NUM_CHANNELS; c++)1731rgba[TGSI_NUM_CHANNELS*c] = lerp_2d(xw, yw,1732tx[0][c], tx[1][c],1733tx[2][c], tx[3][c]);1734}1735}173617371738static void1739img_filter_cube_linear(const struct sp_sampler_view *sp_sview,1740const struct sp_sampler *sp_samp,1741const struct img_filter_args *args,1742float *rgba)1743{1744const struct pipe_resource *texture = sp_sview->base.texture;1745const int width = u_minify(texture->width0, args->level);1746const int height = u_minify(texture->height0, args->level);1747const int layer = sp_sview->base.u.tex.first_layer;1748int x0, y0, x1, y1;1749float xw, yw; /* weights */1750union tex_tile_address addr;1751const float *tx[4];1752float corner0[TGSI_QUAD_SIZE], corner1[TGSI_QUAD_SIZE],1753corner2[TGSI_QUAD_SIZE], corner3[TGSI_QUAD_SIZE];1754int c;17551756assert(width > 0);1757assert(height > 0);17581759addr.value = 0;1760addr.bits.level = args->level;17611762/*1763* For seamless if LINEAR filtering is done within a miplevel,1764* always apply wrap mode CLAMP_TO_BORDER.1765*/1766if (sp_samp->base.seamless_cube_map) {1767/* Note this is a bit overkill, actual clamping is not required */1768wrap_linear_clamp_to_border(args->s, width, args->offset[0], &x0, &x1, &xw);1769wrap_linear_clamp_to_border(args->t, height, args->offset[1], &y0, &y1, &yw);1770} else {1771/* Would probably make sense to ignore mode and just do edge clamp */1772sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);1773sp_samp->linear_texcoord_t(args->t, height, args->offset[1], &y0, &y1, &yw);1774}17751776if (sp_samp->base.seamless_cube_map) {1777tx[0] = get_texel_cube_seamless(sp_sview, addr, x0, y0, corner0, layer, args->face_id);1778tx[1] = get_texel_cube_seamless(sp_sview, addr, x1, y0, corner1, layer, args->face_id);1779tx[2] = get_texel_cube_seamless(sp_sview, addr, x0, y1, corner2, layer, args->face_id);1780tx[3] = get_texel_cube_seamless(sp_sview, addr, x1, y1, corner3, layer, args->face_id);1781} else {1782tx[0] = get_texel_cube_array(sp_sview, sp_samp, addr, x0, y0, layer + args->face_id);1783tx[1] = get_texel_cube_array(sp_sview, sp_samp, addr, x1, y0, layer + args->face_id);1784tx[2] = get_texel_cube_array(sp_sview, sp_samp, addr, x0, y1, layer + args->face_id);1785tx[3] = get_texel_cube_array(sp_sview, sp_samp, addr, x1, y1, layer + args->face_id);1786}17871788if (args->gather_only) {1789for (c = 0; c < TGSI_NUM_CHANNELS; c++)1790rgba[TGSI_NUM_CHANNELS*c] = get_gather_value(sp_sview, c,1791args->gather_comp,1792tx);1793} else {1794/* interpolate R, G, B, A */1795for (c = 0; c < TGSI_NUM_CHANNELS; c++)1796rgba[TGSI_NUM_CHANNELS*c] = lerp_2d(xw, yw,1797tx[0][c], tx[1][c],1798tx[2][c], tx[3][c]);1799}1800}180118021803static void1804img_filter_cube_array_linear(const struct sp_sampler_view *sp_sview,1805const struct sp_sampler *sp_samp,1806const struct img_filter_args *args,1807float *rgba)1808{1809const struct pipe_resource *texture = sp_sview->base.texture;1810const int width = u_minify(texture->width0, args->level);1811const int height = u_minify(texture->height0, args->level);18121813const int layer = CLAMP(6 * util_ifloor(args->p + 0.5f) + sp_sview->base.u.tex.first_layer,1814sp_sview->base.u.tex.first_layer,1815sp_sview->base.u.tex.last_layer - 5);18161817int x0, y0, x1, y1;1818float xw, yw; /* weights */1819union tex_tile_address addr;1820const float *tx[4];1821float corner0[TGSI_QUAD_SIZE], corner1[TGSI_QUAD_SIZE],1822corner2[TGSI_QUAD_SIZE], corner3[TGSI_QUAD_SIZE];1823int c;18241825assert(width > 0);1826assert(height > 0);18271828addr.value = 0;1829addr.bits.level = args->level;18301831/*1832* For seamless if LINEAR filtering is done within a miplevel,1833* always apply wrap mode CLAMP_TO_BORDER.1834*/1835if (sp_samp->base.seamless_cube_map) {1836/* Note this is a bit overkill, actual clamping is not required */1837wrap_linear_clamp_to_border(args->s, width, args->offset[0], &x0, &x1, &xw);1838wrap_linear_clamp_to_border(args->t, height, args->offset[1], &y0, &y1, &yw);1839} else {1840/* Would probably make sense to ignore mode and just do edge clamp */1841sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);1842sp_samp->linear_texcoord_t(args->t, height, args->offset[1], &y0, &y1, &yw);1843}18441845if (sp_samp->base.seamless_cube_map) {1846tx[0] = get_texel_cube_seamless(sp_sview, addr, x0, y0, corner0, layer, args->face_id);1847tx[1] = get_texel_cube_seamless(sp_sview, addr, x1, y0, corner1, layer, args->face_id);1848tx[2] = get_texel_cube_seamless(sp_sview, addr, x0, y1, corner2, layer, args->face_id);1849tx[3] = get_texel_cube_seamless(sp_sview, addr, x1, y1, corner3, layer, args->face_id);1850} else {1851tx[0] = get_texel_cube_array(sp_sview, sp_samp, addr, x0, y0, layer + args->face_id);1852tx[1] = get_texel_cube_array(sp_sview, sp_samp, addr, x1, y0, layer + args->face_id);1853tx[2] = get_texel_cube_array(sp_sview, sp_samp, addr, x0, y1, layer + args->face_id);1854tx[3] = get_texel_cube_array(sp_sview, sp_samp, addr, x1, y1, layer + args->face_id);1855}18561857if (args->gather_only) {1858for (c = 0; c < TGSI_NUM_CHANNELS; c++)1859rgba[TGSI_NUM_CHANNELS*c] = get_gather_value(sp_sview, c,1860args->gather_comp,1861tx);1862} else {1863/* interpolate R, G, B, A */1864for (c = 0; c < TGSI_NUM_CHANNELS; c++)1865rgba[TGSI_NUM_CHANNELS*c] = lerp_2d(xw, yw,1866tx[0][c], tx[1][c],1867tx[2][c], tx[3][c]);1868}1869}18701871static void1872img_filter_3d_linear(const struct sp_sampler_view *sp_sview,1873const struct sp_sampler *sp_samp,1874const struct img_filter_args *args,1875float *rgba)1876{1877const struct pipe_resource *texture = sp_sview->base.texture;1878const int width = u_minify(texture->width0, args->level);1879const int height = u_minify(texture->height0, args->level);1880const int depth = u_minify(texture->depth0, args->level);1881int x0, x1, y0, y1, z0, z1;1882float xw, yw, zw; /* interpolation weights */1883union tex_tile_address addr;1884const float *tx00, *tx01, *tx02, *tx03, *tx10, *tx11, *tx12, *tx13;1885int c;18861887addr.value = 0;1888addr.bits.level = args->level;18891890assert(width > 0);1891assert(height > 0);1892assert(depth > 0);18931894sp_samp->linear_texcoord_s(args->s, width, args->offset[0], &x0, &x1, &xw);1895sp_samp->linear_texcoord_t(args->t, height, args->offset[1], &y0, &y1, &yw);1896sp_samp->linear_texcoord_p(args->p, depth, args->offset[2], &z0, &z1, &zw);18971898tx00 = get_texel_3d(sp_sview, sp_samp, addr, x0, y0, z0);1899tx01 = get_texel_3d(sp_sview, sp_samp, addr, x1, y0, z0);1900tx02 = get_texel_3d(sp_sview, sp_samp, addr, x0, y1, z0);1901tx03 = get_texel_3d(sp_sview, sp_samp, addr, x1, y1, z0);19021903tx10 = get_texel_3d(sp_sview, sp_samp, addr, x0, y0, z1);1904tx11 = get_texel_3d(sp_sview, sp_samp, addr, x1, y0, z1);1905tx12 = get_texel_3d(sp_sview, sp_samp, addr, x0, y1, z1);1906tx13 = get_texel_3d(sp_sview, sp_samp, addr, x1, y1, z1);19071908/* interpolate R, G, B, A */1909for (c = 0; c < TGSI_NUM_CHANNELS; c++)1910rgba[TGSI_NUM_CHANNELS*c] = lerp_3d(xw, yw, zw,1911tx00[c], tx01[c],1912tx02[c], tx03[c],1913tx10[c], tx11[c],1914tx12[c], tx13[c]);1915}191619171918/* Calculate level of detail for every fragment,1919* with lambda already computed.1920* Note that lambda has already been biased by global LOD bias.1921* \param biased_lambda per-quad lambda.1922* \param lod_in per-fragment lod_bias or explicit_lod.1923* \param lod returns the per-fragment lod.1924*/1925static inline void1926compute_lod(const struct pipe_sampler_state *sampler,1927enum tgsi_sampler_control control,1928const float biased_lambda,1929const float lod_in[TGSI_QUAD_SIZE],1930float lod[TGSI_QUAD_SIZE])1931{1932const float min_lod = sampler->min_lod;1933const float max_lod = sampler->max_lod;1934uint i;19351936switch (control) {1937case TGSI_SAMPLER_LOD_NONE:1938case TGSI_SAMPLER_LOD_ZERO:1939lod[0] = lod[1] = lod[2] = lod[3] = CLAMP(biased_lambda, min_lod, max_lod);1940break;1941case TGSI_SAMPLER_DERIVS_EXPLICIT:1942for (i = 0; i < TGSI_QUAD_SIZE; i++)1943lod[i] = lod_in[i];1944break;1945case TGSI_SAMPLER_LOD_BIAS:1946for (i = 0; i < TGSI_QUAD_SIZE; i++) {1947lod[i] = biased_lambda + lod_in[i];1948lod[i] = CLAMP(lod[i], min_lod, max_lod);1949}1950break;1951case TGSI_SAMPLER_LOD_EXPLICIT:1952for (i = 0; i < TGSI_QUAD_SIZE; i++) {1953lod[i] = CLAMP(lod_in[i], min_lod, max_lod);1954}1955break;1956default:1957assert(0);1958lod[0] = lod[1] = lod[2] = lod[3] = 0.0f;1959}1960}196119621963/* Calculate level of detail for every fragment. The computed value is not1964* clamped to lod_min and lod_max.1965* \param lod_in per-fragment lod_bias or explicit_lod.1966* \param lod results per-fragment lod.1967*/1968static inline void1969compute_lambda_lod_unclamped(const struct sp_sampler_view *sp_sview,1970const struct sp_sampler *sp_samp,1971const float s[TGSI_QUAD_SIZE],1972const float t[TGSI_QUAD_SIZE],1973const float p[TGSI_QUAD_SIZE],1974const float derivs[3][2][TGSI_QUAD_SIZE],1975const float lod_in[TGSI_QUAD_SIZE],1976enum tgsi_sampler_control control,1977float lod[TGSI_QUAD_SIZE])1978{1979const struct pipe_sampler_state *sampler = &sp_samp->base;1980const float lod_bias = sampler->lod_bias;1981float lambda;1982uint i;19831984switch (control) {1985case TGSI_SAMPLER_LOD_NONE:1986lambda = sp_sview->compute_lambda(sp_sview, s, t, p) + lod_bias;1987lod[0] = lod[1] = lod[2] = lod[3] = lambda;1988break;1989case TGSI_SAMPLER_DERIVS_EXPLICIT:1990for (i = 0; i < TGSI_QUAD_SIZE; i++)1991lod[i] = sp_sview->compute_lambda_from_grad(sp_sview, derivs, i);1992break;1993case TGSI_SAMPLER_LOD_BIAS:1994lambda = sp_sview->compute_lambda(sp_sview, s, t, p) + lod_bias;1995for (i = 0; i < TGSI_QUAD_SIZE; i++) {1996lod[i] = lambda + lod_in[i];1997}1998break;1999case TGSI_SAMPLER_LOD_EXPLICIT:2000for (i = 0; i < TGSI_QUAD_SIZE; i++) {2001lod[i] = lod_in[i] + lod_bias;2002}2003break;2004case TGSI_SAMPLER_LOD_ZERO:2005case TGSI_SAMPLER_GATHER:2006lod[0] = lod[1] = lod[2] = lod[3] = lod_bias;2007break;2008default:2009assert(0);2010lod[0] = lod[1] = lod[2] = lod[3] = 0.0f;2011}2012}20132014/* Calculate level of detail for every fragment.2015* \param lod_in per-fragment lod_bias or explicit_lod.2016* \param lod results per-fragment lod.2017*/2018static inline void2019compute_lambda_lod(const struct sp_sampler_view *sp_sview,2020const struct sp_sampler *sp_samp,2021const float s[TGSI_QUAD_SIZE],2022const float t[TGSI_QUAD_SIZE],2023const float p[TGSI_QUAD_SIZE],2024float derivs[3][2][TGSI_QUAD_SIZE],2025const float lod_in[TGSI_QUAD_SIZE],2026enum tgsi_sampler_control control,2027float lod[TGSI_QUAD_SIZE])2028{2029const struct pipe_sampler_state *sampler = &sp_samp->base;2030const float min_lod = sampler->min_lod;2031const float max_lod = sampler->max_lod;2032int i;20332034compute_lambda_lod_unclamped(sp_sview, sp_samp,2035s, t, p, derivs, lod_in, control, lod);2036for (i = 0; i < TGSI_QUAD_SIZE; i++) {2037lod[i] = CLAMP(lod[i], min_lod, max_lod);2038}2039}20402041static inline unsigned2042get_gather_component(const float lod_in[TGSI_QUAD_SIZE])2043{2044/* gather component is stored in lod_in slot as unsigned */2045return (*(unsigned int *)lod_in) & 0x3;2046}20472048/**2049* Clamps given lod to both lod limits and mip level limits. Clamping to the2050* latter limits is done so that lod is relative to the first (base) level.2051*/2052static void2053clamp_lod(const struct sp_sampler_view *sp_sview,2054const struct sp_sampler *sp_samp,2055const float lod[TGSI_QUAD_SIZE],2056float clamped[TGSI_QUAD_SIZE])2057{2058const float min_lod = sp_samp->base.min_lod;2059const float max_lod = sp_samp->base.max_lod;2060const float min_level = sp_sview->base.u.tex.first_level;2061const float max_level = sp_sview->base.u.tex.last_level;2062int i;20632064for (i = 0; i < TGSI_QUAD_SIZE; i++) {2065float cl = lod[i];20662067cl = CLAMP(cl, min_lod, max_lod);2068cl = CLAMP(cl, 0, max_level - min_level);2069clamped[i] = cl;2070}2071}20722073/**2074* Get mip level relative to base level for linear mip filter2075*/2076static void2077mip_rel_level_linear(const struct sp_sampler_view *sp_sview,2078const struct sp_sampler *sp_samp,2079const float lod[TGSI_QUAD_SIZE],2080float level[TGSI_QUAD_SIZE])2081{2082clamp_lod(sp_sview, sp_samp, lod, level);2083}20842085static void2086mip_filter_linear(const struct sp_sampler_view *sp_sview,2087const struct sp_sampler *sp_samp,2088img_filter_func min_filter,2089img_filter_func mag_filter,2090const float s[TGSI_QUAD_SIZE],2091const float t[TGSI_QUAD_SIZE],2092const float p[TGSI_QUAD_SIZE],2093int gather_comp,2094const float lod[TGSI_QUAD_SIZE],2095const struct filter_args *filt_args,2096float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2097{2098const struct pipe_sampler_view *psview = &sp_sview->base;2099int j;2100struct img_filter_args args;21012102args.offset = filt_args->offset;2103args.gather_only = filt_args->control == TGSI_SAMPLER_GATHER;2104args.gather_comp = gather_comp;21052106for (j = 0; j < TGSI_QUAD_SIZE; j++) {2107const int level0 = psview->u.tex.first_level + (int)lod[j];21082109args.s = s[j];2110args.t = t[j];2111args.p = p[j];2112args.face_id = filt_args->faces[j];21132114if (lod[j] <= 0.0 && !args.gather_only) {2115args.level = psview->u.tex.first_level;2116mag_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2117}2118else if (level0 >= (int) psview->u.tex.last_level) {2119args.level = psview->u.tex.last_level;2120min_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2121}2122else {2123float levelBlend = frac(lod[j]);2124float rgbax[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];2125int c;21262127args.level = level0;2128min_filter(sp_sview, sp_samp, &args, &rgbax[0][0]);2129args.level = level0+1;2130min_filter(sp_sview, sp_samp, &args, &rgbax[0][1]);21312132for (c = 0; c < 4; c++) {2133rgba[c][j] = lerp(levelBlend, rgbax[c][0], rgbax[c][1]);2134}2135}2136}21372138if (DEBUG_TEX) {2139print_sample_4(__FUNCTION__, rgba);2140}2141}214221432144/**2145* Get mip level relative to base level for nearest mip filter2146*/2147static void2148mip_rel_level_nearest(const struct sp_sampler_view *sp_sview,2149const struct sp_sampler *sp_samp,2150const float lod[TGSI_QUAD_SIZE],2151float level[TGSI_QUAD_SIZE])2152{2153int j;21542155clamp_lod(sp_sview, sp_samp, lod, level);2156for (j = 0; j < TGSI_QUAD_SIZE; j++)2157/* TODO: It should rather be:2158* level[j] = ceil(level[j] + 0.5F) - 1.0F;2159*/2160level[j] = (int)(level[j] + 0.5F);2161}21622163/**2164* Compute nearest mipmap level from texcoords.2165* Then sample the texture level for four elements of a quad.2166* \param c0 the LOD bias factors, or absolute LODs (depending on control)2167*/2168static void2169mip_filter_nearest(const struct sp_sampler_view *sp_sview,2170const struct sp_sampler *sp_samp,2171img_filter_func min_filter,2172img_filter_func mag_filter,2173const float s[TGSI_QUAD_SIZE],2174const float t[TGSI_QUAD_SIZE],2175const float p[TGSI_QUAD_SIZE],2176int gather_component,2177const float lod[TGSI_QUAD_SIZE],2178const struct filter_args *filt_args,2179float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2180{2181const struct pipe_sampler_view *psview = &sp_sview->base;2182int j;2183struct img_filter_args args;21842185args.offset = filt_args->offset;2186args.gather_only = filt_args->control == TGSI_SAMPLER_GATHER;2187args.gather_comp = gather_component;21882189for (j = 0; j < TGSI_QUAD_SIZE; j++) {2190args.s = s[j];2191args.t = t[j];2192args.p = p[j];2193args.face_id = filt_args->faces[j];21942195if (lod[j] <= 0.0f && !args.gather_only) {2196args.level = psview->u.tex.first_level;2197mag_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2198} else {2199const int level = psview->u.tex.first_level + (int)(lod[j] + 0.5F);2200args.level = MIN2(level, (int)psview->u.tex.last_level);2201min_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2202}2203}22042205if (DEBUG_TEX) {2206print_sample_4(__FUNCTION__, rgba);2207}2208}220922102211/**2212* Get mip level relative to base level for none mip filter2213*/2214static void2215mip_rel_level_none(const struct sp_sampler_view *sp_sview,2216const struct sp_sampler *sp_samp,2217const float lod[TGSI_QUAD_SIZE],2218float level[TGSI_QUAD_SIZE])2219{2220int j;22212222for (j = 0; j < TGSI_QUAD_SIZE; j++) {2223level[j] = 0;2224}2225}22262227static void2228mip_filter_none(const struct sp_sampler_view *sp_sview,2229const struct sp_sampler *sp_samp,2230img_filter_func min_filter,2231img_filter_func mag_filter,2232const float s[TGSI_QUAD_SIZE],2233const float t[TGSI_QUAD_SIZE],2234const float p[TGSI_QUAD_SIZE],2235int gather_component,2236const float lod[TGSI_QUAD_SIZE],2237const struct filter_args *filt_args,2238float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2239{2240int j;2241struct img_filter_args args;22422243args.level = sp_sview->base.u.tex.first_level;2244args.offset = filt_args->offset;2245args.gather_only = filt_args->control == TGSI_SAMPLER_GATHER;2246args.gather_comp = gather_component;22472248for (j = 0; j < TGSI_QUAD_SIZE; j++) {2249args.s = s[j];2250args.t = t[j];2251args.p = p[j];2252args.face_id = filt_args->faces[j];2253if (lod[j] <= 0.0f && !args.gather_only) {2254mag_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2255}2256else {2257min_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2258}2259}2260}226122622263/**2264* Get mip level relative to base level for none mip filter2265*/2266static void2267mip_rel_level_none_no_filter_select(const struct sp_sampler_view *sp_sview,2268const struct sp_sampler *sp_samp,2269const float lod[TGSI_QUAD_SIZE],2270float level[TGSI_QUAD_SIZE])2271{2272mip_rel_level_none(sp_sview, sp_samp, lod, level);2273}22742275static void2276mip_filter_none_no_filter_select(const struct sp_sampler_view *sp_sview,2277const struct sp_sampler *sp_samp,2278img_filter_func min_filter,2279img_filter_func mag_filter,2280const float s[TGSI_QUAD_SIZE],2281const float t[TGSI_QUAD_SIZE],2282const float p[TGSI_QUAD_SIZE],2283int gather_comp,2284const float lod_in[TGSI_QUAD_SIZE],2285const struct filter_args *filt_args,2286float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2287{2288int j;2289struct img_filter_args args;2290args.level = sp_sview->base.u.tex.first_level;2291args.offset = filt_args->offset;2292args.gather_only = filt_args->control == TGSI_SAMPLER_GATHER;2293args.gather_comp = gather_comp;2294for (j = 0; j < TGSI_QUAD_SIZE; j++) {2295args.s = s[j];2296args.t = t[j];2297args.p = p[j];2298args.face_id = filt_args->faces[j];2299mag_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2300}2301}230223032304/* For anisotropic filtering */2305#define WEIGHT_LUT_SIZE 102423062307static const float *weightLut = NULL;23082309/**2310* Creates the look-up table used to speed-up EWA sampling2311*/2312static void2313create_filter_table(void)2314{2315unsigned i;2316if (!weightLut) {2317float *lut = (float *) MALLOC(WEIGHT_LUT_SIZE * sizeof(float));23182319for (i = 0; i < WEIGHT_LUT_SIZE; ++i) {2320const float alpha = 2;2321const float r2 = (float) i / (float) (WEIGHT_LUT_SIZE - 1);2322const float weight = (float) expf(-alpha * r2);2323lut[i] = weight;2324}2325weightLut = lut;2326}2327}232823292330/**2331* Elliptical weighted average (EWA) filter for producing high quality2332* anisotropic filtered results.2333* Based on the Higher Quality Elliptical Weighted Average Filter2334* published by Paul S. Heckbert in his Master's Thesis2335* "Fundamentals of Texture Mapping and Image Warping" (1989)2336*/2337static void2338img_filter_2d_ewa(const struct sp_sampler_view *sp_sview,2339const struct sp_sampler *sp_samp,2340img_filter_func min_filter,2341img_filter_func mag_filter,2342const float s[TGSI_QUAD_SIZE],2343const float t[TGSI_QUAD_SIZE],2344const float p[TGSI_QUAD_SIZE],2345const uint faces[TGSI_QUAD_SIZE],2346const int8_t *offset,2347unsigned level,2348const float dudx, const float dvdx,2349const float dudy, const float dvdy,2350float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2351{2352const struct pipe_resource *texture = sp_sview->base.texture;23532354// ??? Won't the image filters blow up if level is negative?2355const unsigned level0 = level > 0 ? level : 0;2356const float scaling = 1.0f / (1 << level0);2357const int width = u_minify(texture->width0, level0);2358const int height = u_minify(texture->height0, level0);2359struct img_filter_args args;2360const float ux = dudx * scaling;2361const float vx = dvdx * scaling;2362const float uy = dudy * scaling;2363const float vy = dvdy * scaling;23642365/* compute ellipse coefficients to bound the region:2366* A*x*x + B*x*y + C*y*y = F.2367*/2368float A = vx*vx+vy*vy+1;2369float B = -2*(ux*vx+uy*vy);2370float C = ux*ux+uy*uy+1;2371float F = A*C-B*B/4.0f;23722373/* check if it is an ellipse */2374/* assert(F > 0.0); */23752376/* Compute the ellipse's (u,v) bounding box in texture space */2377const float d = -B*B+4.0f*C*A;2378const float box_u = 2.0f / d * sqrtf(d*C*F); /* box_u -> half of bbox with */2379const float box_v = 2.0f / d * sqrtf(A*d*F); /* box_v -> half of bbox height */23802381float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];2382float s_buffer[TGSI_QUAD_SIZE];2383float t_buffer[TGSI_QUAD_SIZE];2384float weight_buffer[TGSI_QUAD_SIZE];2385int j;23862387/* Scale ellipse formula to directly index the Filter Lookup Table.2388* i.e. scale so that F = WEIGHT_LUT_SIZE-12389*/2390const double formScale = (double) (WEIGHT_LUT_SIZE - 1) / F;2391A *= formScale;2392B *= formScale;2393C *= formScale;2394/* F *= formScale; */ /* no need to scale F as we don't use it below here */23952396/* For each quad, the du and dx values are the same and so the ellipse is2397* also the same. Note that texel/image access can only be performed using2398* a quad, i.e. it is not possible to get the pixel value for a single2399* tex coord. In order to have a better performance, the access is buffered2400* using the s_buffer/t_buffer and weight_buffer. Only when the buffer is2401* full, then the pixel values are read from the image.2402*/2403const float ddq = 2 * A;24042405args.level = level;2406args.offset = offset;24072408for (j = 0; j < TGSI_QUAD_SIZE; j++) {2409/* Heckbert MS thesis, p. 59; scan over the bounding box of the ellipse2410* and incrementally update the value of Ax^2+Bxy*Cy^2; when this2411* value, q, is less than F, we're inside the ellipse2412*/2413const float tex_u = -0.5F + s[j] * texture->width0 * scaling;2414const float tex_v = -0.5F + t[j] * texture->height0 * scaling;24152416const int u0 = (int) floorf(tex_u - box_u);2417const int u1 = (int) ceilf(tex_u + box_u);2418const int v0 = (int) floorf(tex_v - box_v);2419const int v1 = (int) ceilf(tex_v + box_v);2420const float U = u0 - tex_u;24212422float num[4] = {0.0F, 0.0F, 0.0F, 0.0F};2423unsigned buffer_next = 0;2424float den = 0;2425int v;2426args.face_id = faces[j];24272428for (v = v0; v <= v1; ++v) {2429const float V = v - tex_v;2430float dq = A * (2 * U + 1) + B * V;2431float q = (C * V + B * U) * V + A * U * U;24322433int u;2434for (u = u0; u <= u1; ++u) {2435/* Note that the ellipse has been pre-scaled so F =2436* WEIGHT_LUT_SIZE - 12437*/2438if (q < WEIGHT_LUT_SIZE) {2439/* as a LUT is used, q must never be negative;2440* should not happen, though2441*/2442const int qClamped = q >= 0.0F ? q : 0;2443const float weight = weightLut[qClamped];24442445weight_buffer[buffer_next] = weight;2446s_buffer[buffer_next] = u / ((float) width);2447t_buffer[buffer_next] = v / ((float) height);24482449buffer_next++;2450if (buffer_next == TGSI_QUAD_SIZE) {2451/* 4 texel coords are in the buffer -> read it now */2452unsigned jj;2453/* it is assumed that samp->min_img_filter is set to2454* img_filter_2d_nearest or one of the2455* accelerated img_filter_2d_nearest_XXX functions.2456*/2457for (jj = 0; jj < buffer_next; jj++) {2458args.s = s_buffer[jj];2459args.t = t_buffer[jj];2460args.p = p[jj];2461min_filter(sp_sview, sp_samp, &args, &rgba_temp[0][jj]);2462num[0] += weight_buffer[jj] * rgba_temp[0][jj];2463num[1] += weight_buffer[jj] * rgba_temp[1][jj];2464num[2] += weight_buffer[jj] * rgba_temp[2][jj];2465num[3] += weight_buffer[jj] * rgba_temp[3][jj];2466}24672468buffer_next = 0;2469}24702471den += weight;2472}2473q += dq;2474dq += ddq;2475}2476}24772478/* if the tex coord buffer contains unread values, we will read2479* them now.2480*/2481if (buffer_next > 0) {2482unsigned jj;2483/* it is assumed that samp->min_img_filter is set to2484* img_filter_2d_nearest or one of the2485* accelerated img_filter_2d_nearest_XXX functions.2486*/2487for (jj = 0; jj < buffer_next; jj++) {2488args.s = s_buffer[jj];2489args.t = t_buffer[jj];2490args.p = p[jj];2491min_filter(sp_sview, sp_samp, &args, &rgba_temp[0][jj]);2492num[0] += weight_buffer[jj] * rgba_temp[0][jj];2493num[1] += weight_buffer[jj] * rgba_temp[1][jj];2494num[2] += weight_buffer[jj] * rgba_temp[2][jj];2495num[3] += weight_buffer[jj] * rgba_temp[3][jj];2496}2497}24982499if (den <= 0.0F) {2500/* Reaching this place would mean that no pixels intersected2501* the ellipse. This should never happen because the filter2502* we use always intersects at least one pixel.2503*/25042505/*rgba[0]=0;2506rgba[1]=0;2507rgba[2]=0;2508rgba[3]=0;*/2509/* not enough pixels in resampling, resort to direct interpolation */2510args.s = s[j];2511args.t = t[j];2512args.p = p[j];2513min_filter(sp_sview, sp_samp, &args, &rgba_temp[0][j]);2514den = 1;2515num[0] = rgba_temp[0][j];2516num[1] = rgba_temp[1][j];2517num[2] = rgba_temp[2][j];2518num[3] = rgba_temp[3][j];2519}25202521rgba[0][j] = num[0] / den;2522rgba[1][j] = num[1] / den;2523rgba[2][j] = num[2] / den;2524rgba[3][j] = num[3] / den;2525}2526}252725282529/**2530* Get mip level relative to base level for linear mip filter2531*/2532static void2533mip_rel_level_linear_aniso(const struct sp_sampler_view *sp_sview,2534const struct sp_sampler *sp_samp,2535const float lod[TGSI_QUAD_SIZE],2536float level[TGSI_QUAD_SIZE])2537{2538mip_rel_level_linear(sp_sview, sp_samp, lod, level);2539}25402541/**2542* Sample 2D texture using an anisotropic filter.2543*/2544static void2545mip_filter_linear_aniso(const struct sp_sampler_view *sp_sview,2546const struct sp_sampler *sp_samp,2547img_filter_func min_filter,2548img_filter_func mag_filter,2549const float s[TGSI_QUAD_SIZE],2550const float t[TGSI_QUAD_SIZE],2551const float p[TGSI_QUAD_SIZE],2552UNUSED int gather_comp,2553const float lod_in[TGSI_QUAD_SIZE],2554const struct filter_args *filt_args,2555float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2556{2557const struct pipe_resource *texture = sp_sview->base.texture;2558const struct pipe_sampler_view *psview = &sp_sview->base;2559int level0;2560float lambda;2561float lod[TGSI_QUAD_SIZE];25622563const float s_to_u = u_minify(texture->width0, psview->u.tex.first_level);2564const float t_to_v = u_minify(texture->height0, psview->u.tex.first_level);2565const float dudx = (s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]) * s_to_u;2566const float dudy = (s[QUAD_TOP_LEFT] - s[QUAD_BOTTOM_LEFT]) * s_to_u;2567const float dvdx = (t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]) * t_to_v;2568const float dvdy = (t[QUAD_TOP_LEFT] - t[QUAD_BOTTOM_LEFT]) * t_to_v;2569struct img_filter_args args;25702571args.offset = filt_args->offset;25722573if (filt_args->control == TGSI_SAMPLER_LOD_BIAS ||2574filt_args->control == TGSI_SAMPLER_LOD_NONE ||2575/* XXX FIXME */2576filt_args->control == TGSI_SAMPLER_DERIVS_EXPLICIT) {2577/* note: instead of working with Px and Py, we will use the2578* squared length instead, to avoid sqrt.2579*/2580const float Px2 = dudx * dudx + dvdx * dvdx;2581const float Py2 = dudy * dudy + dvdy * dvdy;25822583float Pmax2;2584float Pmin2;2585float e;2586const float maxEccentricity = sp_samp->base.max_anisotropy * sp_samp->base.max_anisotropy;25872588if (Px2 < Py2) {2589Pmax2 = Py2;2590Pmin2 = Px2;2591}2592else {2593Pmax2 = Px2;2594Pmin2 = Py2;2595}25962597/* if the eccentricity of the ellipse is too big, scale up the shorter2598* of the two vectors to limit the maximum amount of work per pixel2599*/2600e = Pmax2 / Pmin2;2601if (e > maxEccentricity) {2602/* float s=e / maxEccentricity;2603minor[0] *= s;2604minor[1] *= s;2605Pmin2 *= s; */2606Pmin2 = Pmax2 / maxEccentricity;2607}26082609/* note: we need to have Pmin=sqrt(Pmin2) here, but we can avoid2610* this since 0.5*log(x) = log(sqrt(x))2611*/2612lambda = 0.5F * util_fast_log2(Pmin2) + sp_samp->base.lod_bias;2613compute_lod(&sp_samp->base, filt_args->control, lambda, lod_in, lod);2614}2615else {2616assert(filt_args->control == TGSI_SAMPLER_LOD_EXPLICIT ||2617filt_args->control == TGSI_SAMPLER_LOD_ZERO);2618compute_lod(&sp_samp->base, filt_args->control, sp_samp->base.lod_bias, lod_in, lod);2619}26202621/* XXX: Take into account all lod values.2622*/2623lambda = lod[0];2624level0 = psview->u.tex.first_level + (int)lambda;26252626/* If the ellipse covers the whole image, we can2627* simply return the average of the whole image.2628*/2629if (level0 >= (int) psview->u.tex.last_level) {2630int j;2631for (j = 0; j < TGSI_QUAD_SIZE; j++) {2632args.s = s[j];2633args.t = t[j];2634args.p = p[j];2635args.level = psview->u.tex.last_level;2636args.face_id = filt_args->faces[j];2637/*2638* XXX: we overwrote any linear filter with nearest, so this2639* isn't right (albeit if last level is 1x1 and no border it2640* will work just the same).2641*/2642min_filter(sp_sview, sp_samp, &args, &rgba[0][j]);2643}2644}2645else {2646/* don't bother interpolating between multiple LODs; it doesn't2647* seem to be worth the extra running time.2648*/2649img_filter_2d_ewa(sp_sview, sp_samp, min_filter, mag_filter,2650s, t, p, filt_args->faces, filt_args->offset,2651level0, dudx, dvdx, dudy, dvdy, rgba);2652}26532654if (DEBUG_TEX) {2655print_sample_4(__FUNCTION__, rgba);2656}2657}26582659/**2660* Get mip level relative to base level for linear mip filter2661*/2662static void2663mip_rel_level_linear_2d_linear_repeat_POT(2664const struct sp_sampler_view *sp_sview,2665const struct sp_sampler *sp_samp,2666const float lod[TGSI_QUAD_SIZE],2667float level[TGSI_QUAD_SIZE])2668{2669mip_rel_level_linear(sp_sview, sp_samp, lod, level);2670}26712672/**2673* Specialized version of mip_filter_linear with hard-wired calls to2674* 2d lambda calculation and 2d_linear_repeat_POT img filters.2675*/2676static void2677mip_filter_linear_2d_linear_repeat_POT(2678const struct sp_sampler_view *sp_sview,2679const struct sp_sampler *sp_samp,2680img_filter_func min_filter,2681img_filter_func mag_filter,2682const float s[TGSI_QUAD_SIZE],2683const float t[TGSI_QUAD_SIZE],2684const float p[TGSI_QUAD_SIZE],2685int gather_comp,2686const float lod[TGSI_QUAD_SIZE],2687const struct filter_args *filt_args,2688float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2689{2690const struct pipe_sampler_view *psview = &sp_sview->base;2691int j;26922693for (j = 0; j < TGSI_QUAD_SIZE; j++) {2694const int level0 = psview->u.tex.first_level + (int)lod[j];2695struct img_filter_args args;2696/* Catches both negative and large values of level0:2697*/2698args.s = s[j];2699args.t = t[j];2700args.p = p[j];2701args.face_id = filt_args->faces[j];2702args.offset = filt_args->offset;2703args.gather_only = filt_args->control == TGSI_SAMPLER_GATHER;2704args.gather_comp = gather_comp;2705if ((unsigned)level0 >= psview->u.tex.last_level) {2706if (level0 < 0)2707args.level = psview->u.tex.first_level;2708else2709args.level = psview->u.tex.last_level;2710img_filter_2d_linear_repeat_POT(sp_sview, sp_samp, &args,2711&rgba[0][j]);27122713}2714else {2715const float levelBlend = frac(lod[j]);2716float rgbax[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];2717int c;27182719args.level = level0;2720img_filter_2d_linear_repeat_POT(sp_sview, sp_samp, &args, &rgbax[0][0]);2721args.level = level0+1;2722img_filter_2d_linear_repeat_POT(sp_sview, sp_samp, &args, &rgbax[0][1]);27232724for (c = 0; c < TGSI_NUM_CHANNELS; c++)2725rgba[c][j] = lerp(levelBlend, rgbax[c][0], rgbax[c][1]);2726}2727}27282729if (DEBUG_TEX) {2730print_sample_4(__FUNCTION__, rgba);2731}2732}27332734static const struct sp_filter_funcs funcs_linear = {2735mip_rel_level_linear,2736mip_filter_linear2737};27382739static const struct sp_filter_funcs funcs_nearest = {2740mip_rel_level_nearest,2741mip_filter_nearest2742};27432744static const struct sp_filter_funcs funcs_none = {2745mip_rel_level_none,2746mip_filter_none2747};27482749static const struct sp_filter_funcs funcs_none_no_filter_select = {2750mip_rel_level_none_no_filter_select,2751mip_filter_none_no_filter_select2752};27532754static const struct sp_filter_funcs funcs_linear_aniso = {2755mip_rel_level_linear_aniso,2756mip_filter_linear_aniso2757};27582759static const struct sp_filter_funcs funcs_linear_2d_linear_repeat_POT = {2760mip_rel_level_linear_2d_linear_repeat_POT,2761mip_filter_linear_2d_linear_repeat_POT2762};27632764/**2765* Do shadow/depth comparisons.2766*/2767static void2768sample_compare(const struct sp_sampler_view *sp_sview,2769const struct sp_sampler *sp_samp,2770const float c0[TGSI_QUAD_SIZE],2771enum tgsi_sampler_control control,2772float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2773{2774const struct pipe_sampler_state *sampler = &sp_samp->base;2775int j, v;2776int k[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];2777float pc[4];2778const struct util_format_description *format_desc =2779util_format_description(sp_sview->base.format);2780/* not entirely sure we couldn't end up with non-valid swizzle here */2781const unsigned chan_type =2782format_desc->swizzle[0] <= PIPE_SWIZZLE_W ?2783format_desc->channel[format_desc->swizzle[0]].type :2784UTIL_FORMAT_TYPE_FLOAT;2785const bool is_gather = (control == TGSI_SAMPLER_GATHER);27862787/**2788* Compare texcoord 'p' (aka R) against texture value 'rgba[0]'2789* for 2D Array texture we need to use the 'c0' (aka Q).2790* When we sampled the depth texture, the depth value was put into all2791* RGBA channels. We look at the red channel here.2792*/2793279427952796if (chan_type != UTIL_FORMAT_TYPE_FLOAT) {2797/*2798* clamping is a result of conversion to texture format, hence2799* doesn't happen with floats. Technically also should do comparison2800* in texture format (quantization!).2801*/2802pc[0] = CLAMP(c0[0], 0.0F, 1.0F);2803pc[1] = CLAMP(c0[1], 0.0F, 1.0F);2804pc[2] = CLAMP(c0[2], 0.0F, 1.0F);2805pc[3] = CLAMP(c0[3], 0.0F, 1.0F);2806} else {2807pc[0] = c0[0];2808pc[1] = c0[1];2809pc[2] = c0[2];2810pc[3] = c0[3];2811}28122813for (v = 0; v < (is_gather ? TGSI_NUM_CHANNELS : 1); v++) {2814/* compare four texcoords vs. four texture samples */2815switch (sampler->compare_func) {2816case PIPE_FUNC_LESS:2817k[v][0] = pc[0] < rgba[v][0];2818k[v][1] = pc[1] < rgba[v][1];2819k[v][2] = pc[2] < rgba[v][2];2820k[v][3] = pc[3] < rgba[v][3];2821break;2822case PIPE_FUNC_LEQUAL:2823k[v][0] = pc[0] <= rgba[v][0];2824k[v][1] = pc[1] <= rgba[v][1];2825k[v][2] = pc[2] <= rgba[v][2];2826k[v][3] = pc[3] <= rgba[v][3];2827break;2828case PIPE_FUNC_GREATER:2829k[v][0] = pc[0] > rgba[v][0];2830k[v][1] = pc[1] > rgba[v][1];2831k[v][2] = pc[2] > rgba[v][2];2832k[v][3] = pc[3] > rgba[v][3];2833break;2834case PIPE_FUNC_GEQUAL:2835k[v][0] = pc[0] >= rgba[v][0];2836k[v][1] = pc[1] >= rgba[v][1];2837k[v][2] = pc[2] >= rgba[v][2];2838k[v][3] = pc[3] >= rgba[v][3];2839break;2840case PIPE_FUNC_EQUAL:2841k[v][0] = pc[0] == rgba[v][0];2842k[v][1] = pc[1] == rgba[v][1];2843k[v][2] = pc[2] == rgba[v][2];2844k[v][3] = pc[3] == rgba[v][3];2845break;2846case PIPE_FUNC_NOTEQUAL:2847k[v][0] = pc[0] != rgba[v][0];2848k[v][1] = pc[1] != rgba[v][1];2849k[v][2] = pc[2] != rgba[v][2];2850k[v][3] = pc[3] != rgba[v][3];2851break;2852case PIPE_FUNC_ALWAYS:2853k[v][0] = k[v][1] = k[v][2] = k[v][3] = 1;2854break;2855case PIPE_FUNC_NEVER:2856k[v][0] = k[v][1] = k[v][2] = k[v][3] = 0;2857break;2858default:2859k[v][0] = k[v][1] = k[v][2] = k[v][3] = 0;2860assert(0);2861break;2862}2863}28642865if (is_gather) {2866for (j = 0; j < TGSI_QUAD_SIZE; j++) {2867for (v = 0; v < TGSI_NUM_CHANNELS; v++) {2868rgba[v][j] = k[v][j];2869}2870}2871} else {2872for (j = 0; j < TGSI_QUAD_SIZE; j++) {2873rgba[0][j] = k[0][j];2874rgba[1][j] = k[0][j];2875rgba[2][j] = k[0][j];2876rgba[3][j] = 1.0F;2877}2878}2879}28802881static void2882do_swizzling(const struct pipe_sampler_view *sview,2883float in[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE],2884float out[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])2885{2886struct sp_sampler_view *sp_sview = (struct sp_sampler_view *)sview;2887int j;2888const unsigned swizzle_r = sview->swizzle_r;2889const unsigned swizzle_g = sview->swizzle_g;2890const unsigned swizzle_b = sview->swizzle_b;2891const unsigned swizzle_a = sview->swizzle_a;28922893switch (swizzle_r) {2894case PIPE_SWIZZLE_0:2895for (j = 0; j < 4; j++)2896out[0][j] = 0.0f;2897break;2898case PIPE_SWIZZLE_1:2899for (j = 0; j < 4; j++)2900out[0][j] = sp_sview->oneval;2901break;2902default:2903assert(swizzle_r < 4);2904for (j = 0; j < 4; j++)2905out[0][j] = in[swizzle_r][j];2906}29072908switch (swizzle_g) {2909case PIPE_SWIZZLE_0:2910for (j = 0; j < 4; j++)2911out[1][j] = 0.0f;2912break;2913case PIPE_SWIZZLE_1:2914for (j = 0; j < 4; j++)2915out[1][j] = sp_sview->oneval;2916break;2917default:2918assert(swizzle_g < 4);2919for (j = 0; j < 4; j++)2920out[1][j] = in[swizzle_g][j];2921}29222923switch (swizzle_b) {2924case PIPE_SWIZZLE_0:2925for (j = 0; j < 4; j++)2926out[2][j] = 0.0f;2927break;2928case PIPE_SWIZZLE_1:2929for (j = 0; j < 4; j++)2930out[2][j] = sp_sview->oneval;2931break;2932default:2933assert(swizzle_b < 4);2934for (j = 0; j < 4; j++)2935out[2][j] = in[swizzle_b][j];2936}29372938switch (swizzle_a) {2939case PIPE_SWIZZLE_0:2940for (j = 0; j < 4; j++)2941out[3][j] = 0.0f;2942break;2943case PIPE_SWIZZLE_1:2944for (j = 0; j < 4; j++)2945out[3][j] = sp_sview->oneval;2946break;2947default:2948assert(swizzle_a < 4);2949for (j = 0; j < 4; j++)2950out[3][j] = in[swizzle_a][j];2951}2952}295329542955static wrap_nearest_func2956get_nearest_unorm_wrap(unsigned mode)2957{2958switch (mode) {2959case PIPE_TEX_WRAP_CLAMP:2960return wrap_nearest_unorm_clamp;2961case PIPE_TEX_WRAP_CLAMP_TO_EDGE:2962return wrap_nearest_unorm_clamp_to_edge;2963case PIPE_TEX_WRAP_CLAMP_TO_BORDER:2964return wrap_nearest_unorm_clamp_to_border;2965default:2966debug_printf("illegal wrap mode %d with non-normalized coords\n", mode);2967return wrap_nearest_unorm_clamp;2968}2969}297029712972static wrap_nearest_func2973get_nearest_wrap(unsigned mode)2974{2975switch (mode) {2976case PIPE_TEX_WRAP_REPEAT:2977return wrap_nearest_repeat;2978case PIPE_TEX_WRAP_CLAMP:2979return wrap_nearest_clamp;2980case PIPE_TEX_WRAP_CLAMP_TO_EDGE:2981return wrap_nearest_clamp_to_edge;2982case PIPE_TEX_WRAP_CLAMP_TO_BORDER:2983return wrap_nearest_clamp_to_border;2984case PIPE_TEX_WRAP_MIRROR_REPEAT:2985return wrap_nearest_mirror_repeat;2986case PIPE_TEX_WRAP_MIRROR_CLAMP:2987return wrap_nearest_mirror_clamp;2988case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:2989return wrap_nearest_mirror_clamp_to_edge;2990case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:2991return wrap_nearest_mirror_clamp_to_border;2992default:2993assert(0);2994return wrap_nearest_repeat;2995}2996}299729982999static wrap_linear_func3000get_linear_unorm_wrap(unsigned mode)3001{3002switch (mode) {3003case PIPE_TEX_WRAP_CLAMP:3004return wrap_linear_unorm_clamp;3005case PIPE_TEX_WRAP_CLAMP_TO_EDGE:3006return wrap_linear_unorm_clamp_to_edge;3007case PIPE_TEX_WRAP_CLAMP_TO_BORDER:3008return wrap_linear_unorm_clamp_to_border;3009default:3010debug_printf("illegal wrap mode %d with non-normalized coords\n", mode);3011return wrap_linear_unorm_clamp;3012}3013}301430153016static wrap_linear_func3017get_linear_wrap(unsigned mode)3018{3019switch (mode) {3020case PIPE_TEX_WRAP_REPEAT:3021return wrap_linear_repeat;3022case PIPE_TEX_WRAP_CLAMP:3023return wrap_linear_clamp;3024case PIPE_TEX_WRAP_CLAMP_TO_EDGE:3025return wrap_linear_clamp_to_edge;3026case PIPE_TEX_WRAP_CLAMP_TO_BORDER:3027return wrap_linear_clamp_to_border;3028case PIPE_TEX_WRAP_MIRROR_REPEAT:3029return wrap_linear_mirror_repeat;3030case PIPE_TEX_WRAP_MIRROR_CLAMP:3031return wrap_linear_mirror_clamp;3032case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:3033return wrap_linear_mirror_clamp_to_edge;3034case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:3035return wrap_linear_mirror_clamp_to_border;3036default:3037assert(0);3038return wrap_linear_repeat;3039}3040}304130423043/**3044* Is swizzling needed for the given state key?3045*/3046static inline bool3047any_swizzle(const struct pipe_sampler_view *view)3048{3049return (view->swizzle_r != PIPE_SWIZZLE_X ||3050view->swizzle_g != PIPE_SWIZZLE_Y ||3051view->swizzle_b != PIPE_SWIZZLE_Z ||3052view->swizzle_a != PIPE_SWIZZLE_W);3053}305430553056static img_filter_func3057get_img_filter(const struct sp_sampler_view *sp_sview,3058const struct pipe_sampler_state *sampler,3059unsigned filter, bool gather)3060{3061switch (sp_sview->base.target) {3062case PIPE_BUFFER:3063case PIPE_TEXTURE_1D:3064if (filter == PIPE_TEX_FILTER_NEAREST)3065return img_filter_1d_nearest;3066else3067return img_filter_1d_linear;3068break;3069case PIPE_TEXTURE_1D_ARRAY:3070if (filter == PIPE_TEX_FILTER_NEAREST)3071return img_filter_1d_array_nearest;3072else3073return img_filter_1d_array_linear;3074break;3075case PIPE_TEXTURE_2D:3076case PIPE_TEXTURE_RECT:3077/* Try for fast path:3078*/3079if (!gather && sp_sview->pot2d &&3080sampler->wrap_s == sampler->wrap_t &&3081sampler->normalized_coords)3082{3083switch (sampler->wrap_s) {3084case PIPE_TEX_WRAP_REPEAT:3085switch (filter) {3086case PIPE_TEX_FILTER_NEAREST:3087return img_filter_2d_nearest_repeat_POT;3088case PIPE_TEX_FILTER_LINEAR:3089return img_filter_2d_linear_repeat_POT;3090default:3091break;3092}3093break;3094case PIPE_TEX_WRAP_CLAMP:3095switch (filter) {3096case PIPE_TEX_FILTER_NEAREST:3097return img_filter_2d_nearest_clamp_POT;3098default:3099break;3100}3101}3102}3103/* Otherwise use default versions:3104*/3105if (filter == PIPE_TEX_FILTER_NEAREST)3106return img_filter_2d_nearest;3107else3108return img_filter_2d_linear;3109break;3110case PIPE_TEXTURE_2D_ARRAY:3111if (filter == PIPE_TEX_FILTER_NEAREST)3112return img_filter_2d_array_nearest;3113else3114return img_filter_2d_array_linear;3115break;3116case PIPE_TEXTURE_CUBE:3117if (filter == PIPE_TEX_FILTER_NEAREST)3118return img_filter_cube_nearest;3119else3120return img_filter_cube_linear;3121break;3122case PIPE_TEXTURE_CUBE_ARRAY:3123if (filter == PIPE_TEX_FILTER_NEAREST)3124return img_filter_cube_array_nearest;3125else3126return img_filter_cube_array_linear;3127break;3128case PIPE_TEXTURE_3D:3129if (filter == PIPE_TEX_FILTER_NEAREST)3130return img_filter_3d_nearest;3131else3132return img_filter_3d_linear;3133break;3134default:3135assert(0);3136return img_filter_1d_nearest;3137}3138}31393140/**3141* Get mip filter funcs, and optionally both img min filter and img mag3142* filter. Note that both img filter function pointers must be either non-NULL3143* or NULL.3144*/3145static void3146get_filters(const struct sp_sampler_view *sp_sview,3147const struct sp_sampler *sp_samp,3148const enum tgsi_sampler_control control,3149const struct sp_filter_funcs **funcs,3150img_filter_func *min,3151img_filter_func *mag)3152{3153assert(funcs);3154if (control == TGSI_SAMPLER_GATHER) {3155*funcs = &funcs_nearest;3156if (min) {3157*min = get_img_filter(sp_sview, &sp_samp->base,3158PIPE_TEX_FILTER_LINEAR, true);3159}3160} else if (sp_sview->pot2d & sp_samp->min_mag_equal_repeat_linear) {3161*funcs = &funcs_linear_2d_linear_repeat_POT;3162} else {3163*funcs = sp_samp->filter_funcs;3164if (min) {3165assert(mag);3166*min = get_img_filter(sp_sview, &sp_samp->base,3167sp_samp->min_img_filter, false);3168if (sp_samp->min_mag_equal) {3169*mag = *min;3170} else {3171*mag = get_img_filter(sp_sview, &sp_samp->base,3172sp_samp->base.mag_img_filter, false);3173}3174}3175}3176}31773178static void3179sample_mip(const struct sp_sampler_view *sp_sview,3180const struct sp_sampler *sp_samp,3181const float s[TGSI_QUAD_SIZE],3182const float t[TGSI_QUAD_SIZE],3183const float p[TGSI_QUAD_SIZE],3184const float c0[TGSI_QUAD_SIZE],3185int gather_comp,3186const float lod[TGSI_QUAD_SIZE],3187const struct filter_args *filt_args,3188float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])3189{3190const struct sp_filter_funcs *funcs = NULL;3191img_filter_func min_img_filter = NULL;3192img_filter_func mag_img_filter = NULL;31933194get_filters(sp_sview, sp_samp, filt_args->control,3195&funcs, &min_img_filter, &mag_img_filter);31963197funcs->filter(sp_sview, sp_samp, min_img_filter, mag_img_filter,3198s, t, p, gather_comp, lod, filt_args, rgba);31993200if (sp_samp->base.compare_mode != PIPE_TEX_COMPARE_NONE) {3201sample_compare(sp_sview, sp_samp, c0, filt_args->control, rgba);3202}32033204if (sp_sview->need_swizzle && filt_args->control != TGSI_SAMPLER_GATHER) {3205float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];3206memcpy(rgba_temp, rgba, sizeof(rgba_temp));3207do_swizzling(&sp_sview->base, rgba_temp, rgba);3208}32093210}321132123213/**3214* This function uses cube texture coordinates to choose a face of a cube and3215* computes the 2D cube face coordinates. Puts face info into the sampler3216* faces[] array.3217*/3218static void3219convert_cube(const struct sp_sampler_view *sp_sview,3220const struct sp_sampler *sp_samp,3221const float s[TGSI_QUAD_SIZE],3222const float t[TGSI_QUAD_SIZE],3223const float p[TGSI_QUAD_SIZE],3224const float c0[TGSI_QUAD_SIZE],3225float ssss[TGSI_QUAD_SIZE],3226float tttt[TGSI_QUAD_SIZE],3227float pppp[TGSI_QUAD_SIZE],3228uint faces[TGSI_QUAD_SIZE])3229{3230unsigned j;32313232pppp[0] = c0[0];3233pppp[1] = c0[1];3234pppp[2] = c0[2];3235pppp[3] = c0[3];3236/*3237major axis3238direction target sc tc ma3239---------- ------------------------------- --- --- ---3240+rx TEXTURE_CUBE_MAP_POSITIVE_X_EXT -rz -ry rx3241-rx TEXTURE_CUBE_MAP_NEGATIVE_X_EXT +rz -ry rx3242+ry TEXTURE_CUBE_MAP_POSITIVE_Y_EXT +rx +rz ry3243-ry TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT +rx -rz ry3244+rz TEXTURE_CUBE_MAP_POSITIVE_Z_EXT +rx -ry rz3245-rz TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT -rx -ry rz3246*/32473248/* Choose the cube face and compute new s/t coords for the 2D face.3249*3250* Use the same cube face for all four pixels in the quad.3251*3252* This isn't ideal, but if we want to use a different cube face3253* per pixel in the quad, we'd have to also compute the per-face3254* LOD here too. That's because the four post-face-selection3255* texcoords are no longer related to each other (they're3256* per-face!) so we can't use subtraction to compute the partial3257* deriviates to compute the LOD. Doing so (near cube edges3258* anyway) gives us pretty much random values.3259*/3260for (j = 0; j < TGSI_QUAD_SIZE; j++) {3261const float rx = s[j], ry = t[j], rz = p[j];3262const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);32633264if (arx >= ary && arx >= arz) {3265const float sign = (rx >= 0.0F) ? 1.0F : -1.0F;3266const uint face = (rx >= 0.0F) ?3267PIPE_TEX_FACE_POS_X : PIPE_TEX_FACE_NEG_X;3268const float ima = -0.5F / fabsf(s[j]);3269ssss[j] = sign * p[j] * ima + 0.5F;3270tttt[j] = t[j] * ima + 0.5F;3271faces[j] = face;3272}3273else if (ary >= arx && ary >= arz) {3274const float sign = (ry >= 0.0F) ? 1.0F : -1.0F;3275const uint face = (ry >= 0.0F) ?3276PIPE_TEX_FACE_POS_Y : PIPE_TEX_FACE_NEG_Y;3277const float ima = -0.5F / fabsf(t[j]);3278ssss[j] = -s[j] * ima + 0.5F;3279tttt[j] = sign * -p[j] * ima + 0.5F;3280faces[j] = face;3281}3282else {3283const float sign = (rz >= 0.0F) ? 1.0F : -1.0F;3284const uint face = (rz >= 0.0F) ?3285PIPE_TEX_FACE_POS_Z : PIPE_TEX_FACE_NEG_Z;3286const float ima = -0.5F / fabsf(p[j]);3287ssss[j] = sign * -s[j] * ima + 0.5F;3288tttt[j] = t[j] * ima + 0.5F;3289faces[j] = face;3290}3291}3292}329332943295static void3296sp_get_dims(const struct sp_sampler_view *sp_sview,3297int level,3298int dims[4])3299{3300const struct pipe_sampler_view *view = &sp_sview->base;3301const struct pipe_resource *texture = view->texture;33023303if (view->target == PIPE_BUFFER) {3304dims[0] = view->u.buf.size / util_format_get_blocksize(view->format);3305/* the other values are undefined, but let's avoid potential valgrind3306* warnings.3307*/3308dims[1] = dims[2] = dims[3] = 0;3309return;3310}33113312/* undefined according to EXT_gpu_program */3313level += view->u.tex.first_level;3314if (level > view->u.tex.last_level)3315return;33163317dims[3] = view->u.tex.last_level - view->u.tex.first_level + 1;3318dims[0] = u_minify(texture->width0, level);33193320switch (view->target) {3321case PIPE_TEXTURE_1D_ARRAY:3322dims[1] = view->u.tex.last_layer - view->u.tex.first_layer + 1;3323FALLTHROUGH;3324case PIPE_TEXTURE_1D:3325return;3326case PIPE_TEXTURE_2D_ARRAY:3327dims[2] = view->u.tex.last_layer - view->u.tex.first_layer + 1;3328FALLTHROUGH;3329case PIPE_TEXTURE_2D:3330case PIPE_TEXTURE_CUBE:3331case PIPE_TEXTURE_RECT:3332dims[1] = u_minify(texture->height0, level);3333return;3334case PIPE_TEXTURE_3D:3335dims[1] = u_minify(texture->height0, level);3336dims[2] = u_minify(texture->depth0, level);3337return;3338case PIPE_TEXTURE_CUBE_ARRAY:3339dims[1] = u_minify(texture->height0, level);3340dims[2] = (view->u.tex.last_layer - view->u.tex.first_layer + 1) / 6;3341break;3342default:3343assert(!"unexpected texture target in sp_get_dims()");3344return;3345}3346}33473348/**3349* This function is only used for getting unfiltered texels via the3350* TXF opcode. The GL spec says that out-of-bounds texel fetches3351* produce undefined results. Instead of crashing, lets just clamp3352* coords to the texture image size.3353*/3354static void3355sp_get_texels(const struct sp_sampler_view *sp_sview,3356const int v_i[TGSI_QUAD_SIZE],3357const int v_j[TGSI_QUAD_SIZE],3358const int v_k[TGSI_QUAD_SIZE],3359const int lod[TGSI_QUAD_SIZE],3360const int8_t offset[3],3361float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])3362{3363union tex_tile_address addr;3364const struct pipe_resource *texture = sp_sview->base.texture;3365int j, c;3366const float *tx;3367/* TODO write a better test for LOD */3368const unsigned level =3369sp_sview->base.target == PIPE_BUFFER ? 0 :3370CLAMP(lod[0] + sp_sview->base.u.tex.first_level,3371sp_sview->base.u.tex.first_level,3372sp_sview->base.u.tex.last_level);3373const int width = u_minify(texture->width0, level);3374const int height = u_minify(texture->height0, level);3375const int depth = u_minify(texture->depth0, level);3376unsigned elem_size, first_element, last_element;33773378addr.value = 0;3379addr.bits.level = level;33803381switch (sp_sview->base.target) {3382case PIPE_BUFFER:3383elem_size = util_format_get_blocksize(sp_sview->base.format);3384first_element = sp_sview->base.u.buf.offset / elem_size;3385last_element = (sp_sview->base.u.buf.offset +3386sp_sview->base.u.buf.size) / elem_size - 1;3387for (j = 0; j < TGSI_QUAD_SIZE; j++) {3388const int x = CLAMP(v_i[j] + offset[0] +3389first_element,3390first_element,3391last_element);3392tx = get_texel_buffer_no_border(sp_sview, addr, x, elem_size);3393for (c = 0; c < 4; c++) {3394rgba[c][j] = tx[c];3395}3396}3397break;3398case PIPE_TEXTURE_1D:3399for (j = 0; j < TGSI_QUAD_SIZE; j++) {3400const int x = CLAMP(v_i[j] + offset[0], 0, width - 1);3401tx = get_texel_2d_no_border(sp_sview, addr, x,3402sp_sview->base.u.tex.first_layer);3403for (c = 0; c < 4; c++) {3404rgba[c][j] = tx[c];3405}3406}3407break;3408case PIPE_TEXTURE_1D_ARRAY:3409for (j = 0; j < TGSI_QUAD_SIZE; j++) {3410const int x = CLAMP(v_i[j] + offset[0], 0, width - 1);3411const int y = CLAMP(v_j[j], sp_sview->base.u.tex.first_layer,3412sp_sview->base.u.tex.last_layer);3413tx = get_texel_2d_no_border(sp_sview, addr, x, y);3414for (c = 0; c < 4; c++) {3415rgba[c][j] = tx[c];3416}3417}3418break;3419case PIPE_TEXTURE_2D:3420case PIPE_TEXTURE_RECT:3421for (j = 0; j < TGSI_QUAD_SIZE; j++) {3422const int x = CLAMP(v_i[j] + offset[0], 0, width - 1);3423const int y = CLAMP(v_j[j] + offset[1], 0, height - 1);3424tx = get_texel_3d_no_border(sp_sview, addr, x, y,3425sp_sview->base.u.tex.first_layer);3426for (c = 0; c < 4; c++) {3427rgba[c][j] = tx[c];3428}3429}3430break;3431case PIPE_TEXTURE_2D_ARRAY:3432for (j = 0; j < TGSI_QUAD_SIZE; j++) {3433const int x = CLAMP(v_i[j] + offset[0], 0, width - 1);3434const int y = CLAMP(v_j[j] + offset[1], 0, height - 1);3435const int layer = CLAMP(v_k[j], sp_sview->base.u.tex.first_layer,3436sp_sview->base.u.tex.last_layer);3437tx = get_texel_3d_no_border(sp_sview, addr, x, y, layer);3438for (c = 0; c < 4; c++) {3439rgba[c][j] = tx[c];3440}3441}3442break;3443case PIPE_TEXTURE_3D:3444for (j = 0; j < TGSI_QUAD_SIZE; j++) {3445int x = CLAMP(v_i[j] + offset[0], 0, width - 1);3446int y = CLAMP(v_j[j] + offset[1], 0, height - 1);3447int z = CLAMP(v_k[j] + offset[2], 0, depth - 1);3448tx = get_texel_3d_no_border(sp_sview, addr, x, y, z);3449for (c = 0; c < 4; c++) {3450rgba[c][j] = tx[c];3451}3452}3453break;3454case PIPE_TEXTURE_CUBE: /* TXF can't work on CUBE according to spec */3455case PIPE_TEXTURE_CUBE_ARRAY:3456default:3457assert(!"Unknown or CUBE texture type in TXF processing\n");3458break;3459}34603461if (sp_sview->need_swizzle) {3462float rgba_temp[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE];3463memcpy(rgba_temp, rgba, sizeof(rgba_temp));3464do_swizzling(&sp_sview->base, rgba_temp, rgba);3465}3466}346734683469void *3470softpipe_create_sampler_state(struct pipe_context *pipe,3471const struct pipe_sampler_state *sampler)3472{3473struct sp_sampler *samp = CALLOC_STRUCT(sp_sampler);34743475samp->base = *sampler;34763477/* Note that (for instance) linear_texcoord_s and3478* nearest_texcoord_s may be active at the same time, if the3479* sampler min_img_filter differs from its mag_img_filter.3480*/3481if (sampler->normalized_coords) {3482samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );3483samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );3484samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );34853486samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );3487samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );3488samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );3489}3490else {3491samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );3492samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );3493samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );34943495samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );3496samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );3497samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );3498}34993500samp->min_img_filter = sampler->min_img_filter;35013502switch (sampler->min_mip_filter) {3503case PIPE_TEX_MIPFILTER_NONE:3504if (sampler->min_img_filter == sampler->mag_img_filter)3505samp->filter_funcs = &funcs_none_no_filter_select;3506else3507samp->filter_funcs = &funcs_none;3508break;35093510case PIPE_TEX_MIPFILTER_NEAREST:3511samp->filter_funcs = &funcs_nearest;3512break;35133514case PIPE_TEX_MIPFILTER_LINEAR:3515if (sampler->min_img_filter == sampler->mag_img_filter &&3516sampler->normalized_coords &&3517sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&3518sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&3519sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR &&3520sampler->max_anisotropy <= 1) {3521samp->min_mag_equal_repeat_linear = TRUE;3522}3523samp->filter_funcs = &funcs_linear;35243525/* Anisotropic filtering extension. */3526if (sampler->max_anisotropy > 1) {3527samp->filter_funcs = &funcs_linear_aniso;35283529/* Override min_img_filter:3530* min_img_filter needs to be set to NEAREST since we need to access3531* each texture pixel as it is and weight it later; using linear3532* filters will have incorrect results.3533* By setting the filter to NEAREST here, we can avoid calling the3534* generic img_filter_2d_nearest in the anisotropic filter function,3535* making it possible to use one of the accelerated implementations3536*/3537samp->min_img_filter = PIPE_TEX_FILTER_NEAREST;35383539/* on first access create the lookup table containing the filter weights. */3540if (!weightLut) {3541create_filter_table();3542}3543}3544break;3545}3546if (samp->min_img_filter == sampler->mag_img_filter) {3547samp->min_mag_equal = TRUE;3548}35493550return (void *)samp;3551}355235533554compute_lambda_func3555softpipe_get_lambda_func(const struct pipe_sampler_view *view,3556enum pipe_shader_type shader)3557{3558if (shader != PIPE_SHADER_FRAGMENT)3559return compute_lambda_vert;35603561switch (view->target) {3562case PIPE_BUFFER:3563case PIPE_TEXTURE_1D:3564case PIPE_TEXTURE_1D_ARRAY:3565return compute_lambda_1d;3566case PIPE_TEXTURE_2D:3567case PIPE_TEXTURE_2D_ARRAY:3568case PIPE_TEXTURE_RECT:3569return compute_lambda_2d;3570case PIPE_TEXTURE_CUBE:3571case PIPE_TEXTURE_CUBE_ARRAY:3572return compute_lambda_cube;3573case PIPE_TEXTURE_3D:3574return compute_lambda_3d;3575default:3576assert(0);3577return compute_lambda_1d;3578}3579}358035813582struct pipe_sampler_view *3583softpipe_create_sampler_view(struct pipe_context *pipe,3584struct pipe_resource *resource,3585const struct pipe_sampler_view *templ)3586{3587struct sp_sampler_view *sview = CALLOC_STRUCT(sp_sampler_view);3588const struct softpipe_resource *spr = (struct softpipe_resource *)resource;35893590if (sview) {3591struct pipe_sampler_view *view = &sview->base;3592*view = *templ;3593view->reference.count = 1;3594view->texture = NULL;3595pipe_resource_reference(&view->texture, resource);3596view->context = pipe;35973598#ifdef DEBUG3599/*3600* This is possibly too lenient, but the primary reason is just3601* to catch gallium frontends which forget to initialize this, so3602* it only catches clearly impossible view targets.3603*/3604if (view->target != resource->target) {3605if (view->target == PIPE_TEXTURE_1D)3606assert(resource->target == PIPE_TEXTURE_1D_ARRAY);3607else if (view->target == PIPE_TEXTURE_1D_ARRAY)3608assert(resource->target == PIPE_TEXTURE_1D);3609else if (view->target == PIPE_TEXTURE_2D)3610assert(resource->target == PIPE_TEXTURE_2D_ARRAY ||3611resource->target == PIPE_TEXTURE_CUBE ||3612resource->target == PIPE_TEXTURE_CUBE_ARRAY);3613else if (view->target == PIPE_TEXTURE_2D_ARRAY)3614assert(resource->target == PIPE_TEXTURE_2D ||3615resource->target == PIPE_TEXTURE_CUBE ||3616resource->target == PIPE_TEXTURE_CUBE_ARRAY);3617else if (view->target == PIPE_TEXTURE_CUBE)3618assert(resource->target == PIPE_TEXTURE_CUBE_ARRAY ||3619resource->target == PIPE_TEXTURE_2D_ARRAY);3620else if (view->target == PIPE_TEXTURE_CUBE_ARRAY)3621assert(resource->target == PIPE_TEXTURE_CUBE ||3622resource->target == PIPE_TEXTURE_2D_ARRAY);3623else3624assert(0);3625}3626#endif36273628if (any_swizzle(view)) {3629sview->need_swizzle = TRUE;3630}36313632sview->need_cube_convert = (view->target == PIPE_TEXTURE_CUBE ||3633view->target == PIPE_TEXTURE_CUBE_ARRAY);3634sview->pot2d = spr->pot &&3635(view->target == PIPE_TEXTURE_2D ||3636view->target == PIPE_TEXTURE_RECT);36373638sview->xpot = util_logbase2( resource->width0 );3639sview->ypot = util_logbase2( resource->height0 );36403641sview->oneval = util_format_is_pure_integer(view->format) ? uif(1) : 1.0f;3642}36433644return (struct pipe_sampler_view *) sview;3645}364636473648static inline const struct sp_tgsi_sampler *3649sp_tgsi_sampler_cast_c(const struct tgsi_sampler *sampler)3650{3651return (const struct sp_tgsi_sampler *)sampler;3652}365336543655static void3656sp_tgsi_get_dims(struct tgsi_sampler *tgsi_sampler,3657const unsigned sview_index,3658int level, int dims[4])3659{3660const struct sp_tgsi_sampler *sp_samp =3661sp_tgsi_sampler_cast_c(tgsi_sampler);36623663assert(sview_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);3664/* always have a view here but texture is NULL if no sampler view was set. */3665if (!sp_samp->sp_sview[sview_index].base.texture) {3666dims[0] = dims[1] = dims[2] = dims[3] = 0;3667return;3668}3669sp_get_dims(&sp_samp->sp_sview[sview_index], level, dims);3670}367136723673static void prepare_compare_values(enum pipe_texture_target target,3674const float p[TGSI_QUAD_SIZE],3675const float c0[TGSI_QUAD_SIZE],3676const float c1[TGSI_QUAD_SIZE],3677float pc[TGSI_QUAD_SIZE])3678{3679if (target == PIPE_TEXTURE_2D_ARRAY ||3680target == PIPE_TEXTURE_CUBE) {3681pc[0] = c0[0];3682pc[1] = c0[1];3683pc[2] = c0[2];3684pc[3] = c0[3];3685} else if (target == PIPE_TEXTURE_CUBE_ARRAY) {3686pc[0] = c1[0];3687pc[1] = c1[1];3688pc[2] = c1[2];3689pc[3] = c1[3];3690} else {3691pc[0] = p[0];3692pc[1] = p[1];3693pc[2] = p[2];3694pc[3] = p[3];3695}3696}36973698static void3699sp_tgsi_get_samples(struct tgsi_sampler *tgsi_sampler,3700const unsigned sview_index,3701const unsigned sampler_index,3702const float s[TGSI_QUAD_SIZE],3703const float t[TGSI_QUAD_SIZE],3704const float p[TGSI_QUAD_SIZE],3705const float c0[TGSI_QUAD_SIZE],3706const float lod_in[TGSI_QUAD_SIZE],3707float derivs[3][2][TGSI_QUAD_SIZE],3708const int8_t offset[3],3709enum tgsi_sampler_control control,3710float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])3711{3712const struct sp_tgsi_sampler *sp_tgsi_samp =3713sp_tgsi_sampler_cast_c(tgsi_sampler);3714struct sp_sampler_view sp_sview;3715const struct sp_sampler *sp_samp;3716struct filter_args filt_args;3717float compare_values[TGSI_QUAD_SIZE];3718float lod[TGSI_QUAD_SIZE];3719int c;37203721assert(sview_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);3722assert(sampler_index < PIPE_MAX_SAMPLERS);3723assert(sp_tgsi_samp->sp_sampler[sampler_index]);37243725memcpy(&sp_sview, &sp_tgsi_samp->sp_sview[sview_index],3726sizeof(struct sp_sampler_view));3727sp_samp = sp_tgsi_samp->sp_sampler[sampler_index];37283729if (util_format_is_unorm(sp_sview.base.format)) {3730for (c = 0; c < TGSI_NUM_CHANNELS; c++)3731sp_sview.border_color.f[c] = CLAMP(sp_samp->base.border_color.f[c],37320.0f, 1.0f);3733} else if (util_format_is_snorm(sp_sview.base.format)) {3734for (c = 0; c < TGSI_NUM_CHANNELS; c++)3735sp_sview.border_color.f[c] = CLAMP(sp_samp->base.border_color.f[c],3736-1.0f, 1.0f);3737} else {3738memcpy(sp_sview.border_color.f, sp_samp->base.border_color.f,3739TGSI_NUM_CHANNELS * sizeof(float));3740}37413742/* always have a view here but texture is NULL if no sampler view was set. */3743if (!sp_sview.base.texture) {3744int i, j;3745for (j = 0; j < TGSI_NUM_CHANNELS; j++) {3746for (i = 0; i < TGSI_QUAD_SIZE; i++) {3747rgba[j][i] = 0.0f;3748}3749}3750return;3751}37523753if (sp_samp->base.compare_mode != PIPE_TEX_COMPARE_NONE)3754prepare_compare_values(sp_sview.base.target, p, c0, lod_in, compare_values);37553756filt_args.control = control;3757filt_args.offset = offset;3758int gather_comp = get_gather_component(lod_in);37593760compute_lambda_lod(&sp_sview, sp_samp, s, t, p, derivs, lod_in, control, lod);37613762if (sp_sview.need_cube_convert) {3763float cs[TGSI_QUAD_SIZE];3764float ct[TGSI_QUAD_SIZE];3765float cp[TGSI_QUAD_SIZE];3766uint faces[TGSI_QUAD_SIZE];37673768convert_cube(&sp_sview, sp_samp, s, t, p, c0, cs, ct, cp, faces);37693770filt_args.faces = faces;3771sample_mip(&sp_sview, sp_samp, cs, ct, cp, compare_values, gather_comp, lod, &filt_args, rgba);3772} else {3773static const uint zero_faces[TGSI_QUAD_SIZE] = {0, 0, 0, 0};37743775filt_args.faces = zero_faces;3776sample_mip(&sp_sview, sp_samp, s, t, p, compare_values, gather_comp, lod, &filt_args, rgba);3777}3778}37793780static void3781sp_tgsi_query_lod(const struct tgsi_sampler *tgsi_sampler,3782const unsigned sview_index,3783const unsigned sampler_index,3784const float s[TGSI_QUAD_SIZE],3785const float t[TGSI_QUAD_SIZE],3786const float p[TGSI_QUAD_SIZE],3787const float c0[TGSI_QUAD_SIZE],3788const enum tgsi_sampler_control control,3789float mipmap[TGSI_QUAD_SIZE],3790float lod[TGSI_QUAD_SIZE])3791{3792static const float lod_in[TGSI_QUAD_SIZE] = { 0.0, 0.0, 0.0, 0.0 };3793static const float dummy_grad[3][2][TGSI_QUAD_SIZE];37943795const struct sp_tgsi_sampler *sp_tgsi_samp =3796sp_tgsi_sampler_cast_c(tgsi_sampler);3797const struct sp_sampler_view *sp_sview;3798const struct sp_sampler *sp_samp;3799const struct sp_filter_funcs *funcs;3800int i;38013802assert(sview_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);3803assert(sampler_index < PIPE_MAX_SAMPLERS);3804assert(sp_tgsi_samp->sp_sampler[sampler_index]);38053806sp_sview = &sp_tgsi_samp->sp_sview[sview_index];3807sp_samp = sp_tgsi_samp->sp_sampler[sampler_index];3808/* always have a view here but texture is NULL if no sampler view was3809* set. */3810if (!sp_sview->base.texture) {3811for (i = 0; i < TGSI_QUAD_SIZE; i++) {3812mipmap[i] = 0.0f;3813lod[i] = 0.0f;3814}3815return;3816}3817compute_lambda_lod_unclamped(sp_sview, sp_samp,3818s, t, p, dummy_grad, lod_in, control, lod);38193820get_filters(sp_sview, sp_samp, control, &funcs, NULL, NULL);3821funcs->relative_level(sp_sview, sp_samp, lod, mipmap);3822}38233824static void3825sp_tgsi_get_texel(struct tgsi_sampler *tgsi_sampler,3826const unsigned sview_index,3827const int i[TGSI_QUAD_SIZE],3828const int j[TGSI_QUAD_SIZE], const int k[TGSI_QUAD_SIZE],3829const int lod[TGSI_QUAD_SIZE], const int8_t offset[3],3830float rgba[TGSI_NUM_CHANNELS][TGSI_QUAD_SIZE])3831{3832const struct sp_tgsi_sampler *sp_samp =3833sp_tgsi_sampler_cast_c(tgsi_sampler);38343835assert(sview_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);3836/* always have a view here but texture is NULL if no sampler view was set. */3837if (!sp_samp->sp_sview[sview_index].base.texture) {3838int i, j;3839for (j = 0; j < TGSI_NUM_CHANNELS; j++) {3840for (i = 0; i < TGSI_QUAD_SIZE; i++) {3841rgba[j][i] = 0.0f;3842}3843}3844return;3845}3846sp_get_texels(&sp_samp->sp_sview[sview_index], i, j, k, lod, offset, rgba);3847}384838493850struct sp_tgsi_sampler *3851sp_create_tgsi_sampler(void)3852{3853struct sp_tgsi_sampler *samp = CALLOC_STRUCT(sp_tgsi_sampler);3854if (!samp)3855return NULL;38563857samp->base.get_dims = sp_tgsi_get_dims;3858samp->base.get_samples = sp_tgsi_get_samples;3859samp->base.get_texel = sp_tgsi_get_texel;3860samp->base.query_lod = sp_tgsi_query_lod;38613862return samp;3863}386438653866