mesa: move sampler state into new gl_sampler_object type
[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_format.h"
40 #include "st_atom.h"
41 #include "st_texture.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44
45 #include "cso_cache/cso_context.h"
46
47
48 /**
49 * Convert GLenum texcoord wrap tokens to pipe tokens.
50 */
51 static GLuint
52 gl_wrap_xlate(GLenum wrap)
53 {
54 switch (wrap) {
55 case GL_REPEAT:
56 return PIPE_TEX_WRAP_REPEAT;
57 case GL_CLAMP:
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;
71 default:
72 assert(0);
73 return 0;
74 }
75 }
76
77
78 static GLuint
79 gl_filter_to_mip_filter(GLenum filter)
80 {
81 switch (filter) {
82 case GL_NEAREST:
83 case GL_LINEAR:
84 return PIPE_TEX_MIPFILTER_NONE;
85
86 case GL_NEAREST_MIPMAP_NEAREST:
87 case GL_LINEAR_MIPMAP_NEAREST:
88 return PIPE_TEX_MIPFILTER_NEAREST;
89
90 case GL_NEAREST_MIPMAP_LINEAR:
91 case GL_LINEAR_MIPMAP_LINEAR:
92 return PIPE_TEX_MIPFILTER_LINEAR;
93
94 default:
95 assert(0);
96 return PIPE_TEX_MIPFILTER_NONE;
97 }
98 }
99
100
101 static GLuint
102 gl_filter_to_img_filter(GLenum filter)
103 {
104 switch (filter) {
105 case GL_NEAREST:
106 case GL_NEAREST_MIPMAP_NEAREST:
107 case GL_NEAREST_MIPMAP_LINEAR:
108 return PIPE_TEX_FILTER_NEAREST;
109
110 case GL_LINEAR:
111 case GL_LINEAR_MIPMAP_NEAREST:
112 case GL_LINEAR_MIPMAP_LINEAR:
113 return PIPE_TEX_FILTER_LINEAR;
114
115 default:
116 assert(0);
117 return PIPE_TEX_FILTER_NEAREST;
118 }
119 }
120
121
122 static void
123 update_samplers(struct st_context *st)
124 {
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);
129 GLuint su;
130
131 st->state.num_samplers = 0;
132
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;
136
137 memset(sampler, 0, sizeof(*sampler));
138
139 if (samplersUsed & (1 << su)) {
140 struct gl_texture_object *texobj;
141 struct gl_texture_image *teximg;
142 struct gl_sampler_object *msamp;
143 GLuint texUnit;
144
145 if (fprog->Base.SamplersUsed & (1 << su))
146 texUnit = fprog->Base.SamplerUnits[su];
147 else
148 texUnit = vprog->Base.SamplerUnits[su];
149
150 texobj = st->ctx->Texture.Unit[texUnit]._Current;
151 if (!texobj) {
152 texobj = st_get_default_texture(st);
153 }
154
155 teximg = texobj->Image[0][texobj->BaseLevel];
156
157 msamp = st_get_mesa_sampler(st->ctx, texUnit);
158
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);
162
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);
166
167 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
168 sampler->normalized_coords = 1;
169
170 sampler->lod_bias = st->ctx->Texture.Unit[texUnit].LodBias +
171 msamp->LodBias;
172
173 sampler->min_lod = CLAMP(msamp->MinLod,
174 0.0f,
175 (GLfloat) texobj->MaxLevel - texobj->BaseLevel);
176 sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel - texobj->BaseLevel,
177 msamp->MaxLod);
178 if (sampler->max_lod < sampler->min_lod) {
179 /* The GL spec doesn't seem to specify what to do in this case.
180 * Swap the values.
181 */
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);
186 }
187
188 st_translate_color(msamp->BorderColor.f,
189 teximg ? teximg->_BaseFormat : GL_RGBA,
190 sampler->border_color);
191
192 sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
193 0 : (GLuint) msamp->MaxAnisotropy);
194
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);
200 }
201
202 st->state.num_samplers = su + 1;
203
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);
208 }
209 }
210 else {
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);
215 }
216 }
217 }
218
219 cso_single_sampler_done(st->cso_context);
220 if (st->ctx->Const.MaxVertexTextureImageUnits > 0) {
221 cso_single_vertex_sampler_done(st->cso_context);
222 }
223 }
224
225
226 const struct st_tracked_state st_update_sampler = {
227 "st_update_sampler", /* name */
228 { /* dirty */
229 _NEW_TEXTURE, /* mesa */
230 0, /* st */
231 },
232 update_samplers /* update */
233 };