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