1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_state.h"
32 #include "util/u_transfer.h"
34 #include "svga_screen_cache.h"
35 #include "svga_screen.h"
37 #include "svga_context.h"
41 * Maximum number of discontiguous ranges
43 #define SVGA_BUFFER_MAX_RANGES 32
47 struct svga_winsys_buffer
;
48 struct svga_winsys_surface
;
51 extern struct u_resource_vtbl svga_buffer_vtbl
;
53 struct svga_buffer_range
59 struct svga_3d_update_gb_image
;
68 /** This is a superset of b.b.bind */
72 * Regular (non DMA'able) memory.
74 * Used for user buffers or for buffers which we know before hand that can
75 * never be used by the virtual hardware directly, such as constant buffers.
80 * Whether swbuf was created by the user or not.
85 * Creation key for the host surface handle.
87 * This structure describes all the host surface characteristics so that it
88 * can be looked up in cache, since creating a host surface is often a slow
91 struct svga_host_surface_cache_key key
;
94 * Host surface handle.
96 * This is a platform independent abstraction for host SID. We create when
99 * Only set for non-user buffers.
101 struct svga_winsys_surface
*handle
;
104 * Information about ongoing and past map operations.
108 * Number of concurrent mappings.
115 * Ranges that were touched by the application and need to be uploaded to
118 * This information will be copied into dma.boxes, when emiting the
119 * SVGA3dCmdSurfaceDMA command.
121 struct svga_buffer_range ranges
[SVGA_BUFFER_MAX_RANGES
];
126 * Information about uploaded version of user buffers.
129 struct pipe_resource
*buffer
;
132 * We combine multiple user buffers into the same hardware buffer. This
133 * is the relative offset within that buffer.
138 * Range of user buffer that is uploaded in @buffer at @offset.
147 * A piece of GMR memory, with the same size of the buffer. It is created
148 * when mapping the buffer, and will be used to upload vertex data to the
151 * Only set for non-user buffers.
153 struct svga_winsys_buffer
*hwbuf
;
156 * Information about pending DMA uploads.
161 * Whether this buffer has an unfinished DMA upload command.
163 * If not set then the rest of the information is null.
167 SVGA3dSurfaceDMAFlags flags
;
170 * Pointer to the DMA copy box *inside* the command buffer.
172 SVGA3dCopyBox
*boxes
;
175 * Pointer to the sequence of update commands
176 * *inside* the command buffer.
178 struct svga_3d_update_gb_image
*updates
;
181 * Context that has the pending DMA to this buffer.
183 struct svga_context
*svga
;
187 * Linked list head, used to gather all buffers with pending dma uploads on
188 * a context. It is only valid if the dma.pending is set above.
190 struct list_head head
;
192 unsigned size
; /**< Approximate size in bytes */
194 boolean dirty
; /**< Need to do a readback before mapping? */
198 static inline struct svga_buffer
*
199 svga_buffer(struct pipe_resource
*buffer
)
202 assert(((struct svga_buffer
*)buffer
)->b
.vtbl
== &svga_buffer_vtbl
);
203 return (struct svga_buffer
*)buffer
;
210 * Returns TRUE for user buffers. We may
211 * decide to use an alternate upload path for these buffers.
213 static inline boolean
214 svga_buffer_is_user_buffer( struct pipe_resource
*buffer
)
217 return svga_buffer(buffer
)->user
;
224 * Returns a pointer to a struct svga_winsys_screen given a
225 * struct svga_buffer.
227 static inline struct svga_winsys_screen
*
228 svga_buffer_winsys_screen(struct svga_buffer
*sbuf
)
230 return svga_screen(sbuf
->b
.b
.screen
)->sws
;
235 * Returns whether a buffer has hardware storage that is
236 * visible to the GPU.
238 static inline boolean
239 svga_buffer_has_hw_storage(struct svga_buffer
*sbuf
)
241 if (svga_buffer_winsys_screen(sbuf
)->have_gb_objects
)
242 return (sbuf
->handle
? TRUE
: FALSE
);
244 return (sbuf
->hwbuf
? TRUE
: FALSE
);
248 * Map the hardware storage of a buffer.
251 svga_buffer_hw_storage_map(struct svga_context
*svga
,
252 struct svga_buffer
*sbuf
,
253 unsigned flags
, boolean
*retry
)
255 struct svga_winsys_screen
*sws
= svga_buffer_winsys_screen(sbuf
);
257 svga
->hud
.num_resources_mapped
++;
259 if (sws
->have_gb_objects
) {
260 return svga
->swc
->surface_map(svga
->swc
, sbuf
->handle
, flags
, retry
);
263 return sws
->buffer_map(sws
, sbuf
->hwbuf
, flags
);
268 * Unmap the hardware storage of a buffer.
271 svga_buffer_hw_storage_unmap(struct svga_context
*svga
,
272 struct svga_buffer
*sbuf
)
274 struct svga_winsys_screen
*sws
= svga_buffer_winsys_screen(sbuf
);
276 if (sws
->have_gb_objects
) {
277 struct svga_winsys_context
*swc
= svga
->swc
;
279 swc
->surface_unmap(swc
, sbuf
->handle
, &rebind
);
282 ret
= SVGA3D_BindGBSurface(swc
, sbuf
->handle
);
283 if (ret
!= PIPE_OK
) {
284 /* flush and retry */
285 svga_context_flush(svga
, NULL
);
286 ret
= SVGA3D_BindGBSurface(swc
, sbuf
->handle
);
287 assert(ret
== PIPE_OK
);
291 sws
->buffer_unmap(sws
, sbuf
->hwbuf
);
295 struct pipe_resource
*
296 svga_user_buffer_create(struct pipe_screen
*screen
,
301 struct pipe_resource
*
302 svga_buffer_create(struct pipe_screen
*screen
,
303 const struct pipe_resource
*template);
308 * Get the host surface handle for this buffer.
310 * This will ensure the host surface is updated, issuing DMAs as needed.
312 * NOTE: This may insert new commands in the context, so it *must* be called
313 * before reserving command buffer space. And, in order to insert commands
314 * it may need to call svga_context_flush().
316 struct svga_winsys_surface
*
317 svga_buffer_handle(struct svga_context
*svga
,
318 struct pipe_resource
*buf
);
321 svga_context_flush_buffers(struct svga_context
*svga
);
323 struct svga_winsys_buffer
*
324 svga_winsys_buffer_create(struct svga_context
*svga
,
329 #endif /* SVGA_BUFFER_H */