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