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 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)));
255 static unsigned last_level(struct st_texture_object
*stObj
)
257 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
258 stObj
->pt
->last_level
);
259 if (stObj
->base
.Immutable
)
260 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
264 static unsigned last_layer(struct st_texture_object
*stObj
)
266 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
267 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
268 stObj
->pt
->array_size
- 1);
269 return stObj
->pt
->array_size
- 1;
272 static struct pipe_sampler_view
*
273 st_create_texture_sampler_view_from_stobj(struct st_context
*st
,
274 struct st_texture_object
*stObj
,
275 enum pipe_format format
,
276 unsigned glsl_version
)
278 struct pipe_sampler_view templ
;
279 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
281 u_sampler_view_default_template(&templ
,
285 if (stObj
->pt
->target
== PIPE_BUFFER
) {
288 base
= stObj
->base
.BufferOffset
;
289 if (base
>= stObj
->pt
->width0
)
291 size
= MIN2(stObj
->pt
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
295 templ
.u
.buf
.offset
= base
;
296 templ
.u
.buf
.size
= size
;
298 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
299 templ
.u
.tex
.last_level
= last_level(stObj
);
300 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
301 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
302 templ
.u
.tex
.last_layer
= last_layer(stObj
);
303 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
304 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
307 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
308 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
309 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
310 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
312 return st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, &templ
);
316 static struct pipe_sampler_view
*
317 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
318 struct st_texture_object
*stObj
,
319 enum pipe_format format
,
320 unsigned glsl_version
)
322 struct pipe_sampler_view
**sv
;
323 const struct st_texture_image
*firstImage
;
324 if (!stObj
|| !stObj
->pt
) {
328 sv
= st_texture_get_sampler_view(st
, stObj
);
330 if (util_format_is_depth_and_stencil(format
)) {
331 if (stObj
->base
.StencilSampling
)
332 format
= util_format_stencil_only(format
);
334 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
335 if (firstImage
->base
._BaseFormat
== GL_STENCIL_INDEX
)
336 format
= util_format_stencil_only(format
);
340 /* if sampler view has changed dereference it */
342 if (check_sampler_swizzle(st
, stObj
, *sv
, glsl_version
) ||
343 (format
!= (*sv
)->format
) ||
344 gl_target_to_pipe(stObj
->base
.Target
) != (*sv
)->target
||
345 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
!= (*sv
)->u
.tex
.first_level
||
346 last_level(stObj
) != (*sv
)->u
.tex
.last_level
||
347 stObj
->base
.MinLayer
!= (*sv
)->u
.tex
.first_layer
||
348 last_layer(stObj
) != (*sv
)->u
.tex
.last_layer
) {
349 pipe_sampler_view_reference(sv
, NULL
);
354 *sv
= st_create_texture_sampler_view_from_stobj(st
, stObj
,
355 format
, glsl_version
);
357 } else if ((*sv
)->context
!= st
->pipe
) {
358 /* Recreate view in correct context, use existing view as template */
359 struct pipe_sampler_view
*new_sv
=
360 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, *sv
);
361 pipe_sampler_view_reference(sv
, NULL
);
369 update_single_texture(struct st_context
*st
,
370 struct pipe_sampler_view
**sampler_view
,
371 GLuint texUnit
, unsigned glsl_version
)
373 struct gl_context
*ctx
= st
->ctx
;
374 const struct gl_sampler_object
*samp
;
375 struct gl_texture_object
*texObj
;
376 struct st_texture_object
*stObj
;
377 enum pipe_format view_format
;
380 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
382 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
385 texObj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
386 samp
= &texObj
->Sampler
;
388 stObj
= st_texture_object(texObj
);
390 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
396 /* Determine the format of the texture sampler view */
397 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
399 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
403 stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
405 /* If sRGB decoding is off, use the linear format */
406 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
) {
407 view_format
= util_format_linear(view_format
);
411 switch (view_format
) {
412 case PIPE_FORMAT_NV12
:
413 case PIPE_FORMAT_IYUV
:
414 view_format
= PIPE_FORMAT_R8_UNORM
;
421 st_get_texture_sampler_view_from_stobj(st
, stObj
, view_format
,
429 update_textures(struct st_context
*st
,
430 gl_shader_stage mesa_shader
,
431 const struct gl_program
*prog
,
433 struct pipe_sampler_view
**sampler_views
,
434 unsigned *num_textures
)
436 const GLuint old_max
= *num_textures
;
437 GLbitfield samplers_used
= prog
->SamplersUsed
;
438 GLbitfield free_slots
= ~prog
->SamplersUsed
;
439 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
441 struct gl_shader_program
*shader
=
442 st
->ctx
->_Shader
->CurrentProgram
[mesa_shader
];
443 unsigned glsl_version
= shader
? shader
->Version
: 0;
444 enum pipe_shader_type shader_stage
= st_shader_stage_to_ptarget(mesa_shader
);
446 if (samplers_used
== 0x0 && old_max
== 0)
451 /* loop over sampler units (aka tex image units) */
452 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
453 struct pipe_sampler_view
*sampler_view
= NULL
;
455 if (samplers_used
& 1) {
456 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
459 retval
= update_single_texture(st
, &sampler_view
, texUnit
,
461 if (retval
== GL_FALSE
)
464 *num_textures
= unit
+ 1;
466 else if (samplers_used
== 0 && unit
>= old_max
) {
467 /* if we've reset all the old views and we have no more new ones */
471 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
474 /* For any external samplers with multiplaner YUV, stuff the additional
475 * sampler views we need at the end.
477 * Trying to cache the sampler view in the stObj looks painful, so just
478 * re-create the sampler view for the extra planes each time. Main use
479 * case is video playback (ie. fps games wouldn't be using this) so I
480 * guess no point to try to optimize this feature.
482 while (unlikely(external_samplers_used
)) {
483 GLuint unit
= u_bit_scan(&external_samplers_used
);
485 struct st_texture_object
*stObj
=
486 st_get_texture_object(st
->ctx
, prog
, unit
);
487 struct pipe_sampler_view tmpl
;
492 /* use original view as template: */
493 tmpl
= *sampler_views
[unit
];
495 switch (st_get_view_format(stObj
)) {
496 case PIPE_FORMAT_NV12
:
497 /* we need one additional R8G8 view: */
498 tmpl
.format
= PIPE_FORMAT_RG88_UNORM
;
499 tmpl
.swizzle_g
= PIPE_SWIZZLE_Y
; /* tmpl from Y plane is R8 */
500 extra
= u_bit_scan(&free_slots
);
501 sampler_views
[extra
] =
502 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
504 case PIPE_FORMAT_IYUV
:
505 /* we need two additional R8 views: */
506 tmpl
.format
= PIPE_FORMAT_R8_UNORM
;
507 extra
= u_bit_scan(&free_slots
);
508 sampler_views
[extra
] =
509 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
510 extra
= u_bit_scan(&free_slots
);
511 sampler_views
[extra
] =
512 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
->next
, &tmpl
);
518 *num_textures
= MAX2(*num_textures
, extra
+ 1);
521 cso_set_sampler_views(st
->cso_context
,
530 update_vertex_textures(struct st_context
*st
)
532 const struct gl_context
*ctx
= st
->ctx
;
534 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
537 &ctx
->VertexProgram
._Current
->Base
,
538 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
539 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
540 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
546 update_fragment_textures(struct st_context
*st
)
548 const struct gl_context
*ctx
= st
->ctx
;
551 MESA_SHADER_FRAGMENT
,
552 &ctx
->FragmentProgram
._Current
->Base
,
553 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
554 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
555 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
560 update_geometry_textures(struct st_context
*st
)
562 const struct gl_context
*ctx
= st
->ctx
;
564 if (ctx
->GeometryProgram
._Current
) {
566 MESA_SHADER_GEOMETRY
,
567 &ctx
->GeometryProgram
._Current
->Base
,
568 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
569 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
570 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
576 update_tessctrl_textures(struct st_context
*st
)
578 const struct gl_context
*ctx
= st
->ctx
;
580 if (ctx
->TessCtrlProgram
._Current
) {
582 MESA_SHADER_TESS_CTRL
,
583 &ctx
->TessCtrlProgram
._Current
->Base
,
584 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
585 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
586 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
592 update_tesseval_textures(struct st_context
*st
)
594 const struct gl_context
*ctx
= st
->ctx
;
596 if (ctx
->TessEvalProgram
._Current
) {
598 MESA_SHADER_TESS_EVAL
,
599 &ctx
->TessEvalProgram
._Current
->Base
,
600 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
601 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
602 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
608 update_compute_textures(struct st_context
*st
)
610 const struct gl_context
*ctx
= st
->ctx
;
612 if (ctx
->ComputeProgram
._Current
) {
615 &ctx
->ComputeProgram
._Current
->Base
,
616 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
617 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
618 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);
623 const struct st_tracked_state st_update_fragment_texture
= {
624 update_fragment_textures
/* update */
628 const struct st_tracked_state st_update_vertex_texture
= {
629 update_vertex_textures
/* update */
633 const struct st_tracked_state st_update_geometry_texture
= {
634 update_geometry_textures
/* update */
638 const struct st_tracked_state st_update_tessctrl_texture
= {
639 update_tessctrl_textures
/* update */
643 const struct st_tracked_state st_update_tesseval_texture
= {
644 update_tesseval_textures
/* update */
648 const struct st_tracked_state st_update_compute_texture
= {
649 update_compute_textures
/* update */