c62f7f2cc0d172834e519f3bddee0a72c08e5c35
[profile/ivi/mesa.git] / src / mesa / state_tracker / st_texture.h
1 /**************************************************************************
2  * 
3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 struct pipe_context;
38 struct pipe_texture;
39
40
41 struct st_texture_image
42 {
43    struct gl_texture_image base;
44
45    /* These aren't stored in gl_texture_image 
46     */
47    GLuint level;
48    GLuint face;
49
50    /* If stImage->pt != NULL, image data is stored here.
51     * Else if stImage->base.Data != NULL, image is stored there.
52     * Else there is no image data.
53     */
54    struct pipe_texture *pt;
55
56    struct pipe_transfer *transfer;
57 };
58
59
60
61 struct st_texture_object
62 {
63    struct gl_texture_object base;       /* The "parent" object */
64
65    /* The texture must include at levels [0..lastLevel] once validated:
66     */
67    GLuint lastLevel;
68
69    /* On validation any active images held in main memory or in other
70     * textures will be copied to this texture and the old storage freed.
71     */
72    struct pipe_texture *pt;
73
74    /* Default sampler view attached to this texture object. Created lazily
75     * on first binding.
76     */
77    struct pipe_sampler_view *sampler_view;
78
79    struct pipe_context *pipe;
80
81    GLboolean teximage_realloc;
82
83    /* True if there is/was a surface bound to this texture object.  It helps
84     * track whether the texture object is surface based or not.
85     */
86    GLboolean surface_based;
87 };
88
89
90 static INLINE struct st_texture_image *
91 st_texture_image(struct gl_texture_image *img)
92 {
93    return (struct st_texture_image *) img;
94 }
95
96 static INLINE struct st_texture_object *
97 st_texture_object(struct gl_texture_object *obj)
98 {
99    return (struct st_texture_object *) obj;
100 }
101
102
103 static INLINE struct pipe_texture *
104 st_get_texobj_texture(struct gl_texture_object *texObj)
105 {
106    struct st_texture_object *stObj = st_texture_object(texObj);
107    return stObj ? stObj->pt : NULL;
108 }
109
110
111 static INLINE struct pipe_texture *
112 st_get_stobj_texture(struct st_texture_object *stObj)
113 {
114    return stObj ? stObj->pt : NULL;
115 }
116
117
118 static INLINE struct pipe_sampler_view *
119 st_sampler_view_from_texture(struct pipe_context *pipe,
120                              struct pipe_texture *texture)
121 {
122    struct pipe_sampler_view templ;
123
124    u_sampler_view_default_template(&templ,
125                                    texture,
126                                    texture->format);
127
128    return pipe->create_sampler_view(pipe, texture, &templ);
129 }
130
131
132 static INLINE struct pipe_sampler_view *
133 st_get_stobj_sampler_view(struct st_texture_object *stObj)
134 {
135    if (!stObj || !stObj->pt) {
136       return NULL;
137    }
138
139    if (!stObj->sampler_view) {
140       stObj->sampler_view = st_sampler_view_from_texture(stObj->pipe, stObj->pt);
141    }
142
143    return stObj->sampler_view;
144 }
145
146
147 extern struct pipe_texture *
148 st_texture_create(struct st_context *st,
149                   enum pipe_texture_target target,
150                   enum pipe_format format,
151                   GLuint last_level,
152                   GLuint width0,
153                   GLuint height0,
154                   GLuint depth0,
155                   GLuint tex_usage );
156
157
158 /* Check if an image fits into an existing texture object.
159  */
160 extern GLboolean
161 st_texture_match_image(const struct pipe_texture *pt,
162                        const struct gl_texture_image *image,
163                        GLuint face, GLuint level);
164
165 /* Return a pointer to an image within a texture.  Return image stride as
166  * well.
167  */
168 extern GLubyte *
169 st_texture_image_map(struct st_context *st,
170                      struct st_texture_image *stImage,
171                      GLuint zoffset,
172                      enum pipe_transfer_usage usage,
173                      unsigned x, unsigned y,
174                      unsigned w, unsigned h);
175
176 extern void
177 st_texture_image_unmap(struct st_context *st,
178                        struct st_texture_image *stImage);
179
180
181 /* Return pointers to each 2d slice within an image.  Indexed by depth
182  * value.
183  */
184 extern const GLuint *
185 st_texture_depth_offsets(struct pipe_texture *pt, GLuint level);
186
187
188 /* Return the linear offset of an image relative to the start of its region.
189  */
190 extern GLuint
191 st_texture_image_offset(const struct pipe_texture *pt,
192                         GLuint face, GLuint level);
193
194 extern GLuint
195 st_texture_texel_offset(const struct pipe_texture * pt,
196                         GLuint face, GLuint level,
197                         GLuint col, GLuint row, GLuint img);
198
199
200 /* Upload an image into a texture
201  */
202 extern void
203 st_texture_image_data(struct st_context *st,
204                       struct pipe_texture *dst,
205                       GLuint face, GLuint level, void *src,
206                       GLuint src_row_pitch, GLuint src_image_pitch);
207
208
209 /* Copy an image between two textures
210  */
211 extern void
212 st_texture_image_copy(struct pipe_context *pipe,
213                       struct pipe_texture *dst, GLuint dstLevel,
214                       struct pipe_texture *src,
215                       GLuint face);
216
217 extern void
218 st_teximage_flush_before_map(struct st_context *st,
219                              struct pipe_texture *pt,
220                              unsigned int face,
221                              unsigned int level,
222                              enum pipe_transfer_usage usage);
223 #endif