set sampler state for shadow test
[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 "st_context.h"
36 #include "st_cache.h"
37 #include "st_atom.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40
41
42 /**
43 * Convert GLenum texcoord wrap tokens to pipe tokens.
44 */
45 static GLuint
46 gl_wrap_to_sp(GLenum wrap)
47 {
48 switch (wrap) {
49 case GL_REPEAT:
50 return PIPE_TEX_WRAP_REPEAT;
51 case GL_CLAMP:
52 return PIPE_TEX_WRAP_CLAMP;
53 case GL_CLAMP_TO_EDGE:
54 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
55 case GL_CLAMP_TO_BORDER:
56 return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
57 case GL_MIRRORED_REPEAT:
58 return PIPE_TEX_WRAP_MIRROR_REPEAT;
59 case GL_MIRROR_CLAMP_EXT:
60 return PIPE_TEX_WRAP_MIRROR_CLAMP;
61 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
62 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
63 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
64 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
65 default:
66 abort();
67 return 0;
68 }
69 }
70
71
72 static GLuint
73 gl_filter_to_mip_filter(GLenum filter)
74 {
75 switch (filter) {
76 case GL_NEAREST:
77 case GL_LINEAR:
78 return PIPE_TEX_MIPFILTER_NONE;
79
80 case GL_NEAREST_MIPMAP_NEAREST:
81 case GL_LINEAR_MIPMAP_NEAREST:
82 return PIPE_TEX_MIPFILTER_NEAREST;
83
84 case GL_NEAREST_MIPMAP_LINEAR:
85 case GL_LINEAR_MIPMAP_LINEAR:
86 return PIPE_TEX_MIPFILTER_LINEAR;
87
88 default:
89 assert(0);
90 return PIPE_TEX_MIPFILTER_NONE;
91 }
92 }
93
94
95 static GLuint
96 gl_filter_to_img_filter(GLenum filter)
97 {
98 switch (filter) {
99 case GL_NEAREST:
100 case GL_NEAREST_MIPMAP_NEAREST:
101 case GL_NEAREST_MIPMAP_LINEAR:
102 return PIPE_TEX_FILTER_NEAREST;
103
104 case GL_LINEAR:
105 case GL_LINEAR_MIPMAP_NEAREST:
106 case GL_LINEAR_MIPMAP_LINEAR:
107 return PIPE_TEX_FILTER_LINEAR;
108
109 default:
110 assert(0);
111 return PIPE_TEX_FILTER_NEAREST;
112 }
113 }
114
115
116 static void
117 update_samplers(struct st_context *st)
118 {
119 GLuint u;
120
121 for (u = 0; u < st->ctx->Const.MaxTextureImageUnits; u++) {
122 const struct gl_texture_object *texobj
123 = st->ctx->Texture.Unit[u]._Current;
124 struct pipe_sampler_state sampler;
125 const struct cso_sampler *cso;
126
127 memset(&sampler, 0, sizeof(sampler));
128
129 if (texobj) {
130 sampler.wrap_s = gl_wrap_to_sp(texobj->WrapS);
131 sampler.wrap_t = gl_wrap_to_sp(texobj->WrapT);
132 sampler.wrap_r = gl_wrap_to_sp(texobj->WrapR);
133
134 sampler.min_img_filter = gl_filter_to_img_filter(texobj->MinFilter);
135 sampler.min_mip_filter = gl_filter_to_mip_filter(texobj->MinFilter);
136 sampler.mag_img_filter = gl_filter_to_img_filter(texobj->MagFilter);
137
138 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
139 sampler.normalized_coords = 1;
140
141 sampler.lod_bias = st->ctx->Texture.Unit[u].LodBias;
142 #if 1
143 sampler.min_lod = texobj->MinLod;
144 sampler.max_lod = texobj->MaxLod;
145 #else
146 /* min/max lod should really be as follows (untested).
147 * Also, calculate_first_last_level() needs to be overhauled
148 * since today's hardware had real support for LOD clamping.
149 */
150 sampler.min_lod = MAX2(texobj->BaseLevel, texobj->MinLod);
151 sampler.max_lod = MIN2(texobj->MaxLevel, texobj->MaxLod);
152 #endif
153
154 sampler.max_anisotropy = texobj->MaxAnisotropy;
155
156 /* only care about ARB_shadow, not SGI shadow */
157 if (texobj->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
158 sampler.compare = 1;
159 sampler.compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
160 sampler.compare_func
161 = st_compare_func_to_pipe(texobj->CompareFunc);
162 }
163
164 /* XXX more sampler state here */
165 }
166
167 cso = st_cached_sampler_state(st, &sampler);
168
169 if (cso != st->state.sampler[u]) {
170 /* state has changed */
171 st->state.sampler[u] = cso;
172 st->pipe->bind_sampler_state(st->pipe, u, cso->data);
173 }
174 }
175
176
177 /* mapping from sampler vars to texture units */
178 {
179 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
180 uint sample_units[PIPE_MAX_SAMPLERS];
181 uint s;
182 for (s = 0; s < PIPE_MAX_SAMPLERS; s++) {
183 sample_units[s] = fprog->Base.SamplerUnits[s];
184 }
185
186 st->pipe->set_sampler_units(st->pipe, PIPE_MAX_SAMPLERS, sample_units);
187 }
188 }
189
190
191 const struct st_tracked_state st_update_sampler = {
192 .name = "st_update_sampler",
193 .dirty = {
194 .mesa = _NEW_TEXTURE | _NEW_PROGRAM,
195 .st = 0,
196 },
197 .update = update_samplers
198 };
199
200
201
202
203