1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "main/samplerobj.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41 #include "program/prog_instruction.h"
43 #include "st_context.h"
45 #include "st_texture.h"
46 #include "st_format.h"
47 #include "st_cb_texture.h"
48 #include "pipe/p_context.h"
49 #include "util/u_format.h"
50 #include "util/u_inlines.h"
51 #include "cso_cache/cso_context.h"
55 * Return swizzle1(swizzle2)
58 swizzle_swizzle(unsigned swizzle1
, unsigned swizzle2
)
62 if (swizzle1
== SWIZZLE_XYZW
) {
63 /* identity swizzle, no change to swizzle2 */
67 for (i
= 0; i
< 4; i
++) {
68 unsigned s
= GET_SWZ(swizzle1
, i
);
74 swz
[i
] = GET_SWZ(swizzle2
, s
);
77 swz
[i
] = SWIZZLE_ZERO
;
83 assert(!"Bad swizzle term");
88 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
93 * Given a user-specified texture base format, the actual gallium texture
94 * format and the current GL_DEPTH_MODE, return a texture swizzle.
96 * Consider the case where the user requests a GL_RGB internal texture
97 * format the driver actually uses an RGBA format. The A component should
98 * be ignored and sampling from the texture should always return (r,g,b,1).
99 * But if we rendered to the texture we might have written A values != 1.
100 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
101 * This function computes the texture swizzle needed to get the expected
104 * In the case of depth textures, the GL_DEPTH_MODE state determines the
107 * This result must be composed with the user-specified swizzle to get
111 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
112 enum pipe_format actualFormat
,
113 unsigned glsl_version
)
115 switch (baseFormat
) {
119 if (util_format_has_alpha(actualFormat
))
120 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
124 if (util_format_get_nr_components(actualFormat
) > 2)
125 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
129 if (util_format_get_nr_components(actualFormat
) > 1)
130 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
131 SWIZZLE_ZERO
, SWIZZLE_ONE
);
135 if (util_format_get_nr_components(actualFormat
) > 1)
136 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
137 SWIZZLE_ZERO
, SWIZZLE_W
);
141 if (util_format_get_nr_components(actualFormat
) > 1)
142 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
145 case GL_LUMINANCE_ALPHA
:
146 if (util_format_get_nr_components(actualFormat
) > 2)
147 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
151 if (util_format_get_nr_components(actualFormat
) > 1)
155 case GL_STENCIL_INDEX
:
156 case GL_DEPTH_STENCIL
:
157 case GL_DEPTH_COMPONENT
:
158 /* Now examine the depth mode */
161 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
163 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
165 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
166 * the depth mode and return float, while older shadow* functions
167 * and ARB_fp instructions return vec4 according to the depth mode.
169 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
170 * them to return 0, breaking them completely.
172 * A proper fix would increase code complexity and that's not worth
173 * it for a rarely used feature such as the GL_ALPHA depth mode
174 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
175 * shaders that use GLSL 1.30 or later.
177 * BTW, it's required that sampler views are updated when
178 * shaders change (check_sampler_swizzle takes care of that).
180 if (glsl_version
&& glsl_version
>= 130)
183 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
184 SWIZZLE_ZERO
, SWIZZLE_X
);
186 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
187 SWIZZLE_ZERO
, SWIZZLE_ONE
);
189 assert(!"Unexpected depthMode");
193 assert(!"Unexpected baseFormat");
200 get_texture_format_swizzle(const struct st_context
*st
,
201 const struct st_texture_object
*stObj
,
202 unsigned glsl_version
)
204 GLenum baseFormat
= _mesa_texture_base_format(&stObj
->base
);
205 unsigned tex_swizzle
;
207 if (baseFormat
!= GL_NONE
) {
208 GLenum depth_mode
= stObj
->base
.DepthMode
;
209 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
210 * with depth component data specified with a sized internal format.
212 if (_mesa_is_gles3(st
->ctx
) &&
213 util_format_is_depth_or_stencil(stObj
->pt
->format
)) {
214 const struct gl_texture_image
*firstImage
=
215 _mesa_base_tex_image(&stObj
->base
);
216 if (firstImage
->InternalFormat
!= GL_DEPTH_COMPONENT
&&
217 firstImage
->InternalFormat
!= GL_DEPTH_STENCIL
&&
218 firstImage
->InternalFormat
!= GL_STENCIL_INDEX
)
221 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
227 tex_swizzle
= SWIZZLE_XYZW
;
230 /* Combine the texture format swizzle with user's swizzle */
231 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
236 * Return TRUE if the texture's sampler view swizzle is not equal to
237 * the texture's swizzle.
239 * \param stObj the st texture object,
242 check_sampler_swizzle(const struct st_context
*st
,
243 const struct st_texture_object
*stObj
,
244 const struct pipe_sampler_view
*sv
, unsigned glsl_version
)
246 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
248 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
249 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
250 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
251 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
256 last_level(const struct st_texture_object
*stObj
)
258 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
259 stObj
->pt
->last_level
);
260 if (stObj
->base
.Immutable
)
261 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
266 last_layer(const struct st_texture_object
*stObj
)
268 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
269 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
270 stObj
->pt
->array_size
- 1);
271 return stObj
->pt
->array_size
- 1;
274 static struct pipe_sampler_view
*
275 st_create_texture_sampler_view_from_stobj(struct st_context
*st
,
276 struct st_texture_object
*stObj
,
277 enum pipe_format format
,
278 unsigned glsl_version
)
280 struct pipe_sampler_view templ
;
281 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
283 u_sampler_view_default_template(&templ
,
287 if (stObj
->pt
->target
== PIPE_BUFFER
) {
290 base
= stObj
->base
.BufferOffset
;
291 if (base
>= stObj
->pt
->width0
)
293 size
= MIN2(stObj
->pt
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
297 templ
.u
.buf
.offset
= base
;
298 templ
.u
.buf
.size
= size
;
300 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
301 templ
.u
.tex
.last_level
= last_level(stObj
);
302 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
303 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
304 templ
.u
.tex
.last_layer
= last_layer(stObj
);
305 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
306 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
309 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
310 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
311 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
312 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
314 return st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, &templ
);
318 static struct pipe_sampler_view
*
319 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
320 struct st_texture_object
*stObj
,
321 enum pipe_format format
,
322 unsigned glsl_version
)
324 struct pipe_sampler_view
**sv
;
325 const struct st_texture_image
*firstImage
;
326 if (!stObj
|| !stObj
->pt
) {
330 sv
= st_texture_get_sampler_view(st
, stObj
);
332 if (util_format_is_depth_and_stencil(format
)) {
333 if (stObj
->base
.StencilSampling
)
334 format
= util_format_stencil_only(format
);
336 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
337 if (firstImage
->base
._BaseFormat
== GL_STENCIL_INDEX
)
338 format
= util_format_stencil_only(format
);
342 /* if sampler view has changed dereference it */
344 if (check_sampler_swizzle(st
, stObj
, *sv
, glsl_version
) ||
345 (format
!= (*sv
)->format
) ||
346 gl_target_to_pipe(stObj
->base
.Target
) != (*sv
)->target
||
347 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
!= (*sv
)->u
.tex
.first_level
||
348 last_level(stObj
) != (*sv
)->u
.tex
.last_level
||
349 stObj
->base
.MinLayer
!= (*sv
)->u
.tex
.first_layer
||
350 last_layer(stObj
) != (*sv
)->u
.tex
.last_layer
) {
351 pipe_sampler_view_reference(sv
, NULL
);
356 *sv
= st_create_texture_sampler_view_from_stobj(st
, stObj
,
357 format
, glsl_version
);
359 } else if ((*sv
)->context
!= st
->pipe
) {
360 /* Recreate view in correct context, use existing view as template */
361 struct pipe_sampler_view
*new_sv
=
362 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, *sv
);
363 pipe_sampler_view_reference(sv
, NULL
);
371 update_single_texture(struct st_context
*st
,
372 struct pipe_sampler_view
**sampler_view
,
373 GLuint texUnit
, unsigned glsl_version
)
375 struct gl_context
*ctx
= st
->ctx
;
376 const struct gl_sampler_object
*samp
;
377 struct gl_texture_object
*texObj
;
378 struct st_texture_object
*stObj
;
379 enum pipe_format view_format
;
382 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
384 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
387 texObj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
388 samp
= &texObj
->Sampler
;
390 stObj
= st_texture_object(texObj
);
392 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
398 /* Determine the format of the texture sampler view */
399 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
401 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
405 stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
407 /* If sRGB decoding is off, use the linear format */
408 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
) {
409 view_format
= util_format_linear(view_format
);
413 switch (view_format
) {
414 case PIPE_FORMAT_NV12
:
415 case PIPE_FORMAT_IYUV
:
416 view_format
= PIPE_FORMAT_R8_UNORM
;
423 st_get_texture_sampler_view_from_stobj(st
, stObj
, view_format
,
431 update_textures(struct st_context
*st
,
432 gl_shader_stage mesa_shader
,
433 const struct gl_program
*prog
,
435 struct pipe_sampler_view
**sampler_views
,
436 unsigned *num_textures
)
438 const GLuint old_max
= *num_textures
;
439 GLbitfield samplers_used
= prog
->SamplersUsed
;
440 GLbitfield free_slots
= ~prog
->SamplersUsed
;
441 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
443 struct gl_shader_program
*shader
=
444 st
->ctx
->_Shader
->CurrentProgram
[mesa_shader
];
445 unsigned glsl_version
= shader
? shader
->Version
: 0;
446 enum pipe_shader_type shader_stage
= st_shader_stage_to_ptarget(mesa_shader
);
448 if (samplers_used
== 0x0 && old_max
== 0)
453 /* loop over sampler units (aka tex image units) */
454 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
455 struct pipe_sampler_view
*sampler_view
= NULL
;
457 if (samplers_used
& 1) {
458 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
461 retval
= update_single_texture(st
, &sampler_view
, texUnit
,
463 if (retval
== GL_FALSE
)
466 *num_textures
= unit
+ 1;
468 else if (samplers_used
== 0 && unit
>= old_max
) {
469 /* if we've reset all the old views and we have no more new ones */
473 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
476 /* For any external samplers with multiplaner YUV, stuff the additional
477 * sampler views we need at the end.
479 * Trying to cache the sampler view in the stObj looks painful, so just
480 * re-create the sampler view for the extra planes each time. Main use
481 * case is video playback (ie. fps games wouldn't be using this) so I
482 * guess no point to try to optimize this feature.
484 while (unlikely(external_samplers_used
)) {
485 GLuint unit
= u_bit_scan(&external_samplers_used
);
487 struct st_texture_object
*stObj
=
488 st_get_texture_object(st
->ctx
, prog
, unit
);
489 struct pipe_sampler_view tmpl
;
494 /* use original view as template: */
495 tmpl
= *sampler_views
[unit
];
497 switch (st_get_view_format(stObj
)) {
498 case PIPE_FORMAT_NV12
:
499 /* we need one additional R8G8 view: */
500 tmpl
.format
= PIPE_FORMAT_RG88_UNORM
;
501 tmpl
.swizzle_g
= PIPE_SWIZZLE_Y
; /* tmpl from Y plane is R8 */
502 extra
= u_bit_scan(&free_slots
);
503 sampler_views
[extra
] =
504 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
506 case PIPE_FORMAT_IYUV
:
507 /* we need two additional R8 views: */
508 tmpl
.format
= PIPE_FORMAT_R8_UNORM
;
509 extra
= u_bit_scan(&free_slots
);
510 sampler_views
[extra
] =
511 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
512 extra
= u_bit_scan(&free_slots
);
513 sampler_views
[extra
] =
514 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
->next
, &tmpl
);
520 *num_textures
= MAX2(*num_textures
, extra
+ 1);
523 cso_set_sampler_views(st
->cso_context
,
532 update_vertex_textures(struct st_context
*st
)
534 const struct gl_context
*ctx
= st
->ctx
;
536 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
539 &ctx
->VertexProgram
._Current
->Base
,
540 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
541 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
542 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
548 update_fragment_textures(struct st_context
*st
)
550 const struct gl_context
*ctx
= st
->ctx
;
553 MESA_SHADER_FRAGMENT
,
554 &ctx
->FragmentProgram
._Current
->Base
,
555 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
556 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
557 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
562 update_geometry_textures(struct st_context
*st
)
564 const struct gl_context
*ctx
= st
->ctx
;
566 if (ctx
->GeometryProgram
._Current
) {
568 MESA_SHADER_GEOMETRY
,
569 &ctx
->GeometryProgram
._Current
->Base
,
570 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
571 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
572 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
578 update_tessctrl_textures(struct st_context
*st
)
580 const struct gl_context
*ctx
= st
->ctx
;
582 if (ctx
->TessCtrlProgram
._Current
) {
584 MESA_SHADER_TESS_CTRL
,
585 &ctx
->TessCtrlProgram
._Current
->Base
,
586 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
587 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
588 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
594 update_tesseval_textures(struct st_context
*st
)
596 const struct gl_context
*ctx
= st
->ctx
;
598 if (ctx
->TessEvalProgram
._Current
) {
600 MESA_SHADER_TESS_EVAL
,
601 &ctx
->TessEvalProgram
._Current
->Base
,
602 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
603 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
604 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
610 update_compute_textures(struct st_context
*st
)
612 const struct gl_context
*ctx
= st
->ctx
;
614 if (ctx
->ComputeProgram
._Current
) {
617 &ctx
->ComputeProgram
._Current
->Base
,
618 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
619 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
620 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);
625 const struct st_tracked_state st_update_fragment_texture
= {
626 update_fragment_textures
/* update */
630 const struct st_tracked_state st_update_vertex_texture
= {
631 update_vertex_textures
/* update */
635 const struct st_tracked_state st_update_geometry_texture
= {
636 update_geometry_textures
/* update */
640 const struct st_tracked_state st_update_tessctrl_texture
= {
641 update_tessctrl_textures
/* update */
645 const struct st_tracked_state st_update_tesseval_texture
= {
646 update_tesseval_textures
/* update */
650 const struct st_tracked_state st_update_compute_texture
= {
651 update_compute_textures
/* update */