Some changed for non-C99 compilers
[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_atom.h"
37 #include "st_program.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_util.h"
41 #include "cso_cache/cso_context.h"
42
43
44 /**
45 * Convert GLenum texcoord wrap tokens to pipe tokens.
46 */
47 static GLuint
48 gl_wrap_to_sp(GLenum wrap)
49 {
50 switch (wrap) {
51 case GL_REPEAT:
52 return PIPE_TEX_WRAP_REPEAT;
53 case GL_CLAMP:
54 return PIPE_TEX_WRAP_CLAMP;
55 case GL_CLAMP_TO_EDGE:
56 return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
57 case GL_CLAMP_TO_BORDER:
58 return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
59 case GL_MIRRORED_REPEAT:
60 return PIPE_TEX_WRAP_MIRROR_REPEAT;
61 case GL_MIRROR_CLAMP_EXT:
62 return PIPE_TEX_WRAP_MIRROR_CLAMP;
63 case GL_MIRROR_CLAMP_TO_EDGE_EXT:
64 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
65 case GL_MIRROR_CLAMP_TO_BORDER_EXT:
66 return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
67 default:
68 abort();
69 return 0;
70 }
71 }
72
73
74 static GLuint
75 gl_filter_to_mip_filter(GLenum filter)
76 {
77 switch (filter) {
78 case GL_NEAREST:
79 case GL_LINEAR:
80 return PIPE_TEX_MIPFILTER_NONE;
81
82 case GL_NEAREST_MIPMAP_NEAREST:
83 case GL_LINEAR_MIPMAP_NEAREST:
84 return PIPE_TEX_MIPFILTER_NEAREST;
85
86 case GL_NEAREST_MIPMAP_LINEAR:
87 case GL_LINEAR_MIPMAP_LINEAR:
88 return PIPE_TEX_MIPFILTER_LINEAR;
89
90 default:
91 assert(0);
92 return PIPE_TEX_MIPFILTER_NONE;
93 }
94 }
95
96
97 static GLuint
98 gl_filter_to_img_filter(GLenum filter)
99 {
100 switch (filter) {
101 case GL_NEAREST:
102 case GL_NEAREST_MIPMAP_NEAREST:
103 case GL_NEAREST_MIPMAP_LINEAR:
104 return PIPE_TEX_FILTER_NEAREST;
105
106 case GL_LINEAR:
107 case GL_LINEAR_MIPMAP_NEAREST:
108 case GL_LINEAR_MIPMAP_LINEAR:
109 return PIPE_TEX_FILTER_LINEAR;
110
111 default:
112 assert(0);
113 return PIPE_TEX_FILTER_NEAREST;
114 }
115 }
116
117
118 static void
119 update_samplers(struct st_context *st)
120 {
121 const struct st_fragment_program *fs = st->fp;
122 GLuint su;
123
124 st->state.num_samplers = 0;
125
126 /* loop over sampler units (aka tex image units) */
127 for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) {
128 struct pipe_sampler_state *sampler = st->state.samplers + su;
129
130 memset(sampler, 0, sizeof(*sampler));
131
132 if (fs->Base.Base.SamplersUsed & (1 << su)) {
133 GLuint texUnit = fs->Base.Base.SamplerUnits[su];
134 const struct gl_texture_object *texobj
135 = st->ctx->Texture.Unit[texUnit]._Current;
136
137 assert(texobj);
138
139 sampler->wrap_s = gl_wrap_to_sp(texobj->WrapS);
140 sampler->wrap_t = gl_wrap_to_sp(texobj->WrapT);
141 sampler->wrap_r = gl_wrap_to_sp(texobj->WrapR);
142
143 sampler->min_img_filter = gl_filter_to_img_filter(texobj->MinFilter);
144 sampler->min_mip_filter = gl_filter_to_mip_filter(texobj->MinFilter);
145 sampler->mag_img_filter = gl_filter_to_img_filter(texobj->MagFilter);
146
147 if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
148 sampler->normalized_coords = 1;
149
150 sampler->lod_bias = st->ctx->Texture.Unit[su].LodBias;
151 sampler->min_lod = MAX2(texobj->BaseLevel, texobj->MinLod);
152 sampler->max_lod = MIN2(texobj->MaxLevel, texobj->MaxLod);
153
154 sampler->border_color[0] = texobj->BorderColor[RCOMP];
155 sampler->border_color[1] = texobj->BorderColor[GCOMP];
156 sampler->border_color[2] = texobj->BorderColor[BCOMP];
157 sampler->border_color[3] = texobj->BorderColor[ACOMP];
158
159 sampler->max_anisotropy = texobj->MaxAnisotropy;
160 if (sampler->max_anisotropy > 1.0) {
161 sampler->min_img_filter = PIPE_TEX_FILTER_ANISO;
162 sampler->mag_img_filter = PIPE_TEX_FILTER_ANISO;
163 }
164
165 /* only care about ARB_shadow, not SGI shadow */
166 if (texobj->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
167 sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
168 sampler->compare_func
169 = st_compare_func_to_pipe(texobj->CompareFunc);
170 }
171
172 st->state.num_samplers = su + 1;
173
174 /* XXX more sampler state here */
175
176 cso_single_sampler(st->cso_context, su, sampler);
177 }
178 else {
179 cso_single_sampler(st->cso_context, su, NULL);
180 }
181 }
182
183 cso_single_sampler_done(st->cso_context);
184 }
185
186
187 const struct st_tracked_state st_update_sampler = {
188 "st_update_sampler", /* name */
189 { /* dirty */
190 _NEW_TEXTURE, /* mesa */
191 0, /* st */
192 },
193 update_samplers /* update */
194 };