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. This is used to free any sampler views
185 * which belong to the context before the context is destroyed.
188 st_texture_release_sampler_view(struct st_context
*st
,
189 struct st_texture_object
*stObj
)
193 simple_mtx_lock(&stObj
->validate_mutex
);
194 struct st_sampler_views
*views
= stObj
->sampler_views
;
195 for (i
= 0; i
< views
->count
; ++i
) {
196 struct pipe_sampler_view
**sv
= &views
->views
[i
].view
;
198 if (*sv
&& (*sv
)->context
== st
->pipe
) {
199 pipe_sampler_view_reference(sv
, NULL
);
203 simple_mtx_unlock(&stObj
->validate_mutex
);
208 * Release all sampler views attached to the given texture object, regardless
209 * of the context. This is called fairly frequently. For example, whenever
210 * the texture's base level, max level or swizzle change.
213 st_texture_release_all_sampler_views(struct st_context
*st
,
214 struct st_texture_object
*stObj
)
218 /* TODO: This happens while a texture is deleted, because the Driver API
219 * is asymmetric: the driver allocates the texture object memory, but
220 * mesa/main frees it.
222 if (!stObj
->sampler_views
)
225 simple_mtx_lock(&stObj
->validate_mutex
);
226 struct st_sampler_views
*views
= stObj
->sampler_views
;
227 for (i
= 0; i
< views
->count
; ++i
)
228 pipe_sampler_view_release(st
->pipe
, &views
->views
[i
].view
);
229 simple_mtx_unlock(&stObj
->validate_mutex
);
234 * Free the texture's st_sampler_views objects. This should be called
235 * after st_texture_release_all_sampler_views().
238 st_texture_free_sampler_views(struct st_texture_object
*stObj
)
240 free(stObj
->sampler_views
);
241 stObj
->sampler_views
= NULL
;
243 while (stObj
->sampler_views_old
) {
244 struct st_sampler_views
*views
= stObj
->sampler_views_old
;
245 stObj
->sampler_views_old
= views
->next
;
252 * Return swizzle1(swizzle2)
255 swizzle_swizzle(unsigned swizzle1
, unsigned swizzle2
)
259 if (swizzle1
== SWIZZLE_XYZW
) {
260 /* identity swizzle, no change to swizzle2 */
264 for (i
= 0; i
< 4; i
++) {
265 unsigned s
= GET_SWZ(swizzle1
, i
);
271 swz
[i
] = GET_SWZ(swizzle2
, s
);
274 swz
[i
] = SWIZZLE_ZERO
;
277 swz
[i
] = SWIZZLE_ONE
;
280 assert(!"Bad swizzle term");
285 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
290 * Given a user-specified texture base format, the actual gallium texture
291 * format and the current GL_DEPTH_MODE, return a texture swizzle.
293 * Consider the case where the user requests a GL_RGB internal texture
294 * format the driver actually uses an RGBA format. The A component should
295 * be ignored and sampling from the texture should always return (r,g,b,1).
296 * But if we rendered to the texture we might have written A values != 1.
297 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
298 * This function computes the texture swizzle needed to get the expected
301 * In the case of depth textures, the GL_DEPTH_MODE state determines the
304 * This result must be composed with the user-specified swizzle to get
308 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
309 bool glsl130_or_later
)
311 switch (baseFormat
) {
315 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
317 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
319 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
320 SWIZZLE_ZERO
, SWIZZLE_ONE
);
322 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
323 SWIZZLE_ZERO
, SWIZZLE_W
);
325 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
326 case GL_LUMINANCE_ALPHA
:
327 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
330 case GL_STENCIL_INDEX
:
331 case GL_DEPTH_STENCIL
:
332 case GL_DEPTH_COMPONENT
:
333 /* Now examine the depth mode */
336 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
338 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
340 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
341 * the depth mode and return float, while older shadow* functions
342 * and ARB_fp instructions return vec4 according to the depth mode.
344 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
345 * them to return 0, breaking them completely.
347 * A proper fix would increase code complexity and that's not worth
348 * it for a rarely used feature such as the GL_ALPHA depth mode
349 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
350 * shaders that use GLSL 1.30 or later.
352 * BTW, it's required that sampler views are updated when
353 * shaders change (check_sampler_swizzle takes care of that).
355 if (glsl130_or_later
)
358 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
359 SWIZZLE_ZERO
, SWIZZLE_X
);
361 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
362 SWIZZLE_ZERO
, SWIZZLE_ONE
);
364 assert(!"Unexpected depthMode");
368 assert(!"Unexpected baseFormat");
375 get_texture_format_swizzle(const struct st_context
*st
,
376 const struct st_texture_object
*stObj
,
377 bool glsl130_or_later
)
379 GLenum baseFormat
= _mesa_base_tex_image(&stObj
->base
)->_BaseFormat
;
380 unsigned tex_swizzle
;
381 GLenum depth_mode
= stObj
->base
.DepthMode
;
383 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
384 * with depth component data specified with a sized internal format.
386 if (_mesa_is_gles3(st
->ctx
) &&
387 (baseFormat
== GL_DEPTH_COMPONENT
||
388 baseFormat
== GL_DEPTH_STENCIL
||
389 baseFormat
== GL_STENCIL_INDEX
)) {
390 const struct gl_texture_image
*firstImage
=
391 _mesa_base_tex_image(&stObj
->base
);
392 if (firstImage
->InternalFormat
!= GL_DEPTH_COMPONENT
&&
393 firstImage
->InternalFormat
!= GL_DEPTH_STENCIL
&&
394 firstImage
->InternalFormat
!= GL_STENCIL_INDEX
)
397 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
401 /* Combine the texture format swizzle with user's swizzle */
402 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
407 * Return TRUE if the texture's sampler view swizzle is not equal to
408 * the texture's swizzle.
410 * \param stObj the st texture object,
412 MAYBE_UNUSED
static boolean
413 check_sampler_swizzle(const struct st_context
*st
,
414 const struct st_texture_object
*stObj
,
415 const struct pipe_sampler_view
*sv
,
416 bool glsl130_or_later
)
418 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl130_or_later
);
420 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
421 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
422 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
423 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
428 last_level(const struct st_texture_object
*stObj
)
430 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
431 stObj
->pt
->last_level
);
432 if (stObj
->base
.Immutable
)
433 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
439 last_layer(const struct st_texture_object
*stObj
)
441 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
442 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
443 stObj
->pt
->array_size
- 1);
444 return stObj
->pt
->array_size
- 1;
449 * Determine the format for the texture sampler view.
451 static enum pipe_format
452 get_sampler_view_format(struct st_context
*st
,
453 const struct st_texture_object
*stObj
,
454 bool srgb_skip_decode
)
456 enum pipe_format format
;
458 GLenum baseFormat
= _mesa_base_tex_image(&stObj
->base
)->_BaseFormat
;
459 format
= stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
461 if (baseFormat
== GL_DEPTH_COMPONENT
||
462 baseFormat
== GL_DEPTH_STENCIL
||
463 baseFormat
== GL_STENCIL_INDEX
) {
464 if (stObj
->base
.StencilSampling
|| baseFormat
== GL_STENCIL_INDEX
)
465 format
= util_format_stencil_only(format
);
470 /* If sRGB decoding is off, use the linear format */
471 if (srgb_skip_decode
)
472 format
= util_format_linear(format
);
474 /* Use R8_UNORM for video formats */
476 case PIPE_FORMAT_NV12
:
477 case PIPE_FORMAT_IYUV
:
478 format
= PIPE_FORMAT_R8_UNORM
;
487 static struct pipe_sampler_view
*
488 st_create_texture_sampler_view_from_stobj(struct st_context
*st
,
489 struct st_texture_object
*stObj
,
490 enum pipe_format format
,
491 bool glsl130_or_later
)
493 /* There is no need to clear this structure (consider CPU overhead). */
494 struct pipe_sampler_view templ
;
495 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl130_or_later
);
497 templ
.format
= format
;
499 if (stObj
->level_override
) {
500 templ
.u
.tex
.first_level
= templ
.u
.tex
.last_level
= stObj
->level_override
;
502 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
503 templ
.u
.tex
.last_level
= last_level(stObj
);
505 if (stObj
->layer_override
) {
506 templ
.u
.tex
.first_layer
= templ
.u
.tex
.last_layer
= stObj
->layer_override
;
508 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
509 templ
.u
.tex
.last_layer
= last_layer(stObj
);
511 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
512 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
513 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
515 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
516 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
517 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
518 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
520 return st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, &templ
);
524 struct pipe_sampler_view
*
525 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
526 struct st_texture_object
*stObj
,
527 const struct gl_sampler_object
*samp
,
528 bool glsl130_or_later
,
529 bool ignore_srgb_decode
)
531 const struct st_sampler_view
*sv
;
532 bool srgb_skip_decode
= false;
534 if (!ignore_srgb_decode
&& samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
)
535 srgb_skip_decode
= true;
537 sv
= st_texture_get_current_sampler_view(st
, stObj
);
540 sv
->glsl130_or_later
== glsl130_or_later
&&
541 sv
->srgb_skip_decode
== srgb_skip_decode
) {
542 /* Debug check: make sure that the sampler view's parameters are
543 * what they're supposed to be.
545 struct pipe_sampler_view
*view
= sv
->view
;
546 assert(stObj
->pt
== view
->texture
);
547 assert(!check_sampler_swizzle(st
, stObj
, view
, glsl130_or_later
));
548 assert(get_sampler_view_format(st
, stObj
, srgb_skip_decode
) == view
->format
);
549 assert(gl_target_to_pipe(stObj
->base
.Target
) == view
->target
);
550 assert(stObj
->level_override
||
551 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
== view
->u
.tex
.first_level
);
552 assert(stObj
->level_override
|| last_level(stObj
) == view
->u
.tex
.last_level
);
553 assert(stObj
->layer_override
|| stObj
->base
.MinLayer
== view
->u
.tex
.first_layer
);
554 assert(stObj
->layer_override
|| last_layer(stObj
) == view
->u
.tex
.last_layer
);
555 assert(!stObj
->layer_override
||
556 (stObj
->layer_override
== view
->u
.tex
.first_layer
&&
557 stObj
->layer_override
== view
->u
.tex
.last_layer
));
561 /* create new sampler view */
562 enum pipe_format format
= get_sampler_view_format(st
, stObj
,
564 struct pipe_sampler_view
*view
=
565 st_create_texture_sampler_view_from_stobj(st
, stObj
, format
,
568 view
= st_texture_set_sampler_view(st
, stObj
, view
,
569 glsl130_or_later
, srgb_skip_decode
);
575 struct pipe_sampler_view
*
576 st_get_buffer_sampler_view_from_stobj(struct st_context
*st
,
577 struct st_texture_object
*stObj
)
579 const struct st_sampler_view
*sv
;
580 struct st_buffer_object
*stBuf
=
581 st_buffer_object(stObj
->base
.BufferObject
);
583 if (!stBuf
|| !stBuf
->buffer
)
586 sv
= st_texture_get_current_sampler_view(st
, stObj
);
588 struct pipe_resource
*buf
= stBuf
->buffer
;
591 struct pipe_sampler_view
*view
= sv
->view
;
593 if (view
->texture
== buf
) {
594 /* Debug check: make sure that the sampler view's parameters are
595 * what they're supposed to be.
597 assert(st_mesa_format_to_pipe_format(st
,
598 stObj
->base
._BufferObjectFormat
)
600 assert(view
->target
== PIPE_BUFFER
);
601 unsigned base
= stObj
->base
.BufferOffset
;
602 MAYBE_UNUSED
unsigned size
= MIN2(buf
->width0
- base
,
603 (unsigned) stObj
->base
.BufferSize
);
604 assert(view
->u
.buf
.offset
== base
);
605 assert(view
->u
.buf
.size
== size
);
610 unsigned base
= stObj
->base
.BufferOffset
;
612 if (base
>= buf
->width0
)
615 unsigned size
= buf
->width0
- base
;
616 size
= MIN2(size
, (unsigned)stObj
->base
.BufferSize
);
620 /* Create a new sampler view. There is no need to clear the entire
621 * structure (consider CPU overhead).
623 struct pipe_sampler_view templ
;
626 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
627 templ
.target
= PIPE_BUFFER
;
628 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
629 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
630 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
631 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
632 templ
.u
.buf
.offset
= base
;
633 templ
.u
.buf
.size
= size
;
635 struct pipe_sampler_view
*view
=
636 st
->pipe
->create_sampler_view(st
->pipe
, buf
, &templ
);
638 view
= st_texture_set_sampler_view(st
, stObj
, view
, false, false);