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 "pipe/p_inlines.h"
31 #include "pipe/p_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
35 #include "svga_screen.h"
36 #include "svga_context.h"
37 #include "svga_screen_texture.h"
38 #include "svga_screen_buffer.h"
39 #include "svga_winsys.h"
40 #include "svga_debug.h"
41 #include "svga_screen_buffer.h"
43 #include <util/u_string.h>
46 /* XXX: This isn't a real hardware flag, but just a hack for kernel to
47 * know about primary surfaces. Find a better way to accomplish this.
49 #define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)
53 * Helper function and arrays
57 svga_translate_format(enum pipe_format format
)
61 case PIPE_FORMAT_A8R8G8B8_UNORM
:
62 return SVGA3D_A8R8G8B8
;
63 case PIPE_FORMAT_X8R8G8B8_UNORM
:
64 return SVGA3D_X8R8G8B8
;
66 /* Required for GL2.1:
68 case PIPE_FORMAT_A8R8G8B8_SRGB
:
69 return SVGA3D_A8R8G8B8
;
71 case PIPE_FORMAT_R5G6B5_UNORM
:
73 case PIPE_FORMAT_A1R5G5B5_UNORM
:
74 return SVGA3D_A1R5G5B5
;
75 case PIPE_FORMAT_A4R4G4B4_UNORM
:
76 return SVGA3D_A4R4G4B4
;
79 /* XXX: Doesn't seem to work properly.
80 case PIPE_FORMAT_Z32_UNORM:
83 case PIPE_FORMAT_Z16_UNORM
:
85 case PIPE_FORMAT_Z24S8_UNORM
:
86 return SVGA3D_Z_D24S8
;
87 case PIPE_FORMAT_Z24X8_UNORM
:
88 return SVGA3D_Z_D24X8
;
90 case PIPE_FORMAT_A8_UNORM
:
92 case PIPE_FORMAT_L8_UNORM
:
93 return SVGA3D_LUMINANCE8
;
95 case PIPE_FORMAT_DXT1_RGB
:
96 case PIPE_FORMAT_DXT1_RGBA
:
98 case PIPE_FORMAT_DXT3_RGBA
:
100 case PIPE_FORMAT_DXT5_RGBA
:
104 return SVGA3D_FORMAT_INVALID
;
110 svga_translate_format_render(enum pipe_format format
)
113 case PIPE_FORMAT_A8R8G8B8_UNORM
:
114 case PIPE_FORMAT_X8R8G8B8_UNORM
:
115 case PIPE_FORMAT_A1R5G5B5_UNORM
:
116 case PIPE_FORMAT_A4R4G4B4_UNORM
:
117 case PIPE_FORMAT_R5G6B5_UNORM
:
118 case PIPE_FORMAT_Z24S8_UNORM
:
119 case PIPE_FORMAT_Z24X8_UNORM
:
120 case PIPE_FORMAT_Z32_UNORM
:
121 case PIPE_FORMAT_Z16_UNORM
:
122 case PIPE_FORMAT_L8_UNORM
:
123 return svga_translate_format(format
);
126 /* For on host conversion */
127 case PIPE_FORMAT_DXT1_RGB
:
128 return SVGA3D_X8R8G8B8
;
129 case PIPE_FORMAT_DXT1_RGBA
:
130 case PIPE_FORMAT_DXT3_RGBA
:
131 case PIPE_FORMAT_DXT5_RGBA
:
132 return SVGA3D_A8R8G8B8
;
136 return SVGA3D_FORMAT_INVALID
;
142 svga_transfer_dma_band(struct svga_transfer
*st
,
143 SVGA3dTransferType transfer
,
144 unsigned y
, unsigned h
, unsigned srcy
)
146 struct svga_texture
*texture
= svga_texture(st
->base
.texture
);
147 struct svga_screen
*screen
= svga_screen(texture
->base
.screen
);
151 SVGA_DBG(DEBUG_DMA
, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
152 transfer
== SVGA3D_WRITE_HOST_VRAM
? "to" : "from",
158 st
->base
.x
+ st
->base
.width
,
161 texture
->base
.block
.size
*8/(texture
->base
.block
.width
*texture
->base
.block
.height
));
165 box
.z
= st
->base
.zslice
;
166 box
.w
= st
->base
.width
;
173 pipe_mutex_lock(screen
->swc_mutex
);
174 ret
= SVGA3D_SurfaceDMA(screen
->swc
, st
, transfer
, &box
, 1);
176 screen
->swc
->flush(screen
->swc
, NULL
);
177 ret
= SVGA3D_SurfaceDMA(screen
->swc
, st
, transfer
, &box
, 1);
178 assert(ret
== PIPE_OK
);
180 pipe_mutex_unlock(screen
->swc_mutex
);
185 svga_transfer_dma(struct svga_transfer
*st
,
186 SVGA3dTransferType transfer
)
188 struct svga_texture
*texture
= svga_texture(st
->base
.texture
);
189 struct svga_screen
*screen
= svga_screen(texture
->base
.screen
);
190 struct svga_winsys_screen
*sws
= screen
->sws
;
191 struct pipe_fence_handle
*fence
= NULL
;
193 if (transfer
== SVGA3D_READ_HOST_VRAM
) {
194 SVGA_DBG(DEBUG_PERF
, "%s: readback transfer\n", __FUNCTION__
);
199 /* Do the DMA transfer in a single go */
201 svga_transfer_dma_band(st
, transfer
, st
->base
.y
, st
->base
.height
, 0);
203 if(transfer
== SVGA3D_READ_HOST_VRAM
) {
204 svga_screen_flush(screen
, &fence
);
205 sws
->fence_finish(sws
, fence
, 0);
206 //sws->fence_reference(sws, &fence, NULL);
211 h
= st
->hw_nblocksy
* st
->base
.block
.height
;
213 for(y
= 0; y
< st
->base
.height
; y
+= h
) {
214 unsigned offset
, length
;
217 if (y
+ h
> st
->base
.height
)
218 h
= st
->base
.height
- y
;
220 /* Transfer band must be aligned to pixel block boundaries */
221 assert(y
% st
->base
.block
.height
== 0);
222 assert(h
% st
->base
.block
.height
== 0);
224 offset
= y
* st
->base
.stride
/ st
->base
.block
.height
;
225 length
= h
* st
->base
.stride
/ st
->base
.block
.height
;
227 sw
= (uint8_t *)st
->swbuf
+ offset
;
229 if(transfer
== SVGA3D_WRITE_HOST_VRAM
) {
230 /* Wait for the previous DMAs to complete */
231 /* TODO: keep one DMA (at half the size) in the background */
233 svga_screen_flush(screen
, &fence
);
234 sws
->fence_finish(sws
, fence
, 0);
235 //sws->fence_reference(sws, &fence, NULL);
238 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_BUFFER_USAGE_CPU_WRITE
);
241 memcpy(hw
, sw
, length
);
242 sws
->buffer_unmap(sws
, st
->hwbuf
);
246 svga_transfer_dma_band(st
, transfer
, y
, h
, srcy
);
248 if(transfer
== SVGA3D_READ_HOST_VRAM
) {
249 svga_screen_flush(screen
, &fence
);
250 sws
->fence_finish(sws
, fence
, 0);
252 hw
= sws
->buffer_map(sws
, st
->hwbuf
, PIPE_BUFFER_USAGE_CPU_READ
);
255 memcpy(sw
, hw
, length
);
256 sws
->buffer_unmap(sws
, st
->hwbuf
);
264 static struct pipe_texture
*
265 svga_texture_create(struct pipe_screen
*screen
,
266 const struct pipe_texture
*templat
)
268 struct svga_screen
*svgascreen
= svga_screen(screen
);
269 struct svga_texture
*tex
= CALLOC_STRUCT(svga_texture
);
270 unsigned width
, height
, depth
;
276 tex
->base
= *templat
;
277 pipe_reference_init(&tex
->base
.reference
, 1);
278 tex
->base
.screen
= screen
;
280 assert(templat
->last_level
< SVGA_MAX_TEXTURE_LEVELS
);
281 if(templat
->last_level
>= SVGA_MAX_TEXTURE_LEVELS
)
284 width
= templat
->width
[0];
285 height
= templat
->height
[0];
286 depth
= templat
->depth
[0];
287 for(level
= 0; level
<= templat
->last_level
; ++level
) {
288 tex
->base
.width
[level
] = width
;
289 tex
->base
.height
[level
] = height
;
290 tex
->base
.depth
[level
] = depth
;
291 tex
->base
.nblocksx
[level
] = pf_get_nblocksx(&tex
->base
.block
, width
);
292 tex
->base
.nblocksy
[level
] = pf_get_nblocksy(&tex
->base
.block
, height
);
293 width
= minify(width
);
294 height
= minify(height
);
295 depth
= minify(depth
);
299 tex
->key
.size
.width
= templat
->width
[0];
300 tex
->key
.size
.height
= templat
->height
[0];
301 tex
->key
.size
.depth
= templat
->depth
[0];
303 if(templat
->target
== PIPE_TEXTURE_CUBE
) {
304 tex
->key
.flags
|= SVGA3D_SURFACE_CUBEMAP
;
305 tex
->key
.numFaces
= 6;
308 tex
->key
.numFaces
= 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_PRIMARY
)
315 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_SCANOUT
;
318 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
319 * know beforehand whether a texture will be used as a rendertarget or not
320 * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
321 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
324 if((templat
->tex_usage
& PIPE_TEXTURE_USAGE_RENDER_TARGET
) &&
325 !pf_is_compressed(templat
->format
))
326 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
329 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)
330 tex
->key
.flags
|= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
332 tex
->key
.numMipLevels
= templat
->last_level
+ 1;
334 tex
->key
.format
= svga_translate_format(templat
->format
);
335 if(tex
->key
.format
== SVGA3D_FORMAT_INVALID
)
338 tex
->key
.cachable
= 1;
340 SVGA_DBG(DEBUG_DMA
, "surface_create for texture\n", tex
->handle
);
341 tex
->handle
= svga_screen_surface_create(svgascreen
, &tex
->key
);
343 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture)\n", tex
->handle
);
354 static struct pipe_texture
*
355 svga_texture_blanket(struct pipe_screen
* screen
,
356 const struct pipe_texture
*base
,
357 const unsigned *stride
,
358 struct pipe_buffer
*buffer
)
360 struct svga_texture
*tex
;
361 struct svga_buffer
*sbuf
= svga_buffer(buffer
);
362 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
365 /* Only supports one type */
366 if (base
->target
!= PIPE_TEXTURE_2D
||
367 base
->last_level
!= 0 ||
368 base
->depth
[0] != 1) {
373 * We currently can't do texture blanket on
374 * SVGA3D_BUFFER. Need to blit to a temporary surface?
377 assert(sbuf
->handle
);
381 if (svga_translate_format(base
->format
) != sbuf
->key
.format
) {
382 unsigned f1
= svga_translate_format(base
->format
);
383 unsigned f2
= sbuf
->key
.format
;
385 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
386 if ( !( (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
387 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
388 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ) ) {
389 debug_printf("%s wrong format %u != %u\n", __FUNCTION__
, f1
, f2
);
394 tex
= CALLOC_STRUCT(svga_texture
);
401 if (sbuf
->key
.format
== 1)
402 tex
->base
.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
403 else if (sbuf
->key
.format
== 2)
404 tex
->base
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
406 pipe_reference_init(&tex
->base
.reference
, 1);
407 tex
->base
.screen
= screen
;
409 SVGA_DBG(DEBUG_DMA
, "blanket sid %p\n", sbuf
->handle
);
411 /* We don't own this storage, so don't try to cache it.
413 assert(sbuf
->key
.cachable
== 0);
414 tex
->key
.cachable
= 0;
415 sws
->surface_reference(sws
, &tex
->handle
, sbuf
->handle
);
422 svga_texture_destroy(struct pipe_texture
*pt
)
424 struct svga_screen
*ss
= svga_screen(pt
->screen
);
425 struct svga_texture
*tex
= (struct svga_texture
*)pt
;
427 ss
->texture_timestamp
++;
429 svga_sampler_view_reference(&tex
->cached_view
, NULL
);
432 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
434 SVGA_DBG(DEBUG_DMA
, "unref sid %p (texture)\n", tex
->handle
);
435 svga_screen_surface_destroy(ss
, &tex
->key
, &tex
->handle
);
442 svga_texture_copy_handle(struct svga_context
*svga
,
443 struct svga_screen
*ss
,
444 struct svga_winsys_surface
*src_handle
,
445 unsigned src_x
, unsigned src_y
, unsigned src_z
,
446 unsigned src_level
, unsigned src_face
,
447 struct svga_winsys_surface
*dst_handle
,
448 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
449 unsigned dst_level
, unsigned dst_face
,
450 unsigned width
, unsigned height
, unsigned depth
)
452 struct svga_surface dst
, src
;
454 SVGA3dCopyBox box
, *boxes
;
458 src
.handle
= src_handle
;
459 src
.real_level
= src_level
;
460 src
.real_face
= src_face
;
463 dst
.handle
= dst_handle
;
464 dst
.real_level
= dst_level
;
465 dst
.real_face
= dst_face
;
479 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
480 src_handle, src_level, src_x, src_y, src_z,
481 dst_handle, dst_level, dst_x, dst_y, dst_z);
485 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
490 svga_context_flush(svga
, NULL
);
491 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
495 assert(ret
== PIPE_OK
);
498 SVGA_FIFOCommitAll(svga
->swc
);
500 pipe_mutex_lock(ss
->swc_mutex
);
501 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
506 ss
->swc
->flush(ss
->swc
, NULL
);
507 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
511 assert(ret
== PIPE_OK
);
514 SVGA_FIFOCommitAll(ss
->swc
);
515 pipe_mutex_unlock(ss
->swc_mutex
);
519 static struct svga_winsys_surface
*
520 svga_texture_view_surface(struct pipe_context
*pipe
,
521 struct svga_texture
*tex
,
522 SVGA3dSurfaceFormat format
,
527 struct svga_host_surface_cache_key
*key
) /* OUT */
529 struct svga_screen
*ss
= svga_screen(tex
->base
.screen
);
530 struct svga_winsys_surface
*handle
;
532 unsigned z_offset
= 0;
535 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
536 face_pick
, zslice_pick
, start_mip
, start_mip
+num_mip
-1);
539 key
->format
= format
;
540 key
->numMipLevels
= num_mip
;
541 key
->size
.width
= tex
->base
.width
[start_mip
];
542 key
->size
.height
= tex
->base
.height
[start_mip
];
543 key
->size
.depth
= zslice_pick
< 0 ? tex
->base
.depth
[start_mip
] : 1;
545 assert(key
->size
.depth
== 1);
547 if(tex
->base
.target
== PIPE_TEXTURE_CUBE
&& face_pick
< 0) {
548 key
->flags
|= SVGA3D_SURFACE_CUBEMAP
;
554 if(key
->format
== SVGA3D_FORMAT_INVALID
) {
559 SVGA_DBG(DEBUG_DMA
, "surface_create for texture view\n");
560 handle
= svga_screen_surface_create(ss
, key
);
566 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture view)\n", handle
);
571 if (zslice_pick
>= 0)
572 z_offset
= zslice_pick
;
574 for (i
= 0; i
< key
->numMipLevels
; i
++) {
575 for (j
= 0; j
< key
->numFaces
; j
++) {
576 if(tex
->defined
[j
+ face_pick
][i
+ start_mip
]) {
577 unsigned depth
= zslice_pick
< 0 ? tex
->base
.depth
[i
+ start_mip
] : 1;
578 svga_texture_copy_handle(svga_context(pipe
),
584 handle
, 0, 0, 0, i
, j
,
585 tex
->base
.width
[i
+ start_mip
],
586 tex
->base
.height
[i
+ start_mip
],
596 static struct pipe_surface
*
597 svga_get_tex_surface(struct pipe_screen
*screen
,
598 struct pipe_texture
*pt
,
599 unsigned face
, unsigned level
, unsigned zslice
,
602 struct svga_texture
*tex
= svga_texture(pt
);
603 struct svga_surface
*s
;
604 boolean render
= flags
& PIPE_BUFFER_USAGE_GPU_WRITE
? TRUE
: FALSE
;
605 boolean view
= FALSE
;
606 SVGA3dSurfaceFormat format
;
608 s
= CALLOC_STRUCT(svga_surface
);
612 pipe_reference_init(&s
->base
.reference
, 1);
613 pipe_texture_reference(&s
->base
.texture
, pt
);
614 s
->base
.format
= pt
->format
;
615 s
->base
.width
= pt
->width
[level
];
616 s
->base
.height
= pt
->height
[level
];
617 s
->base
.usage
= flags
;
618 s
->base
.level
= level
;
620 s
->base
.zslice
= zslice
;
623 format
= svga_translate_format(pt
->format
);
625 format
= svga_translate_format_render(pt
->format
);
627 assert(format
!= SVGA3D_FORMAT_INVALID
);
628 assert(!(flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
631 if (svga_screen(screen
)->debug
.force_surface_view
)
634 /* Currently only used for compressed textures */
636 format
!= svga_translate_format(pt
->format
)) {
641 svga_screen(screen
)->debug
.force_level_surface_view
)
644 if (pt
->target
== PIPE_TEXTURE_3D
)
647 if (svga_screen(screen
)->debug
.no_surface_view
)
651 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
652 pt
, level
, face
, zslice
, s
);
654 s
->handle
= svga_texture_view_surface(NULL
, tex
, format
, level
, 1, face
, zslice
,
660 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
661 pt
, level
, face
, zslice
, s
);
663 memset(&s
->key
, 0, sizeof s
->key
);
664 s
->handle
= tex
->handle
;
666 s
->real_level
= level
;
667 s
->real_zslice
= zslice
;
675 svga_tex_surface_destroy(struct pipe_surface
*surf
)
677 struct svga_surface
*s
= svga_surface(surf
);
678 struct svga_texture
*t
= svga_texture(surf
->texture
);
679 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
681 if(s
->handle
!= t
->handle
) {
682 SVGA_DBG(DEBUG_DMA
, "unref sid %p (tex surface)\n", s
->handle
);
683 svga_screen_surface_destroy(ss
, &s
->key
, &s
->handle
);
686 pipe_texture_reference(&surf
->texture
, NULL
);
692 svga_mark_surface_dirty(struct pipe_surface
*surf
)
694 struct svga_surface
*s
= svga_surface(surf
);
697 struct svga_texture
*tex
= svga_texture(surf
->texture
);
701 if (s
->handle
== tex
->handle
)
702 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
704 /* this will happen later in svga_propagate_surface */
710 void svga_mark_surfaces_dirty(struct svga_context
*svga
)
714 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
715 if (svga
->curr
.framebuffer
.cbufs
[i
])
716 svga_mark_surface_dirty(svga
->curr
.framebuffer
.cbufs
[i
]);
718 if (svga
->curr
.framebuffer
.zsbuf
)
719 svga_mark_surface_dirty(svga
->curr
.framebuffer
.zsbuf
);
723 * Progagate any changes from surfaces to texture.
724 * pipe is optional context to inline the blit command in.
727 svga_propagate_surface(struct pipe_context
*pipe
, struct pipe_surface
*surf
)
729 struct svga_surface
*s
= svga_surface(surf
);
730 struct svga_texture
*tex
= svga_texture(surf
->texture
);
731 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
737 ss
->texture_timestamp
++;
738 tex
->view_age
[surf
->level
] = ++(tex
->age
);
740 if (s
->handle
!= tex
->handle
) {
741 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface propagate: tex %p, level %u, from %p\n", tex
, surf
->level
, surf
);
742 svga_texture_copy_handle(svga_context(pipe
), ss
,
743 s
->handle
, 0, 0, 0, s
->real_level
, s
->real_face
,
744 tex
->handle
, 0, 0, surf
->zslice
, surf
->level
, surf
->face
,
745 tex
->base
.width
[surf
->level
], tex
->base
.height
[surf
->level
], 1);
746 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
751 * Check if we should call svga_propagate_surface on the surface.
754 svga_surface_needs_propagation(struct pipe_surface
*surf
)
756 struct svga_surface
*s
= svga_surface(surf
);
757 struct svga_texture
*tex
= svga_texture(surf
->texture
);
759 return s
->dirty
&& s
->handle
!= tex
->handle
;
763 static struct pipe_transfer
*
764 svga_get_tex_transfer(struct pipe_screen
*screen
,
765 struct pipe_texture
*texture
,
766 unsigned face
, unsigned level
, unsigned zslice
,
767 enum pipe_transfer_usage usage
, unsigned x
, unsigned y
,
768 unsigned w
, unsigned h
)
770 struct svga_screen
*ss
= svga_screen(screen
);
771 struct svga_winsys_screen
*sws
= ss
->sws
;
772 struct svga_transfer
*st
;
774 /* We can't map texture storage directly */
775 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
778 st
= CALLOC_STRUCT(svga_transfer
);
782 st
->base
.format
= texture
->format
;
783 st
->base
.block
= texture
->block
;
788 st
->base
.nblocksx
= pf_get_nblocksx(&texture
->block
, w
);
789 st
->base
.nblocksy
= pf_get_nblocksy(&texture
->block
, h
);
790 st
->base
.stride
= st
->base
.nblocksx
*st
->base
.block
.size
;
791 st
->base
.usage
= usage
;
792 st
->base
.face
= face
;
793 st
->base
.level
= level
;
794 st
->base
.zslice
= zslice
;
796 st
->hw_nblocksy
= st
->base
.nblocksy
;
798 st
->hwbuf
= svga_winsys_buffer_create(ss
,
801 st
->hw_nblocksy
*st
->base
.stride
);
802 while(!st
->hwbuf
&& (st
->hw_nblocksy
/= 2)) {
803 st
->hwbuf
= svga_winsys_buffer_create(ss
,
806 st
->hw_nblocksy
*st
->base
.stride
);
812 if(st
->hw_nblocksy
< st
->base
.nblocksy
) {
813 /* We couldn't allocate a hardware buffer big enough for the transfer,
814 * so allocate regular malloc memory instead */
815 debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
817 (st
->base
.nblocksy
*st
->base
.stride
+ 1023)/1024,
818 (st
->base
.nblocksy
+ st
->hw_nblocksy
- 1)/st
->hw_nblocksy
,
819 (st
->hw_nblocksy
*st
->base
.stride
+ 1023)/1024);
820 st
->swbuf
= MALLOC(st
->base
.nblocksy
*st
->base
.stride
);
825 pipe_texture_reference(&st
->base
.texture
, texture
);
827 if (usage
& PIPE_TRANSFER_READ
)
828 svga_transfer_dma(st
, SVGA3D_READ_HOST_VRAM
);
833 sws
->buffer_destroy(sws
, st
->hwbuf
);
841 svga_transfer_map( struct pipe_screen
*screen
,
842 struct pipe_transfer
*transfer
)
844 struct svga_screen
*ss
= svga_screen(screen
);
845 struct svga_winsys_screen
*sws
= ss
->sws
;
846 struct svga_transfer
*st
= svga_transfer(transfer
);
851 /* The wait for read transfers already happened when svga_transfer_dma
853 return sws
->buffer_map(sws
, st
->hwbuf
,
854 pipe_transfer_buffer_flags(transfer
));
859 svga_transfer_unmap(struct pipe_screen
*screen
,
860 struct pipe_transfer
*transfer
)
862 struct svga_screen
*ss
= svga_screen(screen
);
863 struct svga_winsys_screen
*sws
= ss
->sws
;
864 struct svga_transfer
*st
= svga_transfer(transfer
);
867 sws
->buffer_unmap(sws
, st
->hwbuf
);
872 svga_tex_transfer_destroy(struct pipe_transfer
*transfer
)
874 struct svga_texture
*tex
= svga_texture(transfer
->texture
);
875 struct svga_screen
*ss
= svga_screen(transfer
->texture
->screen
);
876 struct svga_winsys_screen
*sws
= ss
->sws
;
877 struct svga_transfer
*st
= svga_transfer(transfer
);
879 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
880 svga_transfer_dma(st
, SVGA3D_WRITE_HOST_VRAM
);
881 ss
->texture_timestamp
++;
882 tex
->view_age
[transfer
->level
] = ++(tex
->age
);
883 tex
->defined
[transfer
->face
][transfer
->level
] = TRUE
;
886 pipe_texture_reference(&st
->base
.texture
, NULL
);
888 sws
->buffer_destroy(sws
, st
->hwbuf
);
893 svga_screen_init_texture_functions(struct pipe_screen
*screen
)
895 screen
->texture_create
= svga_texture_create
;
896 screen
->texture_destroy
= svga_texture_destroy
;
897 screen
->get_tex_surface
= svga_get_tex_surface
;
898 screen
->tex_surface_destroy
= svga_tex_surface_destroy
;
899 screen
->texture_blanket
= svga_texture_blanket
;
900 screen
->get_tex_transfer
= svga_get_tex_transfer
;
901 screen
->transfer_map
= svga_transfer_map
;
902 screen
->transfer_unmap
= svga_transfer_unmap
;
903 screen
->tex_transfer_destroy
= svga_tex_transfer_destroy
;
906 /***********************************************************************
909 struct svga_sampler_view
*
910 svga_get_tex_sampler_view(struct pipe_context
*pipe
, struct pipe_texture
*pt
,
911 unsigned min_lod
, unsigned max_lod
)
913 struct svga_screen
*ss
= svga_screen(pt
->screen
);
914 struct svga_texture
*tex
= svga_texture(pt
);
915 struct svga_sampler_view
*sv
= NULL
;
916 SVGA3dSurfaceFormat format
= svga_translate_format(pt
->format
);
920 assert(min_lod
>= 0);
921 assert(min_lod
<= max_lod
);
922 assert(max_lod
<= pt
->last_level
);
925 /* Is a view needed */
928 * Can't control max lod. For first level views and when we only
929 * look at one level we disable mip filtering to achive the same
932 if (min_lod
== 0 && max_lod
>= pt
->last_level
)
935 if (pf_is_compressed(pt
->format
) && view
) {
936 format
= svga_translate_format_render(pt
->format
);
939 if (ss
->debug
.no_sampler_view
)
942 if (ss
->debug
.force_sampler_view
)
946 /* First try the cache */
948 pipe_mutex_lock(ss
->tex_mutex
);
949 if (tex
->cached_view
&&
950 tex
->cached_view
->min_lod
== min_lod
&&
951 tex
->cached_view
->max_lod
== max_lod
) {
952 svga_sampler_view_reference(&sv
, tex
->cached_view
);
953 pipe_mutex_unlock(ss
->tex_mutex
);
954 SVGA_DBG(DEBUG_VIEWS
, "svga: Sampler view: reuse %p, %u %u, last %u\n",
955 pt
, min_lod
, max_lod
, pt
->last_level
);
956 svga_validate_sampler_view(svga_context(pipe
), sv
);
959 pipe_mutex_unlock(ss
->tex_mutex
);
962 sv
= CALLOC_STRUCT(svga_sampler_view
);
963 pipe_reference_init(&sv
->reference
, 1);
964 pipe_texture_reference(&sv
->texture
, pt
);
965 sv
->min_lod
= min_lod
;
966 sv
->max_lod
= max_lod
;
968 /* No view needed just use the whole texture */
970 SVGA_DBG(DEBUG_VIEWS
,
971 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
972 pt
, min_lod
, max_lod
,
973 max_lod
- min_lod
+ 1,
978 sv
->key
.cachable
= 0;
979 sv
->handle
= tex
->handle
;
983 SVGA_DBG(DEBUG_VIEWS
,
984 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
985 pt
, min_lod
, max_lod
,
986 max_lod
- min_lod
+ 1,
993 sv
->handle
= svga_texture_view_surface(pipe
, tex
, format
,
995 max_lod
- min_lod
+ 1,
1001 sv
->key
.cachable
= 0;
1002 sv
->handle
= tex
->handle
;
1006 pipe_mutex_lock(ss
->tex_mutex
);
1007 svga_sampler_view_reference(&tex
->cached_view
, sv
);
1008 pipe_mutex_unlock(ss
->tex_mutex
);
1014 svga_validate_sampler_view(struct svga_context
*svga
, struct svga_sampler_view
*v
)
1016 struct svga_texture
*tex
= svga_texture(v
->texture
);
1023 if (v
->handle
== tex
->handle
)
1028 if(tex
->base
.target
== PIPE_TEXTURE_CUBE
)
1033 for (i
= v
->min_lod
; i
<= v
->max_lod
; i
++) {
1034 for (k
= 0; k
< numFaces
; k
++) {
1035 if (v
->age
< tex
->view_age
[i
])
1036 svga_texture_copy_handle(svga
, NULL
,
1037 tex
->handle
, 0, 0, 0, i
, k
,
1038 v
->handle
, 0, 0, 0, i
- v
->min_lod
, k
,
1040 tex
->base
.height
[i
],
1041 tex
->base
.depth
[i
]);
1049 svga_destroy_sampler_view_priv(struct svga_sampler_view
*v
)
1051 struct svga_texture
*tex
= svga_texture(v
->texture
);
1053 if(v
->handle
!= tex
->handle
) {
1054 struct svga_screen
*ss
= svga_screen(v
->texture
->screen
);
1055 SVGA_DBG(DEBUG_DMA
, "unref sid %p (sampler view)\n", v
->handle
);
1056 svga_screen_surface_destroy(ss
, &v
->key
, &v
->handle
);
1058 pipe_texture_reference(&v
->texture
, NULL
);
1063 svga_screen_buffer_from_texture(struct pipe_texture
*texture
,
1064 struct pipe_buffer
**buffer
,
1067 struct svga_texture
*stex
= svga_texture(texture
);
1069 *buffer
= svga_screen_buffer_wrap_surface
1071 svga_translate_format(texture
->format
),
1074 *stride
= pf_get_nblocksx(&texture
->block
, texture
->width
[0]) *
1075 texture
->block
.size
;
1077 return *buffer
!= NULL
;
1081 struct svga_winsys_surface
*
1082 svga_screen_texture_get_winsys_surface(struct pipe_texture
*texture
)
1084 struct svga_winsys_screen
*sws
= svga_winsys_screen(texture
->screen
);
1085 struct svga_winsys_surface
*vsurf
= NULL
;
1087 assert(svga_texture(texture
)->key
.cachable
== 0);
1088 svga_texture(texture
)->key
.cachable
= 0;
1089 sws
->surface_reference(sws
, &vsurf
, svga_texture(texture
)->handle
);