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"
45 /* XXX: This isn't a real hardware flag, but just a hack for kernel to
46 * know about primary surfaces. Find a better way to accomplish this.
48 #define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)
52 * Helper function and arrays
56 svga_translate_format(enum pipe_format format)
60 case PIPE_FORMAT_B8G8R8A8_UNORM:
61 return SVGA3D_A8R8G8B8;
62 case PIPE_FORMAT_B8G8R8X8_UNORM:
63 return SVGA3D_X8R8G8B8;
65 /* Required for GL2.1:
67 case PIPE_FORMAT_B8G8R8A8_SRGB:
68 return SVGA3D_A8R8G8B8;
70 case PIPE_FORMAT_B5G6R5_UNORM:
72 case PIPE_FORMAT_B5G5R5A1_UNORM:
73 return SVGA3D_A1R5G5B5;
74 case PIPE_FORMAT_B4G4R4A4_UNORM:
75 return SVGA3D_A4R4G4B4;
78 /* XXX: Doesn't seem to work properly.
79 case PIPE_FORMAT_Z32_UNORM:
82 case PIPE_FORMAT_Z16_UNORM:
84 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
85 return SVGA3D_Z_D24S8;
86 case PIPE_FORMAT_X8Z24_UNORM:
87 return SVGA3D_Z_D24X8;
89 case PIPE_FORMAT_A8_UNORM:
91 case PIPE_FORMAT_L8_UNORM:
92 return SVGA3D_LUMINANCE8;
94 case PIPE_FORMAT_DXT1_RGB:
95 case PIPE_FORMAT_DXT1_RGBA:
97 case PIPE_FORMAT_DXT3_RGBA:
99 case PIPE_FORMAT_DXT5_RGBA:
103 return SVGA3D_FORMAT_INVALID;
109 svga_translate_format_render(enum pipe_format format)
112 case PIPE_FORMAT_B8G8R8A8_UNORM:
113 case PIPE_FORMAT_B8G8R8X8_UNORM:
114 case PIPE_FORMAT_B5G5R5A1_UNORM:
115 case PIPE_FORMAT_B4G4R4A4_UNORM:
116 case PIPE_FORMAT_B5G6R5_UNORM:
117 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
118 case PIPE_FORMAT_X8Z24_UNORM:
119 case PIPE_FORMAT_Z32_UNORM:
120 case PIPE_FORMAT_Z16_UNORM:
121 case PIPE_FORMAT_L8_UNORM:
122 return svga_translate_format(format);
125 /* For on host conversion */
126 case PIPE_FORMAT_DXT1_RGB:
127 return SVGA3D_X8R8G8B8;
128 case PIPE_FORMAT_DXT1_RGBA:
129 case PIPE_FORMAT_DXT3_RGBA:
130 case PIPE_FORMAT_DXT5_RGBA:
131 return SVGA3D_A8R8G8B8;
135 return SVGA3D_FORMAT_INVALID;
141 svga_transfer_dma_band(struct svga_transfer *st,
142 SVGA3dTransferType transfer,
143 unsigned y, unsigned h, unsigned srcy)
145 struct svga_texture *texture = svga_texture(st->base.texture);
146 struct svga_screen *screen = svga_screen(texture->base.screen);
150 SVGA_DBG(DEBUG_DMA, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
151 transfer == SVGA3D_WRITE_HOST_VRAM ? "to" : "from",
157 st->base.x + st->base.width,
160 util_format_get_blocksize(texture->base.format)*8/
161 (util_format_get_blockwidth(texture->base.format)*util_format_get_blockheight(texture->base.format)));
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 unsigned blockheight = util_format_get_blockheight(st->base.texture->format);
212 h = st->hw_nblocksy * blockheight;
214 for(y = 0; y < st->base.height; y += h) {
215 unsigned offset, length;
218 if (y + h > st->base.height)
219 h = st->base.height - y;
221 /* Transfer band must be aligned to pixel block boundaries */
222 assert(y % blockheight == 0);
223 assert(h % blockheight == 0);
225 offset = y * st->base.stride / blockheight;
226 length = h * st->base.stride / blockheight;
228 sw = (uint8_t *)st->swbuf + offset;
230 if(transfer == SVGA3D_WRITE_HOST_VRAM) {
231 /* Wait for the previous DMAs to complete */
232 /* TODO: keep one DMA (at half the size) in the background */
234 svga_screen_flush(screen, &fence);
235 sws->fence_finish(sws, fence, 0);
236 sws->fence_reference(sws, &fence, NULL);
239 hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
242 memcpy(hw, sw, length);
243 sws->buffer_unmap(sws, st->hwbuf);
247 svga_transfer_dma_band(st, transfer, y, h, srcy);
249 if(transfer == SVGA3D_READ_HOST_VRAM) {
250 svga_screen_flush(screen, &fence);
251 sws->fence_finish(sws, fence, 0);
253 hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_READ);
256 memcpy(sw, hw, length);
257 sws->buffer_unmap(sws, st->hwbuf);
265 static struct pipe_texture *
266 svga_texture_create(struct pipe_screen *screen,
267 const struct pipe_texture *templat)
269 struct svga_screen *svgascreen = svga_screen(screen);
270 struct svga_texture *tex = CALLOC_STRUCT(svga_texture);
271 unsigned width, height, depth;
277 tex->base = *templat;
278 pipe_reference_init(&tex->base.reference, 1);
279 tex->base.screen = screen;
281 assert(templat->last_level < SVGA_MAX_TEXTURE_LEVELS);
282 if(templat->last_level >= SVGA_MAX_TEXTURE_LEVELS)
285 width = templat->width0;
286 height = templat->height0;
287 depth = templat->depth0;
288 for(level = 0; level <= templat->last_level; ++level) {
289 width = u_minify(width, 1);
290 height = u_minify(height, 1);
291 depth = u_minify(depth, 1);
295 tex->key.size.width = templat->width0;
296 tex->key.size.height = templat->height0;
297 tex->key.size.depth = templat->depth0;
299 if(templat->target == PIPE_TEXTURE_CUBE) {
300 tex->key.flags |= SVGA3D_SURFACE_CUBEMAP;
301 tex->key.numFaces = 6;
304 tex->key.numFaces = 1;
307 tex->key.cachable = 1;
309 if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
310 tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
312 if(templat->tex_usage & PIPE_TEXTURE_USAGE_DISPLAY_TARGET) {
313 tex->key.cachable = 0;
316 if(templat->tex_usage & PIPE_TEXTURE_USAGE_SHARED) {
317 tex->key.cachable = 0;
320 if(templat->tex_usage & PIPE_TEXTURE_USAGE_SCANOUT) {
321 tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
322 tex->key.cachable = 0;
326 * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
327 * know beforehand whether a texture will be used as a rendertarget or not
328 * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
329 * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
332 if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
333 !util_format_is_s3tc(templat->format))
334 tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
337 if(templat->tex_usage & PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
338 tex->key.flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
340 tex->key.numMipLevels = templat->last_level + 1;
342 tex->key.format = svga_translate_format(templat->format);
343 if(tex->key.format == SVGA3D_FORMAT_INVALID)
346 SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
347 tex->handle = svga_screen_surface_create(svgascreen, &tex->key);
349 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture)\n", tex->handle);
363 static struct pipe_texture *
364 svga_screen_texture_from_handle(struct pipe_screen *screen,
365 const struct pipe_texture *base,
366 struct winsys_handle *whandle)
368 struct svga_winsys_screen *sws = svga_winsys_screen(screen);
369 struct svga_winsys_surface *srf;
370 struct svga_texture *tex;
371 enum SVGA3dSurfaceFormat format = 0;
374 /* Only supports one type */
375 if (base->target != PIPE_TEXTURE_2D ||
376 base->last_level != 0 ||
381 srf = sws->surface_from_handle(sws, whandle, &format);
386 if (svga_translate_format(base->format) != format) {
387 unsigned f1 = svga_translate_format(base->format);
388 unsigned f2 = format;
390 /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
391 if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
392 (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
393 (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ) ) {
394 debug_printf("%s wrong format %u != %u\n", __FUNCTION__, f1, f2);
399 tex = CALLOC_STRUCT(svga_texture);
407 tex->base.format = PIPE_FORMAT_B8G8R8X8_UNORM;
408 else if (format == 2)
409 tex->base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
411 pipe_reference_init(&tex->base.reference, 1);
412 tex->base.screen = screen;
414 SVGA_DBG(DEBUG_DMA, "wrap surface sid %p\n", srf);
416 tex->key.cachable = 0;
424 svga_screen_texture_get_handle(struct pipe_screen *screen,
425 struct pipe_texture *texture,
426 struct winsys_handle *whandle)
428 struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
431 assert(svga_texture(texture)->key.cachable == 0);
432 svga_texture(texture)->key.cachable = 0;
433 stride = util_format_get_nblocksx(texture->format, texture->width0) *
434 util_format_get_blocksize(texture->format);
435 return sws->surface_get_handle(sws, svga_texture(texture)->handle, stride, whandle);
440 svga_texture_destroy(struct pipe_texture *pt)
442 struct svga_screen *ss = svga_screen(pt->screen);
443 struct svga_texture *tex = (struct svga_texture *)pt;
445 ss->texture_timestamp++;
447 svga_sampler_view_reference(&tex->cached_view, NULL);
450 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
452 SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
453 svga_screen_surface_destroy(ss, &tex->key, &tex->handle);
460 svga_texture_copy_handle(struct svga_context *svga,
461 struct svga_screen *ss,
462 struct svga_winsys_surface *src_handle,
463 unsigned src_x, unsigned src_y, unsigned src_z,
464 unsigned src_level, unsigned src_face,
465 struct svga_winsys_surface *dst_handle,
466 unsigned dst_x, unsigned dst_y, unsigned dst_z,
467 unsigned dst_level, unsigned dst_face,
468 unsigned width, unsigned height, unsigned depth)
470 struct svga_surface dst, src;
472 SVGA3dCopyBox box, *boxes;
476 src.handle = src_handle;
477 src.real_level = src_level;
478 src.real_face = src_face;
481 dst.handle = dst_handle;
482 dst.real_level = dst_level;
483 dst.real_face = dst_face;
497 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
498 src_handle, src_level, src_x, src_y, src_z,
499 dst_handle, dst_level, dst_x, dst_y, dst_z);
503 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
508 svga_context_flush(svga, NULL);
509 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
513 assert(ret == PIPE_OK);
516 SVGA_FIFOCommitAll(svga->swc);
518 pipe_mutex_lock(ss->swc_mutex);
519 ret = SVGA3D_BeginSurfaceCopy(ss->swc,
524 ss->swc->flush(ss->swc, NULL);
525 ret = SVGA3D_BeginSurfaceCopy(ss->swc,
529 assert(ret == PIPE_OK);
532 SVGA_FIFOCommitAll(ss->swc);
533 pipe_mutex_unlock(ss->swc_mutex);
537 static struct svga_winsys_surface *
538 svga_texture_view_surface(struct pipe_context *pipe,
539 struct svga_texture *tex,
540 SVGA3dSurfaceFormat format,
545 struct svga_host_surface_cache_key *key) /* OUT */
547 struct svga_screen *ss = svga_screen(tex->base.screen);
548 struct svga_winsys_surface *handle;
550 unsigned z_offset = 0;
553 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
554 face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
557 key->format = format;
558 key->numMipLevels = num_mip;
559 key->size.width = u_minify(tex->base.width0, start_mip);
560 key->size.height = u_minify(tex->base.height0, start_mip);
561 key->size.depth = zslice_pick < 0 ? u_minify(tex->base.depth0, start_mip) : 1;
563 assert(key->size.depth == 1);
565 if(tex->base.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
566 key->flags |= SVGA3D_SURFACE_CUBEMAP;
572 if(key->format == SVGA3D_FORMAT_INVALID) {
577 SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
578 handle = svga_screen_surface_create(ss, key);
584 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
589 if (zslice_pick >= 0)
590 z_offset = zslice_pick;
592 for (i = 0; i < key->numMipLevels; i++) {
593 for (j = 0; j < key->numFaces; j++) {
594 if(tex->defined[j + face_pick][i + start_mip]) {
595 unsigned depth = (zslice_pick < 0 ?
596 u_minify(tex->base.depth0, i + start_mip) :
599 svga_texture_copy_handle(svga_context(pipe),
605 handle, 0, 0, 0, i, j,
606 u_minify(tex->base.width0, i + start_mip),
607 u_minify(tex->base.height0, i + start_mip),
617 static struct pipe_surface *
618 svga_get_tex_surface(struct pipe_screen *screen,
619 struct pipe_texture *pt,
620 unsigned face, unsigned level, unsigned zslice,
623 struct svga_texture *tex = svga_texture(pt);
624 struct svga_surface *s;
625 boolean render = flags & PIPE_BUFFER_USAGE_GPU_WRITE ? TRUE : FALSE;
626 boolean view = FALSE;
627 SVGA3dSurfaceFormat format;
629 s = CALLOC_STRUCT(svga_surface);
633 pipe_reference_init(&s->base.reference, 1);
634 pipe_texture_reference(&s->base.texture, pt);
635 s->base.format = pt->format;
636 s->base.width = u_minify(pt->width0, level);
637 s->base.height = u_minify(pt->height0, level);
638 s->base.usage = flags;
639 s->base.level = level;
641 s->base.zslice = zslice;
644 format = svga_translate_format(pt->format);
646 format = svga_translate_format_render(pt->format);
648 assert(format != SVGA3D_FORMAT_INVALID);
649 assert(!(flags & PIPE_BUFFER_USAGE_CPU_READ_WRITE));
652 if (svga_screen(screen)->debug.force_surface_view)
655 /* Currently only used for compressed textures */
657 format != svga_translate_format(pt->format)) {
662 svga_screen(screen)->debug.force_level_surface_view)
665 if (pt->target == PIPE_TEXTURE_3D)
668 if (svga_screen(screen)->debug.no_surface_view)
672 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
673 pt, level, face, zslice, s);
675 s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice,
681 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
682 pt, level, face, zslice, s);
684 memset(&s->key, 0, sizeof s->key);
685 s->handle = tex->handle;
687 s->real_level = level;
688 s->real_zslice = zslice;
696 svga_tex_surface_destroy(struct pipe_surface *surf)
698 struct svga_surface *s = svga_surface(surf);
699 struct svga_texture *t = svga_texture(surf->texture);
700 struct svga_screen *ss = svga_screen(surf->texture->screen);
702 if(s->handle != t->handle) {
703 SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
704 svga_screen_surface_destroy(ss, &s->key, &s->handle);
707 pipe_texture_reference(&surf->texture, NULL);
713 svga_mark_surface_dirty(struct pipe_surface *surf)
715 struct svga_surface *s = svga_surface(surf);
718 struct svga_texture *tex = svga_texture(surf->texture);
722 if (s->handle == tex->handle)
723 tex->defined[surf->face][surf->level] = TRUE;
725 /* this will happen later in svga_propagate_surface */
731 void svga_mark_surfaces_dirty(struct svga_context *svga)
735 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
736 if (svga->curr.framebuffer.cbufs[i])
737 svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
739 if (svga->curr.framebuffer.zsbuf)
740 svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
744 * Progagate any changes from surfaces to texture.
745 * pipe is optional context to inline the blit command in.
748 svga_propagate_surface(struct pipe_context *pipe, struct pipe_surface *surf)
750 struct svga_surface *s = svga_surface(surf);
751 struct svga_texture *tex = svga_texture(surf->texture);
752 struct svga_screen *ss = svga_screen(surf->texture->screen);
758 ss->texture_timestamp++;
759 tex->view_age[surf->level] = ++(tex->age);
761 if (s->handle != tex->handle) {
762 SVGA_DBG(DEBUG_VIEWS, "svga: Surface propagate: tex %p, level %u, from %p\n", tex, surf->level, surf);
763 svga_texture_copy_handle(svga_context(pipe), ss,
764 s->handle, 0, 0, 0, s->real_level, s->real_face,
765 tex->handle, 0, 0, surf->zslice, surf->level, surf->face,
766 u_minify(tex->base.width0, surf->level),
767 u_minify(tex->base.height0, surf->level), 1);
768 tex->defined[surf->face][surf->level] = TRUE;
773 * Check if we should call svga_propagate_surface on the surface.
776 svga_surface_needs_propagation(struct pipe_surface *surf)
778 struct svga_surface *s = svga_surface(surf);
779 struct svga_texture *tex = svga_texture(surf->texture);
781 return s->dirty && s->handle != tex->handle;
784 /* XXX: Still implementing this as if it was a screen function, but
785 * can now modify it to queue transfers on the context.
787 static struct pipe_transfer *
788 svga_get_tex_transfer(struct pipe_context *pipe,
789 struct pipe_texture *texture,
790 unsigned face, unsigned level, unsigned zslice,
791 enum pipe_transfer_usage usage, unsigned x, unsigned y,
792 unsigned w, unsigned h)
794 struct svga_screen *ss = svga_screen(pipe->screen);
795 struct svga_winsys_screen *sws = ss->sws;
796 struct svga_transfer *st;
797 unsigned nblocksx = util_format_get_nblocksx(texture->format, w);
798 unsigned nblocksy = util_format_get_nblocksy(texture->format, h);
800 /* We can't map texture storage directly */
801 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
804 st = CALLOC_STRUCT(svga_transfer);
812 st->base.stride = nblocksx*util_format_get_blocksize(texture->format);
813 st->base.usage = usage;
814 st->base.face = face;
815 st->base.level = level;
816 st->base.zslice = zslice;
818 st->hw_nblocksy = nblocksy;
820 st->hwbuf = svga_winsys_buffer_create(ss,
823 st->hw_nblocksy*st->base.stride);
824 while(!st->hwbuf && (st->hw_nblocksy /= 2)) {
825 st->hwbuf = svga_winsys_buffer_create(ss,
828 st->hw_nblocksy*st->base.stride);
834 if(st->hw_nblocksy < nblocksy) {
835 /* We couldn't allocate a hardware buffer big enough for the transfer,
836 * so allocate regular malloc memory instead */
837 debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
839 (nblocksy*st->base.stride + 1023)/1024,
840 (nblocksy + st->hw_nblocksy - 1)/st->hw_nblocksy,
841 (st->hw_nblocksy*st->base.stride + 1023)/1024);
842 st->swbuf = MALLOC(nblocksy*st->base.stride);
847 pipe_texture_reference(&st->base.texture, texture);
849 if (usage & PIPE_TRANSFER_READ)
850 svga_transfer_dma(st, SVGA3D_READ_HOST_VRAM);
855 sws->buffer_destroy(sws, st->hwbuf);
862 /* XXX: Still implementing this as if it was a screen function, but
863 * can now modify it to queue transfers on the context.
866 svga_transfer_map( struct pipe_context *pipe,
867 struct pipe_transfer *transfer )
869 struct svga_screen *ss = svga_screen(pipe->screen);
870 struct svga_winsys_screen *sws = ss->sws;
871 struct svga_transfer *st = svga_transfer(transfer);
876 /* The wait for read transfers already happened when svga_transfer_dma
878 return sws->buffer_map(sws, st->hwbuf,
879 pipe_transfer_buffer_flags(transfer));
883 /* XXX: Still implementing this as if it was a screen function, but
884 * can now modify it to queue transfers on the context.
887 svga_transfer_unmap(struct pipe_context *pipe,
888 struct pipe_transfer *transfer)
890 struct svga_screen *ss = svga_screen(pipe->screen);
891 struct svga_winsys_screen *sws = ss->sws;
892 struct svga_transfer *st = svga_transfer(transfer);
895 sws->buffer_unmap(sws, st->hwbuf);
900 svga_tex_transfer_destroy(struct pipe_context *pipe,
901 struct pipe_transfer *transfer)
903 struct svga_texture *tex = svga_texture(transfer->texture);
904 struct svga_screen *ss = svga_screen(pipe->screen);
905 struct svga_winsys_screen *sws = ss->sws;
906 struct svga_transfer *st = svga_transfer(transfer);
908 if (st->base.usage & PIPE_TRANSFER_WRITE) {
909 svga_transfer_dma(st, SVGA3D_WRITE_HOST_VRAM);
910 ss->texture_timestamp++;
911 tex->view_age[transfer->level] = ++(tex->age);
912 tex->defined[transfer->face][transfer->level] = TRUE;
915 pipe_texture_reference(&st->base.texture, NULL);
917 sws->buffer_destroy(sws, st->hwbuf);
923 svga_init_texture_functions(struct pipe_context *pipe)
925 pipe->get_tex_transfer = svga_get_tex_transfer;
926 pipe->transfer_map = svga_transfer_map;
927 pipe->transfer_unmap = svga_transfer_unmap;
928 pipe->tex_transfer_destroy = svga_tex_transfer_destroy;
933 svga_screen_init_texture_functions(struct pipe_screen *screen)
935 screen->texture_create = svga_texture_create;
936 screen->texture_from_handle = svga_screen_texture_from_handle;
937 screen->texture_get_handle = svga_screen_texture_get_handle;
938 screen->texture_destroy = svga_texture_destroy;
939 screen->get_tex_surface = svga_get_tex_surface;
940 screen->tex_surface_destroy = svga_tex_surface_destroy;
943 /***********************************************************************
946 struct svga_sampler_view *
947 svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
948 unsigned min_lod, unsigned max_lod)
950 struct svga_screen *ss = svga_screen(pt->screen);
951 struct svga_texture *tex = svga_texture(pt);
952 struct svga_sampler_view *sv = NULL;
953 SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
957 assert(min_lod >= 0);
958 assert(min_lod <= max_lod);
959 assert(max_lod <= pt->last_level);
962 /* Is a view needed */
965 * Can't control max lod. For first level views and when we only
966 * look at one level we disable mip filtering to achive the same
969 if (min_lod == 0 && max_lod >= pt->last_level)
972 if (util_format_is_s3tc(pt->format) && view) {
973 format = svga_translate_format_render(pt->format);
976 if (ss->debug.no_sampler_view)
979 if (ss->debug.force_sampler_view)
983 /* First try the cache */
985 pipe_mutex_lock(ss->tex_mutex);
986 if (tex->cached_view &&
987 tex->cached_view->min_lod == min_lod &&
988 tex->cached_view->max_lod == max_lod) {
989 svga_sampler_view_reference(&sv, tex->cached_view);
990 pipe_mutex_unlock(ss->tex_mutex);
991 SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
992 pt, min_lod, max_lod, pt->last_level);
993 svga_validate_sampler_view(svga_context(pipe), sv);
996 pipe_mutex_unlock(ss->tex_mutex);
999 sv = CALLOC_STRUCT(svga_sampler_view);
1000 pipe_reference_init(&sv->reference, 1);
1001 pipe_texture_reference(&sv->texture, pt);
1002 sv->min_lod = min_lod;
1003 sv->max_lod = max_lod;
1005 /* No view needed just use the whole texture */
1007 SVGA_DBG(DEBUG_VIEWS,
1008 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
1009 pt, min_lod, max_lod,
1010 max_lod - min_lod + 1,
1015 sv->key.cachable = 0;
1016 sv->handle = tex->handle;
1020 SVGA_DBG(DEBUG_VIEWS,
1021 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
1022 pt, min_lod, max_lod,
1023 max_lod - min_lod + 1,
1030 sv->handle = svga_texture_view_surface(pipe, tex, format,
1032 max_lod - min_lod + 1,
1038 sv->key.cachable = 0;
1039 sv->handle = tex->handle;
1043 pipe_mutex_lock(ss->tex_mutex);
1044 svga_sampler_view_reference(&tex->cached_view, sv);
1045 pipe_mutex_unlock(ss->tex_mutex);
1051 svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
1053 struct svga_texture *tex = svga_texture(v->texture);
1060 if (v->handle == tex->handle)
1065 if(tex->base.target == PIPE_TEXTURE_CUBE)
1070 for (i = v->min_lod; i <= v->max_lod; i++) {
1071 for (k = 0; k < numFaces; k++) {
1072 if (v->age < tex->view_age[i])
1073 svga_texture_copy_handle(svga, NULL,
1074 tex->handle, 0, 0, 0, i, k,
1075 v->handle, 0, 0, 0, i - v->min_lod, k,
1076 u_minify(tex->base.width0, i),
1077 u_minify(tex->base.height0, i),
1078 u_minify(tex->base.depth0, i));
1086 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
1088 struct svga_texture *tex = svga_texture(v->texture);
1090 if(v->handle != tex->handle) {
1091 struct svga_screen *ss = svga_screen(v->texture->screen);
1092 SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
1093 svga_screen_surface_destroy(ss, &v->key, &v->handle);
1095 pipe_texture_reference(&v->texture, NULL);