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_sampler_view.h"
46 #include "st_texture.h"
47 #include "st_format.h"
48 #include "st_cb_texture.h"
49 #include "pipe/p_context.h"
50 #include "util/u_format.h"
51 #include "util/u_inlines.h"
52 #include "cso_cache/cso_context.h"
56 update_single_texture(struct st_context
*st
,
57 struct pipe_sampler_view
**sampler_view
,
58 GLuint texUnit
, unsigned glsl_version
)
60 struct gl_context
*ctx
= st
->ctx
;
61 const struct gl_sampler_object
*samp
;
62 struct gl_texture_object
*texObj
;
63 struct st_texture_object
*stObj
;
66 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
68 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
71 texObj
= _mesa_get_fallback_texture(ctx
, TEXTURE_2D_INDEX
);
72 samp
= &texObj
->Sampler
;
74 stObj
= st_texture_object(texObj
);
76 retval
= st_finalize_texture(ctx
, st
->pipe
, texObj
);
82 /* Check a few pieces of state outside the texture object to see if we
83 * need to force revalidation.
85 if (stObj
->prev_glsl_version
!= glsl_version
||
86 stObj
->prev_sRGBDecode
!= samp
->sRGBDecode
) {
88 st_texture_release_all_sampler_views(st
, stObj
);
90 stObj
->prev_glsl_version
= glsl_version
;
91 stObj
->prev_sRGBDecode
= samp
->sRGBDecode
;
95 st_get_texture_sampler_view_from_stobj(st
, stObj
, samp
, glsl_version
);
102 update_textures(struct st_context
*st
,
103 gl_shader_stage mesa_shader
,
104 const struct gl_program
*prog
,
106 struct pipe_sampler_view
**sampler_views
,
107 unsigned *num_textures
)
109 const GLuint old_max
= *num_textures
;
110 GLbitfield samplers_used
= prog
->SamplersUsed
;
111 GLbitfield free_slots
= ~prog
->SamplersUsed
;
112 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
114 enum pipe_shader_type shader_stage
= st_shader_stage_to_ptarget(mesa_shader
);
116 if (samplers_used
== 0x0 && old_max
== 0)
121 /* loop over sampler units (aka tex image units) */
122 for (unit
= 0; unit
< max_units
; unit
++, samplers_used
>>= 1) {
123 struct pipe_sampler_view
*sampler_view
= NULL
;
125 if (samplers_used
& 1) {
126 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
129 retval
= update_single_texture(st
, &sampler_view
, texUnit
,
130 prog
->sh
.data
->Version
);
131 if (retval
== GL_FALSE
)
134 *num_textures
= unit
+ 1;
136 else if (samplers_used
== 0 && unit
>= old_max
) {
137 /* if we've reset all the old views and we have no more new ones */
141 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
144 /* For any external samplers with multiplaner YUV, stuff the additional
145 * sampler views we need at the end.
147 * Trying to cache the sampler view in the stObj looks painful, so just
148 * re-create the sampler view for the extra planes each time. Main use
149 * case is video playback (ie. fps games wouldn't be using this) so I
150 * guess no point to try to optimize this feature.
152 while (unlikely(external_samplers_used
)) {
153 GLuint unit
= u_bit_scan(&external_samplers_used
);
155 struct st_texture_object
*stObj
=
156 st_get_texture_object(st
->ctx
, prog
, unit
);
157 struct pipe_sampler_view tmpl
;
162 /* use original view as template: */
163 tmpl
= *sampler_views
[unit
];
165 switch (st_get_view_format(stObj
)) {
166 case PIPE_FORMAT_NV12
:
167 /* we need one additional R8G8 view: */
168 tmpl
.format
= PIPE_FORMAT_RG88_UNORM
;
169 tmpl
.swizzle_g
= PIPE_SWIZZLE_Y
; /* tmpl from Y plane is R8 */
170 extra
= u_bit_scan(&free_slots
);
171 sampler_views
[extra
] =
172 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
174 case PIPE_FORMAT_IYUV
:
175 /* we need two additional R8 views: */
176 tmpl
.format
= PIPE_FORMAT_R8_UNORM
;
177 extra
= u_bit_scan(&free_slots
);
178 sampler_views
[extra
] =
179 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
180 extra
= u_bit_scan(&free_slots
);
181 sampler_views
[extra
] =
182 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
->next
, &tmpl
);
188 *num_textures
= MAX2(*num_textures
, extra
+ 1);
191 cso_set_sampler_views(st
->cso_context
,
200 update_vertex_textures(struct st_context
*st
)
202 const struct gl_context
*ctx
= st
->ctx
;
204 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
207 ctx
->VertexProgram
._Current
,
208 ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
,
209 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
210 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
216 update_fragment_textures(struct st_context
*st
)
218 const struct gl_context
*ctx
= st
->ctx
;
221 MESA_SHADER_FRAGMENT
,
222 ctx
->FragmentProgram
._Current
,
223 ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTextureImageUnits
,
224 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
225 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
230 update_geometry_textures(struct st_context
*st
)
232 const struct gl_context
*ctx
= st
->ctx
;
234 if (ctx
->GeometryProgram
._Current
) {
236 MESA_SHADER_GEOMETRY
,
237 ctx
->GeometryProgram
._Current
,
238 ctx
->Const
.Program
[MESA_SHADER_GEOMETRY
].MaxTextureImageUnits
,
239 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
240 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
246 update_tessctrl_textures(struct st_context
*st
)
248 const struct gl_context
*ctx
= st
->ctx
;
250 if (ctx
->TessCtrlProgram
._Current
) {
252 MESA_SHADER_TESS_CTRL
,
253 ctx
->TessCtrlProgram
._Current
,
254 ctx
->Const
.Program
[MESA_SHADER_TESS_CTRL
].MaxTextureImageUnits
,
255 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
256 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
262 update_tesseval_textures(struct st_context
*st
)
264 const struct gl_context
*ctx
= st
->ctx
;
266 if (ctx
->TessEvalProgram
._Current
) {
268 MESA_SHADER_TESS_EVAL
,
269 ctx
->TessEvalProgram
._Current
,
270 ctx
->Const
.Program
[MESA_SHADER_TESS_EVAL
].MaxTextureImageUnits
,
271 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
272 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
278 update_compute_textures(struct st_context
*st
)
280 const struct gl_context
*ctx
= st
->ctx
;
282 if (ctx
->ComputeProgram
._Current
) {
285 ctx
->ComputeProgram
._Current
,
286 ctx
->Const
.Program
[MESA_SHADER_COMPUTE
].MaxTextureImageUnits
,
287 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
288 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);
293 const struct st_tracked_state st_update_fragment_texture
= {
294 update_fragment_textures
/* update */
298 const struct st_tracked_state st_update_vertex_texture
= {
299 update_vertex_textures
/* update */
303 const struct st_tracked_state st_update_geometry_texture
= {
304 update_geometry_textures
/* update */
308 const struct st_tracked_state st_update_tessctrl_texture
= {
309 update_tessctrl_textures
/* update */
313 const struct st_tracked_state st_update_tesseval_texture
= {
314 update_tesseval_textures
/* update */
318 const struct st_tracked_state st_update_compute_texture
= {
319 update_compute_textures
/* update */