brw_wm_sampler_state.c revision 3e3644c6e8f3559fd5bcd862d5edb627aceb3eca
19f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/*
29f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Copyright (C) Intel Corp.  2006.  All Rights Reserved.
39f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
49f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt develop this 3D driver.
59f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
69f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt Permission is hereby granted, free of charge, to any person obtaining
79f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt a copy of this software and associated documentation files (the
89f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt "Software"), to deal in the Software without restriction, including
99f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt without limitation the rights to use, copy, modify, merge, publish,
109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt distribute, sublicense, and/or sell copies of the Software, and to
119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt permit persons to whom the Software is furnished to do so, subject to
129f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt the following conditions:
139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt The above copyright notice and this permission notice (including the
159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt next paragraph) shall be included in all copies or substantial
169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt portions of the Software.
179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt **********************************************************************/
279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt /*
289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  * Authors:
299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  *   Keith Whitwell <keith@tungstengraphics.com>
309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt  */
319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_context.h"
349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_state.h"
359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "brw_defines.h"
369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#include "macros.h"
389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/* Samplers aren't strictly wm state from the hardware's perspective,
429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * but that is the only situation in which we use them in this driver.
439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt/* The brw (and related graphics cores) do not support GL_CLAMP.  The
489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * Intel drivers for "other operating systems" implement GL_CLAMP as
499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt * GL_CLAMP_TO_EDGE, so the same is done here.
509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic GLuint translate_wrap_mode( GLenum wrap )
529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch( wrap ) {
549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_REPEAT:
559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_WRAP;
569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP:
57ab999608582534bb5187a786b2ea437167f2d8a4Xiang, Haihao      return BRW_TEXCOORDMODE_CLAMP;
589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP_TO_EDGE:
599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_CLAMP; /* conform likes it this way */
609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP_TO_BORDER:
619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_CLAMP_BORDER;
629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_MIRRORED_REPEAT:
639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_MIRROR;
649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_WRAP;
669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic GLuint U_FIXED(GLfloat value, GLuint frac_bits)
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   value *= (1<<frac_bits);
739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   return value < 0 ? 0 : value;
749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtstatic GLint S_FIXED(GLfloat value, GLuint frac_bits)
779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   return value * (1<<frac_bits);
799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
8238bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholtstatic dri_bo *upload_default_color( struct brw_context *brw,
8338bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt				     const GLfloat *color )
849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   struct brw_sampler_default_color sdc;
869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   COPY_4V(sdc.color, color);
889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
8938bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   return brw_cache_data( &brw->cache, BRW_SAMPLER_DEFAULT_COLOR, &sdc,
9038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt			  NULL, 0 );
919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
943e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstruct wm_sampler_key {
953e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   int sampler_count;
963e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
973e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   struct wm_sampler_entry {
983e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      GLenum wrap_r, wrap_s, wrap_t;
993e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      float maxlod, minlod;
1003e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      float lod_bias;
1013e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      float max_aniso;
1023e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      GLenum minfilter, magfilter;
1033e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      GLenum comparemode;
1043e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      dri_bo *sdc_bo;
1053e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   } sampler[BRW_MAX_TEX_UNIT];
1063e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt};
1073e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1083e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/**
1093e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * Sets the sampler state for a single unit based off of the sampler key
1103e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * entry.
1119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
1123e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstatic void brw_update_sampler_state(struct wm_sampler_entry *key,
1133e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				     dri_bo *sdc_bo,
1143e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				     struct brw_sampler_state *sampler)
1153e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
1169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   _mesa_memset(sampler, 0, sizeof(*sampler));
1179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1183e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   switch (key->minfilter) {
1199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST:
1209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR:
1249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_NEAREST:
1289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_NEAREST:
1329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_LINEAR:
1369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_LINEAR:
1409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1419f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
1449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set Anisotropy:
1489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
1493e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (key->max_aniso > 1.0) {
1509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
1519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
1529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1533e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      if (key->max_aniso > 2.0) {
1543e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 sampler->ss3.max_aniso = MAX2((key->max_aniso - 2) / 2,
1559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				       BRW_ANISORATIO_16);
1569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
1579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   else {
1593e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      switch (key->magfilter) {
1609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_NEAREST:
1619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
1629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
1639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_LINEAR:
1649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
1659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
1669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      default:
1679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
1689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
1699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1713e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss1.r_wrap_mode = translate_wrap_mode(key->wrap_r);
1723e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss1.s_wrap_mode = translate_wrap_mode(key->wrap_s);
1733e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss1.t_wrap_mode = translate_wrap_mode(key->wrap_t);
1749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Fulsim complains if I don't do this.  Hardware doesn't mind:
1769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
1779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#if 0
1789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   if (texObj->Target == GL_TEXTURE_CUBE_MAP_ARB) {
1799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
1809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
1819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
1829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt#endif
1849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set shadow function:
1869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
1873e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (key->comparemode == GL_COMPARE_R_TO_TEXTURE_ARB) {
1889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      /* Shadowing is "enabled" by emitting a particular sampler
1899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * message (sample_c).  So need to recompile WM program when
1909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * shadow comparison is enabled on each/any texture unit.
1919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       */
1923e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      sampler->ss0.shadow_function =
1933e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 intel_translate_shadow_compare_func(key->comparemode);
1949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set LOD bias:
1979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
1983e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss0.lod_bias = S_FIXED(CLAMP(key->lod_bias, -16, 15), 6);
1999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
2019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
2029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set BaseMipLevel, MaxLOD, MinLOD:
2049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    *
2059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    * XXX: I don't think that using firstLevel, lastLevel works,
2069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    * because we always setup the surface state as if firstLevel ==
2079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    * level zero.  Probably have to subtract firstLevel from each of
2089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    * these:
2099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.base_level = U_FIXED(0, 1);
2119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2123e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss1.max_lod = U_FIXED(MIN2(MAX2(key->maxlod, 0), 13), 6);
2133e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss1.min_lod = U_FIXED(MIN2(MAX2(key->minlod, 0), 13), 6);
2149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
21538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   sampler->ss2.default_color_pointer = sdc_bo->offset >> 5; /* reloc */
2169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
2179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2183e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/** Sets up the cache key for sampler state for all texture units */
2193e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstatic void
2203e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtbrw_wm_sampler_populate_key(struct brw_context *brw,
2213e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt			    struct wm_sampler_key *key)
2229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
2233e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   int unit;
2243e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2253e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   memset(key, 0, sizeof(*key));
2269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
2283e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      if (brw->attribs.Texture->Unit[unit]._ReallyEnabled) {
2293e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 struct wm_sampler_entry *entry = &key->sampler[unit];
2309f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 struct gl_texture_unit *texUnit = &brw->attribs.Texture->Unit[unit];
2319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 struct gl_texture_object *texObj = texUnit->_Current;
2329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2333e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->wrap_r = texObj->WrapR;
2343e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->wrap_s = texObj->WrapS;
2353e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->wrap_t = texObj->WrapT;
2369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2373e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->maxlod = texObj->MaxLod;
2383e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->minlod = texObj->MinLod;
2393e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->lod_bias = texUnit->LodBias + texObj->LodBias;
2403e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->max_aniso = texObj->MaxAnisotropy;
2413e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->minfilter = texObj->MinFilter;
2423e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->magfilter = texObj->MagFilter;
2433e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 entry->comparemode = texObj->CompareMode;
2449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
24538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt	 dri_bo_unreference(brw->wm.sdc_bo[unit]);
2463e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 brw->wm.sdc_bo[unit] = upload_default_color(brw, texObj->BorderColor);
2473e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2483e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 key->sampler_count = unit + 1;
2499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
2509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2513e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt}
2523e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2533e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/* All samplers must be uploaded in a single contiguous array, which
2543e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * complicates various things.  However, this is still too confusing -
2553e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * FIXME: simplify all the different new texture state flags.
2563e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt */
2573e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstatic void upload_wm_samplers( struct brw_context *brw )
2583e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
2593e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   struct wm_sampler_key key;
2603e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   int i;
2613e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2623e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   brw_wm_sampler_populate_key(brw, &key);
2633e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2643e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_count != key.sampler_count) {
2653e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      brw->wm.sampler_count = key.sampler_count;
2669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
2679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
26938bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   dri_bo_unreference(brw->wm.sampler_bo);
2703e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   brw->wm.sampler_bo = NULL;
2713e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_count == 0)
2723e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      return;
2733e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2743e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   brw->wm.sampler_bo = brw_search_cache(&brw->cache, BRW_SAMPLER,
2753e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					 &key, sizeof(key),
2763e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					 brw->wm.sdc_bo, key.sampler_count,
2773e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					 NULL);
2783e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2793e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   /* If we didnt find it in the cache, compute the state and put it in the
2803e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt    * cache.
2813e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt    */
2823e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_bo == NULL) {
2833e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      struct brw_sampler_state sampler[BRW_MAX_TEX_UNIT];
2843e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2853e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      memset(sampler, 0, sizeof(sampler));
2863e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      for (i = 0; i < key.sampler_count; i++) {
2873e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 if (brw->wm.sdc_bo[i] == NULL)
2883e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	    continue;
2893e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2903e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 brw_update_sampler_state(&key.sampler[i], brw->wm.sdc_bo[i],
2913e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				  &sampler[i]);
2923e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      }
2933e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2943e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      brw->wm.sampler_bo = brw_upload_cache(&brw->cache, BRW_SAMPLER,
2953e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					    &key, sizeof(key),
2963e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					    brw->wm.sdc_bo, key.sampler_count,
2973e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					    &sampler, sizeof(sampler),
2983e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					    NULL, NULL);
29938bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   }
3009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
3019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
30238bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholtstatic void emit_reloc_wm_samplers(struct brw_context *brw)
30338bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt{
30438bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   GLuint unit;
30538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt
30638bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   if (brw->wm.sampler_count == 0)
30738bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt      return;
30838bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt
30938bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   /* Emit SDC relocations */
31038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
31138bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt      if (!brw->attribs.Texture->Unit[unit]._ReallyEnabled)
31238bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt	 continue;
31338bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt
31438bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt      dri_emit_reloc(brw->wm.sampler_bo,
31538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt		     DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
31638bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt		     0,
31738bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt		     unit * sizeof(struct brw_sampler_state) +
31838bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt		     offsetof(struct brw_sampler_state, ss2),
31938bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt		     brw->wm.sdc_bo[unit]);
32038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   }
32138bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt}
3229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_samplers = {
3249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
3259f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa = _NEW_TEXTURE,
3269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw = 0,
3279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .cache = 0
3289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
32938bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   .update = upload_wm_samplers,
33038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt  .emit_reloc = emit_reloc_wm_samplers,
3319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
3329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
334