st/mesa: improve sampler view handling
[mesa.git] / src / mesa / state_tracker / st_atom_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
31 * Brian Paul
32 */
33
34
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/samplerobj.h"
38 #include "main/texobj.h"
39 #include "program/prog_instruction.h"
40
41 #include "st_context.h"
42 #include "st_atom.h"
43 #include "st_texture.h"
44 #include "st_format.h"
45 #include "st_cb_texture.h"
46 #include "pipe/p_context.h"
47 #include "util/u_format.h"
48 #include "util/u_inlines.h"
49 #include "cso_cache/cso_context.h"
50
51
52 /**
53 * Return swizzle1(swizzle2)
54 */
55 static unsigned
56 swizzle_swizzle(unsigned swizzle1, unsigned swizzle2)
57 {
58 unsigned i, swz[4];
59
60 for (i = 0; i < 4; i++) {
61 unsigned s = GET_SWZ(swizzle1, i);
62 switch (s) {
63 case SWIZZLE_X:
64 case SWIZZLE_Y:
65 case SWIZZLE_Z:
66 case SWIZZLE_W:
67 swz[i] = GET_SWZ(swizzle2, s);
68 break;
69 case SWIZZLE_ZERO:
70 swz[i] = SWIZZLE_ZERO;
71 break;
72 case SWIZZLE_ONE:
73 swz[i] = SWIZZLE_ONE;
74 break;
75 default:
76 assert(!"Bad swizzle term");
77 swz[i] = SWIZZLE_X;
78 }
79 }
80
81 return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
82 }
83
84
85 /**
86 * Given a user-specified texture base format, the actual gallium texture
87 * format and the current GL_DEPTH_MODE, return a texture swizzle.
88 *
89 * Consider the case where the user requests a GL_RGB internal texture
90 * format the driver actually uses an RGBA format. The A component should
91 * be ignored and sampling from the texture should always return (r,g,b,1).
92 * But if we rendered to the texture we might have written A values != 1.
93 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
94 * This function computes the texture swizzle needed to get the expected
95 * values.
96 *
97 * In the case of depth textures, the GL_DEPTH_MODE state determines the
98 * texture swizzle.
99 *
100 * This result must be composed with the user-specified swizzle to get
101 * the final swizzle.
102 */
103 static unsigned
104 compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
105 enum pipe_format actualFormat)
106 {
107 switch (baseFormat) {
108 case GL_RGBA:
109 return SWIZZLE_XYZW;
110 case GL_RGB:
111 if (util_format_has_alpha(actualFormat))
112 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
113 else
114 return SWIZZLE_XYZW;
115 case GL_RG:
116 if (util_format_get_nr_components(actualFormat) > 2)
117 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
118 else
119 return SWIZZLE_XYZW;
120 case GL_RED:
121 if (util_format_get_nr_components(actualFormat) > 1)
122 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
123 SWIZZLE_ZERO, SWIZZLE_ONE);
124 else
125 return SWIZZLE_XYZW;
126 case GL_ALPHA:
127 if (util_format_get_nr_components(actualFormat) > 1)
128 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
129 SWIZZLE_ZERO, SWIZZLE_W);
130 else
131 return SWIZZLE_XYZW;
132 case GL_LUMINANCE:
133 if (util_format_get_nr_components(actualFormat) > 1)
134 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
135 else
136 return SWIZZLE_XYZW;
137 case GL_LUMINANCE_ALPHA:
138 if (util_format_get_nr_components(actualFormat) > 2)
139 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
140 else
141 return SWIZZLE_XYZW;
142 case GL_INTENSITY:
143 if (util_format_get_nr_components(actualFormat) > 1)
144 return SWIZZLE_XXXX;
145 else
146 return SWIZZLE_XYZW;
147 case GL_STENCIL_INDEX:
148 return SWIZZLE_XYZW;
149 case GL_DEPTH_STENCIL:
150 /* fall-through */
151 case GL_DEPTH_COMPONENT:
152 /* Now examine the depth mode */
153 switch (depthMode) {
154 case GL_LUMINANCE:
155 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
156 case GL_INTENSITY:
157 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
158 case GL_ALPHA:
159 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
160 SWIZZLE_ZERO, SWIZZLE_X);
161 case GL_RED:
162 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
163 SWIZZLE_ZERO, SWIZZLE_ONE);
164 default:
165 assert(!"Unexpected depthMode");
166 return SWIZZLE_XYZW;
167 }
168 default:
169 assert(!"Unexpected baseFormat");
170 return SWIZZLE_XYZW;
171 }
172 }
173
174
175 static unsigned
176 get_texture_format_swizzle(const struct st_texture_object *stObj)
177 {
178 const struct gl_texture_image *texImage =
179 stObj->base.Image[0][stObj->base.BaseLevel];
180 unsigned tex_swizzle;
181
182 if (texImage) {
183 tex_swizzle = compute_texture_format_swizzle(texImage->_BaseFormat,
184 stObj->base.DepthMode,
185 stObj->pt->format);
186 }
187 else {
188 tex_swizzle = SWIZZLE_XYZW;
189 }
190
191 /* Combine the texture format swizzle with user's swizzle */
192 return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle);
193 }
194
195
196 /**
197 * Return TRUE if the texture's sampler view swizzle is equal to
198 * the texture's swizzle.
199 *
200 * \param stObj the st texture object,
201 */
202 static boolean
203 check_sampler_swizzle(const struct st_texture_object *stObj,
204 struct pipe_sampler_view *sv)
205 {
206 unsigned swizzle = get_texture_format_swizzle(stObj);
207
208 return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
209 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
210 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
211 (sv->swizzle_a != GET_SWZ(swizzle, 3)));
212 }
213
214
215 static struct pipe_sampler_view *
216 st_create_texture_sampler_view_from_stobj(struct pipe_context *pipe,
217 struct st_texture_object *stObj,
218 const struct gl_sampler_object *samp,
219 enum pipe_format format)
220 {
221 struct pipe_sampler_view templ;
222 unsigned swizzle = get_texture_format_swizzle(stObj);
223
224 u_sampler_view_default_template(&templ,
225 stObj->pt,
226 format);
227
228 if (stObj->pt->target == PIPE_BUFFER) {
229 unsigned base, size;
230 unsigned f, n;
231 const struct util_format_description *desc
232 = util_format_description(templ.format);
233
234 base = stObj->base.BufferOffset;
235 if (base >= stObj->pt->width0)
236 return NULL;
237 size = MIN2(stObj->pt->width0 - base, (unsigned)stObj->base.BufferSize);
238
239 f = ((base * 8) / desc->block.bits) * desc->block.width;
240 n = ((size * 8) / desc->block.bits) * desc->block.width;
241 if (!n)
242 return NULL;
243 templ.u.buf.first_element = f;
244 templ.u.buf.last_element = f + (n - 1);
245 } else {
246 templ.u.tex.first_level = stObj->base.BaseLevel;
247 }
248
249 if (swizzle != SWIZZLE_NOOP) {
250 templ.swizzle_r = GET_SWZ(swizzle, 0);
251 templ.swizzle_g = GET_SWZ(swizzle, 1);
252 templ.swizzle_b = GET_SWZ(swizzle, 2);
253 templ.swizzle_a = GET_SWZ(swizzle, 3);
254 }
255
256 return pipe->create_sampler_view(pipe, stObj->pt, &templ);
257 }
258
259
260 static struct pipe_sampler_view *
261 st_get_texture_sampler_view_from_stobj(struct st_context *st,
262 struct st_texture_object *stObj,
263 const struct gl_sampler_object *samp,
264 enum pipe_format format)
265 {
266 struct pipe_sampler_view **sv;
267
268 if (!stObj || !stObj->pt) {
269 return NULL;
270 }
271
272 sv = st_texture_get_sampler_view(st, stObj);
273
274 /* if sampler view has changed dereference it */
275 if (*sv) {
276 if (check_sampler_swizzle(stObj, *sv) ||
277 (format != (*sv)->format) ||
278 stObj->base.BaseLevel != (*sv)->u.tex.first_level) {
279 pipe_sampler_view_reference(sv, NULL);
280 }
281 }
282
283 if (!*sv) {
284 *sv = st_create_texture_sampler_view_from_stobj(st->pipe, stObj, samp, format);
285
286 } else if ((*sv)->context != st->pipe) {
287 /* Recreate view in correct context, use existing view as template */
288 struct pipe_sampler_view *new_sv =
289 st->pipe->create_sampler_view(st->pipe, stObj->pt, *sv);
290 pipe_sampler_view_reference(sv, NULL);
291 *sv = new_sv;
292 }
293
294 return *sv;
295 }
296
297 static GLboolean
298 update_single_texture(struct st_context *st,
299 struct pipe_sampler_view **sampler_view,
300 GLuint texUnit)
301 {
302 struct gl_context *ctx = st->ctx;
303 const struct gl_sampler_object *samp;
304 struct gl_texture_object *texObj;
305 struct st_texture_object *stObj;
306 enum pipe_format view_format;
307 GLboolean retval;
308
309 samp = _mesa_get_samplerobj(ctx, texUnit);
310
311 texObj = ctx->Texture.Unit[texUnit]._Current;
312
313 if (!texObj) {
314 texObj = _mesa_get_fallback_texture(ctx, TEXTURE_2D_INDEX);
315 samp = &texObj->Sampler;
316 }
317 stObj = st_texture_object(texObj);
318
319 retval = st_finalize_texture(ctx, st->pipe, texObj);
320 if (!retval) {
321 /* out of mem */
322 return GL_FALSE;
323 }
324
325 /* Determine the format of the texture sampler view */
326 if (texObj->Target == GL_TEXTURE_BUFFER) {
327 view_format =
328 st_mesa_format_to_pipe_format(stObj->base._BufferObjectFormat);
329 }
330 else {
331 view_format =
332 stObj->surface_based ? stObj->surface_format : stObj->pt->format;
333
334 /* If sRGB decoding is off, use the linear format */
335 if (samp->sRGBDecode == GL_SKIP_DECODE_EXT) {
336 view_format = util_format_linear(view_format);
337 }
338 }
339
340 *sampler_view = st_get_texture_sampler_view_from_stobj(st, stObj, samp,
341 view_format);
342 return GL_TRUE;
343 }
344
345
346
347 static void
348 update_textures(struct st_context *st,
349 unsigned shader_stage,
350 const struct gl_program *prog,
351 unsigned max_units,
352 struct pipe_sampler_view **sampler_views,
353 unsigned *num_textures)
354 {
355 const GLuint old_max = *num_textures;
356 GLbitfield samplers_used = prog->SamplersUsed;
357 GLuint unit, new_count;
358
359 if (samplers_used == 0x0 && old_max == 0)
360 return;
361
362 *num_textures = 0;
363
364 /* loop over sampler units (aka tex image units) */
365 for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
366 struct pipe_sampler_view *sampler_view = NULL;
367
368 if (samplers_used & 1) {
369 const GLuint texUnit = prog->SamplerUnits[unit];
370 GLboolean retval;
371
372 retval = update_single_texture(st, &sampler_view, texUnit);
373 if (retval == GL_FALSE)
374 continue;
375
376 *num_textures = unit + 1;
377 }
378 else if (samplers_used == 0 && unit >= old_max) {
379 /* if we've reset all the old views and we have no more new ones */
380 break;
381 }
382
383 pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
384 }
385
386 /* Ex: if old_max = 3 and *num_textures = 1, we need to pass an
387 * array of views={X, NULL, NULL} to unref the old texture views
388 * at positions [1] and [2].
389 */
390 new_count = MAX2(*num_textures, old_max);
391 assert(new_count <= max_units);
392
393 cso_set_sampler_views(st->cso_context,
394 shader_stage,
395 new_count,
396 sampler_views);
397 }
398
399
400
401 static void
402 update_vertex_textures(struct st_context *st)
403 {
404 const struct gl_context *ctx = st->ctx;
405
406 if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
407 update_textures(st,
408 PIPE_SHADER_VERTEX,
409 &ctx->VertexProgram._Current->Base,
410 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
411 st->state.sampler_views[PIPE_SHADER_VERTEX],
412 &st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
413 }
414 }
415
416
417 static void
418 update_fragment_textures(struct st_context *st)
419 {
420 const struct gl_context *ctx = st->ctx;
421
422 update_textures(st,
423 PIPE_SHADER_FRAGMENT,
424 &ctx->FragmentProgram._Current->Base,
425 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
426 st->state.sampler_views[PIPE_SHADER_FRAGMENT],
427 &st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
428 }
429
430
431 static void
432 update_geometry_textures(struct st_context *st)
433 {
434 const struct gl_context *ctx = st->ctx;
435
436 if (ctx->GeometryProgram._Current) {
437 update_textures(st,
438 PIPE_SHADER_GEOMETRY,
439 &ctx->GeometryProgram._Current->Base,
440 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
441 st->state.sampler_views[PIPE_SHADER_GEOMETRY],
442 &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
443 }
444 }
445
446
447 const struct st_tracked_state st_update_fragment_texture = {
448 "st_update_texture", /* name */
449 { /* dirty */
450 _NEW_TEXTURE, /* mesa */
451 ST_NEW_FRAGMENT_PROGRAM, /* st */
452 },
453 update_fragment_textures /* update */
454 };
455
456
457 const struct st_tracked_state st_update_vertex_texture = {
458 "st_update_vertex_texture", /* name */
459 { /* dirty */
460 _NEW_TEXTURE, /* mesa */
461 ST_NEW_VERTEX_PROGRAM, /* st */
462 },
463 update_vertex_textures /* update */
464 };
465
466
467 const struct st_tracked_state st_update_geometry_texture = {
468 "st_update_geometry_texture", /* name */
469 { /* dirty */
470 _NEW_TEXTURE, /* mesa */
471 ST_NEW_GEOMETRY_PROGRAM, /* st */
472 },
473 update_geometry_textures /* update */
474 };
475
476
477
478 static void
479 finalize_textures(struct st_context *st)
480 {
481 struct gl_context *ctx = st->ctx;
482 struct gl_fragment_program *fprog = ctx->FragmentProgram._Current;
483 const GLboolean prev_missing_textures = st->missing_textures;
484 GLuint su;
485
486 st->missing_textures = GL_FALSE;
487
488 for (su = 0; su < ctx->Const.MaxTextureCoordUnits; su++) {
489 if (fprog->Base.SamplersUsed & (1 << su)) {
490 const GLuint texUnit = fprog->Base.SamplerUnits[su];
491 struct gl_texture_object *texObj
492 = ctx->Texture.Unit[texUnit]._Current;
493
494 if (texObj) {
495 GLboolean retval;
496
497 retval = st_finalize_texture(ctx, st->pipe, texObj);
498 if (!retval) {
499 /* out of mem */
500 st->missing_textures = GL_TRUE;
501 continue;
502 }
503 }
504 }
505 }
506
507 if (prev_missing_textures != st->missing_textures)
508 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
509 }
510
511
512 const struct st_tracked_state st_finalize_textures = {
513 "st_finalize_textures", /* name */
514 { /* dirty */
515 _NEW_TEXTURE, /* mesa */
516 0, /* st */
517 },
518 finalize_textures /* update */
519 };