1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Keith Whitwell <keithw@vmware.com>
31   *   Brian Paul
32   */
33 
34 
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"
42 
43 #include "st_context.h"
44 #include "st_atom.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/format/u_format.h"
51 #include "util/u_inlines.h"
52 #include "cso_cache/cso_context.h"
53 
54 
55 /**
56  * Get a pipe_sampler_view object from a texture unit.
57  */
58 void
st_update_single_texture(struct st_context * st,struct pipe_sampler_view ** sampler_view,GLuint texUnit,bool glsl130_or_later,bool ignore_srgb_decode)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)
63 {
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;
68 
69    samp = _mesa_get_samplerobj(ctx, texUnit);
70 
71    texObj = ctx->Texture.Unit[texUnit]._Current;
72    assert(texObj);
73 
74    stObj = st_texture_object(texObj);
75 
76    if (unlikely(texObj->Target == GL_TEXTURE_BUFFER)) {
77       *sampler_view = st_get_buffer_sampler_view_from_stobj(st, stObj);
78       return;
79    }
80 
81    if (!st_finalize_texture(ctx, st->pipe, texObj, 0) ||
82        !stObj->pt) {
83       /* out of mem */
84       *sampler_view = NULL;
85       return;
86    }
87 
88    if (texObj->TargetIndex == TEXTURE_EXTERNAL_INDEX &&
89        stObj->pt->screen->resource_changed)
90          stObj->pt->screen->resource_changed(stObj->pt->screen, stObj->pt);
91 
92    *sampler_view =
93       st_get_texture_sampler_view_from_stobj(st, stObj, samp,
94                                              glsl130_or_later,
95                                              ignore_srgb_decode);
96 }
97 
98 
99 
100 static void
update_textures(struct st_context * st,enum pipe_shader_type shader_stage,const struct gl_program * prog,struct pipe_sampler_view ** sampler_views)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 {
106    const GLuint old_max = st->state.num_sampler_views[shader_stage];
107    GLbitfield samplers_used = prog->SamplersUsed;
108    GLbitfield texel_fetch_samplers = prog->info.textures_used_by_txf;
109    GLbitfield free_slots = ~prog->SamplersUsed;
110    GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
111    GLuint unit;
112 
113    if (samplers_used == 0x0 && old_max == 0)
114       return;
115 
116    unsigned num_textures = 0;
117 
118    /* prog->sh.data is NULL if it's ARB_fragment_program */
119    bool glsl130 = (prog->sh.data ? prog->sh.data->Version : 0) >= 130;
120 
121    /* loop over sampler units (aka tex image units) */
122    for (unit = 0; samplers_used || unit < old_max;
123         unit++, samplers_used >>= 1, texel_fetch_samplers >>= 1) {
124       struct pipe_sampler_view *sampler_view = NULL;
125 
126       if (samplers_used & 1) {
127          const GLuint texUnit = prog->SamplerUnits[unit];
128 
129          /* The EXT_texture_sRGB_decode extension says:
130           *
131           *    "The conversion of sRGB color space components to linear color
132           *     space is always performed if the texel lookup function is one
133           *     of the texelFetch builtin functions.
134           *
135           *     Otherwise, if the texel lookup function is one of the texture
136           *     builtin functions or one of the texture gather functions, the
137           *     conversion of sRGB color space components to linear color space
138           *     is controlled by the TEXTURE_SRGB_DECODE_EXT parameter.
139           *
140           *     If the TEXTURE_SRGB_DECODE_EXT parameter is DECODE_EXT, the
141           *     conversion of sRGB color space components to linear color space
142           *     is performed.
143           *
144           *     If the TEXTURE_SRGB_DECODE_EXT parameter is SKIP_DECODE_EXT,
145           *     the value is returned without decoding. However, if the texture
146           *     is also [statically] accessed with a texelFetch function, then
147           *     the result of texture builtin functions and/or texture gather
148           *     functions may be returned with decoding or without decoding."
149           *
150           * Note: the "statically" will be added to the language per
151           *       https://cvs.khronos.org/bugzilla/show_bug.cgi?id=14934
152           *
153           * So we simply ignore the setting entirely for samplers that are
154           * (statically) accessed with a texelFetch function.
155           */
156          st_update_single_texture(st, &sampler_view, texUnit, glsl130,
157                                   texel_fetch_samplers & 1);
158          num_textures = unit + 1;
159       }
160 
161       pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
162    }
163 
164    /* For any external samplers with multiplaner YUV, stuff the additional
165     * sampler views we need at the end.
166     *
167     * Trying to cache the sampler view in the stObj looks painful, so just
168     * re-create the sampler view for the extra planes each time.  Main use
169     * case is video playback (ie. fps games wouldn't be using this) so I
170     * guess no point to try to optimize this feature.
171     */
172    while (unlikely(external_samplers_used)) {
173       GLuint unit = u_bit_scan(&external_samplers_used);
174       GLuint extra = 0;
175       struct st_texture_object *stObj =
176             st_get_texture_object(st->ctx, prog, unit);
177       struct pipe_sampler_view tmpl;
178 
179       if (!stObj)
180          continue;
181 
182       /* use original view as template: */
183       tmpl = *sampler_views[unit];
184 
185       /* if resource format matches then YUV wasn't lowered */
186       if (st_get_view_format(stObj) == stObj->pt->format)
187          continue;
188 
189       switch (st_get_view_format(stObj)) {
190       case PIPE_FORMAT_NV12:
191          /* we need one additional R8G8 view: */
192          tmpl.format = PIPE_FORMAT_RG88_UNORM;
193          tmpl.swizzle_g = PIPE_SWIZZLE_Y;   /* tmpl from Y plane is R8 */
194          extra = u_bit_scan(&free_slots);
195          sampler_views[extra] =
196                st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
197          break;
198       case PIPE_FORMAT_P010:
199       case PIPE_FORMAT_P016:
200          /* we need one additional R16G16 view: */
201          tmpl.format = PIPE_FORMAT_RG1616_UNORM;
202          tmpl.swizzle_g = PIPE_SWIZZLE_Y;   /* tmpl from Y plane is R16 */
203          extra = u_bit_scan(&free_slots);
204          sampler_views[extra] =
205                st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
206          break;
207       case PIPE_FORMAT_IYUV:
208          /* we need two additional R8 views: */
209          tmpl.format = PIPE_FORMAT_R8_UNORM;
210          extra = u_bit_scan(&free_slots);
211          sampler_views[extra] =
212                st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
213          extra = u_bit_scan(&free_slots);
214          sampler_views[extra] =
215                st->pipe->create_sampler_view(st->pipe, stObj->pt->next->next, &tmpl);
216          break;
217       case PIPE_FORMAT_YUYV:
218          /* we need one additional BGRA8888 view: */
219          tmpl.format = PIPE_FORMAT_BGRA8888_UNORM;
220          tmpl.swizzle_b = PIPE_SWIZZLE_Z;
221          tmpl.swizzle_a = PIPE_SWIZZLE_W;
222          extra = u_bit_scan(&free_slots);
223          sampler_views[extra] =
224                st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
225          break;
226       case PIPE_FORMAT_UYVY:
227          /* we need one additional RGBA8888 view: */
228          tmpl.format = PIPE_FORMAT_RGBA8888_UNORM;
229          tmpl.swizzle_b = PIPE_SWIZZLE_Z;
230          tmpl.swizzle_a = PIPE_SWIZZLE_W;
231          extra = u_bit_scan(&free_slots);
232          sampler_views[extra] =
233                st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
234          break;
235       default:
236          break;
237       }
238 
239       num_textures = MAX2(num_textures, extra + 1);
240    }
241 
242    cso_set_sampler_views(st->cso_context,
243                          shader_stage,
244                          num_textures,
245                          sampler_views);
246    st->state.num_sampler_views[shader_stage] = num_textures;
247 }
248 
249 /* Same as update_textures, but don't store the views in st_context. */
250 static void
update_textures_local(struct st_context * st,enum pipe_shader_type shader_stage,const struct gl_program * prog)251 update_textures_local(struct st_context *st,
252                       enum pipe_shader_type shader_stage,
253                       const struct gl_program *prog)
254 {
255    struct pipe_sampler_view *local_views[PIPE_MAX_SAMPLERS] = {0};
256 
257    update_textures(st, shader_stage, prog, local_views);
258 
259    unsigned num = st->state.num_sampler_views[shader_stage];
260    for (unsigned i = 0; i < num; i++)
261       pipe_sampler_view_reference(&local_views[i], NULL);
262 }
263 
264 void
st_update_vertex_textures(struct st_context * st)265 st_update_vertex_textures(struct st_context *st)
266 {
267    const struct gl_context *ctx = st->ctx;
268 
269    if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
270       update_textures(st,
271                       PIPE_SHADER_VERTEX,
272                       ctx->VertexProgram._Current,
273                       st->state.vert_sampler_views);
274    }
275 }
276 
277 
278 void
st_update_fragment_textures(struct st_context * st)279 st_update_fragment_textures(struct st_context *st)
280 {
281    const struct gl_context *ctx = st->ctx;
282 
283    update_textures(st,
284                    PIPE_SHADER_FRAGMENT,
285                    ctx->FragmentProgram._Current,
286                    st->state.frag_sampler_views);
287 }
288 
289 
290 void
st_update_geometry_textures(struct st_context * st)291 st_update_geometry_textures(struct st_context *st)
292 {
293    const struct gl_context *ctx = st->ctx;
294 
295    if (ctx->GeometryProgram._Current) {
296       update_textures_local(st, PIPE_SHADER_GEOMETRY,
297                             ctx->GeometryProgram._Current);
298    }
299 }
300 
301 
302 void
st_update_tessctrl_textures(struct st_context * st)303 st_update_tessctrl_textures(struct st_context *st)
304 {
305    const struct gl_context *ctx = st->ctx;
306 
307    if (ctx->TessCtrlProgram._Current) {
308       update_textures_local(st, PIPE_SHADER_TESS_CTRL,
309                             ctx->TessCtrlProgram._Current);
310    }
311 }
312 
313 
314 void
st_update_tesseval_textures(struct st_context * st)315 st_update_tesseval_textures(struct st_context *st)
316 {
317    const struct gl_context *ctx = st->ctx;
318 
319    if (ctx->TessEvalProgram._Current) {
320       update_textures_local(st, PIPE_SHADER_TESS_EVAL,
321                             ctx->TessEvalProgram._Current);
322    }
323 }
324 
325 
326 void
st_update_compute_textures(struct st_context * st)327 st_update_compute_textures(struct st_context *st)
328 {
329    const struct gl_context *ctx = st->ctx;
330 
331    if (ctx->ComputeProgram._Current) {
332       update_textures_local(st, PIPE_SHADER_COMPUTE,
333                             ctx->ComputeProgram._Current);
334    }
335 }
336