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