gen7_sampler_state.c revision 461e193971db4ac456be0d383e9aebf15868542a
1/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "brw_context.h"
25#include "brw_state.h"
26#include "brw_defines.h"
27#include "intel_batchbuffer.h"
28
29#include "main/macros.h"
30#include "main/samplerobj.h"
31
32/**
33 * Sets the sampler state for a single unit.
34 */
35static void
36gen7_update_sampler_state(struct brw_context *brw, int unit,
37			  struct gen7_sampler_state *sampler)
38{
39   struct intel_context *intel = &brw->intel;
40   struct gl_context *ctx = &intel->ctx;
41   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
42   struct gl_texture_object *texObj = texUnit->_Current;
43   struct gl_sampler_object *gl_sampler = _mesa_get_samplerobj(ctx, unit);
44
45   switch (gl_sampler->MinFilter) {
46   case GL_NEAREST:
47      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
48      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
49      break;
50   case GL_LINEAR:
51      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
52      sampler->ss0.mip_filter = BRW_MIPFILTER_NONE;
53      break;
54   case GL_NEAREST_MIPMAP_NEAREST:
55      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
56      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
57      break;
58   case GL_LINEAR_MIPMAP_NEAREST:
59      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
60      sampler->ss0.mip_filter = BRW_MIPFILTER_NEAREST;
61      break;
62   case GL_NEAREST_MIPMAP_LINEAR:
63      sampler->ss0.min_filter = BRW_MAPFILTER_NEAREST;
64      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
65      break;
66   case GL_LINEAR_MIPMAP_LINEAR:
67      sampler->ss0.min_filter = BRW_MAPFILTER_LINEAR;
68      sampler->ss0.mip_filter = BRW_MIPFILTER_LINEAR;
69      break;
70   default:
71      break;
72   }
73
74   /* Set Anisotropy: */
75   if (gl_sampler->MaxAnisotropy > 1.0) {
76      sampler->ss0.min_filter = BRW_MAPFILTER_ANISOTROPIC;
77      sampler->ss0.mag_filter = BRW_MAPFILTER_ANISOTROPIC;
78
79      if (gl_sampler->MaxAnisotropy > 2.0) {
80	 sampler->ss3.max_aniso = MIN2((gl_sampler->MaxAnisotropy - 2) / 2,
81				       BRW_ANISORATIO_16);
82      }
83   }
84   else {
85      switch (gl_sampler->MagFilter) {
86      case GL_NEAREST:
87	 sampler->ss0.mag_filter = BRW_MAPFILTER_NEAREST;
88	 break;
89      case GL_LINEAR:
90	 sampler->ss0.mag_filter = BRW_MAPFILTER_LINEAR;
91	 break;
92      default:
93	 break;
94      }
95   }
96
97   sampler->ss3.r_wrap_mode = translate_wrap_mode(gl_sampler->WrapR);
98   sampler->ss3.s_wrap_mode = translate_wrap_mode(gl_sampler->WrapS);
99   sampler->ss3.t_wrap_mode = translate_wrap_mode(gl_sampler->WrapT);
100
101   /* Cube-maps on 965 and later must use the same wrap mode for all 3
102    * coordinate dimensions.  Futher, only CUBE and CLAMP are valid.
103    */
104   if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
105      if (ctx->Texture.CubeMapSeamless &&
106	  (gl_sampler->MinFilter != GL_NEAREST ||
107	   gl_sampler->MagFilter != GL_NEAREST)) {
108	 sampler->ss3.r_wrap_mode = BRW_TEXCOORDMODE_CUBE;
109	 sampler->ss3.s_wrap_mode = BRW_TEXCOORDMODE_CUBE;
110	 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_CUBE;
111      } else {
112	 sampler->ss3.r_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
113	 sampler->ss3.s_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
114	 sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_CLAMP;
115      }
116   } else if (texObj->Target == GL_TEXTURE_1D) {
117      /* There's a bug in 1D texture sampling - it actually pays
118       * attention to the wrap_t value, though it should not.
119       * Override the wrap_t value here to GL_REPEAT to keep
120       * any nonexistent border pixels from floating in.
121       */
122      sampler->ss3.t_wrap_mode = BRW_TEXCOORDMODE_WRAP;
123   }
124
125   /* Set shadow function: */
126   if (gl_sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB) {
127      /* Shadowing is "enabled" by emitting a particular sampler
128       * message (sample_c).  So need to recompile WM program when
129       * shadow comparison is enabled on each/any texture unit.
130       */
131      sampler->ss1.shadow_function =
132	 intel_translate_shadow_compare_func(gl_sampler->CompareFunc);
133   }
134
135   /* Set LOD bias: */
136   sampler->ss0.lod_bias = S_FIXED(CLAMP(texUnit->LodBias +
137					 gl_sampler->LodBias, -16, 15), 8);
138
139   sampler->ss0.lod_preclamp = 1; /* OpenGL mode */
140   sampler->ss0.default_color_mode = 0; /* OpenGL/DX10 mode */
141
142   /* Set BaseMipLevel, MaxLOD, MinLOD:
143    *
144    * XXX: I don't think that using firstLevel, lastLevel works,
145    * because we always setup the surface state as if firstLevel ==
146    * level zero.  Probably have to subtract firstLevel from each of
147    * these:
148    */
149   sampler->ss0.base_level = U_FIXED(0, 1);
150
151   sampler->ss1.max_lod = U_FIXED(CLAMP(gl_sampler->MaxLod, 0, 13), 8);
152   sampler->ss1.min_lod = U_FIXED(CLAMP(gl_sampler->MinLod, 0, 13), 8);
153
154   upload_default_color(brw, gl_sampler, unit);
155
156   sampler->ss2.default_color_pointer = brw->wm.sdc_offset[unit] >> 5;
157}
158
159
160/* All samplers must be uploaded in a single contiguous array, which
161 * complicates various things.  However, this is still too confusing -
162 * FIXME: simplify all the different new texture state flags.
163 */
164static void
165gen7_prepare_samplers(struct brw_context *brw)
166{
167   struct gl_context *ctx = &brw->intel.ctx;
168   struct gen7_sampler_state *samplers;
169   int i;
170
171   brw->wm.sampler_count = 0;
172   for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
173      if (ctx->Texture.Unit[i]._ReallyEnabled)
174	 brw->wm.sampler_count = i + 1;
175   }
176
177   if (brw->wm.sampler_count == 0)
178      return;
179
180   samplers = brw_state_batch(brw, brw->wm.sampler_count * sizeof(*samplers),
181			      32, &brw->wm.sampler_offset);
182   memset(samplers, 0, brw->wm.sampler_count * sizeof(*samplers));
183
184   for (i = 0; i < brw->wm.sampler_count; i++) {
185      if (ctx->Texture.Unit[i]._ReallyEnabled)
186	 gen7_update_sampler_state(brw, i, &samplers[i]);
187   }
188
189   brw->state.dirty.cache |= CACHE_NEW_SAMPLER;
190}
191
192const struct brw_tracked_state gen7_samplers = {
193   .dirty = {
194      .mesa = _NEW_TEXTURE,
195      .brw = BRW_NEW_BATCH,
196      .cache = 0
197   },
198   .prepare = gen7_prepare_samplers,
199};
200