brw_wm_sampler_state.c revision ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33
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 */
513984372104ec6ac5986dedb07b8ca99d53dede18Kenneth GraunkeGLuint
523984372104ec6ac5986dedb07b8ca99d53dede18Kenneth Graunketranslate_wrap_mode(GLenum wrap)
539f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch( wrap ) {
559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_REPEAT:
569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_WRAP;
579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP:
58ab999608582534bb5187a786b2ea437167f2d8a4Xiang, Haihao      return BRW_TEXCOORDMODE_CLAMP;
599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP_TO_EDGE:
609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_CLAMP; /* conform likes it this way */
619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP_TO_BORDER:
629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_CLAMP_BORDER;
639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_MIRRORED_REPEAT:
649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_MIRROR;
659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_WRAP;
679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
70ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke/**
71ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke * Upload SAMPLER_BORDER_COLOR_STATE.
72ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke */
733984372104ec6ac5986dedb07b8ca99d53dede18Kenneth Graunkevoid
745c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholtupload_default_color(struct brw_context *brw, struct gl_sampler_object *sampler,
755c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt		     int unit)
769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
7730f25a10190632a971e2b214c443e54afa463eadEric Anholt   struct intel_context *intel = &brw->intel;
785c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_context *ctx = &intel->ctx;
795c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
805c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_object *texObj = texUnit->_Current;
815c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
825c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   float color[4];
835c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
845c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
855c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      /* GL specs that border color for depth textures is taken from the
865c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt       * R channel, while the hardware uses A.  Spam R into all the
875c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt       * channels for safety.
885c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt       */
895c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[0] = sampler->BorderColor.f[0];
905c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[1] = sampler->BorderColor.f[0];
915c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[2] = sampler->BorderColor.f[0];
925c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[3] = sampler->BorderColor.f[0];
935c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   } else {
945c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[0] = sampler->BorderColor.f[0];
955c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[1] = sampler->BorderColor.f[1];
965c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[2] = sampler->BorderColor.f[2];
975c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[3] = sampler->BorderColor.f[3];
985c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   }
999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
100ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke   if (intel->gen == 5 || intel->gen == 6) {
1015c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      struct gen5_sampler_default_color *sdc;
10230f25a10190632a971e2b214c443e54afa463eadEric Anholt
1035c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc = brw_state_batch(brw, sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
10430f25a10190632a971e2b214c443e54afa463eadEric Anholt
1055c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      memset(sdc, 0, sizeof(*sdc));
1069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1075c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
1085c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
1095c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
1105c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
1119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1125c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
1135c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
1145c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
1155c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
1163e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1175c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
1185c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
1195c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
1205c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
121e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
1225c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[0] = _mesa_float_to_half(color[0]);
1235c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[1] = _mesa_float_to_half(color[1]);
1245c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[2] = _mesa_float_to_half(color[2]);
1255c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[3] = _mesa_float_to_half(color[3]);
1265c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1275c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[0] = sdc->s[0] >> 8;
1285c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[1] = sdc->s[1] >> 8;
1295c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[2] = sdc->s[2] >> 8;
1305c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[3] = sdc->s[3] >> 8;
1315c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1325c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[0] = color[0];
1335c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[1] = color[1];
1345c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[2] = color[2];
1355c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[3] = color[3];
1365c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   } else {
1375c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      struct brw_sampler_default_color *sdc;
1385c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1395c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc = brw_state_batch(brw, sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
1405c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1415c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      COPY_4V(sdc->color, color);
1425c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   }
1435c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt}
1443e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1453e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/**
1463e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * Sets the sampler state for a single unit based off of the sampler key
1473e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * entry.
1489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
149956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wangstatic void brw_update_sampler_state(struct brw_context *brw,
1505c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt				     int unit,
1513e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				     struct brw_sampler_state *sampler)
1523e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
153956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang   struct intel_context *intel = &brw->intel;
1545c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_context *ctx = &intel->ctx;
1555c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1565c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_object *texObj = texUnit->_Current;
1575c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
158956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang
1595c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   switch (gl_sampler->MinFilter) {
1609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST:
1619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR:
1659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_NEAREST:
1699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_NEAREST:
1739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_LINEAR:
1779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_LINEAR:
1819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
1859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set Anisotropy:
1899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
1905c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (gl_sampler->MaxAnisotropy > 1.0) {
1919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
1929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
1939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1945c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (gl_sampler->MaxAnisotropy > 2.0) {
1955c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
1969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				       BRW_ANISORATIO_16);
1979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
1989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   else {
2005c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      switch (gl_sampler->MagFilter) {
2019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_NEAREST:
2029f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
2039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
2049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_LINEAR:
2059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
2069f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
2079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      default:
2089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
2099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
2109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2125c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR);
2135c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS);
2145c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT);
215e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
216956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang   if (intel->gen >= 6 &&
217956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang       sampler->ss0.min_filter != sampler->ss0.mag_filter)
218956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang	sampler->ss0.min_mag_neq = 1;
219956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang
220e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   /* Cube-maps on 965 and later must use the same wrap mode for all 3
221e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick    * coordinate dimensions.  Futher, only CUBE and CLAMP are valid.
222e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick    */
2235c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
2245c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (ctx->Texture.CubeMapSeamless &&
2255c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	  (gl_sampler->MinFilter != GL_NEAREST ||
2265c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	   gl_sampler->MagFilter != GL_NEAREST)) {
227e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
228e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
229e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
230e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      } else {
231e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
232e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
233e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
234e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      }
2355c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   } else if (texObj->Target == GL_TEXTURE_1D) {
236ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison      /* There's a bug in 1D texture sampling - it actually pays
237ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * attention to the wrap_t value, though it should not.
238ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * Override the wrap_t value here to GL_REPEAT to keep
239ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * any nonexistent border pixels from floating in.
240ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       */
241ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison      sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
242ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison   }
243e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
2449f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2459f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set shadow function:
2469f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2475c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
2489f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      /* Shadowing is "enabled" by emitting a particular sampler
2499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * message (sample_c).  So need to recompile WM program when
2509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * shadow comparison is enabled on each/any texture unit.
2519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       */
2523e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      sampler->ss0.shadow_function =
2535c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
2549f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2559f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2569f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set LOD bias:
2579f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2585c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
2595c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt					 gl_sampler->LodBias, -16, 15), 6);
2609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
2629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
2639f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2645b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   /* Set BaseMipLevel, MaxLOD, MinLOD:
2655b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    *
2665b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * XXX: I don't think that using firstLevel, lastLevel works,
2675b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * because we always setup the surface state as if firstLevel ==
2685b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * level zero.  Probably have to subtract firstLevel from each of
2695b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * these:
2705b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    */
2715b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   sampler->ss0.base_level = U_FIXED(0, 1);
2729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2735c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
2745c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
2759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2765c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   upload_default_color(brw, gl_sampler, unit);
2778c093a1fb0843e152d2a515c5127ccd5999b0d94Brian Paul
278a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   if (intel->gen >= 6) {
279a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      sampler->ss2.default_color_pointer = brw->wm.sdc_offset[unit] >> 5;
280a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   } else {
281a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      /* reloc */
282a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      sampler->ss2.default_color_pointer = (intel->batch.bo->offset +
283a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt					    brw->wm.sdc_offset[unit]) >> 5;
284a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt
285a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      drm_intel_bo_emit_reloc(intel->batch.bo,
286a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      brw->wm.sampler_offset +
287a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      unit * sizeof(struct brw_sampler_state) +
288a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      offsetof(struct brw_sampler_state, ss2),
289a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      intel->batch.bo, brw->wm.sdc_offset[unit],
290a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      I915_GEM_DOMAIN_SAMPLER, 0);
291a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   }
2923e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt}
2933e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
2945c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
2953e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/* All samplers must be uploaded in a single contiguous array, which
2963e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * complicates various things.  However, this is still too confusing -
2973e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * FIXME: simplify all the different new texture state flags.
2983e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt */
2995c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholtstatic void
3005c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholtprepare_wm_samplers(struct brw_context *brw)
3013e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
302f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
3035c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct brw_sampler_state *samplers;
3045c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   int i;
3053e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3065c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   brw->wm.sampler_count = 0;
3075c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
3085c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (ctx->Texture.Unit[i]._ReallyEnabled)
3095c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 brw->wm.sampler_count = i + 1;
3109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3123e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt   if (brw->wm.sampler_count == 0)
313f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie      return;
3143e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3155c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   samplers = brw_state_batch(brw, brw->wm.sampler_count * sizeof(*samplers),
3165c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt			      32, &brw->wm.sampler_offset);
3175c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   memset(samplers, 0, brw->wm.sampler_count * sizeof(*samplers));
3183e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3195c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   for (i = 0; i < brw->wm.sampler_count; i++) {
3205c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (ctx->Texture.Unit[i]._ReallyEnabled)
3215c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 brw_update_sampler_state(brw, i, &samplers[i]);
32238bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   }
3235c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
3245c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
32538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt}
3269f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3279f344b3e7d6e23674dd4747faec253f103563b36Eric Anholtconst struct brw_tracked_state brw_wm_samplers = {
3289f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
3299f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa = _NEW_TEXTURE,
3305c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      .brw = BRW_NEW_BATCH,
3319f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .cache = 0
3329f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
3335c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   .prepare = prepare_wm_samplers,
3349f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
3359f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3369f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
337