1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/samplerobj.h"
38 #include "main/texobj.h"
40 #include "st_context.h"
41 #include "st_cb_texture.h"
42 #include "st_format.h"
44 #include "st_texture.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
48 #include "cso_cache/cso_context.h"
52 * Convert GLenum texcoord wrap tokens to pipe tokens.
55 gl_wrap_xlate(GLenum wrap
)
59 return PIPE_TEX_WRAP_REPEAT
;
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
;
82 gl_filter_to_mip_filter(GLenum filter
)
87 return PIPE_TEX_MIPFILTER_NONE
;
89 case GL_NEAREST_MIPMAP_NEAREST
:
90 case GL_LINEAR_MIPMAP_NEAREST
:
91 return PIPE_TEX_MIPFILTER_NEAREST
;
93 case GL_NEAREST_MIPMAP_LINEAR
:
94 case GL_LINEAR_MIPMAP_LINEAR
:
95 return PIPE_TEX_MIPFILTER_LINEAR
;
99 return PIPE_TEX_MIPFILTER_NONE
;
105 gl_filter_to_img_filter(GLenum filter
)
109 case GL_NEAREST_MIPMAP_NEAREST
:
110 case GL_NEAREST_MIPMAP_LINEAR
:
111 return PIPE_TEX_FILTER_NEAREST
;
114 case GL_LINEAR_MIPMAP_NEAREST
:
115 case GL_LINEAR_MIPMAP_LINEAR
:
116 return PIPE_TEX_FILTER_LINEAR
;
120 return PIPE_TEX_FILTER_NEAREST
;
126 convert_sampler(struct st_context
*st
,
127 struct pipe_sampler_state
*sampler
,
130 struct gl_texture_object
*texobj
;
131 struct gl_context
*ctx
= st
->ctx
;
132 struct gl_sampler_object
*msamp
;
134 texobj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
136 texobj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
139 msamp
= _mesa_get_samplerobj(ctx
, texUnit
);
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
);
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
);
150 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
151 sampler
->normalized_coords
= 1;
153 sampler
->lod_bias
= ctx
->Texture
.Unit
[texUnit
].LodBias
+ msamp
->LodBias
;
155 sampler
->min_lod
= CLAMP(msamp
->MinLod
,
157 (GLfloat
) texobj
->MaxLevel
- texobj
->BaseLevel
);
158 sampler
->max_lod
= MIN2((GLfloat
) texobj
->MaxLevel
- texobj
->BaseLevel
,
160 if (sampler
->max_lod
< sampler
->min_lod
) {
161 /* The GL spec doesn't seem to specify what to do in this case.
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
);
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
;
176 teximg
= texobj
->Image
[0][texobj
->BaseLevel
];
178 st_translate_color(msamp
->BorderColor
.f
,
179 teximg
? teximg
->_BaseFormat
: GL_RGBA
,
180 sampler
->border_color
.f
);
183 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
184 0 : (GLuint
) msamp
->MaxAnisotropy
);
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
);
193 sampler
->seamless_cube_map
=
194 ctx
->Texture
.CubeMapSeamless
|| msamp
->CubeMapSeamless
;
199 update_vertex_samplers(struct st_context
*st
)
201 const struct gl_context
*ctx
= st
->ctx
;
202 struct gl_vertex_program
*vprog
= ctx
->VertexProgram
._Current
;
205 if (st
->state
.num_vertex_samplers
== 0 && vprog
->Base
.SamplersUsed
== 0)
207 st
->state
.num_vertex_samplers
= 0;
209 /* loop over sampler units (aka tex image units) */
210 for (su
= 0; su
< ctx
->Const
.MaxVertexTextureImageUnits
; su
++) {
211 struct pipe_sampler_state
*sampler
= st
->state
.vertex_samplers
+ su
;
213 if (vprog
->Base
.SamplersUsed
& (1 << su
)) {
216 texUnit
= vprog
->Base
.SamplerUnits
[su
];
218 convert_sampler(st
, sampler
, texUnit
);
220 st
->state
.num_vertex_samplers
= su
+ 1;
222 cso_single_sampler(st
->cso_context
, PIPE_SHADER_VERTEX
, su
, sampler
);
224 cso_single_sampler(st
->cso_context
, PIPE_SHADER_VERTEX
, su
, NULL
);
227 cso_single_sampler_done(st
->cso_context
, PIPE_SHADER_VERTEX
);
232 update_fragment_samplers(struct st_context
*st
)
234 const struct gl_context
*ctx
= st
->ctx
;
235 struct gl_fragment_program
*fprog
= ctx
->FragmentProgram
._Current
;
237 GLuint samplers_used
= fprog
->Base
.SamplersUsed
;
238 GLuint old_max
= st
->state
.num_samplers
;
240 st
->state
.num_samplers
= 0;
242 /* loop over sampler units (aka tex image units) */
243 for (su
= 0; su
< ctx
->Const
.MaxTextureImageUnits
; su
++, samplers_used
>>= 1) {
244 struct pipe_sampler_state
*sampler
= st
->state
.samplers
+ su
;
246 if (samplers_used
& 1) {
249 texUnit
= fprog
->Base
.SamplerUnits
[su
];
251 convert_sampler(st
, sampler
, texUnit
);
253 st
->state
.num_samplers
= su
+ 1;
255 /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
256 cso_single_sampler(st
->cso_context
, PIPE_SHADER_FRAGMENT
, su
, sampler
);
258 else if (samplers_used
!= 0 || su
< old_max
) {
259 /*printf("%s su=%u null\n", __FUNCTION__, su);*/
260 cso_single_sampler(st
->cso_context
, PIPE_SHADER_FRAGMENT
, su
, NULL
);
262 /* if we've reset all the old views and we have no more new ones */
267 cso_single_sampler_done(st
->cso_context
, PIPE_SHADER_FRAGMENT
);
272 update_samplers(struct st_context
*st
)
274 update_fragment_samplers(st
);
275 update_vertex_samplers(st
);
279 const struct st_tracked_state st_update_sampler
= {
280 "st_update_sampler", /* name */
282 _NEW_TEXTURE
, /* mesa */
285 update_samplers
/* update */