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"
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.
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.
57 static GLuint
apply_depthmode(enum pipe_format format
,
58 GLuint swizzle
, GLenum depthmode
)
60 const struct util_format_description
*desc
=
61 util_format_description(format
);
62 unsigned char swiz
[4];
65 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
||
66 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_NONE
) {
67 /* Not a depth format. */
71 for (i
= 0; i
< 4; i
++)
72 swiz
[i
] = GET_SWZ(swizzle
, i
);
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
)
85 /* Rewrite reads from XYZW to XXXX. */
86 for (i
= 0; i
< 4; i
++)
87 if (swiz
[i
] <= SWIZZLE_W
)
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
)
96 else if (swiz
[i
] < SWIZZLE_W
)
97 swiz
[i
] = SWIZZLE_ZERO
;
101 return MAKE_SWIZZLE4(swiz
[0], swiz
[1], swiz
[2], swiz
[3]);
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.
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.
113 static boolean
check_sampler_swizzle(struct pipe_sampler_view
*sv
,
114 GLuint swizzle
, GLenum depthmode
)
116 swizzle
= apply_depthmode(sv
->texture
->format
, swizzle
, depthmode
);
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)))
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
)
132 struct pipe_sampler_view templ
;
133 GLuint swizzle
= apply_depthmode(stObj
->pt
->format
,
134 stObj
->base
._Swizzle
,
135 stObj
->base
.DepthMode
);
137 u_sampler_view_default_template(&templ
,
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);
148 return pipe
->create_sampler_view(pipe
, stObj
->pt
, &templ
);
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
)
158 if (!stObj
|| !stObj
->pt
) {
162 if (!stObj
->sampler_view
) {
163 stObj
->sampler_view
= st_create_texture_sampler_view_from_stobj(pipe
, stObj
, format
);
166 return stObj
->sampler_view
;
170 update_textures(struct st_context
*st
)
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
);
179 st
->state
.num_textures
= 0;
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
;
191 if (fprog
->Base
.SamplersUsed
& (1 << su
))
192 texUnit
= fprog
->Base
.SamplerUnits
[su
];
194 texUnit
= vprog
->Base
.SamplerUnits
[su
];
196 texObj
= st
->ctx
->Texture
.Unit
[texUnit
]._Current
;
199 texObj
= st_get_default_texture(st
);
201 stObj
= st_texture_object(texObj
);
203 retval
= st_finalize_texture(st
->ctx
, st
->pipe
, texObj
);
209 st_view_format
= stObj
->pt
->format
;
211 struct st_texture_image
*firstImage
;
212 enum pipe_format firstImageFormat
;
213 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
215 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
216 if (firstImageFormat
!= stObj
->pt
->format
)
217 st_view_format
= firstImageFormat
;
220 st
->state
.num_textures
= su
+ 1;
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
);
230 sampler_view
= st_get_texture_sampler_view_from_stobj(stObj
, pipe
, st_view_format
);
232 pipe_sampler_view_reference(&st
->state
.sampler_views
[su
], sampler_view
);
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
);
247 const struct st_tracked_state st_update_texture
= {
248 "st_update_texture", /* name */
250 _NEW_TEXTURE
, /* mesa */
251 ST_NEW_FRAGMENT_PROGRAM
, /* st */
253 update_textures
/* update */
260 finalize_textures(struct st_context
*st
)
262 struct gl_fragment_program
*fprog
= st
->ctx
->FragmentProgram
._Current
;
263 const GLboolean prev_missing_textures
= st
->missing_textures
;
266 st
->missing_textures
= GL_FALSE
;
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
;
277 retval
= st_finalize_texture(st
->ctx
, st
->pipe
, texObj
);
280 st
->missing_textures
= GL_TRUE
;
287 if (prev_missing_textures
!= st
->missing_textures
)
288 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
293 const struct st_tracked_state st_finalize_textures
= {
294 "st_finalize_textures", /* name */
296 _NEW_TEXTURE
, /* mesa */
299 finalize_textures
/* update */