1 /**********************************************************
2 * Copyright 2008-2009 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 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
36 #include "svga_screen.h"
37 #include "svga_context.h"
38 #include "svga_resource_texture.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_sampler_view.h"
41 #include "svga_winsys.h"
42 #include "svga_debug.h"
45 /* XXX: This isn't a real hardware flag, but just a hack for kernel to
46 * know about primary surfaces. Find a better way to accomplish this.
48 #define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)
52 svga_texture_is_referenced( struct pipe_context
*pipe
,
53 struct pipe_resource
*texture
,
54 unsigned level
, int layer
)
56 struct svga_texture
*tex
= svga_texture(texture
);
57 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
60 * The screen does not cache texture writes.
63 if (!tex
->handle
|| ss
->sws
->surface_is_flushed(ss
->sws
, tex
->handle
))
64 return PIPE_UNREFERENCED
;
67 * sws->surface_is_flushed() does not distinguish between read references
68 * and write references. So assume a reference is both.
71 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
77 * Helper function and arrays
81 svga_translate_format(enum pipe_format format
)
85 case PIPE_FORMAT_B8G8R8A8_UNORM
:
86 return SVGA3D_A8R8G8B8
;
87 case PIPE_FORMAT_B8G8R8X8_UNORM
:
88 return SVGA3D_X8R8G8B8
;
90 /* Required for GL2.1:
92 case PIPE_FORMAT_B8G8R8A8_SRGB
:
93 return SVGA3D_A8R8G8B8
;
95 case PIPE_FORMAT_B5G6R5_UNORM
:
97 case PIPE_FORMAT_B5G5R5A1_UNORM
:
98 return SVGA3D_A1R5G5B5
;
99 case PIPE_FORMAT_B4G4R4A4_UNORM
:
100 return SVGA3D_A4R4G4B4
;
103 /* XXX: Doesn't seem to work properly.
104 case PIPE_FORMAT_Z32_UNORM:
107 case PIPE_FORMAT_Z16_UNORM
:
109 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
110 return SVGA3D_Z_D24S8
;
111 case PIPE_FORMAT_X8Z24_UNORM
:
112 return SVGA3D_Z_D24X8
;
114 case PIPE_FORMAT_A8_UNORM
:
115 return SVGA3D_ALPHA8
;
116 case PIPE_FORMAT_L8_UNORM
:
117 return SVGA3D_LUMINANCE8
;
119 case PIPE_FORMAT_DXT1_RGB
:
120 case PIPE_FORMAT_DXT1_RGBA
:
122 case PIPE_FORMAT_DXT3_RGBA
:
124 case PIPE_FORMAT_DXT5_RGBA
:
128 return SVGA3D_FORMAT_INVALID
;
134 svga_translate_format_render(enum pipe_format format
)
137 case PIPE_FORMAT_B8G8R8A8_UNORM
:
138 case PIPE_FORMAT_B8G8R8X8_UNORM
:
139 case PIPE_FORMAT_B5G5R5A1_UNORM
:
140 case PIPE_FORMAT_B4G4R4A4_UNORM
:
141 case PIPE_FORMAT_B5G6R5_UNORM
:
142 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
143 case PIPE_FORMAT_X8Z24_UNORM
:
144 case PIPE_FORMAT_Z32_UNORM
:
145 case PIPE_FORMAT_Z16_UNORM
:
146 case PIPE_FORMAT_L8_UNORM
:
147 return svga_translate_format(format
);
150 return SVGA3D_FORMAT_INVALID
;
156 svga_transfer_dma_band(struct svga_context
*svga
,
157 struct svga_transfer
*st
,
158 SVGA3dTransferType transfer
,
159 unsigned y
, unsigned h
, unsigned srcy
)
161 struct svga_texture
*texture
= svga_texture(st
->base
.resource
);
165 box
.x
= st
->base
.box
.x
;
167 box
.z
= st
->base
.box
.z
;
168 box
.w
= st
->base
.box
.width
;
175 if (st
->base
.resource
->target
== PIPE_TEXTURE_CUBE
) {
176 st
->face
= st
->base
.box
.z
;
182 SVGA_DBG(DEBUG_DMA
, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
183 transfer
== SVGA3D_WRITE_HOST_VRAM
? "to" : "from",
189 st
->base
.box
.x
+ st
->base
.box
.width
,
192 util_format_get_blocksize(texture
->b
.b
.format
) * 8 /
193 (util_format_get_blockwidth(texture
->b
.b
.format
)*util_format_get_blockheight(texture
->b
.b
.format
)));
195 ret
= SVGA3D_SurfaceDMA(svga
->swc
, st
, transfer
, &box
, 1);
197 svga_context_flush(svga
, NULL
);
198 ret
= SVGA3D_SurfaceDMA(svga
->swc
, st
, transfer
, &box
, 1);
199 assert(ret
== PIPE_OK
);
205 svga_transfer_dma(struct svga_context
*svga
,
206 struct svga_transfer
*st
,
207 SVGA3dTransferType transfer
)
209 struct svga_texture
*texture
= svga_texture(st
->base
.resource
);
210 struct svga_screen
*screen
= svga_screen(texture
->b
.b
.screen
);
211 struct svga_winsys_screen
*sws
= screen
->sws
;
212 struct pipe_fence_handle
*fence
= NULL
;
214 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
215 SVGA_DBG(DEBUG_PERF
, "%s: readback transfer\n", __FUNCTION__
);
220 /* Do the DMA transfer in a single go */
222 svga_transfer_dma_band(svga
, st
, transfer
, st
->base
.box
.y
, st
->base
.box
.height
, 0);
224 if(transfer
== SVGA3D_READ_HOST_VRAM
) {
225 svga_context_flush(svga
, &fence
);
226 sws
->fence_finish(sws
, fence
, 0);
227 sws
->fence_reference(sws
, &fence
, NULL
);
232 unsigned blockheight
= util_format_get_blockheight(st
->base
.resource
->format
);
233 h
= st
->hw_nblocksy
* blockheight
;
235 for(y
= 0; y
< st
->base
.box
.height
; y
+= h
) {
236 unsigned offset
, length
;
239 if (y
+ h
> st
->base
.box
.height
)
240 h
= st
->base
.box
.height
- y
;
242 /* Transfer band must be aligned to pixel block boundaries */
243 assert(y
% blockheight
== 0);
244 assert(h
% blockheight
== 0);
246 offset
= y
* st
->base
.stride
/ blockheight
;
247 length
= h
* st
->base
.stride
/ blockheight
;
249 sw
= (uint8_t *)st
->swbuf
+ offset
;
251 if(transfer
== SVGA3D_WRITE_HOST_VRAM
) {
252 /* Wait for the previous DMAs to complete */
253 /* TODO: keep one DMA (at half the size) in the background */
255 svga_context_flush(svga
, &fence
);
256 sws
->fence_finish(sws
, fence
, 0);
257 sws
->fence_reference(sws
, &fence
, NULL
);
260 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_TRANSFER_WRITE
);
263 memcpy(hw
, sw
, length
);
264 sws
->buffer_unmap(sws
, st
->hwbuf
);
268 svga_transfer_dma_band(svga
, st
, transfer
, y
, h
, srcy
);
270 if(transfer
== SVGA3D_READ_HOST_VRAM
) {
271 svga_context_flush(svga
, &fence
);
272 sws
->fence_finish(sws
, fence
, 0);
274 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_TRANSFER_READ
);
277 memcpy(sw
, hw
, length
);
278 sws
->buffer_unmap(sws
, st
->hwbuf
);
290 svga_texture_get_handle(struct pipe_screen
*screen
,
291 struct pipe_resource
*texture
,
292 struct winsys_handle
*whandle
)
294 struct svga_winsys_screen
*sws
= svga_winsys_screen(texture
->screen
);
297 assert(svga_texture(texture
)->key
.cachable
== 0);
298 svga_texture(texture
)->key
.cachable
= 0;
299 stride
= util_format_get_nblocksx(texture
->format
, texture
->width0
) *
300 util_format_get_blocksize(texture
->format
);
301 return sws
->surface_get_handle(sws
, svga_texture(texture
)->handle
, stride
, whandle
);
306 svga_texture_destroy(struct pipe_screen
*screen
,
307 struct pipe_resource
*pt
)
309 struct svga_screen
*ss
= svga_screen(screen
);
310 struct svga_texture
*tex
= (struct svga_texture
*)pt
;
312 ss
->texture_timestamp
++;
314 svga_sampler_view_reference(&tex
->cached_view
, NULL
);
317 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
319 SVGA_DBG(DEBUG_DMA
, "unref sid %p (texture)\n", tex
->handle
);
320 svga_screen_surface_destroy(ss
, &tex
->key
, &tex
->handle
);
331 /* XXX: Still implementing this as if it was a screen function, but
332 * can now modify it to queue transfers on the context.
334 static struct pipe_transfer
*
335 svga_texture_get_transfer(struct pipe_context
*pipe
,
336 struct pipe_resource
*texture
,
339 const struct pipe_box
*box
)
341 struct svga_context
*svga
= svga_context(pipe
);
342 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
343 struct svga_winsys_screen
*sws
= ss
->sws
;
344 struct svga_transfer
*st
;
345 unsigned nblocksx
= util_format_get_nblocksx(texture
->format
, box
->width
);
346 unsigned nblocksy
= util_format_get_nblocksy(texture
->format
, box
->height
);
348 /* We can't map texture storage directly */
349 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
352 assert(box
->depth
== 1);
353 st
= CALLOC_STRUCT(svga_transfer
);
357 pipe_resource_reference(&st
->base
.resource
, texture
);
358 st
->base
.level
= level
;
359 st
->base
.usage
= usage
;
361 st
->base
.stride
= nblocksx
*util_format_get_blocksize(texture
->format
);
362 st
->base
.layer_stride
= 0;
364 st
->hw_nblocksy
= nblocksy
;
366 st
->hwbuf
= svga_winsys_buffer_create(svga
,
369 st
->hw_nblocksy
*st
->base
.stride
);
370 while(!st
->hwbuf
&& (st
->hw_nblocksy
/= 2)) {
371 st
->hwbuf
= svga_winsys_buffer_create(svga
,
374 st
->hw_nblocksy
*st
->base
.stride
);
380 if(st
->hw_nblocksy
< nblocksy
) {
381 /* We couldn't allocate a hardware buffer big enough for the transfer,
382 * so allocate regular malloc memory instead */
384 debug_printf("%s: failed to allocate %u KB of DMA, "
385 "splitting into %u x %u KB DMA transfers\n",
387 (nblocksy
*st
->base
.stride
+ 1023)/1024,
388 (nblocksy
+ st
->hw_nblocksy
- 1)/st
->hw_nblocksy
,
389 (st
->hw_nblocksy
*st
->base
.stride
+ 1023)/1024);
392 st
->swbuf
= MALLOC(nblocksy
*st
->base
.stride
);
397 if (usage
& PIPE_TRANSFER_READ
)
398 svga_transfer_dma(svga
, st
, SVGA3D_READ_HOST_VRAM
);
403 sws
->buffer_destroy(sws
, st
->hwbuf
);
410 /* XXX: Still implementing this as if it was a screen function, but
411 * can now modify it to queue transfers on the context.
414 svga_texture_transfer_map( struct pipe_context
*pipe
,
415 struct pipe_transfer
*transfer
)
417 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
418 struct svga_winsys_screen
*sws
= ss
->sws
;
419 struct svga_transfer
*st
= svga_transfer(transfer
);
424 /* The wait for read transfers already happened when svga_transfer_dma
426 return sws
->buffer_map(sws
, st
->hwbuf
, transfer
->usage
);
430 /* XXX: Still implementing this as if it was a screen function, but
431 * can now modify it to queue transfers on the context.
434 svga_texture_transfer_unmap(struct pipe_context
*pipe
,
435 struct pipe_transfer
*transfer
)
437 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
438 struct svga_winsys_screen
*sws
= ss
->sws
;
439 struct svga_transfer
*st
= svga_transfer(transfer
);
442 sws
->buffer_unmap(sws
, st
->hwbuf
);
447 svga_texture_transfer_destroy(struct pipe_context
*pipe
,
448 struct pipe_transfer
*transfer
)
450 struct svga_context
*svga
= svga_context(pipe
);
451 struct svga_texture
*tex
= svga_texture(transfer
->resource
);
452 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
453 struct svga_winsys_screen
*sws
= ss
->sws
;
454 struct svga_transfer
*st
= svga_transfer(transfer
);
456 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
457 svga_transfer_dma(svga
, st
, SVGA3D_WRITE_HOST_VRAM
);
458 ss
->texture_timestamp
++;
459 tex
->view_age
[transfer
->level
] = ++(tex
->age
);
460 if (transfer
->resource
->target
== PIPE_TEXTURE_CUBE
)
461 tex
->defined
[transfer
->box
.z
][transfer
->level
] = TRUE
;
463 tex
->defined
[0][transfer
->level
] = TRUE
;
466 pipe_resource_reference(&st
->base
.resource
, NULL
);
468 sws
->buffer_destroy(sws
, st
->hwbuf
);
476 struct u_resource_vtbl svga_texture_vtbl
=
478 svga_texture_get_handle
, /* get_handle */
479 svga_texture_destroy
, /* resource_destroy */
480 svga_texture_is_referenced
, /* is_resource_referenced */
481 svga_texture_get_transfer
, /* get_transfer */
482 svga_texture_transfer_destroy
, /* transfer_destroy */
483 svga_texture_transfer_map
, /* transfer_map */
484 u_default_transfer_flush_region
, /* transfer_flush_region */
485 svga_texture_transfer_unmap
, /* transfer_unmap */
486 u_default_transfer_inline_write
/* transfer_inline_write */
492 struct pipe_resource
*
493 svga_texture_create(struct pipe_screen
*screen
,
494 const struct pipe_resource
*template)
496 struct svga_screen
*svgascreen
= svga_screen(screen
);
497 struct svga_texture
*tex
= CALLOC_STRUCT(svga_texture
);
502 tex
->b
.b
= *template;
503 tex
->b
.vtbl
= &svga_texture_vtbl
;
504 pipe_reference_init(&tex
->b
.b
.reference
, 1);
505 tex
->b
.b
.screen
= screen
;
507 assert(template->last_level
< SVGA_MAX_TEXTURE_LEVELS
);
508 if(template->last_level
>= SVGA_MAX_TEXTURE_LEVELS
)
512 tex
->key
.size
.width
= template->width0
;
513 tex
->key
.size
.height
= template->height0
;
514 tex
->key
.size
.depth
= template->depth0
;
516 if(template->target
== PIPE_TEXTURE_CUBE
) {
517 tex
->key
.flags
|= SVGA3D_SURFACE_CUBEMAP
;
518 tex
->key
.numFaces
= 6;
521 tex
->key
.numFaces
= 1;
524 /* XXX: Disabled for now */
525 tex
->key
.cachable
= 0;
527 if (template->bind
& PIPE_BIND_SAMPLER_VIEW
)
528 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_TEXTURE
;
530 if (template->bind
& PIPE_BIND_DISPLAY_TARGET
) {
531 tex
->key
.cachable
= 0;
534 if (template->bind
& PIPE_BIND_SHARED
) {
535 tex
->key
.cachable
= 0;
538 if (template->bind
& PIPE_BIND_SCANOUT
) {
539 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_SCANOUT
;
540 tex
->key
.cachable
= 0;
544 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
545 * know beforehand whether a texture will be used as a rendertarget or not
546 * and it always requests PIPE_BIND_RENDER_TARGET, therefore
547 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
550 if((template->bind
& PIPE_BIND_RENDER_TARGET
) &&
551 !util_format_is_s3tc(template->format
))
552 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
555 if(template->bind
& PIPE_BIND_DEPTH_STENCIL
)
556 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
558 tex
->key
.numMipLevels
= template->last_level
+ 1;
560 tex
->key
.format
= svga_translate_format(template->format
);
561 if(tex
->key
.format
== SVGA3D_FORMAT_INVALID
)
564 SVGA_DBG(DEBUG_DMA
, "surface_create for texture\n", tex
->handle
);
565 tex
->handle
= svga_screen_surface_create(svgascreen
, &tex
->key
);
567 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture)\n", tex
->handle
);
569 debug_reference(&tex
->b
.b
.reference
,
570 (debug_reference_descriptor
)debug_describe_resource
, 0);
583 struct pipe_resource
*
584 svga_texture_from_handle(struct pipe_screen
*screen
,
585 const struct pipe_resource
*template,
586 struct winsys_handle
*whandle
)
588 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
589 struct svga_winsys_surface
*srf
;
590 struct svga_texture
*tex
;
591 enum SVGA3dSurfaceFormat format
= 0;
594 /* Only supports one type */
595 if ((template->target
!= PIPE_TEXTURE_2D
&&
596 template->target
!= PIPE_TEXTURE_RECT
) ||
597 template->last_level
!= 0 ||
598 template->depth0
!= 1) {
602 srf
= sws
->surface_from_handle(sws
, whandle
, &format
);
607 if (svga_translate_format(template->format
) != format
) {
608 unsigned f1
= svga_translate_format(template->format
);
609 unsigned f2
= format
;
611 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
612 if ( !( (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
613 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
614 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ) ) {
615 debug_printf("%s wrong format %u != %u\n", __FUNCTION__
, f1
, f2
);
620 tex
= CALLOC_STRUCT(svga_texture
);
624 tex
->b
.b
= *template;
625 tex
->b
.vtbl
= &svga_texture_vtbl
;
626 pipe_reference_init(&tex
->b
.b
.reference
, 1);
627 tex
->b
.b
.screen
= screen
;
629 if (format
== SVGA3D_X8R8G8B8
)
630 tex
->b
.b
.format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
631 else if (format
== SVGA3D_A8R8G8B8
)
632 tex
->b
.b
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
637 SVGA_DBG(DEBUG_DMA
, "wrap surface sid %p\n", srf
);
639 tex
->key
.cachable
= 0;