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_winsys_screen
*sws
= svgascreen
->sws
;
270 struct svga_texture
*tex
= CALLOC_STRUCT(svga_texture
);
271 unsigned width
, height
, depth
;
272 SVGA3dSurfaceFlags flags
= 0;
273 SVGA3dSurfaceFormat format
;
282 tex
->base
= *templat
;
283 pipe_reference_init(&tex
->base
.reference
, 1);
284 tex
->base
.screen
= screen
;
286 assert(templat
->last_level
< SVGA_MAX_TEXTURE_LEVELS
);
287 if(templat
->last_level
>= SVGA_MAX_TEXTURE_LEVELS
)
290 width
= templat
->width
[0];
291 height
= templat
->height
[0];
292 depth
= templat
->depth
[0];
293 for(level
= 0; level
<= templat
->last_level
; ++level
) {
294 tex
->base
.width
[level
] = width
;
295 tex
->base
.height
[level
] = height
;
296 tex
->base
.depth
[level
] = depth
;
297 tex
->base
.nblocksx
[level
] = pf_get_nblocksx(&tex
->base
.block
, width
);
298 tex
->base
.nblocksy
[level
] = pf_get_nblocksy(&tex
->base
.block
, height
);
299 width
= minify(width
);
300 height
= minify(height
);
301 depth
= minify(depth
);
304 size
.width
= templat
->width
[0];
305 size
.height
= templat
->height
[0];
306 size
.depth
= templat
->depth
[0];
308 if(templat
->target
== PIPE_TEXTURE_CUBE
) {
309 flags
|= SVGA3D_SURFACE_CUBEMAP
;
316 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_SAMPLER
)
317 flags
|= SVGA3D_SURFACE_HINT_TEXTURE
;
319 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_PRIMARY
)
320 flags
|= SVGA3D_SURFACE_HINT_SCANOUT
;
323 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
324 * know beforehand whether a texture will be used as a rendertarget or not
325 * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
326 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
329 if((templat
->tex_usage
& PIPE_TEXTURE_USAGE_RENDER_TARGET
) &&
330 !pf_is_compressed(templat
->format
))
331 flags
|= SVGA3D_SURFACE_HINT_RENDERTARGET
;
334 if(templat
->tex_usage
& PIPE_TEXTURE_USAGE_DEPTH_STENCIL
)
335 flags
|= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
337 numMipLevels
= templat
->last_level
+ 1;
339 format
= svga_translate_format(templat
->format
);
340 if(format
== SVGA3D_FORMAT_INVALID
)
343 tex
->handle
= sws
->surface_create(sws
, flags
, format
, size
, numFaces
, numMipLevels
);
345 SVGA_DBG(DEBUG_DMA
, "create sid %p (texture)\n", tex
->handle
);
356 static struct pipe_texture
*
357 svga_texture_blanket(struct pipe_screen
* screen
,
358 const struct pipe_texture
*base
,
359 const unsigned *stride
,
360 struct pipe_buffer
*buffer
)
362 struct svga_texture
*tex
;
363 struct svga_buffer
*sbuf
= svga_buffer(buffer
);
364 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
367 /* Only supports one type */
368 if (base
->target
!= PIPE_TEXTURE_2D
||
369 base
->last_level
!= 0 ||
370 base
->depth
[0] != 1) {
375 * We currently can't do texture blanket on
376 * SVGA3D_BUFFER. Need to blit to a temporary surface?
379 assert(sbuf
->handle
);
383 if (svga_translate_format(base
->format
) != sbuf
->key
.format
) {
384 unsigned f1
= svga_translate_format(base
->format
);
385 unsigned f2
= sbuf
->key
.format
;
387 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
388 if ( !( (f1
== SVGA3D_X8R8G8B8
&& f2
== SVGA3D_A8R8G8B8
) ||
389 (f1
== SVGA3D_A8R8G8B8
&& f2
== SVGA3D_X8R8G8B8
) ||
390 (f1
== SVGA3D_Z_D24X8
&& f2
== SVGA3D_Z_D24S8
) ) ) {
391 debug_printf("%s wrong format %u != %u\n", __FUNCTION__
, f1
, f2
);
396 tex
= CALLOC_STRUCT(svga_texture
);
402 if (sbuf
->key
.format
== 1)
403 tex
->base
.format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
404 else if (sbuf
->key
.format
== 2)
405 tex
->base
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
407 pipe_reference_init(&tex
->base
.reference
, 1);
408 tex
->base
.screen
= screen
;
410 sws
->surface_reference(sws
, &tex
->handle
, sbuf
->handle
);
417 svga_texture_destroy(struct pipe_texture
*pt
)
419 struct svga_screen
*ss
= svga_screen(pt
->screen
);
420 struct svga_texture
*tex
= (struct svga_texture
*)pt
;
422 ss
->texture_timestamp
++;
424 svga_sampler_view_reference(&tex
->cached_view
, NULL
);
427 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
429 SVGA_DBG(DEBUG_DMA
, "unref sid %p (texture)\n", tex
->handle
);
430 ss
->sws
->surface_reference(ss
->sws
, &tex
->handle
, NULL
);
437 svga_texture_copy_handle(struct svga_context
*svga
,
438 struct svga_screen
*ss
,
439 struct svga_winsys_surface
*src_handle
,
440 unsigned src_x
, unsigned src_y
, unsigned src_z
,
441 unsigned src_level
, unsigned src_face
,
442 struct svga_winsys_surface
*dst_handle
,
443 unsigned dst_x
, unsigned dst_y
, unsigned dst_z
,
444 unsigned dst_level
, unsigned dst_face
,
445 unsigned width
, unsigned height
, unsigned depth
)
447 struct svga_surface dst
, src
;
449 SVGA3dCopyBox box
, *boxes
;
453 src
.handle
= src_handle
;
454 src
.real_level
= src_level
;
455 src
.real_face
= src_face
;
458 dst
.handle
= dst_handle
;
459 dst
.real_level
= dst_level
;
460 dst
.real_face
= dst_face
;
474 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
475 src_handle, src_level, src_x, src_y, src_z,
476 dst_handle, dst_level, dst_x, dst_y, dst_z);
480 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
485 svga_context_flush(svga
, NULL
);
486 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
490 assert(ret
== PIPE_OK
);
493 SVGA_FIFOCommitAll(svga
->swc
);
495 pipe_mutex_lock(ss
->swc_mutex
);
496 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
501 ss
->swc
->flush(ss
->swc
, NULL
);
502 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
506 assert(ret
== PIPE_OK
);
509 SVGA_FIFOCommitAll(ss
->swc
);
510 pipe_mutex_unlock(ss
->swc_mutex
);
514 static struct svga_winsys_surface
*
515 svga_texture_view_surface(struct pipe_context
*pipe
,
516 struct svga_texture
*tex
,
517 SVGA3dSurfaceFormat format
,
523 struct svga_screen
*ss
= svga_screen(tex
->base
.screen
);
524 struct svga_winsys_screen
*sws
= ss
->sws
;
525 struct svga_winsys_surface
*handle
;
527 SVGA3dSurfaceFlags flags
= 0;
530 uint32 numMipLevels
= num_mip
;
531 unsigned z_offset
= 0;
534 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
535 face_pick
, zslice_pick
, start_mip
, start_mip
+num_mip
-1);
537 size
.width
= tex
->base
.width
[start_mip
];
538 size
.height
= tex
->base
.height
[start_mip
];
539 size
.depth
= zslice_pick
< 0 ? tex
->base
.depth
[start_mip
] : 1;
540 assert(size
.depth
== 1);
542 if(tex
->base
.target
== PIPE_TEXTURE_CUBE
&& face_pick
< 0) {
543 flags
|= SVGA3D_SURFACE_CUBEMAP
;
549 if(format
== SVGA3D_FORMAT_INVALID
)
552 handle
= sws
->surface_create(sws
, flags
, format
, size
, numFaces
, numMipLevels
);
557 SVGA_DBG(DEBUG_DMA
, "create sid %p (texture view)\n", handle
);
562 if (zslice_pick
>= 0)
563 z_offset
= zslice_pick
;
565 for (i
= 0; i
< num_mip
; i
++) {
566 for (j
= 0; j
< numFaces
; j
++) {
567 if(tex
->defined
[j
+ face_pick
][i
+ start_mip
]) {
568 unsigned depth
= zslice_pick
< 0 ? tex
->base
.depth
[i
+ start_mip
] : 1;
569 svga_texture_copy_handle(svga_context(pipe
), ss
,
570 tex
->handle
, 0, 0, z_offset
, i
+ start_mip
, j
+ face_pick
,
571 handle
, 0, 0, 0, i
, j
,
572 tex
->base
.width
[i
+ start_mip
], tex
->base
.height
[i
+ start_mip
], depth
);
581 static struct pipe_surface
*
582 svga_get_tex_surface(struct pipe_screen
*screen
,
583 struct pipe_texture
*pt
,
584 unsigned face
, unsigned level
, unsigned zslice
,
587 struct svga_texture
*tex
= svga_texture(pt
);
588 struct svga_surface
*s
;
589 struct pipe_surface
*ps
;
590 boolean render
= flags
& PIPE_BUFFER_USAGE_GPU_WRITE
? TRUE
: FALSE
;
591 boolean view
= FALSE
;
592 SVGA3dSurfaceFormat format
;
594 s
= CALLOC_STRUCT(svga_surface
);
599 pipe_reference_init(&ps
->reference
, 1);
600 pipe_texture_reference(&ps
->texture
, pt
);
601 ps
->format
= pt
->format
;
602 ps
->width
= pt
->width
[level
];
603 ps
->height
= pt
->height
[level
];
610 format
= svga_translate_format(pt
->format
);
612 format
= svga_translate_format_render(pt
->format
);
614 assert(format
!= SVGA3D_FORMAT_INVALID
);
615 assert(!(flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
618 if (svga_screen(screen
)->debug
.force_surface_view
)
621 /* Currently only used for compressed textures */
622 if (render
&& (format
!= svga_translate_format(pt
->format
))) {
626 if (level
!= 0 && svga_screen(screen
)->debug
.force_level_surface_view
)
629 if (pt
->target
== PIPE_TEXTURE_3D
)
632 if (svga_screen(screen
)->debug
.no_surface_view
)
636 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
637 pt
, level
, face
, zslice
, ps
);
639 s
->handle
= svga_texture_view_surface(NULL
, tex
, format
, level
, 1, face
, zslice
);
644 struct svga_winsys_screen
*sws
= svga_winsys_screen(screen
);
646 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
647 pt
, level
, face
, zslice
, ps
);
649 sws
->surface_reference(sws
, &s
->handle
, tex
->handle
);
651 s
->real_level
= level
;
652 s
->real_zslice
= zslice
;
660 svga_tex_surface_destroy(struct pipe_surface
*surf
)
662 struct svga_surface
*s
= svga_surface(surf
);
663 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
665 SVGA_DBG(DEBUG_DMA
, "unref sid %p (tex surface)\n", s
->handle
);
666 ss
->sws
->surface_reference(ss
->sws
, &s
->handle
, NULL
);
667 pipe_texture_reference(&surf
->texture
, NULL
);
673 svga_mark_surface_dirty(struct pipe_surface
*surf
)
675 struct svga_surface
*s
= svga_surface(surf
);
678 struct svga_texture
*tex
= svga_texture(surf
->texture
);
682 if (s
->handle
== tex
->handle
)
683 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
685 /* this will happen later in svga_propagate_surface */
691 void svga_mark_surfaces_dirty(struct svga_context
*svga
)
695 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
696 if (svga
->curr
.framebuffer
.cbufs
[i
])
697 svga_mark_surface_dirty(svga
->curr
.framebuffer
.cbufs
[i
]);
699 if (svga
->curr
.framebuffer
.zsbuf
)
700 svga_mark_surface_dirty(svga
->curr
.framebuffer
.zsbuf
);
704 * Progagate any changes from surfaces to texture.
705 * pipe is optional context to inline the blit command in.
708 svga_propagate_surface(struct pipe_context
*pipe
, struct pipe_surface
*surf
)
710 struct svga_surface
*s
= svga_surface(surf
);
711 struct svga_texture
*tex
= svga_texture(surf
->texture
);
712 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
718 ss
->texture_timestamp
++;
719 tex
->view_age
[surf
->level
] = ++(tex
->age
);
721 if (s
->handle
!= tex
->handle
) {
722 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface propagate: tex %p, level %u, from %p\n", tex
, surf
->level
, surf
);
723 svga_texture_copy_handle(svga_context(pipe
), ss
,
724 s
->handle
, 0, 0, 0, s
->real_level
, s
->real_face
,
725 tex
->handle
, 0, 0, surf
->zslice
, surf
->level
, surf
->face
,
726 tex
->base
.width
[surf
->level
], tex
->base
.height
[surf
->level
], 1);
727 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
732 * Check if we should call svga_propagate_surface on the surface.
735 svga_surface_needs_propagation(struct pipe_surface
*surf
)
737 struct svga_surface
*s
= svga_surface(surf
);
738 struct svga_texture
*tex
= svga_texture(surf
->texture
);
740 return s
->dirty
&& s
->handle
!= tex
->handle
;
744 static struct pipe_transfer
*
745 svga_get_tex_transfer(struct pipe_screen
*screen
,
746 struct pipe_texture
*texture
,
747 unsigned face
, unsigned level
, unsigned zslice
,
748 enum pipe_transfer_usage usage
, unsigned x
, unsigned y
,
749 unsigned w
, unsigned h
)
751 struct svga_screen
*ss
= svga_screen(screen
);
752 struct svga_winsys_screen
*sws
= ss
->sws
;
753 struct svga_transfer
*st
;
755 /* We can't map texture storage directly */
756 if (usage
& PIPE_TRANSFER_MAP_DIRECTLY
)
759 st
= CALLOC_STRUCT(svga_transfer
);
763 st
->base
.format
= texture
->format
;
764 st
->base
.block
= texture
->block
;
769 st
->base
.nblocksx
= pf_get_nblocksx(&texture
->block
, w
);
770 st
->base
.nblocksy
= pf_get_nblocksy(&texture
->block
, h
);
771 st
->base
.stride
= st
->base
.nblocksx
*st
->base
.block
.size
;
772 st
->base
.usage
= usage
;
773 st
->base
.face
= face
;
774 st
->base
.level
= level
;
775 st
->base
.zslice
= zslice
;
777 st
->hw_nblocksy
= st
->base
.nblocksy
;
779 st
->hwbuf
= svga_winsys_buffer_create(ss
,
782 st
->hw_nblocksy
*st
->base
.stride
);
783 while(!st
->hwbuf
&& (st
->hw_nblocksy
/= 2)) {
784 st
->hwbuf
= svga_winsys_buffer_create(ss
,
787 st
->hw_nblocksy
*st
->base
.stride
);
793 if(st
->hw_nblocksy
< st
->base
.nblocksy
) {
794 /* We couldn't allocate a hardware buffer big enough for the transfer,
795 * so allocate regular malloc memory instead */
796 debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
798 (st
->base
.nblocksy
*st
->base
.stride
+ 1023)/1024,
799 (st
->base
.nblocksy
+ st
->hw_nblocksy
- 1)/st
->hw_nblocksy
,
800 (st
->hw_nblocksy
*st
->base
.stride
+ 1023)/1024);
801 st
->swbuf
= MALLOC(st
->base
.nblocksy
*st
->base
.stride
);
806 pipe_texture_reference(&st
->base
.texture
, texture
);
808 if (usage
& PIPE_TRANSFER_READ
)
809 svga_transfer_dma(st
, SVGA3D_READ_HOST_VRAM
);
814 sws
->buffer_destroy(sws
, st
->hwbuf
);
822 svga_transfer_map( struct pipe_screen
*screen
,
823 struct pipe_transfer
*transfer
)
825 struct svga_screen
*ss
= svga_screen(screen
);
826 struct svga_winsys_screen
*sws
= ss
->sws
;
827 struct svga_transfer
*st
= svga_transfer(transfer
);
832 /* The wait for read transfers already happened when svga_transfer_dma
834 return sws
->buffer_map(sws
, st
->hwbuf
,
835 pipe_transfer_buffer_flags(transfer
));
840 svga_transfer_unmap(struct pipe_screen
*screen
,
841 struct pipe_transfer
*transfer
)
843 struct svga_screen
*ss
= svga_screen(screen
);
844 struct svga_winsys_screen
*sws
= ss
->sws
;
845 struct svga_transfer
*st
= svga_transfer(transfer
);
848 sws
->buffer_unmap(sws
, st
->hwbuf
);
853 svga_tex_transfer_destroy(struct pipe_transfer
*transfer
)
855 struct svga_texture
*tex
= svga_texture(transfer
->texture
);
856 struct svga_screen
*ss
= svga_screen(transfer
->texture
->screen
);
857 struct svga_winsys_screen
*sws
= ss
->sws
;
858 struct svga_transfer
*st
= svga_transfer(transfer
);
860 if (st
->base
.usage
& PIPE_TRANSFER_WRITE
) {
861 svga_transfer_dma(st
, SVGA3D_WRITE_HOST_VRAM
);
862 ss
->texture_timestamp
++;
863 tex
->view_age
[transfer
->level
] = ++(tex
->age
);
864 tex
->defined
[transfer
->face
][transfer
->level
] = TRUE
;
867 pipe_texture_reference(&st
->base
.texture
, NULL
);
869 sws
->buffer_destroy(sws
, st
->hwbuf
);
874 svga_screen_init_texture_functions(struct pipe_screen
*screen
)
876 screen
->texture_create
= svga_texture_create
;
877 screen
->texture_destroy
= svga_texture_destroy
;
878 screen
->get_tex_surface
= svga_get_tex_surface
;
879 screen
->tex_surface_destroy
= svga_tex_surface_destroy
;
880 screen
->texture_blanket
= svga_texture_blanket
;
881 screen
->get_tex_transfer
= svga_get_tex_transfer
;
882 screen
->transfer_map
= svga_transfer_map
;
883 screen
->transfer_unmap
= svga_transfer_unmap
;
884 screen
->tex_transfer_destroy
= svga_tex_transfer_destroy
;
887 /***********************************************************************
890 struct svga_sampler_view
*
891 svga_get_tex_sampler_view(struct pipe_context
*pipe
, struct pipe_texture
*pt
,
892 unsigned min_lod
, unsigned max_lod
)
894 struct svga_screen
*ss
= svga_screen(pt
->screen
);
895 struct svga_winsys_screen
*sws
= ss
->sws
;
896 struct svga_texture
*tex
= svga_texture(pt
);
897 struct svga_sampler_view
*sv
= NULL
;
898 SVGA3dSurfaceFormat format
= svga_translate_format(pt
->format
);
902 assert(min_lod
>= 0);
903 assert(min_lod
<= max_lod
);
904 assert(max_lod
<= pt
->last_level
);
907 /* Is a view needed */
910 * Can't control max lod. For first level views and when we only
911 * look at one level we disable mip filtering to achive the same
914 if (min_lod
== 0 && max_lod
>= pt
->last_level
)
917 if (pf_is_compressed(pt
->format
) && view
) {
918 format
= svga_translate_format_render(pt
->format
);
921 if (ss
->debug
.no_sampler_view
)
924 if (ss
->debug
.force_sampler_view
)
928 /* First try the cache */
930 pipe_mutex_lock(ss
->tex_mutex
);
931 if (tex
->cached_view
&&
932 tex
->cached_view
->min_lod
== min_lod
&&
933 tex
->cached_view
->max_lod
== max_lod
) {
934 svga_sampler_view_reference(&sv
, tex
->cached_view
);
935 pipe_mutex_unlock(ss
->tex_mutex
);
936 SVGA_DBG(DEBUG_VIEWS
, "svga: Sampler view: reuse %p, %u %u, last %u\n",
937 pt
, min_lod
, max_lod
, pt
->last_level
);
938 svga_validate_sampler_view(svga_context(pipe
), sv
);
941 pipe_mutex_unlock(ss
->tex_mutex
);
944 sv
= CALLOC_STRUCT(svga_sampler_view
);
945 pipe_reference_init(&sv
->reference
, 1);
947 sv
->min_lod
= min_lod
;
948 sv
->max_lod
= max_lod
;
950 /* No view needed just use the whole texture */
952 SVGA_DBG(DEBUG_VIEWS
,
953 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
954 pt
, min_lod
, max_lod
,
955 max_lod
- min_lod
+ 1,
960 sws
->surface_reference(sws
, &sv
->handle
, tex
->handle
);
964 SVGA_DBG(DEBUG_VIEWS
,
965 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
966 pt
, min_lod
, max_lod
,
967 max_lod
- min_lod
+ 1,
974 sv
->handle
= svga_texture_view_surface(pipe
, tex
, format
,
976 max_lod
- min_lod
+ 1,
981 sws
->surface_reference(sws
, &sv
->handle
, tex
->handle
);
985 pipe_mutex_lock(ss
->tex_mutex
);
986 svga_sampler_view_reference(&tex
->cached_view
, sv
);
987 pipe_mutex_unlock(ss
->tex_mutex
);
993 svga_validate_sampler_view(struct svga_context
*svga
, struct svga_sampler_view
*v
)
995 struct svga_texture
*tex
= v
->texture
;
1002 if (v
->handle
== v
->texture
->handle
)
1007 if(tex
->base
.target
== PIPE_TEXTURE_CUBE
)
1012 for (i
= v
->min_lod
; i
<= v
->max_lod
; i
++) {
1013 for (k
= 0; k
< numFaces
; k
++) {
1014 if (v
->age
< tex
->view_age
[i
])
1015 svga_texture_copy_handle(svga
, NULL
,
1016 tex
->handle
, 0, 0, 0, i
, k
,
1017 v
->handle
, 0, 0, 0, i
- v
->min_lod
, k
,
1019 tex
->base
.height
[i
],
1020 tex
->base
.depth
[i
]);
1028 svga_destroy_sampler_view_priv(struct svga_sampler_view
*v
)
1030 struct svga_screen
*ss
= svga_screen(v
->texture
->base
.screen
);
1032 SVGA_DBG(DEBUG_DMA
, "unref sid %p (sampler view)\n", v
->handle
);
1033 ss
->sws
->surface_reference(ss
->sws
, &v
->handle
, NULL
);
1039 svga_screen_buffer_from_texture(struct pipe_texture
*texture
,
1040 struct pipe_buffer
**buffer
,
1043 struct svga_texture
*stex
= svga_texture(texture
);
1045 *buffer
= svga_screen_buffer_wrap_surface
1047 svga_translate_format(texture
->format
),
1050 *stride
= pf_get_nblocksx(&texture
->block
, texture
->width
[0]) *
1051 texture
->block
.size
;
1053 return *buffer
!= NULL
;
1057 struct svga_winsys_surface
*
1058 svga_screen_texture_get_winsys_surface(struct pipe_texture
*texture
)
1060 struct svga_winsys_screen
*sws
= svga_winsys_screen(texture
->screen
);
1061 struct svga_winsys_surface
*vsurf
= NULL
;
1063 sws
->surface_reference(sws
, &vsurf
, svga_texture(texture
)->handle
);