1 /**********************************************************
2 * Copyright 2008-2023 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 "util/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 #include "svga_context.h"
37
38 struct pipe_context;
39 struct pipe_screen;
40 struct svga_context;
41 struct svga_winsys_surface;
42 enum SVGA3dSurfaceFormat;
43
44
45 #define SVGA_MAX_TEXTURE_LEVELS 16
46
47 struct svga_texture
48 {
49 struct pipe_resource b;
50
51 uint16_t *defined;
52
53 struct svga_sampler_view *cached_view;
54
55 unsigned view_age[SVGA_MAX_TEXTURE_LEVELS];
56 unsigned age;
57
58 bool views_modified;
59
60 /**
61 * Creation key for the host surface handle.
62 *
63 * This structure describes all the host surface characteristics so that it
64 * can be looked up in cache, since creating a host surface is often a slow
65 * operation.
66 */
67 struct svga_host_surface_cache_key key;
68
69 /**
70 * Handle for the host side surface.
71 *
72 * This handle is owned by this texture. Views should hold on to a reference
73 * to this texture and never destroy this handle directly.
74 */
75 struct svga_winsys_surface *handle;
76
77 /**
78 * Whether the host side surface is imported and not created by this
79 * driver.
80 */
81 bool imported;
82
83 /**
84 * Whether texture upload buffer can be used on this texture
85 */
86 bool can_use_upload;
87
88 /**
89 * Whether texture is modified. Set if any of the dirty bits is set.
90 */
91 bool modified;
92
93 unsigned size; /**< Approximate size in bytes */
94
95 /** array indexed by cube face or 3D/array slice, one bit per mipmap level */
96 uint16_t *rendered_to;
97
98 /** array indexed by cube face or 3D/array slice, one bit per mipmap level.
99 * Set if the level is marked as dirty.
100 */
101 uint16_t *dirty;
102
103 enum svga_surface_state surface_state;
104
105 /**
106 * A cached backing host side surface to be used if this texture is being
107 * used for rendering and sampling at the same time.
108 * Currently we only cache one handle. If needed, we can extend this to
109 * support multiple handles.
110 */
111 struct svga_host_surface_cache_key backed_key;
112 struct svga_winsys_surface *backed_handle;
113 unsigned backed_age;
114 };
115
116
117
118 /* Note this is only used for texture (not buffer) transfers:
119 */
120 struct svga_transfer
121 {
122 struct pipe_transfer base;
123
124 unsigned slice; /**< array slice or cube face */
125 SVGA3dBox box; /* The adjusted box with slice index removed from z */
126
127 struct svga_winsys_buffer *hwbuf;
128
129 /* Height of the hardware buffer in pixel blocks */
130 unsigned hw_nblocksy;
131
132 /* Temporary malloc buffer when we can't allocate a hardware buffer
133 * big enough */
134 void *swbuf;
135
136 /* True if guest backed surface is supported and we can directly map
137 * to the surface for this transfer.
138 */
139 bool use_direct_map;
140
141 struct {
142 struct pipe_resource *buf; /* points to the upload buffer if this
143 * transfer is done via the upload buffer
144 * instead of directly mapping to the
145 * resource's surface.
146 */
147 void *map;
148 unsigned offset;
149 SVGA3dBox box;
150 unsigned nlayers;
151 } upload;
152 };
153
154
155 static inline struct svga_texture *
svga_texture(struct pipe_resource * resource)156 svga_texture(struct pipe_resource *resource)
157 {
158 struct svga_texture *tex = (struct svga_texture *)resource;
159 assert(tex == NULL || tex->b.target != PIPE_BUFFER);
160 return tex;
161 }
162
163
164 static inline struct svga_transfer *
svga_transfer(struct pipe_transfer * transfer)165 svga_transfer(struct pipe_transfer *transfer)
166 {
167 assert(transfer);
168 return (struct svga_transfer *)transfer;
169 }
170
171
172 /**
173 * Increment the age of a view into a texture
174 * This is used to track updates to textures when we draw into
175 * them via a surface.
176 */
177 static inline void
svga_age_texture_view(struct svga_texture * tex,unsigned level)178 svga_age_texture_view(struct svga_texture *tex, unsigned level)
179 {
180 assert(level < ARRAY_SIZE(tex->view_age));
181 tex->view_age[level] = ++(tex->age);
182 }
183
184
185 /** For debugging, check that face and level are legal */
186 static inline void
check_face_level(const struct svga_texture * tex,unsigned face,unsigned level)187 check_face_level(const struct svga_texture *tex,
188 unsigned face, unsigned level)
189 {
190 if (tex->b.target == PIPE_TEXTURE_CUBE) {
191 assert(face < 6);
192 }
193 else if (tex->b.target == PIPE_TEXTURE_3D) {
194 assert(face < tex->b.depth0);
195 }
196 else {
197 assert(face < tex->b.array_size);
198 }
199
200 assert(level < 8 * sizeof(tex->rendered_to[0]));
201 }
202
203
204 /**
205 * Mark the given texture face/level as being defined.
206 */
207 static inline void
svga_define_texture_level(struct svga_texture * tex,unsigned face,unsigned level)208 svga_define_texture_level(struct svga_texture *tex,
209 unsigned face,unsigned level)
210 {
211 check_face_level(tex, face, level);
212 tex->defined[face] |= 1 << level;
213 }
214
215
216 static inline bool
svga_is_texture_level_defined(const struct svga_texture * tex,unsigned face,unsigned level)217 svga_is_texture_level_defined(const struct svga_texture *tex,
218 unsigned face, unsigned level)
219 {
220 check_face_level(tex, face, level);
221 return (tex->defined[face] & (1 << level)) != 0;
222 }
223
224
225 static inline void
svga_set_texture_rendered_to(struct svga_texture * tex)226 svga_set_texture_rendered_to(struct svga_texture *tex)
227 {
228 tex->surface_state = SVGA_SURFACE_STATE_RENDERED;
229 }
230
231
232 static inline void
svga_clear_texture_rendered_to(struct svga_texture * tex)233 svga_clear_texture_rendered_to(struct svga_texture *tex)
234 {
235 tex->surface_state = SVGA_SURFACE_STATE_UPDATED;
236 }
237
238 static inline bool
svga_was_texture_rendered_to(const struct svga_texture * tex)239 svga_was_texture_rendered_to(const struct svga_texture *tex)
240 {
241 return (tex->surface_state == SVGA_SURFACE_STATE_RENDERED);
242 }
243
244 static inline void
svga_set_texture_dirty(struct svga_texture * tex,unsigned face,unsigned level)245 svga_set_texture_dirty(struct svga_texture *tex,
246 unsigned face, unsigned level)
247 {
248 check_face_level(tex, face, level);
249 tex->dirty[face] |= 1 << level;
250 tex->modified = true;
251 }
252
253 static inline void
svga_clear_texture_dirty(struct svga_texture * tex)254 svga_clear_texture_dirty(struct svga_texture *tex)
255 {
256 unsigned i;
257 for (i = 0; i < tex->b.depth0 * tex->b.array_size; i++) {
258 tex->dirty[i] = 0;
259 }
260 tex->modified = false;
261 }
262
263 static inline bool
svga_is_texture_level_dirty(const struct svga_texture * tex,unsigned face,unsigned level)264 svga_is_texture_level_dirty(const struct svga_texture *tex,
265 unsigned face, unsigned level)
266 {
267 check_face_level(tex, face, level);
268 return !!(tex->dirty[face] & (1 << level));
269 }
270
271 static inline bool
svga_is_texture_dirty(const struct svga_texture * tex)272 svga_is_texture_dirty(const struct svga_texture *tex)
273 {
274 return tex->modified;
275 }
276
277 struct pipe_resource *
278 svga_texture_create(struct pipe_screen *screen,
279 const struct pipe_resource *template);
280
281 bool
282 svga_resource_get_handle(struct pipe_screen *screen,
283 struct pipe_context *context,
284 struct pipe_resource *texture,
285 struct winsys_handle *whandle,
286 unsigned usage);
287
288 struct pipe_resource *
289 svga_texture_from_handle(struct pipe_screen * screen,
290 const struct pipe_resource *template,
291 struct winsys_handle *whandle);
292
293 bool
294 svga_texture_generate_mipmap(struct pipe_context *pipe,
295 struct pipe_resource *pt,
296 enum pipe_format format,
297 unsigned base_level,
298 unsigned last_level,
299 unsigned first_layer,
300 unsigned last_layer);
301
302 bool
303 svga_texture_transfer_map_upload_create(struct svga_context *svga);
304
305 void
306 svga_texture_transfer_map_upload_destroy(struct svga_context *svga);
307
308 bool
309 svga_texture_transfer_map_can_upload(const struct svga_screen *svgascreen,
310 const struct pipe_resource *pt);
311
312 void *
313 svga_texture_transfer_map_upload(struct svga_context *svga,
314 struct svga_transfer *st);
315
316 void
317 svga_texture_transfer_unmap_upload(struct svga_context *svga,
318 struct svga_transfer *st);
319
320 bool
321 svga_texture_device_format_has_alpha(struct pipe_resource *texture);
322
323 void *
324 svga_texture_transfer_map(struct pipe_context *pipe,
325 struct pipe_resource *texture,
326 unsigned level,
327 unsigned usage,
328 const struct pipe_box *box,
329 struct pipe_transfer **ptransfer);
330
331 void
332 svga_texture_transfer_unmap(struct pipe_context *pipe,
333 struct pipe_transfer *transfer);
334
335 #endif /* SVGA_TEXTURE_H */
336