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_screen_texture.h"
39 #include "svga_screen_buffer.h"
40 #include "svga_winsys.h"
41 #include "svga_debug.h"
42 #include "svga_screen_buffer.h"
44 #include <util/u_string.h>
47 /* XXX: This isn't a real hardware flag, but just a hack for kernel to
48 * know about primary surfaces. Find a better way to accomplish this.
50 #define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)
54 * Helper function and arrays
58 svga_translate_format(enum pipe_format format
)
62 case PIPE_FORMAT_A8R8G8B8_UNORM
:
63 return SVGA3D_A8R8G8B8
;
64 case PIPE_FORMAT_X8R8G8B8_UNORM
:
65 return SVGA3D_X8R8G8B8
;
67 /* Required for GL2.1:
69 case PIPE_FORMAT_A8R8G8B8_SRGB
:
70 return SVGA3D_A8R8G8B8
;
72 case PIPE_FORMAT_R5G6B5_UNORM
:
74 case PIPE_FORMAT_A1R5G5B5_UNORM
:
75 return SVGA3D_A1R5G5B5
;
76 case PIPE_FORMAT_A4R4G4B4_UNORM
:
77 return SVGA3D_A4R4G4B4
;
80 /* XXX: Doesn't seem to work properly.
81 case PIPE_FORMAT_Z32_UNORM:
84 case PIPE_FORMAT_Z16_UNORM
:
86 case PIPE_FORMAT_Z24S8_UNORM
:
87 return SVGA3D_Z_D24S8
;
88 case PIPE_FORMAT_Z24X8_UNORM
:
89 return SVGA3D_Z_D24X8
;
91 case PIPE_FORMAT_A8_UNORM
:
93 case PIPE_FORMAT_L8_UNORM
:
94 return SVGA3D_LUMINANCE8
;
96 case PIPE_FORMAT_DXT1_RGB
:
97 case PIPE_FORMAT_DXT1_RGBA
:
99 case PIPE_FORMAT_DXT3_RGBA
:
101 case PIPE_FORMAT_DXT5_RGBA
:
105 return SVGA3D_FORMAT_INVALID
;
111 svga_translate_format_render(enum pipe_format format
)
114 case PIPE_FORMAT_A8R8G8B8_UNORM
:
115 case PIPE_FORMAT_X8R8G8B8_UNORM
:
116 case PIPE_FORMAT_A1R5G5B5_UNORM
:
117 case PIPE_FORMAT_A4R4G4B4_UNORM
:
118 case PIPE_FORMAT_R5G6B5_UNORM
:
119 case PIPE_FORMAT_Z24S8_UNORM
:
120 case PIPE_FORMAT_Z24X8_UNORM
:
121 case PIPE_FORMAT_Z32_UNORM
:
122 case PIPE_FORMAT_Z16_UNORM
:
123 case PIPE_FORMAT_L8_UNORM
:
124 return svga_translate_format(format
);
127 /* For on host conversion */
128 case PIPE_FORMAT_DXT1_RGB
:
129 return SVGA3D_X8R8G8B8
;
130 case PIPE_FORMAT_DXT1_RGBA
:
131 case PIPE_FORMAT_DXT3_RGBA
:
132 case PIPE_FORMAT_DXT5_RGBA
:
133 return SVGA3D_A8R8G8B8
;
137 return SVGA3D_FORMAT_INVALID
;
143 svga_transfer_dma_band(struct svga_transfer
*st
,
144 SVGA3dTransferType transfer
,
145 unsigned y
, unsigned h
, unsigned srcy
)
147 struct svga_texture
*texture
= svga_texture(st
->base
.texture
);
148 struct svga_screen
*screen
= svga_screen(texture
->base
.screen
);
152 SVGA_DBG(DEBUG_DMA
, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
153 transfer
== SVGA3D_WRITE_HOST_VRAM
? "to" : "from",
159 st
->base
.x
+ st
->base
.width
,
162 util_format_get_blocksize(texture
->base
.format
)*8/
163 (util_format_get_blockwidth(texture
->base
.format
)*util_format_get_blockheight(texture
->base
.format
)));
167 box
.z
= st
->base
.zslice
;
168 box
.w
= st
->base
.width
;
175 pipe_mutex_lock(screen
->swc_mutex
);
176 ret
= SVGA3D_SurfaceDMA(screen
->swc
, st
, transfer
, &box
, 1);
178 screen
->swc
->flush(screen
->swc
, NULL
);
179 ret
= SVGA3D_SurfaceDMA(screen
->swc
, st
, transfer
, &box
, 1);
180 assert(ret
== PIPE_OK
);
182 pipe_mutex_unlock(screen
->swc_mutex
);
187 svga_transfer_dma(struct svga_transfer
*st
,
188 SVGA3dTransferType transfer
)
190 struct svga_texture
*texture
= svga_texture(st
->base
.texture
);
191 struct svga_screen
*screen
= svga_screen(texture
->base
.screen
);
192 struct svga_winsys_screen
*sws
= screen
->sws
;
193 struct pipe_fence_handle
*fence
= NULL
;
195 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
196 SVGA_DBG(DEBUG_PERF
, "%s: readback transfer\n", __FUNCTION__
);
201 /* Do the DMA transfer in a single go */
203 svga_transfer_dma_band(st
, transfer
, st
->base
.y
, st
->base
.height
, 0);
205 if(transfer
== SVGA3D_READ_HOST_VRAM
) {
206 svga_screen_flush(screen
, &fence
);
207 sws
->fence_finish(sws
, fence
, 0);
208 sws
->fence_reference(sws
, &fence
, NULL
);
213 unsigned blockheight
= util_format_get_blockheight(st
->base
.texture
->format
);
214 h
= st
->hw_nblocksy
* blockheight
;
216 for(y
= 0; y
< st
->base
.height
; y
+= h
) {
217 unsigned offset
, length
;
220 if (y
+ h
> st
->base
.height
)
221 h
= st
->base
.height
- y
;
223 /* Transfer band must be aligned to pixel block boundaries */
224 assert(y
% blockheight
== 0);
225 assert(h
% blockheight
== 0);
227 offset
= y
* st
->base
.stride
/ blockheight
;
228 length
= h
* st
->base
.stride
/ blockheight
;
230 sw
= (uint8_t *)st
->swbuf
+ offset
;
232 if(transfer
== SVGA3D_WRITE_HOST_VRAM
) {
233 /* Wait for the previous DMAs to complete */
234 /* TODO: keep one DMA (at half the size) in the background */
236 svga_screen_flush(screen
, &fence
);
237 sws
->fence_finish(sws
, fence
, 0);
238 sws
->fence_reference(sws
, &fence
, NULL
);
241 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
244 memcpy(hw
, sw
, length
);
245 sws
->buffer_unmap(sws
, st
->hwbuf
);
249 svga_transfer_dma_band(st
, transfer
, y
, h
, srcy
);
251 if(transfer
== SVGA3D_READ_HOST_VRAM
) {
252 svga_screen_flush(screen
, &fence
);
253 sws
->fence_finish(sws
, fence
, 0);
255 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_BUFFER_USAGE_CPU_READ
);
258 memcpy(sw
, hw
, length
);
259 sws
->buffer_unmap(sws
, st
->hwbuf
);
267 static struct pipe_texture
*
268 svga_texture_create(struct pipe_screen
*screen
,
269 const struct pipe_texture
*templat
)
271 struct svga_screen
*svgascreen
= svga_screen(screen
);
272 struct svga_texture
*tex
= CALLOC_STRUCT(svga_texture
);
273 unsigned width
, height
, depth
;
279 tex
->base
= *templat
;
280 pipe_reference_init(&tex
->base
.reference
, 1);
281 tex
->base
.screen
= screen
;
283 assert(templat
->last_level
< SVGA_MAX_TEXTURE_LEVELS
);
284 if(templat
->last_level
>= SVGA_MAX_TEXTURE_LEVELS
)
287 width
= templat
->width0
;
288 height
= templat
->height0
;
289 depth
= templat
->depth0
;
290 for(level
= 0; level
<= templat
->last_level
; ++level
) {
291 width
= u_minify(width
, 1);
292 height
= u_minify(height
, 1);
293 depth
= u_minify(depth
, 1);
297 tex
->key
.size
.width
= templat
->width0
;
298 tex
->key
.size
.height
= templat
->height0
;
299 tex
->key
.size
.depth
= templat
->depth0
;
301 if(templat
->target
== PIPE_TEXTURE_CUBE
) {
302 tex
->key
.flags
|= SVGA3D_SURFACE_CUBEMAP
;
303 tex
->key
.numFaces
= 6;
306 tex
->key
.numFaces
= 1;
309 tex
->key
.cachable
= 1;
311 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_SAMPLER
)
312 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_TEXTURE
;
314 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
315 tex
->key
.cachable
= 0;
318 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_SHARED
) {
319 tex
->key
.cachable
= 0;
322 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_SCANOUT
) {
323 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_SCANOUT
;
324 tex
->key
.cachable
= 0;
328 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
329 * know beforehand whether a texture will be used as a rendertarget or not
330 * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
331 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
334 if((templat
->tex_usage
& PIPE_TEXTURE_USAGE_RENDER_TARGET
) &&
335 !util_format_is_compressed(templat
->format
))
336 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
339 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)
340 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
342 tex
->key
.numMipLevels
= templat
->last_level
+ 1;
344 tex
->key
.format
= svga_translate_format(templat
->format
);
345 if(tex
->key
.format
== SVGA3D_FORMAT_INVALID
)
348 SVGA_DBG(DEBUG_DMA
, "surface_create for texture\n", tex
->handle
);
349 tex
->handle
= svga_screen_surface_create(svgascreen
, &tex
->key
);
351 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture)\n", tex
->handle
);
362 static struct pipe_texture
*
363 svga_texture_blanket(struct pipe_screen
* screen
,
364 const struct pipe_texture
*base
,
365 const unsigned *stride
,
366 struct pipe_buffer
*buffer
)
368 struct svga_texture
*tex
;
369 struct svga_buffer
*sbuf
= svga_buffer(buffer
);
370 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
373 /* Only supports one type */
374 if (base
->target
!= PIPE_TEXTURE_2D
||
375 base
->last_level
!= 0 ||
381 * We currently can't do texture blanket on
382 * SVGA3D_BUFFER. Need to blit to a temporary surface?
385 assert(sbuf
->handle
);
389 if (svga_translate_format(base
->format
) != sbuf
->key
.format
) {
390 unsigned f1
= svga_translate_format(base
->format
);
391 unsigned f2
= sbuf
->key
.format
;
393 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
394 if ( !( (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
395 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
396 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ) ) {
397 debug_printf("%s wrong format %u != %u\n", __FUNCTION__
, f1
, f2
);
402 tex
= CALLOC_STRUCT(svga_texture
);
409 if (sbuf
->key
.format
== 1)
410 tex
->base
.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
411 else if (sbuf
->key
.format
== 2)
412 tex
->base
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
414 pipe_reference_init(&tex
->base
.reference
, 1);
415 tex
->base
.screen
= screen
;
417 SVGA_DBG(DEBUG_DMA
, "blanket sid %p\n", sbuf
->handle
);
419 /* We don't own this storage, so don't try to cache it.
421 assert(sbuf
->key
.cachable
== 0);
422 tex
->key
.cachable
= 0;
423 sws
->surface_reference(sws
, &tex
->handle
, sbuf
->handle
);
429 struct pipe_texture
*
430 svga_screen_texture_wrap_surface(struct pipe_screen
*screen
,
431 struct pipe_texture
*base
,
432 enum SVGA3dSurfaceFormat format
,
433 struct svga_winsys_surface
*srf
)
435 struct svga_texture
*tex
;
438 /* Only supports one type */
439 if (base
->target
!= PIPE_TEXTURE_2D
||
440 base
->last_level
!= 0 ||
448 if (svga_translate_format(base
->format
) != format
) {
449 unsigned f1
= svga_translate_format(base
->format
);
450 unsigned f2
= format
;
452 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
453 if ( !( (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
454 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
455 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ) ) {
456 debug_printf("%s wrong format %u != %u\n", __FUNCTION__
, f1
, f2
);
461 tex
= CALLOC_STRUCT(svga_texture
);
469 tex
->base
.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
470 else if (format
== 2)
471 tex
->base
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
473 pipe_reference_init(&tex
->base
.reference
, 1);
474 tex
->base
.screen
= screen
;
476 SVGA_DBG(DEBUG_DMA
, "wrap surface sid %p\n", srf
);
478 tex
->key
.cachable
= 0;
486 svga_texture_destroy(struct pipe_texture
*pt
)
488 struct svga_screen
*ss
= svga_screen(pt
->screen
);
489 struct svga_texture
*tex
= (struct svga_texture
*)pt
;
491 ss
->texture_timestamp
++;
493 svga_sampler_view_reference(&tex
->cached_view
, NULL
);
496 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
498 SVGA_DBG(DEBUG_DMA
, "unref sid %p (texture)\n", tex
->handle
);
499 svga_screen_surface_destroy(ss
, &tex
->key
, &tex
->handle
);
506 svga_texture_copy_handle(struct svga_context
*svga
,
507 struct svga_screen
*ss
,
508 struct svga_winsys_surface
*src_handle
,
509 unsigned src_x
, unsigned src_y
, unsigned src_z
,
510 unsigned src_level
, unsigned src_face
,
511 struct svga_winsys_surface
*dst_handle
,
512 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
513 unsigned dst_level
, unsigned dst_face
,
514 unsigned width
, unsigned height
, unsigned depth
)
516 struct svga_surface dst
, src
;
518 SVGA3dCopyBox box
, *boxes
;
522 src
.handle
= src_handle
;
523 src
.real_level
= src_level
;
524 src
.real_face
= src_face
;
527 dst
.handle
= dst_handle
;
528 dst
.real_level
= dst_level
;
529 dst
.real_face
= dst_face
;
543 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
544 src_handle, src_level, src_x, src_y, src_z,
545 dst_handle, dst_level, dst_x, dst_y, dst_z);
549 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
554 svga_context_flush(svga
, NULL
);
555 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
559 assert(ret
== PIPE_OK
);
562 SVGA_FIFOCommitAll(svga
->swc
);
564 pipe_mutex_lock(ss
->swc_mutex
);
565 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
570 ss
->swc
->flush(ss
->swc
, NULL
);
571 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
575 assert(ret
== PIPE_OK
);
578 SVGA_FIFOCommitAll(ss
->swc
);
579 pipe_mutex_unlock(ss
->swc_mutex
);
583 static struct svga_winsys_surface
*
584 svga_texture_view_surface(struct pipe_context
*pipe
,
585 struct svga_texture
*tex
,
586 SVGA3dSurfaceFormat format
,
591 struct svga_host_surface_cache_key
*key
) /* OUT */
593 struct svga_screen
*ss
= svga_screen(tex
->base
.screen
);
594 struct svga_winsys_surface
*handle
;
596 unsigned z_offset
= 0;
599 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
600 face_pick
, zslice_pick
, start_mip
, start_mip
+num_mip
-1);
603 key
->format
= format
;
604 key
->numMipLevels
= num_mip
;
605 key
->size
.width
= u_minify(tex
->base
.width0
, start_mip
);
606 key
->size
.height
= u_minify(tex
->base
.height0
, start_mip
);
607 key
->size
.depth
= zslice_pick
< 0 ? u_minify(tex
->base
.depth0
, start_mip
) : 1;
609 assert(key
->size
.depth
== 1);
611 if(tex
->base
.target
== PIPE_TEXTURE_CUBE
&& face_pick
< 0) {
612 key
->flags
|= SVGA3D_SURFACE_CUBEMAP
;
618 if(key
->format
== SVGA3D_FORMAT_INVALID
) {
623 SVGA_DBG(DEBUG_DMA
, "surface_create for texture view\n");
624 handle
= svga_screen_surface_create(ss
, key
);
630 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture view)\n", handle
);
635 if (zslice_pick
>= 0)
636 z_offset
= zslice_pick
;
638 for (i
= 0; i
< key
->numMipLevels
; i
++) {
639 for (j
= 0; j
< key
->numFaces
; j
++) {
640 if(tex
->defined
[j
+ face_pick
][i
+ start_mip
]) {
641 unsigned depth
= (zslice_pick
< 0 ?
642 u_minify(tex
->base
.depth0
, i
+ start_mip
) :
645 svga_texture_copy_handle(svga_context(pipe
),
651 handle
, 0, 0, 0, i
, j
,
652 u_minify(tex
->base
.width0
, i
+ start_mip
),
653 u_minify(tex
->base
.height0
, i
+ start_mip
),
663 static struct pipe_surface
*
664 svga_get_tex_surface(struct pipe_screen
*screen
,
665 struct pipe_texture
*pt
,
666 unsigned face
, unsigned level
, unsigned zslice
,
669 struct svga_texture
*tex
= svga_texture(pt
);
670 struct svga_surface
*s
;
671 boolean render
= flags
& PIPE_BUFFER_USAGE_GPU_WRITE
? TRUE
: FALSE
;
672 boolean view
= FALSE
;
673 SVGA3dSurfaceFormat format
;
675 s
= CALLOC_STRUCT(svga_surface
);
679 pipe_reference_init(&s
->base
.reference
, 1);
680 pipe_texture_reference(&s
->base
.texture
, pt
);
681 s
->base
.format
= pt
->format
;
682 s
->base
.width
= u_minify(pt
->width0
, level
);
683 s
->base
.height
= u_minify(pt
->height0
, level
);
684 s
->base
.usage
= flags
;
685 s
->base
.level
= level
;
687 s
->base
.zslice
= zslice
;
690 format
= svga_translate_format(pt
->format
);
692 format
= svga_translate_format_render(pt
->format
);
694 assert(format
!= SVGA3D_FORMAT_INVALID
);
695 assert(!(flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
698 if (svga_screen(screen
)->debug
.force_surface_view
)
701 /* Currently only used for compressed textures */
703 format
!= svga_translate_format(pt
->format
)) {
708 svga_screen(screen
)->debug
.force_level_surface_view
)
711 if (pt
->target
== PIPE_TEXTURE_3D
)
714 if (svga_screen(screen
)->debug
.no_surface_view
)
718 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
719 pt
, level
, face
, zslice
, s
);
721 s
->handle
= svga_texture_view_surface(NULL
, tex
, format
, level
, 1, face
, zslice
,
727 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
728 pt
, level
, face
, zslice
, s
);
730 memset(&s
->key
, 0, sizeof s
->key
);
731 s
->handle
= tex
->handle
;
733 s
->real_level
= level
;
734 s
->real_zslice
= zslice
;
742 svga_tex_surface_destroy(struct pipe_surface
*surf
)
744 struct svga_surface
*s
= svga_surface(surf
);
745 struct svga_texture
*t
= svga_texture(surf
->texture
);
746 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
748 if(s
->handle
!= t
->handle
) {
749 SVGA_DBG(DEBUG_DMA
, "unref sid %p (tex surface)\n", s
->handle
);
750 svga_screen_surface_destroy(ss
, &s
->key
, &s
->handle
);
753 pipe_texture_reference(&surf
->texture
, NULL
);
759 svga_mark_surface_dirty(struct pipe_surface
*surf
)
761 struct svga_surface
*s
= svga_surface(surf
);
764 struct svga_texture
*tex
= svga_texture(surf
->texture
);
768 if (s
->handle
== tex
->handle
)
769 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
771 /* this will happen later in svga_propagate_surface */
777 void svga_mark_surfaces_dirty(struct svga_context
*svga
)
781 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
782 if (svga
->curr
.framebuffer
.cbufs
[i
])
783 svga_mark_surface_dirty(svga
->curr
.framebuffer
.cbufs
[i
]);
785 if (svga
->curr
.framebuffer
.zsbuf
)
786 svga_mark_surface_dirty(svga
->curr
.framebuffer
.zsbuf
);
790 * Progagate any changes from surfaces to texture.
791 * pipe is optional context to inline the blit command in.
794 svga_propagate_surface(struct pipe_context
*pipe
, struct pipe_surface
*surf
)
796 struct svga_surface
*s
= svga_surface(surf
);
797 struct svga_texture
*tex
= svga_texture(surf
->texture
);
798 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
804 ss
->texture_timestamp
++;
805 tex
->view_age
[surf
->level
] = ++(tex
->age
);
807 if (s
->handle
!= tex
->handle
) {
808 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface propagate: tex %p, level %u, from %p\n", tex
, surf
->level
, surf
);
809 svga_texture_copy_handle(svga_context(pipe
), ss
,
810 s
->handle
, 0, 0, 0, s
->real_level
, s
->real_face
,
811 tex
->handle
, 0, 0, surf
->zslice
, surf
->level
, surf
->face
,
812 u_minify(tex
->base
.width0
, surf
->level
),
813 u_minify(tex
->base
.height0
, surf
->level
), 1);
814 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
819 * Check if we should call svga_propagate_surface on the surface.
822 svga_surface_needs_propagation(struct pipe_surface
*surf
)
824 struct svga_surface
*s
= svga_surface(surf
);
825 struct svga_texture
*tex
= svga_texture(surf
->texture
);
827 return s
->dirty
&& s
->handle
!= tex
->handle
;
831 static struct pipe_transfer
*
832 svga_get_tex_transfer(struct pipe_screen
*screen
,
833 struct pipe_texture
*texture
,
834 unsigned face
, unsigned level
, unsigned zslice
,
835 enum pipe_transfer_usage usage
, unsigned x
, unsigned y
,
836 unsigned w
, unsigned h
)
838 struct svga_screen
*ss
= svga_screen(screen
);
839 struct svga_winsys_screen
*sws
= ss
->sws
;
840 struct svga_transfer
*st
;
841 unsigned nblocksx
= util_format_get_nblocksx(texture
->format
, w
);
842 unsigned nblocksy
= util_format_get_nblocksy(texture
->format
, h
);
844 /* We can't map texture storage directly */
845 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
848 st
= CALLOC_STRUCT(svga_transfer
);
856 st
->base
.stride
= nblocksx
*util_format_get_blocksize(texture
->format
);
857 st
->base
.usage
= usage
;
858 st
->base
.face
= face
;
859 st
->base
.level
= level
;
860 st
->base
.zslice
= zslice
;
862 st
->hw_nblocksy
= nblocksy
;
864 st
->hwbuf
= svga_winsys_buffer_create(ss
,
867 st
->hw_nblocksy
*st
->base
.stride
);
868 while(!st
->hwbuf
&& (st
->hw_nblocksy
/= 2)) {
869 st
->hwbuf
= svga_winsys_buffer_create(ss
,
872 st
->hw_nblocksy
*st
->base
.stride
);
878 if(st
->hw_nblocksy
< nblocksy
) {
879 /* We couldn't allocate a hardware buffer big enough for the transfer,
880 * so allocate regular malloc memory instead */
881 debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
883 (nblocksy
*st
->base
.stride
+ 1023)/1024,
884 (nblocksy
+ st
->hw_nblocksy
- 1)/st
->hw_nblocksy
,
885 (st
->hw_nblocksy
*st
->base
.stride
+ 1023)/1024);
886 st
->swbuf
= MALLOC(nblocksy
*st
->base
.stride
);
891 pipe_texture_reference(&st
->base
.texture
, texture
);
893 if (usage
& PIPE_TRANSFER_READ
)
894 svga_transfer_dma(st
, SVGA3D_READ_HOST_VRAM
);
899 sws
->buffer_destroy(sws
, st
->hwbuf
);
907 svga_transfer_map( struct pipe_screen
*screen
,
908 struct pipe_transfer
*transfer
)
910 struct svga_screen
*ss
= svga_screen(screen
);
911 struct svga_winsys_screen
*sws
= ss
->sws
;
912 struct svga_transfer
*st
= svga_transfer(transfer
);
917 /* The wait for read transfers already happened when svga_transfer_dma
919 return sws
->buffer_map(sws
, st
->hwbuf
,
920 pipe_transfer_buffer_flags(transfer
));
925 svga_transfer_unmap(struct pipe_screen
*screen
,
926 struct pipe_transfer
*transfer
)
928 struct svga_screen
*ss
= svga_screen(screen
);
929 struct svga_winsys_screen
*sws
= ss
->sws
;
930 struct svga_transfer
*st
= svga_transfer(transfer
);
933 sws
->buffer_unmap(sws
, st
->hwbuf
);
938 svga_tex_transfer_destroy(struct pipe_transfer
*transfer
)
940 struct svga_texture
*tex
= svga_texture(transfer
->texture
);
941 struct svga_screen
*ss
= svga_screen(transfer
->texture
->screen
);
942 struct svga_winsys_screen
*sws
= ss
->sws
;
943 struct svga_transfer
*st
= svga_transfer(transfer
);
945 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
946 svga_transfer_dma(st
, SVGA3D_WRITE_HOST_VRAM
);
947 ss
->texture_timestamp
++;
948 tex
->view_age
[transfer
->level
] = ++(tex
->age
);
949 tex
->defined
[transfer
->face
][transfer
->level
] = TRUE
;
952 pipe_texture_reference(&st
->base
.texture
, NULL
);
954 sws
->buffer_destroy(sws
, st
->hwbuf
);
959 svga_screen_init_texture_functions(struct pipe_screen
*screen
)
961 screen
->texture_create
= svga_texture_create
;
962 screen
->texture_destroy
= svga_texture_destroy
;
963 screen
->get_tex_surface
= svga_get_tex_surface
;
964 screen
->tex_surface_destroy
= svga_tex_surface_destroy
;
965 screen
->texture_blanket
= svga_texture_blanket
;
966 screen
->get_tex_transfer
= svga_get_tex_transfer
;
967 screen
->transfer_map
= svga_transfer_map
;
968 screen
->transfer_unmap
= svga_transfer_unmap
;
969 screen
->tex_transfer_destroy
= svga_tex_transfer_destroy
;
972 /***********************************************************************
975 struct svga_sampler_view
*
976 svga_get_tex_sampler_view(struct pipe_context
*pipe
, struct pipe_texture
*pt
,
977 unsigned min_lod
, unsigned max_lod
)
979 struct svga_screen
*ss
= svga_screen(pt
->screen
);
980 struct svga_texture
*tex
= svga_texture(pt
);
981 struct svga_sampler_view
*sv
= NULL
;
982 SVGA3dSurfaceFormat format
= svga_translate_format(pt
->format
);
986 assert(min_lod
>= 0);
987 assert(min_lod
<= max_lod
);
988 assert(max_lod
<= pt
->last_level
);
991 /* Is a view needed */
994 * Can't control max lod. For first level views and when we only
995 * look at one level we disable mip filtering to achive the same
998 if (min_lod
== 0 && max_lod
>= pt
->last_level
)
1001 if (util_format_is_compressed(pt
->format
) && view
) {
1002 format
= svga_translate_format_render(pt
->format
);
1005 if (ss
->debug
.no_sampler_view
)
1008 if (ss
->debug
.force_sampler_view
)
1012 /* First try the cache */
1014 pipe_mutex_lock(ss
->tex_mutex
);
1015 if (tex
->cached_view
&&
1016 tex
->cached_view
->min_lod
== min_lod
&&
1017 tex
->cached_view
->max_lod
== max_lod
) {
1018 svga_sampler_view_reference(&sv
, tex
->cached_view
);
1019 pipe_mutex_unlock(ss
->tex_mutex
);
1020 SVGA_DBG(DEBUG_VIEWS
, "svga: Sampler view: reuse %p, %u %u, last %u\n",
1021 pt
, min_lod
, max_lod
, pt
->last_level
);
1022 svga_validate_sampler_view(svga_context(pipe
), sv
);
1025 pipe_mutex_unlock(ss
->tex_mutex
);
1028 sv
= CALLOC_STRUCT(svga_sampler_view
);
1029 pipe_reference_init(&sv
->reference
, 1);
1030 pipe_texture_reference(&sv
->texture
, pt
);
1031 sv
->min_lod
= min_lod
;
1032 sv
->max_lod
= max_lod
;
1034 /* No view needed just use the whole texture */
1036 SVGA_DBG(DEBUG_VIEWS
,
1037 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
1038 pt
, min_lod
, max_lod
,
1039 max_lod
- min_lod
+ 1,
1044 sv
->key
.cachable
= 0;
1045 sv
->handle
= tex
->handle
;
1049 SVGA_DBG(DEBUG_VIEWS
,
1050 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
1051 pt
, min_lod
, max_lod
,
1052 max_lod
- min_lod
+ 1,
1059 sv
->handle
= svga_texture_view_surface(pipe
, tex
, format
,
1061 max_lod
- min_lod
+ 1,
1067 sv
->key
.cachable
= 0;
1068 sv
->handle
= tex
->handle
;
1072 pipe_mutex_lock(ss
->tex_mutex
);
1073 svga_sampler_view_reference(&tex
->cached_view
, sv
);
1074 pipe_mutex_unlock(ss
->tex_mutex
);
1080 svga_validate_sampler_view(struct svga_context
*svga
, struct svga_sampler_view
*v
)
1082 struct svga_texture
*tex
= svga_texture(v
->texture
);
1089 if (v
->handle
== tex
->handle
)
1094 if(tex
->base
.target
== PIPE_TEXTURE_CUBE
)
1099 for (i
= v
->min_lod
; i
<= v
->max_lod
; i
++) {
1100 for (k
= 0; k
< numFaces
; k
++) {
1101 if (v
->age
< tex
->view_age
[i
])
1102 svga_texture_copy_handle(svga
, NULL
,
1103 tex
->handle
, 0, 0, 0, i
, k
,
1104 v
->handle
, 0, 0, 0, i
- v
->min_lod
, k
,
1105 u_minify(tex
->base
.width0
, i
),
1106 u_minify(tex
->base
.height0
, i
),
1107 u_minify(tex
->base
.depth0
, i
));
1115 svga_destroy_sampler_view_priv(struct svga_sampler_view
*v
)
1117 struct svga_texture
*tex
= svga_texture(v
->texture
);
1119 if(v
->handle
!= tex
->handle
) {
1120 struct svga_screen
*ss
= svga_screen(v
->texture
->screen
);
1121 SVGA_DBG(DEBUG_DMA
, "unref sid %p (sampler view)\n", v
->handle
);
1122 svga_screen_surface_destroy(ss
, &v
->key
, &v
->handle
);
1124 pipe_texture_reference(&v
->texture
, NULL
);
1129 svga_screen_buffer_from_texture(struct pipe_texture
*texture
,
1130 struct pipe_buffer
**buffer
,
1133 struct svga_texture
*stex
= svga_texture(texture
);
1135 *buffer
= svga_screen_buffer_wrap_surface
1137 svga_translate_format(texture
->format
),
1140 *stride
= util_format_get_stride(texture
->format
, texture
->width0
);
1142 return *buffer
!= NULL
;
1146 struct svga_winsys_surface
*
1147 svga_screen_texture_get_winsys_surface(struct pipe_texture
*texture
)
1149 struct svga_winsys_screen
*sws
= svga_winsys_screen(texture
->screen
);
1150 struct svga_winsys_surface
*vsurf
= NULL
;
1152 assert(svga_texture(texture
)->key
.cachable
== 0);
1153 svga_texture(texture
)->key
.cachable
= 0;
1154 sws
->surface_reference(sws
, &vsurf
, svga_texture(texture
)->handle
);