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