sp_tex_sample.c revision 779e9cb658dba4ef44fae7e8aa62409f7227f46c
10dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**************************************************************************
20dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
30dc4eea64f56cc93e5359372b08b99a2d600273cBrian * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
40dc4eea64f56cc93e5359372b08b99a2d600273cBrian * All Rights Reserved.
54bfe1c955fe679547c8a03119d1681e33593c768Michal Krol * Copyright 2008-2010 VMware, Inc.  All rights reserved.
60dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
70dc4eea64f56cc93e5359372b08b99a2d600273cBrian * Permission is hereby granted, free of charge, to any person obtaining a
80dc4eea64f56cc93e5359372b08b99a2d600273cBrian * copy of this software and associated documentation files (the
90dc4eea64f56cc93e5359372b08b99a2d600273cBrian * "Software"), to deal in the Software without restriction, including
100dc4eea64f56cc93e5359372b08b99a2d600273cBrian * without limitation the rights to use, copy, modify, merge, publish,
110dc4eea64f56cc93e5359372b08b99a2d600273cBrian * distribute, sub license, and/or sell copies of the Software, and to
120dc4eea64f56cc93e5359372b08b99a2d600273cBrian * permit persons to whom the Software is furnished to do so, subject to
130dc4eea64f56cc93e5359372b08b99a2d600273cBrian * the following conditions:
140dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
150dc4eea64f56cc93e5359372b08b99a2d600273cBrian * The above copyright notice and this permission notice (including the
160dc4eea64f56cc93e5359372b08b99a2d600273cBrian * next paragraph) shall be included in all copies or substantial portions
170dc4eea64f56cc93e5359372b08b99a2d600273cBrian * of the Software.
180dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
190dc4eea64f56cc93e5359372b08b99a2d600273cBrian * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
200dc4eea64f56cc93e5359372b08b99a2d600273cBrian * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
210dc4eea64f56cc93e5359372b08b99a2d600273cBrian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
220dc4eea64f56cc93e5359372b08b99a2d600273cBrian * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
230dc4eea64f56cc93e5359372b08b99a2d600273cBrian * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
240dc4eea64f56cc93e5359372b08b99a2d600273cBrian * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
250dc4eea64f56cc93e5359372b08b99a2d600273cBrian * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
260dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
270dc4eea64f56cc93e5359372b08b99a2d600273cBrian **************************************************************************/
280dc4eea64f56cc93e5359372b08b99a2d600273cBrian
290dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**
300dc4eea64f56cc93e5359372b08b99a2d600273cBrian * Texture sampling
310dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
320dc4eea64f56cc93e5359372b08b99a2d600273cBrian * Authors:
330dc4eea64f56cc93e5359372b08b99a2d600273cBrian *   Brian Paul
344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell *   Keith Whitwell
350dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
360dc4eea64f56cc93e5359372b08b99a2d600273cBrian
370dc4eea64f56cc93e5359372b08b99a2d600273cBrian#include "pipe/p_context.h"
380dc4eea64f56cc93e5359372b08b99a2d600273cBrian#include "pipe/p_defines.h"
3900c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell#include "pipe/p_shader_tokens.h"
401a46dcc8a927dfb38ca1381e7b3dafb789f8257cBrian Paul#include "util/u_math.h"
414f25420bdd834e81a3e22733304efc5261c2998aBrian Paul#include "util/u_memory.h"
42d204659c8c725c02212ad4a49275c7447f2d02a6Brian Paul#include "sp_quad.h"   /* only for #define QUAD_* tokens */
43d204659c8c725c02212ad4a49275c7447f2d02a6Brian Paul#include "sp_tex_sample.h"
447670102468a55de50cf0cfa0b938d36aaf212f1fKeith Whitwell#include "sp_tex_tile_cache.h"
450dc4eea64f56cc93e5359372b08b99a2d600273cBrian
460dc4eea64f56cc93e5359372b08b99a2d600273cBrian
474250882ccf8326ba9074c671110370534489caa6Brian Paul/** Set to one to help debug texture sampling */
484250882ccf8326ba9074c671110370534489caa6Brian Paul#define DEBUG_TEX 0
494250882ccf8326ba9074c671110370534489caa6Brian Paul
503ffd529ff19bf8dd7b022a267bf2afe44c7f0f65Brian Paul
5108f33a025100dea2d951e6d628891fe294b18082Brian/*
52b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Return fractional part of 'f'.  Used for computing interpolation weights.
53b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Need to be careful with negative values.
54b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
55b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * of improperly weighted linear-filtered textures.
5608f33a025100dea2d951e6d628891fe294b18082Brian * The tests/texwrap.c demo is a good test.
5708f33a025100dea2d951e6d628891fe294b18082Brian */
58b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulstatic INLINE float
59b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulfrac(float f)
60b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul{
61b37c54150058c07ab2d3db2d7e5891a457b51e76Brian Paul   return f - floorf(f);
62b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul}
63b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul
6408f33a025100dea2d951e6d628891fe294b18082Brian
6508f33a025100dea2d951e6d628891fe294b18082Brian
6608f33a025100dea2d951e6d628891fe294b18082Brian/**
6708f33a025100dea2d951e6d628891fe294b18082Brian * Linear interpolation macro
6808f33a025100dea2d951e6d628891fe294b18082Brian */
6938bee46e83b18ff4ad42d340b507b1a15b4326c7Brianstatic INLINE float
7038bee46e83b18ff4ad42d340b507b1a15b4326c7Brianlerp(float a, float v0, float v1)
7138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian{
7238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   return v0 + a * (v1 - v0);
7338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian}
740dc4eea64f56cc93e5359372b08b99a2d600273cBrian
750dc4eea64f56cc93e5359372b08b99a2d600273cBrian
760dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**
77fd60bf8e33bbcba7b7749ae5a4285bad60769b9bBrian Paul * Do 2D/bilinear interpolation of float values.
780dc4eea64f56cc93e5359372b08b99a2d600273cBrian * v00, v10, v01 and v11 are typically four texture samples in a square/box.
790dc4eea64f56cc93e5359372b08b99a2d600273cBrian * a and b are the horizontal and vertical interpolants.
800dc4eea64f56cc93e5359372b08b99a2d600273cBrian * It's important that this function is inlined when compiled with
810dc4eea64f56cc93e5359372b08b99a2d600273cBrian * optimization!  If we find that's not true on some systems, convert
820dc4eea64f56cc93e5359372b08b99a2d600273cBrian * to a macro.
830dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
84b4480285ed5098f1c862690ee105dd46f5e6cd1eBrianstatic INLINE float
85b4480285ed5098f1c862690ee105dd46f5e6cd1eBrianlerp_2d(float a, float b,
86b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian        float v00, float v10, float v01, float v11)
870dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
8838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp0 = lerp(a, v00, v10);
8938bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp1 = lerp(a, v01, v11);
9038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   return lerp(b, temp0, temp1);
9138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian}
9238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
9338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
9438bee46e83b18ff4ad42d340b507b1a15b4326c7Brian/**
9538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * As above, but 3D interpolation of 8 values.
9638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian */
9738bee46e83b18ff4ad42d340b507b1a15b4326c7Brianstatic INLINE float
9838bee46e83b18ff4ad42d340b507b1a15b4326c7Brianlerp_3d(float a, float b, float c,
9938bee46e83b18ff4ad42d340b507b1a15b4326c7Brian        float v000, float v100, float v010, float v110,
10038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian        float v001, float v101, float v011, float v111)
10138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian{
10238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp0 = lerp_2d(a, b, v000, v100, v010, v110);
10338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp1 = lerp_2d(a, b, v001, v101, v011, v111);
10438bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   return lerp(c, temp0, temp1);
1050dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
1060dc4eea64f56cc93e5359372b08b99a2d600273cBrian
1070dc4eea64f56cc93e5359372b08b99a2d600273cBrian
10838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
1090dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**
110b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Compute coord % size for repeat wrap modes.
111e31f0f996537046228602a251706613ca4163209Brian Paul * Note that if coord is negative, coord % size doesn't give the right
112e31f0f996537046228602a251706613ca4163209Brian Paul * value.  To avoid that problem we add a large multiple of the size
113e31f0f996537046228602a251706613ca4163209Brian Paul * (rather than using a conditional).
1140dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
115b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulstatic INLINE int
116b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulrepeat(int coord, unsigned size)
117b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul{
118e31f0f996537046228602a251706613ca4163209Brian Paul   return (coord + size * 1024) % size;
119b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul}
12008f33a025100dea2d951e6d628891fe294b18082Brian
12108f33a025100dea2d951e6d628891fe294b18082Brian
12208f33a025100dea2d951e6d628891fe294b18082Brian/**
12338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * Apply texture coord wrapping mode and return integer texture indexes
12438bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * for a vector of four texcoords (S or T or P).
12508f33a025100dea2d951e6d628891fe294b18082Brian * \param wrapMode  PIPE_TEX_WRAP_x
12638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param s  the incoming texcoords
12708f33a025100dea2d951e6d628891fe294b18082Brian * \param size  the texture image size
12838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord  returns the integer texcoords
12908f33a025100dea2d951e6d628891fe294b18082Brian * \return  integer texture index
13008f33a025100dea2d951e6d628891fe294b18082Brian */
1314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
132e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_repeat(const float s[4], unsigned size, int icoord[4])
1334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
1354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [0,1) */
1364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0,size-1] */
1374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int i = util_ifloor(s[ch] * size);
139b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      icoord[ch] = repeat(i, size);
1404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
145e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_clamp(const float s[4], unsigned size, int icoord[4])
1460dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
14738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
1484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [0,1] */
1494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0,size-1] */
1504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (s[ch] <= 0.0F)
1524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
1534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (s[ch] >= 1.0F)
1544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
1554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
1564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(s[ch] * size);
1574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
162e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_clamp_to_edge(const float s[4], unsigned size, int icoord[4])
1634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
1654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
1664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0, size-1] */
1674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = 1.0F / (2.0F * size);
1684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
1694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (s[ch] < min)
1714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
1724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (s[ch] > max)
1734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
1744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
1754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(s[ch] * size);
1764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
181e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_clamp_to_border(const float s[4], unsigned size, int icoord[4])
1824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
1844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
1854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [-1, size] */
1864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
1874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
1884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (s[ch] <= min)
1904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = -1;
1914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (s[ch] >= max)
1924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size;
1934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
1944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(s[ch] * size);
1954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
198e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
200e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_mirror_repeat(const float s[4], unsigned size, int icoord[4])
2014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = 1.0F / (2.0F * size);
2044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
2054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const int flr = util_ifloor(s[ch]);
207b37c54150058c07ab2d3db2d7e5891a457b51e76Brian Paul      float u = frac(s[ch]);
2084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (flr & 1)
209b37c54150058c07ab2d3db2d7e5891a457b51e76Brian Paul         u = 1.0F - u;
2104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u < min)
2114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
2124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u > max)
2134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
2144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
2184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
219e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
221e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_mirror_clamp(const float s[4], unsigned size, int icoord[4])
2224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
22508f33a025100dea2d951e6d628891fe294b18082Brian      /* s limited to [0,1] */
22608f33a025100dea2d951e6d628891fe294b18082Brian      /* i limited to [0,size-1] */
2274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float u = fabsf(s[ch]);
2284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u <= 0.0F)
2294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
2304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u >= 1.0F)
2314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
2324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
2364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
237e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
2394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_nearest_mirror_clamp_to_edge(const float s[4], unsigned size,
240e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                  int icoord[4])
2414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
2444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0, size-1] */
2454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = 1.0F / (2.0F * size);
2464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
2474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float u = fabsf(s[ch]);
2494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u < min)
2504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
2514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u > max)
2524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
2534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
2574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
2584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
2594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
2604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_nearest_mirror_clamp_to_border(const float s[4], unsigned size,
2614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                    int icoord[4])
2624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
2654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0, size-1] */
2664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
2674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
2684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float u = fabsf(s[ch]);
2704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u < min)
2714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = -1;
2724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u > max)
2734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size;
2744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2760dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
2770dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
2780dc4eea64f56cc93e5359372b08b99a2d600273cBrian
27908f33a025100dea2d951e6d628891fe294b18082Brian
28008f33a025100dea2d951e6d628891fe294b18082Brian/**
28138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * Used to compute texel locations for linear sampling for four texcoords.
28208f33a025100dea2d951e6d628891fe294b18082Brian * \param wrapMode  PIPE_TEX_WRAP_x
28338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param s  the texcoords
28408f33a025100dea2d951e6d628891fe294b18082Brian * \param size  the texture image size
28538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord0  returns first texture indexes
28638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord1  returns second texture indexes (usually icoord0 + 1)
28738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param w  returns blend factor/weight between texture indexes
28838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord  returns the computed integer texture coords
28908f33a025100dea2d951e6d628891fe294b18082Brian */
2904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
2914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_repeat(const float s[4], unsigned size,
2924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   int icoord0[4], int icoord1[4], float w[4])
2934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = s[ch] * size - 0.5F;
297b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      icoord0[ch] = repeat(util_ifloor(u), size);
298b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      icoord1[ch] = repeat(icoord0[ch] + 1, size);
299b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
303e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_clamp(const float s[4], unsigned size,
30638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian                  int icoord0[4], int icoord1[4], float w[4])
3070dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
30838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
3094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], 0.0F, 1.0F);
3114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5f;
3124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
314b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
31738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
318e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_clamp_to_edge(const float s[4], unsigned size,
3214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                          int icoord0[4], int icoord1[4], float w[4])
3224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], 0.0F, 1.0F);
3264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5f;
3274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
3294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord0[ch] < 0)
3304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord0[ch] = 0;
3314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] >= (int) size)
3324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
333b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
337e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_clamp_to_border(const float s[4], unsigned size,
3404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                            int icoord0[4], int icoord1[4], float w[4])
3414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
3434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
3444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], min, max);
3474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5f;
3484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
350b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
3544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
3554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_repeat(const float s[4], unsigned size,
3574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                          int icoord0[4], int icoord1[4], float w[4])
3584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const int flr = util_ifloor(s[ch]);
362b37c54150058c07ab2d3db2d7e5891a457b51e76Brian Paul      float u = frac(s[ch]);
3634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (flr & 1)
364b37c54150058c07ab2d3db2d7e5891a457b51e76Brian Paul         u = 1.0F - u;
3654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5F;
3664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
3684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord0[ch] < 0)
3694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord0[ch] = 0;
3704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] >= (int) size)
3714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
372b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
376e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_clamp(const float s[4], unsigned size,
3794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                         int icoord0[4], int icoord1[4], float w[4])
3804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = fabsf(s[ch]);
3844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u >= 1.0F)
3854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = (float) size;
3864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
3874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u *= size;
3884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
3894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
391b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
395e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_clamp_to_edge(const float s[4], unsigned size,
3984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 int icoord0[4], int icoord1[4], float w[4])
3994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
4004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = fabsf(s[ch]);
4034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u >= 1.0F)
4044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = (float) size;
4054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
4064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u *= size;
4074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
4084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
4094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
4104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord0[ch] < 0)
4114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord0[ch] = 0;
4124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] >= (int) size)
4134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
414b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
4154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
4164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
4174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
418e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
4194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
4204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_clamp_to_border(const float s[4], unsigned size,
4214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                   int icoord0[4], int icoord1[4], float w[4])
4224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
4234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
4244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
4254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = fabsf(s[ch]);
4284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u <= min)
4294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = min * size;
4304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u >= max)
4314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = max * size;
4324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
4334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u *= size;
4344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
4354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
4364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
437b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
4380dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
4390dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
4400dc4eea64f56cc93e5359372b08b99a2d600273cBrian
4410dc4eea64f56cc93e5359372b08b99a2d600273cBrian
442b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian/**
443b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul * PIPE_TEX_WRAP_CLAMP for nearest sampling, unnormalized coords.
444b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian */
4454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
446e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_unorm_clamp(const float s[4], unsigned size, int icoord[4])
447b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
44838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
4494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int i = util_ifloor(s[ch]);
4514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord[ch]= CLAMP(i, 0, (int) size-1);
4524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
4534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
4544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
455e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
456e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
457b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul * PIPE_TEX_WRAP_CLAMP_TO_BORDER for nearest sampling, unnormalized coords.
4584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell */
4594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
4604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_nearest_unorm_clamp_to_border(const float s[4], unsigned size,
461e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                   int icoord[4])
4624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
4634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
465b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      icoord[ch]= util_ifloor( CLAMP(s[ch], -0.5F, (float) size + 0.5F) );
466b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul   }
467b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul}
468b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul
469b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul
470b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul/**
471b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul * PIPE_TEX_WRAP_CLAMP_TO_EDGE for nearest sampling, unnormalized coords.
472b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul */
473b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paulstatic void
474b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paulwrap_nearest_unorm_clamp_to_edge(const float s[4], unsigned size,
475b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul                                 int icoord[4])
476b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul{
477b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul   uint ch;
478b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul   for (ch = 0; ch < 4; ch++) {
4794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord[ch]= util_ifloor( CLAMP(s[ch], 0.5F, (float) size - 0.5F) );
480b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
481b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
482b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
483b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
484b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian/**
485b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul * PIPE_TEX_WRAP_CLAMP for linear sampling, unnormalized coords.
486b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian */
4874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
4884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_unorm_clamp(const float s[4], unsigned size,
489e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        int icoord0[4], int icoord1[4], float w[4])
490b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
49138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
4924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Not exactly what the spec says, but it matches NVIDIA output */
4944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch] - 0.5F, 0.0f, (float) size - 1.0f);
4954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
4964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
497b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
498b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
499b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
500b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
501e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
502b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul/**
503b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul * PIPE_TEX_WRAP_CLAMP_TO_BORDER for linear sampling, unnormalized coords.
504b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul */
5054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
506e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_linear_unorm_clamp_to_border(const float s[4], unsigned size,
507e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                  int icoord0[4], int icoord1[4], float w[4])
50834a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
5094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
5104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
511b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      float u = CLAMP(s[ch], -0.5F, (float) size + 0.5F);
512b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      u -= 0.5F;
513b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      icoord0[ch] = util_ifloor(u);
514b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      icoord1[ch] = icoord0[ch] + 1;
515b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      if (icoord1[ch] > (int) size - 1)
516b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul         icoord1[ch] = size - 1;
517b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      w[ch] = frac(u);
518b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul   }
519b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul}
520b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul
521b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul
522b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul/**
523b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul * PIPE_TEX_WRAP_CLAMP_TO_EDGE for linear sampling, unnormalized coords.
524b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul */
525b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paulstatic void
526b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paulwrap_linear_unorm_clamp_to_edge(const float s[4], unsigned size,
527b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul                                int icoord0[4], int icoord1[4], float w[4])
528b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul{
529b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul   uint ch;
530b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul   for (ch = 0; ch < 4; ch++) {
531b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      float u = CLAMP(s[ch], +0.5F, (float) size - 0.5F);
5324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
5334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
5344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
5354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] > (int) size - 1)
5364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
537b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
53834a48abd5ff82ce9748fc29191e35a0985d47c5fBrian   }
5394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
5404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
54134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
542779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul/**
543779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul * Do coordinate to array index conversion.  For array textures.
544779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul */
545779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulstatic INLINE void
546779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulwrap_array_layer(const float coord[4], unsigned size, int layer[4])
547779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul{
548779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   uint ch;
549779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   for (ch = 0; ch < 4; ch++) {
550779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      int c = util_ifloor(coord[ch] + 0.5F);
551779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      layer[ch] = CLAMP(c, 0, size - 1);
552779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
553779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul}
554779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
55534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
556b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian/**
557b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * Examine the quad's texture coordinates to compute the partial
558b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * derivatives w.r.t X and Y, then compute lambda (level of detail).
559b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian */
560b4480285ed5098f1c862690ee105dd46f5e6cd1eBrianstatic float
561f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulcompute_lambda_1d(const struct sp_sampler_variant *samp,
5624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float s[QUAD_SIZE],
5634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float t[QUAD_SIZE],
5644bfe1c955fe679547c8a03119d1681e33593c768Michal Krol                  const float p[QUAD_SIZE])
565b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian{
56621148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
5674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
5684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdy = fabsf(s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT]);
569683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   float rho = MAX2(dsdx, dsdy) * texture->width0;
570b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
5714bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   return util_fast_log2(rho);
572b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian}
573b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
574e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
5754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic float
576f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulcompute_lambda_2d(const struct sp_sampler_variant *samp,
5774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float s[QUAD_SIZE],
5784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float t[QUAD_SIZE],
5794bfe1c955fe679547c8a03119d1681e33593c768Michal Krol                  const float p[QUAD_SIZE])
58009a1b912605ff48c8782dcc5aae55ac77e27037bBrian{
58121148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
5824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
5834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdy = fabsf(s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT]);
5844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
5854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdy = fabsf(t[QUAD_TOP_LEFT]     - t[QUAD_BOTTOM_LEFT]);
586683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   float maxx = MAX2(dsdx, dsdy) * texture->width0;
587683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   float maxy = MAX2(dtdx, dtdy) * texture->height0;
5884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float rho  = MAX2(maxx, maxy);
589c7722edcfdf36e0d0bfdc51013ecb199fc7fa9f6Brian
5904bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   return util_fast_log2(rho);
59109a1b912605ff48c8782dcc5aae55ac77e27037bBrian}
59209a1b912605ff48c8782dcc5aae55ac77e27037bBrian
59308f33a025100dea2d951e6d628891fe294b18082Brian
5944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic float
595f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulcompute_lambda_3d(const struct sp_sampler_variant *samp,
5964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float s[QUAD_SIZE],
5974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float t[QUAD_SIZE],
5984bfe1c955fe679547c8a03119d1681e33593c768Michal Krol                  const float p[QUAD_SIZE])
59909a1b912605ff48c8782dcc5aae55ac77e27037bBrian{
60021148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
6014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
6024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdy = fabsf(s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT]);
6034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
6044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdy = fabsf(t[QUAD_TOP_LEFT]     - t[QUAD_BOTTOM_LEFT]);
6054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dpdx = fabsf(p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]);
6064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dpdy = fabsf(p[QUAD_TOP_LEFT]     - p[QUAD_BOTTOM_LEFT]);
607683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   float maxx = MAX2(dsdx, dsdy) * texture->width0;
608683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   float maxy = MAX2(dtdx, dtdy) * texture->height0;
609683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   float maxz = MAX2(dpdx, dpdy) * texture->depth0;
6104bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   float rho;
61100c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell
6124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   rho = MAX2(maxx, maxy);
6134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   rho = MAX2(rho, maxz);
614c7722edcfdf36e0d0bfdc51013ecb199fc7fa9f6Brian
6154bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   return util_fast_log2(rho);
61609a1b912605ff48c8782dcc5aae55ac77e27037bBrian}
61709a1b912605ff48c8782dcc5aae55ac77e27037bBrian
61808f33a025100dea2d951e6d628891fe294b18082Brian
619e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
620e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Compute lambda for a vertex texture sampler.
6214bfe1c955fe679547c8a03119d1681e33593c768Michal Krol * Since there aren't derivatives to use, just return 0.
622e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul */
6234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic float
624f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulcompute_lambda_vert(const struct sp_sampler_variant *samp,
6254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                    const float s[QUAD_SIZE],
6264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                    const float t[QUAD_SIZE],
6274bfe1c955fe679547c8a03119d1681e33593c768Michal Krol                    const float p[QUAD_SIZE])
6284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
6294bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   return 0.0f;
6304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
6314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
6324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
6334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
6344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell/**
6354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell * Get a texel from a texture, using the texture tile cache.
6364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell *
63781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell * \param addr  the template tex address containing cube, z, face info.
6384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell * \param x  the x coord of texel within 2D image
639b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * \param y  the y coord of texel within 2D image
640b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * \param rgba  the quad to put the texel/color into
64170eb7996f265f3634dabda078f13d1be3533cc65Brian *
64280c78472ad43f4288c9ef5076074ba9d31a39885Keith Whitwell * XXX maybe move this into sp_tex_tile_cache.c and merge with the
64370eb7996f265f3634dabda078f13d1be3533cc65Brian * sp_get_cached_tile_tex() function.  Also, get 4 texels instead of 1...
644b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian */
64581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
64681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
64781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
64881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
64981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE const float *
650f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_2d_no_border(const struct sp_sampler_variant *samp,
65181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		       union tex_tile_address addr, int x, int y)
652b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian{
65381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   const struct softpipe_tex_cached_tile *tile;
65481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
65581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.x = x / TILE_SIZE;
65681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.y = y / TILE_SIZE;
65781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   y %= TILE_SIZE;
65881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   x %= TILE_SIZE;
65981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
66081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   tile = sp_get_cached_tile_tex(samp->cache, addr);
66181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
66281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   return &tile->data.color[y][x][0];
66381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
66481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
66581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
66681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE const float *
667f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_2d(const struct sp_sampler_variant *samp,
66881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell	     union tex_tile_address addr, int x, int y)
66981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
67021148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
67181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   unsigned level = addr.bits.level;
67281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
673683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
674683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell       y < 0 || y >= (int) u_minify(texture->height0, level)) {
67542a0967a36ff3331de96289125243e6afeb5d560Brian Paul      return samp->sampler->border_color;
67681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   }
67781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   else {
67881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      return get_texel_2d_no_border( samp, addr, x, y );
67981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   }
68081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
6816142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
6826142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
68381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Gather a quad of adjacent texels within a tile:
68481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
68581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE void
686f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_quad_2d_no_border_single_tile(const struct sp_sampler_variant *samp,
68781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell					union tex_tile_address addr,
68881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell					unsigned x, unsigned y,
68981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell					const float *out[4])
69081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
69181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   const struct softpipe_tex_cached_tile *tile;
69281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
69381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.x = x / TILE_SIZE;
69481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.y = y / TILE_SIZE;
6956142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   y %= TILE_SIZE;
6966142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   x %= TILE_SIZE;
69781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
69881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   tile = sp_get_cached_tile_tex(samp->cache, addr);
6996142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7006142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[0] = &tile->data.color[y  ][x  ][0];
7016142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[1] = &tile->data.color[y  ][x+1][0];
7026142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[2] = &tile->data.color[y+1][x  ][0];
7036142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[3] = &tile->data.color[y+1][x+1][0];
7046142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
7056142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
70681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
70781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Gather a quad of potentially non-adjacent texels:
70881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
70981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE void
710f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_quad_2d_no_border(const struct sp_sampler_variant *samp,
71181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    union tex_tile_address addr,
71281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    int x0, int y0,
71381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    int x1, int y1,
71481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    const float *out[4])
71581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
71681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[0] = get_texel_2d_no_border( samp, addr, x0, y0 );
71781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[1] = get_texel_2d_no_border( samp, addr, x1, y0 );
71881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[2] = get_texel_2d_no_border( samp, addr, x0, y1 );
71981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[3] = get_texel_2d_no_border( samp, addr, x1, y1 );
72081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
72181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
72281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Can involve a lot of unnecessary checks for border color:
72381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
72481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE void
725f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_quad_2d(const struct sp_sampler_variant *samp,
72681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  union tex_tile_address addr,
72781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  int x0, int y0,
72881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  int x1, int y1,
72981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  const float *out[4])
73081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
73181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[0] = get_texel_2d( samp, addr, x0, y0 );
73281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[1] = get_texel_2d( samp, addr, x1, y0 );
73381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[3] = get_texel_2d( samp, addr, x1, y1 );
73481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[2] = get_texel_2d( samp, addr, x0, y1 );
73581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
73681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
73781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
73881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
739f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul/* 3d variants:
74081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
7416142de393fe34ff0866f8489f1292eb473276f11Keith Whitwellstatic INLINE const float *
742f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_3d_no_border(const struct sp_sampler_variant *samp,
743e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       union tex_tile_address addr, int x, int y, int z)
7446142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
745153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   const struct softpipe_tex_cached_tile *tile;
7466142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
747153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.x = x / TILE_SIZE;
748153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.y = y / TILE_SIZE;
74981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.z = z;
7506142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   y %= TILE_SIZE;
7516142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   x %= TILE_SIZE;
7526142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
753153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   tile = sp_get_cached_tile_tex(samp->cache, addr);
754153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
7556142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   return &tile->data.color[y][x][0];
7566142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
7576142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7586142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
75981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE const float *
760f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulget_texel_3d(const struct sp_sampler_variant *samp,
761e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul	     union tex_tile_address addr, int x, int y, int z)
762b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian{
76321148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
76481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   unsigned level = addr.bits.level;
7650b9e96fae9493d5d58f046e01c983a3c4267090eBrian
766683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   if (x < 0 || x >= (int) u_minify(texture->width0, level) ||
767683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell       y < 0 || y >= (int) u_minify(texture->height0, level) ||
768683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell       z < 0 || z >= (int) u_minify(texture->depth0, level)) {
76942a0967a36ff3331de96289125243e6afeb5d560Brian Paul      return samp->sampler->border_color;
770ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   }
771ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   else {
77281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      return get_texel_3d_no_border( samp, addr, x, y, z );
773ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   }
774b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian}
775b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
776b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
77775276ea316610a5737f2115326482024aa09d02aroot/**
77875276ea316610a5737f2115326482024aa09d02aroot * Given the logbase2 of a mipmap's base level size and a mipmap level,
77975276ea316610a5737f2115326482024aa09d02aroot * return the size (in texels) of that mipmap level.
78075276ea316610a5737f2115326482024aa09d02aroot * For example, if level[0].width = 256 then base_pot will be 8.
78175276ea316610a5737f2115326482024aa09d02aroot * If level = 2, then we'll return 64 (the width at level=2).
78275276ea316610a5737f2115326482024aa09d02aroot * Return 1 if level > base_pot.
78375276ea316610a5737f2115326482024aa09d02aroot */
78475276ea316610a5737f2115326482024aa09d02arootstatic INLINE unsigned
78575276ea316610a5737f2115326482024aa09d02arootpot_level_size(unsigned base_pot, unsigned level)
78675276ea316610a5737f2115326482024aa09d02aroot{
78775276ea316610a5737f2115326482024aa09d02aroot   return (base_pot >= level) ? (1 << (base_pot - level)) : 1;
78875276ea316610a5737f2115326482024aa09d02aroot}
78975276ea316610a5737f2115326482024aa09d02aroot
79075276ea316610a5737f2115326482024aa09d02aroot
7914250882ccf8326ba9074c671110370534489caa6Brian Paulstatic void
7924250882ccf8326ba9074c671110370534489caa6Brian Paulprint_sample(const char *function, float rgba[NUM_CHANNELS][QUAD_SIZE])
7934250882ccf8326ba9074c671110370534489caa6Brian Paul{
7944250882ccf8326ba9074c671110370534489caa6Brian Paul   debug_printf("%s %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
7954250882ccf8326ba9074c671110370534489caa6Brian Paul                function,
7964250882ccf8326ba9074c671110370534489caa6Brian Paul                rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],
7974250882ccf8326ba9074c671110370534489caa6Brian Paul                rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],
7984250882ccf8326ba9074c671110370534489caa6Brian Paul                rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],
7994250882ccf8326ba9074c671110370534489caa6Brian Paul                rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);
8004250882ccf8326ba9074c671110370534489caa6Brian Paul}
8014250882ccf8326ba9074c671110370534489caa6Brian Paul
8024250882ccf8326ba9074c671110370534489caa6Brian Paul
80381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Some image-filter fastpaths:
80481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
805efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paulstatic INLINE void
8064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
807e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                const float s[QUAD_SIZE],
808e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                const float t[QUAD_SIZE],
809e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                const float p[QUAD_SIZE],
8104440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                                const float c0[QUAD_SIZE],
8114440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                                enum tgsi_sampler_control control,
812e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                float rgba[NUM_CHANNELS][QUAD_SIZE])
8136142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
814f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
8156142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned  j;
8166142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned level = samp->level;
81775276ea316610a5737f2115326482024aa09d02aroot   unsigned xpot = pot_level_size(samp->xpot, level);
81875276ea316610a5737f2115326482024aa09d02aroot   unsigned ypot = pot_level_size(samp->ypot, level);
8191fd40e506c2207664f0c3f435e4614472ea4c540Keith Whitwell   unsigned xmax = (xpot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, xpot) - 1; */
8201fd40e506c2207664f0c3f435e4614472ea4c540Keith Whitwell   unsigned ymax = (ypot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, ypot) - 1; */
821153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   union tex_tile_address addr;
822153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
823153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.value = 0;
824153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.level = samp->level;
825153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
8266142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
8276142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int c;
8286142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8296142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float u = s[j] * xpot - 0.5F;
8306142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float v = t[j] * ypot - 0.5F;
8316142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8326142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int uflr = util_ifloor(u);
8336142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int vflr = util_ifloor(v);
8346142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8356142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float xw = u - (float)uflr;
8366142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float yw = v - (float)vflr;
8376142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8386142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int x0 = uflr & (xpot - 1);
8396142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int y0 = vflr & (ypot - 1);
8406142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
841153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell      const float *tx[4];
8426142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8436142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      /* Can we fetch all four at once:
8446142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell       */
845e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul      if (x0 < xmax && y0 < ymax) {
84681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         get_texel_quad_2d_no_border_single_tile(samp, addr, x0, y0, tx);
8476142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
848e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul      else {
8494f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell         unsigned x1 = (x0 + 1) & (xpot - 1);
8504f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell         unsigned y1 = (y0 + 1) & (ypot - 1);
85181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         get_texel_quad_2d_no_border(samp, addr, x0, y0, x1, y1, tx);
8526142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
8536142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8546142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      /* interpolate R, G, B, A */
8556142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      for (c = 0; c < 4; c++) {
8566142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         rgba[c][j] = lerp_2d(xw, yw,
8576142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell                              tx[0][c], tx[1][c],
8586142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell                              tx[2][c], tx[3][c]);
8596142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
8606142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
8614250882ccf8326ba9074c671110370534489caa6Brian Paul
8624250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
8634250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
8644250882ccf8326ba9074c671110370534489caa6Brian Paul   }
8656142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
8666142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8676142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic INLINE void
8694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
8704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const float s[QUAD_SIZE],
8714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const float t[QUAD_SIZE],
8724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const float p[QUAD_SIZE],
8734440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                                 const float c0[QUAD_SIZE],
8744440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                                 enum tgsi_sampler_control control,
8754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 float rgba[NUM_CHANNELS][QUAD_SIZE])
8766142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
877f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
8786142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned  j;
8796142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned level = samp->level;
88075276ea316610a5737f2115326482024aa09d02aroot   unsigned xpot = pot_level_size(samp->xpot, level);
88175276ea316610a5737f2115326482024aa09d02aroot   unsigned ypot = pot_level_size(samp->ypot, level);
882153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   union tex_tile_address addr;
883153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
884153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.value = 0;
885153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.level = samp->level;
8866142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8876142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
8886142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int c;
8896142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8905fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float u = s[j] * xpot;
8915fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float v = t[j] * ypot;
8926142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8936142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int uflr = util_ifloor(u);
8946142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int vflr = util_ifloor(v);
8956142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8966142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int x0 = uflr & (xpot - 1);
8976142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int y0 = vflr & (ypot - 1);
8986142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
89981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_2d_no_border(samp, addr, x0, y0);
9006142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9016142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      for (c = 0; c < 4; c++) {
9026142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         rgba[c][j] = out[c];
9036142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
9046142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
9054250882ccf8326ba9074c671110370534489caa6Brian Paul
9064250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
9074250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
9084250882ccf8326ba9074c671110370534489caa6Brian Paul   }
9096142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
9106142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9116142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic INLINE void
9134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
9144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                const float s[QUAD_SIZE],
9154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                const float t[QUAD_SIZE],
9164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                const float p[QUAD_SIZE],
9174440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                                const float c0[QUAD_SIZE],
9184440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                                enum tgsi_sampler_control control,
9194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                float rgba[NUM_CHANNELS][QUAD_SIZE])
9206142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
921f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
9226142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned  j;
9236142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned level = samp->level;
92475276ea316610a5737f2115326482024aa09d02aroot   unsigned xpot = pot_level_size(samp->xpot, level);
92575276ea316610a5737f2115326482024aa09d02aroot   unsigned ypot = pot_level_size(samp->ypot, level);
926153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   union tex_tile_address addr;
927153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
928153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.value = 0;
929153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.level = samp->level;
9306142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9316142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
9326142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int c;
9336142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9345fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float u = s[j] * xpot;
9355fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float v = t[j] * ypot;
9366142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9376142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int x0, y0;
9386142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      const float *out;
9396142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9406142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      x0 = util_ifloor(u);
9416142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      if (x0 < 0)
9426142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         x0 = 0;
9436142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      else if (x0 > xpot - 1)
9446142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         x0 = xpot - 1;
9456142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9466142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      y0 = util_ifloor(v);
9476142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      if (y0 < 0)
9486142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         y0 = 0;
9496142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      else if (y0 > ypot - 1)
9506142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         y0 = ypot - 1;
9516142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
95281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      out = get_texel_2d_no_border(samp, addr, x0, y0);
9536142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9546142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      for (c = 0; c < 4; c++) {
9556142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         rgba[c][j] = out[c];
9566142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
9576142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
9584250882ccf8326ba9074c671110370534489caa6Brian Paul
9594250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
9604250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
9614250882ccf8326ba9074c671110370534489caa6Brian Paul   }
9626142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
9636142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
964e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
9656142de393fe34ff0866f8489f1292eb473276f11Keith Whitwellstatic void
9664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_1d_nearest(struct tgsi_sampler *tgsi_sampler,
9674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                        const float s[QUAD_SIZE],
9684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                        const float t[QUAD_SIZE],
9694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                        const float p[QUAD_SIZE],
9704440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                        const float c0[QUAD_SIZE],
9714440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                        enum tgsi_sampler_control control,
9724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                        float rgba[NUM_CHANNELS][QUAD_SIZE])
9736142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
974f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
97521148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
9764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
9774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int width;
9784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x[4];
97981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
9806142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
982683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
9836142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(width > 0);
9854f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell
98681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
98781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
98881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
9894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_s(s, width, x);
9904f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell
9914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
99281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_2d(samp, addr, x[j], 0);
99381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      int c;
99481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      for (c = 0; c < 4; c++) {
99581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = out[c];
99681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      }
9974f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell   }
9984250882ccf8326ba9074c671110370534489caa6Brian Paul
9994250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
10004250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
10014250882ccf8326ba9074c671110370534489caa6Brian Paul   }
10026142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
10036142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
10044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
100560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellstatic void
1006779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulimg_filter_1d_array_nearest(struct tgsi_sampler *tgsi_sampler,
1007779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float s[QUAD_SIZE],
1008779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float t[QUAD_SIZE],
1009779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float p[QUAD_SIZE],
1010779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float c0[QUAD_SIZE],
1011779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            enum tgsi_sampler_control control,
1012779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            float rgba[NUM_CHANNELS][QUAD_SIZE])
1013779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul{
1014779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1015779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct pipe_resource *texture = samp->view->texture;
1016779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   unsigned level0, j;
1017779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int width;
1018779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int x[4], layer[4];
1019779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   union tex_tile_address addr;
1020779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1021779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   level0 = samp->level;
1022779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   width = u_minify(texture->width0, level0);
1023779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1024779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   assert(width > 0);
1025779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1026779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.value = 0;
1027779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.bits.level = samp->level;
1028779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1029779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   samp->nearest_texcoord_s(s, width, x);
1030779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   wrap_array_layer(t, texture->height0, layer);
1031779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1032779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   for (j = 0; j < QUAD_SIZE; j++) {
1033779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *out = get_texel_2d(samp, addr, x[j], layer[j]);
1034779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      int c;
1035779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      for (c = 0; c < 4; c++) {
1036779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         rgba[c][j] = out[c];
1037779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      }
1038779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
1039779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1040779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   if (DEBUG_TEX) {
1041779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      print_sample(__FUNCTION__, rgba);
1042779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
1043779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul}
1044779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1045779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1046779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulstatic void
104760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_2d_nearest(struct tgsi_sampler *tgsi_sampler,
104860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      const float s[QUAD_SIZE],
104960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      const float t[QUAD_SIZE],
105060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      const float p[QUAD_SIZE],
10514440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                      const float c0[QUAD_SIZE],
10524440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                      enum tgsi_sampler_control control,
105360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      float rgba[NUM_CHANNELS][QUAD_SIZE])
105460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell{
1055f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
105621148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
105760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   unsigned level0, j;
105860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int width, height;
105960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int x[4], y[4];
106060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   union tex_tile_address addr;
106181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
106281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
106360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   level0 = samp->level;
1064683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
1065683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   height = u_minify(texture->height0, level0);
106660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
106760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   assert(width > 0);
10685dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
106960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
107060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.value = 0;
107160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.bits.level = samp->level;
107260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
107360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->nearest_texcoord_s(s, width, x);
107460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->nearest_texcoord_t(t, height, y);
107560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
107660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
107760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *out = get_texel_2d(samp, addr, x[j], y[j]);
107860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      int c;
107960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      for (c = 0; c < 4; c++) {
108060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         rgba[c][j] = out[c];
108160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      }
108260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   }
10834250882ccf8326ba9074c671110370534489caa6Brian Paul
10844250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
10854250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
10864250882ccf8326ba9074c671110370534489caa6Brian Paul   }
108760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell}
108860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
1089e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1090779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulstatic void
1091779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulimg_filter_2d_array_nearest(struct tgsi_sampler *tgsi_sampler,
1092779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float s[QUAD_SIZE],
1093779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float t[QUAD_SIZE],
1094779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float p[QUAD_SIZE],
1095779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            const float c0[QUAD_SIZE],
1096779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            enum tgsi_sampler_control control,
1097779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                            float rgba[NUM_CHANNELS][QUAD_SIZE])
1098779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul{
1099779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1100779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct pipe_resource *texture = samp->view->texture;
1101779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   unsigned level0, j;
1102779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int width, height;
1103779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int x[4], y[4], layer[4];
1104779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   union tex_tile_address addr;
1105779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1106779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   level0 = samp->level;
1107779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   width = u_minify(texture->width0, level0);
1108779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   height = u_minify(texture->height0, level0);
1109779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1110779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   assert(width > 0);
1111779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   assert(height > 0);
1112779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1113779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.value = 0;
1114779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.bits.level = samp->level;
1115779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1116779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   samp->nearest_texcoord_s(s, width, x);
1117779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   samp->nearest_texcoord_t(t, height, y);
1118779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   wrap_array_layer(p, texture->depth0, layer);
1119779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1120779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   for (j = 0; j < QUAD_SIZE; j++) {
1121779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *out = get_texel_3d(samp, addr, x[j], y[j], layer[j]);
1122779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      int c;
1123779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      for (c = 0; c < 4; c++) {
1124779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         rgba[c][j] = out[c];
1125779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      }
1126779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
1127779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1128779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   if (DEBUG_TEX) {
1129779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      print_sample(__FUNCTION__, rgba);
1130779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
1131779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul}
1132779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1133779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
11349659aa6482291d1530c74450612bcd952f542e01José Fonsecastatic INLINE union tex_tile_address
1135e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulface(union tex_tile_address addr, unsigned face )
113681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
113781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.face = face;
113881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   return addr;
113981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
114081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
1141e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1142e12810d92ffb3547680b227bf88937c03018112bBrianstatic void
114360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_cube_nearest(struct tgsi_sampler *tgsi_sampler,
1144e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        const float s[QUAD_SIZE],
1145e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        const float t[QUAD_SIZE],
1146e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        const float p[QUAD_SIZE],
11474440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                        const float c0[QUAD_SIZE],
11484440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                        enum tgsi_sampler_control control,
1149e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        float rgba[NUM_CHANNELS][QUAD_SIZE])
11500dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
1151f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
115221148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
11534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const unsigned *faces = samp->faces; /* zero when not cube-mapping */
11544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
1155f9e331a574cc4eba60e0de5a29a4aed4bb40520cBrian   int width, height;
11564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x[4], y[4];
115781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
115881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
11594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
1160683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
1161683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   height = u_minify(texture->height0, level0);
116209a1b912605ff48c8782dcc5aae55ac77e27037bBrian
1163b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   assert(width > 0);
11645dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
116581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
116681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
116781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
1168612cfb749c3526eeb446bbc631bf24716522f373Brian
11694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_s(s, width, x);
11704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_t(t, height, y);
1171f9e331a574cc4eba60e0de5a29a4aed4bb40520cBrian
11724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
117381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_2d(samp, face(addr, faces[j]), x[j], y[j]);
117481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      int c;
117581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      for (c = 0; c < 4; c++) {
117681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = out[c];
117781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      }
11780dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
11794250882ccf8326ba9074c671110370534489caa6Brian Paul
11804250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
11814250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
11824250882ccf8326ba9074c671110370534489caa6Brian Paul   }
11830dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
118434a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
118534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
11864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
11874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_3d_nearest(struct tgsi_sampler *tgsi_sampler,
11884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      const float s[QUAD_SIZE],
11894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      const float t[QUAD_SIZE],
11904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      const float p[QUAD_SIZE],
11914440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                      const float c0[QUAD_SIZE],
11924440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                      enum tgsi_sampler_control control,
11934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      float rgba[NUM_CHANNELS][QUAD_SIZE])
119434a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
1195f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
119621148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
11974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
11983d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   int width, height, depth;
11994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x[4], y[4], z[4];
120081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
1201b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
12024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
1203683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
1204683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   height = u_minify(texture->height0, level0);
1205683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   depth = u_minify(texture->depth0, level0);
12063d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian
12073d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   assert(width > 0);
12083d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   assert(height > 0);
12093d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   assert(depth > 0);
12103d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian
12114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_s(s, width,  x);
12124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_t(t, height, y);
12134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_p(p, depth,  z);
12143d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian
121581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
121681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
121781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
12184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
121981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_3d(samp, addr, x[j], y[j], z[j]);
122081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      int c;
122181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      for (c = 0; c < 4; c++) {
122281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = out[c];
122381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      }
12243d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   }
122534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
122634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
122734a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
122834a48abd5ff82ce9748fc29191e35a0985d47c5fBrianstatic void
12294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_1d_linear(struct tgsi_sampler *tgsi_sampler,
12304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float s[QUAD_SIZE],
12314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float t[QUAD_SIZE],
12324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float p[QUAD_SIZE],
12334440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                     const float c0[QUAD_SIZE],
12344440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                     enum tgsi_sampler_control control,
12354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     float rgba[NUM_CHANNELS][QUAD_SIZE])
123634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
1237f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
123821148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
12394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
12404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int width;
12414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x0[4], x1[4];
12424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float xw[4]; /* weights */
124381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
12444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
1246683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
12474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(width > 0);
12494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
125081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
125181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
125281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
12534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->linear_texcoord_s(s, width, x0, x1, xw);
12544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1255b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   for (j = 0; j < QUAD_SIZE; j++) {
125681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx0 = get_texel_2d(samp, addr, x0[j], 0);
125781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx1 = get_texel_2d(samp, addr, x1[j], 0);
12584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c;
12594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* interpolate R, G, B, A */
12614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (c = 0; c < 4; c++) {
126281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
12634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
1264b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   }
126534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
126634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
126781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
126860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellstatic void
1269779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulimg_filter_1d_array_linear(struct tgsi_sampler *tgsi_sampler,
1270779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float s[QUAD_SIZE],
1271779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float t[QUAD_SIZE],
1272779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float p[QUAD_SIZE],
1273779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float c0[QUAD_SIZE],
1274779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           enum tgsi_sampler_control control,
1275779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           float rgba[NUM_CHANNELS][QUAD_SIZE])
1276779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul{
1277779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1278779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct pipe_resource *texture = samp->view->texture;
1279779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   unsigned level0, j;
1280779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int width;
1281779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int x0[4], x1[4], layer[4];
1282779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   float xw[4]; /* weights */
1283779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   union tex_tile_address addr;
1284779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1285779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   level0 = samp->level;
1286779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   width = u_minify(texture->width0, level0);
1287779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1288779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   assert(width > 0);
1289779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1290779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.value = 0;
1291779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.bits.level = samp->level;
1292779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1293779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   samp->linear_texcoord_s(s, width, x0, x1, xw);
1294779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   wrap_array_layer(t, texture->height0, layer);
1295779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1296779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   for (j = 0; j < QUAD_SIZE; j++) {
1297779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *tx0 = get_texel_2d(samp, addr, x0[j], layer[j]);
1298779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *tx1 = get_texel_2d(samp, addr, x1[j], layer[j]);
1299779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      int c;
1300779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1301779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      /* interpolate R, G, B, A */
1302779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      for (c = 0; c < 4; c++) {
1303779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
1304779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      }
1305779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
1306779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul}
1307779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1308779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1309779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulstatic void
131060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_2d_linear(struct tgsi_sampler *tgsi_sampler,
131160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     const float s[QUAD_SIZE],
131260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     const float t[QUAD_SIZE],
131360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     const float p[QUAD_SIZE],
13144440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                     const float c0[QUAD_SIZE],
13154440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                     enum tgsi_sampler_control control,
131660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     float rgba[NUM_CHANNELS][QUAD_SIZE])
131760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell{
1318f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
131921148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
132060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   unsigned level0, j;
132160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int width, height;
132260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int x0[4], y0[4], x1[4], y1[4];
132360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   float xw[4], yw[4]; /* weights */
132460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   union tex_tile_address addr;
132560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
132660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   level0 = samp->level;
1327683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
1328683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   height = u_minify(texture->height0, level0);
132960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
133060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   assert(width > 0);
13315dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
133260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
133360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.value = 0;
133460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.bits.level = samp->level;
133560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
133660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->linear_texcoord_s(s, width,  x0, x1, xw);
133760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->linear_texcoord_t(t, height, y0, y1, yw);
133860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
133960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
134060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx0 = get_texel_2d(samp, addr, x0[j], y0[j]);
134160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx1 = get_texel_2d(samp, addr, x1[j], y0[j]);
134260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx2 = get_texel_2d(samp, addr, x0[j], y1[j]);
134360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx3 = get_texel_2d(samp, addr, x1[j], y1[j]);
134460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      int c;
134560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
134660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      /* interpolate R, G, B, A */
134760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      for (c = 0; c < 4; c++) {
134860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         rgba[c][j] = lerp_2d(xw[j], yw[j],
134960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                              tx0[c], tx1[c],
135060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                              tx2[c], tx3[c]);
135160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      }
135260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   }
135360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell}
135481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
135581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
1356b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrianstatic void
1357779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulimg_filter_2d_array_linear(struct tgsi_sampler *tgsi_sampler,
1358779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float s[QUAD_SIZE],
1359779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float t[QUAD_SIZE],
1360779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float p[QUAD_SIZE],
1361779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           const float c0[QUAD_SIZE],
1362779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           enum tgsi_sampler_control control,
1363779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                           float rgba[NUM_CHANNELS][QUAD_SIZE])
1364779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul{
1365779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1366779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   const struct pipe_resource *texture = samp->view->texture;
1367779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   unsigned level0, j;
1368779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int width, height;
1369779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   int x0[4], y0[4], x1[4], y1[4], layer[4];
1370779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   float xw[4], yw[4]; /* weights */
1371779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   union tex_tile_address addr;
1372779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1373779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   level0 = samp->level;
1374779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   width = u_minify(texture->width0, level0);
1375779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   height = u_minify(texture->height0, level0);
1376779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1377779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   assert(width > 0);
1378779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   assert(height > 0);
1379779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1380779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.value = 0;
1381779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   addr.bits.level = samp->level;
1382779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1383779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   samp->linear_texcoord_s(s, width,  x0, x1, xw);
1384779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   samp->linear_texcoord_t(t, height, y0, y1, yw);
1385779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   wrap_array_layer(p, texture->depth0, layer);
1386779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1387779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   for (j = 0; j < QUAD_SIZE; j++) {
1388779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *tx0 = get_texel_3d(samp, addr, x0[j], y0[j], layer[j]);
1389779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *tx1 = get_texel_3d(samp, addr, x1[j], y0[j], layer[j]);
1390779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *tx2 = get_texel_3d(samp, addr, x0[j], y1[j], layer[j]);
1391779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      const float *tx3 = get_texel_3d(samp, addr, x1[j], y1[j], layer[j]);
1392779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      int c;
1393779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1394779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      /* interpolate R, G, B, A */
1395779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      for (c = 0; c < 4; c++) {
1396779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         rgba[c][j] = lerp_2d(xw[j], yw[j],
1397779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                              tx0[c], tx1[c],
1398779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul                              tx2[c], tx3[c]);
1399779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      }
1400779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   }
1401779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul}
1402779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1403779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul
1404779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paulstatic void
140560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_cube_linear(struct tgsi_sampler *tgsi_sampler,
1406e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       const float s[QUAD_SIZE],
1407e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       const float t[QUAD_SIZE],
1408e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       const float p[QUAD_SIZE],
14094440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                       const float c0[QUAD_SIZE],
14104440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                       enum tgsi_sampler_control control,
1411e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       float rgba[NUM_CHANNELS][QUAD_SIZE])
1412b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
1413f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
141421148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
14154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const unsigned *faces = samp->faces; /* zero when not cube-mapping */
14164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
1417b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   int width, height;
14184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x0[4], y0[4], x1[4], y1[4];
14194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float xw[4], yw[4]; /* weights */
142081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
1421b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
14224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
1423683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
1424683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   height = u_minify(texture->height0, level0);
1425b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1426b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   assert(width > 0);
14275dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
1428b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
142981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
143081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
143181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
14324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->linear_texcoord_s(s, width,  x0, x1, xw);
14334e5c385d2183e7006c9d7ac0823919156bd4b8e6Brian Paul   samp->linear_texcoord_t(t, height, y0, y1, yw);
14344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
143681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      union tex_tile_address addrj = face(addr, faces[j]);
143781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx0 = get_texel_2d(samp, addrj, x0[j], y0[j]);
143881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx1 = get_texel_2d(samp, addrj, x1[j], y0[j]);
143981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx2 = get_texel_2d(samp, addrj, x0[j], y1[j]);
144081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx3 = get_texel_2d(samp, addrj, x1[j], y1[j]);
14414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c;
14424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* interpolate R, G, B, A */
14444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (c = 0; c < 4; c++) {
14454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         rgba[c][j] = lerp_2d(xw[j], yw[j],
144681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx0[c], tx1[c],
144781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx2[c], tx3[c]);
1448b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      }
14494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
14504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
14514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
14544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_3d_linear(struct tgsi_sampler *tgsi_sampler,
14554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float s[QUAD_SIZE],
14564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float t[QUAD_SIZE],
14574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float p[QUAD_SIZE],
14584440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                     const float c0[QUAD_SIZE],
14594440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                     enum tgsi_sampler_control control,
14604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     float rgba[NUM_CHANNELS][QUAD_SIZE])
14614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1462f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   const struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
146321148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
14644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
14654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int width, height, depth;
14664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
14674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float xw[4], yw[4], zw[4]; /* interpolation weights */
146881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
14694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
1471683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   width = u_minify(texture->width0, level0);
1472683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   height = u_minify(texture->height0, level0);
1473683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   depth = u_minify(texture->depth0, level0);
14744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
147581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
147681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = level0;
147781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
14784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(width > 0);
14794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(height > 0);
14804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(depth > 0);
14814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->linear_texcoord_s(s, width,  x0, x1, xw);
14834e5c385d2183e7006c9d7ac0823919156bd4b8e6Brian Paul   samp->linear_texcoord_t(t, height, y0, y1, yw);
14844e5c385d2183e7006c9d7ac0823919156bd4b8e6Brian Paul   samp->linear_texcoord_p(p, depth,  z0, z1, zw);
14854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
14874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c;
14884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
148981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx00 = get_texel_3d(samp, addr, x0[j], y0[j], z0[j]);
149081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx01 = get_texel_3d(samp, addr, x1[j], y0[j], z0[j]);
149181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx02 = get_texel_3d(samp, addr, x0[j], y1[j], z0[j]);
149281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx03 = get_texel_3d(samp, addr, x1[j], y1[j], z0[j]);
149381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
149481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx10 = get_texel_3d(samp, addr, x0[j], y0[j], z1[j]);
149581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx11 = get_texel_3d(samp, addr, x1[j], y0[j], z1[j]);
149681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx12 = get_texel_3d(samp, addr, x0[j], y1[j], z1[j]);
149781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx13 = get_texel_3d(samp, addr, x1[j], y1[j], z1[j]);
149881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
14994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* interpolate R, G, B, A */
15004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (c = 0; c < 4; c++) {
15014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
150281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx00[c], tx01[c],
150381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx02[c], tx03[c],
150481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx10[c], tx11[c],
150581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx12[c], tx13[c]);
15064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
15074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
15084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
15094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15114bfe1c955fe679547c8a03119d1681e33593c768Michal Krol/* Calculate level of detail for every fragment.
15124bfe1c955fe679547c8a03119d1681e33593c768Michal Krol * Note that lambda has already been biased by global LOD bias.
15134bfe1c955fe679547c8a03119d1681e33593c768Michal Krol */
15144bfe1c955fe679547c8a03119d1681e33593c768Michal Krolstatic INLINE void
15154bfe1c955fe679547c8a03119d1681e33593c768Michal Krolcompute_lod(const struct pipe_sampler_state *sampler,
15164bfe1c955fe679547c8a03119d1681e33593c768Michal Krol            const float biased_lambda,
15174bfe1c955fe679547c8a03119d1681e33593c768Michal Krol            const float lodbias[QUAD_SIZE],
15184bfe1c955fe679547c8a03119d1681e33593c768Michal Krol            float lod[QUAD_SIZE])
15194bfe1c955fe679547c8a03119d1681e33593c768Michal Krol{
15204bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   uint i;
15214bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
15224bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   for (i = 0; i < QUAD_SIZE; i++) {
15234bfe1c955fe679547c8a03119d1681e33593c768Michal Krol      lod[i] = biased_lambda + lodbias[i];
15244bfe1c955fe679547c8a03119d1681e33593c768Michal Krol      lod[i] = CLAMP(lod[i], sampler->min_lod, sampler->max_lod);
15254bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   }
15264bfe1c955fe679547c8a03119d1681e33593c768Michal Krol}
15274bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
15284bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
15294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
15304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_linear(struct tgsi_sampler *tgsi_sampler,
1531e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  const float s[QUAD_SIZE],
1532e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  const float t[QUAD_SIZE],
1533e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  const float p[QUAD_SIZE],
15344440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                  const float c0[QUAD_SIZE],
15354440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                  enum tgsi_sampler_control control,
1536e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  float rgba[NUM_CHANNELS][QUAD_SIZE])
15374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1538f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
153921148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
15404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int level0;
15414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
15424bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   float lod[QUAD_SIZE];
15434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15444440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   if (control == tgsi_sampler_lod_bias) {
15454440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
15464440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      compute_lod(samp->sampler, lambda, c0, lod);
15474440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   } else {
15484440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      assert(control == tgsi_sampler_lod_explicit);
15494bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
15504440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      memcpy(lod, c0, sizeof(lod));
15514440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   }
15524bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
15534bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   /* XXX: Take into account all lod values.
15544bfe1c955fe679547c8a03119d1681e33593c768Michal Krol    */
15554bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   lambda = lod[0];
15564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = (int)lambda;
15574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (lambda < 0.0) {
15594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = 0;
15604440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
15614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
15624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else if (level0 >= texture->last_level) {
15634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = texture->last_level;
15644440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
15654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
15664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
15674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float levelBlend = lambda - level0;
15684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba0[4][4];
15694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba1[4][4];
15704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c,j;
15714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0;
15734440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
15744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0+1;
15764440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
15774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
15784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (j = 0; j < QUAD_SIZE; j++) {
15794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         for (c = 0; c < 4; c++) {
15804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1581b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian         }
1582b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      }
1583b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
15844250882ccf8326ba9074c671110370534489caa6Brian Paul
15854250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
15864250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
15874250882ccf8326ba9074c671110370534489caa6Brian Paul   }
1588b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
1589b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1590b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
159185425b3b609c480cd024b217b1efd0b9153bed58Brian Paul/**
159285425b3b609c480cd024b217b1efd0b9153bed58Brian Paul * Compute nearest mipmap level from texcoords.
159385425b3b609c480cd024b217b1efd0b9153bed58Brian Paul * Then sample the texture level for four elements of a quad.
159485425b3b609c480cd024b217b1efd0b9153bed58Brian Paul * \param c0  the LOD bias factors, or absolute LODs (depending on control)
159585425b3b609c480cd024b217b1efd0b9153bed58Brian Paul */
15964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
15974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_nearest(struct tgsi_sampler *tgsi_sampler,
15984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   const float s[QUAD_SIZE],
15994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   const float t[QUAD_SIZE],
16004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   const float p[QUAD_SIZE],
16014440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                   const float c0[QUAD_SIZE],
16024440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                   enum tgsi_sampler_control control,
16034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   float rgba[NUM_CHANNELS][QUAD_SIZE])
16044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1605f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
160621148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
16074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
16084bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   float lod[QUAD_SIZE];
16094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16104440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   if (control == tgsi_sampler_lod_bias) {
16114440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
16124440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      compute_lod(samp->sampler, lambda, c0, lod);
16134440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   } else {
16144440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      assert(control == tgsi_sampler_lod_explicit);
16154bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
16164440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      memcpy(lod, c0, sizeof(lod));
16174440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   }
16184bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
16194bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   /* XXX: Take into account all lod values.
16204bfe1c955fe679547c8a03119d1681e33593c768Michal Krol    */
16214bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   lambda = lod[0];
16224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (lambda < 0.0) {
16244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = 0;
16254440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
16264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
16274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
16284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = (int)(lambda + 0.5) ;
16294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = MIN2(samp->level, (int)texture->last_level);
16304440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
16314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
163275276ea316610a5737f2115326482024aa09d02aroot
16334250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
16344250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
16354250882ccf8326ba9074c671110370534489caa6Brian Paul   }
16364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
16374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
16404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_none(struct tgsi_sampler *tgsi_sampler,
16414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                const float s[QUAD_SIZE],
16424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                const float t[QUAD_SIZE],
16434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                const float p[QUAD_SIZE],
16444440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                const float c0[QUAD_SIZE],
16454440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol                enum tgsi_sampler_control control,
16464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                float rgba[NUM_CHANNELS][QUAD_SIZE])
16474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1648f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
16494bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   float lambda;
16504bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   float lod[QUAD_SIZE];
16514bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
16524440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   if (control == tgsi_sampler_lod_bias) {
16534440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
16544440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      compute_lod(samp->sampler, lambda, c0, lod);
16554440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   } else {
16564440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      assert(control == tgsi_sampler_lod_explicit);
16574bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
16584440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      memcpy(lod, c0, sizeof(lod));
16594440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   }
16604bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
16614bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   /* XXX: Take into account all lod values.
16624bfe1c955fe679547c8a03119d1681e33593c768Michal Krol    */
16634bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   lambda = lod[0];
16644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (lambda < 0.0) {
16664440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->mag_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
16674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
16684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
16694440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      samp->min_img_filter(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
16704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
16714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
16724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1675e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
1676e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Specialized version of mip_filter_linear with hard-wired calls to
16774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1678a34b8594b7b2d00404bb639621ec1ce918ba0786Brian */
16794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
16804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_linear_2d_linear_repeat_POT(
16814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct tgsi_sampler *tgsi_sampler,
16824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float s[QUAD_SIZE],
16834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float t[QUAD_SIZE],
16844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float p[QUAD_SIZE],
16854440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   const float c0[QUAD_SIZE],
16864440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   enum tgsi_sampler_control control,
16874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float rgba[NUM_CHANNELS][QUAD_SIZE])
168800c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell{
1689f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
169021148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = samp->view->texture;
16914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int level0;
16924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
16934bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   float lod[QUAD_SIZE];
16944bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
16954440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   if (control == tgsi_sampler_lod_bias) {
16964440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      lambda = samp->compute_lambda(samp, s, t, p) + samp->sampler->lod_bias;
16974440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      compute_lod(samp->sampler, lambda, c0, lod);
16984440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   } else {
16994440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      assert(control == tgsi_sampler_lod_explicit);
170000c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell
17014440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      memcpy(lod, c0, sizeof(lod));
17024440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   }
17034bfe1c955fe679547c8a03119d1681e33593c768Michal Krol
17044bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   /* XXX: Take into account all lod values.
17054bfe1c955fe679547c8a03119d1681e33593c768Michal Krol    */
17064bfe1c955fe679547c8a03119d1681e33593c768Michal Krol   lambda = lod[0];
17074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = (int)lambda;
17084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
17094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* Catches both negative and large values of level0:
17104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    */
17114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if ((unsigned)level0 >= texture->last_level) {
17124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (level0 < 0)
17134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->level = 0;
17144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
17154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->level = texture->last_level;
17164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
17174440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba);
17184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
17194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
17204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float levelBlend = lambda - level0;
17214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba0[4][4];
17224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba1[4][4];
17234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c,j;
17244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
17254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0;
17264440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba0);
17274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
17284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0+1;
17294440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol      img_filter_2d_linear_repeat_POT(tgsi_sampler, s, t, p, NULL, tgsi_sampler_lod_bias, rgba1);
17304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
17314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (j = 0; j < QUAD_SIZE; j++) {
17324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         for (c = 0; c < 4; c++) {
17334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
17344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
1735b1ff7dac537947d412bf423a73e7eacd76f90d84Brian Paul      }
1736ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   }
17374250882ccf8326ba9074c671110370534489caa6Brian Paul
17384250882ccf8326ba9074c671110370534489caa6Brian Paul   if (DEBUG_TEX) {
17394250882ccf8326ba9074c671110370534489caa6Brian Paul      print_sample(__FUNCTION__, rgba);
17404250882ccf8326ba9074c671110370534489caa6Brian Paul   }
1741b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian}
1742b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
1743b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
17444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1745e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
1746e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Do shadow/depth comparisons.
17473d8c05f7320151898dd224c1daaf3118e1f7ea34Brian */
17484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
17494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellsample_compare(struct tgsi_sampler *tgsi_sampler,
1750b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian               const float s[QUAD_SIZE],
1751b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian               const float t[QUAD_SIZE],
17523d8c05f7320151898dd224c1daaf3118e1f7ea34Brian               const float p[QUAD_SIZE],
17534440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol               const float c0[QUAD_SIZE],
17544440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol               enum tgsi_sampler_control control,
1755b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian               float rgba[NUM_CHANNELS][QUAD_SIZE])
17563d8c05f7320151898dd224c1daaf3118e1f7ea34Brian{
1757f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1758aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
17594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int j, k0, k1, k2, k3;
17604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float val;
17613d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
17624440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   samp->mip_filter(tgsi_sampler, s, t, p, c0, control, rgba);
17633d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
17644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /**
17654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
17664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * When we sampled the depth texture, the depth value was put into all
17674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * RGBA channels.  We look at the red channel here.
17684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    */
1769efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1770efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   /* compare four texcoords vs. four texture samples */
1771efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   switch (sampler->compare_func) {
1772efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_LESS:
1773efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] < rgba[0][0];
1774efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] < rgba[0][1];
1775efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] < rgba[0][2];
1776efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] < rgba[0][3];
1777efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1778efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_LEQUAL:
1779efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] <= rgba[0][0];
1780efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] <= rgba[0][1];
1781efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] <= rgba[0][2];
1782efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] <= rgba[0][3];
1783efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1784efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_GREATER:
1785efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] > rgba[0][0];
1786efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] > rgba[0][1];
1787efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] > rgba[0][2];
1788efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] > rgba[0][3];
1789efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1790efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_GEQUAL:
1791efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] >= rgba[0][0];
1792efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] >= rgba[0][1];
1793efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] >= rgba[0][2];
1794efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] >= rgba[0][3];
1795efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1796efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_EQUAL:
1797efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] == rgba[0][0];
1798efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] == rgba[0][1];
1799efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] == rgba[0][2];
1800efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] == rgba[0][3];
1801efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1802efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_NOTEQUAL:
1803efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] != rgba[0][0];
1804efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] != rgba[0][1];
1805efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] != rgba[0][2];
1806efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] != rgba[0][3];
1807efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1808efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_ALWAYS:
1809efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = k1 = k2 = k3 = 1;
1810efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1811efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_NEVER:
1812efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = k1 = k2 = k3 = 0;
1813efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1814efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   default:
1815efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = k1 = k2 = k3 = 0;
1816efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      assert(0);
1817efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1818efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   }
1819efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1820efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   /* convert four pass/fail values to an intensity in [0,1] */
1821efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   val = 0.25F * (k0 + k1 + k2 + k3);
1822efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1823efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1824efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   for (j = 0; j < 4; j++) {
1825efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
1826efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      rgba[3][j] = 1.0F;
1827efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   }
1828efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul}
1829efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1830e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1831e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
183285425b3b609c480cd024b217b1efd0b9153bed58Brian Paul * Use 3D texcoords to choose a cube face, then sample the 2D cube faces.
183385425b3b609c480cd024b217b1efd0b9153bed58Brian Paul * Put face info into the sampler faces[] array.
18340dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
1835e12810d92ffb3547680b227bf88937c03018112bBrianstatic void
18364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellsample_cube(struct tgsi_sampler *tgsi_sampler,
18374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            const float s[QUAD_SIZE],
18384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            const float t[QUAD_SIZE],
18394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            const float p[QUAD_SIZE],
18404440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol            const float c0[QUAD_SIZE],
18414440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol            enum tgsi_sampler_control control,
18424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            float rgba[NUM_CHANNELS][QUAD_SIZE])
18430dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
1844f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
18454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned j;
18464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float ssss[4], tttt[4];
184709a1b912605ff48c8782dcc5aae55ac77e27037bBrian
18484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /*
18494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     major axis
185085425b3b609c480cd024b217b1efd0b9153bed58Brian Paul     direction    target                             sc     tc    ma
185185425b3b609c480cd024b217b1efd0b9153bed58Brian Paul     ----------   -------------------------------    ---    ---   ---
18524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     +rx          TEXTURE_CUBE_MAP_POSITIVE_X_EXT    -rz    -ry   rx
18534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     -rx          TEXTURE_CUBE_MAP_NEGATIVE_X_EXT    +rz    -ry   rx
18544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     +ry          TEXTURE_CUBE_MAP_POSITIVE_Y_EXT    +rx    +rz   ry
18554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     -ry          TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT    +rx    -rz   ry
18564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     +rz          TEXTURE_CUBE_MAP_POSITIVE_Z_EXT    +rx    -ry   rz
18574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     -rz          TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT    -rx    -ry   rz
18584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   */
185985425b3b609c480cd024b217b1efd0b9153bed58Brian Paul
18609ffdc78d1a308bb21a8627abb7bfc9da8abd2f81Brian Paul   /* Choose the cube face and compute new s/t coords for the 2D face.
18619ffdc78d1a308bb21a8627abb7bfc9da8abd2f81Brian Paul    *
186285425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * Use the same cube face for all four pixels in the quad.
186385425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    *
186485425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * This isn't ideal, but if we want to use a different cube face
186585425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * per pixel in the quad, we'd have to also compute the per-face
186685425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * LOD here too.  That's because the four post-face-selection
186785425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * texcoords are no longer related to each other (they're
186885425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * per-face!)  so we can't use subtraction to compute the partial
186985425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * deriviates to compute the LOD.  Doing so (near cube edges
187085425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    * anyway) gives us pretty much random values.
187185425b3b609c480cd024b217b1efd0b9153bed58Brian Paul    */
187285425b3b609c480cd024b217b1efd0b9153bed58Brian Paul   {
187385425b3b609c480cd024b217b1efd0b9153bed58Brian Paul      /* use the average of the four pixel's texcoords to choose the face */
187485425b3b609c480cd024b217b1efd0b9153bed58Brian Paul      const float rx = 0.25 * (s[0] + s[1] + s[2] + s[3]);
187585425b3b609c480cd024b217b1efd0b9153bed58Brian Paul      const float ry = 0.25 * (t[0] + t[1] + t[2] + t[3]);
187685425b3b609c480cd024b217b1efd0b9153bed58Brian Paul      const float rz = 0.25 * (p[0] + p[1] + p[2] + p[3]);
18774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
18784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1879890679d4322e7ba4f12f32532a3fdd277edff886Keith Whitwell      if (arx >= ary && arx >= arz) {
1880c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         float sign = (rx >= 0.0F) ? 1.0F : -1.0F;
1881c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         uint face = (rx >= 0.0F) ? PIPE_TEX_FACE_POS_X : PIPE_TEX_FACE_NEG_X;
1882c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         for (j = 0; j < QUAD_SIZE; j++) {
1883c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            const float ima = -0.5F / fabsf(s[j]);
1884c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            ssss[j] = sign *  p[j] * ima + 0.5F;
1885c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            tttt[j] =         t[j] * ima + 0.5F;
1886c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            samp->faces[j] = face;
18874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
18884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
1889890679d4322e7ba4f12f32532a3fdd277edff886Keith Whitwell      else if (ary >= arx && ary >= arz) {
1890c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         float sign = (ry >= 0.0F) ? 1.0F : -1.0F;
1891c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         uint face = (ry >= 0.0F) ? PIPE_TEX_FACE_POS_Y : PIPE_TEX_FACE_NEG_Y;
1892c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         for (j = 0; j < QUAD_SIZE; j++) {
1893c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            const float ima = -0.5F / fabsf(t[j]);
1894c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            ssss[j] =        -s[j] * ima + 0.5F;
1895c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            tttt[j] = sign * -p[j] * ima + 0.5F;
1896c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            samp->faces[j] = face;
18974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
18984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
18994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else {
1900c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         float sign = (rz >= 0.0F) ? 1.0F : -1.0F;
1901c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         uint face = (rz >= 0.0F) ? PIPE_TEX_FACE_POS_Z : PIPE_TEX_FACE_NEG_Z;
1902c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul         for (j = 0; j < QUAD_SIZE; j++) {
1903c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            const float ima = -0.5 / fabsf(p[j]);
1904c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            ssss[j] = sign * -s[j] * ima + 0.5F;
1905c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            tttt[j] =         t[j] * ima + 0.5F;
1906c72a3b4f2ffe0673e753ad144d1b5557a42c670fBrian Paul            samp->faces[j] = face;
19074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
19084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
190985425b3b609c480cd024b217b1efd0b9153bed58Brian Paul   }
191085425b3b609c480cd024b217b1efd0b9153bed58Brian Paul
19114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* In our little pipeline, the compare stage is next.  If compare
19124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * is not active, this will point somewhere deeper into the
19134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * pipeline, eg. to mip_filter or even img_filter.
19146142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell    */
19154440428faa82f01b4dfb4be89618be2aaf153abdMichal Krol   samp->compare(tgsi_sampler, ssss, tttt, NULL, c0, control, rgba);
19164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1917612cfb749c3526eeb446bbc631bf24716522f373Brian
19183d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
1919c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paulstatic void
1920c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paulsample_swizzle(struct tgsi_sampler *tgsi_sampler,
1921c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul               const float s[QUAD_SIZE],
1922c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul               const float t[QUAD_SIZE],
1923c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul               const float p[QUAD_SIZE],
1924c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul               const float c0[QUAD_SIZE],
1925c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul               enum tgsi_sampler_control control,
1926c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul               float rgba[NUM_CHANNELS][QUAD_SIZE])
1927c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul{
1928f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = sp_sampler_variant(tgsi_sampler);
1929c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   float rgba_temp[NUM_CHANNELS][QUAD_SIZE];
1930c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   const unsigned swizzle_r = samp->key.bits.swizzle_r;
1931c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   const unsigned swizzle_g = samp->key.bits.swizzle_g;
1932c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   const unsigned swizzle_b = samp->key.bits.swizzle_b;
1933c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   const unsigned swizzle_a = samp->key.bits.swizzle_a;
1934c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   unsigned j;
1935c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
1936c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   samp->sample_target(tgsi_sampler, s, t, p, c0, control, rgba_temp);
1937c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
1938c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   switch (swizzle_r) {
1939c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ZERO:
1940c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1941c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[0][j] = 0.0f;
1942c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1943c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ONE:
1944c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1945c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[0][j] = 1.0f;
1946c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1947c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   default:
1948c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      assert(swizzle_r < 4);
1949c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1950c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[0][j] = rgba_temp[swizzle_r][j];
1951c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   }
1952c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
1953c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   switch (swizzle_g) {
1954c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ZERO:
1955c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1956c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[1][j] = 0.0f;
1957c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1958c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ONE:
1959c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1960c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[1][j] = 1.0f;
1961c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1962c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   default:
1963c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      assert(swizzle_g < 4);
1964c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1965c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[1][j] = rgba_temp[swizzle_g][j];
1966c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   }
1967c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
1968c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   switch (swizzle_b) {
1969c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ZERO:
1970c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1971c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[2][j] = 0.0f;
1972c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1973c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ONE:
1974c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1975c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[2][j] = 1.0f;
1976c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1977c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   default:
1978c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      assert(swizzle_b < 4);
1979c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1980c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[2][j] = rgba_temp[swizzle_b][j];
1981c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   }
1982c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
1983c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   switch (swizzle_a) {
1984c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ZERO:
1985c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1986c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[3][j] = 0.0f;
1987c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1988c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   case PIPE_SWIZZLE_ONE:
1989c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1990c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[3][j] = 1.0f;
1991c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      break;
1992c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   default:
1993c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      assert(swizzle_a < 4);
1994c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      for (j = 0; j < 4; j++)
1995c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul         rgba[3][j] = rgba_temp[swizzle_a][j];
1996c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   }
1997c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul}
1998c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
19993d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
2000e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_nearest_func
2001e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_nearest_unorm_wrap(unsigned mode)
20024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
20034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
20044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
20054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_unorm_clamp;
20064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2007b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      return wrap_nearest_unorm_clamp_to_edge;
20084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
20094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_unorm_clamp_to_border;
20100dc4eea64f56cc93e5359372b08b99a2d600273cBrian   default:
20110dc4eea64f56cc93e5359372b08b99a2d600273cBrian      assert(0);
20124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_unorm_clamp;
20130dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
20140dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
201534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
201634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
2017e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_nearest_func
2018e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_nearest_wrap(unsigned mode)
2019a34b8594b7b2d00404bb639621ec1ce918ba0786Brian{
20204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
20214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_REPEAT:
20224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_repeat;
20234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
20244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_clamp;
20254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
20264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_clamp_to_edge;
20274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
20284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_clamp_to_border;
20294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_REPEAT:
20304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_repeat;
20314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP:
20324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_clamp;
20334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
20344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_clamp_to_edge;
20354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
20364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_clamp_to_border;
20374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   default:
20384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      assert(0);
20394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_repeat;
20404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2041a34b8594b7b2d00404bb639621ec1ce918ba0786Brian}
2042a34b8594b7b2d00404bb639621ec1ce918ba0786Brian
2043e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2044e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_linear_func
2045e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_linear_unorm_wrap(unsigned mode)
2046a34b8594b7b2d00404bb639621ec1ce918ba0786Brian{
20474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
20484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
20494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_unorm_clamp;
20504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
2051b5b128b26841e7f947edd8f0cbcc91a530d6bb8fBrian Paul      return wrap_linear_unorm_clamp_to_edge;
20524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
20534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_unorm_clamp_to_border;
20544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   default:
20554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      assert(0);
20564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_unorm_clamp;
20574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2058a34b8594b7b2d00404bb639621ec1ce918ba0786Brian}
2059a34b8594b7b2d00404bb639621ec1ce918ba0786Brian
2060e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2061e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_linear_func
2062e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_linear_wrap(unsigned mode)
206334a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
20644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
20654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_REPEAT:
20664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_repeat;
20674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
20684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_clamp;
20694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
20704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_clamp_to_edge;
20714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
20724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_clamp_to_border;
20734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_REPEAT:
20744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_repeat;
20754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP:
20764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_clamp;
20774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
20784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_clamp_to_edge;
20794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
20804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_clamp_to_border;
20813d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   default:
20823d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian      assert(0);
20834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_repeat;
20843d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   }
208534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
208634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
2087e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2088e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic compute_lambda_func
2089e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_lambda_func(const union sp_sampler_key key)
209034a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
20914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (key.bits.processor == TGSI_PROCESSOR_VERTEX)
20924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_vert;
20934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
20944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (key.bits.target) {
20954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_1D:
2096779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   case PIPE_TEXTURE_1D_ARRAY:
20974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_1d;
20984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_2D:
2099779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   case PIPE_TEXTURE_2D_ARRAY:
2100ae0ef6f69f351cacdc7eaa9b21097a7c1b414e44Luca Barbieri   case PIPE_TEXTURE_RECT:
210187ec83afd58536c31bf02c307f1d5488abc84861Brian Paul   case PIPE_TEXTURE_CUBE:
21024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_2d;
21034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_3D:
21044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_3d;
21054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   default:
21064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      assert(0);
21074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_1d;
2108b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   }
210934a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
211034a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
2111e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2112e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic filter_func
2113e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_img_filter(const union sp_sampler_key key,
2114e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul               unsigned filter,
2115e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul               const struct pipe_sampler_state *sampler)
2116b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
21174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (key.bits.target) {
21184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_1D:
21194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
21204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_1d_nearest;
21214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
21224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_1d_linear;
2123b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      break;
2124779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   case PIPE_TEXTURE_1D_ARRAY:
2125779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      if (filter == PIPE_TEX_FILTER_NEAREST)
2126779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         return img_filter_1d_array_nearest;
2127779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      else
2128779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         return img_filter_1d_array_linear;
2129779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      break;
21304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_2D:
2131ae0ef6f69f351cacdc7eaa9b21097a7c1b414e44Luca Barbieri   case PIPE_TEXTURE_RECT:
21324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Try for fast path:
21334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
21344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (key.bits.is_pot &&
21354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->wrap_s == sampler->wrap_t &&
21364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->normalized_coords)
213738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian      {
21384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         switch (sampler->wrap_s) {
21394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         case PIPE_TEX_WRAP_REPEAT:
21404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            switch (filter) {
21416142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            case PIPE_TEX_FILTER_NEAREST:
21424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell               return img_filter_2d_nearest_repeat_POT;
21436142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            case PIPE_TEX_FILTER_LINEAR:
21444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell               return img_filter_2d_linear_repeat_POT;
21456142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            default:
21466142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell               break;
214738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian            }
21484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            break;
21494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         case PIPE_TEX_WRAP_CLAMP:
21504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            switch (filter) {
21516142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            case PIPE_TEX_FILTER_NEAREST:
21524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell               return img_filter_2d_nearest_clamp_POT;
21536142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            default:
21546142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell               break;
215538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian            }
2156b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian         }
2157b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      }
215860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      /* Otherwise use default versions:
21594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
21604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
21614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_2d_nearest;
21624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
21634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_2d_linear;
21644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      break;
2165779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul   case PIPE_TEXTURE_2D_ARRAY:
2166779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      if (filter == PIPE_TEX_FILTER_NEAREST)
2167779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         return img_filter_2d_array_nearest;
2168779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      else
2169779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul         return img_filter_2d_array_linear;
2170779e9cb658dba4ef44fae7e8aa62409f7227f46cBrian Paul      break;
217160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   case PIPE_TEXTURE_CUBE:
217260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
217360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         return img_filter_cube_nearest;
217460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      else
217560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         return img_filter_cube_linear;
217660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      break;
21774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_3D:
21784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
21794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_3d_nearest;
21804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
21814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_3d_linear;
2182b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      break;
2183b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   default:
2184b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      assert(0);
21854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return img_filter_1d_nearest;
2186b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
2187b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
2188b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
2189b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
2190a34b8594b7b2d00404bb639621ec1ce918ba0786Brian/**
2191f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul * Bind the given texture object and texture cache to the sampler variant.
2192a34b8594b7b2d00404bb639621ec1ce918ba0786Brian */
21934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellvoid
219421148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeetsp_sampler_variant_bind_view( struct sp_sampler_variant *samp,
219521148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet                              struct softpipe_tex_tile_cache *tex_cache,
219621148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet                              const struct pipe_sampler_view *view )
219734a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
21984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
219921148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   const struct pipe_resource *texture = view->texture;
22004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
220121148e6a8806736b4487da41b01e2d3c01cbc743Henri Verbeet   samp->view = view;
22024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->cache = tex_cache;
2203683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   samp->xpot = util_unsigned_logbase2( texture->width0 );
2204683e35f726a182ed9fc6b6d5cb07146eebe14deaKeith Whitwell   samp->ypot = util_unsigned_logbase2( texture->height0 );
22054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->level = CLAMP((int) sampler->min_lod, 0, (int) texture->last_level);
22064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
22070b9e96fae9493d5d58f046e01c983a3c4267090eBrian
22084f23468bd0d14b8ed687a641003d587b91ad39a7Brian
2209ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwellvoid
2210f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulsp_sampler_variant_destroy( struct sp_sampler_variant *samp )
2211ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell{
2212ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell   FREE(samp);
2213ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell}
2214ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell
2215ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell
2216e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
2217f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul * Create a sampler variant for a given set of non-orthogonal state.
22184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell */
2219f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulstruct sp_sampler_variant *
2220f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paulsp_create_sampler_variant( const struct pipe_sampler_state *sampler,
22214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                           const union sp_sampler_key key )
22224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2223f3955f6fcdd1a3106a6538642131ccea5ef1cef0Brian Paul   struct sp_sampler_variant *samp = CALLOC_STRUCT(sp_sampler_variant);
22244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (!samp)
22254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return NULL;
22264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->sampler = sampler;
22284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->key = key;
22294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* Note that (for instance) linear_texcoord_s and
22314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * nearest_texcoord_s may be active at the same time, if the
22324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * sampler min_img_filter differs from its mag_img_filter.
22334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    */
22344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (sampler->normalized_coords) {
22354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );
22364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );
22374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );
22384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );
22404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );
22414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );
22424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
22434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
22444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );
22454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );
22464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );
22474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );
22494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );
22504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );
22514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
22524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->compute_lambda = get_lambda_func( key );
22544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->min_img_filter = get_img_filter(key, sampler->min_img_filter, sampler);
225641483627f0fd3dc9df2cc55dfd5f3e5987fcfd22Brian Paul   samp->mag_img_filter = get_img_filter(key, sampler->mag_img_filter, sampler);
22574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (sampler->min_mip_filter) {
22594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_MIPFILTER_NONE:
22604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (sampler->min_img_filter == sampler->mag_img_filter)
22614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = samp->min_img_filter;
2262b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      else
22634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = mip_filter_none;
226434a48abd5ff82ce9748fc29191e35a0985d47c5fBrian      break;
22654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_MIPFILTER_NEAREST:
22674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->mip_filter = mip_filter_nearest;
226834a48abd5ff82ce9748fc29191e35a0985d47c5fBrian      break;
22694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_MIPFILTER_LINEAR:
22714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (key.bits.is_pot &&
22724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->min_img_filter == sampler->mag_img_filter &&
22734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->normalized_coords &&
22744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&
2275cf102b031e7ef33c8e3ffce2f9dcd064f44e8190Brian Paul          sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&
22764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR)
22774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      {
22784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = mip_filter_linear_2d_linear_repeat_POT;
22794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
22804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
22814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      {
22824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = mip_filter_linear;
22836142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
228434a48abd5ff82ce9748fc29191e35a0985d47c5fBrian      break;
228534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian   }
22863d53d38d5e35386de4793162b9dd32e171927059Brian Paul
22874e014c0a148ba3ac015d0e83dcf975ca6e814e1fMichal Krol   if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
22884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->compare = sample_compare;
22893d53d38d5e35386de4793162b9dd32e171927059Brian Paul   }
22904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
22914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Skip compare operation by promoting the mip_filter function
22924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       * pointer:
22934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
22944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->compare = samp->mip_filter;
22954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
22964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
22974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (key.bits.target == PIPE_TEXTURE_CUBE) {
2298c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      samp->sample_target = sample_cube;
22994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
23004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
23014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[0] = 0;
23024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[1] = 0;
23034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[2] = 0;
23044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[3] = 0;
23054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
23064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Skip cube face determination by promoting the compare
23074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       * function pointer:
23084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
2309c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      samp->sample_target = samp->compare;
2310c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   }
2311c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul
2312c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   if (key.bits.swizzle_r != PIPE_SWIZZLE_RED ||
2313c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul       key.bits.swizzle_g != PIPE_SWIZZLE_GREEN ||
2314c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul       key.bits.swizzle_b != PIPE_SWIZZLE_BLUE ||
2315c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul       key.bits.swizzle_a != PIPE_SWIZZLE_ALPHA) {
2316c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      samp->base.get_samples = sample_swizzle;
2317c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   }
2318c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul   else {
2319c62bb90d6a864468fe1a717aebb9c1a9c43bf3c8Brian Paul      samp->base.get_samples = samp->sample_target;
23206142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
23216142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
23224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   return samp;
232334a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
2324