1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/glformats.h"
38 #include "main/samplerobj.h"
39 #include "main/texobj.h"
41 #include "st_context.h"
42 #include "st_cb_texture.h"
43 #include "st_format.h"
45 #include "st_texture.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
49 #include "cso_cache/cso_context.h"
51 #include "util/u_format.h"
55 * Convert GLenum texcoord wrap tokens to pipe tokens.
58 gl_wrap_xlate(GLenum wrap)
62 return PIPE_TEX_WRAP_REPEAT;
64 return PIPE_TEX_WRAP_CLAMP;
65 case GL_CLAMP_TO_EDGE:
66 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
67 case GL_CLAMP_TO_BORDER:
68 return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
69 case GL_MIRRORED_REPEAT:
70 return PIPE_TEX_WRAP_MIRROR_REPEAT;
71 case GL_MIRROR_CLAMP_EXT:
72 return PIPE_TEX_WRAP_MIRROR_CLAMP;
73 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
74 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
75 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
76 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
85 gl_filter_to_mip_filter(GLenum filter)
90 return PIPE_TEX_MIPFILTER_NONE;
92 case GL_NEAREST_MIPMAP_NEAREST:
93 case GL_LINEAR_MIPMAP_NEAREST:
94 return PIPE_TEX_MIPFILTER_NEAREST;
96 case GL_NEAREST_MIPMAP_LINEAR:
97 case GL_LINEAR_MIPMAP_LINEAR:
98 return PIPE_TEX_MIPFILTER_LINEAR;
102 return PIPE_TEX_MIPFILTER_NONE;
108 gl_filter_to_img_filter(GLenum filter)
112 case GL_NEAREST_MIPMAP_NEAREST:
113 case GL_NEAREST_MIPMAP_LINEAR:
114 return PIPE_TEX_FILTER_NEAREST;
117 case GL_LINEAR_MIPMAP_NEAREST:
118 case GL_LINEAR_MIPMAP_LINEAR:
119 return PIPE_TEX_FILTER_LINEAR;
123 return PIPE_TEX_FILTER_NEAREST;
129 convert_sampler(struct st_context *st,
130 struct pipe_sampler_state *sampler,
133 const struct gl_texture_object *texobj;
134 struct gl_context *ctx = st->ctx;
135 struct gl_sampler_object *msamp;
136 const struct gl_texture_image *teximg;
137 GLenum texBaseFormat;
139 texobj = ctx->Texture.Unit[texUnit]._Current;
141 texobj = _mesa_get_fallback_texture(ctx, TEXTURE_2D_INDEX);
144 teximg = texobj->Image[0][texobj->BaseLevel];
145 texBaseFormat = teximg ? teximg->_BaseFormat : GL_RGBA;
147 msamp = _mesa_get_samplerobj(ctx, texUnit);
149 memset(sampler, 0, sizeof(*sampler));
150 sampler->wrap_s = gl_wrap_xlate(msamp->WrapS);
151 sampler->wrap_t = gl_wrap_xlate(msamp->WrapT);
152 sampler->wrap_r = gl_wrap_xlate(msamp->WrapR);
154 sampler->min_img_filter = gl_filter_to_img_filter(msamp->MinFilter);
155 sampler->min_mip_filter = gl_filter_to_mip_filter(msamp->MinFilter);
156 sampler->mag_img_filter = gl_filter_to_img_filter(msamp->MagFilter);
158 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
159 sampler->normalized_coords = 1;
161 sampler->lod_bias = ctx->Texture.Unit[texUnit].LodBias + msamp->LodBias;
163 sampler->min_lod = CLAMP(msamp->MinLod,
165 (GLfloat) texobj->MaxLevel - texobj->BaseLevel);
166 sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel - texobj->BaseLevel,
168 if (sampler->max_lod < sampler->min_lod) {
169 /* The GL spec doesn't seem to specify what to do in this case.
172 float tmp = sampler->max_lod;
173 sampler->max_lod = sampler->min_lod;
174 sampler->min_lod = tmp;
175 assert(sampler->min_lod <= sampler->max_lod);
178 /* For non-black borders... */
179 if (msamp->BorderColor.ui[0] ||
180 msamp->BorderColor.ui[1] ||
181 msamp->BorderColor.ui[2] ||
182 msamp->BorderColor.ui[3]) {
183 const struct st_texture_object *stobj = st_texture_object_const(texobj);
184 const GLboolean is_integer = texobj->_IsIntegerFormat;
185 union pipe_color_union border_color;
187 if (st->apply_texture_swizzle_to_border_color && stobj->sampler_view) {
188 const unsigned char swz[4] =
190 stobj->sampler_view->swizzle_r,
191 stobj->sampler_view->swizzle_g,
192 stobj->sampler_view->swizzle_b,
193 stobj->sampler_view->swizzle_a,
196 st_translate_color(&msamp->BorderColor,
198 texBaseFormat, is_integer);
200 util_format_apply_color_swizzle(&sampler->border_color,
201 &border_color, swz, is_integer);
203 st_translate_color(&msamp->BorderColor,
204 &sampler->border_color,
205 texBaseFormat, is_integer);
209 sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
210 0 : (GLuint) msamp->MaxAnisotropy);
212 /* If sampling a depth texture and using shadow comparison */
213 if ((texBaseFormat == GL_DEPTH_COMPONENT ||
214 texBaseFormat == GL_DEPTH_STENCIL) &&
215 msamp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
216 sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
217 sampler->compare_func = st_compare_func_to_pipe(msamp->CompareFunc);
220 sampler->seamless_cube_map =
221 ctx->Texture.CubeMapSeamless || msamp->CubeMapSeamless;
226 * Update the gallium driver's sampler state for fragment, vertex or
227 * geometry shader stage.
230 update_shader_samplers(struct st_context *st,
231 unsigned shader_stage,
232 const struct gl_program *prog,
234 struct pipe_sampler_state *samplers,
235 unsigned *num_samplers)
238 GLbitfield samplers_used;
239 const GLuint old_max = *num_samplers;
241 samplers_used = prog->SamplersUsed;
243 if (*num_samplers == 0 && samplers_used == 0x0)
248 /* loop over sampler units (aka tex image units) */
249 for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
250 struct pipe_sampler_state *sampler = samplers + unit;
252 if (samplers_used & 1) {
253 const GLuint texUnit = prog->SamplerUnits[unit];
255 convert_sampler(st, sampler, texUnit);
257 *num_samplers = unit + 1;
259 cso_single_sampler(st->cso_context, shader_stage, unit, sampler);
261 else if (samplers_used != 0 || unit < old_max) {
262 cso_single_sampler(st->cso_context, shader_stage, unit, NULL);
265 /* if we've reset all the old samplers and we have no more new ones */
270 cso_single_sampler_done(st->cso_context, shader_stage);
275 update_samplers(struct st_context *st)
277 const struct gl_context *ctx = st->ctx;
279 update_shader_samplers(st,
280 PIPE_SHADER_FRAGMENT,
281 &ctx->FragmentProgram._Current->Base,
282 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
283 st->state.samplers[PIPE_SHADER_FRAGMENT],
284 &st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
286 update_shader_samplers(st,
288 &ctx->VertexProgram._Current->Base,
289 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
290 st->state.samplers[PIPE_SHADER_VERTEX],
291 &st->state.num_samplers[PIPE_SHADER_VERTEX]);
293 if (ctx->GeometryProgram._Current) {
294 update_shader_samplers(st,
295 PIPE_SHADER_GEOMETRY,
296 &ctx->GeometryProgram._Current->Base,
297 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits,
298 st->state.samplers[PIPE_SHADER_GEOMETRY],
299 &st->state.num_samplers[PIPE_SHADER_GEOMETRY]);
304 const struct st_tracked_state st_update_sampler = {
305 "st_update_sampler", /* name */
307 _NEW_TEXTURE, /* mesa */
310 update_samplers /* update */