2 * Copyright 2016 VMware, Inc.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 #include "pipe/p_context.h"
27 #include "util/u_format.h"
28 #include "util/u_inlines.h"
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"
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"
46 * Set the given view as the current context's view for the texture.
48 * Overwrites any pre-existing view of the context.
50 * Takes ownership of the view (i.e., stores the view without incrementing the
53 * \return the view, or NULL on error. In case of error, the reference to the
56 static struct pipe_sampler_view
*
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
)
62 struct st_sampler_views
*views
;
63 struct st_sampler_view
*free
= NULL
;
64 struct st_sampler_view
*sv
;
67 simple_mtx_lock(&stObj
->validate_mutex
);
68 views
= stObj
->sampler_views
;
70 for (i
= 0; i
< views
->count
; ++i
) {
71 sv
= &views
->views
[i
];
73 /* Is the array entry used ? */
75 /* check if the context matches */
76 if (sv
->view
->context
== st
->pipe
) {
77 pipe_sampler_view_release(st
->pipe
, &sv
->view
);
81 /* Found a free slot, remember that */
86 /* Couldn't find a slot for our context, create a new one */
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]);
95 if (new_max
< views
->max
||
96 new_max
> (UINT_MAX
- sizeof(*views
)) / sizeof(views
->views
[0])) {
97 pipe_sampler_view_release(st
->pipe
, &view
);
101 struct st_sampler_views
*new_views
= malloc(new_size
);
103 pipe_sampler_view_release(st
->pipe
, &view
);
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]));
112 /* Initialize the pipe_sampler_view pointers to zero so that we don't
113 * have to worry about racing against readers when incrementing
116 memset(&new_views
->views
[views
->count
], 0,
117 (new_max
- views
->count
) * sizeof(views
->views
[0]));
119 /* Use memory release semantics to ensure that concurrent readers will
120 * get the correct contents of the new container.
122 * Also, the write should be atomic, but that's guaranteed anyway on
123 * all supported platforms.
125 p_atomic_set(&stObj
->sampler_views
, new_views
);
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.
132 views
->next
= stObj
->sampler_views_old
;
133 stObj
->sampler_views_old
= views
;
138 sv
= &views
->views
[views
->count
];
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.
148 assert(sv
->view
== NULL
);
150 sv
->glsl130_or_later
= glsl130_or_later
;
151 sv
->srgb_skip_decode
= srgb_skip_decode
;
155 simple_mtx_unlock(&stObj
->validate_mutex
);
161 * Return the most-recently validated sampler view for the texture \p stObj
162 * in the given context, if any.
164 * Performs no additional validation.
166 const struct st_sampler_view
*
167 st_texture_get_current_sampler_view(const struct st_context
*st
,
168 const struct st_texture_object
*stObj
)
170 const struct st_sampler_views
*views
= p_atomic_read(&stObj
->sampler_views
);
172 for (unsigned i
= 0; i
< views
->count
; ++i
) {
173 const struct st_sampler_view
*sv
= &views
->views
[i
];
174 if (sv
->view
&& sv
->view
->context
== st
->pipe
)
183 * For the given texture object, release any sampler views which belong
184 * to the calling context.
187 st_texture_release_sampler_view(struct st_context
*st
,
188 struct st_texture_object
*stObj
)
192 simple_mtx_lock(&stObj
->validate_mutex
);
193 struct st_sampler_views
*views
= stObj
->sampler_views
;
194 for (i
= 0; i
< views
->count
; ++i
) {
195 struct pipe_sampler_view
**sv
= &views
->views
[i
].view
;
197 if (*sv
&& (*sv
)->context
== st
->pipe
) {
198 pipe_sampler_view_reference(sv
, NULL
);
202 simple_mtx_unlock(&stObj
->validate_mutex
);
207 * Release all sampler views attached to the given texture object, regardless
211 st_texture_release_all_sampler_views(struct st_context
*st
,
212 struct st_texture_object
*stObj
)
216 /* TODO: This happens while a texture is deleted, because the Driver API
217 * is asymmetric: the driver allocates the texture object memory, but
218 * mesa/main frees it.
220 if (!stObj
->sampler_views
)
223 simple_mtx_lock(&stObj
->validate_mutex
);
224 struct st_sampler_views
*views
= stObj
->sampler_views
;
225 for (i
= 0; i
< views
->count
; ++i
)
226 pipe_sampler_view_release(st
->pipe
, &views
->views
[i
].view
);
227 simple_mtx_unlock(&stObj
->validate_mutex
);
232 st_texture_free_sampler_views(struct st_texture_object
*stObj
)
234 free(stObj
->sampler_views
);
235 stObj
->sampler_views
= NULL
;
237 while (stObj
->sampler_views_old
) {
238 struct st_sampler_views
*views
= stObj
->sampler_views_old
;
239 stObj
->sampler_views_old
= views
->next
;
246 * Return swizzle1(swizzle2)
249 swizzle_swizzle(unsigned swizzle1
, unsigned swizzle2
)
253 if (swizzle1
== SWIZZLE_XYZW
) {
254 /* identity swizzle, no change to swizzle2 */
258 for (i
= 0; i
< 4; i
++) {
259 unsigned s
= GET_SWZ(swizzle1
, i
);
265 swz
[i
] = GET_SWZ(swizzle2
, s
);
268 swz
[i
] = SWIZZLE_ZERO
;
271 swz
[i
] = SWIZZLE_ONE
;
274 assert(!"Bad swizzle term");
279 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
284 * Given a user-specified texture base format, the actual gallium texture
285 * format and the current GL_DEPTH_MODE, return a texture swizzle.
287 * Consider the case where the user requests a GL_RGB internal texture
288 * format the driver actually uses an RGBA format. The A component should
289 * be ignored and sampling from the texture should always return (r,g,b,1).
290 * But if we rendered to the texture we might have written A values != 1.
291 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
292 * This function computes the texture swizzle needed to get the expected
295 * In the case of depth textures, the GL_DEPTH_MODE state determines the
298 * This result must be composed with the user-specified swizzle to get
302 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
303 bool glsl130_or_later
)
305 switch (baseFormat
) {
309 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
311 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
313 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
314 SWIZZLE_ZERO
, SWIZZLE_ONE
);
316 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
317 SWIZZLE_ZERO
, SWIZZLE_W
);
319 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
320 case GL_LUMINANCE_ALPHA
:
321 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
324 case GL_STENCIL_INDEX
:
325 case GL_DEPTH_STENCIL
:
326 case GL_DEPTH_COMPONENT
:
327 /* Now examine the depth mode */
330 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
332 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
334 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
335 * the depth mode and return float, while older shadow* functions
336 * and ARB_fp instructions return vec4 according to the depth mode.
338 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
339 * them to return 0, breaking them completely.
341 * A proper fix would increase code complexity and that's not worth
342 * it for a rarely used feature such as the GL_ALPHA depth mode
343 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
344 * shaders that use GLSL 1.30 or later.
346 * BTW, it's required that sampler views are updated when
347 * shaders change (check_sampler_swizzle takes care of that).
349 if (glsl130_or_later
)
352 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
353 SWIZZLE_ZERO
, SWIZZLE_X
);
355 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
356 SWIZZLE_ZERO
, SWIZZLE_ONE
);
358 assert(!"Unexpected depthMode");
362 assert(!"Unexpected baseFormat");
369 get_texture_format_swizzle(const struct st_context
*st
,
370 const struct st_texture_object
*stObj
,
371 bool glsl130_or_later
)
373 GLenum baseFormat
= _mesa_base_tex_image(&stObj
->base
)->_BaseFormat
;
374 unsigned tex_swizzle
;
375 GLenum depth_mode
= stObj
->base
.DepthMode
;
377 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
378 * with depth component data specified with a sized internal format.
380 if (_mesa_is_gles3(st
->ctx
) &&
381 (baseFormat
== GL_DEPTH_COMPONENT
||
382 baseFormat
== GL_DEPTH_STENCIL
||
383 baseFormat
== GL_STENCIL_INDEX
)) {
384 const struct gl_texture_image
*firstImage
=
385 _mesa_base_tex_image(&stObj
->base
);
386 if (firstImage
->InternalFormat
!= GL_DEPTH_COMPONENT
&&
387 firstImage
->InternalFormat
!= GL_DEPTH_STENCIL
&&
388 firstImage
->InternalFormat
!= GL_STENCIL_INDEX
)
391 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
395 /* Combine the texture format swizzle with user's swizzle */
396 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
401 * Return TRUE if the texture's sampler view swizzle is not equal to
402 * the texture's swizzle.
404 * \param stObj the st texture object,
406 MAYBE_UNUSED
static boolean
407 check_sampler_swizzle(const struct st_context
*st
,
408 const struct st_texture_object
*stObj
,
409 const struct pipe_sampler_view
*sv
,
410 bool glsl130_or_later
)
412 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl130_or_later
);
414 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
415 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
416 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
417 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
422 last_level(const struct st_texture_object
*stObj
)
424 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
425 stObj
->pt
->last_level
);
426 if (stObj
->base
.Immutable
)
427 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
433 last_layer(const struct st_texture_object
*stObj
)
435 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
436 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
437 stObj
->pt
->array_size
- 1);
438 return stObj
->pt
->array_size
- 1;
443 * Determine the format for the texture sampler view.
445 static enum pipe_format
446 get_sampler_view_format(struct st_context
*st
,
447 const struct st_texture_object
*stObj
,
448 bool srgb_skip_decode
)
450 enum pipe_format format
;
452 GLenum baseFormat
= _mesa_base_tex_image(&stObj
->base
)->_BaseFormat
;
453 format
= stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
455 if (baseFormat
== GL_DEPTH_COMPONENT
||
456 baseFormat
== GL_DEPTH_STENCIL
||
457 baseFormat
== GL_STENCIL_INDEX
) {
458 if (stObj
->base
.StencilSampling
|| baseFormat
== GL_STENCIL_INDEX
)
459 format
= util_format_stencil_only(format
);
464 /* If sRGB decoding is off, use the linear format */
465 if (srgb_skip_decode
)
466 format
= util_format_linear(format
);
468 /* Use R8_UNORM for video formats */
470 case PIPE_FORMAT_NV12
:
471 case PIPE_FORMAT_IYUV
:
472 format
= PIPE_FORMAT_R8_UNORM
;
481 static struct pipe_sampler_view
*
482 st_create_texture_sampler_view_from_stobj(struct st_context
*st
,
483 struct st_texture_object
*stObj
,
484 enum pipe_format format
,
485 bool glsl130_or_later
)
487 /* There is no need to clear this structure (consider CPU overhead). */
488 struct pipe_sampler_view templ
;
489 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl130_or_later
);
491 templ
.format
= format
;
493 if (stObj
->level_override
) {
494 templ
.u
.tex
.first_level
= templ
.u
.tex
.last_level
= stObj
->level_override
;
496 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
497 templ
.u
.tex
.last_level
= last_level(stObj
);
499 if (stObj
->layer_override
) {
500 templ
.u
.tex
.first_layer
= templ
.u
.tex
.last_layer
= stObj
->layer_override
;
502 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
503 templ
.u
.tex
.last_layer
= last_layer(stObj
);
505 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
506 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
507 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
509 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
510 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
511 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
512 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
514 return st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, &templ
);
518 struct pipe_sampler_view
*
519 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
520 struct st_texture_object
*stObj
,
521 const struct gl_sampler_object
*samp
,
522 bool glsl130_or_later
,
523 bool ignore_srgb_decode
)
525 const struct st_sampler_view
*sv
;
526 bool srgb_skip_decode
= false;
528 if (!ignore_srgb_decode
&& samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
)
529 srgb_skip_decode
= true;
531 sv
= st_texture_get_current_sampler_view(st
, stObj
);
534 sv
->glsl130_or_later
== glsl130_or_later
&&
535 sv
->srgb_skip_decode
== srgb_skip_decode
) {
536 /* Debug check: make sure that the sampler view's parameters are
537 * what they're supposed to be.
539 struct pipe_sampler_view
*view
= sv
->view
;
540 assert(stObj
->pt
== view
->texture
);
541 assert(!check_sampler_swizzle(st
, stObj
, view
, glsl130_or_later
));
542 assert(get_sampler_view_format(st
, stObj
, srgb_skip_decode
) == view
->format
);
543 assert(gl_target_to_pipe(stObj
->base
.Target
) == view
->target
);
544 assert(stObj
->level_override
||
545 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
== view
->u
.tex
.first_level
);
546 assert(stObj
->level_override
|| last_level(stObj
) == view
->u
.tex
.last_level
);
547 assert(stObj
->layer_override
|| stObj
->base
.MinLayer
== view
->u
.tex
.first_layer
);
548 assert(stObj
->layer_override
|| last_layer(stObj
) == view
->u
.tex
.last_layer
);
549 assert(!stObj
->layer_override
||
550 (stObj
->layer_override
== view
->u
.tex
.first_layer
&&
551 stObj
->layer_override
== view
->u
.tex
.last_layer
));
555 /* create new sampler view */
556 enum pipe_format format
= get_sampler_view_format(st
, stObj
, srgb_skip_decode
);
557 struct pipe_sampler_view
*view
=
558 st_create_texture_sampler_view_from_stobj(st
, stObj
, format
, glsl130_or_later
);
560 view
= st_texture_set_sampler_view(st
, stObj
, view
, glsl130_or_later
, srgb_skip_decode
);
566 struct pipe_sampler_view
*
567 st_get_buffer_sampler_view_from_stobj(struct st_context
*st
,
568 struct st_texture_object
*stObj
)
570 const struct st_sampler_view
*sv
;
571 struct st_buffer_object
*stBuf
=
572 st_buffer_object(stObj
->base
.BufferObject
);
574 if (!stBuf
|| !stBuf
->buffer
)
577 sv
= st_texture_get_current_sampler_view(st
, stObj
);
579 struct pipe_resource
*buf
= stBuf
->buffer
;
582 struct pipe_sampler_view
*view
= sv
->view
;
584 if (view
->texture
== buf
) {
585 /* Debug check: make sure that the sampler view's parameters are
586 * what they're supposed to be.
588 assert(st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
)
590 assert(view
->target
== PIPE_BUFFER
);
591 unsigned base
= stObj
->base
.BufferOffset
;
592 MAYBE_UNUSED
unsigned size
= MIN2(buf
->width0
- base
,
593 (unsigned) stObj
->base
.BufferSize
);
594 assert(view
->u
.buf
.offset
== base
);
595 assert(view
->u
.buf
.size
== size
);
600 unsigned base
= stObj
->base
.BufferOffset
;
602 if (base
>= buf
->width0
)
605 unsigned size
= buf
->width0
- base
;
606 size
= MIN2(size
, (unsigned)stObj
->base
.BufferSize
);
610 /* Create a new sampler view. There is no need to clear the entire
611 * structure (consider CPU overhead).
613 struct pipe_sampler_view templ
;
616 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
617 templ
.target
= PIPE_BUFFER
;
618 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
619 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
620 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
621 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
622 templ
.u
.buf
.offset
= base
;
623 templ
.u
.buf
.size
= size
;
625 struct pipe_sampler_view
*view
=
626 st
->pipe
->create_sampler_view(st
->pipe
, buf
, &templ
);
628 view
= st_texture_set_sampler_view(st
, stObj
, view
, false, false);