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