mesa: Remove SGI_color_matrix.
[mesa.git] / src / mesa / state_tracker / st_atom_texture.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 #include "program/prog_instruction.h"
37
38 #include "st_context.h"
39 #include "st_atom.h"
40 #include "st_texture.h"
41 #include "st_format.h"
42 #include "st_cb_texture.h"
43 #include "pipe/p_context.h"
44 #include "util/u_format.h"
45 #include "util/u_inlines.h"
46 #include "cso_cache/cso_context.h"
47
48 /**
49 * Combine depth texture mode with "swizzle" so that depth mode swizzling
50 * takes place before texture swizzling, and return the resulting swizzle.
51 * If the format is not a depth format, return "swizzle" unchanged.
52 *
53 * \param format PIPE_FORMAT_*.
54 * \param swizzle Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
55 * \param depthmode One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA.
56 */
57 static GLuint apply_depthmode(enum pipe_format format,
58 GLuint swizzle, GLenum depthmode)
59 {
60 const struct util_format_description *desc =
61 util_format_description(format);
62 unsigned char swiz[4];
63 unsigned i;
64
65 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS ||
66 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
67 /* Not a depth format. */
68 return swizzle;
69 }
70
71 for (i = 0; i < 4; i++)
72 swiz[i] = GET_SWZ(swizzle, i);
73
74 switch (depthmode) {
75 case GL_LUMINANCE:
76 /* Rewrite reads from W to ONE, and reads from XYZ to XXX. */
77 for (i = 0; i < 4; i++)
78 if (swiz[i] == SWIZZLE_W)
79 swiz[i] = SWIZZLE_ONE;
80 else if (swiz[i] < SWIZZLE_W)
81 swiz[i] = SWIZZLE_X;
82 break;
83
84 case GL_INTENSITY:
85 /* Rewrite reads from XYZW to XXXX. */
86 for (i = 0; i < 4; i++)
87 if (swiz[i] <= SWIZZLE_W)
88 swiz[i] = SWIZZLE_X;
89 break;
90
91 case GL_ALPHA:
92 /* Rewrite reads from W to X, and reads from XYZ to 000. */
93 for (i = 0; i < 4; i++)
94 if (swiz[i] == SWIZZLE_W)
95 swiz[i] = SWIZZLE_X;
96 else if (swiz[i] < SWIZZLE_W)
97 swiz[i] = SWIZZLE_ZERO;
98 break;
99 }
100
101 return MAKE_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
102 }
103
104 /**
105 * Return TRUE if the swizzling described by "swizzle" and
106 * "depthmode" (for depth textures only) is different from the swizzling
107 * set in the given sampler view.
108 *
109 * \param sv A sampler view.
110 * \param swizzle Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
111 * \param depthmode One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA.
112 */
113 static boolean check_sampler_swizzle(struct pipe_sampler_view *sv,
114 GLuint swizzle, GLenum depthmode)
115 {
116 swizzle = apply_depthmode(sv->texture->format, swizzle, depthmode);
117
118 if ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
119 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
120 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
121 (sv->swizzle_a != GET_SWZ(swizzle, 3)))
122 return true;
123 return false;
124 }
125
126 static INLINE struct pipe_sampler_view *
127 st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
128 struct st_texture_object *stObj,
129 enum pipe_format format)
130
131 {
132 struct pipe_sampler_view templ;
133 GLuint swizzle = apply_depthmode(stObj->pt->format,
134 stObj->base._Swizzle,
135 stObj->base.DepthMode);
136
137 u_sampler_view_default_template(&templ,
138 stObj->pt,
139 format);
140
141 if (swizzle != SWIZZLE_NOOP) {
142 templ.swizzle_r = GET_SWZ(swizzle, 0);
143 templ.swizzle_g = GET_SWZ(swizzle, 1);
144 templ.swizzle_b = GET_SWZ(swizzle, 2);
145 templ.swizzle_a = GET_SWZ(swizzle, 3);
146 }
147
148 return pipe->create_sampler_view(pipe, stObj->pt, &templ);
149 }
150
151
152 static INLINE struct pipe_sampler_view *
153 st_get_texture_sampler_view_from_stobj(struct st_texture_object *stObj,
154 struct pipe_context *pipe,
155 enum pipe_format format)
156
157 {
158 if (!stObj || !stObj->pt) {
159 return NULL;
160 }
161
162 if (!stObj->sampler_view) {
163 stObj->sampler_view = st_create_texture_sampler_view_from_stobj(pipe, stObj, format);
164 }
165
166 return stObj->sampler_view;
167 }
168
169 static void
170 update_textures(struct st_context *st)
171 {
172 struct pipe_context *pipe = st->pipe;
173 struct gl_vertex_program *vprog = st->ctx->VertexProgram._Current;
174 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
175 const GLbitfield samplersUsed = (vprog->Base.SamplersUsed |
176 fprog->Base.SamplersUsed);
177 GLuint su;
178
179 st->state.num_textures = 0;
180
181 /* loop over sampler units (aka tex image units) */
182 for (su = 0; su < st->ctx->Const.MaxTextureImageUnits; su++) {
183 struct pipe_sampler_view *sampler_view = NULL;
184 enum pipe_format st_view_format;
185 if (samplersUsed & (1 << su)) {
186 struct gl_texture_object *texObj;
187 struct st_texture_object *stObj;
188 GLboolean retval;
189 GLuint texUnit;
190
191 if (fprog->Base.SamplersUsed & (1 << su))
192 texUnit = fprog->Base.SamplerUnits[su];
193 else
194 texUnit = vprog->Base.SamplerUnits[su];
195
196 texObj = st->ctx->Texture.Unit[texUnit]._Current;
197
198 if (!texObj) {
199 texObj = st_get_default_texture(st);
200 }
201 stObj = st_texture_object(texObj);
202
203 retval = st_finalize_texture(st->ctx, st->pipe, texObj);
204 if (!retval) {
205 /* out of mem */
206 continue;
207 }
208
209 st_view_format = stObj->pt->format;
210 {
211 struct st_texture_image *firstImage;
212 enum pipe_format firstImageFormat;
213 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
214
215 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
216 if (firstImageFormat != stObj->pt->format)
217 st_view_format = firstImageFormat;
218
219 }
220 st->state.num_textures = su + 1;
221
222 /* if sampler view has changed dereference it */
223 if (stObj->sampler_view)
224 if (check_sampler_swizzle(stObj->sampler_view,
225 stObj->base._Swizzle,
226 stObj->base.DepthMode) ||
227 (st_view_format != stObj->sampler_view->format))
228 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
229
230 sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe, st_view_format);
231 }
232 pipe_sampler_view_reference(&st->state.sampler_views[su], sampler_view);
233 }
234
235 cso_set_fragment_sampler_views(st->cso_context,
236 st->state.num_textures,
237 st->state.sampler_views);
238 if (st->ctx->Const.MaxVertexTextureImageUnits > 0) {
239 cso_set_vertex_sampler_views(st->cso_context,
240 MIN2(st->state.num_textures,
241 st->ctx->Const.MaxVertexTextureImageUnits),
242 st->state.sampler_views);
243 }
244 }
245
246
247 const struct st_tracked_state st_update_texture = {
248 "st_update_texture", /* name */
249 { /* dirty */
250 _NEW_TEXTURE, /* mesa */
251 ST_NEW_FRAGMENT_PROGRAM, /* st */
252 },
253 update_textures /* update */
254 };
255
256
257
258
259 static void
260 finalize_textures(struct st_context *st)
261 {
262 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
263 const GLboolean prev_missing_textures = st->missing_textures;
264 GLuint su;
265
266 st->missing_textures = GL_FALSE;
267
268 for (su = 0; su < st->ctx->Const.MaxTextureCoordUnits; su++) {
269 if (fprog->Base.SamplersUsed & (1 << su)) {
270 const GLuint texUnit = fprog->Base.SamplerUnits[su];
271 struct gl_texture_object *texObj
272 = st->ctx->Texture.Unit[texUnit]._Current;
273
274 if (texObj) {
275 GLboolean retval;
276
277 retval = st_finalize_texture(st->ctx, st->pipe, texObj);
278 if (!retval) {
279 /* out of mem */
280 st->missing_textures = GL_TRUE;
281 continue;
282 }
283 }
284 }
285 }
286
287 if (prev_missing_textures != st->missing_textures)
288 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
289 }
290
291
292
293 const struct st_tracked_state st_finalize_textures = {
294 "st_finalize_textures", /* name */
295 { /* dirty */
296 _NEW_TEXTURE, /* mesa */
297 0, /* st */
298 },
299 finalize_textures /* update */
300 };