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 validated, either through the
82 * InvalidateGBSurface command or after the surface is updated
83 * or rendered to.
84 */
85 boolean validated;
86
87 /**
88 * Whether the host side surface is imported and not created by this
89 * driver.
90 */
91 boolean imported;
92
93 /**
94 * Whether texture upload buffer can be used on this texture
95 */
96 boolean can_use_upload;
97
98 unsigned size; /**< Approximate size in bytes */
99
100 /** array indexed by cube face or 3D/array slice, one bit per mipmap level */
101 ushort *rendered_to;
102
103 /** array indexed by cube face or 3D/array slice, one bit per mipmap level.
104 * Set if the level is marked as dirty.
105 */
106 ushort *dirty;
107
108 /**
109 * A cached backing host side surface to be used if this texture is being
110 * used for rendering and sampling at the same time.
111 * Currently we only cache one handle. If needed, we can extend this to
112 * support multiple handles.
113 */
114 struct svga_host_surface_cache_key backed_key;
115 struct svga_winsys_surface *backed_handle;
116 unsigned backed_age;
117 };
118
119
120
121 /* Note this is only used for texture (not buffer) transfers:
122 */
123 struct svga_transfer
124 {
125 struct pipe_transfer base;
126
127 unsigned slice; /**< array slice or cube face */
128 SVGA3dBox box; /* The adjusted box with slice index removed from z */
129
130 struct svga_winsys_buffer *hwbuf;
131
132 /* Height of the hardware buffer in pixel blocks */
133 unsigned hw_nblocksy;
134
135 /* Temporary malloc buffer when we can't allocate a hardware buffer
136 * big enough */
137 void *swbuf;
138
139 /* True if guest backed surface is supported and we can directly map
140 * to the surface for this transfer.
141 */
142 boolean use_direct_map;
143
144 struct {
145 struct pipe_resource *buf; /* points to the upload buffer if this
146 * transfer is done via the upload buffer
147 * instead of directly mapping to the
148 * resource's surface.
149 */
150 void *map;
151 unsigned offset;
152 SVGA3dBox box;
153 unsigned nlayers;
154 } upload;
155 };
156
157
158 static inline struct svga_texture *
svga_texture(struct pipe_resource * resource)159 svga_texture(struct pipe_resource *resource)
160 {
161 struct svga_texture *tex = (struct svga_texture *)resource;
162 assert(tex == NULL || tex->b.vtbl == &svga_texture_vtbl);
163 return tex;
164 }
165
166
167 static inline struct svga_transfer *
svga_transfer(struct pipe_transfer * transfer)168 svga_transfer(struct pipe_transfer *transfer)
169 {
170 assert(transfer);
171 return (struct svga_transfer *)transfer;
172 }
173
174
175 /**
176 * Increment the age of a view into a texture
177 * This is used to track updates to textures when we draw into
178 * them via a surface.
179 */
180 static inline void
svga_age_texture_view(struct svga_texture * tex,unsigned level)181 svga_age_texture_view(struct svga_texture *tex, unsigned level)
182 {
183 assert(level < ARRAY_SIZE(tex->view_age));
184 tex->view_age[level] = ++(tex->age);
185 }
186
187
188 /** For debugging, check that face and level are legal */
189 static inline void
check_face_level(const struct svga_texture * tex,unsigned face,unsigned level)190 check_face_level(const struct svga_texture *tex,
191 unsigned face, unsigned level)
192 {
193 if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
194 assert(face < 6);
195 }
196 else if (tex->b.b.target == PIPE_TEXTURE_3D) {
197 assert(face < tex->b.b.depth0);
198 }
199 else {
200 assert(face < tex->b.b.array_size);
201 }
202
203 assert(level < 8 * sizeof(tex->rendered_to[0]));
204 }
205
206
207 /**
208 * Mark the given texture face/level as being defined.
209 */
210 static inline void
svga_define_texture_level(struct svga_texture * tex,unsigned face,unsigned level)211 svga_define_texture_level(struct svga_texture *tex,
212 unsigned face,unsigned level)
213 {
214 check_face_level(tex, face, level);
215 tex->defined[face] |= 1 << level;
216 tex->validated = TRUE;
217 }
218
219
220 static inline bool
svga_is_texture_level_defined(const struct svga_texture * tex,unsigned face,unsigned level)221 svga_is_texture_level_defined(const struct svga_texture *tex,
222 unsigned face, unsigned level)
223 {
224 check_face_level(tex, face, level);
225 return (tex->defined[face] & (1 << level)) != 0;
226 }
227
228
229 static inline void
svga_set_texture_rendered_to(struct svga_texture * tex,unsigned face,unsigned level)230 svga_set_texture_rendered_to(struct svga_texture *tex,
231 unsigned face, unsigned level)
232 {
233 check_face_level(tex, face, level);
234 tex->rendered_to[face] |= 1 << level;
235 tex->validated = TRUE;
236 }
237
238
239 static inline void
svga_clear_texture_rendered_to(struct svga_texture * tex,unsigned face,unsigned level)240 svga_clear_texture_rendered_to(struct svga_texture *tex,
241 unsigned face, unsigned level)
242 {
243 check_face_level(tex, face, level);
244 tex->rendered_to[face] &= ~(1 << level);
245 }
246
247
248 static inline boolean
svga_was_texture_rendered_to(const struct svga_texture * tex,unsigned face,unsigned level)249 svga_was_texture_rendered_to(const struct svga_texture *tex,
250 unsigned face, unsigned level)
251 {
252 check_face_level(tex, face, level);
253 return !!(tex->rendered_to[face] & (1 << level));
254 }
255
256 static inline void
svga_set_texture_dirty(struct svga_texture * tex,unsigned face,unsigned level)257 svga_set_texture_dirty(struct svga_texture *tex,
258 unsigned face, unsigned level)
259 {
260 check_face_level(tex, face, level);
261 tex->dirty[face] |= 1 << level;
262 }
263
264 static inline void
svga_clear_texture_dirty(struct svga_texture * tex)265 svga_clear_texture_dirty(struct svga_texture *tex)
266 {
267 unsigned i;
268 for (i = 0; i < tex->b.b.depth0 * tex->b.b.array_size; i++) {
269 tex->dirty[i] = 0;
270 }
271 }
272
273 static inline boolean
svga_is_texture_dirty(const struct svga_texture * tex,unsigned face,unsigned level)274 svga_is_texture_dirty(const struct svga_texture *tex,
275 unsigned face, unsigned level)
276 {
277 check_face_level(tex, face, level);
278 return !!(tex->dirty[face] & (1 << level));
279 }
280
281 struct pipe_resource *
282 svga_texture_create(struct pipe_screen *screen,
283 const struct pipe_resource *template);
284
285 struct pipe_resource *
286 svga_texture_from_handle(struct pipe_screen * screen,
287 const struct pipe_resource *template,
288 struct winsys_handle *whandle);
289
290 bool
291 svga_texture_generate_mipmap(struct pipe_context *pipe,
292 struct pipe_resource *pt,
293 enum pipe_format format,
294 unsigned base_level,
295 unsigned last_level,
296 unsigned first_layer,
297 unsigned last_layer);
298
299 boolean
300 svga_texture_transfer_map_upload_create(struct svga_context *svga);
301
302 void
303 svga_texture_transfer_map_upload_destroy(struct svga_context *svga);
304
305 boolean
306 svga_texture_transfer_map_can_upload(const struct svga_screen *svgascreen,
307 const struct pipe_resource *pt);
308
309 void *
310 svga_texture_transfer_map_upload(struct svga_context *svga,
311 struct svga_transfer *st);
312
313 void
314 svga_texture_transfer_unmap_upload(struct svga_context *svga,
315 struct svga_transfer *st);
316
317 boolean
318 svga_texture_device_format_has_alpha(struct pipe_resource *texture);
319
320 #endif /* SVGA_TEXTURE_H */
321