56da010b3856e0a4e47dadfce816e0c49020c30b
[mesa.git] / src / mesa / state_tracker / st_atom_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/samplerobj.h"
38
39 #include "st_context.h"
40 #include "st_cb_texture.h"
41 #include "st_format.h"
42 #include "st_atom.h"
43 #include "st_texture.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46
47 #include "cso_cache/cso_context.h"
48
49
50 /**
51 * Convert GLenum texcoord wrap tokens to pipe tokens.
52 */
53 static GLuint
54 gl_wrap_xlate(GLenum wrap)
55 {
56 switch (wrap) {
57 case GL_REPEAT:
58 return PIPE_TEX_WRAP_REPEAT;
59 case GL_CLAMP:
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;
73 default:
74 assert(0);
75 return 0;
76 }
77 }
78
79
80 static GLuint
81 gl_filter_to_mip_filter(GLenum filter)
82 {
83 switch (filter) {
84 case GL_NEAREST:
85 case GL_LINEAR:
86 return PIPE_TEX_MIPFILTER_NONE;
87
88 case GL_NEAREST_MIPMAP_NEAREST:
89 case GL_LINEAR_MIPMAP_NEAREST:
90 return PIPE_TEX_MIPFILTER_NEAREST;
91
92 case GL_NEAREST_MIPMAP_LINEAR:
93 case GL_LINEAR_MIPMAP_LINEAR:
94 return PIPE_TEX_MIPFILTER_LINEAR;
95
96 default:
97 assert(0);
98 return PIPE_TEX_MIPFILTER_NONE;
99 }
100 }
101
102
103 static GLuint
104 gl_filter_to_img_filter(GLenum filter)
105 {
106 switch (filter) {
107 case GL_NEAREST:
108 case GL_NEAREST_MIPMAP_NEAREST:
109 case GL_NEAREST_MIPMAP_LINEAR:
110 return PIPE_TEX_FILTER_NEAREST;
111
112 case GL_LINEAR:
113 case GL_LINEAR_MIPMAP_NEAREST:
114 case GL_LINEAR_MIPMAP_LINEAR:
115 return PIPE_TEX_FILTER_LINEAR;
116
117 default:
118 assert(0);
119 return PIPE_TEX_FILTER_NEAREST;
120 }
121 }
122
123
124 static void
125 update_samplers(struct st_context *st)
126 {
127 struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current;
128 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
129 const GLbitfield samplersUsed = (vprog->Base.SamplersUsed |
130 fprog->Base.SamplersUsed);
131 GLuint su;
132
133 st->state.num_samplers = 0;
134
135 /* loop over sampler units (aka tex image units) */
136 for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) {
137 struct pipe_sampler_state *sampler = st->state.samplers + su;
138
139 memset(sampler, 0, sizeof(*sampler));
140
141 if (samplersUsed & (1 << su)) {
142 struct gl_texture_object *texobj;
143 struct gl_texture_image *teximg;
144 struct gl_sampler_object *msamp;
145 GLuint texUnit;
146
147 if (fprog->Base.SamplersUsed & (1 << su))
148 texUnit = fprog->Base.SamplerUnits[su];
149 else
150 texUnit = vprog->Base.SamplerUnits[su];
151
152 texobj = st->ctx->Texture.Unit[texUnit]._Current;
153 if (!texobj) {
154 texobj = st_get_default_texture(st);
155 }
156
157 teximg = texobj->Image[0][texobj->BaseLevel];
158
159 msamp = _mesa_get_samplerobj(st->ctx, texUnit);
160
161 sampler->wrap_s = gl_wrap_xlate(msamp->WrapS);
162 sampler->wrap_t = gl_wrap_xlate(msamp->WrapT);
163 sampler->wrap_r = gl_wrap_xlate(msamp->WrapR);
164
165 sampler->min_img_filter = gl_filter_to_img_filter(msamp->MinFilter);
166 sampler->min_mip_filter = gl_filter_to_mip_filter(msamp->MinFilter);
167 sampler->mag_img_filter = gl_filter_to_img_filter(msamp->MagFilter);
168
169 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
170 sampler->normalized_coords = 1;
171
172 sampler->lod_bias = st->ctx->Texture.Unit[texUnit].LodBias +
173 msamp->LodBias;
174
175 sampler->min_lod = CLAMP(msamp->MinLod,
176 0.0f,
177 (GLfloat) texobj->MaxLevel - texobj->BaseLevel);
178 sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel - texobj->BaseLevel,
179 msamp->MaxLod);
180 if (sampler->max_lod < sampler->min_lod) {
181 /* The GL spec doesn't seem to specify what to do in this case.
182 * Swap the values.
183 */
184 float tmp = sampler->max_lod;
185 sampler->max_lod = sampler->min_lod;
186 sampler->min_lod = tmp;
187 assert(sampler->min_lod <= sampler->max_lod);
188 }
189
190 st_translate_color(msamp->BorderColor.f,
191 teximg ? teximg->_BaseFormat : GL_RGBA,
192 sampler->border_color);
193
194 sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
195 0 : (GLuint) msamp->MaxAnisotropy);
196
197 /* only care about ARB_shadow, not SGI shadow */
198 if (msamp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
199 sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
200 sampler->compare_func
201 = st_compare_func_to_pipe(msamp->CompareFunc);
202 }
203
204 st->state.num_samplers = su + 1;
205
206 /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
207 cso_single_sampler(st->cso_context, su, sampler);
208 if (su < st->ctx->Const.MaxVertexTextureImageUnits) {
209 cso_single_vertex_sampler(st->cso_context, su, sampler);
210 }
211 }
212 else {
213 /*printf("%s su=%u null\n", __FUNCTION__, su);*/
214 cso_single_sampler(st->cso_context, su, NULL);
215 if (su < st->ctx->Const.MaxVertexTextureImageUnits) {
216 cso_single_vertex_sampler(st->cso_context, su, NULL);
217 }
218 }
219 }
220
221 cso_single_sampler_done(st->cso_context);
222 if (st->ctx->Const.MaxVertexTextureImageUnits > 0) {
223 cso_single_vertex_sampler_done(st->cso_context);
224 }
225 }
226
227
228 const struct st_tracked_state st_update_sampler = {
229 "st_update_sampler", /* name */
230 { /* dirty */
231 _NEW_TEXTURE, /* mesa */
232 0, /* st */
233 },
234 update_samplers /* update */
235 };