ffd5feac9a326e22a8242049ff1aa938ad5b7479
[mesa.git] / src / gallium / drivers / svga / svga_resource_texture.h
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #ifndef SVGA_TEXTURE_H
27 #define SVGA_TEXTURE_H
28
29
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_state.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_transfer.h"
35 #include "svga_screen_cache.h"
36
37 struct pipe_context;
38 struct pipe_screen;
39 struct svga_context;
40 struct svga_winsys_surface;
41 enum SVGA3dSurfaceFormat;
42
43
44 #define SVGA_MAX_TEXTURE_LEVELS 16
45
46
47 extern struct u_resource_vtbl svga_texture_vtbl;
48
49
50 struct svga_texture
51 {
52 struct u_resource b;
53
54 ushort *defined;
55
56 struct svga_sampler_view *cached_view;
57
58 unsigned view_age[SVGA_MAX_TEXTURE_LEVELS];
59 unsigned age;
60
61 boolean views_modified;
62
63 /**
64 * Creation key for the host surface handle.
65 *
66 * This structure describes all the host surface characteristics so that it
67 * can be looked up in cache, since creating a host surface is often a slow
68 * operation.
69 */
70 struct svga_host_surface_cache_key key;
71
72 /**
73 * Handle for the host side surface.
74 *
75 * This handle is owned by this texture. Views should hold on to a reference
76 * to this texture and never destroy this handle directly.
77 */
78 struct svga_winsys_surface *handle;
79
80 /**
81 * Whether the host side surface is imported and not created by this
82 * driver.
83 */
84 boolean imported;
85
86 unsigned size; /**< Approximate size in bytes */
87
88 /** array indexed by cube face or 3D/array slice, one bit per mipmap level */
89 ushort *rendered_to;
90
91 /** array indexed by cube face or 3D/array slice, one bit per mipmap level.
92 * Set if the level is marked as dirty.
93 */
94 ushort *dirty;
95 };
96
97
98
99 /* Note this is only used for texture (not buffer) transfers:
100 */
101 struct svga_transfer
102 {
103 struct pipe_transfer base;
104
105 unsigned slice; /**< array slice or cube face */
106
107 struct svga_winsys_buffer *hwbuf;
108
109 /* Height of the hardware buffer in pixel blocks */
110 unsigned hw_nblocksy;
111
112 /* Temporary malloc buffer when we can't allocate a hardware buffer
113 * big enough */
114 void *swbuf;
115
116 boolean use_direct_map;
117 };
118
119
120 static inline struct svga_texture *
121 svga_texture(struct pipe_resource *resource)
122 {
123 struct svga_texture *tex = (struct svga_texture *)resource;
124 assert(tex == NULL || tex->b.vtbl == &svga_texture_vtbl);
125 return tex;
126 }
127
128
129 static inline struct svga_transfer *
130 svga_transfer(struct pipe_transfer *transfer)
131 {
132 assert(transfer);
133 return (struct svga_transfer *)transfer;
134 }
135
136
137 /**
138 * Increment the age of a view into a texture
139 * This is used to track updates to textures when we draw into
140 * them via a surface.
141 */
142 static inline void
143 svga_age_texture_view(struct svga_texture *tex, unsigned level)
144 {
145 assert(level < ARRAY_SIZE(tex->view_age));
146 tex->view_age[level] = ++(tex->age);
147 }
148
149
150 /** For debugging, check that face and level are legal */
151 static inline void
152 check_face_level(const struct svga_texture *tex,
153 unsigned face, unsigned level)
154 {
155 if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
156 assert(face < 6);
157 }
158 else if (tex->b.b.target == PIPE_TEXTURE_3D) {
159 assert(face < tex->b.b.depth0);
160 }
161 else {
162 assert(face < tex->b.b.array_size);
163 }
164
165 assert(level < 8 * sizeof(tex->rendered_to[0]));
166 }
167
168
169 /**
170 * Mark the given texture face/level as being defined.
171 */
172 static inline void
173 svga_define_texture_level(struct svga_texture *tex,
174 unsigned face,unsigned level)
175 {
176 check_face_level(tex, face, level);
177 tex->defined[face] |= 1 << level;
178 }
179
180
181 static inline bool
182 svga_is_texture_level_defined(const struct svga_texture *tex,
183 unsigned face, unsigned level)
184 {
185 check_face_level(tex, face, level);
186 return (tex->defined[face] & (1 << level)) != 0;
187 }
188
189
190 static inline void
191 svga_set_texture_rendered_to(struct svga_texture *tex,
192 unsigned face, unsigned level)
193 {
194 check_face_level(tex, face, level);
195 tex->rendered_to[face] |= 1 << level;
196 }
197
198
199 static inline void
200 svga_clear_texture_rendered_to(struct svga_texture *tex,
201 unsigned face, unsigned level)
202 {
203 check_face_level(tex, face, level);
204 tex->rendered_to[face] &= ~(1 << level);
205 }
206
207
208 static inline boolean
209 svga_was_texture_rendered_to(const struct svga_texture *tex,
210 unsigned face, unsigned level)
211 {
212 check_face_level(tex, face, level);
213 return !!(tex->rendered_to[face] & (1 << level));
214 }
215
216 static inline void
217 svga_set_texture_dirty(struct svga_texture *tex,
218 unsigned face, unsigned level)
219 {
220 check_face_level(tex, face, level);
221 tex->dirty[face] |= 1 << level;
222 }
223
224 static inline void
225 svga_clear_texture_dirty(struct svga_texture *tex)
226 {
227 unsigned i;
228 for (i = 0; i < tex->b.b.depth0 * tex->b.b.array_size; i++) {
229 tex->dirty[i] = 0;
230 }
231 }
232
233 static inline boolean
234 svga_is_texture_dirty(const struct svga_texture *tex,
235 unsigned face, unsigned level)
236 {
237 check_face_level(tex, face, level);
238 return !!(tex->dirty[face] & (1 << level));
239 }
240
241 struct pipe_resource *
242 svga_texture_create(struct pipe_screen *screen,
243 const struct pipe_resource *template);
244
245 struct pipe_resource *
246 svga_texture_from_handle(struct pipe_screen * screen,
247 const struct pipe_resource *template,
248 struct winsys_handle *whandle);
249
250 boolean
251 svga_texture_generate_mipmap(struct pipe_context *pipe,
252 struct pipe_resource *pt,
253 enum pipe_format format,
254 unsigned base_level,
255 unsigned last_level,
256 unsigned first_layer,
257 unsigned last_layer);
258
259
260 #endif /* SVGA_TEXTURE_H */