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"
39 #include "st_context.h"
40 #include "st_cb_texture.h"
41 #include "st_format.h"
43 #include "st_texture.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
47 #include "cso_cache/cso_context.h"
51 * Convert GLenum texcoord wrap tokens to pipe tokens.
54 gl_wrap_xlate(GLenum wrap
)
58 return PIPE_TEX_WRAP_REPEAT
;
60 return PIPE_TEX_WRAP_CLAMP
;
61 case GL_CLAMP_TO_EDGE
:
62 return PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
63 case GL_CLAMP_TO_BORDER
:
64 return PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
65 case GL_MIRRORED_REPEAT
:
66 return PIPE_TEX_WRAP_MIRROR_REPEAT
;
67 case GL_MIRROR_CLAMP_EXT
:
68 return PIPE_TEX_WRAP_MIRROR_CLAMP
;
69 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
70 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
71 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
72 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
;
81 gl_filter_to_mip_filter(GLenum filter
)
86 return PIPE_TEX_MIPFILTER_NONE
;
88 case GL_NEAREST_MIPMAP_NEAREST
:
89 case GL_LINEAR_MIPMAP_NEAREST
:
90 return PIPE_TEX_MIPFILTER_NEAREST
;
92 case GL_NEAREST_MIPMAP_LINEAR
:
93 case GL_LINEAR_MIPMAP_LINEAR
:
94 return PIPE_TEX_MIPFILTER_LINEAR
;
98 return PIPE_TEX_MIPFILTER_NONE
;
104 gl_filter_to_img_filter(GLenum filter
)
108 case GL_NEAREST_MIPMAP_NEAREST
:
109 case GL_NEAREST_MIPMAP_LINEAR
:
110 return PIPE_TEX_FILTER_NEAREST
;
113 case GL_LINEAR_MIPMAP_NEAREST
:
114 case GL_LINEAR_MIPMAP_LINEAR
:
115 return PIPE_TEX_FILTER_LINEAR
;
119 return PIPE_TEX_FILTER_NEAREST
;
123 static void convert_sampler(struct st_context
*st
,
124 struct pipe_sampler_state
*sampler
,
127 struct gl_texture_object
*texobj
;
128 struct gl_sampler_object
*msamp
;
130 texobj
= st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
132 texobj
= st_get_default_texture(st
);
135 msamp
= _mesa_get_samplerobj(st
->ctx
, texUnit
);
136 sampler
->wrap_s
= gl_wrap_xlate(msamp
->WrapS
);
137 sampler
->wrap_t
= gl_wrap_xlate(msamp
->WrapT
);
138 sampler
->wrap_r
= gl_wrap_xlate(msamp
->WrapR
);
140 sampler
->min_img_filter
= gl_filter_to_img_filter(msamp
->MinFilter
);
141 sampler
->min_mip_filter
= gl_filter_to_mip_filter(msamp
->MinFilter
);
142 sampler
->mag_img_filter
= gl_filter_to_img_filter(msamp
->MagFilter
);
144 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
145 sampler
->normalized_coords
= 1;
147 sampler
->lod_bias
= st
->ctx
->Texture
.Unit
[texUnit
].LodBias
+
150 sampler
->min_lod
= CLAMP(msamp
->MinLod
,
152 (GLfloat
) texobj
->MaxLevel
- texobj
->BaseLevel
);
153 sampler
->max_lod
= MIN2((GLfloat
) texobj
->MaxLevel
- texobj
->BaseLevel
,
155 if (sampler
->max_lod
< sampler
->min_lod
) {
156 /* The GL spec doesn't seem to specify what to do in this case.
159 float tmp
= sampler
->max_lod
;
160 sampler
->max_lod
= sampler
->min_lod
;
161 sampler
->min_lod
= tmp
;
162 assert(sampler
->min_lod
<= sampler
->max_lod
);
165 if (msamp
->BorderColor
.ui
[0] ||
166 msamp
->BorderColor
.ui
[1] ||
167 msamp
->BorderColor
.ui
[2] ||
168 msamp
->BorderColor
.ui
[3]) {
169 struct gl_texture_image
*teximg
;
171 teximg
= texobj
->Image
[0][texobj
->BaseLevel
];
173 st_translate_color(msamp
->BorderColor
.f
,
174 teximg
? teximg
->_BaseFormat
: GL_RGBA
,
175 sampler
->border_color
);
178 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
179 0 : (GLuint
) msamp
->MaxAnisotropy
);
181 /* only care about ARB_shadow, not SGI shadow */
182 if (msamp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
183 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
184 sampler
->compare_func
185 = st_compare_func_to_pipe(msamp
->CompareFunc
);
188 sampler
->seamless_cube_map
=
189 st
->ctx
->Texture
.CubeMapSeamless
|| msamp
->CubeMapSeamless
;
193 update_vertex_samplers(struct st_context
*st
)
195 struct gl_vertex_program
*vprog
= st
->ctx
->VertexProgram
._Current
;
198 st
->state
.num_vertex_samplers
= 0;
200 /* loop over sampler units (aka tex image units) */
201 for (su
= 0; su
< st
->ctx
->Const
.MaxVertexTextureImageUnits
; su
++) {
202 struct pipe_sampler_state
*sampler
= st
->state
.vertex_samplers
+ su
;
204 memset(sampler
, 0, sizeof(*sampler
));
206 if (vprog
->Base
.SamplersUsed
& (1 << su
)) {
209 texUnit
= vprog
->Base
.SamplerUnits
[su
];
211 convert_sampler(st
, sampler
, texUnit
);
213 st
->state
.num_vertex_samplers
= su
+ 1;
215 cso_single_vertex_sampler(st
->cso_context
, su
, sampler
);
217 cso_single_vertex_sampler(st
->cso_context
, su
, NULL
);
220 cso_single_vertex_sampler_done(st
->cso_context
);
224 update_fragment_samplers(struct st_context
*st
)
226 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
229 st
->state
.num_samplers
= 0;
231 /* loop over sampler units (aka tex image units) */
232 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureImageUnits
; su
++) {
233 struct pipe_sampler_state
*sampler
= st
->state
.samplers
+ su
;
235 memset(sampler
, 0, sizeof(*sampler
));
237 if (fprog
->Base
.SamplersUsed
& (1 << su
)) {
240 texUnit
= fprog
->Base
.SamplerUnits
[su
];
242 convert_sampler(st
, sampler
, texUnit
);
244 st
->state
.num_samplers
= su
+ 1;
246 /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
247 cso_single_sampler(st
->cso_context
, su
, sampler
);
250 /*printf("%s su=%u null\n", __FUNCTION__, su);*/
251 cso_single_sampler(st
->cso_context
, su
, NULL
);
255 cso_single_sampler_done(st
->cso_context
);
259 update_samplers(struct st_context
*st
)
261 update_fragment_samplers(st
);
262 update_vertex_samplers(st
);
265 const struct st_tracked_state st_update_sampler
= {
266 "st_update_sampler", /* name */
268 _NEW_TEXTURE
, /* mesa */
271 update_samplers
/* update */