gallium/svga: Only upload parts of vertexarrays that are actually used
[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
38
39 /**
40 * Maximum number of discontiguous ranges
41 */
42 #define SVGA_BUFFER_MAX_RANGES 32
43
44
45 struct svga_screen;
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
60 /**
61 * SVGA pipe buffer.
62 */
63 struct svga_buffer
64 {
65 struct u_resource b;
66
67 /**
68 * Regular (non DMA'able) memory.
69 *
70 * Used for user buffers or for buffers which we know before hand that can
71 * never be used by the virtual hardware directly, such as constant buffers.
72 */
73 void *swbuf;
74
75 /**
76 * Whether swbuf was created by the user or not.
77 */
78 boolean user;
79
80 /**
81 * Creation key for the host surface handle.
82 *
83 * This structure describes all the host surface characteristics so that it
84 * can be looked up in cache, since creating a host surface is often a slow
85 * operation.
86 */
87 struct svga_host_surface_cache_key key;
88
89 /**
90 * Host surface handle.
91 *
92 * This is a platform independent abstraction for host SID. We create when
93 * trying to bind
94 */
95 struct svga_winsys_surface *handle;
96
97 /**
98 * Information about ongoing and past map operations.
99 */
100 struct {
101 /**
102 * Number of concurrent mappings.
103 *
104 * XXX: It is impossible to guarantee concurrent maps work in all
105 * circumstances -- pipe_buffers really need transfer objects too.
106 */
107 unsigned count;
108
109 /**
110 * Whether this buffer is currently mapped for writing.
111 */
112 boolean writing;
113
114 /**
115 * Whether the application will tell us explicity which ranges it touched
116 * or not.
117 */
118 boolean flush_explicit;
119
120 /**
121 * Dirty ranges.
122 *
123 * Ranges that were touched by the application and need to be uploaded to
124 * the host.
125 *
126 * This information will be copied into dma.boxes, when emiting the
127 * SVGA3dCmdSurfaceDMA command.
128 */
129 struct svga_buffer_range ranges[SVGA_BUFFER_MAX_RANGES];
130 unsigned num_ranges;
131 } map;
132
133 /**
134 * Information about uploaded version of user buffers.
135 */
136 struct {
137 struct pipe_resource *buffer;
138
139 /**
140 * We combine multiple user buffers into the same hardware buffer. This
141 * is the relative offset within that buffer.
142 */
143 unsigned offset;
144 } uploaded;
145
146 /**
147 * The offset in the source user buffer that matches the
148 * uploaded offset
149 */
150 unsigned source_offset;
151
152 /**
153 * DMA'ble memory.
154 *
155 * A piece of GMR memory, with the same size of the buffer. It is created
156 * when mapping the buffer, and will be used to upload vertex data to the
157 * host.
158 */
159 struct svga_winsys_buffer *hwbuf;
160
161 /**
162 * Information about pending DMA uploads.
163 *
164 */
165 struct {
166 /**
167 * Whether this buffer has an unfinished DMA upload command.
168 *
169 * If not set then the rest of the information is null.
170 */
171 boolean pending;
172
173 SVGA3dSurfaceDMAFlags flags;
174
175 /**
176 * Pointer to the DMA copy box *inside* the command buffer.
177 */
178 SVGA3dCopyBox *boxes;
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
193
194 static INLINE struct svga_buffer *
195 svga_buffer(struct pipe_resource *buffer)
196 {
197 if (buffer) {
198 assert(((struct svga_buffer *)buffer)->b.vtbl == &svga_buffer_vtbl);
199 return (struct svga_buffer *)buffer;
200 }
201 return NULL;
202 }
203
204
205 /**
206 * Returns TRUE for user buffers. We may
207 * decide to use an alternate upload path for these buffers.
208 */
209 static INLINE boolean
210 svga_buffer_is_user_buffer( struct pipe_resource *buffer )
211 {
212 return svga_buffer(buffer)->user;
213 }
214
215
216
217
218 struct pipe_resource *
219 svga_user_buffer_create(struct pipe_screen *screen,
220 void *ptr,
221 unsigned bytes,
222 unsigned usage);
223
224 struct pipe_resource *
225 svga_buffer_create(struct pipe_screen *screen,
226 const struct pipe_resource *template);
227
228
229
230 /**
231 * Get the host surface handle for this buffer.
232 *
233 * This will ensure the host surface is updated, issuing DMAs as needed.
234 *
235 * NOTE: This may insert new commands in the context, so it *must* be called
236 * before reserving command buffer space. And, in order to insert commands
237 * it may need to call svga_context_flush().
238 */
239 struct svga_winsys_surface *
240 svga_buffer_handle(struct svga_context *svga,
241 struct pipe_resource *buf);
242
243 void
244 svga_context_flush_buffers(struct svga_context *svga);
245
246 struct svga_winsys_buffer *
247 svga_winsys_buffer_create(struct svga_context *svga,
248 unsigned alignment,
249 unsigned usage,
250 unsigned size);
251
252 void
253 svga_redefine_user_buffer(struct pipe_context *ctx,
254 struct pipe_resource *resource,
255 unsigned offset,
256 unsigned size);
257
258 #endif /* SVGA_BUFFER_H */