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/macros.h"
36 #include "main/mtypes.h"
37 #include "main/samplerobj.h"
38 #include "main/teximage.h"
39 #include "main/texobj.h"
40 #include "program/prog_instruction.h"
42 #include "st_context.h"
44 #include "st_texture.h"
45 #include "st_format.h"
46 #include "st_cb_texture.h"
47 #include "pipe/p_context.h"
48 #include "util/u_format.h"
49 #include "util/u_inlines.h"
50 #include "cso_cache/cso_context.h"
54 * Return swizzle1(swizzle2)
57 swizzle_swizzle(unsigned swizzle1
, unsigned swizzle2
)
61 for (i
= 0; i
< 4; i
++) {
62 unsigned s
= GET_SWZ(swizzle1
, i
);
68 swz
[i
] = GET_SWZ(swizzle2
, s
);
71 swz
[i
] = SWIZZLE_ZERO
;
77 assert(!"Bad swizzle term");
82 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
87 * Given a user-specified texture base format, the actual gallium texture
88 * format and the current GL_DEPTH_MODE, return a texture swizzle.
90 * Consider the case where the user requests a GL_RGB internal texture
91 * format the driver actually uses an RGBA format. The A component should
92 * be ignored and sampling from the texture should always return (r,g,b,1).
93 * But if we rendered to the texture we might have written A values != 1.
94 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
95 * This function computes the texture swizzle needed to get the expected
98 * In the case of depth textures, the GL_DEPTH_MODE state determines the
101 * This result must be composed with the user-specified swizzle to get
105 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
106 enum pipe_format actualFormat
,
107 unsigned glsl_version
)
109 switch (baseFormat
) {
113 if (util_format_has_alpha(actualFormat
))
114 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
118 if (util_format_get_nr_components(actualFormat
) > 2)
119 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
123 if (util_format_get_nr_components(actualFormat
) > 1)
124 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
125 SWIZZLE_ZERO
, SWIZZLE_ONE
);
129 if (util_format_get_nr_components(actualFormat
) > 1)
130 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
131 SWIZZLE_ZERO
, SWIZZLE_W
);
135 if (util_format_get_nr_components(actualFormat
) > 1)
136 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
139 case GL_LUMINANCE_ALPHA
:
140 if (util_format_get_nr_components(actualFormat
) > 2)
141 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
145 if (util_format_get_nr_components(actualFormat
) > 1)
149 case GL_STENCIL_INDEX
:
151 case GL_DEPTH_STENCIL
:
153 case GL_DEPTH_COMPONENT
:
154 /* Now examine the depth mode */
157 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
159 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
161 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
162 * the depth mode and return float, while older shadow* functions
163 * and ARB_fp instructions return vec4 according to the depth mode.
165 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
166 * them to return 0, breaking them completely.
168 * A proper fix would increase code complexity and that's not worth
169 * it for a rarely used feature such as the GL_ALPHA depth mode
170 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
171 * shaders that use GLSL 1.30 or later.
173 * BTW, it's required that sampler views are updated when
174 * shaders change (check_sampler_swizzle takes care of that).
176 if (glsl_version
&& glsl_version
>= 130)
179 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
180 SWIZZLE_ZERO
, SWIZZLE_X
);
182 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
183 SWIZZLE_ZERO
, SWIZZLE_ONE
);
185 assert(!"Unexpected depthMode");
189 assert(!"Unexpected baseFormat");
196 get_texture_format_swizzle(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 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
204 stObj
->base
.DepthMode
,
209 tex_swizzle
= SWIZZLE_XYZW
;
212 /* Combine the texture format swizzle with user's swizzle */
213 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
218 * Return TRUE if the texture's sampler view swizzle is not equal to
219 * the texture's swizzle.
221 * \param stObj the st texture object,
224 check_sampler_swizzle(const struct st_texture_object
*stObj
,
225 struct pipe_sampler_view
*sv
, unsigned glsl_version
)
227 unsigned swizzle
= get_texture_format_swizzle(stObj
, glsl_version
);
229 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
230 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
231 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
232 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
236 static unsigned last_level(struct st_texture_object
*stObj
)
238 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
239 stObj
->pt
->last_level
);
240 if (stObj
->base
.Immutable
)
241 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
245 static unsigned last_layer(struct st_texture_object
*stObj
)
247 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
248 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
249 stObj
->pt
->array_size
- 1);
250 return stObj
->pt
->array_size
- 1;
253 static struct pipe_sampler_view
*
254 st_create_texture_sampler_view_from_stobj(struct pipe_context
*pipe
,
255 struct st_texture_object
*stObj
,
256 enum pipe_format format
,
257 unsigned glsl_version
)
259 struct pipe_sampler_view templ
;
260 unsigned swizzle
= get_texture_format_swizzle(stObj
, glsl_version
);
262 u_sampler_view_default_template(&templ
,
266 if (stObj
->pt
->target
== PIPE_BUFFER
) {
269 const struct util_format_description
*desc
270 = util_format_description(templ
.format
);
272 base
= stObj
->base
.BufferOffset
;
273 if (base
>= stObj
->pt
->width0
)
275 size
= MIN2(stObj
->pt
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
277 f
= (base
/ (desc
->block
.bits
/ 8)) * desc
->block
.width
;
278 n
= (size
/ (desc
->block
.bits
/ 8)) * desc
->block
.width
;
281 templ
.u
.buf
.first_element
= f
;
282 templ
.u
.buf
.last_element
= f
+ (n
- 1);
284 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
285 templ
.u
.tex
.last_level
= last_level(stObj
);
286 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
287 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
288 templ
.u
.tex
.last_layer
= last_layer(stObj
);
289 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
290 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
293 if (swizzle
!= SWIZZLE_NOOP
) {
294 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
295 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
296 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
297 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
300 return pipe
->create_sampler_view(pipe
, stObj
->pt
, &templ
);
304 static struct pipe_sampler_view
*
305 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
306 struct st_texture_object
*stObj
,
307 enum pipe_format format
,
308 unsigned glsl_version
)
310 struct pipe_sampler_view
**sv
;
311 const struct st_texture_image
*firstImage
;
312 if (!stObj
|| !stObj
->pt
) {
316 sv
= st_texture_get_sampler_view(st
, stObj
);
318 if (util_format_is_depth_and_stencil(format
)) {
319 if (stObj
->base
.StencilSampling
)
320 format
= util_format_stencil_only(format
);
322 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
323 if (firstImage
->base
._BaseFormat
== GL_STENCIL_INDEX
)
324 format
= util_format_stencil_only(format
);
328 /* if sampler view has changed dereference it */
330 if (check_sampler_swizzle(stObj
, *sv
, glsl_version
) ||
331 (format
!= (*sv
)->format
) ||
332 gl_target_to_pipe(stObj
->base
.Target
) != (*sv
)->target
||
333 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
!= (*sv
)->u
.tex
.first_level
||
334 last_level(stObj
) != (*sv
)->u
.tex
.last_level
||
335 stObj
->base
.MinLayer
!= (*sv
)->u
.tex
.first_layer
||
336 last_layer(stObj
) != (*sv
)->u
.tex
.last_layer
) {
337 pipe_sampler_view_reference(sv
, NULL
);
342 *sv
= st_create_texture_sampler_view_from_stobj(st
->pipe
, stObj
,
343 format
, glsl_version
);
345 } else if ((*sv
)->context
!= st
->pipe
) {
346 /* Recreate view in correct context, use existing view as template */
347 struct pipe_sampler_view
*new_sv
=
348 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, *sv
);
349 pipe_sampler_view_reference(sv
, NULL
);
357 update_single_texture(struct st_context
*st
,
358 struct pipe_sampler_view
**sampler_view
,
359 GLuint texUnit
, unsigned glsl_version
)
361 struct gl_context
*ctx
= st
->ctx
;
362 const struct gl_sampler_object
*samp
;
363 struct gl_texture_object
*texObj
;
364 struct st_texture_object
*stObj
;
365 enum pipe_format view_format
;
368 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
370 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
373 texObj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
374 samp
= &texObj
->Sampler
;
376 stObj
= st_texture_object(texObj
);
378 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
384 /* Determine the format of the texture sampler view */
385 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
387 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
391 stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
393 /* If sRGB decoding is off, use the linear format */
394 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
) {
395 view_format
= util_format_linear(view_format
);
400 st_get_texture_sampler_view_from_stobj(st
, stObj
, view_format
,
408 update_textures(struct st_context
*st
,
409 gl_shader_stage mesa_shader
,
410 const struct gl_program
*prog
,
412 struct pipe_sampler_view
**sampler_views
,
413 unsigned *num_textures
)
415 const GLuint old_max
= *num_textures
;
416 GLbitfield samplers_used
= prog
->SamplersUsed
;
418 struct gl_shader_program
*shader
=
419 st
->ctx
->_Shader
->CurrentProgram
[mesa_shader
];
420 unsigned glsl_version
= shader
? shader
->Version
: 0;
421 unsigned shader_stage
= st_shader_stage_to_ptarget(mesa_shader
);
423 if (samplers_used
== 0x0 && old_max
== 0)
428 /* loop over sampler units (aka tex image units) */
429 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
430 struct pipe_sampler_view
*sampler_view
= NULL
;
432 if (samplers_used
& 1) {
433 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
436 retval
= update_single_texture(st
, &sampler_view
, texUnit
,
438 if (retval
== GL_FALSE
)
441 *num_textures
= unit
+ 1;
443 else if (samplers_used
== 0 && unit
>= old_max
) {
444 /* if we've reset all the old views and we have no more new ones */
448 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
451 cso_set_sampler_views(st
->cso_context
,
460 update_vertex_textures(struct st_context
*st
)
462 const struct gl_context
*ctx
= st
->ctx
;
464 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
467 &ctx
->VertexProgram
._Current
->Base
,
468 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
469 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
470 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
476 update_fragment_textures(struct st_context
*st
)
478 const struct gl_context
*ctx
= st
->ctx
;
481 MESA_SHADER_FRAGMENT
,
482 &ctx
->FragmentProgram
._Current
->Base
,
483 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
484 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
485 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
490 update_geometry_textures(struct st_context
*st
)
492 const struct gl_context
*ctx
= st
->ctx
;
494 if (ctx
->GeometryProgram
._Current
) {
496 MESA_SHADER_GEOMETRY
,
497 &ctx
->GeometryProgram
._Current
->Base
,
498 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
499 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
500 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
506 update_tessctrl_textures(struct st_context
*st
)
508 const struct gl_context
*ctx
= st
->ctx
;
510 if (ctx
->TessCtrlProgram
._Current
) {
512 MESA_SHADER_TESS_CTRL
,
513 &ctx
->TessCtrlProgram
._Current
->Base
,
514 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
515 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
516 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
522 update_tesseval_textures(struct st_context
*st
)
524 const struct gl_context
*ctx
= st
->ctx
;
526 if (ctx
->TessEvalProgram
._Current
) {
528 MESA_SHADER_TESS_EVAL
,
529 &ctx
->TessEvalProgram
._Current
->Base
,
530 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
531 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
532 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
537 const struct st_tracked_state st_update_fragment_texture
= {
538 "st_update_texture", /* name */
540 _NEW_TEXTURE
, /* mesa */
541 ST_NEW_FRAGMENT_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
543 update_fragment_textures
/* update */
547 const struct st_tracked_state st_update_vertex_texture
= {
548 "st_update_vertex_texture", /* name */
550 _NEW_TEXTURE
, /* mesa */
551 ST_NEW_VERTEX_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
553 update_vertex_textures
/* update */
557 const struct st_tracked_state st_update_geometry_texture
= {
558 "st_update_geometry_texture", /* name */
560 _NEW_TEXTURE
, /* mesa */
561 ST_NEW_GEOMETRY_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
563 update_geometry_textures
/* update */
567 const struct st_tracked_state st_update_tessctrl_texture
= {
568 "st_update_tessctrl_texture", /* name */
570 _NEW_TEXTURE
, /* mesa */
571 ST_NEW_TESSCTRL_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
573 update_tessctrl_textures
/* update */
577 const struct st_tracked_state st_update_tesseval_texture
= {
578 "st_update_tesseval_texture", /* name */
580 _NEW_TEXTURE
, /* mesa */
581 ST_NEW_TESSEVAL_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
583 update_tesseval_textures
/* update */