brw_wm_sampler_state.c revision 1f32c665c8af0622e2bbf451edb999ffbcd7d0fe
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
37ecadb51bbcb972a79f3ed79e65a7986b9396e757Brian Paul#include "main/macros.h"
381f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt#include "main/samplerobj.h"
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
6934474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholtstatic drm_intel_bo *upload_default_color( struct brw_context *brw,
7038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt				     const GLfloat *color )
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
7230f25a10190632a971e2b214c443e54afa463eadEric Anholt   struct intel_context *intel = &brw->intel;
739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
7430f25a10190632a971e2b214c443e54afa463eadEric Anholt   if (intel->gen >= 5) {
7530f25a10190632a971e2b214c443e54afa463eadEric Anholt      struct gen5_sampler_default_color sdc;
7630f25a10190632a971e2b214c443e54afa463eadEric Anholt
7730f25a10190632a971e2b214c443e54afa463eadEric Anholt      memset(&sdc, 0, sizeof(sdc));
7830f25a10190632a971e2b214c443e54afa463eadEric Anholt
7930f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc.ub[0], color[0]);
8030f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc.ub[1], color[1]);
8130f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc.ub[2], color[2]);
8230f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc.ub[3], color[3]);
8330f25a10190632a971e2b214c443e54afa463eadEric Anholt
8430f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc.us[0], color[0]);
8530f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc.us[1], color[1]);
8630f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc.us[2], color[2]);
8730f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc.us[3], color[3]);
8830f25a10190632a971e2b214c443e54afa463eadEric Anholt
8930f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc.s[0], color[0]);
9030f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc.s[1], color[1]);
9130f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc.s[2], color[2]);
9230f25a10190632a971e2b214c443e54afa463eadEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc.s[3], color[3]);
9330f25a10190632a971e2b214c443e54afa463eadEric Anholt
9430f25a10190632a971e2b214c443e54afa463eadEric Anholt      /* XXX: Fill in half floats */
9530f25a10190632a971e2b214c443e54afa463eadEric Anholt      /* XXX: Fill in signed bytes */
9630f25a10190632a971e2b214c443e54afa463eadEric Anholt
9730f25a10190632a971e2b214c443e54afa463eadEric Anholt      COPY_4V(sdc.f, color);
9830f25a10190632a971e2b214c443e54afa463eadEric Anholt
9930f25a10190632a971e2b214c443e54afa463eadEric Anholt      return brw_cache_data(&brw->cache, BRW_SAMPLER_DEFAULT_COLOR,
10030f25a10190632a971e2b214c443e54afa463eadEric Anholt			    &sdc, sizeof(sdc));
10130f25a10190632a971e2b214c443e54afa463eadEric Anholt   } else {
10230f25a10190632a971e2b214c443e54afa463eadEric Anholt      struct brw_sampler_default_color sdc;
10330f25a10190632a971e2b214c443e54afa463eadEric Anholt
10430f25a10190632a971e2b214c443e54afa463eadEric Anholt      COPY_4V(sdc.color, color);
10530f25a10190632a971e2b214c443e54afa463eadEric Anholt
10630f25a10190632a971e2b214c443e54afa463eadEric Anholt      return brw_cache_data(&brw->cache, BRW_SAMPLER_DEFAULT_COLOR,
10730f25a10190632a971e2b214c443e54afa463eadEric Anholt			    &sdc, sizeof(sdc));
10830f25a10190632a971e2b214c443e54afa463eadEric Anholt   }
1099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
1109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1123e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstruct wm_sampler_key {
1133e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   int sampler_count;
1143e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1153e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   struct wm_sampler_entry {
1166f915b10d5963466567ea5445631192fd9c4e802Brian Paul      GLenum tex_target;
1173e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      GLenum wrap_r, wrap_s, wrap_t;
1183e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      float maxlod, minlod;
1193e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      float lod_bias;
1203e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      float max_aniso;
1213e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      GLenum minfilter, magfilter;
1227086df58688dc375ffd4c0fb9a9884eae05a6e46Xiang, Haihao      GLenum comparemode, comparefunc;
123e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
124e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      /** If target is cubemap, take context setting.
125e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick       */
126e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      GLboolean seamless_cube_map;
1273e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   } sampler[BRW_MAX_TEX_UNIT];
1283e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt};
1293e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1303e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/**
1313e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * Sets the sampler state for a single unit based off of the sampler key
1323e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * entry.
1339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
134956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wangstatic void brw_update_sampler_state(struct brw_context *brw,
135956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang				     struct wm_sampler_entry *key,
13634474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt				     drm_intel_bo *sdc_bo,
1373e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				     struct brw_sampler_state *sampler)
1383e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
139956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang   struct intel_context *intel = &brw->intel;
140956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang
14126f8fad1456fdc2b352cea9d3b4c32cb5f6ae947Kenneth Graunke   memset(sampler, 0, sizeof(*sampler));
1429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1433e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   switch (key->minfilter) {
1449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST:
1459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1479f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR:
1499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_NEAREST:
1539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_NEAREST:
1579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_LINEAR:
1619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_LINEAR:
1659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
1699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set Anisotropy:
1739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
1743e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (key->max_aniso > 1.0) {
1759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
1769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
1779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1783e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      if (key->max_aniso > 2.0) {
17950853be894aa3edd1e9271f7d625f319209e340fRoland Scheidegger	 sampler->ss3.max_aniso = MIN2((key->max_aniso - 2) / 2,
1809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				       BRW_ANISORATIO_16);
1819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
1829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   else {
1843e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      switch (key->magfilter) {
1859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_NEAREST:
1869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
1879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
1889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_LINEAR:
1899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
1909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
1919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      default:
1929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
1939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
1949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
196e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   sampler->ss1.r_wrap_mode = translate_wrap_mode(key->wrap_r);
197e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   sampler->ss1.s_wrap_mode = translate_wrap_mode(key->wrap_s);
198e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   sampler->ss1.t_wrap_mode = translate_wrap_mode(key->wrap_t);
199e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
200956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang   if (intel->gen >= 6 &&
201956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang       sampler->ss0.min_filter != sampler->ss0.mag_filter)
202956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang	sampler->ss0.min_mag_neq = 1;
203956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang
204e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   /* Cube-maps on 965 and later must use the same wrap mode for all 3
205e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick    * coordinate dimensions.  Futher, only CUBE and CLAMP are valid.
206e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick    */
207e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   if (key->tex_target == GL_TEXTURE_CUBE_MAP) {
208e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      if (key->seamless_cube_map &&
209e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	  (key->minfilter != GL_NEAREST || key->magfilter != GL_NEAREST)) {
210e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
211e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
212e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
213e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      } else {
214e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
215e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
216e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
217e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      }
218e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   } else if (key->tex_target == GL_TEXTURE_1D) {
219ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison      /* There's a bug in 1D texture sampling - it actually pays
220ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * attention to the wrap_t value, though it should not.
221ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * Override the wrap_t value here to GL_REPEAT to keep
222ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * any nonexistent border pixels from floating in.
223ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       */
224ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison      sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
225ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison   }
226e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
2279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set shadow function:
2299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2303e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (key->comparemode == GL_COMPARE_R_TO_TEXTURE_ARB) {
2319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      /* Shadowing is "enabled" by emitting a particular sampler
2329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * message (sample_c).  So need to recompile WM program when
2339f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * shadow comparison is enabled on each/any texture unit.
2349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       */
2353e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      sampler->ss0.shadow_function =
2367086df58688dc375ffd4c0fb9a9884eae05a6e46Xiang, Haihao	 intel_translate_shadow_compare_func(key->comparefunc);
2379f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2389f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2399f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set LOD bias:
2409f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2413e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   sampler->ss0.lod_bias = S_FIXED(CLAMP(key->lod_bias, -16, 15), 6);
2429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
2449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
2459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2465b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   /* Set BaseMipLevel, MaxLOD, MinLOD:
2475b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    *
2485b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * XXX: I don't think that using firstLevel, lastLevel works,
2495b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * because we always setup the surface state as if firstLevel ==
2505b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * level zero.  Probably have to subtract firstLevel from each of
2515b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * these:
2525b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    */
2535b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   sampler->ss0.base_level = U_FIXED(0, 1);
2549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
255374447244165b3b79892cb6840a76ea4c2f21f1eEric Anholt   sampler->ss1.max_lod = U_FIXED(CLAMP(key->maxlod, 0, 13), 6);
256374447244165b3b79892cb6840a76ea4c2f21f1eEric Anholt   sampler->ss1.min_lod = U_FIXED(CLAMP(key->minlod, 0, 13), 6);
2579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
25838bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   sampler->ss2.default_color_pointer = sdc_bo->offset >> 5; /* reloc */
2599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
2609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2618c093a1fb0843e152d2a515c5127ccd5999b0d94Brian Paul
2623e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/** Sets up the cache key for sampler state for all texture units */
2633e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtstatic void
2643e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholtbrw_wm_sampler_populate_key(struct brw_context *brw,
2653e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt			    struct wm_sampler_key *key)
2669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
267f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
2683e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   int unit;
269da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock   char *last_entry_end = ((char*)&key->sampler_count) +
270da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock      sizeof(key->sampler_count);
2713e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2728c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt   key->sampler_count = 0;
2739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
275052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt      if (ctx->Texture.Unit[unit]._ReallyEnabled) {
2763e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 struct wm_sampler_entry *entry = &key->sampler[unit];
277052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
2789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 struct gl_texture_object *texObj = texUnit->_Current;
2797216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	 struct gl_texture_image *firstImage =
2801b18b45d79e065e4e05a1e89e9d756d96258ded5Eric Anholt	    texObj->Image[0][texObj->BaseLevel];
2811f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
2826f915b10d5963466567ea5445631192fd9c4e802Brian Paul
283da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock	 memset(last_entry_end, 0,
284da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock		(char*)entry - last_entry_end + sizeof(*entry));
285da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock	 last_entry_end = ((char*)entry) + sizeof(*entry);
2868c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt
2876f915b10d5963466567ea5445631192fd9c4e802Brian Paul         entry->tex_target = texObj->Target;
288e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
289e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 entry->seamless_cube_map = (texObj->Target == GL_TEXTURE_CUBE_MAP)
290e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	    ? ctx->Texture.CubeMapSeamless : GL_FALSE;
2919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2921f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->wrap_r = sampler->WrapR;
2931f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->wrap_s = sampler->WrapS;
2941f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->wrap_t = sampler->WrapT;
295ecfaab88b2577bd0395bc05d75a036126806a9c4Brian Paul
2961f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->maxlod = sampler->MaxLod;
2971f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->minlod = sampler->MinLod;
2981f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->lod_bias = texUnit->LodBias + sampler->LodBias;
2991f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->max_aniso = sampler->MaxAnisotropy;
3001f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->minfilter = sampler->MinFilter;
3011f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->magfilter = sampler->MagFilter;
3021f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	 entry->comparemode = sampler->CompareMode;
3031f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt         entry->comparefunc = sampler->CompareFunc;
3049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
30534474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt	 drm_intel_bo_unreference(brw->wm.sdc_bo[unit]);
3067216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	 if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
3077216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	    float bordercolor[4] = {
3081f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	       sampler->BorderColor.f[0],
3091f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	       sampler->BorderColor.f[0],
3101f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	       sampler->BorderColor.f[0],
3111f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt	       sampler->BorderColor.f[0]
3127216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	    };
3137216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	    /* GL specs that border color for depth textures is taken from the
3147216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	     * R channel, while the hardware uses A.  Spam R into all the
3157216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	     * channels for safety.
3167216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	     */
3177216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	    brw->wm.sdc_bo[unit] = upload_default_color(brw, bordercolor);
3187216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	 } else {
3197216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	    brw->wm.sdc_bo[unit] = upload_default_color(brw,
3201f32c665c8af0622e2bbf451edb999ffbcd7d0feEric Anholt							sampler->BorderColor.f);
3217216679c1998b49ff5b08e6b43f8d5779415bf54Eric Anholt	 }
3223e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 key->sampler_count = unit + 1;
3239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
3249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
325da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock   struct wm_sampler_entry *entry = &key->sampler[key->sampler_count];
326da498ef41e88e98e3710d7de5e2488f359196621Pierre Willenbrock   memset(last_entry_end, 0, (char*)entry - last_entry_end);
3273e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt}
3283e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3293e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/* All samplers must be uploaded in a single contiguous array, which
3303e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * complicates various things.  However, this is still too confusing -
3313e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * FIXME: simplify all the different new texture state flags.
3323e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt */
333f75843a517bd188639e6866db2a7b04de3524e16Dave Airliestatic void upload_wm_samplers( struct brw_context *brw )
3343e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
335f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
3363e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   struct wm_sampler_key key;
3378c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt   int i, sampler_key_size;
3383e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3393e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   brw_wm_sampler_populate_key(brw, &key);
3403e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3413e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_count != key.sampler_count) {
3423e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      brw->wm.sampler_count = key.sampler_count;
3439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
3449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
34634474fa4119378ef9fbb9fb557cc19c0a1ca1f7eEric Anholt   drm_intel_bo_unreference(brw->wm.sampler_bo);
3473e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   brw->wm.sampler_bo = NULL;
3483e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_count == 0)
349f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie      return;
3503e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3518c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt   /* Only include the populated portion of the key in the search. */
3528c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt   sampler_key_size = offsetof(struct wm_sampler_key,
3538c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt			       sampler[key.sampler_count]);
3543e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   brw->wm.sampler_bo = brw_search_cache(&brw->cache, BRW_SAMPLER,
3558c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt					 &key, sampler_key_size,
3563e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					 brw->wm.sdc_bo, key.sampler_count,
3573e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					 NULL);
3583e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3593e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   /* If we didnt find it in the cache, compute the state and put it in the
3603e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt    * cache.
3613e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt    */
3623e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_bo == NULL) {
3633e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      struct brw_sampler_state sampler[BRW_MAX_TEX_UNIT];
3643e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3653e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      memset(sampler, 0, sizeof(sampler));
3663e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      for (i = 0; i < key.sampler_count; i++) {
3673e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	 if (brw->wm.sdc_bo[i] == NULL)
3683e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt	    continue;
3693e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
370956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang	 brw_update_sampler_state(brw, &key.sampler[i], brw->wm.sdc_bo[i],
3713e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				  &sampler[i]);
3723e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      }
3733e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3743e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      brw->wm.sampler_bo = brw_upload_cache(&brw->cache, BRW_SAMPLER,
3758c8e664f1086e702e1102ee7f5043c8c12c037bdEric Anholt					    &key, sampler_key_size,
3763e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt					    brw->wm.sdc_bo, key.sampler_count,
37762a96f74c9a1fd07301d349e4181a7212fc7d45cEric Anholt					    &sampler, sizeof(sampler));
3789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3798abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt      /* Emit SDC relocations */
3808abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt      for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
381052c1d66a1ab1f2665870dc77dab28d20416cdf1Eric Anholt	 if (!ctx->Texture.Unit[i]._ReallyEnabled)
3828abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt	    continue;
38338bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt
384df3c1a563f3d76b07ab82c7b230b0030452f36ffEric Anholt	 drm_intel_bo_emit_reloc(brw->wm.sampler_bo,
385df3c1a563f3d76b07ab82c7b230b0030452f36ffEric Anholt				 i * sizeof(struct brw_sampler_state) +
386df3c1a563f3d76b07ab82c7b230b0030452f36ffEric Anholt				 offsetof(struct brw_sampler_state, ss2),
387df3c1a563f3d76b07ab82c7b230b0030452f36ffEric Anholt				 brw->wm.sdc_bo[i], 0,
388df3c1a563f3d76b07ab82c7b230b0030452f36ffEric Anholt				 I915_GEM_DOMAIN_SAMPLER, 0);
3898abffada70fcd62e3c2dcbcdc6d00d258805326bEric Anholt      }
39038bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   }
39138bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt}
3929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_samplers = {
3949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
3959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa = _NEW_TEXTURE,
3969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .brw = 0,
3979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .cache = 0
3989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
399008653ac55776d6b1c6d1627ad20937aa1c4dbdaDave Airlie   .prepare = upload_wm_samplers,
4009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
4019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
4029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
403