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