e8a46a722440f6190f8b0cd0e61d66d5e879522f
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 * Try to find a matching sampler view for the given context.
47 * If none is found an empty slot is initialized with a
48 * template and returned instead.
50 static struct pipe_sampler_view
**
51 st_texture_get_sampler_view(struct st_context
*st
,
52 struct st_texture_object
*stObj
)
54 struct pipe_sampler_view
**free
= NULL
;
57 for (i
= 0; i
< stObj
->num_sampler_views
; ++i
) {
58 struct pipe_sampler_view
**sv
= &stObj
->sampler_views
[i
];
59 /* Is the array entry used ? */
61 /* check if the context matches */
62 if ((*sv
)->context
== st
->pipe
) {
66 /* Found a free slot, remember that */
71 /* Couldn't find a slot for our context, create a new one */
74 /* Haven't even found a free one, resize the array */
75 unsigned new_size
= (stObj
->num_sampler_views
+ 1) *
76 sizeof(struct pipe_sampler_view
*);
77 stObj
->sampler_views
= realloc(stObj
->sampler_views
, new_size
);
78 free
= &stObj
->sampler_views
[stObj
->num_sampler_views
++];
82 assert(*free
== NULL
);
89 * For the given texture object, release any sampler views which belong
90 * to the calling context.
93 st_texture_release_sampler_view(struct st_context
*st
,
94 struct st_texture_object
*stObj
)
98 for (i
= 0; i
< stObj
->num_sampler_views
; ++i
) {
99 struct pipe_sampler_view
**sv
= &stObj
->sampler_views
[i
];
101 if (*sv
&& (*sv
)->context
== st
->pipe
) {
102 pipe_sampler_view_reference(sv
, NULL
);
110 * Release all sampler views attached to the given texture object, regardless
114 st_texture_release_all_sampler_views(struct st_context
*st
,
115 struct st_texture_object
*stObj
)
119 /* XXX This should use sampler_views[i]->pipe, not st->pipe */
120 for (i
= 0; i
< stObj
->num_sampler_views
; ++i
)
121 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_views
[i
]);
126 st_texture_free_sampler_views(struct st_texture_object
*stObj
)
128 free(stObj
->sampler_views
);
129 stObj
->sampler_views
= NULL
;
130 stObj
->num_sampler_views
= 0;
135 * Return swizzle1(swizzle2)
138 swizzle_swizzle(unsigned swizzle1
, unsigned swizzle2
)
142 if (swizzle1
== SWIZZLE_XYZW
) {
143 /* identity swizzle, no change to swizzle2 */
147 for (i
= 0; i
< 4; i
++) {
148 unsigned s
= GET_SWZ(swizzle1
, i
);
154 swz
[i
] = GET_SWZ(swizzle2
, s
);
157 swz
[i
] = SWIZZLE_ZERO
;
160 swz
[i
] = SWIZZLE_ONE
;
163 assert(!"Bad swizzle term");
168 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
173 * Given a user-specified texture base format, the actual gallium texture
174 * format and the current GL_DEPTH_MODE, return a texture swizzle.
176 * Consider the case where the user requests a GL_RGB internal texture
177 * format the driver actually uses an RGBA format. The A component should
178 * be ignored and sampling from the texture should always return (r,g,b,1).
179 * But if we rendered to the texture we might have written A values != 1.
180 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
181 * This function computes the texture swizzle needed to get the expected
184 * In the case of depth textures, the GL_DEPTH_MODE state determines the
187 * This result must be composed with the user-specified swizzle to get
191 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
192 enum pipe_format actualFormat
,
193 unsigned glsl_version
)
195 switch (baseFormat
) {
199 if (util_format_has_alpha(actualFormat
))
200 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
204 if (util_format_get_nr_components(actualFormat
) > 2)
205 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
209 if (util_format_get_nr_components(actualFormat
) > 1)
210 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
211 SWIZZLE_ZERO
, SWIZZLE_ONE
);
215 if (util_format_get_nr_components(actualFormat
) > 1)
216 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
217 SWIZZLE_ZERO
, SWIZZLE_W
);
221 if (util_format_get_nr_components(actualFormat
) > 1)
222 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
225 case GL_LUMINANCE_ALPHA
:
226 if (util_format_get_nr_components(actualFormat
) > 2)
227 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
231 if (util_format_get_nr_components(actualFormat
) > 1)
235 case GL_STENCIL_INDEX
:
236 case GL_DEPTH_STENCIL
:
237 case GL_DEPTH_COMPONENT
:
238 /* Now examine the depth mode */
241 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
243 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
245 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
246 * the depth mode and return float, while older shadow* functions
247 * and ARB_fp instructions return vec4 according to the depth mode.
249 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
250 * them to return 0, breaking them completely.
252 * A proper fix would increase code complexity and that's not worth
253 * it for a rarely used feature such as the GL_ALPHA depth mode
254 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
255 * shaders that use GLSL 1.30 or later.
257 * BTW, it's required that sampler views are updated when
258 * shaders change (check_sampler_swizzle takes care of that).
260 if (glsl_version
&& glsl_version
>= 130)
263 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
264 SWIZZLE_ZERO
, SWIZZLE_X
);
266 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
267 SWIZZLE_ZERO
, SWIZZLE_ONE
);
269 assert(!"Unexpected depthMode");
273 assert(!"Unexpected baseFormat");
280 get_texture_format_swizzle(const struct st_context
*st
,
281 const struct st_texture_object
*stObj
,
282 unsigned glsl_version
)
284 GLenum baseFormat
= _mesa_base_tex_image(&stObj
->base
)->_BaseFormat
;
285 unsigned tex_swizzle
;
286 GLenum depth_mode
= stObj
->base
.DepthMode
;
288 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
289 * with depth component data specified with a sized internal format.
291 if (_mesa_is_gles3(st
->ctx
) &&
292 (baseFormat
== GL_DEPTH_COMPONENT
||
293 baseFormat
== GL_DEPTH_STENCIL
||
294 baseFormat
== GL_STENCIL_INDEX
)) {
295 const struct gl_texture_image
*firstImage
=
296 _mesa_base_tex_image(&stObj
->base
);
297 if (firstImage
->InternalFormat
!= GL_DEPTH_COMPONENT
&&
298 firstImage
->InternalFormat
!= GL_DEPTH_STENCIL
&&
299 firstImage
->InternalFormat
!= GL_STENCIL_INDEX
)
302 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
307 /* Combine the texture format swizzle with user's swizzle */
308 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
313 * Return TRUE if the texture's sampler view swizzle is not equal to
314 * the texture's swizzle.
316 * \param stObj the st texture object,
318 MAYBE_UNUSED
static boolean
319 check_sampler_swizzle(const struct st_context
*st
,
320 const struct st_texture_object
*stObj
,
321 const struct pipe_sampler_view
*sv
, unsigned glsl_version
)
323 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
325 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
326 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
327 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
328 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
333 last_level(const struct st_texture_object
*stObj
)
335 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
336 stObj
->pt
->last_level
);
337 if (stObj
->base
.Immutable
)
338 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
344 last_layer(const struct st_texture_object
*stObj
)
346 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
347 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
348 stObj
->pt
->array_size
- 1);
349 return stObj
->pt
->array_size
- 1;
354 * Determine the format for the texture sampler view.
356 static enum pipe_format
357 get_sampler_view_format(struct st_context
*st
,
358 const struct st_texture_object
*stObj
,
359 const struct gl_sampler_object
*samp
)
361 enum pipe_format format
;
363 GLenum baseFormat
= _mesa_base_tex_image(&stObj
->base
)->_BaseFormat
;
364 format
= stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
366 if (baseFormat
== GL_DEPTH_COMPONENT
||
367 baseFormat
== GL_DEPTH_STENCIL
||
368 baseFormat
== GL_STENCIL_INDEX
) {
369 if (stObj
->base
.StencilSampling
|| baseFormat
== GL_STENCIL_INDEX
)
370 format
= util_format_stencil_only(format
);
375 /* If sRGB decoding is off, use the linear format */
376 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
)
377 format
= util_format_linear(format
);
379 /* Use R8_UNORM for video formats */
381 case PIPE_FORMAT_NV12
:
382 case PIPE_FORMAT_IYUV
:
383 format
= PIPE_FORMAT_R8_UNORM
;
392 static struct pipe_sampler_view
*
393 st_create_texture_sampler_view_from_stobj(struct st_context
*st
,
394 struct st_texture_object
*stObj
,
395 enum pipe_format format
,
396 unsigned glsl_version
)
398 struct pipe_sampler_view templ
;
399 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
401 u_sampler_view_default_template(&templ
, stObj
->pt
, format
);
403 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
404 templ
.u
.tex
.last_level
= last_level(stObj
);
405 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
406 if (stObj
->layer_override
) {
407 templ
.u
.tex
.first_layer
= templ
.u
.tex
.last_layer
= stObj
->layer_override
;
409 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
410 templ
.u
.tex
.last_layer
= last_layer(stObj
);
412 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
413 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
415 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
416 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
417 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
418 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
420 return st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, &templ
);
424 struct pipe_sampler_view
*
425 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
426 struct st_texture_object
*stObj
,
427 const struct gl_sampler_object
*samp
,
428 unsigned glsl_version
)
430 struct pipe_sampler_view
**sv
;
432 if (!stObj
|| !stObj
->pt
) {
436 sv
= st_texture_get_sampler_view(st
, stObj
);
439 /* Debug check: make sure that the sampler view's parameters are
440 * what they're supposed to be.
442 MAYBE_UNUSED
struct pipe_sampler_view
*view
= *sv
;
443 assert(stObj
->pt
== view
->texture
);
444 assert(!check_sampler_swizzle(st
, stObj
, view
, glsl_version
));
445 assert(get_sampler_view_format(st
, stObj
, samp
) == view
->format
);
446 assert(gl_target_to_pipe(stObj
->base
.Target
) == view
->target
);
447 assert(stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
==
448 view
->u
.tex
.first_level
);
449 assert(last_level(stObj
) == view
->u
.tex
.last_level
);
450 assert(stObj
->layer_override
|| stObj
->base
.MinLayer
== view
->u
.tex
.first_layer
);
451 assert(stObj
->layer_override
|| last_layer(stObj
) == view
->u
.tex
.last_layer
);
452 assert(!stObj
->layer_override
||
453 (stObj
->layer_override
== view
->u
.tex
.first_layer
&&
454 stObj
->layer_override
== view
->u
.tex
.last_layer
));
457 /* create new sampler view */
458 enum pipe_format format
= get_sampler_view_format(st
, stObj
, samp
);
460 *sv
= st_create_texture_sampler_view_from_stobj(st
, stObj
,
461 format
, glsl_version
);
469 struct pipe_sampler_view
*
470 st_get_buffer_sampler_view_from_stobj(struct st_context
*st
,
471 struct st_texture_object
*stObj
)
473 struct pipe_sampler_view
**sv
;
474 struct st_buffer_object
*stBuf
=
475 st_buffer_object(stObj
->base
.BufferObject
);
477 if (!stBuf
|| !stBuf
->buffer
)
480 sv
= st_texture_get_sampler_view(st
, stObj
);
482 struct pipe_resource
*buf
= stBuf
->buffer
;
483 struct pipe_sampler_view
*view
= *sv
;
485 if (view
&& view
->texture
== buf
) {
486 /* Debug check: make sure that the sampler view's parameters are
487 * what they're supposed to be.
489 assert(st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
)
491 assert(view
->target
== PIPE_BUFFER
);
492 unsigned base
= stObj
->base
.BufferOffset
;
493 MAYBE_UNUSED
unsigned size
= MIN2(buf
->width0
- base
,
494 (unsigned) stObj
->base
.BufferSize
);
495 assert(view
->u
.buf
.offset
== base
);
496 assert(view
->u
.buf
.size
== size
);
498 unsigned base
= stObj
->base
.BufferOffset
;
500 if (base
>= buf
->width0
)
503 unsigned size
= buf
->width0
- base
;
504 size
= MIN2(size
, (unsigned)stObj
->base
.BufferSize
);
508 /* Create a new sampler view. There is no need to clear the entire
509 * structure (consider CPU overhead).
511 struct pipe_sampler_view templ
;
514 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
515 templ
.target
= PIPE_BUFFER
;
516 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
517 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
518 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
519 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
520 templ
.u
.buf
.offset
= base
;
521 templ
.u
.buf
.size
= size
;
523 pipe_sampler_view_reference(sv
, NULL
);
524 *sv
= st
->pipe
->create_sampler_view(st
->pipe
, buf
, &templ
);