svga: add new helper functions for GBS buffers
[mesa.git] / src / gallium / drivers / svga / svga_winsys.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 /**
27 * @file
28 * VMware SVGA specific winsys interface.
29 *
30 * @author Jose Fonseca <jfonseca@vmware.com>
31 *
32 * Documentation taken from the VMware SVGA DDK.
33 */
34
35 #ifndef SVGA_WINSYS_H_
36 #define SVGA_WINSYS_H_
37
38
39 #include "svga_types.h"
40 #include "svga_reg.h"
41 #include "svga3d_reg.h"
42
43 #include "pipe/p_compiler.h"
44 #include "pipe/p_defines.h"
45
46
47 struct svga_winsys_screen;
48 struct svga_winsys_buffer;
49 struct pipe_screen;
50 struct pipe_context;
51 struct pipe_fence_handle;
52 struct pipe_resource;
53 struct svga_region;
54 struct winsys_handle;
55
56
57 #define SVGA_BUFFER_USAGE_PINNED (1 << 0)
58 #define SVGA_BUFFER_USAGE_WRAPPED (1 << 1)
59 #define SVGA_BUFFER_USAGE_SHADER (1 << 2)
60
61 /**
62 * Relocation flags to help with dirty tracking
63 * SVGA_RELOC_WRITE - The command will cause a GPU write to this
64 * resource.
65 * SVGA_RELOC_READ - The command will cause a GPU read from this
66 * resource.
67 * SVGA_RELOC_INTERNAL The command will only transfer data internally
68 * within the resource, and optionally clear
69 * dirty bits
70 * SVGA_RELOC_DMA - Only set for resource buffer DMA uploads for winsys
71 * implementations that want to track the amount
72 * of such data referenced in the command stream.
73 */
74 #define SVGA_RELOC_WRITE (1 << 0)
75 #define SVGA_RELOC_READ (1 << 1)
76 #define SVGA_RELOC_INTERNAL (1 << 2)
77 #define SVGA_RELOC_DMA (1 << 3)
78
79 #define SVGA_FENCE_FLAG_EXEC (1 << 0)
80 #define SVGA_FENCE_FLAG_QUERY (1 << 1)
81
82
83 /** Opaque surface handle */
84 struct svga_winsys_surface;
85
86
87 /** Opaque guest-backed objects */
88 struct svga_winsys_gb_shader;
89
90
91
92 /**
93 * SVGA per-context winsys interface.
94 */
95 struct svga_winsys_context
96 {
97 void
98 (*destroy)(struct svga_winsys_context *swc);
99
100 void *
101 (*reserve)(struct svga_winsys_context *swc,
102 uint32_t nr_bytes, uint32_t nr_relocs );
103
104 /**
105 * Emit a relocation for a host surface.
106 *
107 * @param flags bitmask of SVGA_RELOC_* flags
108 *
109 * NOTE: Order of this call does matter. It should be the same order
110 * as relocations appear in the command buffer.
111 */
112 void
113 (*surface_relocation)(struct svga_winsys_context *swc,
114 uint32 *sid,
115 uint32 *mobid,
116 struct svga_winsys_surface *surface,
117 unsigned flags);
118
119 /**
120 * Emit a relocation for a guest memory region.
121 *
122 * @param flags bitmask of SVGA_RELOC_* flags
123 *
124 * NOTE: Order of this call does matter. It should be the same order
125 * as relocations appear in the command buffer.
126 */
127 void
128 (*region_relocation)(struct svga_winsys_context *swc,
129 struct SVGAGuestPtr *ptr,
130 struct svga_winsys_buffer *buffer,
131 uint32 offset,
132 unsigned flags);
133
134 /**
135 * Emit a relocation for a guest-backed shader object.
136 *
137 * NOTE: Order of this call does matter. It should be the same order
138 * as relocations appear in the command buffer.
139 */
140 void
141 (*shader_relocation)(struct svga_winsys_context *swc,
142 uint32 *shid,
143 uint32 *mobid,
144 uint32 *offset,
145 struct svga_winsys_gb_shader *shader);
146
147 /**
148 * Emit a relocation for a guest-backed context.
149 *
150 * NOTE: Order of this call does matter. It should be the same order
151 * as relocations appear in the command buffer.
152 */
153 void
154 (*context_relocation)(struct svga_winsys_context *swc, uint32 *cid);
155
156 /**
157 * Emit a relocation for a guest Memory OBject.
158 *
159 * @param flags bitmask of SVGA_RELOC_* flags
160 * @param offset_into_mob Buffer starts at this offset into the MOB.
161 *
162 * Note that not all commands accept an offset into the MOB and
163 * those commands can't use suballocated buffer pools. To trap
164 * errors from improper buffer pool usage, set the offset_into_mob
165 * pointer to NULL.
166 */
167 void
168 (*mob_relocation)(struct svga_winsys_context *swc,
169 SVGAMobId *id,
170 uint32 *offset_into_mob,
171 struct svga_winsys_buffer *buffer,
172 uint32 offset,
173 unsigned flags);
174
175 void
176 (*commit)(struct svga_winsys_context *swc);
177
178 enum pipe_error
179 (*flush)(struct svga_winsys_context *swc,
180 struct pipe_fence_handle **pfence);
181
182 /**
183 * Context ID used to fill in the commands
184 *
185 * Context IDs are arbitrary small non-negative integers,
186 * global to the entire SVGA device.
187 */
188 uint32 cid;
189
190 /**
191 ** BEGIN new functions for guest-backed surfaces.
192 **/
193
194 boolean have_gb_objects;
195
196 /**
197 * Map a guest-backed surface.
198 * \param flags bitmaks of PIPE_TRANSFER_x flags
199 *
200 * The surface_map() member is allowed to fail due to a
201 * shortage of command buffer space, if the
202 * PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE bit is set in flags.
203 * In that case, the caller must flush the current command
204 * buffer and reissue the map.
205 */
206 void *
207 (*surface_map)(struct svga_winsys_context *swc,
208 struct svga_winsys_surface *surface,
209 unsigned flags, boolean *retry);
210
211 /**
212 * Unmap a guest-backed surface.
213 * \param rebind returns a flag indicating whether the caller should
214 * issue a SVGA3D_BindGBSurface() call.
215 */
216 void
217 (*surface_unmap)(struct svga_winsys_context *swc,
218 struct svga_winsys_surface *surface,
219 boolean *rebind);
220
221 };
222
223
224 /**
225 * SVGA per-screen winsys interface.
226 */
227 struct svga_winsys_screen
228 {
229 void
230 (*destroy)(struct svga_winsys_screen *sws);
231
232 SVGA3dHardwareVersion
233 (*get_hw_version)(struct svga_winsys_screen *sws);
234
235 boolean
236 (*get_cap)(struct svga_winsys_screen *sws,
237 SVGA3dDevCapIndex index,
238 SVGA3dDevCapResult *result);
239
240 /**
241 * Create a new context.
242 *
243 * Context objects encapsulate all render state, and shader
244 * objects are per-context.
245 *
246 * Surfaces are not per-context. The same surface can be shared
247 * between multiple contexts, and surface operations can occur
248 * without a context.
249 */
250 struct svga_winsys_context *
251 (*context_create)(struct svga_winsys_screen *sws);
252
253
254 /**
255 * This creates a "surface" object in the SVGA3D device,
256 * and returns the surface ID (sid). Surfaces are generic
257 * containers for host VRAM objects like textures, vertex
258 * buffers, and depth/stencil buffers.
259 *
260 * Surfaces are hierarchial:
261 *
262 * - Surface may have multiple faces (for cube maps)
263 *
264 * - Each face has a list of mipmap levels
265 *
266 * - Each mipmap image may have multiple volume
267 * slices, if the image is three dimensional.
268 *
269 * - Each slice is a 2D array of 'blocks'
270 *
271 * - Each block may be one or more pixels.
272 * (Usually 1, more for DXT or YUV formats.)
273 *
274 * Surfaces are generic host VRAM objects. The SVGA3D device
275 * may optimize surfaces according to the format they were
276 * created with, but this format does not limit the ways in
277 * which the surface may be used. For example, a depth surface
278 * can be used as a texture, or a floating point image may
279 * be used as a vertex buffer. Some surface usages may be
280 * lower performance, due to software emulation, but any
281 * usage should work with any surface.
282 */
283 struct svga_winsys_surface *
284 (*surface_create)(struct svga_winsys_screen *sws,
285 SVGA3dSurfaceFlags flags,
286 SVGA3dSurfaceFormat format,
287 SVGA3dSize size,
288 uint32 numFaces,
289 uint32 numMipLevels);
290
291 /**
292 * Creates a surface from a winsys handle.
293 * Used to implement pipe_screen::resource_from_handle.
294 */
295 struct svga_winsys_surface *
296 (*surface_from_handle)(struct svga_winsys_screen *sws,
297 struct winsys_handle *whandle,
298 SVGA3dSurfaceFormat *format);
299
300 /**
301 * Get a winsys_handle from a surface.
302 * Used to implement pipe_screen::resource_get_handle.
303 */
304 boolean
305 (*surface_get_handle)(struct svga_winsys_screen *sws,
306 struct svga_winsys_surface *surface,
307 unsigned stride,
308 struct winsys_handle *whandle);
309
310 /**
311 * Whether this surface is sitting in a validate list
312 */
313 boolean
314 (*surface_is_flushed)(struct svga_winsys_screen *sws,
315 struct svga_winsys_surface *surface);
316
317 /**
318 * Reference a SVGA3D surface object. This allows sharing of a
319 * surface between different objects.
320 */
321 void
322 (*surface_reference)(struct svga_winsys_screen *sws,
323 struct svga_winsys_surface **pdst,
324 struct svga_winsys_surface *src);
325
326 /**
327 * Buffer management. Buffer attributes are mostly fixed over its lifetime.
328 *
329 * @param usage bitmask of SVGA_BUFFER_USAGE_* flags.
330 *
331 * alignment indicates the client's alignment requirements, eg for
332 * SSE instructions.
333 */
334 struct svga_winsys_buffer *
335 (*buffer_create)( struct svga_winsys_screen *sws,
336 unsigned alignment,
337 unsigned usage,
338 unsigned size );
339
340 /**
341 * Map the entire data store of a buffer object into the client's address.
342 * usage is a bitmask of PIPE_TRANSFER_*
343 */
344 void *
345 (*buffer_map)( struct svga_winsys_screen *sws,
346 struct svga_winsys_buffer *buf,
347 unsigned usage );
348
349 void
350 (*buffer_unmap)( struct svga_winsys_screen *sws,
351 struct svga_winsys_buffer *buf );
352
353 void
354 (*buffer_destroy)( struct svga_winsys_screen *sws,
355 struct svga_winsys_buffer *buf );
356
357
358 /**
359 * Reference a fence object.
360 */
361 void
362 (*fence_reference)( struct svga_winsys_screen *sws,
363 struct pipe_fence_handle **pdst,
364 struct pipe_fence_handle *src );
365
366 /**
367 * Checks whether the fence has been signalled.
368 * \param flags driver-specific meaning
369 * \return zero on success.
370 */
371 int (*fence_signalled)( struct svga_winsys_screen *sws,
372 struct pipe_fence_handle *fence,
373 unsigned flag );
374
375 /**
376 * Wait for the fence to finish.
377 * \param flags driver-specific meaning
378 * \return zero on success.
379 */
380 int (*fence_finish)( struct svga_winsys_screen *sws,
381 struct pipe_fence_handle *fence,
382 unsigned flag );
383
384
385 /**
386 ** BEGIN new functions for guest-backed surfaces.
387 **/
388
389 /** Are guest-backed objects enabled? */
390 bool have_gb_objects;
391
392 /** Can we do DMA with guest-backed objects enabled? */
393 bool have_gb_dma;
394
395 /**
396 * Create and define a GB shader.
397 */
398 struct svga_winsys_gb_shader *
399 (*shader_create)(struct svga_winsys_screen *sws,
400 SVGA3dShaderType type,
401 const uint32 *bytecode,
402 uint32 bytecodeLen);
403
404 /**
405 * Destroy a GB shader. It's safe to call this function even
406 * if the shader is referenced in a context's command stream.
407 */
408 void
409 (*shader_destroy)(struct svga_winsys_screen *sws,
410 struct svga_winsys_gb_shader *shader);
411
412 };
413
414
415 struct svga_winsys_screen *
416 svga_winsys_screen(struct pipe_screen *screen);
417
418 struct svga_winsys_context *
419 svga_winsys_context(struct pipe_context *context);
420
421 struct pipe_resource *
422 svga_screen_buffer_wrap_surface(struct pipe_screen *screen,
423 enum SVGA3dSurfaceFormat format,
424 struct svga_winsys_surface *srf);
425
426 struct svga_winsys_surface *
427 svga_screen_buffer_get_winsys_surface(struct pipe_resource *buffer);
428
429 #endif /* SVGA_WINSYS_H_ */