Tizen 2.0 Release
[profile/ivi/osmesa.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 "main/mtypes.h"
37 #include "main/samplerobj.h"
38 #include "program/prog_instruction.h"
39
40 #include "st_context.h"
41 #include "st_atom.h"
42 #include "st_texture.h"
43 #include "st_format.h"
44 #include "st_cb_texture.h"
45 #include "pipe/p_context.h"
46 #include "util/u_format.h"
47 #include "util/u_inlines.h"
48 #include "cso_cache/cso_context.h"
49
50
51 /**
52  * Combine depth texture mode with "swizzle" so that depth mode swizzling
53  * takes place before texture swizzling, and return the resulting swizzle.
54  * If the format is not a depth format, return "swizzle" unchanged.
55  *
56  * \param format     PIPE_FORMAT_*.
57  * \param swizzle    Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
58  * \param depthmode  One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA, GL_RED.
59  */
60 static GLuint
61 apply_depthmode(enum pipe_format format, GLuint swizzle, GLenum depthmode)
62 {
63    const struct util_format_description *desc =
64          util_format_description(format);
65    unsigned char swiz[4];
66    unsigned i;
67
68    if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS ||
69        desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
70       /* Not a depth format. */
71       return swizzle;
72    }
73
74    for (i = 0; i < 4; i++)
75       swiz[i] = GET_SWZ(swizzle, i);
76
77    switch (depthmode) {
78       case GL_LUMINANCE:
79          /* Rewrite reads from W to ONE, and reads from XYZ to XXX. */
80          for (i = 0; i < 4; i++)
81             if (swiz[i] == SWIZZLE_W)
82                swiz[i] = SWIZZLE_ONE;
83             else if (swiz[i] < SWIZZLE_W)
84                swiz[i] = SWIZZLE_X;
85          break;
86
87       case GL_INTENSITY:
88          /* Rewrite reads from XYZW to XXXX. */
89          for (i = 0; i < 4; i++)
90             if (swiz[i] <= SWIZZLE_W)
91                swiz[i] = SWIZZLE_X;
92          break;
93
94       case GL_ALPHA:
95          /* Rewrite reads from W to X, and reads from XYZ to 000. */
96          for (i = 0; i < 4; i++)
97             if (swiz[i] == SWIZZLE_W)
98                swiz[i] = SWIZZLE_X;
99             else if (swiz[i] < SWIZZLE_W)
100                swiz[i] = SWIZZLE_ZERO;
101          break;
102       case GL_RED:
103          /* Rewrite reads W to 1, XYZ to X00 */
104          for (i = 0; i < 4; i++)
105             if (swiz[i] == SWIZZLE_W)
106                swiz[i] = SWIZZLE_ONE;
107             else if (swiz[i] == SWIZZLE_Y || swiz[i] == SWIZZLE_Z)
108                swiz[i] = SWIZZLE_ZERO;
109          break;
110    }
111
112    return MAKE_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
113 }
114
115
116 /**
117  * Return TRUE if the swizzling described by "swizzle" and
118  * "depthmode" (for depth textures only) is different from the swizzling
119  * set in the given sampler view.
120  *
121  * \param sv         A sampler view.
122  * \param swizzle    Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
123  * \param depthmode  One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA.
124  */
125 static boolean
126 check_sampler_swizzle(struct pipe_sampler_view *sv,
127                       GLuint swizzle, GLenum depthmode)
128 {
129    swizzle = apply_depthmode(sv->texture->format, swizzle, depthmode);
130
131    if ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
132        (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
133        (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
134        (sv->swizzle_a != GET_SWZ(swizzle, 3)))
135       return TRUE;
136    return FALSE;
137 }
138
139
140 static INLINE struct pipe_sampler_view *
141 st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
142                                           struct st_texture_object *stObj,
143                                           const struct gl_sampler_object *samp,
144                                           enum pipe_format format)
145 {
146    struct pipe_sampler_view templ;
147    GLuint swizzle = apply_depthmode(stObj->pt->format,
148                                     stObj->base._Swizzle,
149                                     samp->DepthMode);
150
151    u_sampler_view_default_template(&templ,
152                                    stObj->pt,
153                                    format);
154    templ.u.tex.first_level = stObj->base.BaseLevel;
155
156    if (swizzle != SWIZZLE_NOOP) {
157       templ.swizzle_r = GET_SWZ(swizzle, 0);
158       templ.swizzle_g = GET_SWZ(swizzle, 1);
159       templ.swizzle_b = GET_SWZ(swizzle, 2);
160       templ.swizzle_a = GET_SWZ(swizzle, 3);
161    }
162
163    return pipe->create_sampler_view(pipe, stObj->pt, &templ);
164 }
165
166
167 static INLINE struct pipe_sampler_view *
168 st_get_texture_sampler_view_from_stobj(struct st_texture_object *stObj,
169                                        struct pipe_context *pipe,
170                                        const struct gl_sampler_object *samp,
171                                        enum pipe_format format)
172 {
173    if (!stObj || !stObj->pt) {
174       return NULL;
175    }
176
177    if (!stObj->sampler_view) {
178       stObj->sampler_view =
179          st_create_texture_sampler_view_from_stobj(pipe, stObj, samp, format);
180    }
181
182    return stObj->sampler_view;
183 }
184
185 static GLboolean
186 update_single_texture(struct st_context *st, struct pipe_sampler_view **sampler_view,
187                       GLuint texUnit)
188 {
189    struct pipe_context *pipe = st->pipe;
190    struct gl_context *ctx = st->ctx;
191    const struct gl_sampler_object *samp;
192    struct gl_texture_object *texObj;
193    struct st_texture_object *stObj;
194    enum pipe_format st_view_format;
195    GLboolean retval;
196
197    samp = _mesa_get_samplerobj(ctx, texUnit);
198
199    texObj = ctx->Texture.Unit[texUnit]._Current;
200
201    if (!texObj) {
202       texObj = st_get_default_texture(st);
203       samp = &texObj->Sampler;
204    }
205    stObj = st_texture_object(texObj);
206
207    retval = st_finalize_texture(ctx, st->pipe, texObj);
208    if (!retval) {
209       /* out of mem */
210       return GL_FALSE;
211    }
212
213    /* Determine the format of the texture sampler view */
214    st_view_format = stObj->pt->format;
215    {
216       const struct st_texture_image *firstImage =
217          st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
218       const gl_format texFormat = firstImage->base.TexFormat;
219       enum pipe_format firstImageFormat =
220          st_mesa_format_to_pipe_format(texFormat);
221
222       if ((samp->sRGBDecode == GL_SKIP_DECODE_EXT) &&
223           (_mesa_get_format_color_encoding(texFormat) == GL_SRGB)) {
224          /* don't do sRGB->RGB conversion.  Interpret the texture
225           * texture data as linear values.
226           */
227          const gl_format linearFormat =
228             _mesa_get_srgb_format_linear(texFormat);
229          firstImageFormat = st_mesa_format_to_pipe_format(linearFormat);
230       }
231
232       if (firstImageFormat != stObj->pt->format)
233          st_view_format = firstImageFormat;
234    }
235
236
237    /* if sampler view has changed dereference it */
238    if (stObj->sampler_view) {
239       if (check_sampler_swizzle(stObj->sampler_view,
240                                 stObj->base._Swizzle,
241                                 samp->DepthMode) ||
242           (st_view_format != stObj->sampler_view->format) ||
243           stObj->base.BaseLevel != stObj->sampler_view->u.tex.first_level) {
244          pipe_sampler_view_reference(&stObj->sampler_view, NULL);
245       }
246    }
247
248    *sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe,
249                                                           samp,
250                                                           st_view_format);
251    return GL_TRUE;
252 }
253
254 static void 
255 update_vertex_textures(struct st_context *st)
256 {
257    const struct gl_context *ctx = st->ctx;
258    struct gl_vertex_program *vprog = ctx->VertexProgram._Current;
259    GLuint su;
260
261    st->state.num_vertex_textures = 0;
262
263    /* loop over sampler units (aka tex image units) */
264    for (su = 0; su < ctx->Const.MaxTextureImageUnits; su++) {
265       struct pipe_sampler_view *sampler_view = NULL;
266       if (vprog->Base.SamplersUsed & (1 << su)) {
267          GLboolean retval;
268          GLuint texUnit;
269
270          texUnit = vprog->Base.SamplerUnits[su];
271
272          retval = update_single_texture(st, &sampler_view, texUnit);
273          if (retval == GL_FALSE)
274             continue;
275
276          st->state.num_vertex_textures = su + 1;
277
278       }
279       pipe_sampler_view_reference(&st->state.sampler_vertex_views[su], sampler_view);
280    }
281
282    if (ctx->Const.MaxVertexTextureImageUnits > 0) {
283       GLuint numUnits = MIN2(st->state.num_vertex_textures,
284                              ctx->Const.MaxVertexTextureImageUnits);
285       cso_set_vertex_sampler_views(st->cso_context,
286                                    numUnits,
287                                    st->state.sampler_vertex_views);
288    }
289 }
290
291 static void 
292 update_fragment_textures(struct st_context *st)
293 {
294    const struct gl_context *ctx = st->ctx;
295    struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
296    GLuint su;
297
298    st->state.num_textures = 0;
299
300    /* loop over sampler units (aka tex image units) */
301    for (su = 0; su < ctx->Const.MaxTextureImageUnits; su++) {
302       struct pipe_sampler_view *sampler_view = NULL;
303       if (fprog->Base.SamplersUsed & (1 << su)) {
304          GLboolean retval;
305          GLuint texUnit;
306
307          texUnit = fprog->Base.SamplerUnits[su];
308
309          retval = update_single_texture(st, &sampler_view, texUnit);
310          if (retval == GL_FALSE)
311             continue;
312
313          st->state.num_textures = su + 1;
314       }
315       pipe_sampler_view_reference(&st->state.sampler_views[su], sampler_view);
316    }
317
318    cso_set_fragment_sampler_views(st->cso_context,
319                                   st->state.num_textures,
320                                   st->state.sampler_views);
321 }
322
323 const struct st_tracked_state st_update_texture = {
324    "st_update_texture",                                 /* name */
325    {                                                    /* dirty */
326       _NEW_TEXTURE,                                     /* mesa */
327       ST_NEW_FRAGMENT_PROGRAM,                          /* st */
328    },
329    update_fragment_textures                             /* update */
330 };
331
332 const struct st_tracked_state st_update_vertex_texture = {
333    "st_update_vertex_texture",                                  /* name */
334    {                                                    /* dirty */
335       _NEW_TEXTURE,                                     /* mesa */
336       ST_NEW_VERTEX_PROGRAM,                            /* st */
337    },
338    update_vertex_textures                               /* update */
339 };
340
341 static void 
342 finalize_textures(struct st_context *st)
343 {
344    struct gl_context *ctx = st->ctx;
345    struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
346    const GLboolean prev_missing_textures = st->missing_textures;
347    GLuint su;
348
349    st->missing_textures = GL_FALSE;
350
351    for (su = 0; su < ctx->Const.MaxTextureCoordUnits; su++) {
352       if (fprog->Base.SamplersUsed & (1 << su)) {
353          const GLuint texUnit = fprog->Base.SamplerUnits[su];
354          struct gl_texture_object *texObj
355             = ctx->Texture.Unit[texUnit]._Current;
356
357          if (texObj) {
358             GLboolean retval;
359
360             retval = st_finalize_texture(ctx, st->pipe, texObj);
361             if (!retval) {
362                /* out of mem */
363                st->missing_textures = GL_TRUE;
364                continue;
365             }
366          }
367       }
368    }
369
370    if (prev_missing_textures != st->missing_textures)
371       st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
372 }
373
374
375
376 const struct st_tracked_state st_finalize_textures = {
377    "st_finalize_textures",              /* name */
378    {                                    /* dirty */
379       _NEW_TEXTURE,                     /* mesa */
380       0,                                /* st */
381    },
382    finalize_textures                    /* update */
383 };