sp_tex_sample.c revision 9659aa6482291d1530c74450612bcd952f542e01
10dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**************************************************************************
20dc4eea64f56cc93e5359372b08b99a2d600273cBrian *
30dc4eea64f56cc93e5359372b08b99a2d600273cBrian * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
40dc4eea64f56cc93e5359372b08b99a2d600273cBrian * All Rights Reserved.
53ffd529ff19bf8dd7b022a267bf2afe44c7f0f65Brian Paul * Copyright 2008 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
473ffd529ff19bf8dd7b022a267bf2afe44c7f0f65Brian Paul
4808f33a025100dea2d951e6d628891fe294b18082Brian/*
49b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Return fractional part of 'f'.  Used for computing interpolation weights.
50b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Need to be careful with negative values.
51b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Note, if this function isn't perfect you'll sometimes see 1-pixel bands
52b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * of improperly weighted linear-filtered textures.
5308f33a025100dea2d951e6d628891fe294b18082Brian * The tests/texwrap.c demo is a good test.
5408f33a025100dea2d951e6d628891fe294b18082Brian */
55b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulstatic INLINE float
56b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulfrac(float f)
57b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul{
58b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul   return f - util_ifloor(f);
59b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul}
60b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul
6108f33a025100dea2d951e6d628891fe294b18082Brian
6208f33a025100dea2d951e6d628891fe294b18082Brian
6308f33a025100dea2d951e6d628891fe294b18082Brian/**
6408f33a025100dea2d951e6d628891fe294b18082Brian * Linear interpolation macro
6508f33a025100dea2d951e6d628891fe294b18082Brian */
6638bee46e83b18ff4ad42d340b507b1a15b4326c7Brianstatic INLINE float
6738bee46e83b18ff4ad42d340b507b1a15b4326c7Brianlerp(float a, float v0, float v1)
6838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian{
6938bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   return v0 + a * (v1 - v0);
7038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian}
710dc4eea64f56cc93e5359372b08b99a2d600273cBrian
720dc4eea64f56cc93e5359372b08b99a2d600273cBrian
730dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**
740dc4eea64f56cc93e5359372b08b99a2d600273cBrian * Do 2D/biliner interpolation of float values.
750dc4eea64f56cc93e5359372b08b99a2d600273cBrian * v00, v10, v01 and v11 are typically four texture samples in a square/box.
760dc4eea64f56cc93e5359372b08b99a2d600273cBrian * a and b are the horizontal and vertical interpolants.
770dc4eea64f56cc93e5359372b08b99a2d600273cBrian * It's important that this function is inlined when compiled with
780dc4eea64f56cc93e5359372b08b99a2d600273cBrian * optimization!  If we find that's not true on some systems, convert
790dc4eea64f56cc93e5359372b08b99a2d600273cBrian * to a macro.
800dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
81b4480285ed5098f1c862690ee105dd46f5e6cd1eBrianstatic INLINE float
82b4480285ed5098f1c862690ee105dd46f5e6cd1eBrianlerp_2d(float a, float b,
83b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian        float v00, float v10, float v01, float v11)
840dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
8538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp0 = lerp(a, v00, v10);
8638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp1 = lerp(a, v01, v11);
8738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   return lerp(b, temp0, temp1);
8838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian}
8938bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
9038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
9138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian/**
9238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * As above, but 3D interpolation of 8 values.
9338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian */
9438bee46e83b18ff4ad42d340b507b1a15b4326c7Brianstatic INLINE float
9538bee46e83b18ff4ad42d340b507b1a15b4326c7Brianlerp_3d(float a, float b, float c,
9638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian        float v000, float v100, float v010, float v110,
9738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian        float v001, float v101, float v011, float v111)
9838bee46e83b18ff4ad42d340b507b1a15b4326c7Brian{
9938bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp0 = lerp_2d(a, b, v000, v100, v010, v110);
10038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   const float temp1 = lerp_2d(a, b, v001, v101, v011, v111);
10138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   return lerp(c, temp0, temp1);
1020dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
1030dc4eea64f56cc93e5359372b08b99a2d600273cBrian
1040dc4eea64f56cc93e5359372b08b99a2d600273cBrian
10538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
1060dc4eea64f56cc93e5359372b08b99a2d600273cBrian/**
107b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Compute coord % size for repeat wrap modes.
108b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * Note that if coord is a signed integer, coord % size doesn't give
109b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * the right value for coord < 0 (in terms of texture repeat).  Just
110b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul * casting to unsigned fixes that.
1110dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
112b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulstatic INLINE int
113b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paulrepeat(int coord, unsigned size)
114b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul{
115b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul   return (int) ((unsigned) coord % size);
116b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul}
11708f33a025100dea2d951e6d628891fe294b18082Brian
11808f33a025100dea2d951e6d628891fe294b18082Brian
11908f33a025100dea2d951e6d628891fe294b18082Brian/**
12038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * Apply texture coord wrapping mode and return integer texture indexes
12138bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * for a vector of four texcoords (S or T or P).
12208f33a025100dea2d951e6d628891fe294b18082Brian * \param wrapMode  PIPE_TEX_WRAP_x
12338bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param s  the incoming texcoords
12408f33a025100dea2d951e6d628891fe294b18082Brian * \param size  the texture image size
12538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord  returns the integer texcoords
12608f33a025100dea2d951e6d628891fe294b18082Brian * \return  integer texture index
12708f33a025100dea2d951e6d628891fe294b18082Brian */
1284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
129e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_repeat(const float s[4], unsigned size, int icoord[4])
1304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
1324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [0,1) */
1334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0,size-1] */
1344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int i = util_ifloor(s[ch] * size);
136b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      icoord[ch] = repeat(i, size);
1374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
142e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_clamp(const float s[4], unsigned size, int icoord[4])
1430dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
14438bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
1454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [0,1] */
1464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0,size-1] */
1474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (s[ch] <= 0.0F)
1494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
1504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (s[ch] >= 1.0F)
1514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
1524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
1534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(s[ch] * size);
1544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
159e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_clamp_to_edge(const float s[4], unsigned size, int icoord[4])
1604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
1624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
1634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0, size-1] */
1644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = 1.0F / (2.0F * size);
1654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
1664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (s[ch] < min)
1684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
1694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (s[ch] > max)
1704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
1714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
1724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(s[ch] * size);
1734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
178e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_clamp_to_border(const float s[4], unsigned size, int icoord[4])
1794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
1814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
1824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [-1, size] */
1834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
1844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
1854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
1864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (s[ch] <= min)
1874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = -1;
1884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (s[ch] >= max)
1894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size;
1904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
1914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(s[ch] * size);
1924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
195e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
197e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_mirror_repeat(const float s[4], unsigned size, int icoord[4])
1984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
1994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = 1.0F / (2.0F * size);
2014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
2024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const int flr = util_ifloor(s[ch]);
2044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u;
2054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (flr & 1)
2064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = 1.0F - (s[ch] - (float) flr);
2074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = s[ch] - (float) flr;
2094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u < min)
2104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
2114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u > max)
2124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
2134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
2174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
218e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
220e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_mirror_clamp(const float s[4], unsigned size, int icoord[4])
2214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
22408f33a025100dea2d951e6d628891fe294b18082Brian      /* s limited to [0,1] */
22508f33a025100dea2d951e6d628891fe294b18082Brian      /* i limited to [0,size-1] */
2264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float u = fabsf(s[ch]);
2274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u <= 0.0F)
2284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
2294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u >= 1.0F)
2304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
2314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
2354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
236e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
2374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
2384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_nearest_mirror_clamp_to_edge(const float s[4], unsigned size,
239e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                  int icoord[4])
2404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
2434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0, size-1] */
2444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = 1.0F / (2.0F * size);
2454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
2464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float u = fabsf(s[ch]);
2484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u < min)
2494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = 0;
2504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u > max)
2514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size - 1;
2524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
2554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
2564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
2574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
2584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
2594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_nearest_mirror_clamp_to_border(const float s[4], unsigned size,
2604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                    int icoord[4])
2614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* s limited to [min,max] */
2644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* i limited to [0, size-1] */
2654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
2664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
2674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float u = fabsf(s[ch]);
2694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u < min)
2704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = -1;
2714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u > max)
2724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = size;
2734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
2744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord[ch] = util_ifloor(u * size);
2750dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
2760dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
2770dc4eea64f56cc93e5359372b08b99a2d600273cBrian
27808f33a025100dea2d951e6d628891fe294b18082Brian
27908f33a025100dea2d951e6d628891fe294b18082Brian/**
28038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * Used to compute texel locations for linear sampling for four texcoords.
28108f33a025100dea2d951e6d628891fe294b18082Brian * \param wrapMode  PIPE_TEX_WRAP_x
28238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param s  the texcoords
28308f33a025100dea2d951e6d628891fe294b18082Brian * \param size  the texture image size
28438bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord0  returns first texture indexes
28538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord1  returns second texture indexes (usually icoord0 + 1)
28638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param w  returns blend factor/weight between texture indexes
28738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian * \param icoord  returns the computed integer texture coords
28808f33a025100dea2d951e6d628891fe294b18082Brian */
2894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
2904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_repeat(const float s[4], unsigned size,
2914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   int icoord0[4], int icoord1[4], float w[4])
2924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
2934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
2944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
2954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = s[ch] * size - 0.5F;
296b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      icoord0[ch] = repeat(util_ifloor(u), size);
297b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      icoord1[ch] = repeat(icoord0[ch] + 1, size);
298b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
2994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
302e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_clamp(const float s[4], unsigned size,
30538bee46e83b18ff4ad42d340b507b1a15b4326c7Brian                  int icoord0[4], int icoord1[4], float w[4])
3060dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
30738bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
3084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], 0.0F, 1.0F);
3104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5f;
3114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
313b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
31638bee46e83b18ff4ad42d340b507b1a15b4326c7Brian
317e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_clamp_to_edge(const float s[4], unsigned size,
3204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                          int icoord0[4], int icoord1[4], float w[4])
3214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], 0.0F, 1.0F);
3254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5f;
3264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
3284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord0[ch] < 0)
3294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord0[ch] = 0;
3304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] >= (int) size)
3314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
332b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
336e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_clamp_to_border(const float s[4], unsigned size,
3394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                            int icoord0[4], int icoord1[4], float w[4])
3404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
3424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
3434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], min, max);
3464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5f;
3474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
349b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
3534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
3544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_repeat(const float s[4], unsigned size,
3564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                          int icoord0[4], int icoord1[4], float w[4])
3574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const int flr = util_ifloor(s[ch]);
3614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u;
3624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (flr & 1)
3634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = 1.0F - (s[ch] - (float) flr);
3644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
3654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = s[ch] - (float) flr;
3664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u = u * size - 0.5F;
3674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
3694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord0[ch] < 0)
3704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord0[ch] = 0;
3714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] >= (int) size)
3724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
373b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
377e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_clamp(const float s[4], unsigned size,
3804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                         int icoord0[4], int icoord1[4], float w[4])
3814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
3824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
3834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
3844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = fabsf(s[ch]);
3854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u >= 1.0F)
3864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = (float) size;
3874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
3884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u *= size;
3894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
3904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
3914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
392b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
3934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
3944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
3954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
396e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
3974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
3984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_clamp_to_edge(const float s[4], unsigned size,
3994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 int icoord0[4], int icoord1[4], float w[4])
4004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
4014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = fabsf(s[ch]);
4044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u >= 1.0F)
4054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = (float) size;
4064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
4074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u *= size;
4084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
4094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
4104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
4114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord0[ch] < 0)
4124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord0[ch] = 0;
4134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] >= (int) size)
4144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
415b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
4164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
4174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
4184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
419e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
4204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
4214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_mirror_clamp_to_border(const float s[4], unsigned size,
4224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                   int icoord0[4], int icoord1[4], float w[4])
4234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
4244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float min = -1.0F / (2.0F * size);
4254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float max = 1.0F - min;
4264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = fabsf(s[ch]);
4294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (u <= min)
4304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = min * size;
4314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else if (u >= max)
4324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u = max * size;
4334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
4344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         u *= size;
4354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
4364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
4374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
438b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
4390dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
4400dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
4410dc4eea64f56cc93e5359372b08b99a2d600273cBrian
4420dc4eea64f56cc93e5359372b08b99a2d600273cBrian
443b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian/**
444b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian * For RECT textures / unnormalized texcoords
445b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian * Only a subset of wrap modes supported.
446b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian */
4474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
448e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_nearest_unorm_clamp(const float s[4], unsigned size, int icoord[4])
449b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
45038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
4514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int i = util_ifloor(s[ch]);
4534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord[ch]= CLAMP(i, 0, (int) size-1);
4544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
4554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
4564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
457e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
458e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
459e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Handles clamp_to_edge and clamp_to_border:
4604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell */
4614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
4624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_nearest_unorm_clamp_to_border(const float s[4], unsigned size,
463e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                   int icoord[4])
4644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
4654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord[ch]= util_ifloor( CLAMP(s[ch], 0.5F, (float) size - 0.5F) );
468b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
469b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
470b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
471b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
472b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian/**
473b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian * For RECT textures / unnormalized texcoords.
474b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian * Only a subset of wrap modes supported.
475b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian */
4764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
4774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellwrap_linear_unorm_clamp(const float s[4], unsigned size,
478e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        int icoord0[4], int icoord1[4], float w[4])
479b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
48038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian   uint ch;
4814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Not exactly what the spec says, but it matches NVIDIA output */
4834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch] - 0.5F, 0.0f, (float) size - 1.0f);
4844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
4854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
486b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
487b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
488b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
489b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
490e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
4914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
492e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulwrap_linear_unorm_clamp_to_border(const float s[4], unsigned size,
493e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                  int icoord0[4], int icoord1[4], float w[4])
49434a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
4954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   uint ch;
4964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (ch = 0; ch < 4; ch++) {
4974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float u = CLAMP(s[ch], 0.5F, (float) size - 0.5F);
4984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      u -= 0.5F;
4994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord0[ch] = util_ifloor(u);
5004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      icoord1[ch] = icoord0[ch] + 1;
5014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (icoord1[ch] > (int) size - 1)
5024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         icoord1[ch] = size - 1;
503b4a40d10524a4be6a59805589ee4209ebdb1de4fBrian Paul      w[ch] = frac(u);
50434a48abd5ff82ce9748fc29191e35a0985d47c5fBrian   }
5054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
5064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
50734a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
50834a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
509b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian/**
510b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * Examine the quad's texture coordinates to compute the partial
511b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * derivatives w.r.t X and Y, then compute lambda (level of detail).
512b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian */
513b4480285ed5098f1c862690ee105dd46f5e6cd1eBrianstatic float
5144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellcompute_lambda_1d(const struct sp_sampler_varient *samp,
5154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float s[QUAD_SIZE],
5164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float t[QUAD_SIZE],
5174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float p[QUAD_SIZE],
5184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  float lodbias)
519b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian{
52000c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell   const struct pipe_texture *texture = samp->texture;
52100c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
5224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
5234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdy = fabsf(s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT]);
5244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float rho = MAX2(dsdx, dsdy) * texture->width[0];
5254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
526b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
5271a46dcc8a927dfb38ca1381e7b3dafb789f8257cBrian Paul   lambda = util_fast_log2(rho);
5280b9e96fae9493d5d58f046e01c983a3c4267090eBrian   lambda += lodbias + sampler->lod_bias;
5290b9e96fae9493d5d58f046e01c983a3c4267090eBrian   lambda = CLAMP(lambda, sampler->min_lod, sampler->max_lod);
530b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
531b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   return lambda;
532b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian}
533b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
534e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
5354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic float
5364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellcompute_lambda_2d(const struct sp_sampler_varient *samp,
5374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float s[QUAD_SIZE],
5384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float t[QUAD_SIZE],
5394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float p[QUAD_SIZE],
5404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  float lodbias)
54109a1b912605ff48c8782dcc5aae55ac77e27037bBrian{
5424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_texture *texture = samp->texture;
5434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
5444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
5454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdy = fabsf(s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT]);
5464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
5474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdy = fabsf(t[QUAD_TOP_LEFT]     - t[QUAD_BOTTOM_LEFT]);
5484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float maxx = MAX2(dsdx, dsdy) * texture->width[0];
5494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float maxy = MAX2(dtdx, dtdy) * texture->height[0];
5504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float rho  = MAX2(maxx, maxy);
5514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
552c7722edcfdf36e0d0bfdc51013ecb199fc7fa9f6Brian
5531a46dcc8a927dfb38ca1381e7b3dafb789f8257cBrian Paul   lambda = util_fast_log2(rho);
5540b9e96fae9493d5d58f046e01c983a3c4267090eBrian   lambda += lodbias + sampler->lod_bias;
5550b9e96fae9493d5d58f046e01c983a3c4267090eBrian   lambda = CLAMP(lambda, sampler->min_lod, sampler->max_lod);
556c7722edcfdf36e0d0bfdc51013ecb199fc7fa9f6Brian
557b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   return lambda;
55809a1b912605ff48c8782dcc5aae55ac77e27037bBrian}
55909a1b912605ff48c8782dcc5aae55ac77e27037bBrian
56008f33a025100dea2d951e6d628891fe294b18082Brian
5614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic float
5624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellcompute_lambda_3d(const struct sp_sampler_varient *samp,
5634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float s[QUAD_SIZE],
5644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float t[QUAD_SIZE],
5654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  const float p[QUAD_SIZE],
5664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                  float lodbias)
56709a1b912605ff48c8782dcc5aae55ac77e27037bBrian{
56800c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell   const struct pipe_texture *texture = samp->texture;
56900c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
5704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdx = fabsf(s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT]);
5714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dsdy = fabsf(s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT]);
5724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdx = fabsf(t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT]);
5734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dtdy = fabsf(t[QUAD_TOP_LEFT]     - t[QUAD_BOTTOM_LEFT]);
5744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dpdx = fabsf(p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT]);
5754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float dpdy = fabsf(p[QUAD_TOP_LEFT]     - p[QUAD_BOTTOM_LEFT]);
5764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float maxx = MAX2(dsdx, dsdy) * texture->width[0];
5774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float maxy = MAX2(dtdx, dtdy) * texture->height[0];
5784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float maxz = MAX2(dpdx, dpdy) * texture->depth[0];
5794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float rho, lambda;
58000c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell
5814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   rho = MAX2(maxx, maxy);
5824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   rho = MAX2(rho, maxz);
583c7722edcfdf36e0d0bfdc51013ecb199fc7fa9f6Brian
5844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   lambda = util_fast_log2(rho);
5854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   lambda += lodbias + sampler->lod_bias;
5864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   lambda = CLAMP(lambda, sampler->min_lod, sampler->max_lod);
5874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
5884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   return lambda;
58909a1b912605ff48c8782dcc5aae55ac77e27037bBrian}
59009a1b912605ff48c8782dcc5aae55ac77e27037bBrian
59108f33a025100dea2d951e6d628891fe294b18082Brian
592e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
593e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Compute lambda for a vertex texture sampler.
594e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Since there aren't derivatives to use, just return the LOD bias.
595e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul */
5964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic float
5974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellcompute_lambda_vert(const struct sp_sampler_varient *samp,
5984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                    const float s[QUAD_SIZE],
5994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                    const float t[QUAD_SIZE],
6004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                    const float p[QUAD_SIZE],
6014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                    float lodbias)
6024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
6034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   return lodbias;
6044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
6054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
6064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
6074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
6084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell/**
6094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell * Get a texel from a texture, using the texture tile cache.
6104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell *
61181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell * \param addr  the template tex address containing cube, z, face info.
6124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell * \param x  the x coord of texel within 2D image
613b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * \param y  the y coord of texel within 2D image
614b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian * \param rgba  the quad to put the texel/color into
61570eb7996f265f3634dabda078f13d1be3533cc65Brian *
61680c78472ad43f4288c9ef5076074ba9d31a39885Keith Whitwell * XXX maybe move this into sp_tex_tile_cache.c and merge with the
61770eb7996f265f3634dabda078f13d1be3533cc65Brian * sp_get_cached_tile_tex() function.  Also, get 4 texels instead of 1...
618b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian */
61981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
62081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
62181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
62281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
62381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE const float *
62481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_2d_no_border(const struct sp_sampler_varient *samp,
62581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		       union tex_tile_address addr, int x, int y)
626b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian{
62781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   const struct softpipe_tex_cached_tile *tile;
62881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
62981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.x = x / TILE_SIZE;
63081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.y = y / TILE_SIZE;
63181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   y %= TILE_SIZE;
63281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   x %= TILE_SIZE;
63381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
63481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   tile = sp_get_cached_tile_tex(samp->cache, addr);
63581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
63681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   return &tile->data.color[y][x][0];
63781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
63881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
63981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
64081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE const float *
64181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_2d(const struct sp_sampler_varient *samp,
64281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell	     union tex_tile_address addr, int x, int y)
64381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
64481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   const struct pipe_texture *texture = samp->texture;
64581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   unsigned level = addr.bits.level;
64681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
64781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   if (x < 0 || x >= (int) texture->width[level] ||
64881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell       y < 0 || y >= (int) texture->height[level]) {
64981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      return samp->sampler->border_color;
65081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   }
65181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   else {
65281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      return get_texel_2d_no_border( samp, addr, x, y );
65381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   }
65481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
6556142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
6566142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
65781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Gather a quad of adjacent texels within a tile:
65881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
65981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE void
66081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_quad_2d_no_border_single_tile(const struct sp_sampler_varient *samp,
66181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell					union tex_tile_address addr,
66281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell					unsigned x, unsigned y,
66381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell					const float *out[4])
66481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
66581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   const struct softpipe_tex_cached_tile *tile;
66681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
66781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.x = x / TILE_SIZE;
66881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.y = y / TILE_SIZE;
6696142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   y %= TILE_SIZE;
6706142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   x %= TILE_SIZE;
67181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
67281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   tile = sp_get_cached_tile_tex(samp->cache, addr);
6736142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
6746142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[0] = &tile->data.color[y  ][x  ][0];
6756142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[1] = &tile->data.color[y  ][x+1][0];
6766142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[2] = &tile->data.color[y+1][x  ][0];
6776142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   out[3] = &tile->data.color[y+1][x+1][0];
6786142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
6796142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
68081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
68181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Gather a quad of potentially non-adjacent texels:
68281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
68381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE void
68481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_quad_2d_no_border(const struct sp_sampler_varient *samp,
68581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    union tex_tile_address addr,
68681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    int x0, int y0,
68781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    int x1, int y1,
68881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell			    const float *out[4])
68981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
69081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[0] = get_texel_2d_no_border( samp, addr, x0, y0 );
69181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[1] = get_texel_2d_no_border( samp, addr, x1, y0 );
69281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[2] = get_texel_2d_no_border( samp, addr, x0, y1 );
69381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[3] = get_texel_2d_no_border( samp, addr, x1, y1 );
69481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
69581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
69681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Can involve a lot of unnecessary checks for border color:
69781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
69881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE void
69981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_quad_2d(const struct sp_sampler_varient *samp,
70081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  union tex_tile_address addr,
70181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  int x0, int y0,
70281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  int x1, int y1,
70381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell		  const float *out[4])
70481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
70581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[0] = get_texel_2d( samp, addr, x0, y0 );
70681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[1] = get_texel_2d( samp, addr, x1, y0 );
70781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[3] = get_texel_2d( samp, addr, x1, y1 );
70881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   out[2] = get_texel_2d( samp, addr, x0, y1 );
70981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
71081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
71181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
71281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
71381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* 3d varients:
71481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
7156142de393fe34ff0866f8489f1292eb473276f11Keith Whitwellstatic INLINE const float *
71681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_3d_no_border(const struct sp_sampler_varient *samp,
717e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       union tex_tile_address addr, int x, int y, int z)
7186142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
719153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   const struct softpipe_tex_cached_tile *tile;
7206142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
721153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.x = x / TILE_SIZE;
722153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.y = y / TILE_SIZE;
72381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.z = z;
7246142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   y %= TILE_SIZE;
7256142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   x %= TILE_SIZE;
7266142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
727153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   tile = sp_get_cached_tile_tex(samp->cache, addr);
728153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
7296142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   return &tile->data.color[y][x][0];
7306142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
7316142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7326142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
73381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellstatic INLINE const float *
73481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwellget_texel_3d(const struct sp_sampler_varient *samp,
735e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul	     union tex_tile_address addr, int x, int y, int z)
736b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian{
737aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell   const struct pipe_texture *texture = samp->texture;
73881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   unsigned level = addr.bits.level;
7390b9e96fae9493d5d58f046e01c983a3c4267090eBrian
7400b9e96fae9493d5d58f046e01c983a3c4267090eBrian   if (x < 0 || x >= (int) texture->width[level] ||
7410b9e96fae9493d5d58f046e01c983a3c4267090eBrian       y < 0 || y >= (int) texture->height[level] ||
7420b9e96fae9493d5d58f046e01c983a3c4267090eBrian       z < 0 || z >= (int) texture->depth[level]) {
74381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      return samp->sampler->border_color;
744ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   }
745ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   else {
74681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      return get_texel_3d_no_border( samp, addr, x, y, z );
747ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   }
748b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian}
749b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
750b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
75175276ea316610a5737f2115326482024aa09d02aroot/**
75275276ea316610a5737f2115326482024aa09d02aroot * Given the logbase2 of a mipmap's base level size and a mipmap level,
75375276ea316610a5737f2115326482024aa09d02aroot * return the size (in texels) of that mipmap level.
75475276ea316610a5737f2115326482024aa09d02aroot * For example, if level[0].width = 256 then base_pot will be 8.
75575276ea316610a5737f2115326482024aa09d02aroot * If level = 2, then we'll return 64 (the width at level=2).
75675276ea316610a5737f2115326482024aa09d02aroot * Return 1 if level > base_pot.
75775276ea316610a5737f2115326482024aa09d02aroot */
75875276ea316610a5737f2115326482024aa09d02arootstatic INLINE unsigned
75975276ea316610a5737f2115326482024aa09d02arootpot_level_size(unsigned base_pot, unsigned level)
76075276ea316610a5737f2115326482024aa09d02aroot{
76175276ea316610a5737f2115326482024aa09d02aroot   return (base_pot >= level) ? (1 << (base_pot - level)) : 1;
76275276ea316610a5737f2115326482024aa09d02aroot}
76375276ea316610a5737f2115326482024aa09d02aroot
76475276ea316610a5737f2115326482024aa09d02aroot
76581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell/* Some image-filter fastpaths:
76681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell */
767efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paulstatic INLINE void
7684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
769e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                const float s[QUAD_SIZE],
770e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                const float t[QUAD_SIZE],
771e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                const float p[QUAD_SIZE],
772e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                float lodbias,
773e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                                float rgba[NUM_CHANNELS][QUAD_SIZE])
7746142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
7754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
7766142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned  j;
7776142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned level = samp->level;
77875276ea316610a5737f2115326482024aa09d02aroot   unsigned xpot = pot_level_size(samp->xpot, level);
77975276ea316610a5737f2115326482024aa09d02aroot   unsigned ypot = pot_level_size(samp->ypot, level);
7801fd40e506c2207664f0c3f435e4614472ea4c540Keith Whitwell   unsigned xmax = (xpot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, xpot) - 1; */
7811fd40e506c2207664f0c3f435e4614472ea4c540Keith Whitwell   unsigned ymax = (ypot - 1) & (TILE_SIZE - 1); /* MIN2(TILE_SIZE, ypot) - 1; */
782153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   union tex_tile_address addr;
783153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
784153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.value = 0;
785153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.level = samp->level;
786153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
7876142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
7886142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int c;
7896142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7906142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float u = s[j] * xpot - 0.5F;
7916142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float v = t[j] * ypot - 0.5F;
7926142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7936142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int uflr = util_ifloor(u);
7946142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int vflr = util_ifloor(v);
7956142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7966142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float xw = u - (float)uflr;
7976142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      float yw = v - (float)vflr;
7986142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
7996142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int x0 = uflr & (xpot - 1);
8006142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int y0 = vflr & (ypot - 1);
8016142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
802153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell      const float *tx[4];
8036142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8046142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      /* Can we fetch all four at once:
8056142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell       */
806e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul      if (x0 < xmax && y0 < ymax) {
80781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         get_texel_quad_2d_no_border_single_tile(samp, addr, x0, y0, tx);
8086142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
809e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul      else {
8104f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell         unsigned x1 = (x0 + 1) & (xpot - 1);
8114f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell         unsigned y1 = (y0 + 1) & (ypot - 1);
81281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         get_texel_quad_2d_no_border(samp, addr, x0, y0, x1, y1, tx);
8136142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
8146142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8156142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      /* interpolate R, G, B, A */
8166142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      for (c = 0; c < 4; c++) {
8176142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         rgba[c][j] = lerp_2d(xw, yw,
8186142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell                              tx[0][c], tx[1][c],
8196142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell                              tx[2][c], tx[3][c]);
8206142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
8216142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
8226142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
8236142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8246142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic INLINE void
8264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
8274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const float s[QUAD_SIZE],
8284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const float t[QUAD_SIZE],
8294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const float p[QUAD_SIZE],
8304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 float lodbias,
8314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 float rgba[NUM_CHANNELS][QUAD_SIZE])
8326142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
8334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
8346142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned  j;
8356142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned level = samp->level;
83675276ea316610a5737f2115326482024aa09d02aroot   unsigned xpot = pot_level_size(samp->xpot, level);
83775276ea316610a5737f2115326482024aa09d02aroot   unsigned ypot = pot_level_size(samp->ypot, level);
838153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   union tex_tile_address addr;
839153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
840153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.value = 0;
841153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.level = samp->level;
8426142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8436142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
8446142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int c;
8456142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8465fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float u = s[j] * xpot;
8475fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float v = t[j] * ypot;
8486142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8496142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int uflr = util_ifloor(u);
8506142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int vflr = util_ifloor(v);
8516142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8526142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int x0 = uflr & (xpot - 1);
8536142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int y0 = vflr & (ypot - 1);
8546142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
85581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_2d_no_border(samp, addr, x0, y0);
8566142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8576142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      for (c = 0; c < 4; c++) {
8586142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         rgba[c][j] = out[c];
8596142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
8606142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
8616142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
8626142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8636142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic INLINE void
8654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
8664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                const float s[QUAD_SIZE],
8674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                const float t[QUAD_SIZE],
8684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                const float p[QUAD_SIZE],
8694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                float lodbias,
8704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                float rgba[NUM_CHANNELS][QUAD_SIZE])
8716142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
8724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
8736142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned  j;
8746142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   unsigned level = samp->level;
87575276ea316610a5737f2115326482024aa09d02aroot   unsigned xpot = pot_level_size(samp->xpot, level);
87675276ea316610a5737f2115326482024aa09d02aroot   unsigned ypot = pot_level_size(samp->ypot, level);
877153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   union tex_tile_address addr;
878153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell
879153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.value = 0;
880153e474d22d1b440bb6bd7b04dabf244d7455582Keith Whitwell   addr.bits.level = samp->level;
8816142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8826142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
8836142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int c;
8846142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8855fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float u = s[j] * xpot;
8865fdac2dcea09c654725666b3cab5f59dfc9e31a5Keith Whitwell      float v = t[j] * ypot;
8876142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8886142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      int x0, y0;
8896142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      const float *out;
8906142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8916142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      x0 = util_ifloor(u);
8926142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      if (x0 < 0)
8936142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         x0 = 0;
8946142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      else if (x0 > xpot - 1)
8956142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         x0 = xpot - 1;
8966142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
8976142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      y0 = util_ifloor(v);
8986142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      if (y0 < 0)
8996142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         y0 = 0;
9006142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      else if (y0 > ypot - 1)
9016142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         y0 = ypot - 1;
9026142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
90381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      out = get_texel_2d_no_border(samp, addr, x0, y0);
9046142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9056142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      for (c = 0; c < 4; c++) {
9066142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell         rgba[c][j] = out[c];
9076142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
9086142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
9096142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
9106142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
911e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
9126142de393fe34ff0866f8489f1292eb473276f11Keith Whitwellstatic void
9134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_1d_nearest(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],
9174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                        float lodbias,
9184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                        float rgba[NUM_CHANNELS][QUAD_SIZE])
9196142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell{
9204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
9216142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   const struct pipe_texture *texture = samp->texture;
9224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
9234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int width;
9244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x[4];
92581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
9266142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
9284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   width = texture->width[level0];
9296142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(width > 0);
9314f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell
93281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
93381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
93481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
9354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_s(s, width, x);
9364f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell
9374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
93881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_2d(samp, addr, x[j], 0);
93981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      int c;
94081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      for (c = 0; c < 4; c++) {
94181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = out[c];
94281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      }
9434f409da3456070946eda2d8ff5153b3b4306bb46Keith Whitwell   }
9446142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell}
9456142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
9464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
94760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellstatic void
94860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_2d_nearest(struct tgsi_sampler *tgsi_sampler,
94960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      const float s[QUAD_SIZE],
95060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      const float t[QUAD_SIZE],
95160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      const float p[QUAD_SIZE],
95260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      float lodbias,
95360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                      float rgba[NUM_CHANNELS][QUAD_SIZE])
95460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell{
95560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
95660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   const struct pipe_texture *texture = samp->texture;
95760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   unsigned level0, j;
95860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int width, height;
95960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int x[4], y[4];
96060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   union tex_tile_address addr;
96181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
96281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
96360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   level0 = samp->level;
96460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   width = texture->width[level0];
96560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   height = texture->height[level0];
96660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
96760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   assert(width > 0);
9685dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
96960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
97060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.value = 0;
97160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.bits.level = samp->level;
97260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
97360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->nearest_texcoord_s(s, width, x);
97460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->nearest_texcoord_t(t, height, y);
97560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
97660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
97760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *out = get_texel_2d(samp, addr, x[j], y[j]);
97860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      int c;
97960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      for (c = 0; c < 4; c++) {
98060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         rgba[c][j] = out[c];
98160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      }
98260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   }
98360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell}
98460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
985e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
9869659aa6482291d1530c74450612bcd952f542e01José Fonsecastatic INLINE union tex_tile_address
987e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulface(union tex_tile_address addr, unsigned face )
98881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell{
98981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.face = face;
99081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   return addr;
99181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell}
99281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
993e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
994e12810d92ffb3547680b227bf88937c03018112bBrianstatic void
99560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_cube_nearest(struct tgsi_sampler *tgsi_sampler,
996e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        const float s[QUAD_SIZE],
997e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        const float t[QUAD_SIZE],
998e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        const float p[QUAD_SIZE],
999e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        float lodbias,
1000e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                        float rgba[NUM_CHANNELS][QUAD_SIZE])
10010dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
10024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1003aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell   const struct pipe_texture *texture = samp->texture;
10044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const unsigned *faces = samp->faces; /* zero when not cube-mapping */
10054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
1006f9e331a574cc4eba60e0de5a29a4aed4bb40520cBrian   int width, height;
10074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x[4], y[4];
100881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
100981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
10104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
10110b9e96fae9493d5d58f046e01c983a3c4267090eBrian   width = texture->width[level0];
10120b9e96fae9493d5d58f046e01c983a3c4267090eBrian   height = texture->height[level0];
101309a1b912605ff48c8782dcc5aae55ac77e27037bBrian
1014b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   assert(width > 0);
10155dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
101681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
101781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
101881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
1019612cfb749c3526eeb446bbc631bf24716522f373Brian
10204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_s(s, width, x);
10214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_t(t, height, y);
1022f9e331a574cc4eba60e0de5a29a4aed4bb40520cBrian
10234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
102481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_2d(samp, face(addr, faces[j]), x[j], y[j]);
102581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      int c;
102681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      for (c = 0; c < 4; c++) {
102781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = out[c];
102881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      }
10290dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
10300dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
103134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
103234a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
10334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
10344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_3d_nearest(struct tgsi_sampler *tgsi_sampler,
10354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      const float s[QUAD_SIZE],
10364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      const float t[QUAD_SIZE],
10374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      const float p[QUAD_SIZE],
10384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      float lodbias,
10394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                      float rgba[NUM_CHANNELS][QUAD_SIZE])
104034a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
10414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1042aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell   const struct pipe_texture *texture = samp->texture;
10434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
10443d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   int width, height, depth;
10454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x[4], y[4], z[4];
104681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
1047b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
10484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
10490b9e96fae9493d5d58f046e01c983a3c4267090eBrian   width = texture->width[level0];
10500b9e96fae9493d5d58f046e01c983a3c4267090eBrian   height = texture->height[level0];
10510b9e96fae9493d5d58f046e01c983a3c4267090eBrian   depth = texture->depth[level0];
10523d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian
10533d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   assert(width > 0);
10543d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   assert(height > 0);
10553d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   assert(depth > 0);
10563d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian
10574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_s(s, width,  x);
10584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_t(t, height, y);
10594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->nearest_texcoord_p(p, depth,  z);
10603d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian
106181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
106281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
106381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
10644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
106581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *out = get_texel_3d(samp, addr, x[j], y[j], z[j]);
106681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      int c;
106781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      for (c = 0; c < 4; c++) {
106881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = out[c];
106981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      }
10703d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   }
107134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
107234a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
107334a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
107434a48abd5ff82ce9748fc29191e35a0985d47c5fBrianstatic void
10754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_1d_linear(struct tgsi_sampler *tgsi_sampler,
10764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float s[QUAD_SIZE],
10774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float t[QUAD_SIZE],
10784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float p[QUAD_SIZE],
10794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     float lodbias,
10804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     float rgba[NUM_CHANNELS][QUAD_SIZE])
108134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
10824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
10834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_texture *texture = samp->texture;
10844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
10854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int width;
10864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x0[4], x1[4];
10874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float xw[4]; /* weights */
108881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
10894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
10904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
10914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   width = texture->width[level0];
10924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
10934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(width > 0);
10944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
109581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
109681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
109781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
10984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->linear_texcoord_s(s, width, x0, x1, xw);
10994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1100b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   for (j = 0; j < QUAD_SIZE; j++) {
110181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx0 = get_texel_2d(samp, addr, x0[j], 0);
110281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx1 = get_texel_2d(samp, addr, x1[j], 0);
11034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c;
11044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
11054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* interpolate R, G, B, A */
11064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (c = 0; c < 4; c++) {
110781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell         rgba[c][j] = lerp(xw[j], tx0[c], tx1[c]);
11084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
1109b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   }
111034a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
111134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
111281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
111360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellstatic void
111460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_2d_linear(struct tgsi_sampler *tgsi_sampler,
111560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     const float s[QUAD_SIZE],
111660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     const float t[QUAD_SIZE],
111760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     const float p[QUAD_SIZE],
111860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     float lodbias,
111960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                     float rgba[NUM_CHANNELS][QUAD_SIZE])
112060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell{
112160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
112260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   const struct pipe_texture *texture = samp->texture;
112360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   unsigned level0, j;
112460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int width, height;
112560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   int x0[4], y0[4], x1[4], y1[4];
112660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   float xw[4], yw[4]; /* weights */
112760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   union tex_tile_address addr;
112860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
112960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   level0 = samp->level;
113060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   width = texture->width[level0];
113160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   height = texture->height[level0];
113260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
113360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   assert(width > 0);
11345dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
113560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
113660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.value = 0;
113760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   addr.bits.level = samp->level;
113860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
113960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->linear_texcoord_s(s, width,  x0, x1, xw);
114060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   samp->linear_texcoord_t(t, height, y0, y1, yw);
114160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
114260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
114360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx0 = get_texel_2d(samp, addr, x0[j], y0[j]);
114460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx1 = get_texel_2d(samp, addr, x1[j], y0[j]);
114560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx2 = get_texel_2d(samp, addr, x0[j], y1[j]);
114660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      const float *tx3 = get_texel_2d(samp, addr, x1[j], y1[j]);
114760adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      int c;
114860adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell
114960adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      /* interpolate R, G, B, A */
115060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      for (c = 0; c < 4; c++) {
115160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         rgba[c][j] = lerp_2d(xw[j], yw[j],
115260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                              tx0[c], tx1[c],
115360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell                              tx2[c], tx3[c]);
115460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      }
115560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   }
115660adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell}
115781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
115881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
1159b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrianstatic void
116060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwellimg_filter_cube_linear(struct tgsi_sampler *tgsi_sampler,
1161e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       const float s[QUAD_SIZE],
1162e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       const float t[QUAD_SIZE],
1163e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       const float p[QUAD_SIZE],
1164e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       float lodbias,
1165e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                       float rgba[NUM_CHANNELS][QUAD_SIZE])
1166b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
11674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1168aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell   const struct pipe_texture *texture = samp->texture;
11694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const unsigned *faces = samp->faces; /* zero when not cube-mapping */
11704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
1171b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   int width, height;
11724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x0[4], y0[4], x1[4], y1[4];
11734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float xw[4], yw[4]; /* weights */
117481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
1175b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
11764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
11770b9e96fae9493d5d58f046e01c983a3c4267090eBrian   width = texture->width[level0];
11780b9e96fae9493d5d58f046e01c983a3c4267090eBrian   height = texture->height[level0];
1179b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1180b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   assert(width > 0);
11815dbedf3d7e99efe35fad308d382670e44cd60e25Brian Paul   assert(height > 0);
1182b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
118381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
118481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = samp->level;
118581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
11864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->linear_texcoord_s(s, width,  x0, x1, xw);
11874e5c385d2183e7006c9d7ac0823919156bd4b8e6Brian Paul   samp->linear_texcoord_t(t, height, y0, y1, yw);
11884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
11894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
119081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      union tex_tile_address addrj = face(addr, faces[j]);
119181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx0 = get_texel_2d(samp, addrj, x0[j], y0[j]);
119281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx1 = get_texel_2d(samp, addrj, x1[j], y0[j]);
119381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx2 = get_texel_2d(samp, addrj, x0[j], y1[j]);
119481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx3 = get_texel_2d(samp, addrj, x1[j], y1[j]);
11954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c;
11964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
11974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* interpolate R, G, B, A */
11984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (c = 0; c < 4; c++) {
11994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         rgba[c][j] = lerp_2d(xw[j], yw[j],
120081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx0[c], tx1[c],
120181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx2[c], tx3[c]);
1202b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      }
12034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
12044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
12054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
12084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellimg_filter_3d_linear(struct tgsi_sampler *tgsi_sampler,
12094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float s[QUAD_SIZE],
12104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float t[QUAD_SIZE],
12114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     const float p[QUAD_SIZE],
12124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     float lodbias,
12134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                     float rgba[NUM_CHANNELS][QUAD_SIZE])
12144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
12154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
12164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_texture *texture = samp->texture;
12174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned level0, j;
12184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int width, height, depth;
12194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
12204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float xw[4], yw[4], zw[4]; /* interpolation weights */
122181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   union tex_tile_address addr;
12224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = samp->level;
12244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   width = texture->width[level0];
12254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   height = texture->height[level0];
12264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   depth = texture->depth[level0];
12274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
122881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.value = 0;
122981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell   addr.bits.level = level0;
123081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
12314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(width > 0);
12324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(height > 0);
12334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   assert(depth > 0);
12344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->linear_texcoord_s(s, width,  x0, x1, xw);
12364e5c385d2183e7006c9d7ac0823919156bd4b8e6Brian Paul   samp->linear_texcoord_t(t, height, y0, y1, yw);
12374e5c385d2183e7006c9d7ac0823919156bd4b8e6Brian Paul   samp->linear_texcoord_p(p, depth,  z0, z1, zw);
12384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
12404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c;
12414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
124281601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx00 = get_texel_3d(samp, addr, x0[j], y0[j], z0[j]);
124381601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx01 = get_texel_3d(samp, addr, x1[j], y0[j], z0[j]);
124481601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx02 = get_texel_3d(samp, addr, x0[j], y1[j], z0[j]);
124581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx03 = get_texel_3d(samp, addr, x1[j], y1[j], z0[j]);
124681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
124781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx10 = get_texel_3d(samp, addr, x0[j], y0[j], z1[j]);
124881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx11 = get_texel_3d(samp, addr, x1[j], y0[j], z1[j]);
124981601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx12 = get_texel_3d(samp, addr, x0[j], y1[j], z1[j]);
125081601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell      const float *tx13 = get_texel_3d(samp, addr, x1[j], y1[j], z1[j]);
125181601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell
12524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* interpolate R, G, B, A */
12534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (c = 0; c < 4; c++) {
12544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
125581601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx00[c], tx01[c],
125681601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx02[c], tx03[c],
125781601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx10[c], tx11[c],
125881601d85ef6b82297b046d5aab1b70e75168c2faKeith Whitwell                              tx12[c], tx13[c]);
12594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
12604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
12614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
12624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
12654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_linear(struct tgsi_sampler *tgsi_sampler,
1266e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  const float s[QUAD_SIZE],
1267e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  const float t[QUAD_SIZE],
1268e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  const float p[QUAD_SIZE],
1269e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  float lodbias,
1270e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul                  float rgba[NUM_CHANNELS][QUAD_SIZE])
12714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
12724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
12734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_texture *texture = samp->texture;
12744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int level0;
12754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
12764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   lambda = samp->compute_lambda(samp, s, t, p, lodbias);
12784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = (int)lambda;
12794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (lambda < 0.0) {
12814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = 0;
12824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->mag_img_filter( tgsi_sampler, s, t, p, 0, rgba );
12834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
12844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else if (level0 >= texture->last_level) {
12854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = texture->last_level;
12864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->min_img_filter( tgsi_sampler, s, t, p, 0, rgba );
12874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
12884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
12894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float levelBlend = lambda - level0;
12904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba0[4][4];
12914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba1[4][4];
12924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c,j;
12934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0;
12954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->min_img_filter( tgsi_sampler, s, t, p, 0, rgba0 );
12964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
12974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0+1;
12984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->min_img_filter( tgsi_sampler, s, t, p, 0, rgba1 );
12994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (j = 0; j < QUAD_SIZE; j++) {
13014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         for (c = 0; c < 4; c++) {
13024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
1303b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian         }
1304b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      }
1305b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
1306b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
1307b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1308b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
13094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
13104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_nearest(struct tgsi_sampler *tgsi_sampler,
13114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   const float s[QUAD_SIZE],
13124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   const float t[QUAD_SIZE],
13134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   const float p[QUAD_SIZE],
13144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   float lodbias,
13154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                   float rgba[NUM_CHANNELS][QUAD_SIZE])
13164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
13174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
13184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_texture *texture = samp->texture;
13194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
13204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   lambda = samp->compute_lambda(samp, s, t, p, lodbias);
13224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (lambda < 0.0) {
13244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = 0;
13254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->mag_img_filter( tgsi_sampler, s, t, p, 0, rgba );
13264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
13274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
13284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = (int)(lambda + 0.5) ;
13294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = MIN2(samp->level, (int)texture->last_level);
13304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->min_img_filter( tgsi_sampler, s, t, p, 0, rgba );
13314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
133275276ea316610a5737f2115326482024aa09d02aroot
133375276ea316610a5737f2115326482024aa09d02aroot#if 0
133475276ea316610a5737f2115326482024aa09d02aroot   printf("RGBA %g %g %g %g, %g %g %g %g, %g %g %g %g, %g %g %g %g\n",
133575276ea316610a5737f2115326482024aa09d02aroot          rgba[0][0], rgba[1][0], rgba[2][0], rgba[3][0],
133675276ea316610a5737f2115326482024aa09d02aroot          rgba[0][1], rgba[1][1], rgba[2][1], rgba[3][1],
133775276ea316610a5737f2115326482024aa09d02aroot          rgba[0][2], rgba[1][2], rgba[2][2], rgba[3][2],
133875276ea316610a5737f2115326482024aa09d02aroot          rgba[0][3], rgba[1][3], rgba[2][3], rgba[3][3]);
133975276ea316610a5737f2115326482024aa09d02aroot#endif
13404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
13414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
13444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_none(struct tgsi_sampler *tgsi_sampler,
13454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                const float s[QUAD_SIZE],
13464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                const float t[QUAD_SIZE],
13474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                const float p[QUAD_SIZE],
13484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                float lodbias,
13494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                float rgba[NUM_CHANNELS][QUAD_SIZE])
13504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
13514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
13524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda = samp->compute_lambda(samp, s, t, p, lodbias);
13534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (lambda < 0.0) {
13554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->mag_img_filter( tgsi_sampler, s, t, p, 0, rgba );
13564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
13574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
13584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->min_img_filter( tgsi_sampler, s, t, p, 0, rgba );
13594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
13604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
13614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1364e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
1365e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Specialized version of mip_filter_linear with hard-wired calls to
13664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell * 2d lambda calculation and 2d_linear_repeat_POT img filters.
1367a34b8594b7b2d00404bb639621ec1ce918ba0786Brian */
13684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
13694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellmip_filter_linear_2d_linear_repeat_POT(
13704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct tgsi_sampler *tgsi_sampler,
13714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float s[QUAD_SIZE],
13724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float t[QUAD_SIZE],
13734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const float p[QUAD_SIZE],
13744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lodbias,
13754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float rgba[NUM_CHANNELS][QUAD_SIZE])
137600c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell{
13774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
13784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_texture *texture = samp->texture;
13794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int level0;
13804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float lambda;
138100c835918259f8d41c3f74eca679a972713b11b2Keith Whitwell
13824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   lambda = compute_lambda_2d(samp, s, t, p, lodbias);
13834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   level0 = (int)lambda;
13844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* Catches both negative and large values of level0:
13864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    */
13874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if ((unsigned)level0 >= texture->last_level) {
13884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (level0 < 0)
13894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->level = 0;
13904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
13914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->level = texture->last_level;
13924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
13934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      img_filter_2d_linear_repeat_POT( tgsi_sampler, s, t, p, 0, rgba );
13944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
13954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
13964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float levelBlend = lambda - level0;
13974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba0[4][4];
13984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rgba1[4][4];
13994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      int c,j;
14004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0;
14024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      img_filter_2d_linear_repeat_POT( tgsi_sampler, s, t, p, 0, rgba0 );
14034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->level = level0+1;
14054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      img_filter_2d_linear_repeat_POT( tgsi_sampler, s, t, p, 0, rgba1 );
14064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
14074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      for (j = 0; j < QUAD_SIZE; j++) {
14084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         for (c = 0; c < 4; c++) {
14094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
14104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
1411b1ff7dac537947d412bf423a73e7eacd76f90d84Brian Paul      }
1412ea0007cc4ca077c7e3951c4fda122bd242728d70Brian Paul   }
1413b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian}
1414b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
1415b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian
14164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1417e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
1418e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Do shadow/depth comparisons.
14193d8c05f7320151898dd224c1daaf3118e1f7ea34Brian */
14204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstatic void
14214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellsample_compare(struct tgsi_sampler *tgsi_sampler,
1422b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian               const float s[QUAD_SIZE],
1423b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian               const float t[QUAD_SIZE],
14243d8c05f7320151898dd224c1daaf3118e1f7ea34Brian               const float p[QUAD_SIZE],
1425f9e331a574cc4eba60e0de5a29a4aed4bb40520cBrian               float lodbias,
1426b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian               float rgba[NUM_CHANNELS][QUAD_SIZE])
14273d8c05f7320151898dd224c1daaf3118e1f7ea34Brian{
14284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
1429aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
14304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   int j, k0, k1, k2, k3;
14314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float val;
14323d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
14334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->mip_filter( tgsi_sampler, s, t, p, lodbias, rgba );
14343d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
14354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /**
14364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
14374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * When we sampled the depth texture, the depth value was put into all
14384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * RGBA channels.  We look at the red channel here.
14394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    */
1440efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1441efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   /* compare four texcoords vs. four texture samples */
1442efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   switch (sampler->compare_func) {
1443efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_LESS:
1444efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] < rgba[0][0];
1445efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] < rgba[0][1];
1446efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] < rgba[0][2];
1447efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] < rgba[0][3];
1448efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1449efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_LEQUAL:
1450efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] <= rgba[0][0];
1451efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] <= rgba[0][1];
1452efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] <= rgba[0][2];
1453efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] <= rgba[0][3];
1454efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1455efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_GREATER:
1456efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] > rgba[0][0];
1457efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] > rgba[0][1];
1458efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] > rgba[0][2];
1459efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] > rgba[0][3];
1460efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1461efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_GEQUAL:
1462efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] >= rgba[0][0];
1463efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] >= rgba[0][1];
1464efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] >= rgba[0][2];
1465efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] >= rgba[0][3];
1466efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1467efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_EQUAL:
1468efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] == rgba[0][0];
1469efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] == rgba[0][1];
1470efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] == rgba[0][2];
1471efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] == rgba[0][3];
1472efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1473efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_NOTEQUAL:
1474efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = p[0] != rgba[0][0];
1475efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k1 = p[1] != rgba[0][1];
1476efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k2 = p[2] != rgba[0][2];
1477efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k3 = p[3] != rgba[0][3];
1478efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1479efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_ALWAYS:
1480efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = k1 = k2 = k3 = 1;
1481efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1482efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   case PIPE_FUNC_NEVER:
1483efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = k1 = k2 = k3 = 0;
1484efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1485efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   default:
1486efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      k0 = k1 = k2 = k3 = 0;
1487efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      assert(0);
1488efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      break;
1489efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   }
1490efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1491efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   /* convert four pass/fail values to an intensity in [0,1] */
1492efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   val = 0.25F * (k0 + k1 + k2 + k3);
1493efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1494efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
1495efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   for (j = 0; j < 4; j++) {
1496efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
1497efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul      rgba[3][j] = 1.0F;
1498efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul   }
1499efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul}
1500efe9faf0612778db2423a4f8835b318b95d9efd7Brian Paul
1501e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1502e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
1503e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Compute which cube face is referenced by each texcoord and put that
1504e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * info into the sampler faces[] array.  Then sample the cube faces
15050dc4eea64f56cc93e5359372b08b99a2d600273cBrian */
1506e12810d92ffb3547680b227bf88937c03018112bBrianstatic void
15074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellsample_cube(struct tgsi_sampler *tgsi_sampler,
15084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            const float s[QUAD_SIZE],
15094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            const float t[QUAD_SIZE],
15104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            const float p[QUAD_SIZE],
15114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            float lodbias,
15124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            float rgba[NUM_CHANNELS][QUAD_SIZE])
15130dc4eea64f56cc93e5359372b08b99a2d600273cBrian{
15144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = sp_sampler_varient(tgsi_sampler);
15154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   unsigned j;
15164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   float ssss[4], tttt[4];
151709a1b912605ff48c8782dcc5aae55ac77e27037bBrian
15184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /*
15194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     major axis
15204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     direction     target                             sc     tc    ma
15214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     ----------    -------------------------------    ---    ---   ---
15224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     +rx          TEXTURE_CUBE_MAP_POSITIVE_X_EXT    -rz    -ry   rx
15234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     -rx          TEXTURE_CUBE_MAP_NEGATIVE_X_EXT    +rz    -ry   rx
15244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     +ry          TEXTURE_CUBE_MAP_POSITIVE_Y_EXT    +rx    +rz   ry
15254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     -ry          TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT    +rx    -rz   ry
15264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     +rz          TEXTURE_CUBE_MAP_POSITIVE_Z_EXT    +rx    -ry   rz
15274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell     -rz          TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT    -rx    -ry   rz
15284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   */
15294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   for (j = 0; j < QUAD_SIZE; j++) {
15304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rx = s[j];
15314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float ry = t[j];
15324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float rz = p[j];
15334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
15344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      unsigned face;
15354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      float sc, tc, ma;
15364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
1537890679d4322e7ba4f12f32532a3fdd277edff886Keith Whitwell      if (arx >= ary && arx >= arz) {
15384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         if (rx >= 0.0F) {
15394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            face = PIPE_TEX_FACE_POS_X;
15404fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            sc = -rz;
15414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            tc = -ry;
15424fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            ma = arx;
15434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
15444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         else {
15454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            face = PIPE_TEX_FACE_NEG_X;
15464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            sc = rz;
15474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            tc = -ry;
15484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            ma = arx;
15494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
15504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
1551890679d4322e7ba4f12f32532a3fdd277edff886Keith Whitwell      else if (ary >= arx && ary >= arz) {
15524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         if (ry >= 0.0F) {
15534fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            face = PIPE_TEX_FACE_POS_Y;
15544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            sc = rx;
15554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            tc = rz;
15564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            ma = ary;
15574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
15584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         else {
15594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            face = PIPE_TEX_FACE_NEG_Y;
15604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            sc = rx;
15614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            tc = -rz;
15624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            ma = ary;
15634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
15644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
15654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else {
15664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         if (rz > 0.0F) {
15674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            face = PIPE_TEX_FACE_POS_Z;
15684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            sc = rx;
15694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            tc = -ry;
15704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            ma = arz;
15714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
15724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         else {
15734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            face = PIPE_TEX_FACE_NEG_Z;
15744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            sc = -rx;
15754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            tc = -ry;
15764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            ma = arz;
15774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         }
15784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
1579b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1580fd19e8adcd82e88d0fc8d187360b528100fed244Keith Whitwell      {
1581fd19e8adcd82e88d0fc8d187360b528100fed244Keith Whitwell	 const float ima = 1.0 / ma;
1582fd19e8adcd82e88d0fc8d187360b528100fed244Keith Whitwell	 ssss[j] = ( sc * ima + 1.0F ) * 0.5F;
1583fd19e8adcd82e88d0fc8d187360b528100fed244Keith Whitwell	 tttt[j] = ( tc * ima + 1.0F ) * 0.5F;
1584fd19e8adcd82e88d0fc8d187360b528100fed244Keith Whitwell	 samp->faces[j] = face;
1585fd19e8adcd82e88d0fc8d187360b528100fed244Keith Whitwell      }
15864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
158709a1b912605ff48c8782dcc5aae55ac77e27037bBrian
15884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* In our little pipeline, the compare stage is next.  If compare
15894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * is not active, this will point somewhere deeper into the
15904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * pipeline, eg. to mip_filter or even img_filter.
15916142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell    */
15924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->compare(tgsi_sampler, ssss, tttt, NULL, lodbias, rgba);
15934fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
1594612cfb749c3526eeb446bbc631bf24716522f373Brian
15953d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
15963d8c05f7320151898dd224c1daaf3118e1f7ea34Brian
1597e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_nearest_func
1598e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_nearest_unorm_wrap(unsigned mode)
15994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
16004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
16014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
16024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_unorm_clamp;
16034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
16044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
16054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_unorm_clamp_to_border;
16060dc4eea64f56cc93e5359372b08b99a2d600273cBrian   default:
16070dc4eea64f56cc93e5359372b08b99a2d600273cBrian      assert(0);
16084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_unorm_clamp;
16090dc4eea64f56cc93e5359372b08b99a2d600273cBrian   }
16100dc4eea64f56cc93e5359372b08b99a2d600273cBrian}
161134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
161234a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
1613e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_nearest_func
1614e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_nearest_wrap(unsigned mode)
1615a34b8594b7b2d00404bb639621ec1ce918ba0786Brian{
16164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
16174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_REPEAT:
16184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_repeat;
16194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
16204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_clamp;
16214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
16224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_clamp_to_edge;
16234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
16244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_clamp_to_border;
16254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_REPEAT:
16264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_repeat;
16274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP:
16284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_clamp;
16294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
16304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_clamp_to_edge;
16314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
16324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_mirror_clamp_to_border;
16334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   default:
16344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      assert(0);
16354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_nearest_repeat;
16364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1637a34b8594b7b2d00404bb639621ec1ce918ba0786Brian}
1638a34b8594b7b2d00404bb639621ec1ce918ba0786Brian
1639e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1640e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_linear_func
1641e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_linear_unorm_wrap(unsigned mode)
1642a34b8594b7b2d00404bb639621ec1ce918ba0786Brian{
16434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
16444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
16454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_unorm_clamp;
16464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
16474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
16484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_unorm_clamp_to_border;
16494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   default:
16504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      assert(0);
16514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_unorm_clamp;
16524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
1653a34b8594b7b2d00404bb639621ec1ce918ba0786Brian}
1654a34b8594b7b2d00404bb639621ec1ce918ba0786Brian
1655e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1656e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic wrap_linear_func
1657e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_linear_wrap(unsigned mode)
165834a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
16594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (mode) {
16604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_REPEAT:
16614fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_repeat;
16624fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP:
16634fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_clamp;
16644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
16654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_clamp_to_edge;
16664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
16674fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_clamp_to_border;
16684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_REPEAT:
16694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_repeat;
16704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP:
16714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_clamp;
16724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
16734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_clamp_to_edge;
16744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
16754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_mirror_clamp_to_border;
16763d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   default:
16773d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian      assert(0);
16784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return wrap_linear_repeat;
16793d6f9d904f07b7676cc971eb3faf9dd8e7c58e50Brian   }
168034a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
168134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
1682e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1683e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic compute_lambda_func
1684e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_lambda_func(const union sp_sampler_key key)
168534a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
16864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (key.bits.processor == TGSI_PROCESSOR_VERTEX)
16874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_vert;
16884fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
16894fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (key.bits.target) {
16904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_1D:
16914fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_1d;
16924fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_2D:
169387ec83afd58536c31bf02c307f1d5488abc84861Brian Paul   case PIPE_TEXTURE_CUBE:
16944fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_2d;
16954fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_3D:
16964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_3d;
16974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   default:
16984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      assert(0);
16994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return compute_lambda_1d;
1700b4480285ed5098f1c862690ee105dd46f5e6cd1eBrian   }
170134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
170234a48abd5ff82ce9748fc29191e35a0985d47c5fBrian
1703e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul
1704e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulstatic filter_func
1705e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paulget_img_filter(const union sp_sampler_key key,
1706e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul               unsigned filter,
1707e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul               const struct pipe_sampler_state *sampler)
1708b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian{
17094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (key.bits.target) {
17104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_1D:
17114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
17124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_1d_nearest;
17134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
17144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_1d_linear;
1715b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      break;
17164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_2D:
17174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Try for fast path:
17184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
17194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (key.bits.is_pot &&
17204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->wrap_s == sampler->wrap_t &&
17214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->normalized_coords)
172238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian      {
17234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         switch (sampler->wrap_s) {
17244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         case PIPE_TEX_WRAP_REPEAT:
17254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            switch (filter) {
17266142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            case PIPE_TEX_FILTER_NEAREST:
17274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell               return img_filter_2d_nearest_repeat_POT;
17286142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            case PIPE_TEX_FILTER_LINEAR:
17294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell               return img_filter_2d_linear_repeat_POT;
17306142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            default:
17316142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell               break;
173238bee46e83b18ff4ad42d340b507b1a15b4326c7Brian            }
17334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            break;
17344fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         case PIPE_TEX_WRAP_CLAMP:
17354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell            switch (filter) {
17366142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            case PIPE_TEX_FILTER_NEAREST:
17374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell               return img_filter_2d_nearest_clamp_POT;
17386142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell            default:
17396142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell               break;
174038bee46e83b18ff4ad42d340b507b1a15b4326c7Brian            }
1741b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian         }
1742b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      }
174360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      /* Otherwise use default versions:
17444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
17454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
17464fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_2d_nearest;
17474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
17484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_2d_linear;
17494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      break;
175060adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell   case PIPE_TEXTURE_CUBE:
175160adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
175260adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         return img_filter_cube_nearest;
175360adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      else
175460adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell         return img_filter_cube_linear;
175560adc15ba5633190fc8a68e7c182f06dc7909df4Keith Whitwell      break;
17564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEXTURE_3D:
17574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (filter == PIPE_TEX_FILTER_NEAREST)
17584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_3d_nearest;
17594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
17604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         return img_filter_3d_linear;
1761b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      break;
1762b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   default:
1763b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      assert(0);
17644fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return img_filter_1d_nearest;
1765b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian   }
1766b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian}
1767b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1768b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian
1769a34b8594b7b2d00404bb639621ec1ce918ba0786Brian/**
1770a29447c33d44b3427e0c40a761067c0cc6e71c39Brian Paul * Bind the given texture object and texture cache to the sampler varient.
1771a34b8594b7b2d00404bb639621ec1ce918ba0786Brian */
17724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellvoid
17734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellsp_sampler_varient_bind_texture( struct sp_sampler_varient *samp,
17747670102468a55de50cf0cfa0b938d36aaf212f1fKeith Whitwell                                 struct softpipe_tex_tile_cache *tex_cache,
17754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                                 const struct pipe_texture *texture )
177634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian{
17774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   const struct pipe_sampler_state *sampler = samp->sampler;
17784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
17794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->texture = texture;
17804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->cache = tex_cache;
17814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->xpot = util_unsigned_logbase2( texture->width[0] );
17824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->ypot = util_unsigned_logbase2( texture->height[0] );
17834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->level = CLAMP((int) sampler->min_lod, 0, (int) texture->last_level);
17844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell}
17850b9e96fae9493d5d58f046e01c983a3c4267090eBrian
17864f23468bd0d14b8ed687a641003d587b91ad39a7Brian
1787ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwellvoid
1788ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwellsp_sampler_varient_destroy( struct sp_sampler_varient *samp )
1789ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell{
1790ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell   FREE(samp);
1791ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell}
1792ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell
1793ecfa8be150ed276af816467b467e76e026f5b541Keith Whitwell
1794e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul/**
1795e2329f2795d48d11131e9ac105e7aa3fd2c229c1Brian Paul * Create a sampler varient for a given set of non-orthogonal state.
17964fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell */
17974fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellstruct sp_sampler_varient *
17984fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwellsp_create_sampler_varient( const struct pipe_sampler_state *sampler,
17994fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell                           const union sp_sampler_key key )
18004fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell{
18014fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   struct sp_sampler_varient *samp = CALLOC_STRUCT(sp_sampler_varient);
18024fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (!samp)
18034fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      return NULL;
18044fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18054fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->sampler = sampler;
18064fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->key = key;
18074fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18084fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   /* Note that (for instance) linear_texcoord_s and
18094fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * nearest_texcoord_s may be active at the same time, if the
18104fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    * sampler min_img_filter differs from its mag_img_filter.
18114fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell    */
18124fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (sampler->normalized_coords) {
18134fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_s = get_linear_wrap( sampler->wrap_s );
18144fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_t = get_linear_wrap( sampler->wrap_t );
18154fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_p = get_linear_wrap( sampler->wrap_r );
18164fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18174fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_s = get_nearest_wrap( sampler->wrap_s );
18184fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_t = get_nearest_wrap( sampler->wrap_t );
18194fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_p = get_nearest_wrap( sampler->wrap_r );
18204fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
18214fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
18224fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_s = get_linear_unorm_wrap( sampler->wrap_s );
18234fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_t = get_linear_unorm_wrap( sampler->wrap_t );
18244fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->linear_texcoord_p = get_linear_unorm_wrap( sampler->wrap_r );
18254fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18264fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_s = get_nearest_unorm_wrap( sampler->wrap_s );
18274fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_t = get_nearest_unorm_wrap( sampler->wrap_t );
18284fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->nearest_texcoord_p = get_nearest_unorm_wrap( sampler->wrap_r );
18294fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
18304fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18314fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->compute_lambda = get_lambda_func( key );
18324fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18334fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   samp->min_img_filter = get_img_filter(key, sampler->min_img_filter, sampler);
183441483627f0fd3dc9df2cc55dfd5f3e5987fcfd22Brian Paul   samp->mag_img_filter = get_img_filter(key, sampler->mag_img_filter, sampler);
18354fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18364fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   switch (sampler->min_mip_filter) {
18374fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_MIPFILTER_NONE:
18384fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (sampler->min_img_filter == sampler->mag_img_filter)
18394fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = samp->min_img_filter;
1840b1c8fa5b6002296d9abe21c06d5cb81a3f70828aBrian      else
18414fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = mip_filter_none;
184234a48abd5ff82ce9748fc29191e35a0985d47c5fBrian      break;
18434fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18444fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_MIPFILTER_NEAREST:
18454fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->mip_filter = mip_filter_nearest;
184634a48abd5ff82ce9748fc29191e35a0985d47c5fBrian      break;
18474fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18484fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   case PIPE_TEX_MIPFILTER_LINEAR:
18494fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      if (key.bits.is_pot &&
18504fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->min_img_filter == sampler->mag_img_filter &&
18514fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->normalized_coords &&
18524fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->wrap_s == PIPE_TEX_WRAP_REPEAT &&
1853cf102b031e7ef33c8e3ffce2f9dcd064f44e8190Brian Paul          sampler->wrap_t == PIPE_TEX_WRAP_REPEAT &&
18544fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell          sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR)
18554fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      {
18564fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = mip_filter_linear_2d_linear_repeat_POT;
18574fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      }
18584fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      else
18594fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      {
18604fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell         samp->mip_filter = mip_filter_linear;
18616142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell      }
186234a48abd5ff82ce9748fc29191e35a0985d47c5fBrian      break;
186334a48abd5ff82ce9748fc29191e35a0985d47c5fBrian   }
18643d53d38d5e35386de4793162b9dd32e171927059Brian Paul
18654fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (sampler->compare_mode != FALSE) {
18664fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->compare = sample_compare;
18673d53d38d5e35386de4793162b9dd32e171927059Brian Paul   }
18684fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
18694fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Skip compare operation by promoting the mip_filter function
18704fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       * pointer:
18714fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
18724fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->compare = samp->mip_filter;
18734fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
18744fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18754fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   if (key.bits.target == PIPE_TEXTURE_CUBE) {
18764fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->base.get_samples = sample_cube;
18774fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   }
18784fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   else {
18794fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[0] = 0;
18804fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[1] = 0;
18814fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[2] = 0;
18824fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->faces[3] = 0;
18834fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell
18844fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      /* Skip cube face determination by promoting the compare
18854fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       * function pointer:
18864fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell       */
18874fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell      samp->base.get_samples = samp->compare;
18886142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell   }
18896142de393fe34ff0866f8489f1292eb473276f11Keith Whitwell
18904fc7d0345a18042a79686940fb7cc4e698cc9192Keith Whitwell   return samp;
189134a48abd5ff82ce9748fc29191e35a0985d47c5fBrian}
1892