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
,
62 bool ignore_srgb_decode
)
64 struct gl_context
*ctx
= st
->ctx
;
65 const struct gl_sampler_object
*samp
;
66 struct gl_texture_object
*texObj
;
67 struct st_texture_object
*stObj
;
69 samp
= _mesa_get_samplerobj(ctx
, texUnit
);
71 texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
74 stObj
= st_texture_object(texObj
);
76 if (unlikely(texObj
->Target
== GL_TEXTURE_BUFFER
)) {
77 *sampler_view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
81 if (!st_finalize_texture(ctx
, st
->pipe
, texObj
, 0) ||
88 if (texObj
->TargetIndex
== TEXTURE_EXTERNAL_INDEX
&&
89 stObj
->pt
->screen
->resource_changed
)
90 stObj
->pt
->screen
->resource_changed(stObj
->pt
->screen
, stObj
->pt
);
93 st_get_texture_sampler_view_from_stobj(st
, stObj
, samp
,
101 update_textures(struct st_context
*st
,
102 enum pipe_shader_type shader_stage
,
103 const struct gl_program
*prog
,
104 struct pipe_sampler_view
**sampler_views
,
105 unsigned *out_num_textures
)
107 const GLuint old_max
= *out_num_textures
;
108 GLbitfield samplers_used
= prog
->SamplersUsed
;
109 GLbitfield texel_fetch_samplers
= prog
->info
.textures_used_by_txf
;
110 GLbitfield free_slots
= ~prog
->SamplersUsed
;
111 GLbitfield external_samplers_used
= prog
->ExternalSamplersUsed
;
114 if (samplers_used
== 0x0 && old_max
== 0)
117 unsigned num_textures
= 0;
119 /* prog->sh.data is NULL if it's ARB_fragment_program */
120 bool glsl130
= (prog
->sh
.data
? prog
->sh
.data
->Version
: 0) >= 130;
122 /* loop over sampler units (aka tex image units) */
123 for (unit
= 0; samplers_used
|| unit
< old_max
;
124 unit
++, samplers_used
>>= 1, texel_fetch_samplers
>>= 1) {
125 struct pipe_sampler_view
*sampler_view
= NULL
;
127 if (samplers_used
& 1) {
128 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
130 /* The EXT_texture_sRGB_decode extension says:
132 * "The conversion of sRGB color space components to linear color
133 * space is always performed if the texel lookup function is one
134 * of the texelFetch builtin functions.
136 * Otherwise, if the texel lookup function is one of the texture
137 * builtin functions or one of the texture gather functions, the
138 * conversion of sRGB color space components to linear color space
139 * is controlled by the TEXTURE_SRGB_DECODE_EXT parameter.
141 * If the TEXTURE_SRGB_DECODE_EXT parameter is DECODE_EXT, the
142 * conversion of sRGB color space components to linear color space
145 * If the TEXTURE_SRGB_DECODE_EXT parameter is SKIP_DECODE_EXT,
146 * the value is returned without decoding. However, if the texture
147 * is also [statically] accessed with a texelFetch function, then
148 * the result of texture builtin functions and/or texture gather
149 * functions may be returned with decoding or without decoding."
151 * Note: the "statically" will be added to the language per
152 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=14934
154 * So we simply ignore the setting entirely for samplers that are
155 * (statically) accessed with a texelFetch function.
157 st_update_single_texture(st
, &sampler_view
, texUnit
, glsl130
,
158 texel_fetch_samplers
& 1);
159 num_textures
= unit
+ 1;
162 pipe_sampler_view_reference(&(sampler_views
[unit
]), sampler_view
);
165 /* For any external samplers with multiplaner YUV, stuff the additional
166 * sampler views we need at the end.
168 * Trying to cache the sampler view in the stObj looks painful, so just
169 * re-create the sampler view for the extra planes each time. Main use
170 * case is video playback (ie. fps games wouldn't be using this) so I
171 * guess no point to try to optimize this feature.
173 while (unlikely(external_samplers_used
)) {
174 GLuint unit
= u_bit_scan(&external_samplers_used
);
176 struct st_texture_object
*stObj
=
177 st_get_texture_object(st
->ctx
, prog
, unit
);
178 struct pipe_sampler_view tmpl
;
183 /* use original view as template: */
184 tmpl
= *sampler_views
[unit
];
186 switch (st_get_view_format(stObj
)) {
187 case PIPE_FORMAT_NV12
:
188 /* we need one additional R8G8 view: */
189 tmpl
.format
= PIPE_FORMAT_RG88_UNORM
;
190 tmpl
.swizzle_g
= PIPE_SWIZZLE_Y
; /* tmpl from Y plane is R8 */
191 extra
= u_bit_scan(&free_slots
);
192 sampler_views
[extra
] =
193 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
195 case PIPE_FORMAT_IYUV
:
196 /* we need two additional R8 views: */
197 tmpl
.format
= PIPE_FORMAT_R8_UNORM
;
198 extra
= u_bit_scan(&free_slots
);
199 sampler_views
[extra
] =
200 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
, &tmpl
);
201 extra
= u_bit_scan(&free_slots
);
202 sampler_views
[extra
] =
203 st
->pipe
->create_sampler_view(st
->pipe
, stObj
->pt
->next
->next
, &tmpl
);
209 num_textures
= MAX2(num_textures
, extra
+ 1);
212 cso_set_sampler_views(st
->cso_context
,
216 *out_num_textures
= num_textures
;
222 st_update_vertex_textures(struct st_context
*st
)
224 const struct gl_context
*ctx
= st
->ctx
;
226 if (ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTextureImageUnits
> 0) {
229 ctx
->VertexProgram
._Current
,
230 st
->state
.sampler_views
[PIPE_SHADER_VERTEX
],
231 &st
->state
.num_sampler_views
[PIPE_SHADER_VERTEX
]);
237 st_update_fragment_textures(struct st_context
*st
)
239 const struct gl_context
*ctx
= st
->ctx
;
242 PIPE_SHADER_FRAGMENT
,
243 ctx
->FragmentProgram
._Current
,
244 st
->state
.sampler_views
[PIPE_SHADER_FRAGMENT
],
245 &st
->state
.num_sampler_views
[PIPE_SHADER_FRAGMENT
]);
250 st_update_geometry_textures(struct st_context
*st
)
252 const struct gl_context
*ctx
= st
->ctx
;
254 if (ctx
->GeometryProgram
._Current
) {
256 PIPE_SHADER_GEOMETRY
,
257 ctx
->GeometryProgram
._Current
,
258 st
->state
.sampler_views
[PIPE_SHADER_GEOMETRY
],
259 &st
->state
.num_sampler_views
[PIPE_SHADER_GEOMETRY
]);
265 st_update_tessctrl_textures(struct st_context
*st
)
267 const struct gl_context
*ctx
= st
->ctx
;
269 if (ctx
->TessCtrlProgram
._Current
) {
271 PIPE_SHADER_TESS_CTRL
,
272 ctx
->TessCtrlProgram
._Current
,
273 st
->state
.sampler_views
[PIPE_SHADER_TESS_CTRL
],
274 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_CTRL
]);
280 st_update_tesseval_textures(struct st_context
*st
)
282 const struct gl_context
*ctx
= st
->ctx
;
284 if (ctx
->TessEvalProgram
._Current
) {
286 PIPE_SHADER_TESS_EVAL
,
287 ctx
->TessEvalProgram
._Current
,
288 st
->state
.sampler_views
[PIPE_SHADER_TESS_EVAL
],
289 &st
->state
.num_sampler_views
[PIPE_SHADER_TESS_EVAL
]);
295 st_update_compute_textures(struct st_context
*st
)
297 const struct gl_context
*ctx
= st
->ctx
;
299 if (ctx
->ComputeProgram
._Current
) {
302 ctx
->ComputeProgram
._Current
,
303 st
->state
.sampler_views
[PIPE_SHADER_COMPUTE
],
304 &st
->state
.num_sampler_views
[PIPE_SHADER_COMPUTE
]);