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 for (i
= 0; i
< 4; i
++) {
63 unsigned s
= GET_SWZ(swizzle1
, i
);
69 swz
[i
] = GET_SWZ(swizzle2
, s
);
72 swz
[i
] = SWIZZLE_ZERO
;
78 assert(!"Bad swizzle term");
83 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
88 * Given a user-specified texture base format, the actual gallium texture
89 * format and the current GL_DEPTH_MODE, return a texture swizzle.
91 * Consider the case where the user requests a GL_RGB internal texture
92 * format the driver actually uses an RGBA format. The A component should
93 * be ignored and sampling from the texture should always return (r,g,b,1).
94 * But if we rendered to the texture we might have written A values != 1.
95 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
96 * This function computes the texture swizzle needed to get the expected
99 * In the case of depth textures, the GL_DEPTH_MODE state determines the
102 * This result must be composed with the user-specified swizzle to get
106 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
107 enum pipe_format actualFormat
,
108 unsigned glsl_version
)
110 switch (baseFormat
) {
114 if (util_format_has_alpha(actualFormat
))
115 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
119 if (util_format_get_nr_components(actualFormat
) > 2)
120 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
124 if (util_format_get_nr_components(actualFormat
) > 1)
125 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
126 SWIZZLE_ZERO
, SWIZZLE_ONE
);
130 if (util_format_get_nr_components(actualFormat
) > 1)
131 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
132 SWIZZLE_ZERO
, SWIZZLE_W
);
136 if (util_format_get_nr_components(actualFormat
) > 1)
137 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
140 case GL_LUMINANCE_ALPHA
:
141 if (util_format_get_nr_components(actualFormat
) > 2)
142 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
146 if (util_format_get_nr_components(actualFormat
) > 1)
150 case GL_STENCIL_INDEX
:
151 case GL_DEPTH_STENCIL
:
152 case GL_DEPTH_COMPONENT
:
153 /* Now examine the depth mode */
156 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
158 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
160 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
161 * the depth mode and return float, while older shadow* functions
162 * and ARB_fp instructions return vec4 according to the depth mode.
164 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
165 * them to return 0, breaking them completely.
167 * A proper fix would increase code complexity and that's not worth
168 * it for a rarely used feature such as the GL_ALPHA depth mode
169 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
170 * shaders that use GLSL 1.30 or later.
172 * BTW, it's required that sampler views are updated when
173 * shaders change (check_sampler_swizzle takes care of that).
175 if (glsl_version
&& glsl_version
>= 130)
178 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
179 SWIZZLE_ZERO
, SWIZZLE_X
);
181 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
182 SWIZZLE_ZERO
, SWIZZLE_ONE
);
184 assert(!"Unexpected depthMode");
188 assert(!"Unexpected baseFormat");
195 get_texture_format_swizzle(const struct st_context
*st
,
196 const struct st_texture_object
*stObj
,
197 unsigned glsl_version
)
199 GLenum baseFormat
= _mesa_texture_base_format(&stObj
->base
);
200 unsigned tex_swizzle
;
202 if (baseFormat
!= GL_NONE
) {
203 GLenum depth_mode
= stObj
->base
.DepthMode
;
204 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
205 * with depth component data specified with a sized internal format.
207 if (_mesa_is_gles3(st
->ctx
) &&
208 util_format_is_depth_or_stencil(stObj
->pt
->format
)) {
209 const struct st_texture_image
*firstImage
=
210 st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
211 if (firstImage
->base
.InternalFormat
!= GL_DEPTH_COMPONENT
&&
212 firstImage
->base
.InternalFormat
!= GL_DEPTH_STENCIL
&&
213 firstImage
->base
.InternalFormat
!= GL_STENCIL_INDEX
)
216 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
222 tex_swizzle
= SWIZZLE_XYZW
;
225 /* Combine the texture format swizzle with user's swizzle */
226 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
231 * Return TRUE if the texture's sampler view swizzle is not equal to
232 * the texture's swizzle.
234 * \param stObj the st texture object,
237 check_sampler_swizzle(const struct st_context
*st
,
238 const struct st_texture_object
*stObj
,
239 struct pipe_sampler_view
*sv
, unsigned glsl_version
)
241 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
243 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
244 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
245 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
246 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
250 static unsigned last_level(struct st_texture_object
*stObj
)
252 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
253 stObj
->pt
->last_level
);
254 if (stObj
->base
.Immutable
)
255 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
259 static unsigned last_layer(struct st_texture_object
*stObj
)
261 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
262 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
263 stObj
->pt
->array_size
- 1);
264 return stObj
->pt
->array_size
- 1;
267 static struct pipe_sampler_view
*
268 st_create_texture_sampler_view_from_stobj(struct st_context
*st
,
269 struct st_texture_object
*stObj
,
270 enum pipe_format format
,
271 unsigned glsl_version
)
273 struct pipe_sampler_view templ
;
274 unsigned swizzle
= get_texture_format_swizzle(st
, stObj
, glsl_version
);
276 u_sampler_view_default_template(&templ
,
280 if (stObj
->pt
->target
== PIPE_BUFFER
) {
283 const struct util_format_description
*desc
284 = util_format_description(templ
.format
);
286 base
= stObj
->base
.BufferOffset
;
287 if (base
>= stObj
->pt
->width0
)
289 size
= MIN2(stObj
->pt
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
291 f
= (base
/ (desc
->block
.bits
/ 8)) * desc
->block
.width
;
292 n
= (size
/ (desc
->block
.bits
/ 8)) * desc
->block
.width
;
295 templ
.u
.buf
.first_element
= f
;
296 templ
.u
.buf
.last_element
= f
+ (n
- 1);
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 if (swizzle
!= SWIZZLE_NOOP
) {
308 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
309 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
310 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
311 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
);
414 st_get_texture_sampler_view_from_stobj(st
, stObj
, view_format
,
422 update_textures(struct st_context
*st
,
423 gl_shader_stage mesa_shader
,
424 const struct gl_program
*prog
,
426 struct pipe_sampler_view
**sampler_views
,
427 unsigned *num_textures
)
429 const GLuint old_max
= *num_textures
;
430 GLbitfield samplers_used
= prog
->SamplersUsed
;
432 struct gl_shader_program
*shader
=
433 st
->ctx
->_Shader
->CurrentProgram
[mesa_shader
];
434 unsigned glsl_version
= shader
? shader
->Version
: 0;
435 unsigned shader_stage
= st_shader_stage_to_ptarget(mesa_shader
);
437 if (samplers_used
== 0x0 && old_max
== 0)
442 /* loop over sampler units (aka tex image units) */
443 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
444 struct pipe_sampler_view
*sampler_view
= NULL
;
446 if (samplers_used
& 1) {
447 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
450 retval
= update_single_texture(st
, &sampler_view
, texUnit
,
452 if (retval
== GL_FALSE
)
455 *num_textures
= unit
+ 1;
457 else if (samplers_used
== 0 && unit
>= old_max
) {
458 /* if we've reset all the old views and we have no more new ones */
462 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
465 cso_set_sampler_views(st
->cso_context
,
474 update_vertex_textures(struct st_context
*st
)
476 const struct gl_context
*ctx
= st
->ctx
;
478 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
481 &ctx
->VertexProgram
._Current
->Base
,
482 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
483 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
484 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
490 update_fragment_textures(struct st_context
*st
)
492 const struct gl_context
*ctx
= st
->ctx
;
495 MESA_SHADER_FRAGMENT
,
496 &ctx
->FragmentProgram
._Current
->Base
,
497 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
498 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
499 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
504 update_geometry_textures(struct st_context
*st
)
506 const struct gl_context
*ctx
= st
->ctx
;
508 if (ctx
->GeometryProgram
._Current
) {
510 MESA_SHADER_GEOMETRY
,
511 &ctx
->GeometryProgram
._Current
->Base
,
512 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
513 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
514 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
520 update_tessctrl_textures(struct st_context
*st
)
522 const struct gl_context
*ctx
= st
->ctx
;
524 if (ctx
->TessCtrlProgram
._Current
) {
526 MESA_SHADER_TESS_CTRL
,
527 &ctx
->TessCtrlProgram
._Current
->Base
,
528 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
529 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
530 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
536 update_tesseval_textures(struct st_context
*st
)
538 const struct gl_context
*ctx
= st
->ctx
;
540 if (ctx
->TessEvalProgram
._Current
) {
542 MESA_SHADER_TESS_EVAL
,
543 &ctx
->TessEvalProgram
._Current
->Base
,
544 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
545 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
546 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
552 update_compute_textures(struct st_context
*st
)
554 const struct gl_context
*ctx
= st
->ctx
;
556 if (ctx
->ComputeProgram
._Current
) {
559 &ctx
->ComputeProgram
._Current
->Base
,
560 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
561 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
562 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);
567 const struct st_tracked_state st_update_fragment_texture
= {
568 "st_update_texture", /* name */
570 _NEW_TEXTURE
, /* mesa */
571 ST_NEW_FRAGMENT_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
573 update_fragment_textures
/* update */
577 const struct st_tracked_state st_update_vertex_texture
= {
578 "st_update_vertex_texture", /* name */
580 _NEW_TEXTURE
, /* mesa */
581 ST_NEW_VERTEX_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
583 update_vertex_textures
/* update */
587 const struct st_tracked_state st_update_geometry_texture
= {
588 "st_update_geometry_texture", /* name */
590 _NEW_TEXTURE
, /* mesa */
591 ST_NEW_GEOMETRY_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
593 update_geometry_textures
/* update */
597 const struct st_tracked_state st_update_tessctrl_texture
= {
598 "st_update_tessctrl_texture", /* name */
600 _NEW_TEXTURE
, /* mesa */
601 ST_NEW_TESSCTRL_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
603 update_tessctrl_textures
/* update */
607 const struct st_tracked_state st_update_tesseval_texture
= {
608 "st_update_tesseval_texture", /* name */
610 _NEW_TEXTURE
, /* mesa */
611 ST_NEW_TESSEVAL_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
613 update_tesseval_textures
/* update */
617 const struct st_tracked_state st_update_compute_texture
= {
618 "st_update_compute_texture", /* name */
620 _NEW_TEXTURE
, /* mesa */
621 ST_NEW_COMPUTE_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
623 update_compute_textures
/* update */