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