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 x
, unsigned y
, unsigned z
,
54 unsigned w
, unsigned h
, unsigned d
,
55 unsigned srcx
, unsigned srcy
, unsigned srcz
,
56 SVGA3dSurfaceDMAFlags flags
)
58 struct svga_texture
*texture
= svga_texture(st
->base
.resource
);
62 assert(!st
->use_direct_map
);
74 SVGA_DBG(DEBUG_DMA
, "dma %s sid %p, face %u, (%u, %u, %u) - "
75 "(%u, %u, %u), %ubpp\n",
76 transfer
== SVGA3D_WRITE_HOST_VRAM
? "to" : "from",
85 util_format_get_blocksize(texture
->b
.b
.format
) * 8 /
86 (util_format_get_blockwidth(texture
->b
.b
.format
)
87 * util_format_get_blockheight(texture
->b
.b
.format
)));
89 ret
= SVGA3D_SurfaceDMA(svga
->swc
, st
, transfer
, &box
, 1, flags
);
91 svga_context_flush(svga
, NULL
);
92 ret
= SVGA3D_SurfaceDMA(svga
->swc
, st
, transfer
, &box
, 1, flags
);
93 assert(ret
== PIPE_OK
);
99 svga_transfer_dma(struct svga_context
*svga
,
100 struct svga_transfer
*st
,
101 SVGA3dTransferType transfer
,
102 SVGA3dSurfaceDMAFlags flags
)
104 struct svga_texture
*texture
= svga_texture(st
->base
.resource
);
105 struct svga_screen
*screen
= svga_screen(texture
->b
.b
.screen
);
106 struct svga_winsys_screen
*sws
= screen
->sws
;
107 struct pipe_fence_handle
*fence
= NULL
;
109 assert(!st
->use_direct_map
);
111 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
112 SVGA_DBG(DEBUG_PERF
, "%s: readback transfer\n", __FUNCTION__
);
115 /* Ensure any pending operations on host surfaces are queued on the command
118 svga_surfaces_flush( svga
);
121 /* Do the DMA transfer in a single go */
122 svga_transfer_dma_band(svga
, st
, transfer
,
123 st
->base
.box
.x
, st
->base
.box
.y
, st
->base
.box
.z
,
124 st
->base
.box
.width
, st
->base
.box
.height
, st
->base
.box
.depth
,
128 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
129 svga_context_flush(svga
, &fence
);
130 sws
->fence_finish(sws
, fence
, 0);
131 sws
->fence_reference(sws
, &fence
, NULL
);
136 unsigned blockheight
=
137 util_format_get_blockheight(st
->base
.resource
->format
);
139 h
= st
->hw_nblocksy
* blockheight
;
142 for (y
= 0; y
< st
->base
.box
.height
; y
+= h
) {
143 unsigned offset
, length
;
146 if (y
+ h
> st
->base
.box
.height
)
147 h
= st
->base
.box
.height
- y
;
149 /* Transfer band must be aligned to pixel block boundaries */
150 assert(y
% blockheight
== 0);
151 assert(h
% blockheight
== 0);
153 offset
= y
* st
->base
.stride
/ blockheight
;
154 length
= h
* st
->base
.stride
/ blockheight
;
156 sw
= (uint8_t *) st
->swbuf
+ offset
;
158 if (transfer
== SVGA3D_WRITE_HOST_VRAM
) {
159 unsigned usage
= PIPE_TRANSFER_WRITE
;
161 /* Wait for the previous DMAs to complete */
162 /* TODO: keep one DMA (at half the size) in the background */
164 svga_context_flush(svga
, NULL
);
165 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
168 hw
= sws
->buffer_map(sws
, st
->hwbuf
, usage
);
171 memcpy(hw
, sw
, length
);
172 sws
->buffer_unmap(sws
, st
->hwbuf
);
176 svga_transfer_dma_band(svga
, st
, transfer
,
177 st
->base
.box
.x
, y
, st
->base
.box
.z
,
178 st
->base
.box
.width
, h
, st
->base
.box
.depth
,
182 * Prevent the texture contents to be discarded on the next band
185 flags
.discard
= FALSE
;
187 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
188 svga_context_flush(svga
, &fence
);
189 sws
->fence_finish(sws
, fence
, 0);
191 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_TRANSFER_READ
);
194 memcpy(sw
, hw
, length
);
195 sws
->buffer_unmap(sws
, st
->hwbuf
);
205 svga_texture_get_handle(struct pipe_screen
*screen
,
206 struct pipe_resource
*texture
,
207 struct winsys_handle
*whandle
)
209 struct svga_winsys_screen
*sws
= svga_winsys_screen(texture
->screen
);
212 assert(svga_texture(texture
)->key
.cachable
== 0);
213 svga_texture(texture
)->key
.cachable
= 0;
215 stride
= util_format_get_nblocksx(texture
->format
, texture
->width0
) *
216 util_format_get_blocksize(texture
->format
);
218 return sws
->surface_get_handle(sws
, svga_texture(texture
)->handle
,
224 svga_texture_destroy(struct pipe_screen
*screen
,
225 struct pipe_resource
*pt
)
227 struct svga_screen
*ss
= svga_screen(screen
);
228 struct svga_texture
*tex
= svga_texture(pt
);
230 ss
->texture_timestamp
++;
232 svga_sampler_view_reference(&tex
->cached_view
, NULL
);
235 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
237 SVGA_DBG(DEBUG_DMA
, "unref sid %p (texture)\n", tex
->handle
);
238 svga_screen_surface_destroy(ss
, &tex
->key
, &tex
->handle
);
240 ss
->hud
.total_resource_bytes
-= tex
->size
;
243 FREE(tex
->rendered_to
);
247 assert(ss
->hud
.num_resources
> 0);
248 if (ss
->hud
.num_resources
> 0)
249 ss
->hud
.num_resources
--;
254 * Determine if we need to read back a texture image before mapping it.
257 need_tex_readback(struct pipe_transfer
*transfer
)
259 struct svga_texture
*t
= svga_texture(transfer
->resource
);
261 if (transfer
->usage
& PIPE_TRANSFER_READ
)
264 if ((transfer
->usage
& PIPE_TRANSFER_WRITE
) &&
265 ((transfer
->usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) == 0)) {
268 if (transfer
->resource
->target
== PIPE_TEXTURE_CUBE
) {
269 assert(transfer
->box
.depth
== 1);
270 face
= transfer
->box
.z
;
275 if (svga_was_texture_rendered_to(t
, face
, transfer
->level
)) {
284 static enum pipe_error
285 readback_image_vgpu9(struct svga_context
*svga
,
286 struct svga_winsys_surface
*surf
,
292 ret
= SVGA3D_ReadbackGBImage(svga
->swc
, surf
, slice
, level
);
293 if (ret
!= PIPE_OK
) {
294 svga_context_flush(svga
, NULL
);
295 ret
= SVGA3D_ReadbackGBImage(svga
->swc
, surf
, slice
, level
);
301 static enum pipe_error
302 readback_image_vgpu10(struct svga_context
*svga
,
303 struct svga_winsys_surface
*surf
,
306 unsigned numMipLevels
)
309 unsigned subResource
;
311 subResource
= slice
* numMipLevels
+ level
;
312 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, surf
, subResource
);
313 if (ret
!= PIPE_OK
) {
314 svga_context_flush(svga
, NULL
);
315 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, surf
, subResource
);
322 * Use DMA for the transfer request
325 svga_texture_transfer_map_dma(struct svga_context
*svga
,
326 struct svga_transfer
*st
)
328 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
329 struct pipe_resource
*texture
= st
->base
.resource
;
330 unsigned nblocksx
, nblocksy
;
332 unsigned usage
= st
->base
.usage
;
334 /* we'll put the data into a tightly packed buffer */
335 nblocksx
= util_format_get_nblocksx(texture
->format
, st
->base
.box
.width
);
336 nblocksy
= util_format_get_nblocksy(texture
->format
, st
->base
.box
.height
);
337 d
= st
->base
.box
.depth
;
339 st
->base
.stride
= nblocksx
*util_format_get_blocksize(texture
->format
);
340 st
->base
.layer_stride
= st
->base
.stride
* nblocksy
;
341 st
->hw_nblocksy
= nblocksy
;
343 st
->hwbuf
= svga_winsys_buffer_create(svga
, 1, 0,
344 st
->hw_nblocksy
* st
->base
.stride
* d
);
346 while (!st
->hwbuf
&& (st
->hw_nblocksy
/= 2)) {
348 svga_winsys_buffer_create(svga
, 1, 0,
349 st
->hw_nblocksy
* st
->base
.stride
* d
);
355 if (st
->hw_nblocksy
< nblocksy
) {
356 /* We couldn't allocate a hardware buffer big enough for the transfer,
357 * so allocate regular malloc memory instead
360 debug_printf("%s: failed to allocate %u KB of DMA, "
361 "splitting into %u x %u KB DMA transfers\n",
363 (nblocksy
* st
->base
.stride
+ 1023) / 1024,
364 (nblocksy
+ st
->hw_nblocksy
- 1) / st
->hw_nblocksy
,
365 (st
->hw_nblocksy
* st
->base
.stride
+ 1023) / 1024);
368 st
->swbuf
= MALLOC(nblocksy
* st
->base
.stride
* d
);
370 sws
->buffer_destroy(sws
, st
->hwbuf
);
375 if (usage
& PIPE_TRANSFER_READ
) {
376 SVGA3dSurfaceDMAFlags flags
;
377 memset(&flags
, 0, sizeof flags
);
378 svga_transfer_dma(svga
, st
, SVGA3D_READ_HOST_VRAM
, flags
);
385 return sws
->buffer_map(sws
, st
->hwbuf
, usage
);
391 * Use direct map for the transfer request
394 svga_texture_transfer_map_direct(struct svga_context
*svga
,
395 struct svga_transfer
*st
)
397 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
398 struct pipe_transfer
*transfer
= &st
->base
;
399 struct pipe_resource
*texture
= transfer
->resource
;
400 struct svga_texture
*tex
= svga_texture(texture
);
401 struct svga_winsys_surface
*surf
= tex
->handle
;
402 unsigned level
= st
->base
.level
;
403 unsigned w
, h
, nblocksx
, nblocksy
;
404 unsigned usage
= st
->base
.usage
;
411 /* we'll directly access the guest-backed surface */
412 w
= u_minify(texture
->width0
, level
);
413 h
= u_minify(texture
->height0
, level
);
414 nblocksx
= util_format_get_nblocksx(texture
->format
, w
);
415 nblocksy
= util_format_get_nblocksy(texture
->format
, h
);
416 st
->hw_nblocksy
= nblocksy
;
417 st
->base
.stride
= nblocksx
*util_format_get_blocksize(texture
->format
);
418 st
->base
.layer_stride
= st
->base
.stride
* nblocksy
;
420 /* If this is the first time mapping to the surface in this
421 * command buffer, clear the dirty masks of this surface.
423 if (sws
->surface_is_flushed(sws
, surf
)) {
424 svga_clear_texture_dirty(tex
);
427 if (need_tex_readback(transfer
)) {
430 svga_surfaces_flush(svga
);
432 if (svga_have_vgpu10(svga
)) {
433 ret
= readback_image_vgpu10(svga
, surf
, st
->slice
, level
,
434 tex
->b
.b
.last_level
+ 1);
436 ret
= readback_image_vgpu9(svga
, surf
, st
->slice
, level
);
439 svga
->hud
.num_readbacks
++;
440 SVGA_STATS_COUNT_INC(sws
, SVGA_STATS_COUNT_TEXREADBACK
);
442 assert(ret
== PIPE_OK
);
445 svga_context_flush(svga
, NULL
);
448 * Note: if PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE were specified
449 * we could potentially clear the flag for all faces/layers/mips.
451 svga_clear_texture_rendered_to(tex
, st
->slice
, level
);
454 assert(usage
& PIPE_TRANSFER_WRITE
);
455 if ((usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) == 0) {
456 if (svga_is_texture_dirty(tex
, st
->slice
, level
)) {
458 * do a surface flush if the subresource has been modified
459 * in this command buffer.
461 svga_surfaces_flush(svga
);
462 if (!sws
->surface_is_flushed(sws
, surf
)) {
463 svga
->hud
.surface_write_flushes
++;
464 SVGA_STATS_COUNT_INC(sws
, SVGA_STATS_COUNT_SURFACEWRITEFLUSH
);
465 svga_context_flush(svga
, NULL
);
475 SVGA3dSize baseLevelSize
;
478 unsigned offset
, mip_width
, mip_height
;
480 map
= svga
->swc
->surface_map(svga
->swc
, surf
, usage
, &retry
);
481 if (map
== NULL
&& retry
) {
483 * At this point, the svga_surfaces_flush() should already have
484 * called in svga_texture_get_transfer().
486 svga
->hud
.surface_write_flushes
++;
487 svga_context_flush(svga
, NULL
);
488 map
= svga
->swc
->surface_map(svga
->swc
, surf
, usage
, &retry
);
492 * Make sure we return NULL if the map fails
499 * Compute the offset to the specific texture slice in the buffer.
501 baseLevelSize
.width
= tex
->b
.b
.width0
;
502 baseLevelSize
.height
= tex
->b
.b
.height0
;
503 baseLevelSize
.depth
= tex
->b
.b
.depth0
;
505 if ((tex
->b
.b
.target
== PIPE_TEXTURE_1D_ARRAY
) ||
506 (tex
->b
.b
.target
== PIPE_TEXTURE_2D_ARRAY
)) {
507 st
->base
.layer_stride
=
508 svga3dsurface_get_image_offset(tex
->key
.format
, baseLevelSize
,
509 tex
->b
.b
.last_level
+ 1, 1, 0);
512 offset
= svga3dsurface_get_image_offset(tex
->key
.format
, baseLevelSize
,
513 tex
->b
.b
.last_level
+ 1, /* numMips */
519 mip_width
= u_minify(tex
->b
.b
.width0
, level
);
520 mip_height
= u_minify(tex
->b
.b
.height0
, level
);
522 offset
+= svga3dsurface_get_pixel_offset(tex
->key
.format
,
523 mip_width
, mip_height
,
528 if (usage
& PIPE_TRANSFER_WRITE
) {
529 /* mark this texture level as dirty */
530 svga_set_texture_dirty(tex
, st
->slice
, level
);
533 return (void *) (map
+ offset
);
539 * Request a transfer map to the texture resource
542 svga_texture_transfer_map(struct pipe_context
*pipe
,
543 struct pipe_resource
*texture
,
546 const struct pipe_box
*box
,
547 struct pipe_transfer
**ptransfer
)
549 struct svga_context
*svga
= svga_context(pipe
);
550 struct svga_winsys_screen
*sws
= svga_screen(pipe
->screen
)->sws
;
551 struct svga_texture
*tex
= svga_texture(texture
);
552 struct svga_transfer
*st
;
553 boolean use_direct_map
= svga_have_gb_objects(svga
) &&
554 !svga_have_gb_dma(svga
);
555 void *returnVal
= NULL
;
556 int64_t begin
= svga_get_time(svga
);
558 SVGA_STATS_TIME_PUSH(sws
, SVGA_STATS_TIME_TEXTRANSFERMAP
);
560 /* We can't map texture storage directly unless we have GB objects */
561 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
) {
562 if (svga_have_gb_objects(svga
))
563 use_direct_map
= TRUE
;
568 st
= CALLOC_STRUCT(svga_transfer
);
572 st
->base
.level
= level
;
573 st
->base
.usage
= usage
;
576 switch (tex
->b
.b
.target
) {
577 case PIPE_TEXTURE_CUBE
:
578 st
->slice
= st
->base
.box
.z
;
579 st
->base
.box
.z
= 0; /* so we don't apply double offsets below */
581 case PIPE_TEXTURE_2D_ARRAY
:
582 case PIPE_TEXTURE_1D_ARRAY
:
583 st
->slice
= st
->base
.box
.z
;
584 st
->base
.box
.z
= 0; /* so we don't apply double offsets below */
586 /* Force direct map for transfering multiple slices */
587 if (st
->base
.box
.depth
> 1)
588 use_direct_map
= svga_have_gb_objects(svga
);
596 st
->use_direct_map
= use_direct_map
;
597 pipe_resource_reference(&st
->base
.resource
, texture
);
599 if (use_direct_map
) {
600 returnVal
= svga_texture_transfer_map_direct(svga
, st
);
603 returnVal
= svga_texture_transfer_map_dma(svga
, st
);
610 *ptransfer
= &st
->base
;
611 svga
->hud
.num_textures_mapped
++;
612 if (usage
& PIPE_TRANSFER_WRITE
) {
613 /* record texture upload for HUD */
614 svga
->hud
.num_bytes_uploaded
+=
615 st
->base
.layer_stride
* st
->base
.box
.depth
;
620 svga
->hud
.map_buffer_time
+= (svga_get_time(svga
) - begin
);
621 SVGA_STATS_TIME_POP(sws
);
626 * Unmap a GB texture surface.
629 svga_texture_surface_unmap(struct svga_context
*svga
,
630 struct pipe_transfer
*transfer
)
632 struct svga_winsys_surface
*surf
= svga_texture(transfer
->resource
)->handle
;
633 struct svga_winsys_context
*swc
= svga
->swc
;
638 swc
->surface_unmap(swc
, surf
, &rebind
);
641 ret
= SVGA3D_BindGBSurface(swc
, surf
);
642 if (ret
!= PIPE_OK
) {
643 /* flush and retry */
644 svga_context_flush(svga
, NULL
);
645 ret
= SVGA3D_BindGBSurface(swc
, surf
);
646 assert(ret
== PIPE_OK
);
652 static enum pipe_error
653 update_image_vgpu9(struct svga_context
*svga
,
654 struct svga_winsys_surface
*surf
,
655 const SVGA3dBox
*box
,
661 ret
= SVGA3D_UpdateGBImage(svga
->swc
, surf
, box
, slice
, level
);
662 if (ret
!= PIPE_OK
) {
663 svga_context_flush(svga
, NULL
);
664 ret
= SVGA3D_UpdateGBImage(svga
->swc
, surf
, box
, slice
, level
);
670 static enum pipe_error
671 update_image_vgpu10(struct svga_context
*svga
,
672 struct svga_winsys_surface
*surf
,
673 const SVGA3dBox
*box
,
676 unsigned numMipLevels
)
679 unsigned subResource
;
681 subResource
= slice
* numMipLevels
+ level
;
682 ret
= SVGA3D_vgpu10_UpdateSubResource(svga
->swc
, surf
, box
, subResource
);
683 if (ret
!= PIPE_OK
) {
684 svga_context_flush(svga
, NULL
);
685 ret
= SVGA3D_vgpu10_UpdateSubResource(svga
->swc
, surf
, box
, subResource
);
692 * unmap DMA transfer request
695 svga_texture_transfer_unmap_dma(struct svga_context
*svga
,
696 struct svga_transfer
*st
)
698 struct svga_winsys_screen
*sws
= svga_screen(svga
->pipe
.screen
)->sws
;
701 sws
->buffer_unmap(sws
, st
->hwbuf
);
703 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
704 /* Use DMA to transfer texture data */
705 SVGA3dSurfaceDMAFlags flags
;
707 memset(&flags
, 0, sizeof flags
);
708 if (st
->base
.usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
709 flags
.discard
= TRUE
;
711 if (st
->base
.usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
712 flags
.unsynchronized
= TRUE
;
715 svga_transfer_dma(svga
, st
, SVGA3D_WRITE_HOST_VRAM
, flags
);
719 sws
->buffer_destroy(sws
, st
->hwbuf
);
724 * unmap direct map transfer request
727 svga_texture_transfer_unmap_direct(struct svga_context
*svga
,
728 struct svga_transfer
*st
)
730 struct pipe_transfer
*transfer
= &st
->base
;
731 struct svga_texture
*tex
= svga_texture(transfer
->resource
);
733 svga_texture_surface_unmap(svga
, transfer
);
735 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
736 struct svga_winsys_surface
*surf
= tex
->handle
;
739 unsigned nlayers
= 1;
741 assert(svga_have_gb_objects(svga
));
743 /* update the effected region */
744 box
.x
= transfer
->box
.x
;
745 box
.y
= transfer
->box
.y
;
746 box
.w
= transfer
->box
.width
;
747 box
.h
= transfer
->box
.height
;
748 box
.d
= transfer
->box
.depth
;
750 switch (tex
->b
.b
.target
) {
751 case PIPE_TEXTURE_CUBE
:
754 case PIPE_TEXTURE_2D_ARRAY
:
759 case PIPE_TEXTURE_1D_ARRAY
:
765 box
.z
= transfer
->box
.z
;
770 debug_printf("%s %d, %d, %d %d x %d x %d\n",
773 box
.w
, box
.h
, box
.d
);
775 if (svga_have_vgpu10(svga
)) {
777 for (i
= 0; i
< nlayers
; i
++) {
778 ret
= update_image_vgpu10(svga
, surf
, &box
,
779 st
->slice
+ i
, transfer
->level
,
780 tex
->b
.b
.last_level
+ 1);
781 assert(ret
== PIPE_OK
);
784 assert(nlayers
== 1);
785 ret
= update_image_vgpu9(svga
, surf
, &box
, st
->slice
, transfer
->level
);
786 assert(ret
== PIPE_OK
);
792 svga_texture_transfer_unmap(struct pipe_context
*pipe
,
793 struct pipe_transfer
*transfer
)
795 struct svga_context
*svga
= svga_context(pipe
);
796 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
797 struct svga_winsys_screen
*sws
= ss
->sws
;
798 struct svga_transfer
*st
= svga_transfer(transfer
);
799 struct svga_texture
*tex
= svga_texture(transfer
->resource
);
801 SVGA_STATS_TIME_PUSH(sws
, SVGA_STATS_TIME_TEXTRANSFERUNMAP
);
803 if (st
->use_direct_map
) {
804 svga_texture_transfer_unmap_direct(svga
, st
);
807 svga_texture_transfer_unmap_dma(svga
, st
);
810 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
811 svga
->hud
.num_resource_updates
++;
813 ss
->texture_timestamp
++;
814 svga_age_texture_view(tex
, transfer
->level
);
815 if (transfer
->resource
->target
== PIPE_TEXTURE_CUBE
)
816 svga_define_texture_level(tex
, st
->slice
, transfer
->level
);
818 svga_define_texture_level(tex
, 0, transfer
->level
);
821 pipe_resource_reference(&st
->base
.resource
, NULL
);
823 SVGA_STATS_TIME_POP(sws
);
828 * Does format store depth values?
830 static inline boolean
831 format_has_depth(enum pipe_format format
)
833 const struct util_format_description
*desc
= util_format_description(format
);
834 return util_format_has_depth(desc
);
838 struct u_resource_vtbl svga_texture_vtbl
=
840 svga_texture_get_handle
, /* get_handle */
841 svga_texture_destroy
, /* resource_destroy */
842 svga_texture_transfer_map
, /* transfer_map */
843 u_default_transfer_flush_region
, /* transfer_flush_region */
844 svga_texture_transfer_unmap
, /* transfer_unmap */
848 struct pipe_resource
*
849 svga_texture_create(struct pipe_screen
*screen
,
850 const struct pipe_resource
*template)
852 struct svga_screen
*svgascreen
= svga_screen(screen
);
853 struct svga_texture
*tex
;
854 unsigned bindings
= template->bind
;
856 SVGA_STATS_TIME_PUSH(svgascreen
->sws
,
857 SVGA_STATS_TIME_CREATETEXTURE
);
859 assert(template->last_level
< SVGA_MAX_TEXTURE_LEVELS
);
860 if (template->last_level
>= SVGA_MAX_TEXTURE_LEVELS
) {
864 tex
= CALLOC_STRUCT(svga_texture
);
869 tex
->defined
= CALLOC(template->depth0
* template->array_size
,
870 sizeof(tex
->defined
[0]));
876 tex
->rendered_to
= CALLOC(template->depth0
* template->array_size
,
877 sizeof(tex
->rendered_to
[0]));
878 if (!tex
->rendered_to
) {
882 tex
->dirty
= CALLOC(template->depth0
* template->array_size
,
883 sizeof(tex
->dirty
[0]));
888 tex
->b
.b
= *template;
889 tex
->b
.vtbl
= &svga_texture_vtbl
;
890 pipe_reference_init(&tex
->b
.b
.reference
, 1);
891 tex
->b
.b
.screen
= screen
;
894 tex
->key
.size
.width
= template->width0
;
895 tex
->key
.size
.height
= template->height0
;
896 tex
->key
.size
.depth
= template->depth0
;
897 tex
->key
.arraySize
= 1;
898 tex
->key
.numFaces
= 1;
900 /* single sample texture can be treated as non-multisamples texture */
901 tex
->key
.sampleCount
= template->nr_samples
> 1 ? template->nr_samples
: 0;
903 if (template->nr_samples
> 1) {
904 tex
->key
.flags
|= SVGA3D_SURFACE_MASKABLE_ANTIALIAS
;
907 if (svgascreen
->sws
->have_vgpu10
) {
908 switch (template->target
) {
909 case PIPE_TEXTURE_1D
:
910 tex
->key
.flags
|= SVGA3D_SURFACE_1D
;
912 case PIPE_TEXTURE_1D_ARRAY
:
913 tex
->key
.flags
|= SVGA3D_SURFACE_1D
;
915 case PIPE_TEXTURE_2D_ARRAY
:
916 tex
->key
.flags
|= SVGA3D_SURFACE_ARRAY
;
917 tex
->key
.arraySize
= template->array_size
;
919 case PIPE_TEXTURE_3D
:
920 tex
->key
.flags
|= SVGA3D_SURFACE_VOLUME
;
922 case PIPE_TEXTURE_CUBE
:
923 tex
->key
.flags
|= (SVGA3D_SURFACE_CUBEMAP
| SVGA3D_SURFACE_ARRAY
);
924 tex
->key
.numFaces
= 6;
931 switch (template->target
) {
932 case PIPE_TEXTURE_3D
:
933 tex
->key
.flags
|= SVGA3D_SURFACE_VOLUME
;
935 case PIPE_TEXTURE_CUBE
:
936 tex
->key
.flags
|= SVGA3D_SURFACE_CUBEMAP
;
937 tex
->key
.numFaces
= 6;
944 tex
->key
.cachable
= 1;
946 if ((bindings
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
)) &&
947 !(bindings
& PIPE_BIND_SAMPLER_VIEW
)) {
948 /* Also check if the format can be sampled from */
949 if (screen
->is_format_supported(screen
, template->format
,
951 template->nr_samples
,
952 PIPE_BIND_SAMPLER_VIEW
)) {
953 bindings
|= PIPE_BIND_SAMPLER_VIEW
;
957 if (bindings
& PIPE_BIND_SAMPLER_VIEW
) {
958 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_TEXTURE
;
959 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_SHADER_RESOURCE
;
961 if (!(bindings
& PIPE_BIND_RENDER_TARGET
)) {
962 /* Also check if the format is renderable */
963 if (screen
->is_format_supported(screen
, template->format
,
965 template->nr_samples
,
966 PIPE_BIND_RENDER_TARGET
)) {
967 bindings
|= PIPE_BIND_RENDER_TARGET
;
972 if (bindings
& PIPE_BIND_DISPLAY_TARGET
) {
973 tex
->key
.cachable
= 0;
976 if (bindings
& PIPE_BIND_SHARED
) {
977 tex
->key
.cachable
= 0;
980 if (bindings
& (PIPE_BIND_SCANOUT
| PIPE_BIND_CURSOR
)) {
981 tex
->key
.scanout
= 1;
982 tex
->key
.cachable
= 0;
986 * Note: Previously we never passed the
987 * SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
988 * know beforehand whether a texture will be used as a rendertarget or not
989 * and it always requests PIPE_BIND_RENDER_TARGET, therefore
990 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
992 * However, this was changed since other state trackers
993 * (XA for example) uses it accurately and certain device versions
994 * relies on it in certain situations to render correctly.
996 if ((bindings
& PIPE_BIND_RENDER_TARGET
) &&
997 !util_format_is_s3tc(template->format
)) {
998 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
999 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_RENDER_TARGET
;
1002 if (bindings
& PIPE_BIND_DEPTH_STENCIL
) {
1003 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
1004 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_DEPTH_STENCIL
;
1007 tex
->key
.numMipLevels
= template->last_level
+ 1;
1009 tex
->key
.format
= svga_translate_format(svgascreen
, template->format
,
1011 if (tex
->key
.format
== SVGA3D_FORMAT_INVALID
) {
1015 /* The actual allocation is done with a typeless format. Typeless
1016 * formats can be reinterpreted as other formats. For example,
1017 * SVGA3D_R8G8B8A8_UNORM_TYPELESS can be interpreted as
1018 * SVGA3D_R8G8B8A8_UNORM_SRGB or SVGA3D_R8G8B8A8_UNORM.
1019 * Do not use typeless formats for SHARED, DISPLAY_TARGET or SCANOUT
1022 if (svgascreen
->sws
->have_vgpu10
1023 && ((bindings
& (PIPE_BIND_SHARED
|
1024 PIPE_BIND_DISPLAY_TARGET
|
1025 PIPE_BIND_SCANOUT
)) == 0)) {
1026 SVGA3dSurfaceFormat typeless
= svga_typeless_format(tex
->key
.format
);
1028 debug_printf("Convert resource type %s -> %s (bind 0x%x)\n",
1029 svga_format_name(tex
->key
.format
),
1030 svga_format_name(typeless
),
1034 if (svga_format_is_uncompressed_snorm(tex
->key
.format
)) {
1035 /* We can't normally render to snorm surfaces, but once we
1036 * substitute a typeless format, we can if the rendertarget view
1037 * is unorm. This can happen with GL_ARB_copy_image.
1039 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
1040 tex
->key
.flags
|= SVGA3D_SURFACE_BIND_RENDER_TARGET
;
1043 tex
->key
.format
= typeless
;
1046 SVGA_DBG(DEBUG_DMA
, "surface_create for texture\n", tex
->handle
);
1047 tex
->handle
= svga_screen_surface_create(svgascreen
, bindings
,
1048 tex
->b
.b
.usage
, &tex
->key
);
1053 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture)\n", tex
->handle
);
1055 debug_reference(&tex
->b
.b
.reference
,
1056 (debug_reference_descriptor
)debug_describe_resource
, 0);
1058 tex
->size
= util_resource_size(template);
1059 svgascreen
->hud
.total_resource_bytes
+= tex
->size
;
1060 svgascreen
->hud
.num_resources
++;
1062 SVGA_STATS_TIME_POP(svgascreen
->sws
);
1069 if (tex
->rendered_to
)
1070 FREE(tex
->rendered_to
);
1075 SVGA_STATS_TIME_POP(svgascreen
->sws
);
1080 struct pipe_resource
*
1081 svga_texture_from_handle(struct pipe_screen
*screen
,
1082 const struct pipe_resource
*template,
1083 struct winsys_handle
*whandle
)
1085 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
1086 struct svga_screen
*ss
= svga_screen(screen
);
1087 struct svga_winsys_surface
*srf
;
1088 struct svga_texture
*tex
;
1089 enum SVGA3dSurfaceFormat format
= 0;
1092 /* Only supports one type */
1093 if ((template->target
!= PIPE_TEXTURE_2D
&&
1094 template->target
!= PIPE_TEXTURE_RECT
) ||
1095 template->last_level
!= 0 ||
1096 template->depth0
!= 1) {
1100 srf
= sws
->surface_from_handle(sws
, whandle
, &format
);
1105 if (svga_translate_format(svga_screen(screen
), template->format
,
1106 template->bind
) != format
) {
1107 unsigned f1
= svga_translate_format(svga_screen(screen
),
1108 template->format
, template->bind
);
1109 unsigned f2
= format
;
1111 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up.
1113 if (f1
== SVGA3D_B8G8R8A8_UNORM
)
1114 f1
= SVGA3D_A8R8G8B8
;
1115 if (f1
== SVGA3D_B8G8R8X8_UNORM
)
1116 f1
= SVGA3D_X8R8G8B8
;
1118 if ( !( (f1
== f2
) ||
1119 (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
1120 (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_B8G8R8X8_UNORM
) ||
1121 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
1122 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_B8G8R8A8_UNORM
) ||
1123 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ||
1124 (f1
== SVGA3D_Z_DF24
&& f2
== SVGA3D_Z_D24S8_INT
) ) ) {
1125 debug_printf("%s wrong format %s != %s\n", __FUNCTION__
,
1126 svga_format_name(f1
), svga_format_name(f2
));
1131 tex
= CALLOC_STRUCT(svga_texture
);
1135 tex
->defined
= CALLOC(template->depth0
* template->array_size
,
1136 sizeof(tex
->defined
[0]));
1137 if (!tex
->defined
) {
1142 tex
->b
.b
= *template;
1143 tex
->b
.vtbl
= &svga_texture_vtbl
;
1144 pipe_reference_init(&tex
->b
.b
.reference
, 1);
1145 tex
->b
.b
.screen
= screen
;
1147 SVGA_DBG(DEBUG_DMA
, "wrap surface sid %p\n", srf
);
1149 tex
->key
.cachable
= 0;
1150 tex
->key
.format
= format
;
1153 tex
->rendered_to
= CALLOC(1, sizeof(tex
->rendered_to
[0]));
1154 if (!tex
->rendered_to
)
1157 tex
->dirty
= CALLOC(1, sizeof(tex
->dirty
[0]));
1161 tex
->imported
= TRUE
;
1163 ss
->hud
.num_resources
++;
1170 if (tex
->rendered_to
)
1171 FREE(tex
->rendered_to
);
1179 svga_texture_generate_mipmap(struct pipe_context
*pipe
,
1180 struct pipe_resource
*pt
,
1181 enum pipe_format format
,
1182 unsigned base_level
,
1183 unsigned last_level
,
1184 unsigned first_layer
,
1185 unsigned last_layer
)
1187 struct pipe_sampler_view templ
, *psv
;
1188 struct svga_pipe_sampler_view
*sv
;
1189 struct svga_context
*svga
= svga_context(pipe
);
1190 struct svga_texture
*tex
= svga_texture(pt
);
1191 enum pipe_error ret
;
1193 assert(svga_have_vgpu10(svga
));
1195 /* Only support 2D texture for now */
1196 if (pt
->target
!= PIPE_TEXTURE_2D
)
1199 /* Fallback to the mipmap generation utility for those formats that
1200 * do not support hw generate mipmap
1202 if (!svga_format_support_gen_mips(format
))
1205 /* Make sure the texture surface was created with
1206 * SVGA3D_SURFACE_BIND_RENDER_TARGET
1208 if (!tex
->handle
|| !(tex
->key
.flags
& SVGA3D_SURFACE_BIND_RENDER_TARGET
))
1211 templ
.format
= format
;
1212 templ
.u
.tex
.first_layer
= first_layer
;
1213 templ
.u
.tex
.last_layer
= last_layer
;
1214 templ
.u
.tex
.first_level
= base_level
;
1215 templ
.u
.tex
.last_level
= last_level
;
1217 psv
= pipe
->create_sampler_view(pipe
, pt
, &templ
);
1221 sv
= svga_pipe_sampler_view(psv
);
1222 ret
= svga_validate_pipe_sampler_view(svga
, sv
);
1223 if (ret
!= PIPE_OK
) {
1224 svga_context_flush(svga
, NULL
);
1225 ret
= svga_validate_pipe_sampler_view(svga
, sv
);
1226 assert(ret
== PIPE_OK
);
1229 ret
= SVGA3D_vgpu10_GenMips(svga
->swc
, sv
->id
, tex
->handle
);
1230 if (ret
!= PIPE_OK
) {
1231 svga_context_flush(svga
, NULL
);
1232 ret
= SVGA3D_vgpu10_GenMips(svga
->swc
, sv
->id
, tex
->handle
);
1234 pipe_sampler_view_reference(&psv
, NULL
);
1236 svga
->hud
.num_generate_mipmap
++;