xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_atom_texture.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
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 struct pipe_sampler_view *
st_update_single_texture(struct st_context * st,GLuint texUnit,bool glsl130_or_later,bool ignore_srgb_decode,bool get_reference)59 st_update_single_texture(struct st_context *st,
60                          GLuint texUnit, bool glsl130_or_later,
61                          bool ignore_srgb_decode, bool get_reference)
62 {
63    struct gl_context *ctx = st->ctx;
64    struct gl_texture_object *texObj;
65 
66    texObj = ctx->Texture.Unit[texUnit]._Current;
67    assert(texObj);
68 
69    GLenum target = texObj->Target;
70 
71    if (unlikely(target == GL_TEXTURE_BUFFER))
72       return st_get_buffer_sampler_view_from_stobj(st, texObj, get_reference);
73 
74    if (!st_finalize_texture(ctx, st->pipe, texObj, 0) || !texObj->pt)
75       return NULL; /* out of mem */
76 
77    if (target == GL_TEXTURE_EXTERNAL_OES &&
78        texObj->pt->screen->resource_changed)
79          texObj->pt->screen->resource_changed(texObj->pt->screen, texObj->pt);
80 
81    return st_get_texture_sampler_view_from_stobj(st, texObj,
82                                                  _mesa_get_samplerobj(ctx, texUnit),
83                                                  glsl130_or_later,
84                                                  ignore_srgb_decode, get_reference);
85 }
86 
87 
88 
89 unsigned
st_get_sampler_views(struct st_context * st,enum pipe_shader_type shader_stage,const struct gl_program * prog,struct pipe_sampler_view ** sampler_views)90 st_get_sampler_views(struct st_context *st,
91                      enum pipe_shader_type shader_stage,
92                      const struct gl_program *prog,
93                      struct pipe_sampler_view **sampler_views)
94 {
95    struct pipe_context *pipe = st->pipe;
96    const GLuint old_max = st->state.num_sampler_views[shader_stage];
97    GLbitfield samplers_used = prog->SamplersUsed;
98    GLbitfield texel_fetch_samplers = prog->info.textures_used_by_txf[0];
99    GLbitfield free_slots = ~prog->SamplersUsed;
100    GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
101    GLuint unit;
102 
103    if (samplers_used == 0x0 && old_max == 0)
104       return 0;
105 
106    unsigned num_textures = util_last_bit(samplers_used);
107    const bool glsl130 =
108       (prog->shader_program ? prog->shader_program->GLSL_Version : 0) >= 130;
109 
110    /* loop over sampler units (aka tex image units) */
111    for (unit = 0; unit < num_textures; unit++) {
112       unsigned bit = BITFIELD_BIT(unit);
113 
114       if (!(samplers_used & bit)) {
115          sampler_views[unit] = NULL;
116          continue;
117       }
118 
119       /* The EXT_texture_sRGB_decode extension says:
120        *
121        *    "The conversion of sRGB color space components to linear color
122        *     space is always performed if the texel lookup function is one
123        *     of the texelFetch builtin functions.
124        *
125        *     Otherwise, if the texel lookup function is one of the texture
126        *     builtin functions or one of the texture gather functions, the
127        *     conversion of sRGB color space components to linear color space
128        *     is controlled by the TEXTURE_SRGB_DECODE_EXT parameter.
129        *
130        *     If the TEXTURE_SRGB_DECODE_EXT parameter is DECODE_EXT, the
131        *     conversion of sRGB color space components to linear color space
132        *     is performed.
133        *
134        *     If the TEXTURE_SRGB_DECODE_EXT parameter is SKIP_DECODE_EXT,
135        *     the value is returned without decoding. However, if the texture
136        *     is also [statically] accessed with a texelFetch function, then
137        *     the result of texture builtin functions and/or texture gather
138        *     functions may be returned with decoding or without decoding."
139        *
140        * Note: the "statically" will be added to the language per
141        *       https://cvs.khronos.org/bugzilla/show_bug.cgi?id=14934
142        *
143        * So we simply ignore the setting entirely for samplers that are
144        * (statically) accessed with a texelFetch function.
145        */
146       sampler_views[unit] =
147          st_update_single_texture(st, prog->SamplerUnits[unit], glsl130,
148                                   texel_fetch_samplers & bit, true);
149    }
150 
151    /* For any external samplers with multiplaner YUV, stuff the additional
152     * sampler views we need at the end.
153     *
154     * Trying to cache the sampler view in the texObj looks painful, so just
155     * re-create the sampler view for the extra planes each time.  Main use
156     * case is video playback (ie. fps games wouldn't be using this) so I
157     * guess no point to try to optimize this feature.
158     */
159    while (unlikely(external_samplers_used)) {
160       GLuint unit = u_bit_scan(&external_samplers_used);
161       GLuint extra = 0;
162       struct gl_texture_object *stObj =
163             st_get_texture_object(st->ctx, prog, unit);
164       struct pipe_sampler_view tmpl;
165 
166       if (!stObj)
167          continue;
168 
169       /* use original view as template: */
170       tmpl = *sampler_views[unit];
171 
172       /* if resource format matches then YUV wasn't lowered */
173       if (st_get_view_format(stObj) == stObj->pt->format)
174          continue;
175 
176       switch (st_get_view_format(stObj)) {
177       case PIPE_FORMAT_NV12:
178          if (stObj->pt->format == PIPE_FORMAT_R8_G8B8_420_UNORM)
179             /* no additional views needed */
180             break;
181 
182          /* we need one additional R8G8 view: */
183          tmpl.format = PIPE_FORMAT_RG88_UNORM;
184          tmpl.swizzle_g = PIPE_SWIZZLE_Y;   /* tmpl from Y plane is R8 */
185          extra = u_bit_scan(&free_slots);
186          sampler_views[extra] =
187                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
188          break;
189       case PIPE_FORMAT_NV21:
190          if (stObj->pt->format == PIPE_FORMAT_R8_B8G8_420_UNORM)
191             /* no additional views needed */
192             break;
193 
194          /* we need one additional R8G8 view: */
195          tmpl.format = PIPE_FORMAT_RG88_UNORM;
196          tmpl.swizzle_g = PIPE_SWIZZLE_Y;   /* tmpl from Y plane is R8 */
197          extra = u_bit_scan(&free_slots);
198          sampler_views[extra] =
199                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
200          break;
201       case PIPE_FORMAT_P010:
202       case PIPE_FORMAT_P012:
203       case PIPE_FORMAT_P016:
204       case PIPE_FORMAT_P030:
205          /* we need one additional R16G16 view: */
206          tmpl.format = PIPE_FORMAT_RG1616_UNORM;
207          tmpl.swizzle_g = PIPE_SWIZZLE_Y;   /* tmpl from Y plane is R16 */
208          extra = u_bit_scan(&free_slots);
209          sampler_views[extra] =
210                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
211          break;
212       case PIPE_FORMAT_IYUV:
213          if (stObj->pt->format == PIPE_FORMAT_R8_G8_B8_420_UNORM ||
214              stObj->pt->format == PIPE_FORMAT_R8_B8_G8_420_UNORM)
215             /* no additional views needed */
216             break;
217 
218          /* we need two additional R8 views: */
219          tmpl.format = PIPE_FORMAT_R8_UNORM;
220          extra = u_bit_scan(&free_slots);
221          sampler_views[extra] =
222                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
223          extra = u_bit_scan(&free_slots);
224          sampler_views[extra] =
225                pipe->create_sampler_view(pipe, stObj->pt->next->next, &tmpl);
226          break;
227       case PIPE_FORMAT_YUYV:
228       case PIPE_FORMAT_YVYU:
229          if (stObj->pt->format == PIPE_FORMAT_R8G8_R8B8_UNORM ||
230              stObj->pt->format == PIPE_FORMAT_R8B8_R8G8_UNORM)
231             /* no additional views needed */
232             break;
233 
234          /* we need one additional BGRA8888 view: */
235          tmpl.format = PIPE_FORMAT_BGRA8888_UNORM;
236          tmpl.swizzle_b = PIPE_SWIZZLE_Z;
237          tmpl.swizzle_a = PIPE_SWIZZLE_W;
238          extra = u_bit_scan(&free_slots);
239          sampler_views[extra] =
240                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
241          break;
242       case PIPE_FORMAT_UYVY:
243       case PIPE_FORMAT_VYUY:
244          if (stObj->pt->format == PIPE_FORMAT_G8R8_B8R8_UNORM ||
245              stObj->pt->format == PIPE_FORMAT_B8R8_G8R8_UNORM)
246             /* no additional views needed */
247             break;
248 
249          /* we need one additional RGBA8888 view: */
250          tmpl.format = PIPE_FORMAT_RGBA8888_UNORM;
251          tmpl.swizzle_b = PIPE_SWIZZLE_Z;
252          tmpl.swizzle_a = PIPE_SWIZZLE_W;
253          extra = u_bit_scan(&free_slots);
254          sampler_views[extra] =
255                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
256          break;
257       case PIPE_FORMAT_Y210:
258       case PIPE_FORMAT_Y212:
259       case PIPE_FORMAT_Y216:
260          /* we need one additional R16G16B16A16 view: */
261          tmpl.format = PIPE_FORMAT_R16G16B16A16_UNORM;
262          tmpl.swizzle_b = PIPE_SWIZZLE_Z;
263          tmpl.swizzle_a = PIPE_SWIZZLE_W;
264          extra = u_bit_scan(&free_slots);
265          sampler_views[extra] =
266                pipe->create_sampler_view(pipe, stObj->pt->next, &tmpl);
267          break;
268       default:
269          break;
270       }
271 
272       num_textures = MAX2(num_textures, extra + 1);
273    }
274 
275    return num_textures;
276 }
277 
278 static void
update_textures(struct st_context * st,enum pipe_shader_type shader_stage,const struct gl_program * prog)279 update_textures(struct st_context *st,
280                 enum pipe_shader_type shader_stage,
281                 const struct gl_program *prog)
282 {
283    struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
284    struct pipe_context *pipe = st->pipe;
285    unsigned num_textures =
286       st_get_sampler_views(st, shader_stage, prog, sampler_views);
287 
288    unsigned old_num_textures = st->state.num_sampler_views[shader_stage];
289    unsigned num_unbind = old_num_textures > num_textures ?
290                             old_num_textures - num_textures : 0;
291 
292    pipe->set_sampler_views(pipe, shader_stage, 0, num_textures, num_unbind,
293                            true, sampler_views);
294    st->state.num_sampler_views[shader_stage] = num_textures;
295 }
296 
297 void
st_update_vertex_textures(struct st_context * st)298 st_update_vertex_textures(struct st_context *st)
299 {
300    const struct gl_context *ctx = st->ctx;
301 
302    if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
303       update_textures(st, PIPE_SHADER_VERTEX,
304                             ctx->VertexProgram._Current);
305    }
306 }
307 
308 
309 void
st_update_fragment_textures(struct st_context * st)310 st_update_fragment_textures(struct st_context *st)
311 {
312    const struct gl_context *ctx = st->ctx;
313 
314    update_textures(st, PIPE_SHADER_FRAGMENT,
315                          ctx->FragmentProgram._Current);
316 }
317 
318 
319 void
st_update_geometry_textures(struct st_context * st)320 st_update_geometry_textures(struct st_context *st)
321 {
322    const struct gl_context *ctx = st->ctx;
323 
324    if (ctx->GeometryProgram._Current) {
325       update_textures(st, PIPE_SHADER_GEOMETRY,
326                             ctx->GeometryProgram._Current);
327    }
328 }
329 
330 
331 void
st_update_tessctrl_textures(struct st_context * st)332 st_update_tessctrl_textures(struct st_context *st)
333 {
334    const struct gl_context *ctx = st->ctx;
335 
336    if (ctx->TessCtrlProgram._Current) {
337       update_textures(st, PIPE_SHADER_TESS_CTRL,
338                             ctx->TessCtrlProgram._Current);
339    }
340 }
341 
342 
343 void
st_update_tesseval_textures(struct st_context * st)344 st_update_tesseval_textures(struct st_context *st)
345 {
346    const struct gl_context *ctx = st->ctx;
347 
348    if (ctx->TessEvalProgram._Current) {
349       update_textures(st, PIPE_SHADER_TESS_EVAL,
350                             ctx->TessEvalProgram._Current);
351    }
352 }
353 
354 
355 void
st_update_compute_textures(struct st_context * st)356 st_update_compute_textures(struct st_context *st)
357 {
358    const struct gl_context *ctx = st->ctx;
359 
360    if (ctx->ComputeProgram._Current) {
361       update_textures(st, PIPE_SHADER_COMPUTE,
362                             ctx->ComputeProgram._Current);
363    }
364 }
365