st/mesa: refactor duplicated etc fallback checks
[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 * Subclass of gl_texure_image.
53 */
54 struct st_texture_image
55 {
56 struct gl_texture_image base;
57
58 /* If stImage->pt != NULL, image data is stored here.
59 * Else there is no image data.
60 */
61 struct pipe_resource *pt;
62
63 /* List of transfers, allocated on demand.
64 * transfer[layer] is a mapping for that layer.
65 */
66 struct st_texture_image_transfer *transfer;
67 unsigned num_transfers;
68 };
69
70
71 /**
72 * Subclass of gl_texure_object.
73 */
74 struct st_texture_object
75 {
76 struct gl_texture_object base; /* The "parent" object */
77
78 /* The texture must include at levels [0..lastLevel] once validated:
79 */
80 GLuint lastLevel;
81
82 /* On validation any active images held in main memory or in other
83 * textures will be copied to this texture and the old storage freed.
84 */
85 struct pipe_resource *pt;
86
87 /* Number of views in sampler_views array */
88 GLuint num_sampler_views;
89
90 /* Array of sampler views (one per context) attached to this texture
91 * object. Created lazily on first binding in context.
92 */
93 struct pipe_sampler_view **sampler_views;
94
95 /* True if this texture comes from the window system. Such a texture
96 * cannot be reallocated and the format can only be changed with a sampler
97 * view or a surface.
98 */
99 GLboolean surface_based;
100
101 /* If surface_based is true, this format should be used for all sampler
102 * views and surfaces instead of pt->format.
103 */
104 enum pipe_format surface_format;
105 };
106
107
108 static inline struct st_texture_image *
109 st_texture_image(struct gl_texture_image *img)
110 {
111 return (struct st_texture_image *) img;
112 }
113
114 static inline const struct st_texture_image *
115 st_texture_image_const(const struct gl_texture_image *img)
116 {
117 return (const struct st_texture_image *) img;
118 }
119
120 static inline struct st_texture_object *
121 st_texture_object(struct gl_texture_object *obj)
122 {
123 return (struct st_texture_object *) obj;
124 }
125
126 static inline const struct st_texture_object *
127 st_texture_object_const(const struct gl_texture_object *obj)
128 {
129 return (const struct st_texture_object *) obj;
130 }
131
132
133 static inline struct pipe_resource *
134 st_get_texobj_resource(struct gl_texture_object *texObj)
135 {
136 struct st_texture_object *stObj = st_texture_object(texObj);
137 return stObj ? stObj->pt : NULL;
138 }
139
140
141 static inline struct pipe_resource *
142 st_get_stobj_resource(struct st_texture_object *stObj)
143 {
144 return stObj ? stObj->pt : NULL;
145 }
146
147
148 static inline struct pipe_sampler_view *
149 st_create_texture_sampler_view_format(struct pipe_context *pipe,
150 struct pipe_resource *texture,
151 enum pipe_format format)
152 {
153 struct pipe_sampler_view templ;
154
155 u_sampler_view_default_template(&templ, texture, format);
156
157 return pipe->create_sampler_view(pipe, texture, &templ);
158 }
159
160 static inline struct pipe_sampler_view *
161 st_create_texture_sampler_view(struct pipe_context *pipe,
162 struct pipe_resource *texture)
163 {
164 return st_create_texture_sampler_view_format(pipe, texture,
165 texture->format);
166 }
167
168
169
170 extern struct pipe_resource *
171 st_texture_create(struct st_context *st,
172 enum pipe_texture_target target,
173 enum pipe_format format,
174 GLuint last_level,
175 GLuint width0,
176 GLuint height0,
177 GLuint depth0,
178 GLuint layers,
179 GLuint nr_samples,
180 GLuint tex_usage );
181
182
183 extern void
184 st_gl_texture_dims_to_pipe_dims(GLenum texture,
185 GLuint widthIn,
186 GLuint heightIn,
187 GLuint depthIn,
188 GLuint *widthOut,
189 GLuint *heightOut,
190 GLuint *depthOut,
191 GLuint *layersOut);
192
193 /* Check if an image fits into an existing texture object.
194 */
195 extern GLboolean
196 st_texture_match_image(struct st_context *st,
197 const struct pipe_resource *pt,
198 const struct gl_texture_image *image);
199
200 /* Return a pointer to an image within a texture. Return image stride as
201 * well.
202 */
203 extern GLubyte *
204 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
205 enum pipe_transfer_usage usage,
206 GLuint x, GLuint y, GLuint z,
207 GLuint w, GLuint h, GLuint d,
208 struct pipe_transfer **transfer);
209
210 extern void
211 st_texture_image_unmap(struct st_context *st,
212 struct st_texture_image *stImage, unsigned slice);
213
214
215 /* Return pointers to each 2d slice within an image. Indexed by depth
216 * value.
217 */
218 extern const GLuint *
219 st_texture_depth_offsets(struct pipe_resource *pt, GLuint level);
220
221 /* Copy an image between two textures
222 */
223 extern void
224 st_texture_image_copy(struct pipe_context *pipe,
225 struct pipe_resource *dst, GLuint dstLevel,
226 struct pipe_resource *src, GLuint srcLevel,
227 GLuint face);
228
229
230 extern struct pipe_resource *
231 st_create_color_map_texture(struct gl_context *ctx);
232
233 extern struct pipe_sampler_view **
234 st_texture_get_sampler_view(struct st_context *st,
235 struct st_texture_object *stObj);
236
237 extern void
238 st_texture_release_sampler_view(struct st_context *st,
239 struct st_texture_object *stObj);
240
241 extern void
242 st_texture_release_all_sampler_views(struct st_context *st,
243 struct st_texture_object *stObj);
244
245 void
246 st_texture_free_sampler_views(struct st_texture_object *stObj);
247
248 bool
249 st_etc_fallback(struct st_context *st, struct gl_texture_image *texImage);
250
251 #endif