brw_wm_sampler_state.c revision 32dfa6e5ef3d1fb703ec34942c55408be22e7ec3
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
47b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholtuint32_t
48b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholttranslate_wrap_mode(GLenum wrap, bool using_nearest)
499f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
509f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   switch( wrap ) {
519f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_REPEAT:
529f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_WRAP;
53b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt   case GL_CLAMP:
54b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt      /* GL_CLAMP is the weird mode where coordinates are clamped to
55b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * [0.0, 1.0], so linear filtering of coordinates outside of
56b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * [0.0, 1.0] give you half edge texel value and half border
57b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * color.  The fragment shader will clamp the coordinates, and
58b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * we set clamp_border here, which gets the result desired.  We
59b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * just use clamp(_to_edge) for nearest, because for nearest
60b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * clamping to 1.0 gives border color instead of the desired
61b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       * edge texels.
62b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt       */
63b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt      if (using_nearest)
64b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 return BRW_TEXCOORDMODE_CLAMP;
65b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt      else
66b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 return BRW_TEXCOORDMODE_CLAMP_BORDER;
679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP_TO_EDGE:
68b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt      return BRW_TEXCOORDMODE_CLAMP;
699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_CLAMP_TO_BORDER:
709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_CLAMP_BORDER;
719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_MIRRORED_REPEAT:
729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_MIRROR;
739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
749f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      return BRW_TEXCOORDMODE_WRAP;
759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt}
779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
78ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke/**
79ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke * Upload SAMPLER_BORDER_COLOR_STATE.
80ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke */
813984372104ec6ac5986dedb07b8ca99d53dede18Kenneth Graunkevoid
825c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholtupload_default_color(struct brw_context *brw, struct gl_sampler_object *sampler,
835c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt		     int unit)
849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt{
8530f25a10190632a971e2b214c443e54afa463eadEric Anholt   struct intel_context *intel = &brw->intel;
865c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_context *ctx = &intel->ctx;
875c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
885c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_object *texObj = texUnit->_Current;
895c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
905c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   float color[4];
915c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
925c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (firstImage->_BaseFormat == GL_DEPTH_COMPONENT) {
935c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      /* GL specs that border color for depth textures is taken from the
945c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt       * R channel, while the hardware uses A.  Spam R into all the
955c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt       * channels for safety.
965c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt       */
975c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[0] = sampler->BorderColor.f[0];
985c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[1] = sampler->BorderColor.f[0];
995c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[2] = sampler->BorderColor.f[0];
1005c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[3] = sampler->BorderColor.f[0];
1015c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   } else {
1025c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[0] = sampler->BorderColor.f[0];
1035c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[1] = sampler->BorderColor.f[1];
1045c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[2] = sampler->BorderColor.f[2];
1055c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      color[3] = sampler->BorderColor.f[3];
1065c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   }
1079f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
108ead7d73a5143ccfc55e6c00b8ab5ba0b893e5f33Kenneth Graunke   if (intel->gen == 5 || intel->gen == 6) {
1095c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      struct gen5_sampler_default_color *sdc;
11030f25a10190632a971e2b214c443e54afa463eadEric Anholt
111d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt      sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
112d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt			    sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
11330f25a10190632a971e2b214c443e54afa463eadEric Anholt
1145c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      memset(sdc, 0, sizeof(*sdc));
1159f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1165c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[0], color[0]);
1175c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[1], color[1]);
1185c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[2], color[2]);
1195c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_UBYTE(sdc->ub[3], color[3]);
1209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
1215c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[0], color[0]);
1225c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[1], color[1]);
1235c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[2], color[2]);
1245c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_USHORT(sdc->us[3], color[3]);
1253e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1265c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[0], color[0]);
1275c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[1], color[1]);
1285c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[2], color[2]);
1295c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      UNCLAMPED_FLOAT_TO_SHORT(sdc->s[3], color[3]);
130e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
1315c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[0] = _mesa_float_to_half(color[0]);
1325c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[1] = _mesa_float_to_half(color[1]);
1335c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[2] = _mesa_float_to_half(color[2]);
1345c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->hf[3] = _mesa_float_to_half(color[3]);
1355c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1365c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[0] = sdc->s[0] >> 8;
1375c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[1] = sdc->s[1] >> 8;
1385c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[2] = sdc->s[2] >> 8;
1395c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->b[3] = sdc->s[3] >> 8;
1405c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1415c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[0] = color[0];
1425c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[1] = color[1];
1435c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[2] = color[2];
1445c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      sdc->f[3] = color[3];
1455c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   } else {
1465c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      struct brw_sampler_default_color *sdc;
1475c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
148d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt      sdc = brw_state_batch(brw, AUB_TRACE_SAMPLER_DEFAULT_COLOR,
149d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt			    sizeof(*sdc), 32, &brw->wm.sdc_offset[unit]);
1505c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
1515c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      COPY_4V(sdc->color, color);
1525c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   }
1535c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt}
1543e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
1553e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/**
1563e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * Sets the sampler state for a single unit based off of the sampler key
1573e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * entry.
1589f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt */
159956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wangstatic void brw_update_sampler_state(struct brw_context *brw,
1605c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt				     int unit,
1613e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt				     struct brw_sampler_state *sampler)
1623e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
163956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang   struct intel_context *intel = &brw->intel;
1645c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_context *ctx = &intel->ctx;
1655c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1665c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_texture_object *texObj = texUnit->_Current;
1675c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
168b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt   bool using_nearest = false;
169956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang
1705c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   switch (gl_sampler->MinFilter) {
1719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST:
1729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
174b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt      using_nearest = true;
1759f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR:
1779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
1799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1809f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_NEAREST:
1819f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1829f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1839f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1849f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_NEAREST:
1859f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1869f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
1879f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_NEAREST_MIPMAP_LINEAR:
1899f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
1909f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1929f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   case GL_LINEAR_MIPMAP_LINEAR:
1939f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
1949f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
1959f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1969f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   default:
1979f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      break;
1989f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
1999f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2009f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set Anisotropy:
2019f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2025c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (gl_sampler->MaxAnisotropy > 1.0) {
2039f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
2049f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
2059f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2065c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (gl_sampler->MaxAnisotropy > 2.0) {
2075c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
2089f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt				       BRW_ANISORATIO_16);
2099f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
2109f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2119f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   else {
2125c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      switch (gl_sampler->MagFilter) {
2139f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_NEAREST:
2149f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
215b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt	 using_nearest = true;
2169f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
2179f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      case GL_LINEAR:
2189f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
2199f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
2209f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      default:
2219f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt	 break;
2229f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      }
2239f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2249f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
225b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt   sampler->ss1.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR,
226b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt						  using_nearest);
227b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt   sampler->ss1.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS,
228b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt						  using_nearest);
229b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt   sampler->ss1.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT,
230b126a0c0cb30b1e2f2df1953fe14d8596d1cf4f7Eric Anholt						  using_nearest);
231e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
232956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang   if (intel->gen >= 6 &&
233956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang       sampler->ss0.min_filter != sampler->ss0.mag_filter)
234956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang	sampler->ss0.min_mag_neq = 1;
235956f866030f7bea5fc4a2de28c72e60bdc3a5b3dZhenyu Wang
236e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick   /* Cube-maps on 965 and later must use the same wrap mode for all 3
237e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick    * coordinate dimensions.  Futher, only CUBE and CLAMP are valid.
238e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick    */
2395c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
2405c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (ctx->Texture.CubeMapSeamless &&
2415c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	  (gl_sampler->MinFilter != GL_NEAREST ||
2425c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	   gl_sampler->MagFilter != GL_NEAREST)) {
243e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
244e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
245e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
246e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      } else {
247e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
248e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
249e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick	 sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
250e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick      }
2515c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   } else if (texObj->Target == GL_TEXTURE_1D) {
252ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison      /* There's a bug in 1D texture sampling - it actually pays
253ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * attention to the wrap_t value, though it should not.
254ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * Override the wrap_t value here to GL_REPEAT to keep
255ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       * any nonexistent border pixels from floating in.
256ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison       */
257ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison      sampler->ss1.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
258ab6c4fa582972e25f8800c77b5dd5b3a83afc996Robert Ellison   }
259e304c65a2b9c1005d6216e91d90a99001549a63dIan Romanick
2609f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set shadow function:
2629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2635c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
2649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      /* Shadowing is "enabled" by emitting a particular sampler
2659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * message (sample_c).  So need to recompile WM program when
2669f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       * shadow comparison is enabled on each/any texture unit.
2679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt       */
2683e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt      sampler->ss0.shadow_function =
2695c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
2709f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
2719f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2729f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   /* Set LOD bias:
2739f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt    */
2745c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
2755c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt					 gl_sampler->LodBias, -16, 15), 6);
2769f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2779f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
2789f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
2799f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2805b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   /* Set BaseMipLevel, MaxLOD, MinLOD:
2815b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    *
2825b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * XXX: I don't think that using firstLevel, lastLevel works,
2835b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * because we always setup the surface state as if firstLevel ==
2845b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * level zero.  Probably have to subtract firstLevel from each of
2855b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    * these:
2865b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt    */
2875b3eb7538cd9ceb967b6e9e765896183e7c2c4d4Eric Anholt   sampler->ss0.base_level = U_FIXED(0, 1);
2889f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2895c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 6);
2905c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 6);
2919f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
2929d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt   /* On Gen6+, the sampler can handle non-normalized texture
2939d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt    * rectangle coordinates natively
2949d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt    */
2959d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt   if (intel->gen >= 6 && texObj->Target == GL_TEXTURE_RECTANGLE) {
2969d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt      sampler->ss3.non_normalized_coord = 1;
2979d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt   }
2989d4b98eb9eadecc17cd1cda0074b420a39e74647Eric Anholt
2995c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   upload_default_color(brw, gl_sampler, unit);
3008c093a1fb0843e152d2a515c5127ccd5999b0d94Brian Paul
301a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   if (intel->gen >= 6) {
302a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      sampler->ss2.default_color_pointer = brw->wm.sdc_offset[unit] >> 5;
303a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   } else {
304a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      /* reloc */
305a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      sampler->ss2.default_color_pointer = (intel->batch.bo->offset +
306a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt					    brw->wm.sdc_offset[unit]) >> 5;
307a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt
308a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt      drm_intel_bo_emit_reloc(intel->batch.bo,
30932dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke			      brw->sampler.offset +
310a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      unit * sizeof(struct brw_sampler_state) +
311a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      offsetof(struct brw_sampler_state, ss2),
312a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      intel->batch.bo, brw->wm.sdc_offset[unit],
313a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt			      I915_GEM_DOMAIN_SAMPLER, 0);
314a82a43e8d99e1715dd11c9c091b5ab734079b6a6Eric Anholt   }
31576669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu
31676669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu   if (sampler->ss0.min_filter != BRW_MAPFILTER_NEAREST)
31776669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu      sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MIN |
31876669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu                                    BRW_ADDRESS_ROUNDING_ENABLE_V_MIN |
31976669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu                                    BRW_ADDRESS_ROUNDING_ENABLE_R_MIN;
32076669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu   if (sampler->ss0.mag_filter != BRW_MAPFILTER_NEAREST)
32176669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu      sampler->ss3.address_round |= BRW_ADDRESS_ROUNDING_ENABLE_U_MAG |
32276669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu                                    BRW_ADDRESS_ROUNDING_ENABLE_V_MAG |
32376669381c0de6a49a1edd0b88fa1ae6b86f10b30Yuanhan Liu                                    BRW_ADDRESS_ROUNDING_ENABLE_R_MAG;
3243e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt}
3253e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
3265c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
3273e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt/* All samplers must be uploaded in a single contiguous array, which
3283e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * complicates various things.  However, this is still too confusing -
3293e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt * FIXME: simplify all the different new texture state flags.
3303e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt */
3315c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholtstatic void
33232dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunkebrw_upload_samplers(struct brw_context *brw)
3333e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt{
334f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = &brw->intel.ctx;
3355c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   struct brw_sampler_state *samplers;
3365c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   int i;
3373e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
33832dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke   brw->sampler.count = 0;
3395c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
3405c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (ctx->Texture.Unit[i]._ReallyEnabled)
34132dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke	 brw->sampler.count = i + 1;
3429f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   }
3439f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
34432dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke   if (brw->sampler.count == 0)
345f75843a517bd188639e6866db2a7b04de3524e16Dave Airlie      return;
3463e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
347d375df220fae47f38944c4832bcbd5f5d568884cEric Anholt   samplers = brw_state_batch(brw, AUB_TRACE_SAMPLER_STATE,
34832dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke			      brw->sampler.count * sizeof(*samplers),
34932dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke			      32, &brw->sampler.offset);
35032dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke   memset(samplers, 0, brw->sampler.count * sizeof(*samplers));
3513e3644c6e8f3559fd5bcd862d5edb627aceb3ecaEric Anholt
35232dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke   for (i = 0; i < brw->sampler.count; i++) {
3535c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      if (ctx->Texture.Unit[i]._ReallyEnabled)
3545c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt	 brw_update_sampler_state(brw, i, &samplers[i]);
35538bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt   }
3565c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt
3575c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt   brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
35838bad7677e57d629eeffd4ef39a7fc254db12735Eric Anholt}
3599f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
36032dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunkeconst struct brw_tracked_state brw_samplers = {
3619f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   .dirty = {
3629f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .mesa = _NEW_TEXTURE,
3635c742ea1ee0cea031cb99651155d0c7521f42b4eEric Anholt      .brw = BRW_NEW_BATCH,
3649f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt      .cache = 0
3659f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt   },
36632dfa6e5ef3d1fb703ec34942c55408be22e7ec3Kenneth Graunke   .emit = brw_upload_samplers,
3679f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt};
3689f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
3699f344b3e7d6e23674dd4747faec253f103563b36Eric Anholt
370