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
:
150 case GL_DEPTH_STENCIL
:
151 case GL_DEPTH_COMPONENT
:
152 /* Now examine the depth mode */
155 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
157 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
);
159 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
160 * the depth mode and return float, while older shadow* functions
161 * and ARB_fp instructions return vec4 according to the depth mode.
163 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
164 * them to return 0, breaking them completely.
166 * A proper fix would increase code complexity and that's not worth
167 * it for a rarely used feature such as the GL_ALPHA depth mode
168 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
169 * shaders that use GLSL 1.30 or later.
171 * BTW, it's required that sampler views are updated when
172 * shaders change (check_sampler_swizzle takes care of that).
174 if (glsl_version
&& glsl_version
>= 130)
177 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
178 SWIZZLE_ZERO
, SWIZZLE_X
);
180 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
181 SWIZZLE_ZERO
, SWIZZLE_ONE
);
183 assert(!"Unexpected depthMode");
187 assert(!"Unexpected baseFormat");
194 get_texture_format_swizzle(const struct st_texture_object
*stObj
,
195 unsigned glsl_version
)
197 GLenum baseFormat
= _mesa_texture_base_format(&stObj
->base
);
198 unsigned tex_swizzle
;
200 if (baseFormat
!= GL_NONE
) {
201 tex_swizzle
= compute_texture_format_swizzle(baseFormat
,
202 stObj
->base
.DepthMode
,
207 tex_swizzle
= SWIZZLE_XYZW
;
210 /* Combine the texture format swizzle with user's swizzle */
211 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
216 * Return TRUE if the texture's sampler view swizzle is not equal to
217 * the texture's swizzle.
219 * \param stObj the st texture object,
222 check_sampler_swizzle(const struct st_texture_object
*stObj
,
223 struct pipe_sampler_view
*sv
, unsigned glsl_version
)
225 unsigned swizzle
= get_texture_format_swizzle(stObj
, glsl_version
);
227 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
228 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
229 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
230 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
234 static unsigned last_level(struct st_texture_object
*stObj
)
236 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
237 stObj
->pt
->last_level
);
238 if (stObj
->base
.Immutable
)
239 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
243 static unsigned last_layer(struct st_texture_object
*stObj
)
245 if (stObj
->base
.Immutable
&& stObj
->pt
->array_size
> 1)
246 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
247 stObj
->pt
->array_size
- 1);
248 return stObj
->pt
->array_size
- 1;
251 static struct pipe_sampler_view
*
252 st_create_texture_sampler_view_from_stobj(struct pipe_context
*pipe
,
253 struct st_texture_object
*stObj
,
254 enum pipe_format format
,
255 unsigned glsl_version
)
257 struct pipe_sampler_view templ
;
258 unsigned swizzle
= get_texture_format_swizzle(stObj
, glsl_version
);
260 u_sampler_view_default_template(&templ
,
264 if (stObj
->pt
->target
== PIPE_BUFFER
) {
267 const struct util_format_description
*desc
268 = util_format_description(templ
.format
);
270 base
= stObj
->base
.BufferOffset
;
271 if (base
>= stObj
->pt
->width0
)
273 size
= MIN2(stObj
->pt
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
275 f
= (base
/ (desc
->block
.bits
/ 8)) * desc
->block
.width
;
276 n
= (size
/ (desc
->block
.bits
/ 8)) * desc
->block
.width
;
279 templ
.u
.buf
.first_element
= f
;
280 templ
.u
.buf
.last_element
= f
+ (n
- 1);
282 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
283 templ
.u
.tex
.last_level
= last_level(stObj
);
284 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
285 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
286 templ
.u
.tex
.last_layer
= last_layer(stObj
);
287 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
288 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
291 if (swizzle
!= SWIZZLE_NOOP
) {
292 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
293 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
294 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
295 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
298 return pipe
->create_sampler_view(pipe
, stObj
->pt
, &templ
);
302 static struct pipe_sampler_view
*
303 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
304 struct st_texture_object
*stObj
,
305 enum pipe_format format
,
306 unsigned glsl_version
)
308 struct pipe_sampler_view
**sv
;
309 const struct st_texture_image
*firstImage
;
310 if (!stObj
|| !stObj
->pt
) {
314 sv
= st_texture_get_sampler_view(st
, stObj
);
316 if (util_format_is_depth_and_stencil(format
)) {
317 if (stObj
->base
.StencilSampling
)
318 format
= util_format_stencil_only(format
);
320 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
321 if (firstImage
->base
._BaseFormat
== GL_STENCIL_INDEX
)
322 format
= util_format_stencil_only(format
);
326 /* if sampler view has changed dereference it */
328 if (check_sampler_swizzle(stObj
, *sv
, glsl_version
) ||
329 (format
!= (*sv
)->format
) ||
330 gl_target_to_pipe(stObj
->base
.Target
) != (*sv
)->target
||
331 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
!= (*sv
)->u
.tex
.first_level
||
332 last_level(stObj
) != (*sv
)->u
.tex
.last_level
||
333 stObj
->base
.MinLayer
!= (*sv
)->u
.tex
.first_layer
||
334 last_layer(stObj
) != (*sv
)->u
.tex
.last_layer
) {
335 pipe_sampler_view_reference(sv
, NULL
);
340 *sv
= st_create_texture_sampler_view_from_stobj(st
->pipe
, stObj
,
341 format
, glsl_version
);
343 } else if ((*sv
)->context
!= st
->pipe
) {
344 /* Recreate view in correct context, use existing view as template */
345 struct pipe_sampler_view
*new_sv
=
346 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, *sv
);
347 pipe_sampler_view_reference(sv
, NULL
);
355 update_single_texture(struct st_context
*st
,
356 struct pipe_sampler_view
**sampler_view
,
357 GLuint texUnit
, unsigned glsl_version
)
359 struct gl_context
*ctx
= st
->ctx
;
360 const struct gl_sampler_object
*samp
;
361 struct gl_texture_object
*texObj
;
362 struct st_texture_object
*stObj
;
363 enum pipe_format view_format
;
366 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
368 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
371 texObj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
372 samp
= &texObj
->Sampler
;
374 stObj
= st_texture_object(texObj
);
376 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
382 /* Determine the format of the texture sampler view */
383 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
385 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
389 stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
391 /* If sRGB decoding is off, use the linear format */
392 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
) {
393 view_format
= util_format_linear(view_format
);
398 st_get_texture_sampler_view_from_stobj(st
, stObj
, view_format
,
406 update_textures(struct st_context
*st
,
407 gl_shader_stage mesa_shader
,
408 const struct gl_program
*prog
,
410 struct pipe_sampler_view
**sampler_views
,
411 unsigned *num_textures
)
413 const GLuint old_max
= *num_textures
;
414 GLbitfield samplers_used
= prog
->SamplersUsed
;
416 struct gl_shader_program
*shader
=
417 st
->ctx
->_Shader
->CurrentProgram
[mesa_shader
];
418 unsigned glsl_version
= shader
? shader
->Version
: 0;
419 unsigned shader_stage
= st_shader_stage_to_ptarget(mesa_shader
);
421 if (samplers_used
== 0x0 && old_max
== 0)
426 /* loop over sampler units (aka tex image units) */
427 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
428 struct pipe_sampler_view
*sampler_view
= NULL
;
430 if (samplers_used
& 1) {
431 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
434 retval
= update_single_texture(st
, &sampler_view
, texUnit
,
436 if (retval
== GL_FALSE
)
439 *num_textures
= unit
+ 1;
441 else if (samplers_used
== 0 && unit
>= old_max
) {
442 /* if we've reset all the old views and we have no more new ones */
446 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
449 cso_set_sampler_views(st
->cso_context
,
458 update_vertex_textures(struct st_context
*st
)
460 const struct gl_context
*ctx
= st
->ctx
;
462 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
465 &ctx
->VertexProgram
._Current
->Base
,
466 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
467 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
468 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
474 update_fragment_textures(struct st_context
*st
)
476 const struct gl_context
*ctx
= st
->ctx
;
479 MESA_SHADER_FRAGMENT
,
480 &ctx
->FragmentProgram
._Current
->Base
,
481 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
482 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
483 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
488 update_geometry_textures(struct st_context
*st
)
490 const struct gl_context
*ctx
= st
->ctx
;
492 if (ctx
->GeometryProgram
._Current
) {
494 MESA_SHADER_GEOMETRY
,
495 &ctx
->GeometryProgram
._Current
->Base
,
496 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
497 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
498 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
504 update_tessctrl_textures(struct st_context
*st
)
506 const struct gl_context
*ctx
= st
->ctx
;
508 if (ctx
->TessCtrlProgram
._Current
) {
510 MESA_SHADER_TESS_CTRL
,
511 &ctx
->TessCtrlProgram
._Current
->Base
,
512 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
513 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
514 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
520 update_tesseval_textures(struct st_context
*st
)
522 const struct gl_context
*ctx
= st
->ctx
;
524 if (ctx
->TessEvalProgram
._Current
) {
526 MESA_SHADER_TESS_EVAL
,
527 &ctx
->TessEvalProgram
._Current
->Base
,
528 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
529 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
530 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
536 update_compute_textures(struct st_context
*st
)
538 const struct gl_context
*ctx
= st
->ctx
;
540 if (ctx
->ComputeProgram
._Current
) {
543 &ctx
->ComputeProgram
._Current
->Base
,
544 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
545 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
546 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);
551 const struct st_tracked_state st_update_fragment_texture
= {
552 "st_update_texture", /* name */
554 _NEW_TEXTURE
, /* mesa */
555 ST_NEW_FRAGMENT_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
557 update_fragment_textures
/* update */
561 const struct st_tracked_state st_update_vertex_texture
= {
562 "st_update_vertex_texture", /* name */
564 _NEW_TEXTURE
, /* mesa */
565 ST_NEW_VERTEX_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
567 update_vertex_textures
/* update */
571 const struct st_tracked_state st_update_geometry_texture
= {
572 "st_update_geometry_texture", /* name */
574 _NEW_TEXTURE
, /* mesa */
575 ST_NEW_GEOMETRY_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
577 update_geometry_textures
/* update */
581 const struct st_tracked_state st_update_tessctrl_texture
= {
582 "st_update_tessctrl_texture", /* name */
584 _NEW_TEXTURE
, /* mesa */
585 ST_NEW_TESSCTRL_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
587 update_tessctrl_textures
/* update */
591 const struct st_tracked_state st_update_tesseval_texture
= {
592 "st_update_tesseval_texture", /* name */
594 _NEW_TEXTURE
, /* mesa */
595 ST_NEW_TESSEVAL_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
597 update_tesseval_textures
/* update */
601 const struct st_tracked_state st_update_compute_texture
= {
602 "st_update_compute_texture", /* name */
604 _NEW_TEXTURE
, /* mesa */
605 ST_NEW_COMPUTE_PROGRAM
| ST_NEW_SAMPLER_VIEWS
, /* st */
607 update_compute_textures
/* update */