1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 #include "pipe/p_context.h"
33 #include "util/u_sampler.h"
34 #include "util/simple_mtx.h"
36 #include "main/mtypes.h"
42 struct st_texture_image_transfer
44 struct pipe_transfer
*transfer
;
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. */
54 * Container for one context's validated sampler view.
56 struct st_sampler_view
58 struct pipe_sampler_view
*view
;
60 /** The context which created this view */
61 struct st_context
*st
;
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
;
71 * Container for per-context sampler views of a texture.
73 struct st_sampler_views
75 struct st_sampler_views
*next
;
78 struct st_sampler_view views
[0];
83 * Subclass of gl_texure_image.
85 struct st_texture_image
87 struct gl_texture_image base
;
89 /* If stImage->pt != NULL, image data is stored here.
90 * Else there is no image data.
92 struct pipe_resource
*pt
;
94 /* List of transfers, allocated on demand.
95 * transfer[layer] is a mapping for that layer.
97 struct st_texture_image_transfer
*transfer
;
98 unsigned num_transfers
;
100 /* For compressed images unsupported by the driver. Keep track of
101 * the original data. This is necessary for mapping/unmapping,
102 * as well as image copies.
104 GLubyte
*compressed_data
;
109 * Subclass of gl_texure_object.
111 struct st_texture_object
113 struct gl_texture_object base
; /* The "parent" object */
115 /* The texture must include at levels [0..lastLevel] once validated:
119 unsigned int validated_first_level
;
120 unsigned int validated_last_level
;
122 /* On validation any active images held in main memory or in other
123 * textures will be copied to this texture and the old storage freed.
125 struct pipe_resource
*pt
;
127 /* Protect modifications of the sampler_views array */
128 simple_mtx_t validate_mutex
;
130 /* Container of sampler views (one per context) attached to this texture
131 * object. Created lazily on first binding in context.
133 * Purely read-only accesses to the current context's own sampler view
134 * require no locking. Another thread may simultaneously replace the
135 * container object in order to grow the array, but the old container will
138 * Writing to the container (even for modifying the current context's own
139 * sampler view) always requires taking the validate_mutex to protect against
140 * concurrent container switches.
142 * NULL'ing another context's sampler view is allowed only while
143 * implementing an API call that modifies the texture: an application which
144 * calls those while simultaneously reading the texture in another context
145 * invokes undefined behavior. (TODO: a dubious violation of this rule is
146 * st_finalize_texture, which is a lazy operation that corresponds to a
147 * texture modification.)
149 struct st_sampler_views
*sampler_views
;
151 /* Old sampler views container objects that have not been freed yet because
152 * other threads/contexts may still be reading from them.
154 struct st_sampler_views
*sampler_views_old
;
156 /* True if this texture comes from the window system. Such a texture
157 * cannot be reallocated and the format can only be changed with a sampler
160 GLboolean surface_based
;
162 /* If surface_based is true, this format should be used for all sampler
163 * views and surfaces instead of pt->format.
165 enum pipe_format surface_format
;
167 /* When non-negative, samplers should use this level instead of the level
168 * range specified by the GL state.
170 * This is used for EGL images, which may correspond to a single level out
171 * of an imported pipe_resources with multiple mip levels.
175 /* When non-negative, samplers should use this layer instead of the one
176 * specified by the GL state.
178 * This is used for EGL images and VDPAU interop, where imported
179 * pipe_resources may be cube, 3D, or array textures (containing layers
180 * with different fields in the case of VDPAU) even though the GL state
181 * describes one non-array texture per field.
186 * Set when the texture images of this texture object might not all be in
187 * the pipe_resource *pt above.
189 bool needs_validation
;
193 static inline struct st_texture_image
*
194 st_texture_image(struct gl_texture_image
*img
)
196 return (struct st_texture_image
*) img
;
199 static inline const struct st_texture_image
*
200 st_texture_image_const(const struct gl_texture_image
*img
)
202 return (const struct st_texture_image
*) img
;
205 static inline struct st_texture_object
*
206 st_texture_object(struct gl_texture_object
*obj
)
208 return (struct st_texture_object
*) obj
;
211 static inline const struct st_texture_object
*
212 st_texture_object_const(const struct gl_texture_object
*obj
)
214 return (const struct st_texture_object
*) obj
;
218 static inline struct pipe_resource
*
219 st_get_texobj_resource(struct gl_texture_object
*texObj
)
221 struct st_texture_object
*stObj
= st_texture_object(texObj
);
222 return stObj
? stObj
->pt
: NULL
;
226 static inline struct pipe_resource
*
227 st_get_stobj_resource(struct st_texture_object
*stObj
)
229 return stObj
? stObj
->pt
: NULL
;
233 static inline struct st_texture_object
*
234 st_get_texture_object(struct gl_context
*ctx
,
235 const struct gl_program
*prog
,
238 const GLuint texUnit
= prog
->SamplerUnits
[unit
];
239 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[texUnit
]._Current
;
244 return st_texture_object(texObj
);
247 static inline enum pipe_format
248 st_get_view_format(struct st_texture_object
*stObj
)
251 return PIPE_FORMAT_NONE
;
252 return stObj
->surface_based
? stObj
->surface_format
: stObj
->pt
->format
;
256 extern struct pipe_resource
*
257 st_texture_create(struct st_context
*st
,
258 enum pipe_texture_target target
,
259 enum pipe_format format
,
270 st_gl_texture_dims_to_pipe_dims(GLenum texture
,
277 uint16_t *layersOut
);
279 /* Check if an image fits into an existing texture object.
282 st_texture_match_image(struct st_context
*st
,
283 const struct pipe_resource
*pt
,
284 const struct gl_texture_image
*image
);
286 /* Return a pointer to an image within a texture. Return image stride as
290 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
291 enum pipe_transfer_usage usage
,
292 GLuint x
, GLuint y
, GLuint z
,
293 GLuint w
, GLuint h
, GLuint d
,
294 struct pipe_transfer
**transfer
);
297 st_texture_image_unmap(struct st_context
*st
,
298 struct st_texture_image
*stImage
, unsigned slice
);
301 /* Return pointers to each 2d slice within an image. Indexed by depth
304 extern const GLuint
*
305 st_texture_depth_offsets(struct pipe_resource
*pt
, GLuint level
);
307 /* Copy an image between two textures
310 st_texture_image_copy(struct pipe_context
*pipe
,
311 struct pipe_resource
*dst
, GLuint dstLevel
,
312 struct pipe_resource
*src
, GLuint srcLevel
,
316 extern struct pipe_resource
*
317 st_create_color_map_texture(struct gl_context
*ctx
);
320 st_destroy_bound_texture_handles(struct st_context
*st
);
323 st_destroy_bound_image_handles(struct st_context
*st
);
326 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
);
329 st_convert_image(const struct st_context
*st
, const struct gl_image_unit
*u
,
330 struct pipe_image_view
*img
, unsigned shader_access
);
333 st_convert_image_from_unit(const struct st_context
*st
,
334 struct pipe_image_view
*img
,
336 unsigned shader_access
);
339 st_convert_sampler(const struct st_context
*st
,
340 const struct gl_texture_object
*texobj
,
341 const struct gl_sampler_object
*msamp
,
342 float tex_unit_lod_bias
,
343 struct pipe_sampler_state
*sampler
);
346 st_convert_sampler_from_unit(const struct st_context
*st
,
347 struct pipe_sampler_state
*sampler
,
351 st_update_single_texture(struct st_context
*st
,
352 struct pipe_sampler_view
**sampler_view
,
353 GLuint texUnit
, bool glsl130_or_later
,
354 bool ignore_srgb_decode
);
357 st_make_bound_samplers_resident(struct st_context
*st
,
358 struct gl_program
*prog
);
361 st_make_bound_images_resident(struct st_context
*st
,
362 struct gl_program
*prog
);