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 **********************************************************/
26 #include "svga3d_reg.h"
27 #include "svga3d_surfacedefs.h"
29 #include "pipe/p_state.h"
30 #include "pipe/p_defines.h"
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "util/u_inlines.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "util/u_resource.h"
39 #include "svga_format.h"
40 #include "svga_screen.h"
41 #include "svga_context.h"
42 #include "svga_resource_texture.h"
43 #include "svga_resource_buffer.h"
44 #include "svga_sampler_view.h"
45 #include "svga_winsys.h"
46 #include "svga_debug.h"
50 svga_transfer_dma_band(struct svga_context
*svga
,
51 struct svga_transfer
*st
,
52 SVGA3dTransferType transfer
,
53 unsigned y
, unsigned h
, unsigned srcy
,
54 SVGA3dSurfaceDMAFlags flags
)
56 struct svga_texture
*texture
= svga_texture(st
->base
.resource
);
60 assert(!st
->use_direct_map
);
62 box
.x
= st
->base
.box
.x
;
64 box
.z
= st
->base
.box
.z
;
65 box
.w
= st
->base
.box
.width
;
72 SVGA_DBG(DEBUG_DMA
, "dma %s sid %p, face %u, (%u, %u, %u) - "
73 "(%u, %u, %u), %ubpp\n",
74 transfer
== SVGA3D_WRITE_HOST_VRAM
? "to" : "from",
80 st
->base
.box
.x
+ st
->base
.box
.width
,
83 util_format_get_blocksize(texture
->b
.b
.format
) * 8 /
84 (util_format_get_blockwidth(texture
->b
.b
.format
)
85 * util_format_get_blockheight(texture
->b
.b
.format
)));
87 ret
= SVGA3D_SurfaceDMA(svga
->swc
, st
, transfer
, &box
, 1, flags
);
89 svga_context_flush(svga
, NULL
);
90 ret
= SVGA3D_SurfaceDMA(svga
->swc
, st
, transfer
, &box
, 1, flags
);
91 assert(ret
== PIPE_OK
);
97 svga_transfer_dma(struct svga_context
*svga
,
98 struct svga_transfer
*st
,
99 SVGA3dTransferType transfer
,
100 SVGA3dSurfaceDMAFlags flags
)
102 struct svga_texture
*texture
= svga_texture(st
->base
.resource
);
103 struct svga_screen
*screen
= svga_screen(texture
->b
.b
.screen
);
104 struct svga_winsys_screen
*sws
= screen
->sws
;
105 struct pipe_fence_handle
*fence
= NULL
;
107 assert(!st
->use_direct_map
);
109 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
110 SVGA_DBG(DEBUG_PERF
, "%s: readback transfer\n", __FUNCTION__
);
113 /* Ensure any pending operations on host surfaces are queued on the command
116 svga_surfaces_flush( svga
);
119 /* Do the DMA transfer in a single go */
120 svga_transfer_dma_band(svga
, st
, transfer
,
121 st
->base
.box
.y
, st
->base
.box
.height
, 0,
124 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
125 svga_context_flush(svga
, &fence
);
126 sws
->fence_finish(sws
, fence
, 0);
127 sws
->fence_reference(sws
, &fence
, NULL
);
132 unsigned blockheight
=
133 util_format_get_blockheight(st
->base
.resource
->format
);
135 h
= st
->hw_nblocksy
* blockheight
;
138 for (y
= 0; y
< st
->base
.box
.height
; y
+= h
) {
139 unsigned offset
, length
;
142 if (y
+ h
> st
->base
.box
.height
)
143 h
= st
->base
.box
.height
- y
;
145 /* Transfer band must be aligned to pixel block boundaries */
146 assert(y
% blockheight
== 0);
147 assert(h
% blockheight
== 0);
149 offset
= y
* st
->base
.stride
/ blockheight
;
150 length
= h
* st
->base
.stride
/ blockheight
;
152 sw
= (uint8_t *) st
->swbuf
+ offset
;
154 if (transfer
== SVGA3D_WRITE_HOST_VRAM
) {
155 unsigned usage
= PIPE_TRANSFER_WRITE
;
157 /* Wait for the previous DMAs to complete */
158 /* TODO: keep one DMA (at half the size) in the background */
160 svga_context_flush(svga
, NULL
);
161 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
164 hw
= sws
->buffer_map(sws
, st
->hwbuf
, usage
);
167 memcpy(hw
, sw
, length
);
168 sws
->buffer_unmap(sws
, st
->hwbuf
);
172 svga_transfer_dma_band(svga
, st
, transfer
, y
, h
, srcy
, flags
);
175 * Prevent the texture contents to be discarded on the next band
178 flags
.discard
= FALSE
;
180 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
181 svga_context_flush(svga
, &fence
);
182 sws
->fence_finish(sws
, fence
, 0);
184 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_TRANSFER_READ
);
187 memcpy(sw
, hw
, length
);
188 sws
->buffer_unmap(sws
, st
->hwbuf
);
197 svga_texture_get_handle(struct pipe_screen
*screen
,
198 struct pipe_resource
*texture
,
199 struct winsys_handle
*whandle
)
201 struct svga_winsys_screen
*sws
= svga_winsys_screen(texture
->screen
);
204 assert(svga_texture(texture
)->key
.cachable
== 0);
205 svga_texture(texture
)->key
.cachable
= 0;
207 stride
= util_format_get_nblocksx(texture
->format
, texture
->width0
) *
208 util_format_get_blocksize(texture
->format
);
210 return sws
->surface_get_handle(sws
, svga_texture(texture
)->handle
,
216 svga_texture_destroy(struct pipe_screen
*screen
,
217 struct pipe_resource
*pt
)
219 struct svga_screen
*ss
= svga_screen(screen
);
220 struct svga_texture
*tex
= svga_texture(pt
);
222 ss
->texture_timestamp
++;
224 svga_sampler_view_reference(&tex
->cached_view
, NULL
);
227 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
229 SVGA_DBG(DEBUG_DMA
, "unref sid %p (texture)\n", tex
->handle
);
230 svga_screen_surface_destroy(ss
, &tex
->key
, &tex
->handle
);
232 ss
->total_resource_bytes
-= tex
->size
;
235 FREE(tex
->rendered_to
);
241 * Determine if we need to read back a texture image before mapping it.
244 need_tex_readback(struct pipe_transfer
*transfer
)
246 struct svga_texture
*t
= svga_texture(transfer
->resource
);
248 if (transfer
->usage
& PIPE_TRANSFER_READ
)
251 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) &&
252 ((transfer
->usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) == 0)) {
255 if (transfer
->resource
->target
== PIPE_TEXTURE_CUBE
) {
256 assert(transfer
->box
.depth
== 1);
257 face
= transfer
->box
.z
;
262 if (svga_was_texture_rendered_to(t
, face
, transfer
->level
)) {
271 static enum pipe_error
272 readback_image_vgpu9(struct svga_context
*svga
,
273 struct svga_winsys_surface
*surf
,
279 ret
= SVGA3D_ReadbackGBImage(svga
->swc
, surf
, slice
, level
);
280 if (ret
!= PIPE_OK
) {
281 svga_context_flush(svga
, NULL
);
282 ret
= SVGA3D_ReadbackGBImage(svga
->swc
, surf
, slice
, level
);
288 static enum pipe_error
289 readback_image_vgpu10(struct svga_context
*svga
,
290 struct svga_winsys_surface
*surf
,
293 unsigned numMipLevels
)
296 unsigned subResource
;
298 subResource
= slice
* numMipLevels
+ level
;
299 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, surf
, subResource
);
300 if (ret
!= PIPE_OK
) {
301 svga_context_flush(svga
, NULL
);
302 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, surf
, subResource
);
309 svga_texture_transfer_map(struct pipe_context
*pipe
,
310 struct pipe_resource
*texture
,
313 const struct pipe_box
*box
,
314 struct pipe_transfer
**ptransfer
)
316 struct svga_context
*svga
= svga_context(pipe
);
317 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
318 struct svga_winsys_screen
*sws
= ss
->sws
;
319 struct svga_texture
*tex
= svga_texture(texture
);
320 struct svga_transfer
*st
;
321 unsigned nblocksx
, nblocksy
;
322 boolean use_direct_map
= svga_have_gb_objects(svga
) &&
323 !svga_have_gb_dma(svga
);
326 /* We can't map texture storage directly unless we have GB objects */
327 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
) {
328 if (svga_have_gb_objects(svga
))
329 use_direct_map
= TRUE
;
334 st
= CALLOC_STRUCT(svga_transfer
);
340 if (use_direct_map
) {
341 /* we'll directly access the guest-backed surface */
342 w
= u_minify(texture
->width0
, level
);
343 h
= u_minify(texture
->height0
, level
);
344 d
= u_minify(texture
->depth0
, level
);
347 /* we'll put the data into a tightly packed buffer */
352 nblocksx
= util_format_get_nblocksx(texture
->format
, w
);
353 nblocksy
= util_format_get_nblocksy(texture
->format
, h
);
356 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
= st
->base
.stride
* nblocksy
;
364 switch (tex
->b
.b
.target
) {
365 case PIPE_TEXTURE_CUBE
:
366 case PIPE_TEXTURE_2D_ARRAY
:
367 case PIPE_TEXTURE_1D_ARRAY
:
368 st
->slice
= st
->base
.box
.z
;
369 st
->base
.box
.z
= 0; /* so we don't apply double offsets below */
376 if (!use_direct_map
) {
377 /* Use a DMA buffer */
378 st
->hw_nblocksy
= nblocksy
;
380 st
->hwbuf
= svga_winsys_buffer_create(svga
, 1, 0,
381 st
->hw_nblocksy
* st
->base
.stride
* d
);
382 while(!st
->hwbuf
&& (st
->hw_nblocksy
/= 2)) {
383 st
->hwbuf
= svga_winsys_buffer_create(svga
, 1, 0,
384 st
->hw_nblocksy
* st
->base
.stride
* d
);
392 if (st
->hw_nblocksy
< nblocksy
) {
393 /* We couldn't allocate a hardware buffer big enough for the transfer,
394 * so allocate regular malloc memory instead */
396 debug_printf("%s: failed to allocate %u KB of DMA, "
397 "splitting into %u x %u KB DMA transfers\n",
399 (nblocksy
*st
->base
.stride
+ 1023)/1024,
400 (nblocksy
+ st
->hw_nblocksy
- 1)/st
->hw_nblocksy
,
401 (st
->hw_nblocksy
*st
->base
.stride
+ 1023)/1024);
404 st
->swbuf
= MALLOC(nblocksy
* st
->base
.stride
* d
);
406 sws
->buffer_destroy(sws
, st
->hwbuf
);
412 if (usage
& PIPE_TRANSFER_READ
) {
413 SVGA3dSurfaceDMAFlags flags
;
414 memset(&flags
, 0, sizeof flags
);
415 svga_transfer_dma(svga
, st
, SVGA3D_READ_HOST_VRAM
, flags
);
418 struct pipe_transfer
*transfer
= &st
->base
;
419 struct svga_winsys_surface
*surf
= tex
->handle
;
426 if (need_tex_readback(transfer
)) {
429 svga_surfaces_flush(svga
);
431 if (svga_have_vgpu10(svga
)) {
432 ret
= readback_image_vgpu10(svga
, surf
, st
->slice
, transfer
->level
,
433 tex
->b
.b
.last_level
+ 1);
435 ret
= readback_image_vgpu9(svga
, surf
, st
->slice
, transfer
->level
);
438 assert(ret
== PIPE_OK
);
441 svga_context_flush(svga
, NULL
);
444 * Note: if PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE were specified
445 * we could potentially clear the flag for all faces/layers/mips.
447 svga_clear_texture_rendered_to(tex
, st
->slice
, transfer
->level
);
450 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
451 if ((transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) == 0) {
452 svga_surfaces_flush(svga
);
453 if (!sws
->surface_is_flushed(sws
, surf
))
454 svga_context_flush(svga
, NULL
);
459 st
->use_direct_map
= use_direct_map
;
461 *ptransfer
= &st
->base
;
469 else if (!st
->use_direct_map
) {
470 return sws
->buffer_map(sws
, st
->hwbuf
, usage
);
473 SVGA3dSize baseLevelSize
;
474 struct svga_texture
*tex
= svga_texture(texture
);
475 struct svga_winsys_surface
*surf
= tex
->handle
;
478 unsigned offset
, mip_width
, mip_height
;
479 unsigned xoffset
= st
->base
.box
.x
;
480 unsigned yoffset
= st
->base
.box
.y
;
481 unsigned zoffset
= st
->base
.box
.z
;
483 map
= svga
->swc
->surface_map(svga
->swc
, surf
, usage
, &retry
);
484 if (map
== NULL
&& retry
) {
486 * At this point, the svga_surfaces_flush() should already have
487 * called in svga_texture_get_transfer().
489 svga_context_flush(svga
, NULL
);
490 map
= svga
->swc
->surface_map(svga
->swc
, surf
, usage
, &retry
);
494 * Make sure we return NULL if the map fails
502 * Compute the offset to the specific texture slice in the buffer.
504 baseLevelSize
.width
= tex
->b
.b
.width0
;
505 baseLevelSize
.height
= tex
->b
.b
.height0
;
506 baseLevelSize
.depth
= tex
->b
.b
.depth0
;
508 offset
= svga3dsurface_get_image_offset(tex
->key
.format
, baseLevelSize
,
509 tex
->b
.b
.last_level
+ 1, /* numMips */
515 mip_width
= u_minify(tex
->b
.b
.width0
, level
);
516 mip_height
= u_minify(tex
->b
.b
.height0
, level
);
518 offset
+= svga3dsurface_get_pixel_offset(tex
->key
.format
,
519 mip_width
, mip_height
,
520 xoffset
, yoffset
, zoffset
);
522 return (void *) (map
+ offset
);
528 * Unmap a GB texture surface.
531 svga_texture_surface_unmap(struct svga_context
*svga
,
532 struct pipe_transfer
*transfer
)
534 struct svga_winsys_surface
*surf
= svga_texture(transfer
->resource
)->handle
;
535 struct svga_winsys_context
*swc
= svga
->swc
;
540 swc
->surface_unmap(swc
, surf
, &rebind
);
543 ret
= SVGA3D_BindGBSurface(swc
, surf
);
544 if (ret
!= PIPE_OK
) {
545 /* flush and retry */
546 svga_context_flush(svga
, NULL
);
547 ret
= SVGA3D_BindGBSurface(swc
, surf
);
548 assert(ret
== PIPE_OK
);
554 static enum pipe_error
555 update_image_vgpu9(struct svga_context
*svga
,
556 struct svga_winsys_surface
*surf
,
557 const SVGA3dBox
*box
,
563 ret
= SVGA3D_UpdateGBImage(svga
->swc
, surf
, box
, slice
, level
);
564 if (ret
!= PIPE_OK
) {
565 svga_context_flush(svga
, NULL
);
566 ret
= SVGA3D_UpdateGBImage(svga
->swc
, surf
, box
, slice
, level
);
572 static enum pipe_error
573 update_image_vgpu10(struct svga_context
*svga
,
574 struct svga_winsys_surface
*surf
,
575 const SVGA3dBox
*box
,
578 unsigned numMipLevels
)
581 unsigned subResource
;
583 subResource
= slice
* numMipLevels
+ level
;
584 ret
= SVGA3D_vgpu10_UpdateSubResource(svga
->swc
, surf
, box
, subResource
);
585 if (ret
!= PIPE_OK
) {
586 svga_context_flush(svga
, NULL
);
587 ret
= SVGA3D_vgpu10_UpdateSubResource(svga
->swc
, surf
, box
, subResource
);
594 svga_texture_transfer_unmap(struct pipe_context
*pipe
,
595 struct pipe_transfer
*transfer
)
597 struct svga_context
*svga
= svga_context(pipe
);
598 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
599 struct svga_winsys_screen
*sws
= ss
->sws
;
600 struct svga_transfer
*st
= svga_transfer(transfer
);
601 struct svga_texture
*tex
= svga_texture(transfer
->resource
);
604 if (st
->use_direct_map
) {
605 svga_texture_surface_unmap(svga
, transfer
);
608 sws
->buffer_unmap(sws
, st
->hwbuf
);
612 if (!st
->use_direct_map
&& (st
->base
.usage
& PIPE_TRANSFER_WRITE
)) {
613 /* Use DMA to transfer texture data */
614 SVGA3dSurfaceDMAFlags flags
;
616 memset(&flags
, 0, sizeof flags
);
617 if (transfer
->usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
618 flags
.discard
= TRUE
;
620 if (transfer
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
621 flags
.unsynchronized
= TRUE
;
624 svga_transfer_dma(svga
, st
, SVGA3D_WRITE_HOST_VRAM
, flags
);
625 } else if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
626 struct svga_winsys_surface
*surf
=
627 svga_texture(transfer
->resource
)->handle
;
631 assert(svga_have_gb_objects(svga
));
633 /* update the effected region */
634 box
.x
= transfer
->box
.x
;
635 box
.y
= transfer
->box
.y
;
636 switch (tex
->b
.b
.target
) {
637 case PIPE_TEXTURE_CUBE
:
638 case PIPE_TEXTURE_2D_ARRAY
:
641 case PIPE_TEXTURE_1D_ARRAY
:
645 box
.z
= transfer
->box
.z
;
648 box
.w
= transfer
->box
.width
;
649 box
.h
= transfer
->box
.height
;
650 box
.d
= transfer
->box
.depth
;
653 debug_printf("%s %d, %d, %d %d x %d x %d\n",
656 box
.w
, box
.h
, box
.d
);
658 if (svga_have_vgpu10(svga
)) {
659 ret
= update_image_vgpu10(svga
, surf
, &box
, st
->slice
, transfer
->level
,
660 tex
->b
.b
.last_level
+ 1);
662 ret
= update_image_vgpu9(svga
, surf
, &box
, st
->slice
, transfer
->level
);
665 assert(ret
== PIPE_OK
);
669 ss
->texture_timestamp
++;
670 svga_age_texture_view(tex
, transfer
->level
);
671 if (transfer
->resource
->target
== PIPE_TEXTURE_CUBE
)
672 svga_define_texture_level(tex
, st
->slice
, transfer
->level
);
674 svga_define_texture_level(tex
, 0, transfer
->level
);
676 pipe_resource_reference(&st
->base
.resource
, NULL
);
679 if (!st
->use_direct_map
) {
680 sws
->buffer_destroy(sws
, st
->hwbuf
);
687 * Does format store depth values?
689 static inline boolean
690 format_has_depth(enum pipe_format format
)
692 const struct util_format_description
*desc
= util_format_description(format
);
693 return util_format_has_depth(desc
);
697 struct u_resource_vtbl svga_texture_vtbl
=
699 svga_texture_get_handle
, /* get_handle */
700 svga_texture_destroy
, /* resource_destroy */
701 svga_texture_transfer_map
, /* transfer_map */
702 u_default_transfer_flush_region
, /* transfer_flush_region */
703 svga_texture_transfer_unmap
, /* transfer_unmap */
704 u_default_transfer_inline_write
/* transfer_inline_write */
708 struct pipe_resource
*
709 svga_texture_create(struct pipe_screen
*screen
,
710 const struct pipe_resource
*template)
712 struct svga_screen
*svgascreen
= svga_screen(screen
);
713 struct svga_texture
*tex
;
714 unsigned bindings
= template->bind
;
716 assert(template->last_level
< SVGA_MAX_TEXTURE_LEVELS
);
717 if (template->last_level
>= SVGA_MAX_TEXTURE_LEVELS
) {
721 tex
= CALLOC_STRUCT(svga_texture
);
726 tex
->defined
= CALLOC(template->depth0
* template->array_size
,
727 sizeof(tex
->defined
[0]));
733 tex
->rendered_to
= CALLOC(template->depth0
* template->array_size
,
734 sizeof(tex
->rendered_to
[0]));
735 if (!tex
->rendered_to
) {
741 tex
->b
.b
= *template;
742 tex
->b
.vtbl
= &svga_texture_vtbl
;
743 pipe_reference_init(&tex
->b
.b
.reference
, 1);
744 tex
->b
.b
.screen
= screen
;
747 tex
->key
.size
.width
= template->width0
;
748 tex
->key
.size
.height
= template->height0
;
749 tex
->key
.size
.depth
= template->depth0
;
750 tex
->key
.arraySize
= 1;
751 tex
->key
.numFaces
= 1;
752 tex
->key
.sampleCount
= template->nr_samples
;
754 if (template->nr_samples
> 1) {
755 tex
->key
.flags
|= SVGA3D_SURFACE_MASKABLE_ANTIALIAS
;
758 if (svgascreen
->sws
->have_vgpu10
) {
759 switch (template->target
) {
760 case PIPE_TEXTURE_1D
:
761 tex
->key
.flags
|= SVGA3D_SURFACE_1D
;
763 case PIPE_TEXTURE_1D_ARRAY
:
764 tex
->key
.flags
|= SVGA3D_SURFACE_1D
;
766 case PIPE_TEXTURE_2D_ARRAY
:
767 tex
->key
.flags
|= SVGA3D_SURFACE_ARRAY
;
768 tex
->key
.arraySize
= template->array_size
;
770 case PIPE_TEXTURE_3D
:
771 tex
->key
.flags
|= SVGA3D_SURFACE_VOLUME
;
773 case PIPE_TEXTURE_CUBE
:
774 tex
->key
.flags
|= (SVGA3D_SURFACE_CUBEMAP
| SVGA3D_SURFACE_ARRAY
);
775 tex
->key
.numFaces
= 6;
782 switch (template->target
) {
783 case PIPE_TEXTURE_3D
:
784 tex
->key
.flags
|= SVGA3D_SURFACE_VOLUME
;
786 case PIPE_TEXTURE_CUBE
:
787 tex
->key
.flags
|= SVGA3D_SURFACE_CUBEMAP
;
788 tex
->key
.numFaces
= 6;
795 tex
->key
.cachable
= 1;
797 if (bindings
& PIPE_BIND_SAMPLER_VIEW
) {
798 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_TEXTURE
;
799 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_SHADER_RESOURCE
;
801 if (!(bindings
& PIPE_BIND_RENDER_TARGET
)) {
802 /* Also check if the format is renderable */
803 if (screen
->is_format_supported(screen
, template->format
,
805 template->nr_samples
,
806 PIPE_BIND_RENDER_TARGET
)) {
807 bindings
|= PIPE_BIND_RENDER_TARGET
;
812 if (bindings
& PIPE_BIND_DISPLAY_TARGET
) {
813 tex
->key
.cachable
= 0;
816 if (bindings
& PIPE_BIND_SHARED
) {
817 tex
->key
.cachable
= 0;
820 if (bindings
& (PIPE_BIND_SCANOUT
| PIPE_BIND_CURSOR
)) {
821 tex
->key
.scanout
= 1;
822 tex
->key
.cachable
= 0;
826 * Note: Previously we never passed the
827 * SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
828 * know beforehand whether a texture will be used as a rendertarget or not
829 * and it always requests PIPE_BIND_RENDER_TARGET, therefore
830 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
832 * However, this was changed since other state trackers
833 * (XA for example) uses it accurately and certain device versions
834 * relies on it in certain situations to render correctly.
836 if ((bindings
& PIPE_BIND_RENDER_TARGET
) &&
837 !util_format_is_s3tc(template->format
)) {
838 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
839 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_RENDER_TARGET
;
842 if (bindings
& PIPE_BIND_DEPTH_STENCIL
) {
843 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
844 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_DEPTH_STENCIL
;
847 tex
->key
.numMipLevels
= template->last_level
+ 1;
849 tex
->key
.format
= svga_translate_format(svgascreen
, template->format
,
851 if (tex
->key
.format
== SVGA3D_FORMAT_INVALID
) {
853 FREE(tex
->rendered_to
);
858 /* Use typeless formats for sRGB and depth resources. Typeless
859 * formats can be reinterpreted as other formats. For example,
860 * SVGA3D_R8G8B8A8_UNORM_TYPELESS can be interpreted as
861 * SVGA3D_R8G8B8A8_UNORM_SRGB or SVGA3D_R8G8B8A8_UNORM.
863 if (svgascreen
->sws
->have_vgpu10
&&
864 (util_format_is_srgb(template->format
) ||
865 format_has_depth(template->format
))) {
866 SVGA3dSurfaceFormat typeless
= svga_typeless_format(tex
->key
.format
);
868 debug_printf("Convert resource type %s -> %s (bind 0x%x)\n",
869 svga_format_name(tex
->key
.format
),
870 svga_format_name(typeless
),
873 tex
->key
.format
= typeless
;
876 SVGA_DBG(DEBUG_DMA
, "surface_create for texture\n", tex
->handle
);
877 tex
->handle
= svga_screen_surface_create(svgascreen
, bindings
,
878 tex
->b
.b
.usage
, &tex
->key
);
881 FREE(tex
->rendered_to
);
886 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture)\n", tex
->handle
);
888 debug_reference(&tex
->b
.b
.reference
,
889 (debug_reference_descriptor
)debug_describe_resource
, 0);
891 tex
->size
= util_resource_size(template);
892 svgascreen
->total_resource_bytes
+= tex
->size
;
898 struct pipe_resource
*
899 svga_texture_from_handle(struct pipe_screen
*screen
,
900 const struct pipe_resource
*template,
901 struct winsys_handle
*whandle
)
903 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
904 struct svga_winsys_surface
*srf
;
905 struct svga_texture
*tex
;
906 enum SVGA3dSurfaceFormat format
= 0;
909 /* Only supports one type */
910 if ((template->target
!= PIPE_TEXTURE_2D
&&
911 template->target
!= PIPE_TEXTURE_RECT
) ||
912 template->last_level
!= 0 ||
913 template->depth0
!= 1) {
917 srf
= sws
->surface_from_handle(sws
, whandle
, &format
);
922 if (svga_translate_format(svga_screen(screen
), template->format
,
923 template->bind
) != format
) {
924 unsigned f1
= svga_translate_format(svga_screen(screen
),
925 template->format
, template->bind
);
926 unsigned f2
= format
;
928 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up.
930 if (f1
== SVGA3D_B8G8R8A8_UNORM
)
931 f1
= SVGA3D_A8R8G8B8
;
932 if (f1
== SVGA3D_B8G8R8X8_UNORM
)
933 f1
= SVGA3D_X8R8G8B8
;
935 if ( !( (f1
== f2
) ||
936 (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
937 (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_B8G8R8X8_UNORM
) ||
938 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
939 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_B8G8R8A8_UNORM
) ||
940 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ||
941 (f1
== SVGA3D_Z_DF24
&& f2
== SVGA3D_Z_D24S8_INT
) ) ) {
942 debug_printf("%s wrong format %s != %s\n", __FUNCTION__
,
943 svga_format_name(f1
), svga_format_name(f2
));
948 tex
= CALLOC_STRUCT(svga_texture
);
952 tex
->defined
= CALLOC(template->depth0
* template->array_size
,
953 sizeof(tex
->defined
[0]));
959 tex
->b
.b
= *template;
960 tex
->b
.vtbl
= &svga_texture_vtbl
;
961 pipe_reference_init(&tex
->b
.b
.reference
, 1);
962 tex
->b
.b
.screen
= screen
;
964 SVGA_DBG(DEBUG_DMA
, "wrap surface sid %p\n", srf
);
966 tex
->key
.cachable
= 0;
967 tex
->key
.format
= format
;
970 tex
->rendered_to
= CALLOC(1, sizeof(tex
->rendered_to
[0]));
971 tex
->imported
= TRUE
;