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 GLubyte *map; /**< Saved map pointer of the uncompressed transfer. */
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
69
70 /**
71 * Container for per-context sampler views of a texture.
72 */
73 struct st_sampler_views
74 {
75 struct st_sampler_views *next;
76 uint32_t max;
77 uint32_t count;
78 struct st_sampler_view views[0];
79 };
80
81 struct st_compressed_data
82 {
83 struct pipe_reference reference;
84 GLubyte *ptr;
85 };
86
87
88 /**
89 * Subclass of gl_texure_image.
90 */
91 struct st_texture_image
92 {
93 struct gl_texture_image base;
94
95 /* If stImage->pt != NULL, image data is stored here.
96 * Else there is no image data.
97 */
98 struct pipe_resource *pt;
99
100 /* List of transfers, allocated on demand.
101 * transfer[layer] is a mapping for that layer.
102 */
103 struct st_texture_image_transfer *transfer;
104 unsigned num_transfers;
105
106 /* For compressed images unsupported by the driver. Keep track of
107 * the original data. This is necessary for mapping/unmapping,
108 * as well as image copies.
109 */
110 struct st_compressed_data* compressed_data;
111 };
112
113
114 /**
115 * Subclass of gl_texure_object.
116 */
117 struct st_texture_object
118 {
119 struct gl_texture_object base; /* The "parent" object */
120
121 /* The texture must include at levels [0..lastLevel] once validated:
122 */
123 GLuint lastLevel;
124
125 unsigned int validated_first_level;
126 unsigned int validated_last_level;
127
128 /* On validation any active images held in main memory or in other
129 * textures will be copied to this texture and the old storage freed.
130 */
131 struct pipe_resource *pt;
132
133 /* Protect modifications of the sampler_views array */
134 simple_mtx_t validate_mutex;
135
136 /* Container of sampler views (one per context) attached to this texture
137 * object. Created lazily on first binding in context.
138 *
139 * Purely read-only accesses to the current context's own sampler view
140 * require no locking. Another thread may simultaneously replace the
141 * container object in order to grow the array, but the old container will
142 * be kept alive.
143 *
144 * Writing to the container (even for modifying the current context's own
145 * sampler view) always requires taking the validate_mutex to protect against
146 * concurrent container switches.
147 *
148 * NULL'ing another context's sampler view is allowed only while
149 * implementing an API call that modifies the texture: an application which
150 * calls those while simultaneously reading the texture in another context
151 * invokes undefined behavior. (TODO: a dubious violation of this rule is
152 * st_finalize_texture, which is a lazy operation that corresponds to a
153 * texture modification.)
154 */
155 struct st_sampler_views *sampler_views;
156
157 /* Old sampler views container objects that have not been freed yet because
158 * other threads/contexts may still be reading from them.
159 */
160 struct st_sampler_views *sampler_views_old;
161
162 /* True if this texture comes from the window system. Such a texture
163 * cannot be reallocated and the format can only be changed with a sampler
164 * view or a surface.
165 */
166 GLboolean surface_based;
167
168 /* If surface_based is true, this format should be used for all sampler
169 * views and surfaces instead of pt->format.
170 */
171 enum pipe_format surface_format;
172
173 /* When non-negative, samplers should use this level instead of the level
174 * range specified by the GL state.
175 *
176 * This is used for EGL images, which may correspond to a single level out
177 * of an imported pipe_resources with multiple mip levels.
178 */
179 int level_override;
180
181 /* When non-negative, samplers should use this layer instead of the one
182 * specified by the GL state.
183 *
184 * This is used for EGL images and VDPAU interop, where imported
185 * pipe_resources may be cube, 3D, or array textures (containing layers
186 * with different fields in the case of VDPAU) even though the GL state
187 * describes one non-array texture per field.
188 */
189 int layer_override;
190
191 /**
192 * Set when the texture images of this texture object might not all be in
193 * the pipe_resource *pt above.
194 */
195 bool needs_validation;
196 };
197
198
199 static inline struct st_texture_image *
st_texture_image(struct gl_texture_image * img)200 st_texture_image(struct gl_texture_image *img)
201 {
202 return (struct st_texture_image *) img;
203 }
204
205 static inline const struct st_texture_image *
st_texture_image_const(const struct gl_texture_image * img)206 st_texture_image_const(const struct gl_texture_image *img)
207 {
208 return (const struct st_texture_image *) img;
209 }
210
211 static inline struct st_texture_object *
st_texture_object(struct gl_texture_object * obj)212 st_texture_object(struct gl_texture_object *obj)
213 {
214 return (struct st_texture_object *) obj;
215 }
216
217 static inline const struct st_texture_object *
st_texture_object_const(const struct gl_texture_object * obj)218 st_texture_object_const(const struct gl_texture_object *obj)
219 {
220 return (const struct st_texture_object *) obj;
221 }
222
223
224 static inline struct pipe_resource *
st_get_texobj_resource(struct gl_texture_object * texObj)225 st_get_texobj_resource(struct gl_texture_object *texObj)
226 {
227 struct st_texture_object *stObj = st_texture_object(texObj);
228 return stObj ? stObj->pt : NULL;
229 }
230
231
232 static inline struct pipe_resource *
st_get_stobj_resource(struct st_texture_object * stObj)233 st_get_stobj_resource(struct st_texture_object *stObj)
234 {
235 return stObj ? stObj->pt : NULL;
236 }
237
238
239 static inline struct st_texture_object *
st_get_texture_object(struct gl_context * ctx,const struct gl_program * prog,unsigned unit)240 st_get_texture_object(struct gl_context *ctx,
241 const struct gl_program *prog,
242 unsigned unit)
243 {
244 const GLuint texUnit = prog->SamplerUnits[unit];
245 struct gl_texture_object *texObj = ctx->Texture.Unit[texUnit]._Current;
246
247 if (!texObj)
248 return NULL;
249
250 return st_texture_object(texObj);
251 }
252
253 static inline enum pipe_format
st_get_view_format(struct st_texture_object * stObj)254 st_get_view_format(struct st_texture_object *stObj)
255 {
256 if (!stObj)
257 return PIPE_FORMAT_NONE;
258 return stObj->surface_based ? stObj->surface_format : stObj->pt->format;
259 }
260
261
262 extern struct pipe_resource *
263 st_texture_create(struct st_context *st,
264 enum pipe_texture_target target,
265 enum pipe_format format,
266 GLuint last_level,
267 GLuint width0,
268 GLuint height0,
269 GLuint depth0,
270 GLuint layers,
271 GLuint nr_samples,
272 GLuint tex_usage );
273
274
275 extern void
276 st_gl_texture_dims_to_pipe_dims(GLenum texture,
277 unsigned widthIn,
278 uint16_t heightIn,
279 uint16_t depthIn,
280 unsigned *widthOut,
281 uint16_t *heightOut,
282 uint16_t *depthOut,
283 uint16_t *layersOut);
284
285 /* Check if an image fits into an existing texture object.
286 */
287 extern GLboolean
288 st_texture_match_image(struct st_context *st,
289 const struct pipe_resource *pt,
290 const struct gl_texture_image *image);
291
292 /* Return a pointer to an image within a texture. Return image stride as
293 * well.
294 */
295 extern GLubyte *
296 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
297 enum pipe_map_flags usage,
298 GLuint x, GLuint y, GLuint z,
299 GLuint w, GLuint h, GLuint d,
300 struct pipe_transfer **transfer);
301
302 extern void
303 st_texture_image_unmap(struct st_context *st,
304 struct st_texture_image *stImage, unsigned slice);
305
306
307 /* Return pointers to each 2d slice within an image. Indexed by depth
308 * value.
309 */
310 extern const GLuint *
311 st_texture_depth_offsets(struct pipe_resource *pt, GLuint level);
312
313 /* Copy an image between two textures
314 */
315 extern void
316 st_texture_image_copy(struct pipe_context *pipe,
317 struct pipe_resource *dst, GLuint dstLevel,
318 struct pipe_resource *src, GLuint srcLevel,
319 GLuint face);
320
321
322 extern struct pipe_resource *
323 st_create_color_map_texture(struct gl_context *ctx);
324
325 void
326 st_destroy_bound_texture_handles(struct st_context *st);
327
328 void
329 st_destroy_bound_image_handles(struct st_context *st);
330
331 bool
332 st_astc_format_fallback(const struct st_context *st, mesa_format format);
333
334 bool
335 st_compressed_format_fallback(struct st_context *st, mesa_format format);
336
337 void
338 st_convert_image(const struct st_context *st, const struct gl_image_unit *u,
339 struct pipe_image_view *img, unsigned shader_access);
340
341 void
342 st_convert_image_from_unit(const struct st_context *st,
343 struct pipe_image_view *img,
344 GLuint imgUnit,
345 unsigned shader_access);
346
347 void
348 st_convert_sampler(const struct st_context *st,
349 const struct gl_texture_object *texobj,
350 const struct gl_sampler_object *msamp,
351 float tex_unit_lod_bias,
352 struct pipe_sampler_state *sampler);
353
354 void
355 st_convert_sampler_from_unit(const struct st_context *st,
356 struct pipe_sampler_state *sampler,
357 GLuint texUnit);
358
359 void
360 st_update_single_texture(struct st_context *st,
361 struct pipe_sampler_view **sampler_view,
362 GLuint texUnit, bool glsl130_or_later,
363 bool ignore_srgb_decode);
364
365 void
366 st_make_bound_samplers_resident(struct st_context *st,
367 struct gl_program *prog);
368
369 void
370 st_make_bound_images_resident(struct st_context *st,
371 struct gl_program *prog);
372
373 #endif
374