st_atom_sampler.c revision 6c8a13215813841703e7c2efa233e8d4cf517dfd
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29  * Authors:
30  *   Keith Whitwell <keith@tungstengraphics.com>
31  *   Brian Paul
32  */
33
34
35#include "main/macros.h"
36#include "main/mtypes.h"
37#include "main/samplerobj.h"
38#include "main/texobj.h"
39
40#include "st_context.h"
41#include "st_cb_texture.h"
42#include "st_format.h"
43#include "st_atom.h"
44#include "st_texture.h"
45#include "pipe/p_context.h"
46#include "pipe/p_defines.h"
47
48#include "cso_cache/cso_context.h"
49
50
51/**
52 * Convert GLenum texcoord wrap tokens to pipe tokens.
53 */
54static GLuint
55gl_wrap_xlate(GLenum wrap)
56{
57   switch (wrap) {
58   case GL_REPEAT:
59      return PIPE_TEX_WRAP_REPEAT;
60   case GL_CLAMP:
61      return PIPE_TEX_WRAP_CLAMP;
62   case GL_CLAMP_TO_EDGE:
63      return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
64   case GL_CLAMP_TO_BORDER:
65      return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
66   case GL_MIRRORED_REPEAT:
67      return PIPE_TEX_WRAP_MIRROR_REPEAT;
68   case GL_MIRROR_CLAMP_EXT:
69      return PIPE_TEX_WRAP_MIRROR_CLAMP;
70   case GL_MIRROR_CLAMP_TO_EDGE_EXT:
71      return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
72   case GL_MIRROR_CLAMP_TO_BORDER_EXT:
73      return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
74   default:
75      assert(0);
76      return 0;
77   }
78}
79
80
81static GLuint
82gl_filter_to_mip_filter(GLenum filter)
83{
84   switch (filter) {
85   case GL_NEAREST:
86   case GL_LINEAR:
87      return PIPE_TEX_MIPFILTER_NONE;
88
89   case GL_NEAREST_MIPMAP_NEAREST:
90   case GL_LINEAR_MIPMAP_NEAREST:
91      return PIPE_TEX_MIPFILTER_NEAREST;
92
93   case GL_NEAREST_MIPMAP_LINEAR:
94   case GL_LINEAR_MIPMAP_LINEAR:
95      return PIPE_TEX_MIPFILTER_LINEAR;
96
97   default:
98      assert(0);
99      return PIPE_TEX_MIPFILTER_NONE;
100   }
101}
102
103
104static GLuint
105gl_filter_to_img_filter(GLenum filter)
106{
107   switch (filter) {
108   case GL_NEAREST:
109   case GL_NEAREST_MIPMAP_NEAREST:
110   case GL_NEAREST_MIPMAP_LINEAR:
111      return PIPE_TEX_FILTER_NEAREST;
112
113   case GL_LINEAR:
114   case GL_LINEAR_MIPMAP_NEAREST:
115   case GL_LINEAR_MIPMAP_LINEAR:
116      return PIPE_TEX_FILTER_LINEAR;
117
118   default:
119      assert(0);
120      return PIPE_TEX_FILTER_NEAREST;
121   }
122}
123
124
125static void
126convert_sampler(struct st_context *st,
127                struct pipe_sampler_state *sampler,
128                GLuint texUnit)
129{
130   struct gl_texture_object *texobj;
131   struct gl_context *ctx = st->ctx;
132   struct gl_sampler_object *msamp;
133
134   texobj = ctx->Texture.Unit[texUnit]._Current;
135   if (!texobj) {
136      texobj = _mesa_get_fallback_texture(ctx, TEXTURE_2D_INDEX);
137   }
138
139   msamp = _mesa_get_samplerobj(ctx, texUnit);
140
141   memset(sampler, 0, sizeof(*sampler));
142   sampler->wrap_s = gl_wrap_xlate(msamp->WrapS);
143   sampler->wrap_t = gl_wrap_xlate(msamp->WrapT);
144   sampler->wrap_r = gl_wrap_xlate(msamp->WrapR);
145
146   sampler->min_img_filter = gl_filter_to_img_filter(msamp->MinFilter);
147   sampler->min_mip_filter = gl_filter_to_mip_filter(msamp->MinFilter);
148   sampler->mag_img_filter = gl_filter_to_img_filter(msamp->MagFilter);
149
150   if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
151      sampler->normalized_coords = 1;
152
153   sampler->lod_bias = ctx->Texture.Unit[texUnit].LodBias + msamp->LodBias;
154
155   sampler->min_lod = CLAMP(msamp->MinLod,
156                            0.0f,
157                            (GLfloat) texobj->MaxLevel - texobj->BaseLevel);
158   sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel - texobj->BaseLevel,
159                           msamp->MaxLod);
160   if (sampler->max_lod < sampler->min_lod) {
161      /* The GL spec doesn't seem to specify what to do in this case.
162       * Swap the values.
163       */
164      float tmp = sampler->max_lod;
165      sampler->max_lod = sampler->min_lod;
166      sampler->min_lod = tmp;
167      assert(sampler->min_lod <= sampler->max_lod);
168   }
169
170   if (msamp->BorderColor.ui[0] ||
171       msamp->BorderColor.ui[1] ||
172       msamp->BorderColor.ui[2] ||
173       msamp->BorderColor.ui[3]) {
174      struct gl_texture_image *teximg;
175
176      teximg = texobj->Image[0][texobj->BaseLevel];
177
178      st_translate_color(msamp->BorderColor.f,
179                         teximg ? teximg->_BaseFormat : GL_RGBA,
180                         sampler->border_color.f);
181   }
182
183   sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
184                              0 : (GLuint) msamp->MaxAnisotropy);
185
186   /* only care about ARB_shadow, not SGI shadow */
187   if (msamp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
188      sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
189      sampler->compare_func
190         = st_compare_func_to_pipe(msamp->CompareFunc);
191   }
192
193   sampler->seamless_cube_map =
194      ctx->Texture.CubeMapSeamless || msamp->CubeMapSeamless;
195}
196
197
198/**
199 * Update the gallium driver's sampler state for fragment, vertex or
200 * geometry shader stage.
201 */
202static void
203update_shader_samplers(struct st_context *st,
204                       unsigned shader_stage,
205                       const struct gl_program *prog,
206                       unsigned max_units,
207                       struct pipe_sampler_state *samplers,
208                       unsigned *num_samplers)
209{
210   GLuint unit;
211   GLbitfield samplers_used;
212   const GLuint old_max = *num_samplers;
213
214   samplers_used = prog->SamplersUsed;
215
216   if (*num_samplers == 0 && samplers_used == 0x0)
217       return;
218
219   *num_samplers = 0;
220
221   /* loop over sampler units (aka tex image units) */
222   for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
223      struct pipe_sampler_state *sampler = samplers + unit;
224
225      if (samplers_used & 1) {
226         const GLuint texUnit = prog->SamplerUnits[unit];
227
228         convert_sampler(st, sampler, texUnit);
229
230         *num_samplers = unit + 1;
231
232         cso_single_sampler(st->cso_context, shader_stage, unit, sampler);
233      }
234      else if (samplers_used != 0 || unit < old_max) {
235         cso_single_sampler(st->cso_context, shader_stage, unit, NULL);
236      }
237      else {
238         /* if we've reset all the old samplers and we have no more new ones */
239         break;
240      }
241   }
242
243   cso_single_sampler_done(st->cso_context, shader_stage);
244}
245
246
247static void
248update_samplers(struct st_context *st)
249{
250   const struct gl_context *ctx = st->ctx;
251
252   update_shader_samplers(st,
253                          PIPE_SHADER_FRAGMENT,
254                          &ctx->FragmentProgram._Current->Base,
255                          ctx->Const.MaxTextureImageUnits,
256                          st->state.samplers[PIPE_SHADER_FRAGMENT],
257                          &st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
258
259   update_shader_samplers(st,
260                          PIPE_SHADER_VERTEX,
261                          &ctx->VertexProgram._Current->Base,
262                          ctx->Const.MaxVertexTextureImageUnits,
263                          st->state.samplers[PIPE_SHADER_VERTEX],
264                          &st->state.num_samplers[PIPE_SHADER_VERTEX]);
265
266/*
267   update_shader_samplers(st,
268                          PIPE_SHADER_GEOMETRY,
269                          &ctx->GeometryProgram._Current->Base,
270                          ctx->Const.MaxGeometryTextureImageUnits,
271                          st->state.samplers[PIPE_SHADER_GEOMETRY],
272                          &st->state.num_samplers[PIPE_SHADER_GEOMETRY]);
273*/
274}
275
276
277const struct st_tracked_state st_update_sampler = {
278   "st_update_sampler",					/* name */
279   {							/* dirty */
280      _NEW_TEXTURE,					/* mesa */
281      0,						/* st */
282   },
283   update_samplers					/* update */
284};
285