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 * Get a pipe_sampler_view object from a texture unit.
59 st_update_single_texture(struct st_context
*st
,
60 struct pipe_sampler_view
**sampler_view
,
61 GLuint texUnit
, bool glsl130_or_later
)
63 struct gl_context
*ctx
= st
->ctx
;
64 const struct gl_sampler_object
*samp
;
65 struct gl_texture_object
*texObj
;
66 struct st_texture_object
*stObj
;
68 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
70 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
73 stObj
= st_texture_object(texObj
);
75 if (unlikely(texObj
->Target
== GL_TEXTURE_BUFFER
)) {
76 *sampler_view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
80 if (!st_finalize_texture(ctx
, st
->pipe
, texObj
, 0) ||
87 /* Check a few pieces of state outside the texture object to see if we
88 * need to force revalidation.
90 if (stObj
->prev_glsl130_or_later
!= glsl130_or_later
||
91 stObj
->prev_sRGBDecode
!= samp
->sRGBDecode
) {
93 st_texture_release_all_sampler_views(st
, stObj
);
95 stObj
->prev_glsl130_or_later
= glsl130_or_later
;
96 stObj
->prev_sRGBDecode
= samp
->sRGBDecode
;
99 if (texObj
->TargetIndex
== TEXTURE_EXTERNAL_INDEX
&&
100 stObj
->pt
->screen
->resource_changed
)
101 stObj
->pt
->screen
->resource_changed(stObj
->pt
->screen
, stObj
->pt
);
104 st_get_texture_sampler_view_from_stobj(st
, stObj
, samp
,
111 update_textures(struct st_context
*st
,
112 enum pipe_shader_type shader_stage
,
113 const struct gl_program
*prog
,
114 struct pipe_sampler_view
**sampler_views
,
115 unsigned *out_num_textures
)
117 const GLuint old_max
= *out_num_textures
;
118 GLbitfield samplers_used
= prog
->SamplersUsed
;
119 GLbitfield free_slots
= ~prog
->SamplersUsed
;
120 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
123 if (samplers_used
== 0x0 && old_max
== 0)
126 unsigned num_textures
= 0;
128 /* prog->sh.data is NULL if it's ARB_fragment_program */
129 bool glsl130
= (prog
->sh
.data
? prog
->sh
.data
->Version
: 0) >= 130;
131 /* loop over sampler units (aka tex image units) */
132 for (unit
= 0; samplers_used
|| unit
< old_max
;
133 unit
++, samplers_used
>>= 1) {
134 struct pipe_sampler_view
*sampler_view
= NULL
;
136 if (samplers_used
& 1) {
137 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
139 st_update_single_texture(st
, &sampler_view
, texUnit
, glsl130
);
140 num_textures
= unit
+ 1;
143 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
146 /* For any external samplers with multiplaner YUV, stuff the additional
147 * sampler views we need at the end.
149 * Trying to cache the sampler view in the stObj looks painful, so just
150 * re-create the sampler view for the extra planes each time. Main use
151 * case is video playback (ie. fps games wouldn't be using this) so I
152 * guess no point to try to optimize this feature.
154 while (unlikely(external_samplers_used
)) {
155 GLuint unit
= u_bit_scan(&external_samplers_used
);
157 struct st_texture_object
*stObj
=
158 st_get_texture_object(st
->ctx
, prog
, unit
);
159 struct pipe_sampler_view tmpl
;
164 /* use original view as template: */
165 tmpl
= *sampler_views
[unit
];
167 switch (st_get_view_format(stObj
)) {
168 case PIPE_FORMAT_NV12
:
169 /* we need one additional R8G8 view: */
170 tmpl
.format
= PIPE_FORMAT_RG88_UNORM
;
171 tmpl
.swizzle_g
= PIPE_SWIZZLE_Y
; /* tmpl from Y plane is R8 */
172 extra
= u_bit_scan(&free_slots
);
173 sampler_views
[extra
] =
174 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
176 case PIPE_FORMAT_IYUV
:
177 /* we need two additional R8 views: */
178 tmpl
.format
= PIPE_FORMAT_R8_UNORM
;
179 extra
= u_bit_scan(&free_slots
);
180 sampler_views
[extra
] =
181 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
182 extra
= u_bit_scan(&free_slots
);
183 sampler_views
[extra
] =
184 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
->next
, &tmpl
);
190 num_textures
= MAX2(num_textures
, extra
+ 1);
193 cso_set_sampler_views(st
->cso_context
,
197 *out_num_textures
= num_textures
;
203 st_update_vertex_textures(struct st_context
*st
)
205 const struct gl_context
*ctx
= st
->ctx
;
207 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
210 ctx
->VertexProgram
._Current
,
211 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
212 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
218 st_update_fragment_textures(struct st_context
*st
)
220 const struct gl_context
*ctx
= st
->ctx
;
223 PIPE_SHADER_FRAGMENT
,
224 ctx
->FragmentProgram
._Current
,
225 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
226 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
231 st_update_geometry_textures(struct st_context
*st
)
233 const struct gl_context
*ctx
= st
->ctx
;
235 if (ctx
->GeometryProgram
._Current
) {
237 PIPE_SHADER_GEOMETRY
,
238 ctx
->GeometryProgram
._Current
,
239 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
240 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
246 st_update_tessctrl_textures(struct st_context
*st
)
248 const struct gl_context
*ctx
= st
->ctx
;
250 if (ctx
->TessCtrlProgram
._Current
) {
252 PIPE_SHADER_TESS_CTRL
,
253 ctx
->TessCtrlProgram
._Current
,
254 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
255 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
261 st_update_tesseval_textures(struct st_context
*st
)
263 const struct gl_context
*ctx
= st
->ctx
;
265 if (ctx
->TessEvalProgram
._Current
) {
267 PIPE_SHADER_TESS_EVAL
,
268 ctx
->TessEvalProgram
._Current
,
269 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
270 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
276 st_update_compute_textures(struct st_context
*st
)
278 const struct gl_context
*ctx
= st
->ctx
;
280 if (ctx
->ComputeProgram
._Current
) {
283 ctx
->ComputeProgram
._Current
,
284 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
285 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);