1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
35 #include "main/macros.h"
36 #include "program/prog_instruction.h"
38 #include "st_context.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"
50 * Combine depth texture mode with "swizzle" so that depth mode swizzling
51 * takes place before texture swizzling, and return the resulting swizzle.
52 * If the format is not a depth format, return "swizzle" unchanged.
54 * \param format PIPE_FORMAT_*.
55 * \param swizzle Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
56 * \param depthmode One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA, GL_RED.
59 apply_depthmode(enum pipe_format format
, GLuint swizzle
, GLenum depthmode
)
61 const struct util_format_description
*desc
=
62 util_format_description(format
);
63 unsigned char swiz
[4];
66 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
||
67 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_NONE
) {
68 /* Not a depth format. */
72 for (i
= 0; i
< 4; i
++)
73 swiz
[i
] = GET_SWZ(swizzle
, i
);
77 /* Rewrite reads from W to ONE, and reads from XYZ to XXX. */
78 for (i
= 0; i
< 4; i
++)
79 if (swiz
[i
] == SWIZZLE_W
)
80 swiz
[i
] = SWIZZLE_ONE
;
81 else if (swiz
[i
] < SWIZZLE_W
)
86 /* Rewrite reads from XYZW to XXXX. */
87 for (i
= 0; i
< 4; i
++)
88 if (swiz
[i
] <= SWIZZLE_W
)
93 /* Rewrite reads from W to X, and reads from XYZ to 000. */
94 for (i
= 0; i
< 4; i
++)
95 if (swiz
[i
] == SWIZZLE_W
)
97 else if (swiz
[i
] < SWIZZLE_W
)
98 swiz
[i
] = SWIZZLE_ZERO
;
101 /* Rewrite reads W to 1, XYZ to X00 */
102 for (i
= 0; i
< 4; i
++)
103 if (swiz
[i
] == SWIZZLE_W
)
104 swiz
[i
] = SWIZZLE_ONE
;
105 else if (swiz
[i
] == SWIZZLE_Y
|| swiz
[i
] == SWIZZLE_Z
)
106 swiz
[i
] = SWIZZLE_ZERO
;
110 return MAKE_SWIZZLE4(swiz
[0], swiz
[1], swiz
[2], swiz
[3]);
115 * Return TRUE if the swizzling described by "swizzle" and
116 * "depthmode" (for depth textures only) is different from the swizzling
117 * set in the given sampler view.
119 * \param sv A sampler view.
120 * \param swizzle Texture swizzle, a bitmask computed using MAKE_SWIZZLE4.
121 * \param depthmode One of GL_LUMINANCE, GL_INTENSITY, GL_ALPHA.
124 check_sampler_swizzle(struct pipe_sampler_view
*sv
,
125 GLuint swizzle
, GLenum depthmode
)
127 swizzle
= apply_depthmode(sv
->texture
->format
, swizzle
, depthmode
);
129 if ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
130 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
131 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
132 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)))
138 static INLINE
struct pipe_sampler_view
*
139 st_create_texture_sampler_view_from_stobj(struct pipe_context
*pipe
,
140 struct st_texture_object
*stObj
,
141 enum pipe_format format
)
143 struct pipe_sampler_view templ
;
144 GLuint swizzle
= apply_depthmode(stObj
->pt
->format
,
145 stObj
->base
._Swizzle
,
146 stObj
->base
.DepthMode
);
148 u_sampler_view_default_template(&templ
,
152 if (swizzle
!= SWIZZLE_NOOP
) {
153 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
154 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
155 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
156 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
159 return pipe
->create_sampler_view(pipe
, stObj
->pt
, &templ
);
163 static INLINE
struct pipe_sampler_view
*
164 st_get_texture_sampler_view_from_stobj(struct st_texture_object
*stObj
,
165 struct pipe_context
*pipe
,
166 enum pipe_format format
)
168 if (!stObj
|| !stObj
->pt
) {
172 if (!stObj
->sampler_view
) {
173 stObj
->sampler_view
=
174 st_create_texture_sampler_view_from_stobj(pipe
, stObj
, format
);
177 return stObj
->sampler_view
;
182 update_textures(struct st_context
*st
)
184 struct pipe_context
*pipe
= st
->pipe
;
185 struct gl_vertex_program
*vprog
= st
->ctx
->VertexProgram
._Current
;
186 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
187 const GLbitfield samplersUsed
= (vprog
->Base
.SamplersUsed
|
188 fprog
->Base
.SamplersUsed
);
191 st
->state
.num_textures
= 0;
193 /* loop over sampler units (aka tex image units) */
194 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureImageUnits
; su
++) {
195 struct pipe_sampler_view
*sampler_view
= NULL
;
196 enum pipe_format st_view_format
;
197 if (samplersUsed
& (1 << su
)) {
198 struct gl_texture_object
*texObj
;
199 struct st_texture_object
*stObj
;
203 if (fprog
->Base
.SamplersUsed
& (1 << su
))
204 texUnit
= fprog
->Base
.SamplerUnits
[su
];
206 texUnit
= vprog
->Base
.SamplerUnits
[su
];
208 texObj
= st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
211 texObj
= st_get_default_texture(st
);
213 stObj
= st_texture_object(texObj
);
215 retval
= st_finalize_texture(st
->ctx
, st
->pipe
, texObj
);
221 /* Determine the format of the texture sampler view */
222 st_view_format
= stObj
->pt
->format
;
224 const struct st_texture_image
*firstImage
=
225 st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
226 const gl_format texFormat
= firstImage
->base
.TexFormat
;
227 enum pipe_format firstImageFormat
=
228 st_mesa_format_to_pipe_format(texFormat
);
230 if ((stObj
->base
.sRGBDecode
== GL_SKIP_DECODE_EXT
) &&
231 (_mesa_get_format_color_encoding(texFormat
) == GL_SRGB
)) {
232 /* don't do sRGB->RGB conversion. Interpret the texture
233 * texture data as linear values.
235 const gl_format linearFormat
=
236 _mesa_get_srgb_format_linear(texFormat
);
237 firstImageFormat
= st_mesa_format_to_pipe_format(linearFormat
);
240 if (firstImageFormat
!= stObj
->pt
->format
)
241 st_view_format
= firstImageFormat
;
244 st
->state
.num_textures
= su
+ 1;
246 /* if sampler view has changed dereference it */
247 if (stObj
->sampler_view
)
248 if (check_sampler_swizzle(stObj
->sampler_view
,
249 stObj
->base
._Swizzle
,
250 stObj
->base
.DepthMode
) ||
251 (st_view_format
!= stObj
->sampler_view
->format
))
252 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
254 sampler_view
= st_get_texture_sampler_view_from_stobj(stObj
, pipe
, st_view_format
);
256 pipe_sampler_view_reference(&st
->state
.sampler_views
[su
], sampler_view
);
259 cso_set_fragment_sampler_views(st
->cso_context
,
260 st
->state
.num_textures
,
261 st
->state
.sampler_views
);
262 if (st
->ctx
->Const
.MaxVertexTextureImageUnits
> 0) {
263 cso_set_vertex_sampler_views(st
->cso_context
,
264 MIN2(st
->state
.num_textures
,
265 st
->ctx
->Const
.MaxVertexTextureImageUnits
),
266 st
->state
.sampler_views
);
271 const struct st_tracked_state st_update_texture
= {
272 "st_update_texture", /* name */
274 _NEW_TEXTURE
, /* mesa */
275 ST_NEW_FRAGMENT_PROGRAM
, /* st */
277 update_textures
/* update */
284 finalize_textures(struct st_context
*st
)
286 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
287 const GLboolean prev_missing_textures
= st
->missing_textures
;
290 st
->missing_textures
= GL_FALSE
;
292 for (su
= 0; su
< st
->ctx
->Const
.MaxTextureCoordUnits
; su
++) {
293 if (fprog
->Base
.SamplersUsed
& (1 << su
)) {
294 const GLuint texUnit
= fprog
->Base
.SamplerUnits
[su
];
295 struct gl_texture_object
*texObj
296 = st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
301 retval
= st_finalize_texture(st
->ctx
, st
->pipe
, texObj
);
304 st
->missing_textures
= GL_TRUE
;
311 if (prev_missing_textures
!= st
->missing_textures
)
312 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
317 const struct st_tracked_state st_finalize_textures
= {
318 "st_finalize_textures", /* name */
320 _NEW_TEXTURE
, /* mesa */
323 finalize_textures
/* update */