xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_texture.h (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 #ifndef ST_TEXTURE_H
29 #define ST_TEXTURE_H
30 
31 
32 #include "pipe/p_context.h"
33 #include "util/u_sampler.h"
34 #include "util/simple_mtx.h"
35 
36 #include "main/mtypes.h"
37 
38 
39 struct pipe_resource;
40 
41 
42 struct st_texture_image_transfer
43 {
44    struct pipe_transfer *transfer;
45 
46    /* For compressed texture fallback. */
47    GLubyte *temp_data; /**< Temporary compressed texture storage. */
48    unsigned temp_stride; /**< Stride of the compressed texture storage. */
49    struct pipe_box box; /**< Region of the transfer's resource to write. */
50 };
51 
52 
53 /**
54  * Container for one context's validated sampler view.
55  */
56 struct st_sampler_view
57 {
58    struct pipe_sampler_view *view;
59 
60    /** The context which created this view */
61    struct st_context *st;
62 
63    /** The glsl version of the shader seen during validation */
64    bool glsl130_or_later;
65    /** Derived from the sampler's sRGBDecode state during validation */
66    bool srgb_skip_decode;
67 
68    /* This mechanism allows passing sampler view references to the driver
69     * without using atomics to increase the reference count.
70     *
71     * This private refcount can be decremented without atomics but only one
72     * context (st above) can use this counter (so that it's only used by
73     * 1 thread).
74     *
75     * This number is atomically added to view->reference.count at
76     * initialization. If it's never used, the same number is atomically
77     * subtracted from view->reference.count before destruction. If this
78     * number is decremented, we can pass one reference to the driver without
79     * touching reference.count with atomics. At destruction we only subtract
80     * the number of references we have not returned. This can possibly turn
81     * a million atomic increments into 1 add and 1 subtract atomic op over
82     * the whole lifetime of an app.
83     */
84    int private_refcount;
85 };
86 
87 
88 /**
89  * Container for per-context sampler views of a texture.
90  */
91 struct st_sampler_views
92 {
93    struct st_sampler_views *next;
94    uint32_t max;
95    uint32_t count;
96    struct st_sampler_view views[0];
97 };
98 
99 struct st_compressed_data
100 {
101    struct pipe_reference reference;
102    GLubyte *ptr;
103 };
104 
105 static inline const struct gl_texture_image *
st_texture_image_const(const struct gl_texture_image * img)106 st_texture_image_const(const struct gl_texture_image *img)
107 {
108    return (const struct gl_texture_image *) img;
109 }
110 
111 static inline const struct gl_texture_object *
st_texture_object_const(const struct gl_texture_object * obj)112 st_texture_object_const(const struct gl_texture_object *obj)
113 {
114    return (const struct gl_texture_object *) obj;
115 }
116 
117 
118 static inline struct pipe_resource *
st_get_texobj_resource(struct gl_texture_object * texObj)119 st_get_texobj_resource(struct gl_texture_object *texObj)
120 {
121    return texObj ? texObj->pt : NULL;
122 }
123 
124 
125 static inline struct pipe_resource *
st_get_stobj_resource(struct gl_texture_object * stObj)126 st_get_stobj_resource(struct gl_texture_object *stObj)
127 {
128    return stObj ? stObj->pt : NULL;
129 }
130 
131 
132 static inline struct gl_texture_object *
st_get_texture_object(struct gl_context * ctx,const struct gl_program * prog,unsigned unit)133 st_get_texture_object(struct gl_context *ctx,
134                       const struct gl_program *prog,
135                       unsigned unit)
136 {
137    const GLuint texUnit = prog->SamplerUnits[unit];
138    return ctx->Texture.Unit[texUnit]._Current;
139 }
140 
141 static inline enum pipe_format
st_get_view_format(struct gl_texture_object * stObj)142 st_get_view_format(struct gl_texture_object *stObj)
143 {
144    if (!stObj)
145       return PIPE_FORMAT_NONE;
146    return stObj->surface_based ? stObj->surface_format : stObj->pt->format;
147 }
148 
149 
150 extern struct pipe_resource *
151 st_texture_create(struct st_context *st,
152                   enum pipe_texture_target target,
153                   enum pipe_format format,
154                   GLuint last_level,
155                   GLuint width0,
156                   GLuint height0,
157                   GLuint depth0,
158                   GLuint layers,
159                   GLuint nr_samples,
160                   GLuint tex_usage,
161                   bool sparse,
162                   uint32_t compression);
163 
164 
165 extern void
166 st_gl_texture_dims_to_pipe_dims(GLenum texture,
167                                 unsigned widthIn,
168                                 uint16_t heightIn,
169                                 uint16_t depthIn,
170                                 unsigned *widthOut,
171                                 uint16_t *heightOut,
172                                 uint16_t *depthOut,
173                                 uint16_t *layersOut);
174 
175 /* Check if an image fits into an existing texture object.
176  */
177 extern GLboolean
178 st_texture_match_image(struct st_context *st,
179                        const struct pipe_resource *pt,
180                        const struct gl_texture_image *image);
181 
182 /* Insert a transfer pointer into the image's transfer array at the specified
183  * index. The array is reallocated if necessary.
184  */
185 void
186 st_texture_image_insert_transfer(struct gl_texture_image *stImage,
187                                  unsigned index,
188                                  struct pipe_transfer *transfer);
189 
190 /**
191  * Returns the level of the gl_texture_image with respect to the resource it
192  * is allocated within. Example: returns 0 for non-finalized texture.
193  */
194 GLuint
195 st_texture_image_resource_level(struct gl_texture_image *stImage);
196 
197 /* Return a pointer to an image within a texture.  Return image stride as
198  * well.
199  */
200 extern GLubyte *
201 st_texture_image_map(struct st_context *st, struct gl_texture_image *stImage,
202                      enum pipe_map_flags usage,
203                      GLuint x, GLuint y, GLuint z,
204                      GLuint w, GLuint h, GLuint d,
205                      struct pipe_transfer **transfer);
206 
207 extern void
208 st_texture_image_unmap(struct st_context *st,
209                        struct gl_texture_image *stImage, unsigned slice);
210 
211 
212 /* Return pointers to each 2d slice within an image.  Indexed by depth
213  * value.
214  */
215 extern const GLuint *
216 st_texture_depth_offsets(struct pipe_resource *pt, GLuint level);
217 
218 /* Copy an image between two textures
219  */
220 extern void
221 st_texture_image_copy(struct pipe_context *pipe,
222                       struct pipe_resource *dst, GLuint dstLevel,
223                       struct pipe_resource *src, GLuint srcLevel,
224                       GLuint face);
225 
226 
227 extern struct pipe_resource *
228 st_create_color_map_texture(struct gl_context *ctx);
229 
230 void
231 st_destroy_bound_texture_handles(struct st_context *st);
232 
233 void
234 st_destroy_bound_image_handles(struct st_context *st);
235 
236 bool
237 st_astc_format_fallback(const struct st_context *st, mesa_format format);
238 
239 bool
240 st_compressed_format_fallback(struct st_context *st, mesa_format format);
241 
242 void
243 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
244                  struct pipe_image_view *img, enum gl_access_qualifier shader_access);
245 
246 void
247 st_convert_image_from_unit(const struct st_context *st,
248                            struct pipe_image_view *img,
249                            GLuint imgUnit,
250                            enum gl_access_qualifier shader_access);
251 
252 void
253 st_convert_sampler(const struct st_context *st,
254                    const struct gl_texture_object *texobj,
255                    const struct gl_sampler_object *msamp,
256                    float tex_unit_lod_bias,
257                    struct pipe_sampler_state *sampler,
258                    bool seamless_cube_map,
259                    bool ignore_srgb_decode,
260                    bool glsl130_or_later);
261 
262 void
263 st_convert_sampler_from_unit(const struct st_context *st,
264                              struct pipe_sampler_state *sampler,
265                              GLuint texUnit,
266                              bool glsl130_or_later);
267 
268 struct pipe_sampler_view *
269 st_update_single_texture(struct st_context *st,
270                          GLuint texUnit, bool glsl130_or_later,
271                          bool ignore_srgb_decode, bool get_reference);
272 
273 unsigned
274 st_get_sampler_views(struct st_context *st,
275                      enum pipe_shader_type shader_stage,
276                      const struct gl_program *prog,
277                      struct pipe_sampler_view **sampler_views);
278 
279 void
280 st_make_bound_samplers_resident(struct st_context *st,
281                                 struct gl_program *prog);
282 
283 void
284 st_make_bound_images_resident(struct st_context *st,
285                               struct gl_program *prog);
286 
287 #endif
288