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/texobj.h"
39 #include "program/prog_instruction.h"
41 #include "st_context.h"
43 #include "st_texture.h"
44 #include "st_format.h"
45 #include "st_cb_texture.h"
46 #include "pipe/p_context.h"
47 #include "util/u_format.h"
48 #include "util/u_inlines.h"
49 #include "cso_cache/cso_context.h"
53 * Return swizzle1(swizzle2)
56 swizzle_swizzle(unsigned swizzle1
, unsigned swizzle2
)
60 for (i
= 0; i
< 4; i
++) {
61 unsigned s
= GET_SWZ(swizzle1
, i
);
67 swz
[i
] = GET_SWZ(swizzle2
, s
);
70 swz
[i
] = SWIZZLE_ZERO
;
76 assert(!"Bad swizzle term");
81 return MAKE_SWIZZLE4(swz
[0], swz
[1], swz
[2], swz
[3]);
86 * Given a user-specified texture base format, the actual gallium texture
87 * format and the current GL_DEPTH_MODE, return a texture swizzle.
89 * Consider the case where the user requests a GL_RGB internal texture
90 * format the driver actually uses an RGBA format. The A component should
91 * be ignored and sampling from the texture should always return (r,g,b,1).
92 * But if we rendered to the texture we might have written A values != 1.
93 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
94 * This function computes the texture swizzle needed to get the expected
97 * In the case of depth textures, the GL_DEPTH_MODE state determines the
100 * This result must be composed with the user-specified swizzle to get
104 compute_texture_format_swizzle(GLenum baseFormat
, GLenum depthMode
,
105 enum pipe_format actualFormat
)
107 switch (baseFormat
) {
111 if (util_format_has_alpha(actualFormat
))
112 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
116 if (util_format_get_nr_components(actualFormat
) > 2)
117 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
121 if (util_format_get_nr_components(actualFormat
) > 1)
122 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
123 SWIZZLE_ZERO
, SWIZZLE_ONE
);
127 if (util_format_get_nr_components(actualFormat
) > 1)
128 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
129 SWIZZLE_ZERO
, SWIZZLE_W
);
133 if (util_format_get_nr_components(actualFormat
) > 1)
134 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
137 case GL_LUMINANCE_ALPHA
:
138 if (util_format_get_nr_components(actualFormat
) > 2)
139 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_W
);
143 if (util_format_get_nr_components(actualFormat
) > 1)
147 case GL_STENCIL_INDEX
:
149 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 return MAKE_SWIZZLE4(SWIZZLE_ZERO
, SWIZZLE_ZERO
,
160 SWIZZLE_ZERO
, SWIZZLE_X
);
162 return MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
,
163 SWIZZLE_ZERO
, SWIZZLE_ONE
);
165 assert(!"Unexpected depthMode");
169 assert(!"Unexpected baseFormat");
176 get_texture_format_swizzle(const struct st_texture_object
*stObj
)
178 const struct gl_texture_image
*texImage
=
179 stObj
->base
.Image
[0][stObj
->base
.BaseLevel
];
180 unsigned tex_swizzle
;
183 tex_swizzle
= compute_texture_format_swizzle(texImage
->_BaseFormat
,
184 stObj
->base
.DepthMode
,
188 tex_swizzle
= SWIZZLE_XYZW
;
191 /* Combine the texture format swizzle with user's swizzle */
192 return swizzle_swizzle(stObj
->base
._Swizzle
, tex_swizzle
);
197 * Return TRUE if the texture's sampler view swizzle is not equal to
198 * the texture's swizzle.
200 * \param stObj the st texture object,
203 check_sampler_swizzle(const struct st_texture_object
*stObj
,
204 struct pipe_sampler_view
*sv
)
206 unsigned swizzle
= get_texture_format_swizzle(stObj
);
208 return ((sv
->swizzle_r
!= GET_SWZ(swizzle
, 0)) ||
209 (sv
->swizzle_g
!= GET_SWZ(swizzle
, 1)) ||
210 (sv
->swizzle_b
!= GET_SWZ(swizzle
, 2)) ||
211 (sv
->swizzle_a
!= GET_SWZ(swizzle
, 3)));
215 static unsigned last_level(struct st_texture_object
*stObj
)
217 unsigned ret
= MIN2(stObj
->base
.MinLevel
+ stObj
->base
._MaxLevel
,
218 stObj
->pt
->last_level
);
219 if (stObj
->base
.Immutable
)
220 ret
= MIN2(ret
, stObj
->base
.MinLevel
+ stObj
->base
.NumLevels
- 1);
224 static unsigned last_layer(struct st_texture_object
*stObj
)
226 if (stObj
->base
.Immutable
)
227 return MIN2(stObj
->base
.MinLayer
+ stObj
->base
.NumLayers
- 1,
228 stObj
->pt
->array_size
- 1);
229 return stObj
->pt
->array_size
- 1;
232 static struct pipe_sampler_view
*
233 st_create_texture_sampler_view_from_stobj(struct pipe_context
*pipe
,
234 struct st_texture_object
*stObj
,
235 const struct gl_sampler_object
*samp
,
236 enum pipe_format format
)
238 struct pipe_sampler_view templ
;
239 unsigned swizzle
= get_texture_format_swizzle(stObj
);
241 u_sampler_view_default_template(&templ
,
245 if (stObj
->pt
->target
== PIPE_BUFFER
) {
248 const struct util_format_description
*desc
249 = util_format_description(templ
.format
);
251 base
= stObj
->base
.BufferOffset
;
252 if (base
>= stObj
->pt
->width0
)
254 size
= MIN2(stObj
->pt
->width0
- base
, (unsigned)stObj
->base
.BufferSize
);
256 f
= ((base
* 8) / desc
->block
.bits
) * desc
->block
.width
;
257 n
= ((size
* 8) / desc
->block
.bits
) * desc
->block
.width
;
260 templ
.u
.buf
.first_element
= f
;
261 templ
.u
.buf
.last_element
= f
+ (n
- 1);
263 templ
.u
.tex
.first_level
= stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
;
264 templ
.u
.tex
.last_level
= last_level(stObj
);
265 assert(templ
.u
.tex
.first_level
<= templ
.u
.tex
.last_level
);
266 templ
.u
.tex
.first_layer
= stObj
->base
.MinLayer
;
267 templ
.u
.tex
.last_layer
= last_layer(stObj
);
268 assert(templ
.u
.tex
.first_layer
<= templ
.u
.tex
.last_layer
);
269 templ
.target
= gl_target_to_pipe(stObj
->base
.Target
);
272 if (swizzle
!= SWIZZLE_NOOP
) {
273 templ
.swizzle_r
= GET_SWZ(swizzle
, 0);
274 templ
.swizzle_g
= GET_SWZ(swizzle
, 1);
275 templ
.swizzle_b
= GET_SWZ(swizzle
, 2);
276 templ
.swizzle_a
= GET_SWZ(swizzle
, 3);
279 return pipe
->create_sampler_view(pipe
, stObj
->pt
, &templ
);
283 static struct pipe_sampler_view
*
284 st_get_texture_sampler_view_from_stobj(struct st_context
*st
,
285 struct st_texture_object
*stObj
,
286 const struct gl_sampler_object
*samp
,
287 enum pipe_format format
)
289 struct pipe_sampler_view
**sv
;
291 if (!stObj
|| !stObj
->pt
) {
295 sv
= st_texture_get_sampler_view(st
, stObj
);
297 if (stObj
->base
.StencilSampling
&&
298 util_format_is_depth_and_stencil(format
))
299 format
= util_format_stencil_only(format
);
301 /* if sampler view has changed dereference it */
303 if (check_sampler_swizzle(stObj
, *sv
) ||
304 (format
!= (*sv
)->format
) ||
305 gl_target_to_pipe(stObj
->base
.Target
) != (*sv
)->target
||
306 stObj
->base
.MinLevel
+ stObj
->base
.BaseLevel
!= (*sv
)->u
.tex
.first_level
||
307 last_level(stObj
) != (*sv
)->u
.tex
.last_level
||
308 stObj
->base
.MinLayer
!= (*sv
)->u
.tex
.first_layer
||
309 last_layer(stObj
) != (*sv
)->u
.tex
.last_layer
) {
310 pipe_sampler_view_reference(sv
, NULL
);
315 *sv
= st_create_texture_sampler_view_from_stobj(st
->pipe
, stObj
, samp
, format
);
317 } else if ((*sv
)->context
!= st
->pipe
) {
318 /* Recreate view in correct context, use existing view as template */
319 struct pipe_sampler_view
*new_sv
=
320 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
, *sv
);
321 pipe_sampler_view_reference(sv
, NULL
);
329 update_single_texture(struct st_context
*st
,
330 struct pipe_sampler_view
**sampler_view
,
333 struct gl_context
*ctx
= st
->ctx
;
334 const struct gl_sampler_object
*samp
;
335 struct gl_texture_object
*texObj
;
336 struct st_texture_object
*stObj
;
337 enum pipe_format view_format
;
340 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
342 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
345 texObj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
346 samp
= &texObj
->Sampler
;
348 stObj
= st_texture_object(texObj
);
350 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
356 /* Determine the format of the texture sampler view */
357 if (texObj
->Target
== GL_TEXTURE_BUFFER
) {
359 st_mesa_format_to_pipe_format(st
, stObj
->base
._BufferObjectFormat
);
363 stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
365 /* If sRGB decoding is off, use the linear format */
366 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
) {
367 view_format
= util_format_linear(view_format
);
371 *sampler_view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, samp
,
379 update_textures(struct st_context
*st
,
380 unsigned shader_stage
,
381 const struct gl_program
*prog
,
383 struct pipe_sampler_view
**sampler_views
,
384 unsigned *num_textures
)
386 const GLuint old_max
= *num_textures
;
387 GLbitfield samplers_used
= prog
->SamplersUsed
;
390 if (samplers_used
== 0x0 && old_max
== 0)
395 /* loop over sampler units (aka tex image units) */
396 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
397 struct pipe_sampler_view
*sampler_view
= NULL
;
399 if (samplers_used
& 1) {
400 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
403 retval
= update_single_texture(st
, &sampler_view
, texUnit
);
404 if (retval
== GL_FALSE
)
407 *num_textures
= unit
+ 1;
409 else if (samplers_used
== 0 && unit
>= old_max
) {
410 /* if we've reset all the old views and we have no more new ones */
414 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
417 cso_set_sampler_views(st
->cso_context
,
426 update_vertex_textures(struct st_context
*st
)
428 const struct gl_context
*ctx
= st
->ctx
;
430 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
433 &ctx
->VertexProgram
._Current
->Base
,
434 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
435 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
436 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
442 update_fragment_textures(struct st_context
*st
)
444 const struct gl_context
*ctx
= st
->ctx
;
447 PIPE_SHADER_FRAGMENT
,
448 &ctx
->FragmentProgram
._Current
->Base
,
449 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
450 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
451 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
456 update_geometry_textures(struct st_context
*st
)
458 const struct gl_context
*ctx
= st
->ctx
;
460 if (ctx
->GeometryProgram
._Current
) {
462 PIPE_SHADER_GEOMETRY
,
463 &ctx
->GeometryProgram
._Current
->Base
,
464 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
465 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
466 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
471 const struct st_tracked_state st_update_fragment_texture
= {
472 "st_update_texture", /* name */
474 _NEW_TEXTURE
, /* mesa */
475 ST_NEW_FRAGMENT_PROGRAM
, /* st */
477 update_fragment_textures
/* update */
481 const struct st_tracked_state st_update_vertex_texture
= {
482 "st_update_vertex_texture", /* name */
484 _NEW_TEXTURE
, /* mesa */
485 ST_NEW_VERTEX_PROGRAM
, /* st */
487 update_vertex_textures
/* update */
491 const struct st_tracked_state st_update_geometry_texture
= {
492 "st_update_geometry_texture", /* name */
494 _NEW_TEXTURE
, /* mesa */
495 ST_NEW_GEOMETRY_PROGRAM
, /* st */
497 update_geometry_textures
/* update */
503 finalize_textures(struct st_context
*st
)
505 struct gl_context
*ctx
= st
->ctx
;
506 struct gl_fragment_program
*fprog
= ctx
->FragmentProgram
._Current
;
507 const GLboolean prev_missing_textures
= st
->missing_textures
;
510 st
->missing_textures
= GL_FALSE
;
512 for (su
= 0; su
< ctx
->Const
.MaxTextureCoordUnits
; su
++) {
513 if (fprog
->Base
.SamplersUsed
& (1 << su
)) {
514 const GLuint texUnit
= fprog
->Base
.SamplerUnits
[su
];
515 struct gl_texture_object
*texObj
516 = ctx
->Texture
.Unit
[texUnit
]._Current
;
521 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
524 st
->missing_textures
= GL_TRUE
;
531 if (prev_missing_textures
!= st
->missing_textures
)
532 st
->dirty
.st
|= ST_NEW_FRAGMENT_PROGRAM
;
536 const struct st_tracked_state st_finalize_textures
= {
537 "st_finalize_textures", /* name */
539 _NEW_TEXTURE
, /* mesa */
542 finalize_textures
/* update */