mesa/st: Fix vertex buffer leak
[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 /**
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.
53 *
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.
57 */
58 static GLuint
59 apply_depthmode(enum pipe_format format, GLuint swizzle, GLenum depthmode)
60 {
61 const struct util_format_description *desc =
62 util_format_description(format);
63 unsigned char swiz[4];
64 unsigned i;
65
66 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS ||
67 desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
68 /* Not a depth format. */
69 return swizzle;
70 }
71
72 for (i = 0; i < 4; i++)
73 swiz[i] = GET_SWZ(swizzle, i);
74
75 switch (depthmode) {
76 case GL_LUMINANCE:
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)
82 swiz[i] = SWIZZLE_X;
83 break;
84
85 case GL_INTENSITY:
86 /* Rewrite reads from XYZW to XXXX. */
87 for (i = 0; i < 4; i++)
88 if (swiz[i] <= SWIZZLE_W)
89 swiz[i] = SWIZZLE_X;
90 break;
91
92 case GL_ALPHA:
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)
96 swiz[i] = SWIZZLE_X;
97 else if (swiz[i] < SWIZZLE_W)
98 swiz[i] = SWIZZLE_ZERO;
99 break;
100 case GL_RED:
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;
107 break;
108 }
109
110 return MAKE_SWIZZLE4(swiz[0], swiz[1], swiz[2], swiz[3]);
111 }
112
113
114 /**
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.
118 *
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.
122 */
123 static boolean
124 check_sampler_swizzle(struct pipe_sampler_view *sv,
125 GLuint swizzle, GLenum depthmode)
126 {
127 swizzle = apply_depthmode(sv->texture->format, swizzle, depthmode);
128
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)))
133 return TRUE;
134 return FALSE;
135 }
136
137
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)
142 {
143 struct pipe_sampler_view templ;
144 GLuint swizzle = apply_depthmode(stObj->pt->format,
145 stObj->base._Swizzle,
146 stObj->base.DepthMode);
147
148 u_sampler_view_default_template(&templ,
149 stObj->pt,
150 format);
151
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);
157 }
158
159 return pipe->create_sampler_view(pipe, stObj->pt, &templ);
160 }
161
162
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)
167 {
168 if (!stObj || !stObj->pt) {
169 return NULL;
170 }
171
172 if (!stObj->sampler_view) {
173 stObj->sampler_view =
174 st_create_texture_sampler_view_from_stobj(pipe, stObj, format);
175 }
176
177 return stObj->sampler_view;
178 }
179
180
181 static void
182 update_textures(struct st_context *st)
183 {
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);
189 GLuint su;
190
191 st->state.num_textures = 0;
192
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;
200 GLboolean retval;
201 GLuint texUnit;
202
203 if (fprog->Base.SamplersUsed & (1 << su))
204 texUnit = fprog->Base.SamplerUnits[su];
205 else
206 texUnit = vprog->Base.SamplerUnits[su];
207
208 texObj = st->ctx->Texture.Unit[texUnit]._Current;
209
210 if (!texObj) {
211 texObj = st_get_default_texture(st);
212 }
213 stObj = st_texture_object(texObj);
214
215 retval = st_finalize_texture(st->ctx, st->pipe, texObj);
216 if (!retval) {
217 /* out of mem */
218 continue;
219 }
220
221 /* Determine the format of the texture sampler view */
222 st_view_format = stObj->pt->format;
223 {
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);
229
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.
234 */
235 const gl_format linearFormat =
236 _mesa_get_srgb_format_linear(texFormat);
237 firstImageFormat = st_mesa_format_to_pipe_format(linearFormat);
238 }
239
240 if (firstImageFormat != stObj->pt->format)
241 st_view_format = firstImageFormat;
242 }
243
244 st->state.num_textures = su + 1;
245
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);
253
254 sampler_view = st_get_texture_sampler_view_from_stobj(stObj, pipe, st_view_format);
255 }
256 pipe_sampler_view_reference(&st->state.sampler_views[su], sampler_view);
257 }
258
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);
267 }
268 }
269
270
271 const struct st_tracked_state st_update_texture = {
272 "st_update_texture", /* name */
273 { /* dirty */
274 _NEW_TEXTURE, /* mesa */
275 ST_NEW_FRAGMENT_PROGRAM, /* st */
276 },
277 update_textures /* update */
278 };
279
280
281
282
283 static void
284 finalize_textures(struct st_context *st)
285 {
286 struct gl_fragment_program *fprog = st->ctx->FragmentProgram._Current;
287 const GLboolean prev_missing_textures = st->missing_textures;
288 GLuint su;
289
290 st->missing_textures = GL_FALSE;
291
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;
297
298 if (texObj) {
299 GLboolean retval;
300
301 retval = st_finalize_texture(st->ctx, st->pipe, texObj);
302 if (!retval) {
303 /* out of mem */
304 st->missing_textures = GL_TRUE;
305 continue;
306 }
307 }
308 }
309 }
310
311 if (prev_missing_textures != st->missing_textures)
312 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
313 }
314
315
316
317 const struct st_tracked_state st_finalize_textures = {
318 "st_finalize_textures", /* name */
319 { /* dirty */
320 _NEW_TEXTURE, /* mesa */
321 0, /* st */
322 },
323 finalize_textures /* update */
324 };