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"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
43 #include "cso_cache/cso_context.h"
47 * Convert GLenum texcoord wrap tokens to pipe tokens.
50 gl_wrap_xlate(GLenum wrap
)
54 return PIPE_TEX_WRAP_REPEAT
;
56 return PIPE_TEX_WRAP_CLAMP
;
57 case GL_CLAMP_TO_EDGE
:
58 return PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
59 case GL_CLAMP_TO_BORDER
:
60 return PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
61 case GL_MIRRORED_REPEAT
:
62 return PIPE_TEX_WRAP_MIRROR_REPEAT
;
63 case GL_MIRROR_CLAMP_EXT
:
64 return PIPE_TEX_WRAP_MIRROR_CLAMP
;
65 case GL_MIRROR_CLAMP_TO_EDGE_EXT
:
66 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
67 case GL_MIRROR_CLAMP_TO_BORDER_EXT
:
68 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
;
77 gl_filter_to_mip_filter(GLenum filter
)
82 return PIPE_TEX_MIPFILTER_NONE
;
84 case GL_NEAREST_MIPMAP_NEAREST
:
85 case GL_LINEAR_MIPMAP_NEAREST
:
86 return PIPE_TEX_MIPFILTER_NEAREST
;
88 case GL_NEAREST_MIPMAP_LINEAR
:
89 case GL_LINEAR_MIPMAP_LINEAR
:
90 return PIPE_TEX_MIPFILTER_LINEAR
;
94 return PIPE_TEX_MIPFILTER_NONE
;
100 gl_filter_to_img_filter(GLenum filter
)
104 case GL_NEAREST_MIPMAP_NEAREST
:
105 case GL_NEAREST_MIPMAP_LINEAR
:
106 return PIPE_TEX_FILTER_NEAREST
;
109 case GL_LINEAR_MIPMAP_NEAREST
:
110 case GL_LINEAR_MIPMAP_LINEAR
:
111 return PIPE_TEX_FILTER_LINEAR
;
115 return PIPE_TEX_FILTER_NEAREST
;
121 xlate_border_color(const GLfloat
*colorIn
, GLenum baseFormat
, GLfloat
*colorOut
)
123 switch (baseFormat
) {
125 colorOut
[0] = colorIn
[0];
126 colorOut
[1] = colorIn
[1];
127 colorOut
[2] = colorIn
[2];
131 colorOut
[0] = colorOut
[1] = colorOut
[2] = 0.0;
132 colorOut
[3] = colorIn
[3];
135 colorOut
[0] = colorOut
[1] = colorOut
[2] = colorIn
[0];
138 case GL_LUMINANCE_ALPHA
:
139 colorOut
[0] = colorOut
[1] = colorOut
[2] = colorIn
[0];
140 colorOut
[3] = colorIn
[3];
143 colorOut
[0] = colorOut
[1] = colorOut
[2] = colorOut
[3] = colorIn
[0];
146 COPY_4V(colorOut
, colorIn
);
152 update_samplers(struct st_context
*st
)
154 struct gl_vertex_program
*vprog
= st
->ctx
->VertexProgram
._Current
;
155 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
156 const GLbitfield samplersUsed
= (vprog
->Base
.SamplersUsed
|
157 fprog
->Base
.SamplersUsed
);
160 st
->state
.num_samplers
= 0;
162 /* loop over sampler units (aka tex image units) */
163 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureImageUnits
; su
++) {
164 struct pipe_sampler_state
*sampler
= st
->state
.samplers
+ su
;
166 memset(sampler
, 0, sizeof(*sampler
));
168 if (samplersUsed
& (1 << su
)) {
169 struct gl_texture_object
*texobj
;
170 struct gl_texture_image
*teximg
;
173 if (fprog
->Base
.SamplersUsed
& (1 << su
))
174 texUnit
= fprog
->Base
.SamplerUnits
[su
];
176 texUnit
= vprog
->Base
.SamplerUnits
[su
];
178 texobj
= st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
180 texobj
= st_get_default_texture(st
);
183 teximg
= texobj
->Image
[0][texobj
->BaseLevel
];
185 sampler
->wrap_s
= gl_wrap_xlate(texobj
->WrapS
);
186 sampler
->wrap_t
= gl_wrap_xlate(texobj
->WrapT
);
187 sampler
->wrap_r
= gl_wrap_xlate(texobj
->WrapR
);
189 sampler
->min_img_filter
= gl_filter_to_img_filter(texobj
->MinFilter
);
190 sampler
->min_mip_filter
= gl_filter_to_mip_filter(texobj
->MinFilter
);
191 sampler
->mag_img_filter
= gl_filter_to_img_filter(texobj
->MagFilter
);
193 if (texobj
->Target
!= GL_TEXTURE_RECTANGLE_ARB
)
194 sampler
->normalized_coords
= 1;
196 sampler
->lod_bias
= st
->ctx
->Texture
.Unit
[su
].LodBias
;
197 sampler
->min_lod
= MAX2(0.0f
, texobj
->MinLod
);
198 sampler
->max_lod
= MIN2(texobj
->MaxLevel
- texobj
->BaseLevel
,
200 if (sampler
->max_lod
< sampler
->min_lod
) {
201 /* The GL spec doesn't seem to specify what to do in this case.
204 float tmp
= sampler
->max_lod
;
205 sampler
->max_lod
= sampler
->min_lod
;
206 sampler
->min_lod
= tmp
;
207 assert(sampler
->min_lod
<= sampler
->max_lod
);
210 xlate_border_color(texobj
->BorderColor
.f
,
211 teximg
? teximg
->_BaseFormat
: GL_RGBA
,
212 sampler
->border_color
);
214 sampler
->max_anisotropy
= texobj
->MaxAnisotropy
;
216 /* only care about ARB_shadow, not SGI shadow */
217 if (texobj
->CompareMode
== GL_COMPARE_R_TO_TEXTURE
) {
218 sampler
->compare_mode
= PIPE_TEX_COMPARE_R_TO_TEXTURE
;
219 sampler
->compare_func
220 = st_compare_func_to_pipe(texobj
->CompareFunc
);
223 st
->state
.num_samplers
= su
+ 1;
225 /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
226 cso_single_sampler(st
->cso_context
, su
, sampler
);
227 if (su
< st
->ctx
->Const
.MaxVertexTextureImageUnits
) {
228 cso_single_vertex_sampler(st
->cso_context
, su
, sampler
);
232 /*printf("%s su=%u null\n", __FUNCTION__, su);*/
233 cso_single_sampler(st
->cso_context
, su
, NULL
);
234 if (su
< st
->ctx
->Const
.MaxVertexTextureImageUnits
) {
235 cso_single_vertex_sampler(st
->cso_context
, su
, NULL
);
240 cso_single_sampler_done(st
->cso_context
);
241 if (st
->ctx
->Const
.MaxVertexTextureImageUnits
> 0) {
242 cso_single_vertex_sampler_done(st
->cso_context
);
247 const struct st_tracked_state st_update_sampler
= {
248 "st_update_sampler", /* name */
250 _NEW_TEXTURE
, /* mesa */
253 update_samplers
/* update */