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 "main/mtypes.h"
37 #include "main/samplerobj.h"
38 #include "program/prog_instruction.h"
40 #include "st_context.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"
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.
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.
61 apply_depthmode(enum pipe_format format, GLuint swizzle, GLenum depthmode)
63 const struct util_format_description *desc =
64 util_format_description(format);
65 unsigned char swiz[4];
68 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS ||
69 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
70 /* Not a depth format. */
74 for (i = 0; i < 4; i++)
75 swiz[i] = GET_SWZ(swizzle, i);
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)
88 /* Rewrite reads from XYZW to XXXX. */
89 for (i = 0; i < 4; i++)
90 if (swiz[i] <= SWIZZLE_W)
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)
99 else if (swiz[i] < SWIZZLE_W)
100 swiz[i] = SWIZZLE_ZERO;
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;
112 return MAKE_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
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.
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.
126 check_sampler_swizzle(struct pipe_sampler_view *sv,
127 GLuint swizzle, GLenum depthmode)
129 swizzle = apply_depthmode(sv->texture->format, swizzle, depthmode);
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)))
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)
146 struct pipe_sampler_view templ;
147 GLuint swizzle = apply_depthmode(stObj->pt->format,
148 stObj->base._Swizzle,
151 u_sampler_view_default_template(&templ,
154 templ.u.tex.first_level = stObj->base.BaseLevel;
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);
163 return pipe->create_sampler_view(pipe, stObj->pt, &templ);
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)
173 if (!stObj || !stObj->pt) {
177 if (!stObj->sampler_view) {
178 stObj->sampler_view =
179 st_create_texture_sampler_view_from_stobj(pipe, stObj, samp, format);
182 return stObj->sampler_view;
186 update_single_texture(struct st_context *st, struct pipe_sampler_view **sampler_view,
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;
197 samp = _mesa_get_samplerobj(ctx, texUnit);
199 texObj = ctx->Texture.Unit[texUnit]._Current;
202 texObj = st_get_default_texture(st);
203 samp = &texObj->Sampler;
205 stObj = st_texture_object(texObj);
207 retval = st_finalize_texture(ctx, st->pipe, texObj);
213 /* Determine the format of the texture sampler view */
214 st_view_format = stObj->pt->format;
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);
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.
227 const gl_format linearFormat =
228 _mesa_get_srgb_format_linear(texFormat);
229 firstImageFormat = st_mesa_format_to_pipe_format(linearFormat);
232 if (firstImageFormat != stObj->pt->format)
233 st_view_format = firstImageFormat;
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,
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);
248 *sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe,
255 update_vertex_textures(struct st_context *st)
257 const struct gl_context *ctx = st->ctx;
258 struct gl_vertex_program *vprog = ctx->VertexProgram._Current;
261 st->state.num_vertex_textures = 0;
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)) {
270 texUnit = vprog->Base.SamplerUnits[su];
272 retval = update_single_texture(st, &sampler_view, texUnit);
273 if (retval == GL_FALSE)
276 st->state.num_vertex_textures = su + 1;
279 pipe_sampler_view_reference(&st->state.sampler_vertex_views[su], sampler_view);
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,
287 st->state.sampler_vertex_views);
292 update_fragment_textures(struct st_context *st)
294 const struct gl_context *ctx = st->ctx;
295 struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
298 st->state.num_textures = 0;
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)) {
307 texUnit = fprog->Base.SamplerUnits[su];
309 retval = update_single_texture(st, &sampler_view, texUnit);
310 if (retval == GL_FALSE)
313 st->state.num_textures = su + 1;
315 pipe_sampler_view_reference(&st->state.sampler_views[su], sampler_view);
318 cso_set_fragment_sampler_views(st->cso_context,
319 st->state.num_textures,
320 st->state.sampler_views);
323 const struct st_tracked_state st_update_texture = {
324 "st_update_texture", /* name */
326 _NEW_TEXTURE, /* mesa */
327 ST_NEW_FRAGMENT_PROGRAM, /* st */
329 update_fragment_textures /* update */
332 const struct st_tracked_state st_update_vertex_texture = {
333 "st_update_vertex_texture", /* name */
335 _NEW_TEXTURE, /* mesa */
336 ST_NEW_VERTEX_PROGRAM, /* st */
338 update_vertex_textures /* update */
342 finalize_textures(struct st_context *st)
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;
349 st->missing_textures = GL_FALSE;
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;
360 retval = st_finalize_texture(ctx, st->pipe, texObj);
363 st->missing_textures = GL_TRUE;
370 if (prev_missing_textures != st->missing_textures)
371 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
376 const struct st_tracked_state st_finalize_textures = {
377 "st_finalize_textures", /* name */
379 _NEW_TEXTURE, /* mesa */
382 finalize_textures /* update */