• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 VMware, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "pipe/p_context.h"
27 #include "util/format/u_format.h"
28 #include "util/u_inlines.h"
29 
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
33 #include "main/teximage.h"
34 #include "main/texobj.h"
35 #include "program/prog_instruction.h"
36 
37 #include "st_context.h"
38 #include "st_sampler_view.h"
39 #include "st_texture.h"
40 #include "st_format.h"
41 #include "st_cb_bufferobjects.h"
42 #include "st_cb_texture.h"
43 
44 /* Subtract remaining private references. Typically used before
45  * destruction. See the header file for explanation.
46  */
47 static void
st_remove_private_references(struct st_sampler_view * sv)48 st_remove_private_references(struct st_sampler_view *sv)
49 {
50    if (sv->private_refcount) {
51       assert(sv->private_refcount > 0);
52       p_atomic_add(&sv->view->reference.count, -sv->private_refcount);
53       sv->private_refcount = 0;
54    }
55 }
56 
57 /* Return a sampler view while incrementing the refcount by 1. */
58 static struct pipe_sampler_view *
get_sampler_view_reference(struct st_sampler_view * sv,struct pipe_sampler_view * view)59 get_sampler_view_reference(struct st_sampler_view *sv,
60                            struct pipe_sampler_view *view)
61 {
62    if (unlikely(sv->private_refcount <= 0)) {
63       assert(sv->private_refcount == 0);
64 
65       /* This is the number of atomic increments we will skip. */
66       sv->private_refcount = 100000000;
67       p_atomic_add(&view->reference.count, sv->private_refcount);
68    }
69 
70    /* Return a reference while decrementing the private refcount. */
71    sv->private_refcount--;
72    return view;
73 }
74 
75 /**
76  * Set the given view as the current context's view for the texture.
77  *
78  * Overwrites any pre-existing view of the context.
79  *
80  * Takes ownership of the view (i.e., stores the view without incrementing the
81  * reference count).
82  *
83  * \return the view, or NULL on error. In case of error, the reference to the
84  * view is released.
85  */
86 static struct pipe_sampler_view *
st_texture_set_sampler_view(struct st_context * st,struct st_texture_object * stObj,struct pipe_sampler_view * view,bool glsl130_or_later,bool srgb_skip_decode,bool get_reference)87 st_texture_set_sampler_view(struct st_context *st,
88                             struct st_texture_object *stObj,
89                             struct pipe_sampler_view *view,
90                             bool glsl130_or_later, bool srgb_skip_decode,
91                             bool get_reference)
92 {
93    struct st_sampler_views *views;
94    struct st_sampler_view *free = NULL;
95    struct st_sampler_view *sv;
96    GLuint i;
97 
98    simple_mtx_lock(&stObj->validate_mutex);
99    views = stObj->sampler_views;
100 
101    for (i = 0; i < views->count; ++i) {
102       sv = &views->views[i];
103 
104       /* Is the array entry used ? */
105       if (sv->view) {
106          /* check if the context matches */
107          if (sv->view->context == st->pipe) {
108             st_remove_private_references(sv);
109             pipe_sampler_view_reference(&sv->view, NULL);
110             goto found;
111          }
112       } else {
113          /* Found a free slot, remember that */
114          free = sv;
115       }
116    }
117 
118    /* Couldn't find a slot for our context, create a new one */
119    if (free) {
120       sv = free;
121    } else {
122       if (views->count >= views->max) {
123          /* Allocate a larger container. */
124          unsigned new_max = 2 * views->max;
125          unsigned new_size = sizeof(*views) + new_max * sizeof(views->views[0]);
126 
127          if (new_max < views->max ||
128              new_max > (UINT_MAX - sizeof(*views)) / sizeof(views->views[0])) {
129             pipe_sampler_view_reference(&view, NULL);
130             goto out;
131          }
132 
133          struct st_sampler_views *new_views = malloc(new_size);
134          if (!new_views) {
135             pipe_sampler_view_reference(&view, NULL);
136             goto out;
137          }
138 
139          new_views->count = views->count;
140          new_views->max = new_max;
141          memcpy(&new_views->views[0], &views->views[0],
142                views->count * sizeof(views->views[0]));
143 
144          /* Initialize the pipe_sampler_view pointers to zero so that we don't
145           * have to worry about racing against readers when incrementing
146           * views->count.
147           */
148          memset(&new_views->views[views->count], 0,
149                 (new_max - views->count) * sizeof(views->views[0]));
150 
151          /* Use memory release semantics to ensure that concurrent readers will
152           * get the correct contents of the new container.
153           *
154           * Also, the write should be atomic, but that's guaranteed anyway on
155           * all supported platforms.
156           */
157          p_atomic_set(&stObj->sampler_views, new_views);
158 
159          /* We keep the old container around until the texture object is
160           * deleted, because another thread may still be reading from it. We
161           * double the size of the container each time, so we end up with
162           * at most twice the total memory allocation.
163           */
164          views->next = stObj->sampler_views_old;
165          stObj->sampler_views_old = views;
166 
167          views = new_views;
168       }
169 
170       sv = &views->views[views->count];
171 
172       /* Since modification is guarded by the lock, only the write part of the
173        * increment has to be atomic, and that's already guaranteed on all
174        * supported platforms without using an atomic intrinsic.
175        */
176       views->count++;
177    }
178 
179 found:
180    assert(sv->view == NULL);
181 
182    sv->glsl130_or_later = glsl130_or_later;
183    sv->srgb_skip_decode = srgb_skip_decode;
184    sv->view = view;
185    sv->st = st;
186 
187    if (get_reference)
188       view = get_sampler_view_reference(sv, view);
189 
190 out:
191    simple_mtx_unlock(&stObj->validate_mutex);
192    return view;
193 }
194 
195 
196 /**
197  * Return the most-recently validated sampler view for the texture \p stObj
198  * in the given context, if any.
199  *
200  * Performs no additional validation.
201  */
202 struct st_sampler_view *
st_texture_get_current_sampler_view(const struct st_context * st,const struct st_texture_object * stObj)203 st_texture_get_current_sampler_view(const struct st_context *st,
204                                     const struct st_texture_object *stObj)
205 {
206    struct st_sampler_views *views = p_atomic_read(&stObj->sampler_views);
207 
208    for (unsigned i = 0; i < views->count; ++i) {
209       struct st_sampler_view *sv = &views->views[i];
210       if (sv->view && sv->view->context == st->pipe)
211          return sv;
212    }
213 
214    return NULL;
215 }
216 
217 
218 /**
219  * For the given texture object, release any sampler views which belong
220  * to the calling context.  This is used to free any sampler views
221  * which belong to the context before the context is destroyed.
222  */
223 void
st_texture_release_context_sampler_view(struct st_context * st,struct st_texture_object * stObj)224 st_texture_release_context_sampler_view(struct st_context *st,
225                                         struct st_texture_object *stObj)
226 {
227    GLuint i;
228 
229    simple_mtx_lock(&stObj->validate_mutex);
230    struct st_sampler_views *views = stObj->sampler_views;
231    for (i = 0; i < views->count; ++i) {
232       struct st_sampler_view *sv = &views->views[i];
233 
234       if (sv->view && sv->view->context == st->pipe) {
235          st_remove_private_references(sv);
236          pipe_sampler_view_reference(&sv->view, NULL);
237          break;
238       }
239    }
240    simple_mtx_unlock(&stObj->validate_mutex);
241 }
242 
243 
244 /**
245  * Release all sampler views attached to the given texture object, regardless
246  * of the context.  This is called fairly frequently.  For example, whenever
247  * the texture's base level, max level or swizzle change.
248  */
249 void
st_texture_release_all_sampler_views(struct st_context * st,struct st_texture_object * stObj)250 st_texture_release_all_sampler_views(struct st_context *st,
251                                      struct st_texture_object *stObj)
252 {
253    /* TODO: This happens while a texture is deleted, because the Driver API
254     * is asymmetric: the driver allocates the texture object memory, but
255     * mesa/main frees it.
256     */
257    if (!stObj->sampler_views)
258       return;
259 
260    simple_mtx_lock(&stObj->validate_mutex);
261    struct st_sampler_views *views = stObj->sampler_views;
262    for (unsigned i = 0; i < views->count; ++i) {
263       struct st_sampler_view *stsv = &views->views[i];
264       if (stsv->view) {
265          st_remove_private_references(stsv);
266 
267          if (stsv->st && stsv->st != st) {
268             /* Transfer this reference to the zombie list.  It will
269              * likely be freed when the zombie list is freed.
270              */
271             st_save_zombie_sampler_view(stsv->st, stsv->view);
272             stsv->view = NULL;
273          } else {
274             pipe_sampler_view_reference(&stsv->view, NULL);
275          }
276       }
277    }
278    views->count = 0;
279    simple_mtx_unlock(&stObj->validate_mutex);
280 }
281 
282 
283 /*
284  * Delete the texture's sampler views and st_sampler_views containers.
285  * This is to be called just before a texture is deleted.
286  */
287 void
st_delete_texture_sampler_views(struct st_context * st,struct st_texture_object * stObj)288 st_delete_texture_sampler_views(struct st_context *st,
289                                 struct st_texture_object *stObj)
290 {
291    st_texture_release_all_sampler_views(st, stObj);
292 
293    /* Free the container of the current per-context sampler views */
294    assert(stObj->sampler_views->count == 0);
295    free(stObj->sampler_views);
296    stObj->sampler_views = NULL;
297 
298    /* Free old sampler view containers */
299    while (stObj->sampler_views_old) {
300       struct st_sampler_views *views = stObj->sampler_views_old;
301       stObj->sampler_views_old = views->next;
302       free(views);
303    }
304 }
305 
306 
307 /**
308  * Return swizzle1(swizzle2)
309  */
310 static unsigned
swizzle_swizzle(unsigned swizzle1,unsigned swizzle2)311 swizzle_swizzle(unsigned swizzle1, unsigned swizzle2)
312 {
313    unsigned i, swz[4];
314 
315    if (swizzle1 == SWIZZLE_XYZW) {
316       /* identity swizzle, no change to swizzle2 */
317       return swizzle2;
318    }
319 
320    for (i = 0; i < 4; i++) {
321       unsigned s = GET_SWZ(swizzle1, i);
322       switch (s) {
323       case SWIZZLE_X:
324       case SWIZZLE_Y:
325       case SWIZZLE_Z:
326       case SWIZZLE_W:
327          swz[i] = GET_SWZ(swizzle2, s);
328          break;
329       case SWIZZLE_ZERO:
330          swz[i] = SWIZZLE_ZERO;
331          break;
332       case SWIZZLE_ONE:
333          swz[i] = SWIZZLE_ONE;
334          break;
335       default:
336          assert(!"Bad swizzle term");
337          swz[i] = SWIZZLE_X;
338       }
339    }
340 
341    return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
342 }
343 
344 
345 /**
346  * Given a user-specified texture base format, the actual gallium texture
347  * format and the current GL_DEPTH_MODE, return a texture swizzle.
348  *
349  * Consider the case where the user requests a GL_RGB internal texture
350  * format the driver actually uses an RGBA format.  The A component should
351  * be ignored and sampling from the texture should always return (r,g,b,1).
352  * But if we rendered to the texture we might have written A values != 1.
353  * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
354  * This function computes the texture swizzle needed to get the expected
355  * values.
356  *
357  * In the case of depth textures, the GL_DEPTH_MODE state determines the
358  * texture swizzle.
359  *
360  * This result must be composed with the user-specified swizzle to get
361  * the final swizzle.
362  */
363 static unsigned
compute_texture_format_swizzle(GLenum baseFormat,GLenum depthMode,bool glsl130_or_later)364 compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
365                                bool glsl130_or_later)
366 {
367    switch (baseFormat) {
368    case GL_RGBA:
369       return SWIZZLE_XYZW;
370    case GL_RGB:
371       return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
372    case GL_RG:
373       return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
374    case GL_RED:
375       return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
376                            SWIZZLE_ZERO, SWIZZLE_ONE);
377    case GL_ALPHA:
378       return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
379                            SWIZZLE_ZERO, SWIZZLE_W);
380    case GL_LUMINANCE:
381       return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
382    case GL_LUMINANCE_ALPHA:
383       return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
384    case GL_INTENSITY:
385       return SWIZZLE_XXXX;
386    case GL_STENCIL_INDEX:
387    case GL_DEPTH_STENCIL:
388    case GL_DEPTH_COMPONENT:
389       /* Now examine the depth mode */
390       switch (depthMode) {
391       case GL_LUMINANCE:
392          return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
393       case GL_INTENSITY:
394          return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
395       case GL_ALPHA:
396          /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
397           * the depth mode and return float, while older shadow* functions
398           * and ARB_fp instructions return vec4 according to the depth mode.
399           *
400           * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
401           * them to return 0, breaking them completely.
402           *
403           * A proper fix would increase code complexity and that's not worth
404           * it for a rarely used feature such as the GL_ALPHA depth mode
405           * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
406           * shaders that use GLSL 1.30 or later.
407           *
408           * BTW, it's required that sampler views are updated when
409           * shaders change (check_sampler_swizzle takes care of that).
410           */
411          if (glsl130_or_later)
412             return SWIZZLE_XXXX;
413          else
414             return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
415                                  SWIZZLE_ZERO, SWIZZLE_X);
416       case GL_RED:
417          return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
418                               SWIZZLE_ZERO, SWIZZLE_ONE);
419       default:
420          assert(!"Unexpected depthMode");
421          return SWIZZLE_XYZW;
422       }
423    default:
424       assert(!"Unexpected baseFormat");
425       return SWIZZLE_XYZW;
426    }
427 }
428 
429 
430 static unsigned
get_texture_format_swizzle(const struct st_context * st,const struct st_texture_object * stObj,bool glsl130_or_later)431 get_texture_format_swizzle(const struct st_context *st,
432                            const struct st_texture_object *stObj,
433                            bool glsl130_or_later)
434 {
435    GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
436    unsigned tex_swizzle;
437    GLenum depth_mode = stObj->base.Attrib.DepthMode;
438 
439    /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
440     * with depth component data specified with a sized internal format.
441     */
442    if (_mesa_is_gles3(st->ctx) &&
443        (baseFormat == GL_DEPTH_COMPONENT ||
444         baseFormat == GL_DEPTH_STENCIL ||
445         baseFormat == GL_STENCIL_INDEX)) {
446       const struct gl_texture_image *firstImage =
447          _mesa_base_tex_image(&stObj->base);
448       if (firstImage->InternalFormat != GL_DEPTH_COMPONENT &&
449           firstImage->InternalFormat != GL_DEPTH_STENCIL &&
450           firstImage->InternalFormat != GL_STENCIL_INDEX)
451          depth_mode = GL_RED;
452    }
453    tex_swizzle = compute_texture_format_swizzle(baseFormat,
454                                                 depth_mode,
455                                                 glsl130_or_later);
456 
457    /* Combine the texture format swizzle with user's swizzle */
458    return swizzle_swizzle(stObj->base.Attrib._Swizzle, tex_swizzle);
459 }
460 
461 
462 /**
463  * Return TRUE if the texture's sampler view swizzle is not equal to
464  * the texture's swizzle.
465  *
466  * \param stObj  the st texture object,
467  */
468 ASSERTED static boolean
check_sampler_swizzle(const struct st_context * st,const struct st_texture_object * stObj,const struct pipe_sampler_view * sv,bool glsl130_or_later)469 check_sampler_swizzle(const struct st_context *st,
470                       const struct st_texture_object *stObj,
471                       const struct pipe_sampler_view *sv,
472                       bool glsl130_or_later)
473 {
474    unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later);
475 
476    return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
477            (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
478            (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
479            (sv->swizzle_a != GET_SWZ(swizzle, 3)));
480 }
481 
482 
483 static unsigned
last_level(const struct st_texture_object * stObj)484 last_level(const struct st_texture_object *stObj)
485 {
486    unsigned ret = MIN2(stObj->base.Attrib.MinLevel + stObj->base._MaxLevel,
487                        stObj->pt->last_level);
488    if (stObj->base.Immutable)
489       ret = MIN2(ret, stObj->base.Attrib.MinLevel +
490                  stObj->base.Attrib.NumLevels - 1);
491    return ret;
492 }
493 
494 
495 static unsigned
last_layer(const struct st_texture_object * stObj)496 last_layer(const struct st_texture_object *stObj)
497 {
498    if (stObj->base.Immutable && stObj->pt->array_size > 1)
499       return MIN2(stObj->base.Attrib.MinLayer +
500                   stObj->base.Attrib.NumLayers - 1,
501                   stObj->pt->array_size - 1);
502    return stObj->pt->array_size - 1;
503 }
504 
505 
506 /**
507  * Determine the format for the texture sampler view.
508  */
509 static enum pipe_format
get_sampler_view_format(struct st_context * st,const struct st_texture_object * stObj,bool srgb_skip_decode)510 get_sampler_view_format(struct st_context *st,
511                         const struct st_texture_object *stObj,
512                         bool srgb_skip_decode)
513 {
514    enum pipe_format format;
515 
516    GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
517    format = stObj->surface_based ? stObj->surface_format : stObj->pt->format;
518 
519    if (baseFormat == GL_DEPTH_COMPONENT ||
520        baseFormat == GL_DEPTH_STENCIL ||
521        baseFormat == GL_STENCIL_INDEX) {
522       if (stObj->base.StencilSampling || baseFormat == GL_STENCIL_INDEX)
523          format = util_format_stencil_only(format);
524 
525       return format;
526    }
527 
528    /* If sRGB decoding is off, use the linear format */
529    if (srgb_skip_decode)
530       format = util_format_linear(format);
531 
532    /* if resource format matches then YUV wasn't lowered */
533    if (format == stObj->pt->format)
534       return format;
535 
536    /* Use R8_UNORM for video formats */
537    switch (format) {
538    case PIPE_FORMAT_NV12:
539       if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
540          format = PIPE_FORMAT_R8_G8B8_420_UNORM;
541          break;
542       }
543       FALLTHROUGH;
544    case PIPE_FORMAT_IYUV:
545       format = PIPE_FORMAT_R8_UNORM;
546       break;
547    case PIPE_FORMAT_P010:
548    case PIPE_FORMAT_P012:
549    case PIPE_FORMAT_P016:
550       format = PIPE_FORMAT_R16_UNORM;
551       break;
552    case PIPE_FORMAT_Y210:
553    case PIPE_FORMAT_Y212:
554    case PIPE_FORMAT_Y216:
555       format = PIPE_FORMAT_R16G16_UNORM;
556       break;
557    case PIPE_FORMAT_Y410:
558       format = PIPE_FORMAT_R10G10B10A2_UNORM;
559       break;
560    case PIPE_FORMAT_Y412:
561    case PIPE_FORMAT_Y416:
562       format = PIPE_FORMAT_R16G16B16A16_UNORM;
563       break;
564    case PIPE_FORMAT_YUYV:
565    case PIPE_FORMAT_UYVY:
566       if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM ||
567           stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
568          format = stObj->pt->format;
569          break;
570       }
571       format = PIPE_FORMAT_R8G8_UNORM;
572       break;
573    case PIPE_FORMAT_AYUV:
574       format = PIPE_FORMAT_RGBA8888_UNORM;
575       break;
576    case PIPE_FORMAT_XYUV:
577       format = PIPE_FORMAT_RGBX8888_UNORM;
578       break;
579    default:
580       break;
581    }
582    return format;
583 }
584 
585 
586 static struct pipe_sampler_view *
st_create_texture_sampler_view_from_stobj(struct st_context * st,struct st_texture_object * stObj,enum pipe_format format,bool glsl130_or_later)587 st_create_texture_sampler_view_from_stobj(struct st_context *st,
588                                           struct st_texture_object *stObj,
589                                           enum pipe_format format,
590                                           bool glsl130_or_later)
591 {
592    /* There is no need to clear this structure (consider CPU overhead). */
593    struct pipe_sampler_view templ;
594    unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later);
595 
596    templ.format = format;
597 
598    if (stObj->level_override >= 0) {
599       templ.u.tex.first_level = templ.u.tex.last_level = stObj->level_override;
600    } else {
601       templ.u.tex.first_level = stObj->base.Attrib.MinLevel +
602                                 stObj->base.Attrib.BaseLevel;
603       templ.u.tex.last_level = last_level(stObj);
604    }
605    if (stObj->layer_override >= 0) {
606       templ.u.tex.first_layer = templ.u.tex.last_layer = stObj->layer_override;
607    } else {
608       templ.u.tex.first_layer = stObj->base.Attrib.MinLayer;
609       templ.u.tex.last_layer = last_layer(stObj);
610    }
611    assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
612    assert(templ.u.tex.first_level <= templ.u.tex.last_level);
613    templ.target = gl_target_to_pipe(stObj->base.Target);
614 
615    templ.swizzle_r = GET_SWZ(swizzle, 0);
616    templ.swizzle_g = GET_SWZ(swizzle, 1);
617    templ.swizzle_b = GET_SWZ(swizzle, 2);
618    templ.swizzle_a = GET_SWZ(swizzle, 3);
619 
620    return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ);
621 }
622 
623 struct pipe_sampler_view *
st_get_texture_sampler_view_from_stobj(struct st_context * st,struct st_texture_object * stObj,const struct gl_sampler_object * samp,bool glsl130_or_later,bool ignore_srgb_decode,bool get_reference)624 st_get_texture_sampler_view_from_stobj(struct st_context *st,
625                                        struct st_texture_object *stObj,
626                                        const struct gl_sampler_object *samp,
627                                        bool glsl130_or_later,
628                                        bool ignore_srgb_decode,
629                                        bool get_reference)
630 {
631    struct st_sampler_view *sv;
632    bool srgb_skip_decode = false;
633 
634    if (!ignore_srgb_decode && samp->Attrib.sRGBDecode == GL_SKIP_DECODE_EXT)
635       srgb_skip_decode = true;
636 
637    sv = st_texture_get_current_sampler_view(st, stObj);
638 
639    if (sv &&
640        sv->glsl130_or_later == glsl130_or_later &&
641        sv->srgb_skip_decode == srgb_skip_decode) {
642       /* Debug check: make sure that the sampler view's parameters are
643        * what they're supposed to be.
644        */
645       struct pipe_sampler_view *view = sv->view;
646       assert(stObj->pt == view->texture);
647       assert(!check_sampler_swizzle(st, stObj, view, glsl130_or_later));
648       assert(get_sampler_view_format(st, stObj, srgb_skip_decode) == view->format);
649       assert(gl_target_to_pipe(stObj->base.Target) == view->target);
650       assert(stObj->level_override >= 0 ||
651              stObj->base.Attrib.MinLevel +
652              stObj->base.Attrib.BaseLevel == view->u.tex.first_level);
653       assert(stObj->level_override >= 0 || last_level(stObj) == view->u.tex.last_level);
654       assert(stObj->layer_override >= 0 ||
655              stObj->base.Attrib.MinLayer == view->u.tex.first_layer);
656       assert(stObj->layer_override >= 0 || last_layer(stObj) == view->u.tex.last_layer);
657       assert(stObj->layer_override < 0 ||
658              (stObj->layer_override == view->u.tex.first_layer &&
659               stObj->layer_override == view->u.tex.last_layer));
660       if (get_reference)
661          view = get_sampler_view_reference(sv, view);
662       return view;
663    }
664 
665    /* create new sampler view */
666    enum pipe_format format = get_sampler_view_format(st, stObj,
667                                                      srgb_skip_decode);
668    struct pipe_sampler_view *view =
669          st_create_texture_sampler_view_from_stobj(st, stObj, format,
670                                                    glsl130_or_later);
671 
672    view = st_texture_set_sampler_view(st, stObj, view,
673                                       glsl130_or_later, srgb_skip_decode,
674                                       get_reference);
675    return view;
676 }
677 
678 
679 struct pipe_sampler_view *
st_get_buffer_sampler_view_from_stobj(struct st_context * st,struct st_texture_object * stObj,bool get_reference)680 st_get_buffer_sampler_view_from_stobj(struct st_context *st,
681                                       struct st_texture_object *stObj,
682                                       bool get_reference)
683 {
684    struct st_sampler_view *sv;
685    struct st_buffer_object *stBuf =
686       st_buffer_object(stObj->base.BufferObject);
687 
688    if (!stBuf || !stBuf->buffer)
689       return NULL;
690 
691    sv = st_texture_get_current_sampler_view(st, stObj);
692 
693    struct pipe_resource *buf = stBuf->buffer;
694 
695    if (sv) {
696       struct pipe_sampler_view *view = sv->view;
697 
698       if (view->texture == buf) {
699          /* Debug check: make sure that the sampler view's parameters are
700           * what they're supposed to be.
701           */
702          assert(st_mesa_format_to_pipe_format(st,
703                                               stObj->base._BufferObjectFormat)
704              == view->format);
705          assert(view->target == PIPE_BUFFER);
706          ASSERTED unsigned base = stObj->base.BufferOffset;
707          ASSERTED unsigned size = MIN2(buf->width0 - base,
708                            (unsigned) stObj->base.BufferSize);
709          assert(view->u.buf.offset == base);
710          assert(view->u.buf.size == size);
711          if (get_reference)
712             view = get_sampler_view_reference(sv, view);
713          return view;
714       }
715    }
716 
717    unsigned base = stObj->base.BufferOffset;
718 
719    if (base >= buf->width0)
720       return NULL;
721 
722    unsigned size = buf->width0 - base;
723    size = MIN2(size, (unsigned)stObj->base.BufferSize);
724    if (!size)
725       return NULL;
726 
727    /* Create a new sampler view. There is no need to clear the entire
728     * structure (consider CPU overhead).
729     */
730    struct pipe_sampler_view templ;
731 
732    templ.format =
733       st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
734    templ.target = PIPE_BUFFER;
735    templ.swizzle_r = PIPE_SWIZZLE_X;
736    templ.swizzle_g = PIPE_SWIZZLE_Y;
737    templ.swizzle_b = PIPE_SWIZZLE_Z;
738    templ.swizzle_a = PIPE_SWIZZLE_W;
739    templ.u.buf.offset = base;
740    templ.u.buf.size = size;
741 
742    struct pipe_sampler_view *view =
743       st->pipe->create_sampler_view(st->pipe, buf, &templ);
744 
745    view = st_texture_set_sampler_view(st, stObj, view, false, false,
746                                       get_reference);
747 
748    return view;
749 }
750