Merge remote branch 'origin/master' into nvc0-new
[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
37 #include "st_context.h"
38 #include "st_cb_texture.h"
39 #include "st_atom.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42
43 #include "cso_cache/cso_context.h"
44
45
46 /**
47 * Convert GLenum texcoord wrap tokens to pipe tokens.
48 */
49 static GLuint
50 gl_wrap_xlate(GLenum wrap)
51 {
52 switch (wrap) {
53 case GL_REPEAT:
54 return PIPE_TEX_WRAP_REPEAT;
55 case GL_CLAMP:
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;
69 default:
70 assert(0);
71 return 0;
72 }
73 }
74
75
76 static GLuint
77 gl_filter_to_mip_filter(GLenum filter)
78 {
79 switch (filter) {
80 case GL_NEAREST:
81 case GL_LINEAR:
82 return PIPE_TEX_MIPFILTER_NONE;
83
84 case GL_NEAREST_MIPMAP_NEAREST:
85 case GL_LINEAR_MIPMAP_NEAREST:
86 return PIPE_TEX_MIPFILTER_NEAREST;
87
88 case GL_NEAREST_MIPMAP_LINEAR:
89 case GL_LINEAR_MIPMAP_LINEAR:
90 return PIPE_TEX_MIPFILTER_LINEAR;
91
92 default:
93 assert(0);
94 return PIPE_TEX_MIPFILTER_NONE;
95 }
96 }
97
98
99 static GLuint
100 gl_filter_to_img_filter(GLenum filter)
101 {
102 switch (filter) {
103 case GL_NEAREST:
104 case GL_NEAREST_MIPMAP_NEAREST:
105 case GL_NEAREST_MIPMAP_LINEAR:
106 return PIPE_TEX_FILTER_NEAREST;
107
108 case GL_LINEAR:
109 case GL_LINEAR_MIPMAP_NEAREST:
110 case GL_LINEAR_MIPMAP_LINEAR:
111 return PIPE_TEX_FILTER_LINEAR;
112
113 default:
114 assert(0);
115 return PIPE_TEX_FILTER_NEAREST;
116 }
117 }
118
119
120 static void
121 xlate_border_color(const GLfloat *colorIn, GLenum baseFormat, GLfloat *colorOut)
122 {
123 switch (baseFormat) {
124 case GL_RED:
125 colorOut[0] = colorIn[0];
126 colorOut[1] = 0.0F;
127 colorOut[2] = 0.0F;
128 colorOut[3] = 1.0F;
129 break;
130 case GL_RG:
131 colorOut[0] = colorIn[0];
132 colorOut[1] = colorIn[1];
133 colorOut[2] = 0.0F;
134 colorOut[3] = 1.0F;
135 break;
136 case GL_RGB:
137 colorOut[0] = colorIn[0];
138 colorOut[1] = colorIn[1];
139 colorOut[2] = colorIn[2];
140 colorOut[3] = 1.0F;
141 break;
142 case GL_ALPHA:
143 colorOut[0] = colorOut[1] = colorOut[2] = 0.0;
144 colorOut[3] = colorIn[3];
145 break;
146 case GL_LUMINANCE:
147 colorOut[0] = colorOut[1] = colorOut[2] = colorIn[0];
148 colorOut[3] = 1.0;
149 break;
150 case GL_LUMINANCE_ALPHA:
151 colorOut[0] = colorOut[1] = colorOut[2] = colorIn[0];
152 colorOut[3] = colorIn[3];
153 break;
154 case GL_INTENSITY:
155 colorOut[0] = colorOut[1] = colorOut[2] = colorOut[3] = colorIn[0];
156 break;
157 default:
158 COPY_4V(colorOut, colorIn);
159 }
160 }
161
162
163 static void
164 update_samplers(struct st_context *st)
165 {
166 struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current;
167 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
168 const GLbitfield samplersUsed = (vprog->Base.SamplersUsed |
169 fprog->Base.SamplersUsed);
170 GLuint su;
171
172 st->state.num_samplers = 0;
173
174 /* loop over sampler units (aka tex image units) */
175 for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) {
176 struct pipe_sampler_state *sampler = st->state.samplers + su;
177
178 memset(sampler, 0, sizeof(*sampler));
179
180 if (samplersUsed & (1 << su)) {
181 struct gl_texture_object *texobj;
182 struct gl_texture_image *teximg;
183 GLuint texUnit;
184
185 if (fprog->Base.SamplersUsed & (1 << su))
186 texUnit = fprog->Base.SamplerUnits[su];
187 else
188 texUnit = vprog->Base.SamplerUnits[su];
189
190 texobj = st->ctx->Texture.Unit[texUnit]._Current;
191 if (!texobj) {
192 texobj = st_get_default_texture(st);
193 }
194
195 teximg = texobj->Image[0][texobj->BaseLevel];
196
197 sampler->wrap_s = gl_wrap_xlate(texobj->WrapS);
198 sampler->wrap_t = gl_wrap_xlate(texobj->WrapT);
199 sampler->wrap_r = gl_wrap_xlate(texobj->WrapR);
200
201 sampler->min_img_filter = gl_filter_to_img_filter(texobj->MinFilter);
202 sampler->min_mip_filter = gl_filter_to_mip_filter(texobj->MinFilter);
203 sampler->mag_img_filter = gl_filter_to_img_filter(texobj->MagFilter);
204
205 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
206 sampler->normalized_coords = 1;
207
208 sampler->lod_bias = st->ctx->Texture.Unit[su].LodBias;
209
210 sampler->min_lod = texobj->BaseLevel + texobj->MinLod;
211 if (sampler->min_lod < texobj->BaseLevel)
212 sampler->min_lod = texobj->BaseLevel;
213
214 sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel,
215 (texobj->MaxLod + texobj->BaseLevel));
216 if (sampler->max_lod < sampler->min_lod) {
217 /* The GL spec doesn't seem to specify what to do in this case.
218 * Swap the values.
219 */
220 float tmp = sampler->max_lod;
221 sampler->max_lod = sampler->min_lod;
222 sampler->min_lod = tmp;
223 assert(sampler->min_lod <= sampler->max_lod);
224 }
225
226 xlate_border_color(texobj->BorderColor.f,
227 teximg ? teximg->_BaseFormat : GL_RGBA,
228 sampler->border_color);
229
230 sampler->max_anisotropy = (texobj->MaxAnisotropy == 1.0 ? 0 : (GLuint)texobj->MaxAnisotropy);
231
232 /* only care about ARB_shadow, not SGI shadow */
233 if (texobj->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
234 sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
235 sampler->compare_func
236 = st_compare_func_to_pipe(texobj->CompareFunc);
237 }
238
239 st->state.num_samplers = su + 1;
240
241 /*printf("%s su=%u non-null\n", __FUNCTION__, su);*/
242 cso_single_sampler(st->cso_context, su, sampler);
243 if (su < st->ctx->Const.MaxVertexTextureImageUnits) {
244 cso_single_vertex_sampler(st->cso_context, su, sampler);
245 }
246 }
247 else {
248 /*printf("%s su=%u null\n", __FUNCTION__, su);*/
249 cso_single_sampler(st->cso_context, su, NULL);
250 if (su < st->ctx->Const.MaxVertexTextureImageUnits) {
251 cso_single_vertex_sampler(st->cso_context, su, NULL);
252 }
253 }
254 }
255
256 cso_single_sampler_done(st->cso_context);
257 if (st->ctx->Const.MaxVertexTextureImageUnits > 0) {
258 cso_single_vertex_sampler_done(st->cso_context);
259 }
260 }
261
262
263 const struct st_tracked_state st_update_sampler = {
264 "st_update_sampler", /* name */
265 { /* dirty */
266 _NEW_TEXTURE, /* mesa */
267 0, /* st */
268 },
269 update_samplers /* update */
270 };