• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BUFFER_H
27 #define SVGA_BUFFER_H
28 
29 
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_state.h"
32 #include "util/u_transfer.h"
33 
34 #include "svga_screen_cache.h"
35 #include "svga_screen.h"
36 #include "svga_cmd.h"
37 #include "svga_context.h"
38 
39 
40 /**
41  * Maximum number of discontiguous ranges
42  */
43 #define SVGA_BUFFER_MAX_RANGES 32
44 
45 
46 struct svga_context;
47 struct svga_winsys_buffer;
48 struct svga_winsys_surface;
49 
50 
51 extern struct u_resource_vtbl svga_buffer_vtbl;
52 
53 struct svga_buffer_range
54 {
55    unsigned start;
56    unsigned end;
57 };
58 
59 struct svga_3d_update_gb_image;
60 
61 /**
62  * This structure describes the bind flags and cache key associated
63  * with the host surface.
64  */
65 struct svga_buffer_surface
66 {
67    struct list_head list;
68    unsigned bind_flags;
69    struct svga_host_surface_cache_key key;
70    struct svga_winsys_surface *handle;
71 };
72 
73 /**
74  * SVGA pipe buffer.
75  */
76 struct svga_buffer
77 {
78    struct u_resource b;
79 
80    /** This is a superset of b.b.bind */
81    unsigned bind_flags;
82 
83    /**
84     * Regular (non DMA'able) memory.
85     *
86     * Used for user buffers or for buffers which we know before hand that can
87     * never be used by the virtual hardware directly, such as constant buffers.
88     */
89    void *swbuf;
90 
91    /**
92     * Whether swbuf was created by the user or not.
93     */
94    boolean user;
95 
96    /**
97     * Whether swbuf is used for this buffer.
98     */
99    boolean use_swbuf;
100 
101    /**
102     * Creation key for the host surface handle.
103     *
104     * This structure describes all the host surface characteristics so that it
105     * can be looked up in cache, since creating a host surface is often a slow
106     * operation.
107     */
108    struct svga_host_surface_cache_key key;
109 
110    /**
111     * Host surface handle.
112     *
113     * This is a platform independent abstraction for host SID. We create when
114     * trying to bind.
115     *
116     * Only set for non-user buffers.
117     */
118    struct svga_winsys_surface *handle;
119 
120    /**
121     * List of surfaces created for this buffer resource to support
122     * incompatible bind flags.
123     */
124    struct list_head surfaces;
125 
126    /**
127     * Information about ongoing and past map operations.
128     */
129    struct {
130       /**
131        * Number of concurrent mappings.
132        */
133       unsigned count;
134 
135       /**
136        * Dirty ranges.
137        *
138        * Ranges that were touched by the application and need to be uploaded to
139        * the host.
140        *
141        * This information will be copied into dma.boxes, when emiting the
142        * SVGA3dCmdSurfaceDMA command.
143        */
144       struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES];
145       unsigned num_ranges;
146    } map;
147 
148    /**
149     * Information about uploaded version of user buffers.
150     */
151    struct {
152       struct pipe_resource *buffer;
153 
154       /**
155        * We combine multiple user buffers into the same hardware buffer. This
156        * is the relative offset within that buffer.
157        */
158       unsigned offset;
159 
160       /**
161        * Range of user buffer that is uploaded in @buffer at @offset.
162        */
163       unsigned start;
164       unsigned end;
165    } uploaded;
166 
167    /**
168     * DMA'ble memory.
169     *
170     * A piece of GMR memory, with the same size of the buffer. It is created
171     * when mapping the buffer, and will be used to upload vertex data to the
172     * host.
173     *
174     * Only set for non-user buffers.
175     */
176    struct svga_winsys_buffer *hwbuf;
177 
178    /**
179     * Information about pending DMA uploads.
180     *
181     */
182    struct {
183       /**
184        * Whether this buffer has an unfinished DMA upload command.
185        *
186        * If not set then the rest of the information is null.
187        */
188       boolean pending;
189 
190       SVGA3dSurfaceDMAFlags flags;
191 
192       /**
193        * Pointer to the DMA copy box *inside* the command buffer.
194        */
195       SVGA3dCopyBox *boxes;
196 
197       /**
198        * Pointer to the sequence of update commands
199        * *inside* the command buffer.
200        */
201       struct svga_3d_update_gb_image *updates;
202 
203       /**
204        * Context that has the pending DMA to this buffer.
205        */
206       struct svga_context *svga;
207    } dma;
208 
209    /**
210     * Linked list head, used to gather all buffers with pending dma uploads on
211     * a context. It is only valid if the dma.pending is set above.
212     */
213    struct list_head head;
214 
215    unsigned size;  /**< Approximate size in bytes */
216 
217    boolean dirty;  /**< Need to do a readback before mapping? */
218 
219    /** In some cases we try to keep the results of the translate_indices()
220     * function from svga_draw_elements.c
221     */
222    struct {
223       enum pipe_prim_type orig_prim, new_prim;
224       struct pipe_resource *buffer;
225       unsigned index_size;
226       unsigned offset;  /**< first index */
227       unsigned count;   /**< num indices */
228    } translated_indices;
229 };
230 
231 
232 static inline struct svga_buffer *
svga_buffer(struct pipe_resource * resource)233 svga_buffer(struct pipe_resource *resource)
234 {
235    struct svga_buffer *buf = (struct svga_buffer *) resource;
236    assert(buf == NULL || buf->b.vtbl == &svga_buffer_vtbl);
237    return buf;
238 }
239 
240 
241 /**
242  * Returns TRUE for user buffers.  We may
243  * decide to use an alternate upload path for these buffers.
244  */
245 static inline boolean
svga_buffer_is_user_buffer(struct pipe_resource * buffer)246 svga_buffer_is_user_buffer(struct pipe_resource *buffer)
247 {
248    if (buffer) {
249       return svga_buffer(buffer)->user;
250    } else {
251       return FALSE;
252    }
253 }
254 
255 /**
256  * Returns a pointer to a struct svga_winsys_screen given a
257  * struct svga_buffer.
258  */
259 static inline struct svga_winsys_screen *
svga_buffer_winsys_screen(struct svga_buffer * sbuf)260 svga_buffer_winsys_screen(struct svga_buffer *sbuf)
261 {
262    return svga_screen(sbuf->b.b.screen)->sws;
263 }
264 
265 
266 /**
267  * Returns whether a buffer has hardware storage that is
268  * visible to the GPU.
269  */
270 static inline boolean
svga_buffer_has_hw_storage(struct svga_buffer * sbuf)271 svga_buffer_has_hw_storage(struct svga_buffer *sbuf)
272 {
273    if (svga_buffer_winsys_screen(sbuf)->have_gb_objects)
274       return (sbuf->handle ? TRUE : FALSE);
275    else
276       return (sbuf->hwbuf ? TRUE : FALSE);
277 }
278 
279 /**
280  * Map the hardware storage of a buffer.
281  * \param flags  bitmask of PIPE_MAP_* flags
282  */
283 static inline void *
svga_buffer_hw_storage_map(struct svga_context * svga,struct svga_buffer * sbuf,unsigned flags,boolean * retry)284 svga_buffer_hw_storage_map(struct svga_context *svga,
285                            struct svga_buffer *sbuf,
286                            unsigned flags, boolean *retry)
287 {
288    struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);
289 
290    svga->hud.num_buffers_mapped++;
291 
292    if (sws->have_gb_objects) {
293       struct svga_winsys_context *swc = svga->swc;
294       boolean rebind;
295       void *map;
296 
297       if (swc->force_coherent) {
298          flags |= PIPE_MAP_PERSISTENT | PIPE_MAP_COHERENT;
299       }
300       map = swc->surface_map(swc, sbuf->handle, flags, retry, &rebind);
301       if (map && rebind) {
302          enum pipe_error ret;
303 
304          ret = SVGA3D_BindGBSurface(swc, sbuf->handle);
305          if (ret != PIPE_OK) {
306             svga_context_flush(svga, NULL);
307             ret = SVGA3D_BindGBSurface(swc, sbuf->handle);
308             assert(ret == PIPE_OK);
309          }
310          svga_context_flush(svga, NULL);
311       }
312       return map;
313    } else {
314       *retry = FALSE;
315       return sws->buffer_map(sws, sbuf->hwbuf, flags);
316    }
317 }
318 
319 /**
320  * Unmap the hardware storage of a buffer.
321  */
322 static inline void
svga_buffer_hw_storage_unmap(struct svga_context * svga,struct svga_buffer * sbuf)323 svga_buffer_hw_storage_unmap(struct svga_context *svga,
324                              struct svga_buffer *sbuf)
325 {
326    struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);
327 
328    if (sws->have_gb_objects) {
329       struct svga_winsys_context *swc = svga->swc;
330       boolean rebind;
331 
332       swc->surface_unmap(swc, sbuf->handle, &rebind);
333       if (rebind) {
334          SVGA_RETRY(svga, SVGA3D_BindGBSurface(swc, sbuf->handle));
335       }
336    } else
337       sws->buffer_unmap(sws, sbuf->hwbuf);
338 }
339 
340 
341 struct pipe_resource *
342 svga_user_buffer_create(struct pipe_screen *screen,
343                         void *ptr,
344                         unsigned bytes,
345                         unsigned usage);
346 
347 struct pipe_resource *
348 svga_buffer_create(struct pipe_screen *screen,
349                    const struct pipe_resource *template);
350 
351 
352 
353 /**
354  * Get the host surface handle for this buffer.
355  *
356  * This will ensure the host surface is updated, issuing DMAs as needed.
357  *
358  * NOTE: This may insert new commands in the context, so it *must* be called
359  * before reserving command buffer space. And, in order to insert commands
360  * it may need to call svga_context_flush().
361  */
362 struct svga_winsys_surface *
363 svga_buffer_handle(struct svga_context *svga,
364                    struct pipe_resource *buf,
365                    unsigned tobind_flags);
366 
367 void
368 svga_context_flush_buffers(struct svga_context *svga);
369 
370 struct svga_winsys_buffer *
371 svga_winsys_buffer_create(struct svga_context *svga,
372                           unsigned alignment,
373                           unsigned usage,
374                           unsigned size);
375 
376 #endif /* SVGA_BUFFER_H */
377