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