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/teximage.h"
40 #include "main/texobj.h"
42 #include "st_context.h"
43 #include "st_cb_texture.h"
44 #include "st_format.h"
46 #include "st_texture.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
50 #include "cso_cache/cso_context.h"
52 #include "util/u_format.h"
56 * Convert GLenum texcoord wrap tokens to pipe tokens.
59 gl_wrap_xlate(GLenum wrap
)
63 return PIPE_TEX_WRAP_REPEAT
;
65 return PIPE_TEX_WRAP_CLAMP
;
66 case GL_CLAMP_TO_EDGE
:
67 return PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
68 case GL_CLAMP_TO_BORDER
:
69 return PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
70 case GL_MIRRORED_REPEAT
:
71 return PIPE_TEX_WRAP_MIRROR_REPEAT
;
72 case GL_MIRROR_CLAMP_EXT
:
73 return PIPE_TEX_WRAP_MIRROR_CLAMP
;
74 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
75 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
76 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
77 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
;
86 gl_filter_to_mip_filter(GLenum filter
)
91 return PIPE_TEX_MIPFILTER_NONE
;
93 case GL_NEAREST_MIPMAP_NEAREST
:
94 case GL_LINEAR_MIPMAP_NEAREST
:
95 return PIPE_TEX_MIPFILTER_NEAREST
;
97 case GL_NEAREST_MIPMAP_LINEAR
:
98 case GL_LINEAR_MIPMAP_LINEAR
:
99 return PIPE_TEX_MIPFILTER_LINEAR
;
103 return PIPE_TEX_MIPFILTER_NONE
;
109 gl_filter_to_img_filter(GLenum filter
)
113 case GL_NEAREST_MIPMAP_NEAREST
:
114 case GL_NEAREST_MIPMAP_LINEAR
:
115 return PIPE_TEX_FILTER_NEAREST
;
118 case GL_LINEAR_MIPMAP_NEAREST
:
119 case GL_LINEAR_MIPMAP_LINEAR
:
120 return PIPE_TEX_FILTER_LINEAR
;
124 return PIPE_TEX_FILTER_NEAREST
;
130 convert_sampler(struct st_context
*st
,
131 struct pipe_sampler_state
*sampler
,
134 const struct gl_texture_object
*texobj
;
135 struct gl_context
*ctx
= st
->ctx
;
136 struct gl_sampler_object
*msamp
;
137 GLenum texBaseFormat
;
139 texobj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
141 texobj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
144 texBaseFormat
= _mesa_texture_base_format(texobj
);
146 msamp
= _mesa_get_samplerobj(ctx
, texUnit
);
148 memset(sampler
, 0, sizeof(*sampler
));
149 sampler
->wrap_s
= gl_wrap_xlate(msamp
->WrapS
);
150 sampler
->wrap_t
= gl_wrap_xlate(msamp
->WrapT
);
151 sampler
->wrap_r
= gl_wrap_xlate(msamp
->WrapR
);
153 sampler
->min_img_filter
= gl_filter_to_img_filter(msamp
->MinFilter
);
154 sampler
->min_mip_filter
= gl_filter_to_mip_filter(msamp
->MinFilter
);
155 sampler
->mag_img_filter
= gl_filter_to_img_filter(msamp
->MagFilter
);
157 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
158 sampler
->normalized_coords
= 1;
160 sampler
->lod_bias
= ctx
->Texture
.Unit
[texUnit
].LodBias
+ msamp
->LodBias
;
162 sampler
->min_lod
= MAX2(msamp
->MinLod
, 0.0f
);
163 sampler
->max_lod
= msamp
->MaxLod
;
164 if (sampler
->max_lod
< sampler
->min_lod
) {
165 /* The GL spec doesn't seem to specify what to do in this case.
168 float tmp
= sampler
->max_lod
;
169 sampler
->max_lod
= sampler
->min_lod
;
170 sampler
->min_lod
= tmp
;
171 assert(sampler
->min_lod
<= sampler
->max_lod
);
174 /* For non-black borders... */
175 if (msamp
->BorderColor
.ui
[0] ||
176 msamp
->BorderColor
.ui
[1] ||
177 msamp
->BorderColor
.ui
[2] ||
178 msamp
->BorderColor
.ui
[3]) {
179 const struct st_texture_object
*stobj
= st_texture_object_const(texobj
);
180 const GLboolean is_integer
= texobj
->_IsIntegerFormat
;
181 const struct pipe_sampler_view
*sv
= NULL
;
182 union pipe_color_union border_color
;
185 /* Just search for the first used view. We can do this because the
186 swizzle is per-texture, not per context. */
187 /* XXX: clean that up to not use the sampler view at all */
188 for (i
= 0; i
< stobj
->num_sampler_views
; ++i
) {
189 if (stobj
->sampler_views
[i
]) {
190 sv
= stobj
->sampler_views
[i
];
195 if (st
->apply_texture_swizzle_to_border_color
&& sv
) {
196 const unsigned char swz
[4] =
204 st_translate_color(&msamp
->BorderColor
,
206 texBaseFormat
, is_integer
);
208 util_format_apply_color_swizzle(&sampler
->border_color
,
209 &border_color
, swz
, is_integer
);
211 st_translate_color(&msamp
->BorderColor
,
212 &sampler
->border_color
,
213 texBaseFormat
, is_integer
);
217 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
218 0 : (GLuint
) msamp
->MaxAnisotropy
);
220 /* If sampling a depth texture and using shadow comparison */
221 if ((texBaseFormat
== GL_DEPTH_COMPONENT
||
222 texBaseFormat
== GL_DEPTH_STENCIL
) &&
223 msamp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
224 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
225 sampler
->compare_func
= st_compare_func_to_pipe(msamp
->CompareFunc
);
228 sampler
->seamless_cube_map
=
229 ctx
->Texture
.CubeMapSeamless
|| msamp
->CubeMapSeamless
;
234 * Update the gallium driver's sampler state for fragment, vertex or
235 * geometry shader stage.
238 update_shader_samplers(struct st_context
*st
,
239 unsigned shader_stage
,
240 const struct gl_program
*prog
,
242 struct pipe_sampler_state
*samplers
,
243 unsigned *num_samplers
)
246 GLbitfield samplers_used
;
247 const GLuint old_max
= *num_samplers
;
248 const struct pipe_sampler_state
*states
[PIPE_MAX_SAMPLERS
];
250 samplers_used
= prog
->SamplersUsed
;
252 if (*num_samplers
== 0 && samplers_used
== 0x0)
257 /* loop over sampler units (aka tex image units) */
258 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
259 struct pipe_sampler_state
*sampler
= samplers
+ unit
;
261 if (samplers_used
& 1) {
262 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
264 convert_sampler(st
, sampler
, texUnit
);
265 states
[unit
] = sampler
;
266 *num_samplers
= unit
+ 1;
268 else if (samplers_used
!= 0 || unit
< old_max
) {
272 /* if we've reset all the old samplers and we have no more new ones */
277 cso_set_samplers(st
->cso_context
, shader_stage
, *num_samplers
, states
);
282 update_samplers(struct st_context
*st
)
284 const struct gl_context
*ctx
= st
->ctx
;
286 update_shader_samplers(st
,
287 PIPE_SHADER_FRAGMENT
,
288 &ctx
->FragmentProgram
._Current
->Base
,
289 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
290 st
->state
.samplers
[PIPE_SHADER_FRAGMENT
],
291 &st
->state
.num_samplers
[PIPE_SHADER_FRAGMENT
]);
293 update_shader_samplers(st
,
295 &ctx
->VertexProgram
._Current
->Base
,
296 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
297 st
->state
.samplers
[PIPE_SHADER_VERTEX
],
298 &st
->state
.num_samplers
[PIPE_SHADER_VERTEX
]);
300 if (ctx
->GeometryProgram
._Current
) {
301 update_shader_samplers(st
,
302 PIPE_SHADER_GEOMETRY
,
303 &ctx
->GeometryProgram
._Current
->Base
,
304 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
305 st
->state
.samplers
[PIPE_SHADER_GEOMETRY
],
306 &st
->state
.num_samplers
[PIPE_SHADER_GEOMETRY
]);
308 if (ctx
->TessCtrlProgram
._Current
) {
309 update_shader_samplers(st
,
310 PIPE_SHADER_TESS_CTRL
,
311 &ctx
->TessCtrlProgram
._Current
->Base
,
312 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
313 st
->state
.samplers
[PIPE_SHADER_TESS_CTRL
],
314 &st
->state
.num_samplers
[PIPE_SHADER_TESS_CTRL
]);
316 if (ctx
->TessEvalProgram
._Current
) {
317 update_shader_samplers(st
,
318 PIPE_SHADER_TESS_EVAL
,
319 &ctx
->TessEvalProgram
._Current
->Base
,
320 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
321 st
->state
.samplers
[PIPE_SHADER_TESS_EVAL
],
322 &st
->state
.num_samplers
[PIPE_SHADER_TESS_EVAL
]);
324 if (ctx
->ComputeProgram
._Current
) {
325 update_shader_samplers(st
,
327 &ctx
->ComputeProgram
._Current
->Base
,
328 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
329 st
->state
.samplers
[PIPE_SHADER_COMPUTE
],
330 &st
->state
.num_samplers
[PIPE_SHADER_COMPUTE
]);
335 const struct st_tracked_state st_update_sampler
= {
336 "st_update_sampler", /* name */
338 _NEW_TEXTURE
, /* mesa */
341 update_samplers
/* update */