svga: rewrite svga_buffer() cast wrapper
[mesa.git] / src / gallium / drivers / svga / svga_resource_buffer.h
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 * SVGA pipe buffer.
63 */
64 struct svga_buffer
65 {
66 struct u_resource b;
67
68 /** This is a superset of b.b.bind */
69 unsigned bind_flags;
70
71 /**
72 * Regular (non DMA'able) memory.
73 *
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.
76 */
77 void *swbuf;
78
79 /**
80 * Whether swbuf was created by the user or not.
81 */
82 boolean user;
83
84 /**
85 * Creation key for the host surface handle.
86 *
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
89 * operation.
90 */
91 struct svga_host_surface_cache_key key;
92
93 /**
94 * Host surface handle.
95 *
96 * This is a platform independent abstraction for host SID. We create when
97 * trying to bind.
98 *
99 * Only set for non-user buffers.
100 */
101 struct svga_winsys_surface *handle;
102
103 /**
104 * Information about ongoing and past map operations.
105 */
106 struct {
107 /**
108 * Number of concurrent mappings.
109 */
110 unsigned count;
111
112 /**
113 * Dirty ranges.
114 *
115 * Ranges that were touched by the application and need to be uploaded to
116 * the host.
117 *
118 * This information will be copied into dma.boxes, when emiting the
119 * SVGA3dCmdSurfaceDMA command.
120 */
121 struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES];
122 unsigned num_ranges;
123 } map;
124
125 /**
126 * Information about uploaded version of user buffers.
127 */
128 struct {
129 struct pipe_resource *buffer;
130
131 /**
132 * We combine multiple user buffers into the same hardware buffer. This
133 * is the relative offset within that buffer.
134 */
135 unsigned offset;
136
137 /**
138 * Range of user buffer that is uploaded in @buffer at @offset.
139 */
140 unsigned start;
141 unsigned end;
142 } uploaded;
143
144 /**
145 * DMA'ble memory.
146 *
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
149 * host.
150 *
151 * Only set for non-user buffers.
152 */
153 struct svga_winsys_buffer *hwbuf;
154
155 /**
156 * Information about pending DMA uploads.
157 *
158 */
159 struct {
160 /**
161 * Whether this buffer has an unfinished DMA upload command.
162 *
163 * If not set then the rest of the information is null.
164 */
165 boolean pending;
166
167 SVGA3dSurfaceDMAFlags flags;
168
169 /**
170 * Pointer to the DMA copy box *inside* the command buffer.
171 */
172 SVGA3dCopyBox *boxes;
173
174 /**
175 * Pointer to the sequence of update commands
176 * *inside* the command buffer.
177 */
178 struct svga_3d_update_gb_image *updates;
179
180 /**
181 * Context that has the pending DMA to this buffer.
182 */
183 struct svga_context *svga;
184 } dma;
185
186 /**
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.
189 */
190 struct list_head head;
191
192 unsigned size; /**< Approximate size in bytes */
193
194 boolean dirty; /**< Need to do a readback before mapping? */
195 };
196
197
198 static inline struct svga_buffer *
199 svga_buffer(struct pipe_resource *resource)
200 {
201 struct svga_buffer *buf = (struct svga_buffer *) resource;
202 assert(buf == NULL || buf->b.vtbl == &svga_buffer_vtbl);
203 return buf;
204 }
205
206
207 /**
208 * Returns TRUE for user buffers. We may
209 * decide to use an alternate upload path for these buffers.
210 */
211 static inline boolean
212 svga_buffer_is_user_buffer( struct pipe_resource *buffer )
213 {
214 if (buffer) {
215 return svga_buffer(buffer)->user;
216 } else {
217 return FALSE;
218 }
219 }
220
221 /**
222 * Returns a pointer to a struct svga_winsys_screen given a
223 * struct svga_buffer.
224 */
225 static inline struct svga_winsys_screen *
226 svga_buffer_winsys_screen(struct svga_buffer *sbuf)
227 {
228 return svga_screen(sbuf->b.b.screen)->sws;
229 }
230
231
232 /**
233 * Returns whether a buffer has hardware storage that is
234 * visible to the GPU.
235 */
236 static inline boolean
237 svga_buffer_has_hw_storage(struct svga_buffer *sbuf)
238 {
239 if (svga_buffer_winsys_screen(sbuf)->have_gb_objects)
240 return (sbuf->handle ? TRUE : FALSE);
241 else
242 return (sbuf->hwbuf ? TRUE : FALSE);
243 }
244
245 /**
246 * Map the hardware storage of a buffer.
247 */
248 static inline void *
249 svga_buffer_hw_storage_map(struct svga_context *svga,
250 struct svga_buffer *sbuf,
251 unsigned flags, boolean *retry)
252 {
253 struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);
254
255 svga->hud.num_resources_mapped++;
256
257 if (sws->have_gb_objects) {
258 return svga->swc->surface_map(svga->swc, sbuf->handle, flags, retry);
259 } else {
260 *retry = FALSE;
261 return sws->buffer_map(sws, sbuf->hwbuf, flags);
262 }
263 }
264
265 /**
266 * Unmap the hardware storage of a buffer.
267 */
268 static inline void
269 svga_buffer_hw_storage_unmap(struct svga_context *svga,
270 struct svga_buffer *sbuf)
271 {
272 struct svga_winsys_screen *sws = svga_buffer_winsys_screen(sbuf);
273
274 if (sws->have_gb_objects) {
275 struct svga_winsys_context *swc = svga->swc;
276 boolean rebind;
277 swc->surface_unmap(swc, sbuf->handle, &rebind);
278 if (rebind) {
279 enum pipe_error ret;
280 ret = SVGA3D_BindGBSurface(swc, sbuf->handle);
281 if (ret != PIPE_OK) {
282 /* flush and retry */
283 svga_context_flush(svga, NULL);
284 ret = SVGA3D_BindGBSurface(swc, sbuf->handle);
285 assert(ret == PIPE_OK);
286 }
287 }
288 } else
289 sws->buffer_unmap(sws, sbuf->hwbuf);
290 }
291
292
293 struct pipe_resource *
294 svga_user_buffer_create(struct pipe_screen *screen,
295 void *ptr,
296 unsigned bytes,
297 unsigned usage);
298
299 struct pipe_resource *
300 svga_buffer_create(struct pipe_screen *screen,
301 const struct pipe_resource *template);
302
303
304
305 /**
306 * Get the host surface handle for this buffer.
307 *
308 * This will ensure the host surface is updated, issuing DMAs as needed.
309 *
310 * NOTE: This may insert new commands in the context, so it *must* be called
311 * before reserving command buffer space. And, in order to insert commands
312 * it may need to call svga_context_flush().
313 */
314 struct svga_winsys_surface *
315 svga_buffer_handle(struct svga_context *svga,
316 struct pipe_resource *buf);
317
318 void
319 svga_context_flush_buffers(struct svga_context *svga);
320
321 struct svga_winsys_buffer *
322 svga_winsys_buffer_create(struct svga_context *svga,
323 unsigned alignment,
324 unsigned usage,
325 unsigned size);
326
327 #endif /* SVGA_BUFFER_H */