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