Merge remote-tracking branch 'public/master' into vulkan
[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 #define SVGA_SURFACE_USAGE_SHARED (1 << 0)
83 #define SVGA_SURFACE_USAGE_SCANOUT (1 << 1)
84
85 #define SVGA_QUERY_FLAG_SET (1 << 0)
86 #define SVGA_QUERY_FLAG_REF (1 << 1)
87
88 #define SVGA_HINT_FLAG_CAN_PRE_FLUSH (1 << 0) /* Can preemptively flush */
89
90 /** Opaque surface handle */
91 struct svga_winsys_surface;
92
93 /** Opaque guest-backed objects */
94 struct svga_winsys_gb_shader;
95 struct svga_winsys_gb_query;
96
97
98 /**
99 * SVGA per-context winsys interface.
100 */
101 struct svga_winsys_context
102 {
103 void
104 (*destroy)(struct svga_winsys_context *swc);
105
106 void *
107 (*reserve)(struct svga_winsys_context *swc,
108 uint32_t nr_bytes, uint32_t nr_relocs );
109
110 /**
111 * Returns current size of command buffer, in bytes.
112 */
113 unsigned
114 (*get_command_buffer_size)(struct svga_winsys_context *swc);
115
116 /**
117 * Emit a relocation for a host surface.
118 *
119 * @param flags bitmask of SVGA_RELOC_* flags
120 *
121 * NOTE: Order of this call does matter. It should be the same order
122 * as relocations appear in the command buffer.
123 */
124 void
125 (*surface_relocation)(struct svga_winsys_context *swc,
126 uint32 *sid,
127 uint32 *mobid,
128 struct svga_winsys_surface *surface,
129 unsigned flags);
130
131 /**
132 * Emit a relocation for a guest memory region.
133 *
134 * @param flags bitmask of SVGA_RELOC_* flags
135 *
136 * NOTE: Order of this call does matter. It should be the same order
137 * as relocations appear in the command buffer.
138 */
139 void
140 (*region_relocation)(struct svga_winsys_context *swc,
141 struct SVGAGuestPtr *ptr,
142 struct svga_winsys_buffer *buffer,
143 uint32 offset,
144 unsigned flags);
145
146 /**
147 * Emit a relocation for a guest-backed shader object.
148 *
149 * NOTE: Order of this call does matter. It should be the same order
150 * as relocations appear in the command buffer.
151 */
152 void
153 (*shader_relocation)(struct svga_winsys_context *swc,
154 uint32 *shid,
155 uint32 *mobid,
156 uint32 *offset,
157 struct svga_winsys_gb_shader *shader,
158 unsigned flags);
159
160 /**
161 * Emit a relocation for a guest-backed context.
162 *
163 * NOTE: Order of this call does matter. It should be the same order
164 * as relocations appear in the command buffer.
165 */
166 void
167 (*context_relocation)(struct svga_winsys_context *swc, uint32 *cid);
168
169 /**
170 * Emit a relocation for a guest Memory OBject.
171 *
172 * @param flags bitmask of SVGA_RELOC_* flags
173 * @param offset_into_mob Buffer starts at this offset into the MOB.
174 *
175 * Note that not all commands accept an offset into the MOB and
176 * those commands can't use suballocated buffer pools. To trap
177 * errors from improper buffer pool usage, set the offset_into_mob
178 * pointer to NULL.
179 */
180 void
181 (*mob_relocation)(struct svga_winsys_context *swc,
182 SVGAMobId *id,
183 uint32 *offset_into_mob,
184 struct svga_winsys_buffer *buffer,
185 uint32 offset,
186 unsigned flags);
187
188 /**
189 * Emit a relocation for a guest-backed query object.
190 *
191 * NOTE: Order of this call does matter. It should be the same order
192 * as relocations appear in the command buffer.
193 */
194 void
195 (*query_relocation)(struct svga_winsys_context *swc,
196 SVGAMobId *id,
197 struct svga_winsys_gb_query *query);
198
199 /**
200 * Bind queries to context.
201 * \param flags exactly one of SVGA_QUERY_FLAG_SET/REF
202 */
203 enum pipe_error
204 (*query_bind)(struct svga_winsys_context *sws,
205 struct svga_winsys_gb_query *query,
206 unsigned flags);
207
208 void
209 (*commit)(struct svga_winsys_context *swc);
210
211 enum pipe_error
212 (*flush)(struct svga_winsys_context *swc,
213 struct pipe_fence_handle **pfence);
214
215 /**
216 * Context ID used to fill in the commands
217 *
218 * Context IDs are arbitrary small non-negative integers,
219 * global to the entire SVGA device.
220 */
221 uint32 cid;
222
223 /**
224 * Flags to hint the current context state
225 */
226 uint32 hints;
227
228 /**
229 ** BEGIN new functions for guest-backed surfaces.
230 **/
231
232 boolean have_gb_objects;
233
234 /**
235 * Map a guest-backed surface.
236 * \param flags bitmaks of PIPE_TRANSFER_x flags
237 *
238 * The surface_map() member is allowed to fail due to a
239 * shortage of command buffer space, if the
240 * PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE bit is set in flags.
241 * In that case, the caller must flush the current command
242 * buffer and reissue the map.
243 */
244 void *
245 (*surface_map)(struct svga_winsys_context *swc,
246 struct svga_winsys_surface *surface,
247 unsigned flags, boolean *retry);
248
249 /**
250 * Unmap a guest-backed surface.
251 * \param rebind returns a flag indicating whether the caller should
252 * issue a SVGA3D_BindGBSurface() call.
253 */
254 void
255 (*surface_unmap)(struct svga_winsys_context *swc,
256 struct svga_winsys_surface *surface,
257 boolean *rebind);
258
259 /**
260 * Create and define a DX GB shader that resides in the device COTable.
261 * Caller of this function will issue the DXDefineShader command.
262 */
263 struct svga_winsys_gb_shader *
264 (*shader_create)(struct svga_winsys_context *swc,
265 uint32 shaderId,
266 SVGA3dShaderType shaderType,
267 const uint32 *bytecode,
268 uint32 bytecodeLen);
269
270 /**
271 * Destroy a DX GB shader.
272 * This function will issue the DXDestroyShader command.
273 */
274 void
275 (*shader_destroy)(struct svga_winsys_context *swc,
276 struct svga_winsys_gb_shader *shader);
277
278 /**
279 * Rebind a DX GB resource to a context.
280 * This is called to reference a DX GB resource in the command stream in
281 * order to page in the associated resource in case the memory has been
282 * paged out, and to fence it if necessary after command submission.
283 */
284 enum pipe_error
285 (*resource_rebind)(struct svga_winsys_context *swc,
286 struct svga_winsys_surface *surface,
287 struct svga_winsys_gb_shader *shader,
288 unsigned flags);
289 };
290
291
292 /**
293 * SVGA per-screen winsys interface.
294 */
295 struct svga_winsys_screen
296 {
297 void
298 (*destroy)(struct svga_winsys_screen *sws);
299
300 SVGA3dHardwareVersion
301 (*get_hw_version)(struct svga_winsys_screen *sws);
302
303 boolean
304 (*get_cap)(struct svga_winsys_screen *sws,
305 SVGA3dDevCapIndex index,
306 SVGA3dDevCapResult *result);
307
308 /**
309 * Create a new context.
310 *
311 * Context objects encapsulate all render state, and shader
312 * objects are per-context.
313 *
314 * Surfaces are not per-context. The same surface can be shared
315 * between multiple contexts, and surface operations can occur
316 * without a context.
317 */
318 struct svga_winsys_context *
319 (*context_create)(struct svga_winsys_screen *sws);
320
321
322 /**
323 * This creates a "surface" object in the SVGA3D device.
324 *
325 * \param sws Pointer to an svga_winsys_context
326 * \param flags Device surface create flags
327 * \param format Format Device surface format
328 * \param usage Winsys usage: bitmask of SVGA_SURFACE_USAGE_x flags
329 * \param size Surface size given in device format
330 * \param numLayers Number of layers of the surface (or cube faces)
331 * \param numMipLevels Number of mipmap levels for each face
332 *
333 * Returns the surface ID (sid). Surfaces are generic
334 * containers for host VRAM objects like textures, vertex
335 * buffers, and depth/stencil buffers.
336 *
337 * Surfaces are hierarchial:
338 *
339 * - Surface may have multiple faces (for cube maps)
340 *
341 * - Each face has a list of mipmap levels
342 *
343 * - Each mipmap image may have multiple volume
344 * slices for 3D image, or multiple 2D slices for texture array.
345 *
346 * - Each slice is a 2D array of 'blocks'
347 *
348 * - Each block may be one or more pixels.
349 * (Usually 1, more for DXT or YUV formats.)
350 *
351 * Surfaces are generic host VRAM objects. The SVGA3D device
352 * may optimize surfaces according to the format they were
353 * created with, but this format does not limit the ways in
354 * which the surface may be used. For example, a depth surface
355 * can be used as a texture, or a floating point image may
356 * be used as a vertex buffer. Some surface usages may be
357 * lower performance, due to software emulation, but any
358 * usage should work with any surface.
359 */
360 struct svga_winsys_surface *
361 (*surface_create)(struct svga_winsys_screen *sws,
362 SVGA3dSurfaceFlags flags,
363 SVGA3dSurfaceFormat format,
364 unsigned usage,
365 SVGA3dSize size,
366 uint32 numLayers,
367 uint32 numMipLevels,
368 unsigned sampleCount);
369
370 /**
371 * Creates a surface from a winsys handle.
372 * Used to implement pipe_screen::resource_from_handle.
373 */
374 struct svga_winsys_surface *
375 (*surface_from_handle)(struct svga_winsys_screen *sws,
376 struct winsys_handle *whandle,
377 SVGA3dSurfaceFormat *format);
378
379 /**
380 * Get a winsys_handle from a surface.
381 * Used to implement pipe_screen::resource_get_handle.
382 */
383 boolean
384 (*surface_get_handle)(struct svga_winsys_screen *sws,
385 struct svga_winsys_surface *surface,
386 unsigned stride,
387 struct winsys_handle *whandle);
388
389 /**
390 * Whether this surface is sitting in a validate list
391 */
392 boolean
393 (*surface_is_flushed)(struct svga_winsys_screen *sws,
394 struct svga_winsys_surface *surface);
395
396 /**
397 * Reference a SVGA3D surface object. This allows sharing of a
398 * surface between different objects.
399 */
400 void
401 (*surface_reference)(struct svga_winsys_screen *sws,
402 struct svga_winsys_surface **pdst,
403 struct svga_winsys_surface *src);
404
405 /**
406 * Check if a resource (texture, buffer) of the given size
407 * and format can be created.
408 * \Return TRUE if OK, FALSE if too large.
409 */
410 boolean
411 (*surface_can_create)(struct svga_winsys_screen *sws,
412 SVGA3dSurfaceFormat format,
413 SVGA3dSize size,
414 uint32 numLayers,
415 uint32 numMipLevels);
416
417 /**
418 * Buffer management. Buffer attributes are mostly fixed over its lifetime.
419 *
420 * @param usage bitmask of SVGA_BUFFER_USAGE_* flags.
421 *
422 * alignment indicates the client's alignment requirements, eg for
423 * SSE instructions.
424 */
425 struct svga_winsys_buffer *
426 (*buffer_create)( struct svga_winsys_screen *sws,
427 unsigned alignment,
428 unsigned usage,
429 unsigned size );
430
431 /**
432 * Map the entire data store of a buffer object into the client's address.
433 * usage is a bitmask of PIPE_TRANSFER_*
434 */
435 void *
436 (*buffer_map)( struct svga_winsys_screen *sws,
437 struct svga_winsys_buffer *buf,
438 unsigned usage );
439
440 void
441 (*buffer_unmap)( struct svga_winsys_screen *sws,
442 struct svga_winsys_buffer *buf );
443
444 void
445 (*buffer_destroy)( struct svga_winsys_screen *sws,
446 struct svga_winsys_buffer *buf );
447
448
449 /**
450 * Reference a fence object.
451 */
452 void
453 (*fence_reference)( struct svga_winsys_screen *sws,
454 struct pipe_fence_handle **pdst,
455 struct pipe_fence_handle *src );
456
457 /**
458 * Checks whether the fence has been signalled.
459 * \param flags driver-specific meaning
460 * \return zero on success.
461 */
462 int (*fence_signalled)( struct svga_winsys_screen *sws,
463 struct pipe_fence_handle *fence,
464 unsigned flag );
465
466 /**
467 * Wait for the fence to finish.
468 * \param flags driver-specific meaning
469 * \return zero on success.
470 */
471 int (*fence_finish)( struct svga_winsys_screen *sws,
472 struct pipe_fence_handle *fence,
473 unsigned flag );
474
475
476 /**
477 ** BEGIN new functions for guest-backed surfaces.
478 **/
479
480 /** Are guest-backed objects enabled? */
481 bool have_gb_objects;
482
483 /** Can we do DMA with guest-backed objects enabled? */
484 bool have_gb_dma;
485
486 /**
487 * Create and define a GB shader.
488 */
489 struct svga_winsys_gb_shader *
490 (*shader_create)(struct svga_winsys_screen *sws,
491 SVGA3dShaderType shaderType,
492 const uint32 *bytecode,
493 uint32 bytecodeLen);
494
495 /**
496 * Destroy a GB shader. It's safe to call this function even
497 * if the shader is referenced in a context's command stream.
498 */
499 void
500 (*shader_destroy)(struct svga_winsys_screen *sws,
501 struct svga_winsys_gb_shader *shader);
502
503 /**
504 * Create and define a GB query.
505 */
506 struct svga_winsys_gb_query *
507 (*query_create)(struct svga_winsys_screen *sws, uint32 len);
508
509 /**
510 * Destroy a GB query.
511 */
512 void
513 (*query_destroy)(struct svga_winsys_screen *sws,
514 struct svga_winsys_gb_query *query);
515
516 /**
517 * Initialize the query state of the query that resides in the slot
518 * specified in offset
519 * \return zero on success.
520 */
521 int
522 (*query_init)(struct svga_winsys_screen *sws,
523 struct svga_winsys_gb_query *query,
524 unsigned offset,
525 SVGA3dQueryState queryState);
526
527 /**
528 * Inquire for the query state and result of the query that resides
529 * in the slot specified in offset
530 */
531 void
532 (*query_get_result)(struct svga_winsys_screen *sws,
533 struct svga_winsys_gb_query *query,
534 unsigned offset,
535 SVGA3dQueryState *queryState,
536 void *result, uint32 resultLen);
537
538 /** Have VGPU v10 hardware? */
539 boolean have_vgpu10;
540
541 /** To rebind resources at the beginnning of a new command buffer */
542 boolean need_to_rebind_resources;
543 };
544
545
546 struct svga_winsys_screen *
547 svga_winsys_screen(struct pipe_screen *screen);
548
549 struct svga_winsys_context *
550 svga_winsys_context(struct pipe_context *context);
551
552 struct pipe_resource *
553 svga_screen_buffer_wrap_surface(struct pipe_screen *screen,
554 enum SVGA3dSurfaceFormat format,
555 struct svga_winsys_surface *srf);
556
557 struct svga_winsys_surface *
558 svga_screen_buffer_get_winsys_surface(struct pipe_resource *buffer);
559
560 #endif /* SVGA_WINSYS_H_ */