• 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_texture.h"
42 
43 /* Subtract remaining private references. Typically used before
44  * destruction. See the header file for explanation.
45  */
46 static void
st_remove_private_references(struct st_sampler_view * sv)47 st_remove_private_references(struct st_sampler_view *sv)
48 {
49    if (sv->private_refcount) {
50       assert(sv->private_refcount > 0);
51       p_atomic_add(&sv->view->reference.count, -sv->private_refcount);
52       sv->private_refcount = 0;
53    }
54 }
55 
56 /* Return a sampler view while incrementing the refcount by 1. */
57 static struct pipe_sampler_view *
get_sampler_view_reference(struct st_sampler_view * sv,struct pipe_sampler_view * view)58 get_sampler_view_reference(struct st_sampler_view *sv,
59                            struct pipe_sampler_view *view)
60 {
61    if (unlikely(sv->private_refcount <= 0)) {
62       assert(sv->private_refcount == 0);
63 
64       /* This is the number of atomic increments we will skip. */
65       sv->private_refcount = 100000000;
66       p_atomic_add(&view->reference.count, sv->private_refcount);
67    }
68 
69    /* Return a reference while decrementing the private refcount. */
70    sv->private_refcount--;
71    return view;
72 }
73 
74 /**
75  * Set the given view as the current context's view for the texture.
76  *
77  * Overwrites any pre-existing view of the context.
78  *
79  * Takes ownership of the view (i.e., stores the view without incrementing the
80  * reference count).
81  *
82  * \return the view, or NULL on error. In case of error, the reference to the
83  * view is released.
84  */
85 static struct pipe_sampler_view *
st_texture_set_sampler_view(struct st_context * st,struct gl_texture_object * stObj,struct pipe_sampler_view * view,bool glsl130_or_later,bool srgb_skip_decode,bool get_reference,bool locked)86 st_texture_set_sampler_view(struct st_context *st,
87                             struct gl_texture_object *stObj,
88                             struct pipe_sampler_view *view,
89                             bool glsl130_or_later, bool srgb_skip_decode,
90                             bool get_reference, bool locked)
91 {
92    struct st_sampler_views *views;
93    struct st_sampler_view *free = NULL;
94    struct st_sampler_view *sv;
95    GLuint i;
96 
97    if (!locked)
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    if (!locked)
192       simple_mtx_unlock(&stObj->validate_mutex);
193    return view;
194 }
195 
196 
197 /**
198  * Return the most-recently validated sampler view for the texture \p stObj
199  * in the given context, if any.
200  *
201  * Performs no additional validation.
202  */
203 struct st_sampler_view *
st_texture_get_current_sampler_view(const struct st_context * st,const struct gl_texture_object * stObj)204 st_texture_get_current_sampler_view(const struct st_context *st,
205                                     const struct gl_texture_object *stObj)
206 {
207    struct st_sampler_views *views = p_atomic_read(&stObj->sampler_views);
208 
209    for (unsigned i = 0; i < views->count; ++i) {
210       struct st_sampler_view *sv = &views->views[i];
211       if (sv->view && sv->view->context == st->pipe)
212          return sv;
213    }
214 
215    return NULL;
216 }
217 
218 
219 /**
220  * For the given texture object, release any sampler views which belong
221  * to the calling context.  This is used to free any sampler views
222  * which belong to the context before the context is destroyed.
223  */
224 void
st_texture_release_context_sampler_view(struct st_context * st,struct gl_texture_object * stObj)225 st_texture_release_context_sampler_view(struct st_context *st,
226                                         struct gl_texture_object *stObj)
227 {
228    GLuint i;
229 
230    simple_mtx_lock(&stObj->validate_mutex);
231    struct st_sampler_views *views = stObj->sampler_views;
232    for (i = 0; i < views->count; ++i) {
233       struct st_sampler_view *sv = &views->views[i];
234 
235       if (sv->view && sv->view->context == st->pipe) {
236          st_remove_private_references(sv);
237          pipe_sampler_view_reference(&sv->view, NULL);
238          break;
239       }
240    }
241    simple_mtx_unlock(&stObj->validate_mutex);
242 }
243 
244 
245 /**
246  * Release all sampler views attached to the given texture object, regardless
247  * of the context.  This is called fairly frequently.  For example, whenever
248  * the texture's base level, max level or swizzle change.
249  */
250 void
st_texture_release_all_sampler_views(struct st_context * st,struct gl_texture_object * stObj)251 st_texture_release_all_sampler_views(struct st_context *st,
252                                      struct gl_texture_object *stObj)
253 {
254    /* TODO: This happens while a texture is deleted, because the Driver API
255     * is asymmetric: the driver allocates the texture object memory, but
256     * mesa/main frees it.
257     */
258    if (!stObj->sampler_views)
259       return;
260 
261    simple_mtx_lock(&stObj->validate_mutex);
262    struct st_sampler_views *views = stObj->sampler_views;
263    for (unsigned i = 0; i < views->count; ++i) {
264       struct st_sampler_view *stsv = &views->views[i];
265       if (stsv->view) {
266          st_remove_private_references(stsv);
267 
268          if (stsv->st && stsv->st != st) {
269             /* Transfer this reference to the zombie list.  It will
270              * likely be freed when the zombie list is freed.
271              */
272             st_save_zombie_sampler_view(stsv->st, stsv->view);
273             stsv->view = NULL;
274          } else {
275             pipe_sampler_view_reference(&stsv->view, NULL);
276          }
277       }
278    }
279    views->count = 0;
280    simple_mtx_unlock(&stObj->validate_mutex);
281 }
282 
283 
284 /*
285  * Delete the texture's sampler views and st_sampler_views containers.
286  * This is to be called just before a texture is deleted.
287  */
288 void
st_delete_texture_sampler_views(struct st_context * st,struct gl_texture_object * stObj)289 st_delete_texture_sampler_views(struct st_context *st,
290                                 struct gl_texture_object *stObj)
291 {
292    st_texture_release_all_sampler_views(st, stObj);
293 
294    /* Free the container of the current per-context sampler views */
295    assert(stObj->sampler_views->count == 0);
296    free(stObj->sampler_views);
297    stObj->sampler_views = NULL;
298 
299    /* Free old sampler view containers */
300    while (stObj->sampler_views_old) {
301       struct st_sampler_views *views = stObj->sampler_views_old;
302       stObj->sampler_views_old = views->next;
303       free(views);
304    }
305 }
306 
307 /**
308  * Return TRUE if the texture's sampler view swizzle is not equal to
309  * the texture's swizzle.
310  *
311  * \param texObj  the st texture object,
312  */
313 ASSERTED static bool
check_sampler_swizzle(const struct st_context * st,const struct gl_texture_object * texObj,const struct pipe_sampler_view * sv,bool glsl130_or_later)314 check_sampler_swizzle(const struct st_context *st,
315                       const struct gl_texture_object *texObj,
316                       const struct pipe_sampler_view *sv,
317                       bool glsl130_or_later)
318 {
319    unsigned swizzle = glsl130_or_later ? texObj->SwizzleGLSL130 : texObj->Swizzle;
320 
321    return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
322            (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
323            (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
324            (sv->swizzle_a != GET_SWZ(swizzle, 3)));
325 }
326 
327 
328 static unsigned
last_level(const struct gl_texture_object * texObj)329 last_level(const struct gl_texture_object *texObj)
330 {
331    unsigned ret = MIN2(texObj->Attrib.MinLevel + texObj->_MaxLevel,
332                        texObj->pt->last_level);
333    if (texObj->Immutable)
334       ret = MIN2(ret, texObj->Attrib.MinLevel +
335                  texObj->Attrib.NumLevels - 1);
336    return ret;
337 }
338 
339 
340 static unsigned
last_layer(const struct gl_texture_object * texObj)341 last_layer(const struct gl_texture_object *texObj)
342 {
343    if (texObj->Immutable && texObj->pt->array_size > 1)
344       return MIN2(texObj->Attrib.MinLayer +
345                   texObj->Attrib.NumLayers - 1,
346                   texObj->pt->array_size - 1);
347    return texObj->pt->array_size - 1;
348 }
349 
350 
351 /**
352  * Determine the format for the texture sampler view.
353  */
354 enum pipe_format
st_get_sampler_view_format(const struct st_context * st,const struct gl_texture_object * texObj,bool srgb_skip_decode)355 st_get_sampler_view_format(const struct st_context *st,
356                            const struct gl_texture_object *texObj,
357                            bool srgb_skip_decode)
358 {
359    enum pipe_format format;
360 
361    GLenum baseFormat = _mesa_base_tex_image(texObj)->_BaseFormat;
362    format = texObj->surface_based ? texObj->surface_format : texObj->pt->format;
363 
364    /* From OpenGL 4.3 spec, "Combined Depth/Stencil Textures":
365     *
366     *    "The DEPTH_STENCIL_TEXTURE_MODE is ignored for non
367     *     depth/stencil textures.
368     */
369    const bool has_combined_ds =
370       baseFormat == GL_DEPTH_STENCIL;
371 
372    if (baseFormat == GL_DEPTH_COMPONENT ||
373        baseFormat == GL_DEPTH_STENCIL ||
374        baseFormat == GL_STENCIL_INDEX) {
375       if ((texObj->StencilSampling && has_combined_ds) ||
376           baseFormat == GL_STENCIL_INDEX)
377          format = util_format_stencil_only(format);
378 
379       return format;
380    }
381 
382    /* If sRGB decoding is off, use the linear format */
383    if (srgb_skip_decode)
384       format = util_format_linear(format);
385 
386    /* if resource format matches then YUV wasn't lowered */
387    if (format == texObj->pt->format)
388       return format;
389 
390    /* Use R8_UNORM for video formats */
391    switch (format) {
392    case PIPE_FORMAT_NV12:
393       if (texObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
394          format = PIPE_FORMAT_R8_G8B8_420_UNORM;
395          break;
396       }
397       FALLTHROUGH;
398    case PIPE_FORMAT_NV21:
399       if (texObj->pt->format == PIPE_FORMAT_R8_B8G8_420_UNORM) {
400          format = PIPE_FORMAT_R8_B8G8_420_UNORM;
401          break;
402       }
403       FALLTHROUGH;
404    case PIPE_FORMAT_NV16:
405       if (texObj->pt->format == PIPE_FORMAT_R8_G8B8_422_UNORM) {
406          format = PIPE_FORMAT_R8_G8B8_422_UNORM;
407          break;
408       }
409       FALLTHROUGH;
410    case PIPE_FORMAT_IYUV:
411       if (texObj->pt->format == PIPE_FORMAT_R8_G8_B8_420_UNORM ||
412           texObj->pt->format == PIPE_FORMAT_R8_B8_G8_420_UNORM) {
413          format = texObj->pt->format;
414          break;
415       }
416       format = PIPE_FORMAT_R8_UNORM;
417       break;
418    case PIPE_FORMAT_NV15:
419       if (texObj->pt->format == PIPE_FORMAT_R10_G10B10_420_UNORM) {
420          format = PIPE_FORMAT_R10_G10B10_420_UNORM;
421          break;
422       }
423       FALLTHROUGH;
424    case PIPE_FORMAT_NV20:
425       if (texObj->pt->format == PIPE_FORMAT_R10_G10B10_422_UNORM) {
426          format = PIPE_FORMAT_R10_G10B10_422_UNORM;
427          break;
428       }
429       FALLTHROUGH;
430    case PIPE_FORMAT_P010:
431    case PIPE_FORMAT_P012:
432    case PIPE_FORMAT_P016:
433    case PIPE_FORMAT_P030:
434       format = PIPE_FORMAT_R16_UNORM;
435       break;
436    case PIPE_FORMAT_Y210:
437    case PIPE_FORMAT_Y212:
438    case PIPE_FORMAT_Y216:
439       format = PIPE_FORMAT_R16G16_UNORM;
440       break;
441    case PIPE_FORMAT_Y410:
442       format = PIPE_FORMAT_R10G10B10A2_UNORM;
443       break;
444    case PIPE_FORMAT_Y412:
445    case PIPE_FORMAT_Y416:
446       format = PIPE_FORMAT_R16G16B16A16_UNORM;
447       break;
448    case PIPE_FORMAT_YUYV:
449    case PIPE_FORMAT_YVYU:
450    case PIPE_FORMAT_UYVY:
451    case PIPE_FORMAT_VYUY:
452       if (texObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM ||
453           texObj->pt->format == PIPE_FORMAT_R8B8_R8G8_UNORM ||
454           texObj->pt->format == PIPE_FORMAT_B8R8_G8R8_UNORM ||
455           texObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM) {
456          format = texObj->pt->format;
457          break;
458       }
459       format = PIPE_FORMAT_R8G8_UNORM;
460       break;
461    case PIPE_FORMAT_AYUV:
462       format = PIPE_FORMAT_RGBA8888_UNORM;
463       break;
464    case PIPE_FORMAT_XYUV:
465       format = PIPE_FORMAT_RGBX8888_UNORM;
466       break;
467    default:
468       break;
469    }
470    return format;
471 }
472 
473 static unsigned
gl_astc_decode_precision_to_pipe(GLint precision)474 gl_astc_decode_precision_to_pipe(GLint precision)
475 {
476    switch (precision) {
477    case GL_RGBA8:
478       return PIPE_ASTC_DECODE_FORMAT_UNORM8;
479    case GL_RGB9_E5:
480       return PIPE_ASTC_DECODE_FORMAT_RGB9E5;
481    case GL_RGBA16F:
482    default:
483       return PIPE_ASTC_DECODE_FORMAT_FLOAT16;
484    }
485 }
486 
487 static struct pipe_sampler_view *
st_create_texture_sampler_view_from_stobj(struct st_context * st,struct gl_texture_object * texObj,enum pipe_format format,bool glsl130_or_later)488 st_create_texture_sampler_view_from_stobj(struct st_context *st,
489                                           struct gl_texture_object *texObj,
490                                           enum pipe_format format,
491                                           bool glsl130_or_later)
492 {
493    /* There is no need to clear this structure (consider CPU overhead). */
494    struct pipe_sampler_view templ;
495    unsigned swizzle = glsl130_or_later ? texObj->SwizzleGLSL130 : texObj->Swizzle;
496 
497    templ.format = format;
498    templ.is_tex2d_from_buf = false;
499 
500    if (texObj->level_override >= 0) {
501       templ.u.tex.first_level = templ.u.tex.last_level = texObj->level_override;
502    } else {
503       templ.u.tex.first_level = texObj->Attrib.MinLevel +
504                                 texObj->Attrib.BaseLevel;
505       templ.u.tex.last_level = last_level(texObj);
506    }
507    if (texObj->layer_override >= 0) {
508       templ.u.tex.first_layer = templ.u.tex.last_layer = texObj->layer_override;
509    } else {
510       templ.u.tex.first_layer = texObj->Attrib.MinLayer;
511       templ.u.tex.last_layer = last_layer(texObj);
512    }
513    assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
514    assert(templ.u.tex.first_level <= templ.u.tex.last_level);
515    templ.target = gl_target_to_pipe(texObj->Target);
516 
517    templ.swizzle_r = GET_SWZ(swizzle, 0);
518    templ.swizzle_g = GET_SWZ(swizzle, 1);
519    templ.swizzle_b = GET_SWZ(swizzle, 2);
520    templ.swizzle_a = GET_SWZ(swizzle, 3);
521 
522    templ.astc_decode_format =
523       gl_astc_decode_precision_to_pipe(texObj->AstcDecodePrecision);
524 
525    return st->pipe->create_sampler_view(st->pipe, texObj->pt, &templ);
526 }
527 
528 struct pipe_sampler_view *
st_get_texture_sampler_view_from_stobj(struct st_context * st,struct gl_texture_object * texObj,const struct gl_sampler_object * samp,bool glsl130_or_later,bool ignore_srgb_decode,bool get_reference)529 st_get_texture_sampler_view_from_stobj(struct st_context *st,
530                                        struct gl_texture_object *texObj,
531                                        const struct gl_sampler_object *samp,
532                                        bool glsl130_or_later,
533                                        bool ignore_srgb_decode,
534                                        bool get_reference)
535 {
536    struct st_sampler_view *sv;
537    bool srgb_skip_decode = false;
538 
539    if (!ignore_srgb_decode && samp->Attrib.sRGBDecode == GL_SKIP_DECODE_EXT)
540       srgb_skip_decode = true;
541 
542    simple_mtx_lock(&texObj->validate_mutex);
543    sv = st_texture_get_current_sampler_view(st, texObj);
544 
545    if (sv &&
546        sv->glsl130_or_later == glsl130_or_later &&
547        sv->srgb_skip_decode == srgb_skip_decode) {
548       /* Debug check: make sure that the sampler view's parameters are
549        * what they're supposed to be.
550        */
551       struct pipe_sampler_view *view = sv->view;
552       assert(texObj->pt == view->texture);
553       assert(!check_sampler_swizzle(st, texObj, view, glsl130_or_later));
554       assert(st_get_sampler_view_format(st, texObj, srgb_skip_decode) == view->format);
555       assert(gl_target_to_pipe(texObj->Target) == view->target);
556       assert(texObj->level_override >= 0 ||
557              texObj->Attrib.MinLevel +
558              texObj->Attrib.BaseLevel == view->u.tex.first_level);
559       assert(texObj->level_override >= 0 || last_level(texObj) == view->u.tex.last_level);
560       assert(texObj->layer_override >= 0 ||
561              texObj->Attrib.MinLayer == view->u.tex.first_layer);
562       assert(texObj->layer_override >= 0 || last_layer(texObj) == view->u.tex.last_layer);
563       assert(texObj->layer_override < 0 ||
564              (texObj->layer_override == view->u.tex.first_layer &&
565               texObj->layer_override == view->u.tex.last_layer));
566       if (get_reference)
567          view = get_sampler_view_reference(sv, view);
568       simple_mtx_unlock(&texObj->validate_mutex);
569       return view;
570    }
571 
572    /* create new sampler view */
573    enum pipe_format format = st_get_sampler_view_format(st, texObj,
574                                                         srgb_skip_decode);
575    struct pipe_sampler_view *view =
576          st_create_texture_sampler_view_from_stobj(st, texObj, format,
577                                                    glsl130_or_later);
578 
579    view = st_texture_set_sampler_view(st, texObj, view,
580                                       glsl130_or_later, srgb_skip_decode,
581                                       get_reference, true);
582    simple_mtx_unlock(&texObj->validate_mutex);
583 
584    return view;
585 }
586 
587 
588 struct pipe_sampler_view *
st_get_buffer_sampler_view_from_stobj(struct st_context * st,struct gl_texture_object * texObj,bool get_reference)589 st_get_buffer_sampler_view_from_stobj(struct st_context *st,
590                                       struct gl_texture_object *texObj,
591                                       bool get_reference)
592 {
593    struct st_sampler_view *sv;
594    struct gl_buffer_object *stBuf =
595       texObj->BufferObject;
596 
597    if (!stBuf || !stBuf->buffer)
598       return NULL;
599 
600    sv = st_texture_get_current_sampler_view(st, texObj);
601 
602    struct pipe_resource *buf = stBuf->buffer;
603 
604    if (sv) {
605       struct pipe_sampler_view *view = sv->view;
606 
607       if (view->texture == buf) {
608          /* Debug check: make sure that the sampler view's parameters are
609           * what they're supposed to be.
610           */
611          assert(st_mesa_format_to_pipe_format(st,
612                                               texObj->_BufferObjectFormat)
613              == view->format);
614          assert(view->target == PIPE_BUFFER);
615          ASSERTED unsigned base = texObj->BufferOffset;
616          ASSERTED unsigned size = MIN2(buf->width0 - base,
617                            (unsigned) texObj->BufferSize);
618          assert(view->u.buf.offset == base);
619          assert(view->u.buf.size == size);
620          if (get_reference)
621             view = get_sampler_view_reference(sv, view);
622          return view;
623       }
624    }
625 
626    unsigned base = texObj->BufferOffset;
627 
628    if (base >= buf->width0)
629       return NULL;
630 
631    unsigned size = buf->width0 - base;
632    size = MIN2(size, (unsigned)texObj->BufferSize);
633    if (!size)
634       return NULL;
635 
636    /* Create a new sampler view. There is no need to clear the entire
637     * structure (consider CPU overhead).
638     */
639    struct pipe_sampler_view templ;
640 
641    templ.is_tex2d_from_buf = false;
642    templ.format =
643       st_mesa_format_to_pipe_format(st, texObj->_BufferObjectFormat);
644    templ.target = PIPE_BUFFER;
645    templ.swizzle_r = PIPE_SWIZZLE_X;
646    templ.swizzle_g = PIPE_SWIZZLE_Y;
647    templ.swizzle_b = PIPE_SWIZZLE_Z;
648    templ.swizzle_a = PIPE_SWIZZLE_W;
649    templ.u.buf.offset = base;
650    templ.u.buf.size = size;
651 
652    struct pipe_sampler_view *view =
653       st->pipe->create_sampler_view(st->pipe, buf, &templ);
654 
655    view = st_texture_set_sampler_view(st, texObj, view, false, false,
656                                       get_reference, false);
657 
658    return view;
659 }
660