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"
37 #include "st_context.h"
38 #include "st_cb_texture.h"
39 #include "st_format.h"
41 #include "st_texture.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
45 #include "cso_cache/cso_context.h"
49 * Convert GLenum texcoord wrap tokens to pipe tokens.
52 gl_wrap_xlate(GLenum wrap
)
56 return PIPE_TEX_WRAP_REPEAT
;
58 return PIPE_TEX_WRAP_CLAMP
;
59 case GL_CLAMP_TO_EDGE
:
60 return PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
61 case GL_CLAMP_TO_BORDER
:
62 return PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
63 case GL_MIRRORED_REPEAT
:
64 return PIPE_TEX_WRAP_MIRROR_REPEAT
;
65 case GL_MIRROR_CLAMP_EXT
:
66 return PIPE_TEX_WRAP_MIRROR_CLAMP
;
67 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
68 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
69 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
70 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
;
79 gl_filter_to_mip_filter(GLenum filter
)
84 return PIPE_TEX_MIPFILTER_NONE
;
86 case GL_NEAREST_MIPMAP_NEAREST
:
87 case GL_LINEAR_MIPMAP_NEAREST
:
88 return PIPE_TEX_MIPFILTER_NEAREST
;
90 case GL_NEAREST_MIPMAP_LINEAR
:
91 case GL_LINEAR_MIPMAP_LINEAR
:
92 return PIPE_TEX_MIPFILTER_LINEAR
;
96 return PIPE_TEX_MIPFILTER_NONE
;
102 gl_filter_to_img_filter(GLenum filter
)
106 case GL_NEAREST_MIPMAP_NEAREST
:
107 case GL_NEAREST_MIPMAP_LINEAR
:
108 return PIPE_TEX_FILTER_NEAREST
;
111 case GL_LINEAR_MIPMAP_NEAREST
:
112 case GL_LINEAR_MIPMAP_LINEAR
:
113 return PIPE_TEX_FILTER_LINEAR
;
117 return PIPE_TEX_FILTER_NEAREST
;
123 update_samplers(struct st_context
*st
)
125 struct gl_vertex_program
*vprog
= st
->ctx
->VertexProgram
._Current
;
126 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
127 const GLbitfield samplersUsed
= (vprog
->Base
.SamplersUsed
|
128 fprog
->Base
.SamplersUsed
);
131 st
->state
.num_samplers
= 0;
133 /* loop over sampler units (aka tex image units) */
134 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureImageUnits
; su
++) {
135 struct pipe_sampler_state
*sampler
= st
->state
.samplers
+ su
;
137 memset(sampler
, 0, sizeof(*sampler
));
139 if (samplersUsed
& (1 << su
)) {
140 struct gl_texture_object
*texobj
;
141 struct gl_texture_image
*teximg
;
142 struct gl_sampler_object
*msamp
;
145 if (fprog
->Base
.SamplersUsed
& (1 << su
))
146 texUnit
= fprog
->Base
.SamplerUnits
[su
];
148 texUnit
= vprog
->Base
.SamplerUnits
[su
];
150 texobj
= st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
152 texobj
= st_get_default_texture(st
);
155 teximg
= texobj
->Image
[0][texobj
->BaseLevel
];
157 msamp
= st_get_mesa_sampler(st
->ctx
, texUnit
);
159 sampler
->wrap_s
= gl_wrap_xlate(msamp
->WrapS
);
160 sampler
->wrap_t
= gl_wrap_xlate(msamp
->WrapT
);
161 sampler
->wrap_r
= gl_wrap_xlate(msamp
->WrapR
);
163 sampler
->min_img_filter
= gl_filter_to_img_filter(msamp
->MinFilter
);
164 sampler
->min_mip_filter
= gl_filter_to_mip_filter(msamp
->MinFilter
);
165 sampler
->mag_img_filter
= gl_filter_to_img_filter(msamp
->MagFilter
);
167 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
168 sampler
->normalized_coords
= 1;
170 sampler
->lod_bias
= st
->ctx
->Texture
.Unit
[texUnit
].LodBias
+
173 sampler
->min_lod
= CLAMP(msamp
->MinLod
,
175 (GLfloat
) texobj
->MaxLevel
- texobj
->BaseLevel
);
176 sampler
->max_lod
= MIN2((GLfloat
) texobj
->MaxLevel
- texobj
->BaseLevel
,
178 if (sampler
->max_lod
< sampler
->min_lod
) {
179 /* The GL spec doesn't seem to specify what to do in this case.
182 float tmp
= sampler
->max_lod
;
183 sampler
->max_lod
= sampler
->min_lod
;
184 sampler
->min_lod
= tmp
;
185 assert(sampler
->min_lod
<= sampler
->max_lod
);
188 st_translate_color(msamp
->BorderColor
.f
,
189 teximg
? teximg
->_BaseFormat
: GL_RGBA
,
190 sampler
->border_color
);
192 sampler
->max_anisotropy
= (msamp
->MaxAnisotropy
== 1.0 ?
193 0 : (GLuint
) msamp
->MaxAnisotropy
);
195 /* only care about ARB_shadow, not SGI shadow */
196 if (msamp
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
197 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
198 sampler
->compare_func
199 = st_compare_func_to_pipe(msamp
->CompareFunc
);
202 st
->state
.num_samplers
= su
+ 1;
204 /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
205 cso_single_sampler(st
->cso_context
, su
, sampler
);
206 if (su
< st
->ctx
->Const
.MaxVertexTextureImageUnits
) {
207 cso_single_vertex_sampler(st
->cso_context
, su
, sampler
);
211 /*printf("%s su=%u null\n", __FUNCTION__, su);*/
212 cso_single_sampler(st
->cso_context
, su
, NULL
);
213 if (su
< st
->ctx
->Const
.MaxVertexTextureImageUnits
) {
214 cso_single_vertex_sampler(st
->cso_context
, su
, NULL
);
219 cso_single_sampler_done(st
->cso_context
);
220 if (st
->ctx
->Const
.MaxVertexTextureImageUnits
> 0) {
221 cso_single_vertex_sampler_done(st
->cso_context
);
226 const struct st_tracked_state st_update_sampler
= {
227 "st_update_sampler", /* name */
229 _NEW_TEXTURE
, /* mesa */
232 update_samplers
/* update */