1 /**********************************************************
2 * Copyright 2009-2015 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
28 * This file implements the SVGA interface into this winsys, defined
29 * in drivers/svga/svga_winsys.h.
31 * @author Keith Whitwell
32 * @author Jose Fonseca
37 #include "svga3d_caps.h"
39 #include "util/u_inlines.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42 #include "pipebuffer/pb_buffer.h"
43 #include "pipebuffer/pb_bufmgr.h"
44 #include "svga_winsys.h"
45 #include "vmw_context.h"
46 #include "vmw_screen.h"
47 #include "vmw_surface.h"
48 #include "vmw_buffer.h"
49 #include "vmw_fence.h"
50 #include "vmw_shader.h"
51 #include "vmw_query.h"
52 #include "svga3d_surfacedefs.h"
55 * Try to get a surface backing buffer from the cache
56 * if it's this size or smaller.
58 #define VMW_TRY_CACHED_SIZE (2*1024*1024)
60 static struct svga_winsys_buffer
*
61 vmw_svga_winsys_buffer_create(struct svga_winsys_screen
*sws
,
66 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
67 struct vmw_buffer_desc desc
;
68 struct pb_manager
*provider
;
69 struct pb_buffer
*buffer
;
71 memset(&desc
, 0, sizeof desc
);
72 desc
.pb_desc
.alignment
= alignment
;
73 desc
.pb_desc
.usage
= usage
;
75 if (usage
== SVGA_BUFFER_USAGE_PINNED
) {
76 if (vws
->pools
.query_fenced
== NULL
&& !vmw_query_pools_init(vws
))
78 provider
= vws
->pools
.query_fenced
;
79 } else if (usage
== SVGA_BUFFER_USAGE_SHADER
) {
80 provider
= vws
->pools
.mob_shader_slab_fenced
;
82 provider
= vws
->pools
.gmr_fenced
;
85 buffer
= provider
->create_buffer(provider
, size
, &desc
.pb_desc
);
87 if(!buffer
&& provider
== vws
->pools
.gmr_fenced
) {
90 provider
= vws
->pools
.gmr_slab_fenced
;
91 buffer
= provider
->create_buffer(provider
, size
, &desc
.pb_desc
);
97 return vmw_svga_winsys_buffer_wrap(buffer
);
102 vmw_svga_winsys_fence_reference(struct svga_winsys_screen
*sws
,
103 struct pipe_fence_handle
**pdst
,
104 struct pipe_fence_handle
*src
)
106 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
108 vmw_fence_reference(vws
, pdst
, src
);
113 vmw_svga_winsys_fence_signalled(struct svga_winsys_screen
*sws
,
114 struct pipe_fence_handle
*fence
,
117 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
119 return vmw_fence_signalled(vws
, fence
, flag
);
124 vmw_svga_winsys_fence_finish(struct svga_winsys_screen
*sws
,
125 struct pipe_fence_handle
*fence
,
129 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
131 return vmw_fence_finish(vws
, fence
, timeout
, flag
);
136 static struct svga_winsys_surface
*
137 vmw_svga_winsys_surface_create(struct svga_winsys_screen
*sws
,
138 SVGA3dSurfaceFlags flags
,
139 SVGA3dSurfaceFormat format
,
144 unsigned sampleCount
)
146 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
147 struct vmw_svga_winsys_surface
*surface
;
148 struct vmw_buffer_desc desc
;
149 struct pb_manager
*provider
;
150 uint32_t buffer_size
;
152 memset(&desc
, 0, sizeof(desc
));
153 surface
= CALLOC_STRUCT(vmw_svga_winsys_surface
);
157 pipe_reference_init(&surface
->refcnt
, 1);
158 p_atomic_set(&surface
->validated
, 0);
159 surface
->screen
= vws
;
160 (void) mtx_init(&surface
->mutex
, mtx_plain
);
161 surface
->shared
= !!(usage
& SVGA_SURFACE_USAGE_SHARED
);
162 provider
= (surface
->shared
) ? vws
->pools
.gmr
: vws
->pools
.mob_fenced
;
165 * Used for the backing buffer GB surfaces, and to approximate
166 * when to flush on non-GB hosts.
168 buffer_size
= svga3dsurface_get_serialized_size(format
, size
, numMipLevels
,
170 if (flags
& SVGA3D_SURFACE_BIND_STREAM_OUTPUT
)
171 buffer_size
+= sizeof(SVGA3dDXSOState
);
173 if (buffer_size
> vws
->ioctl
.max_texture_size
) {
177 if (sws
->have_gb_objects
) {
178 SVGAGuestPtr ptr
= {0,0};
181 * If the backing buffer size is small enough, try to allocate a
182 * buffer out of the buffer cache. Otherwise, let the kernel allocate
183 * a suitable buffer for us.
185 if (buffer_size
< VMW_TRY_CACHED_SIZE
&& !surface
->shared
) {
186 struct pb_buffer
*pb_buf
;
188 surface
->size
= buffer_size
;
189 desc
.pb_desc
.alignment
= 4096;
190 desc
.pb_desc
.usage
= 0;
191 pb_buf
= provider
->create_buffer(provider
, buffer_size
, &desc
.pb_desc
);
192 surface
->buf
= vmw_svga_winsys_buffer_wrap(pb_buf
);
193 if (surface
->buf
&& !vmw_gmr_bufmgr_region_ptr(pb_buf
, &ptr
))
197 surface
->sid
= vmw_ioctl_gb_surface_create(vws
, flags
, format
, usage
,
199 numMipLevels
, sampleCount
,
201 surface
->buf
? NULL
:
204 if (surface
->sid
== SVGA3D_INVALID_ID
) {
205 if (surface
->buf
== NULL
) {
209 * Kernel refused to allocate a surface for us.
210 * Perhaps something was wrong with our buffer?
211 * This is really a guard against future new size requirements
212 * on the backing buffers.
214 vmw_svga_winsys_buffer_destroy(sws
, surface
->buf
);
216 surface
->sid
= vmw_ioctl_gb_surface_create(vws
, flags
, format
, usage
,
218 numMipLevels
, sampleCount
,
220 if (surface
->sid
== SVGA3D_INVALID_ID
)
226 * If the kernel created the buffer for us, wrap it into a
227 * vmw_svga_winsys_buffer.
229 if (surface
->buf
== NULL
) {
230 struct pb_buffer
*pb_buf
;
232 surface
->size
= vmw_region_size(desc
.region
);
233 desc
.pb_desc
.alignment
= 4096;
234 desc
.pb_desc
.usage
= VMW_BUFFER_USAGE_SHARED
;
235 pb_buf
= provider
->create_buffer(provider
, surface
->size
,
237 surface
->buf
= vmw_svga_winsys_buffer_wrap(pb_buf
);
238 if (surface
->buf
== NULL
) {
239 vmw_ioctl_region_destroy(desc
.region
);
240 vmw_ioctl_surface_destroy(vws
, surface
->sid
);
245 surface
->sid
= vmw_ioctl_surface_create(vws
, flags
, format
, usage
,
246 size
, numLayers
, numMipLevels
,
248 if(surface
->sid
== SVGA3D_INVALID_ID
)
251 /* Best estimate for surface size, used for early flushing. */
252 surface
->size
= buffer_size
;
256 return svga_winsys_surface(surface
);
260 vmw_svga_winsys_buffer_destroy(sws
, surface
->buf
);
268 vmw_svga_winsys_surface_can_create(struct svga_winsys_screen
*sws
,
269 SVGA3dSurfaceFormat format
,
274 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
275 uint32_t buffer_size
;
277 buffer_size
= svga3dsurface_get_serialized_size(format
, size
,
280 if (buffer_size
> vws
->ioctl
.max_texture_size
) {
288 vmw_svga_winsys_surface_is_flushed(struct svga_winsys_screen
*sws
,
289 struct svga_winsys_surface
*surface
)
291 struct vmw_svga_winsys_surface
*vsurf
= vmw_svga_winsys_surface(surface
);
292 return (p_atomic_read(&vsurf
->validated
) == 0);
297 vmw_svga_winsys_surface_ref(struct svga_winsys_screen
*sws
,
298 struct svga_winsys_surface
**pDst
,
299 struct svga_winsys_surface
*src
)
301 struct vmw_svga_winsys_surface
*d_vsurf
= vmw_svga_winsys_surface(*pDst
);
302 struct vmw_svga_winsys_surface
*s_vsurf
= vmw_svga_winsys_surface(src
);
304 vmw_svga_winsys_surface_reference(&d_vsurf
, s_vsurf
);
305 *pDst
= svga_winsys_surface(d_vsurf
);
310 vmw_svga_winsys_destroy(struct svga_winsys_screen
*sws
)
312 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
314 vmw_winsys_destroy(vws
);
318 static SVGA3dHardwareVersion
319 vmw_svga_winsys_get_hw_version(struct svga_winsys_screen
*sws
)
321 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
323 if (sws
->have_gb_objects
)
324 return SVGA3D_HWVERSION_WS8_B1
;
326 return (SVGA3dHardwareVersion
) vws
->ioctl
.hwversion
;
331 vmw_svga_winsys_get_cap(struct svga_winsys_screen
*sws
,
332 SVGA3dDevCapIndex index
,
333 SVGA3dDevCapResult
*result
)
335 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
337 if (index
> vws
->ioctl
.num_cap_3d
||
338 index
>= SVGA3D_DEVCAP_MAX
||
339 !vws
->ioctl
.cap_3d
[index
].has_cap
)
342 *result
= vws
->ioctl
.cap_3d
[index
].result
;
346 struct svga_winsys_gb_shader
*
347 vmw_svga_winsys_shader_create(struct svga_winsys_screen
*sws
,
348 SVGA3dShaderType type
,
349 const uint32
*bytecode
,
352 struct vmw_winsys_screen
*vws
= vmw_winsys_screen(sws
);
353 struct vmw_svga_winsys_shader
*shader
;
356 shader
= CALLOC_STRUCT(vmw_svga_winsys_shader
);
360 pipe_reference_init(&shader
->refcnt
, 1);
361 p_atomic_set(&shader
->validated
, 0);
362 shader
->screen
= vws
;
363 shader
->buf
= vmw_svga_winsys_buffer_create(sws
, 64,
364 SVGA_BUFFER_USAGE_SHADER
,
369 code
= vmw_svga_winsys_buffer_map(sws
, shader
->buf
, PIPE_TRANSFER_WRITE
);
373 memcpy(code
, bytecode
, bytecodeLen
);
374 vmw_svga_winsys_buffer_unmap(sws
, shader
->buf
);
376 if (!sws
->have_vgpu10
) {
377 shader
->shid
= vmw_ioctl_shader_create(vws
, type
, bytecodeLen
);
378 if (shader
->shid
== SVGA3D_INVALID_ID
)
382 return svga_winsys_shader(shader
);
385 vmw_svga_winsys_buffer_destroy(sws
, shader
->buf
);
393 vmw_svga_winsys_shader_destroy(struct svga_winsys_screen
*sws
,
394 struct svga_winsys_gb_shader
*shader
)
396 struct vmw_svga_winsys_shader
*d_shader
=
397 vmw_svga_winsys_shader(shader
);
399 vmw_svga_winsys_shader_reference(&d_shader
, NULL
);
403 vmw_svga_winsys_stats_inc(enum svga_stats_count index
)
408 vmw_svga_winsys_stats_time_push(enum svga_stats_time index
,
409 struct svga_winsys_stats_timeframe
*tf
)
414 vmw_svga_winsys_stats_time_pop()
419 vmw_winsys_screen_init_svga(struct vmw_winsys_screen
*vws
)
421 vws
->base
.destroy
= vmw_svga_winsys_destroy
;
422 vws
->base
.get_hw_version
= vmw_svga_winsys_get_hw_version
;
423 vws
->base
.get_cap
= vmw_svga_winsys_get_cap
;
424 vws
->base
.context_create
= vmw_svga_winsys_context_create
;
425 vws
->base
.surface_create
= vmw_svga_winsys_surface_create
;
426 vws
->base
.surface_is_flushed
= vmw_svga_winsys_surface_is_flushed
;
427 vws
->base
.surface_reference
= vmw_svga_winsys_surface_ref
;
428 vws
->base
.surface_can_create
= vmw_svga_winsys_surface_can_create
;
429 vws
->base
.buffer_create
= vmw_svga_winsys_buffer_create
;
430 vws
->base
.buffer_map
= vmw_svga_winsys_buffer_map
;
431 vws
->base
.buffer_unmap
= vmw_svga_winsys_buffer_unmap
;
432 vws
->base
.buffer_destroy
= vmw_svga_winsys_buffer_destroy
;
433 vws
->base
.fence_reference
= vmw_svga_winsys_fence_reference
;
434 vws
->base
.fence_signalled
= vmw_svga_winsys_fence_signalled
;
435 vws
->base
.shader_create
= vmw_svga_winsys_shader_create
;
436 vws
->base
.shader_destroy
= vmw_svga_winsys_shader_destroy
;
437 vws
->base
.fence_finish
= vmw_svga_winsys_fence_finish
;
439 vws
->base
.query_create
= vmw_svga_winsys_query_create
;
440 vws
->base
.query_init
= vmw_svga_winsys_query_init
;
441 vws
->base
.query_destroy
= vmw_svga_winsys_query_destroy
;
442 vws
->base
.query_get_result
= vmw_svga_winsys_query_get_result
;
444 vws
->base
.stats_inc
= vmw_svga_winsys_stats_inc
;
445 vws
->base
.stats_time_push
= vmw_svga_winsys_stats_time_push
;
446 vws
->base
.stats_time_pop
= vmw_svga_winsys_stats_time_pop
;