<commit_before>/**********************************************************
 * Copyright 2008-2009 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/

#include "svga_cmd.h"

#include "pipe/p_state.h"
#include "pipe/p_defines.h"
#include "pipe/p_inlines.h"
#include "pipe/p_thread.h"
#include "util/u_math.h"
#include "util/u_memory.h"

#include "svga_screen.h"
#include "svga_context.h"
#include "svga_screen_texture.h"
#include "svga_screen_buffer.h"
#include "svga_winsys.h"
#include "svga_debug.h"
#include "svga_screen_buffer.h"

#include <util/u_string.h>


/* XXX: This isn't a real hardware flag, but just a hack for kernel to
 * know about primary surfaces. Find a better way to accomplish this.
 */
#define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)


/*
 * Helper function and arrays
 */

SVGA3dSurfaceFormat
svga_translate_format(enum pipe_format format)
{
   switch(format) {
   
   case PIPE_FORMAT_A8R8G8B8_UNORM:
      return SVGA3D_A8R8G8B8;
   case PIPE_FORMAT_X8R8G8B8_UNORM:
      return SVGA3D_X8R8G8B8;

      /* Required for GL2.1:
       */
   case PIPE_FORMAT_A8R8G8B8_SRGB:
      return SVGA3D_A8R8G8B8;

   case PIPE_FORMAT_R5G6B5_UNORM:
      return SVGA3D_R5G6B5;
   case PIPE_FORMAT_A1R5G5B5_UNORM:
      return SVGA3D_A1R5G5B5;
   case PIPE_FORMAT_A4R4G4B4_UNORM:
      return SVGA3D_A4R4G4B4;

      
   /* XXX: Doesn't seem to work properly.
   case PIPE_FORMAT_Z32_UNORM:
      return SVGA3D_Z_D32;
    */
   case PIPE_FORMAT_Z16_UNORM:
      return SVGA3D_Z_D16;
   case PIPE_FORMAT_Z24S8_UNORM:
      return SVGA3D_Z_D24S8;
   case PIPE_FORMAT_Z24X8_UNORM:
      return SVGA3D_Z_D24X8;

   case PIPE_FORMAT_A8_UNORM:
      return SVGA3D_ALPHA8;
   case PIPE_FORMAT_L8_UNORM:
      return SVGA3D_LUMINANCE8;

   case PIPE_FORMAT_DXT1_RGB:
   case PIPE_FORMAT_DXT1_RGBA:
      return SVGA3D_DXT1;
   case PIPE_FORMAT_DXT3_RGBA:
      return SVGA3D_DXT3;
   case PIPE_FORMAT_DXT5_RGBA:
      return SVGA3D_DXT5;

   default:
      return SVGA3D_FORMAT_INVALID;
   }
}


SVGA3dSurfaceFormat
svga_translate_format_render(enum pipe_format format)
{
   switch(format) { 
   case PIPE_FORMAT_A8R8G8B8_UNORM:
   case PIPE_FORMAT_X8R8G8B8_UNORM:
   case PIPE_FORMAT_A1R5G5B5_UNORM:
   case PIPE_FORMAT_A4R4G4B4_UNORM:
   case PIPE_FORMAT_R5G6B5_UNORM:
   case PIPE_FORMAT_Z24S8_UNORM:
   case PIPE_FORMAT_Z24X8_UNORM:
   case PIPE_FORMAT_Z32_UNORM:
   case PIPE_FORMAT_Z16_UNORM:
   case PIPE_FORMAT_L8_UNORM:
      return svga_translate_format(format);

#if 1
   /* For on host conversion */
   case PIPE_FORMAT_DXT1_RGB:
      return SVGA3D_X8R8G8B8;
   case PIPE_FORMAT_DXT1_RGBA:
   case PIPE_FORMAT_DXT3_RGBA:
   case PIPE_FORMAT_DXT5_RGBA:
      return SVGA3D_A8R8G8B8;
#endif

   default:
      return SVGA3D_FORMAT_INVALID;
   }
}


static INLINE void
svga_transfer_dma_band(struct svga_transfer *st,
                       SVGA3dTransferType transfer,
                       unsigned y, unsigned h, unsigned srcy)
{
   struct svga_texture *texture = svga_texture(st->base.texture); 
   struct svga_screen *screen = svga_screen(texture->base.screen);
   SVGA3dCopyBox box;
   enum pipe_error ret;
   
   SVGA_DBG(DEBUG_DMA, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
                transfer == SVGA3D_WRITE_HOST_VRAM ? "to" : "from", 
                texture->handle,
                st->base.face,
                st->base.x,
                y,
                st->base.zslice,
                st->base.x + st->base.width,
                y + h,
                st->base.zslice + 1,
                texture->base.block.size*8/(texture->base.block.width*texture->base.block.height));
   
   box.x = st->base.x;
   box.y = y;
   box.z = st->base.zslice;
   box.w = st->base.width;
   box.h = h;
   box.d = 1;
   box.srcx = 0;
   box.srcy = srcy;
   box.srcz = 0;

   pipe_mutex_lock(screen->swc_mutex);
   ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
   if(ret != PIPE_OK) {
      screen->swc->flush(screen->swc, NULL);
      ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
      assert(ret == PIPE_OK);
   }
   pipe_mutex_unlock(screen->swc_mutex);
}


static INLINE void
svga_transfer_dma(struct svga_transfer *st,
                 SVGA3dTransferType transfer)
{
   struct svga_texture *texture = svga_texture(st->base.texture); 
   struct svga_screen *screen = svga_screen(texture->base.screen);
   struct svga_winsys_screen *sws = screen->sws;
   struct pipe_fence_handle *fence = NULL;
   
   if (transfer == SVGA3D_READ_HOST_VRAM) {
      SVGA_DBG(DEBUG_PERF, "%s: readback transfer\n", __FUNCTION__);
   }


   if(!st->swbuf) {
      /* Do the DMA transfer in a single go */
      
      svga_transfer_dma_band(st, transfer, st->base.y, st->base.height, 0);

      if(transfer == SVGA3D_READ_HOST_VRAM) {
         svga_screen_flush(screen, &fence);
         sws->fence_finish(sws, fence, 0);
         //sws->fence_reference(sws, &fence, NULL);
      }
   }
   else {
      unsigned y, h, srcy;
      h = st->hw_nblocksy * st->base.block.height;
      srcy = 0;
      for(y = 0; y < st->base.height; y += h) {
         unsigned offset, length;
         void *hw, *sw;

         if (y + h > st->base.height)
            h = st->base.height - y;

         /* Transfer band must be aligned to pixel block boundaries */
         assert(y % st->base.block.height == 0);
         assert(h % st->base.block.height == 0);
         
         offset = y * st->base.stride / st->base.block.height;
         length = h * st->base.stride / st->base.block.height;

         sw = (uint8_t *)st->swbuf + offset;
         
         if(transfer == SVGA3D_WRITE_HOST_VRAM) {
            /* Wait for the previous DMAs to complete */
            /* TODO: keep one DMA (at half the size) in the background */
            if(y) {
               svga_screen_flush(screen, &fence);
               sws->fence_finish(sws, fence, 0);
               //sws->fence_reference(sws, &fence, NULL);
            }

            hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
            assert(hw);
            if(hw) {
               memcpy(hw, sw, length);
               sws->buffer_unmap(sws, st->hwbuf);
            }
         }
         
         svga_transfer_dma_band(st, transfer, y, h, srcy);
         
         if(transfer == SVGA3D_READ_HOST_VRAM) {
            svga_screen_flush(screen, &fence);
            sws->fence_finish(sws, fence, 0);

            hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_READ);
            assert(hw);
            if(hw) {
               memcpy(sw, hw, length);
               sws->buffer_unmap(sws, st->hwbuf);
            }
         }
      }
   }
}


static struct pipe_texture *
svga_texture_create(struct pipe_screen *screen,
                    const struct pipe_texture *templat)
{
   struct svga_screen *svgascreen = svga_screen(screen);
   struct svga_texture *tex = CALLOC_STRUCT(svga_texture);
   unsigned width, height, depth;
   unsigned level;
   
   if (!tex)
      goto error1;

   tex->base = *templat;
   pipe_reference_init(&tex->base.reference, 1);
   tex->base.screen = screen;

   assert(templat->last_level < SVGA_MAX_TEXTURE_LEVELS);
   if(templat->last_level >= SVGA_MAX_TEXTURE_LEVELS)
      goto error2;
   
   width = templat->width[0];
   height = templat->height[0];
   depth = templat->depth[0];
   for(level = 0; level <= templat->last_level; ++level) {
      tex->base.width[level] = width;
      tex->base.height[level] = height;
      tex->base.depth[level] = depth;
      tex->base.nblocksx[level] = pf_get_nblocksx(&tex->base.block, width);  
      tex->base.nblocksy[level] = pf_get_nblocksy(&tex->base.block, height);  
      width  = minify(width);
      height = minify(height);
      depth = minify(depth);
   }
   
   tex->key.flags = 0;
   tex->key.size.width = templat->width[0];
   tex->key.size.height = templat->height[0];
   tex->key.size.depth = templat->depth[0];
   
   if(templat->target == PIPE_TEXTURE_CUBE) {
      tex->key.flags |= SVGA3D_SURFACE_CUBEMAP;
      tex->key.numFaces = 6;
   }
   else {
      tex->key.numFaces = 1;
   }

   if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
      tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;

   if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
      tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
   
   /* 
    * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
    * know beforehand whether a texture will be used as a rendertarget or not
    * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
    * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
    */
#if 0
   if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
      !pf_is_compressed(templat->format))
      tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
#endif
   
   if(templat->tex_usage & PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
      tex->key.flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
   
   tex->key.numMipLevels = templat->last_level + 1;
   
   tex->key.format = svga_translate_format(templat->format);
   if(tex->key.format == SVGA3D_FORMAT_INVALID)
      goto error2;

   tex->key.cachable = 1;
   
   SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
   tex->handle = svga_screen_surface_create(svgascreen, &tex->key);
   if (tex->handle)
      SVGA_DBG(DEBUG_DMA, "  --> got sid %p (texture)\n", tex->handle);

   return &tex->base;

error2:
   FREE(tex);
error1:
   return NULL;
}


static struct pipe_texture *
svga_texture_blanket(struct pipe_screen * screen,
                     const struct pipe_texture *base,
                     const unsigned *stride,
                     struct pipe_buffer *buffer)
{
   struct svga_texture *tex;
   struct svga_buffer *sbuf = svga_buffer(buffer);
   struct svga_winsys_screen *sws = svga_winsys_screen(screen);
   assert(screen);

   /* Only supports one type */
   if (base->target != PIPE_TEXTURE_2D ||
       base->last_level != 0 ||
       base->depth[0] != 1) {
      return NULL;
   }

   /**
    * We currently can't do texture blanket on
    * SVGA3D_BUFFER. Need to blit to a temporary surface?
    */

   assert(sbuf->handle);
   if (!sbuf->handle)
      return NULL;

   if (svga_translate_format(base->format) != sbuf->key.format) {
      unsigned f1 = svga_translate_format(base->format);
      unsigned f2 = sbuf->key.format;

      /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
      if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
              (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
              (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ) ) {
         debug_printf("%s wrong format %u != %u\n", __FUNCTION__, f1, f2);
         return NULL;
      }
   }

   tex = CALLOC_STRUCT(svga_texture);
   if (!tex)
      return NULL;

   tex->base = *base;
   

   if (sbuf->key.format == 1)
      tex->base.format = PIPE_FORMAT_X8R8G8B8_UNORM;
   else if (sbuf->key.format == 2)
      tex->base.format = PIPE_FORMAT_A8R8G8B8_UNORM;

   pipe_reference_init(&tex->base.reference, 1);
   tex->base.screen = screen;

   SVGA_DBG(DEBUG_DMA, "blanket sid %p\n", sbuf->handle);

   /* We don't own this storage, so don't try to cache it.
    */
   assert(sbuf->key.cachable == 0);
   tex->key.cachable = 0;
   sws->surface_reference(sws, &tex->handle, sbuf->handle);

   return &tex->base;
}


static void
svga_texture_destroy(struct pipe_texture *pt)
{
   struct svga_screen *ss = svga_screen(pt->screen);
   struct svga_texture *tex = (struct svga_texture *)pt;

   ss->texture_timestamp++;

   svga_sampler_view_reference(&tex->cached_view, NULL);

   /*
     DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
   */
   SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
   svga_screen_surface_destroy(ss, &tex->key, &tex->handle);

   FREE(tex);
}


static void
svga_texture_copy_handle(struct svga_context *svga,
                         struct svga_screen *ss,
                         struct svga_winsys_surface *src_handle,
                         unsigned src_x, unsigned src_y, unsigned src_z,
                         unsigned src_level, unsigned src_face,
                         struct svga_winsys_surface *dst_handle,
                         unsigned dst_x, unsigned dst_y, unsigned dst_z,
                         unsigned dst_level, unsigned dst_face,
                         unsigned width, unsigned height, unsigned depth)
{
   struct svga_surface dst, src;
   enum pipe_error ret;
   SVGA3dCopyBox box, *boxes;

   assert(svga || ss);

   src.handle = src_handle;
   src.real_level = src_level;
   src.real_face = src_face;
   src.real_zslice = 0;

   dst.handle = dst_handle;
   dst.real_level = dst_level;
   dst.real_face = dst_face;
   dst.real_zslice = 0;

   box.x = dst_x;
   box.y = dst_y;
   box.z = dst_z;
   box.w = width;
   box.h = height;
   box.d = depth;
   box.srcx = src_x;
   box.srcy = src_y;
   box.srcz = src_z;

/*
   SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
            src_handle, src_level, src_x, src_y, src_z,
            dst_handle, dst_level, dst_x, dst_y, dst_z);
*/

   if (svga) {
      ret = SVGA3D_BeginSurfaceCopy(svga->swc,
                                    &src.base,
                                    &dst.base,
                                    &boxes, 1);
      if(ret != PIPE_OK) {
         svga_context_flush(svga, NULL);
         ret = SVGA3D_BeginSurfaceCopy(svga->swc,
                                       &src.base,
                                       &dst.base,
                                       &boxes, 1);
         assert(ret == PIPE_OK);
      }
      *boxes = box;
      SVGA_FIFOCommitAll(svga->swc);
   } else {
      pipe_mutex_lock(ss->swc_mutex);
      ret = SVGA3D_BeginSurfaceCopy(ss->swc,
                                    &src.base,
                                    &dst.base,
                                    &boxes, 1);
      if(ret != PIPE_OK) {
         ss->swc->flush(ss->swc, NULL);
         ret = SVGA3D_BeginSurfaceCopy(ss->swc,
                                       &src.base,
                                       &dst.base,
                                       &boxes, 1);
         assert(ret == PIPE_OK);
      }
      *boxes = box;
      SVGA_FIFOCommitAll(ss->swc);
      pipe_mutex_unlock(ss->swc_mutex);
   }
}

static struct svga_winsys_surface *
svga_texture_view_surface(struct pipe_context *pipe,
                          struct svga_texture *tex,
                          SVGA3dSurfaceFormat format,
                          unsigned start_mip,
                          unsigned num_mip,
                          int face_pick,
                          int zslice_pick,
                          struct svga_host_surface_cache_key *key) /* OUT */
{
   struct svga_screen *ss = svga_screen(tex->base.screen);
   struct svga_winsys_surface *handle;
   int i, j;
   unsigned z_offset = 0;

   SVGA_DBG(DEBUG_PERF, 
            "svga: Create surface view: face %d zslice %d mips %d..%d\n",
            face_pick, zslice_pick, start_mip, start_mip+num_mip-1);

   key->flags = 0;
   key->format = format;<commit_msg>svga: Fix mixed signed comparisons.<commit_after>/**********************************************************
 * Copyright 2008-2009 VMware, Inc.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 **********************************************************/

#include "svga_cmd.h"

#include "pipe/p_state.h"
#include "pipe/p_defines.h"
#include "pipe/p_inlines.h"
#include "pipe/p_thread.h"
#include "util/u_math.h"
#include "util/u_memory.h"

#include "svga_screen.h"
#include "svga_context.h"
#include "svga_screen_texture.h"
#include "svga_screen_buffer.h"
#include "svga_winsys.h"
#include "svga_debug.h"
#include "svga_screen_buffer.h"

#include <util/u_string.h>


/* XXX: This isn't a real hardware flag, but just a hack for kernel to
 * know about primary surfaces. Find a better way to accomplish this.
 */
#define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)


/*
 * Helper function and arrays
 */

SVGA3dSurfaceFormat
svga_translate_format(enum pipe_format format)
{
   switch(format) {
   
   case PIPE_FORMAT_A8R8G8B8_UNORM:
      return SVGA3D_A8R8G8B8;
   case PIPE_FORMAT_X8R8G8B8_UNORM:
      return SVGA3D_X8R8G8B8;

      /* Required for GL2.1:
       */
   case PIPE_FORMAT_A8R8G8B8_SRGB:
      return SVGA3D_A8R8G8B8;

   case PIPE_FORMAT_R5G6B5_UNORM:
      return SVGA3D_R5G6B5;
   case PIPE_FORMAT_A1R5G5B5_UNORM:
      return SVGA3D_A1R5G5B5;
   case PIPE_FORMAT_A4R4G4B4_UNORM:
      return SVGA3D_A4R4G4B4;

      
   /* XXX: Doesn't seem to work properly.
   case PIPE_FORMAT_Z32_UNORM:
      return SVGA3D_Z_D32;
    */
   case PIPE_FORMAT_Z16_UNORM:
      return SVGA3D_Z_D16;
   case PIPE_FORMAT_Z24S8_UNORM:
      return SVGA3D_Z_D24S8;
   case PIPE_FORMAT_Z24X8_UNORM:
      return SVGA3D_Z_D24X8;

   case PIPE_FORMAT_A8_UNORM:
      return SVGA3D_ALPHA8;
   case PIPE_FORMAT_L8_UNORM:
      return SVGA3D_LUMINANCE8;

   case PIPE_FORMAT_DXT1_RGB:
   case PIPE_FORMAT_DXT1_RGBA:
      return SVGA3D_DXT1;
   case PIPE_FORMAT_DXT3_RGBA:
      return SVGA3D_DXT3;
   case PIPE_FORMAT_DXT5_RGBA:
      return SVGA3D_DXT5;

   default:
      return SVGA3D_FORMAT_INVALID;
   }
}


SVGA3dSurfaceFormat
svga_translate_format_render(enum pipe_format format)
{
   switch(format) { 
   case PIPE_FORMAT_A8R8G8B8_UNORM:
   case PIPE_FORMAT_X8R8G8B8_UNORM:
   case PIPE_FORMAT_A1R5G5B5_UNORM:
   case PIPE_FORMAT_A4R4G4B4_UNORM:
   case PIPE_FORMAT_R5G6B5_UNORM:
   case PIPE_FORMAT_Z24S8_UNORM:
   case PIPE_FORMAT_Z24X8_UNORM:
   case PIPE_FORMAT_Z32_UNORM:
   case PIPE_FORMAT_Z16_UNORM:
   case PIPE_FORMAT_L8_UNORM:
      return svga_translate_format(format);

#if 1
   /* For on host conversion */
   case PIPE_FORMAT_DXT1_RGB:
      return SVGA3D_X8R8G8B8;
   case PIPE_FORMAT_DXT1_RGBA:
   case PIPE_FORMAT_DXT3_RGBA:
   case PIPE_FORMAT_DXT5_RGBA:
      return SVGA3D_A8R8G8B8;
#endif

   default:
      return SVGA3D_FORMAT_INVALID;
   }
}


static INLINE void
svga_transfer_dma_band(struct svga_transfer *st,
                       SVGA3dTransferType transfer,
                       unsigned y, unsigned h, unsigned srcy)
{
   struct svga_texture *texture = svga_texture(st->base.texture); 
   struct svga_screen *screen = svga_screen(texture->base.screen);
   SVGA3dCopyBox box;
   enum pipe_error ret;
   
   SVGA_DBG(DEBUG_DMA, "dma %s sid %p, face %u, (%u, %u, %u) - (%u, %u, %u), %ubpp\n",
                transfer == SVGA3D_WRITE_HOST_VRAM ? "to" : "from", 
                texture->handle,
                st->base.face,
                st->base.x,
                y,
                st->base.zslice,
                st->base.x + st->base.width,
                y + h,
                st->base.zslice + 1,
                texture->base.block.size*8/(texture->base.block.width*texture->base.block.height));
   
   box.x = st->base.x;
   box.y = y;
   box.z = st->base.zslice;
   box.w = st->base.width;
   box.h = h;
   box.d = 1;
   box.srcx = 0;
   box.srcy = srcy;
   box.srcz = 0;

   pipe_mutex_lock(screen->swc_mutex);
   ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
   if(ret != PIPE_OK) {
      screen->swc->flush(screen->swc, NULL);
      ret = SVGA3D_SurfaceDMA(screen->swc, st, transfer, &box, 1);
      assert(ret == PIPE_OK);
   }
   pipe_mutex_unlock(screen->swc_mutex);
}


static INLINE void
svga_transfer_dma(struct svga_transfer *st,
                 SVGA3dTransferType transfer)
{
   struct svga_texture *texture = svga_texture(st->base.texture); 
   struct svga_screen *screen = svga_screen(texture->base.screen);
   struct svga_winsys_screen *sws = screen->sws;
   struct pipe_fence_handle *fence = NULL;
   
   if (transfer == SVGA3D_READ_HOST_VRAM) {
      SVGA_DBG(DEBUG_PERF, "%s: readback transfer\n", __FUNCTION__);
   }


   if(!st->swbuf) {
      /* Do the DMA transfer in a single go */
      
      svga_transfer_dma_band(st, transfer, st->base.y, st->base.height, 0);

      if(transfer == SVGA3D_READ_HOST_VRAM) {
         svga_screen_flush(screen, &fence);
         sws->fence_finish(sws, fence, 0);
         //sws->fence_reference(sws, &fence, NULL);
      }
   }
   else {
      unsigned y, h, srcy;
      h = st->hw_nblocksy * st->base.block.height;
      srcy = 0;
      for(y = 0; y < st->base.height; y += h) {
         unsigned offset, length;
         void *hw, *sw;

         if (y + h > st->base.height)
            h = st->base.height - y;

         /* Transfer band must be aligned to pixel block boundaries */
         assert(y % st->base.block.height == 0);
         assert(h % st->base.block.height == 0);
         
         offset = y * st->base.stride / st->base.block.height;
         length = h * st->base.stride / st->base.block.height;

         sw = (uint8_t *)st->swbuf + offset;
         
         if(transfer == SVGA3D_WRITE_HOST_VRAM) {
            /* Wait for the previous DMAs to complete */
            /* TODO: keep one DMA (at half the size) in the background */
            if(y) {
               svga_screen_flush(screen, &fence);
               sws->fence_finish(sws, fence, 0);
               //sws->fence_reference(sws, &fence, NULL);
            }

            hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
            assert(hw);
            if(hw) {
               memcpy(hw, sw, length);
               sws->buffer_unmap(sws, st->hwbuf);
            }
         }
         
         svga_transfer_dma_band(st, transfer, y, h, srcy);
         
         if(transfer == SVGA3D_READ_HOST_VRAM) {
            svga_screen_flush(screen, &fence);
            sws->fence_finish(sws, fence, 0);

            hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_READ);
            assert(hw);
            if(hw) {
               memcpy(sw, hw, length);
               sws->buffer_unmap(sws, st->hwbuf);
            }
         }
      }
   }
}


static struct pipe_texture *
svga_texture_create(struct pipe_screen *screen,
                    const struct pipe_texture *templat)
{
   struct svga_screen *svgascreen = svga_screen(screen);
   struct svga_texture *tex = CALLOC_STRUCT(svga_texture);
   unsigned width, height, depth;
   unsigned level;
   
   if (!tex)
      goto error1;

   tex->base = *templat;
   pipe_reference_init(&tex->base.reference, 1);
   tex->base.screen = screen;

   assert(templat->last_level < SVGA_MAX_TEXTURE_LEVELS);
   if(templat->last_level >= SVGA_MAX_TEXTURE_LEVELS)
      goto error2;
   
   width = templat->width[0];
   height = templat->height[0];
   depth = templat->depth[0];
   for(level = 0; level <= templat->last_level; ++level) {
      tex->base.width[level] = width;
      tex->base.height[level] = height;
      tex->base.depth[level] = depth;
      tex->base.nblocksx[level] = pf_get_nblocksx(&tex->base.block, width);  
      tex->base.nblocksy[level] = pf_get_nblocksy(&tex->base.block, height);  
      width  = minify(width);
      height = minify(height);
      depth = minify(depth);
   }
   
   tex->key.flags = 0;
   tex->key.size.width = templat->width[0];
   tex->key.size.height = templat->height[0];
   tex->key.size.depth = templat->depth[0];
   
   if(templat->target == PIPE_TEXTURE_CUBE) {
      tex->key.flags |= SVGA3D_SURFACE_CUBEMAP;
      tex->key.numFaces = 6;
   }
   else {
      tex->key.numFaces = 1;
   }

   if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
      tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;

   if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
      tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
   
   /* 
    * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
    * know beforehand whether a texture will be used as a rendertarget or not
    * and it always requests PIPE_TEXTURE_USAGE_RENDER_TARGET, therefore
    * passing the SVGA3D_SURFACE_HINT_RENDERTARGET here defeats its purpose.
    */
#if 0
   if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
      !pf_is_compressed(templat->format))
      tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
#endif
   
   if(templat->tex_usage & PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
      tex->key.flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
   
   tex->key.numMipLevels = templat->last_level + 1;
   
   tex->key.format = svga_translate_format(templat->format);
   if(tex->key.format == SVGA3D_FORMAT_INVALID)
      goto error2;

   tex->key.cachable = 1;
   
   SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
   tex->handle = svga_screen_surface_create(svgascreen, &tex->key);
   if (tex->handle)
      SVGA_DBG(DEBUG_DMA, "  --> got sid %p (texture)\n", tex->handle);

   return &tex->base;

error2:
   FREE(tex);
error1:
   return NULL;
}


static struct pipe_texture *
svga_texture_blanket(struct pipe_screen * screen,
                     const struct pipe_texture *base,
                     const unsigned *stride,
                     struct pipe_buffer *buffer)
{
   struct svga_texture *tex;
   struct svga_buffer *sbuf = svga_buffer(buffer);
   struct svga_winsys_screen *sws = svga_winsys_screen(screen);
   assert(screen);

   /* Only supports one type */
   if (base->target != PIPE_TEXTURE_2D ||
       base->last_level != 0 ||
       base->depth[0] != 1) {
      return NULL;
   }

   /**
    * We currently can't do texture blanket on
    * SVGA3D_BUFFER. Need to blit to a temporary surface?
    */

   assert(sbuf->handle);
   if (!sbuf->handle)
      return NULL;

   if (svga_translate_format(base->format) != sbuf->key.format) {
      unsigned f1 = svga_translate_format(base->format);
      unsigned f2 = sbuf->key.format;

      /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
      if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
              (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
              (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ) ) {
         debug_printf("%s wrong format %u != %u\n", __FUNCTION__, f1, f2);
         return NULL;
      }
   }

   tex = CALLOC_STRUCT(svga_texture);
   if (!tex)
      return NULL;

   tex->base = *base;
   

   if (sbuf->key.format == 1)
      tex->base.format = PIPE_FORMAT_X8R8G8B8_UNORM;
   else if (sbuf->key.format == 2)
      tex->base.format = PIPE_FORMAT_A8R8G8B8_UNORM;

   pipe_reference_init(&tex->base.reference, 1);
   tex->base.screen = screen;

   SVGA_DBG(DEBUG_DMA, "blanket sid %p\n", sbuf->handle);

   /* We don't own this storage, so don't try to cache it.
    */
   assert(sbuf->key.cachable == 0);
   tex->key.cachable = 0;
   sws->surface_reference(sws, &tex->handle, sbuf->handle);

   return &tex->base;
}


static void
svga_texture_destroy(struct pipe_texture *pt)
{
   struct svga_screen *ss = svga_screen(pt->screen);
   struct svga_texture *tex = (struct svga_texture *)pt;

   ss->texture_timestamp++;

   svga_sampler_view_reference(&tex->cached_view, NULL);

   /*
     DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
   */
   SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
   svga_screen_surface_destroy(ss, &tex->key, &tex->handle);

   FREE(tex);
}


static void
svga_texture_copy_handle(struct svga_context *svga,
                         struct svga_screen *ss,
                         struct svga_winsys_surface *src_handle,
                         unsigned src_x, unsigned src_y, unsigned src_z,
                         unsigned src_level, unsigned src_face,
                         struct svga_winsys_surface *dst_handle,
                         unsigned dst_x, unsigned dst_y, unsigned dst_z,
                         unsigned dst_level, unsigned dst_face,
                         unsigned width, unsigned height, unsigned depth)
{
   struct svga_surface dst, src;
   enum pipe_error ret;
   SVGA3dCopyBox box, *boxes;

   assert(svga || ss);

   src.handle = src_handle;
   src.real_level = src_level;
   src.real_face = src_face;
   src.real_zslice = 0;

   dst.handle = dst_handle;
   dst.real_level = dst_level;
   dst.real_face = dst_face;
   dst.real_zslice = 0;

   box.x = dst_x;
   box.y = dst_y;
   box.z = dst_z;
   box.w = width;
   box.h = height;
   box.d = depth;
   box.srcx = src_x;
   box.srcy = src_y;
   box.srcz = src_z;

/*
   SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
            src_handle, src_level, src_x, src_y, src_z,
            dst_handle, dst_level, dst_x, dst_y, dst_z);
*/

   if (svga) {
      ret = SVGA3D_BeginSurfaceCopy(svga->swc,
                                    &src.base,
                                    &dst.base,
                                    &boxes, 1);
      if(ret != PIPE_OK) {
         svga_context_flush(svga, NULL);
         ret = SVGA3D_BeginSurfaceCopy(svga->swc,
                                       &src.base,
                                       &dst.base,
                                       &boxes, 1);
         assert(ret == PIPE_OK);
      }
      *boxes = box;
      SVGA_FIFOCommitAll(svga->swc);
   } else {
      pipe_mutex_lock(ss->swc_mutex);
      ret = SVGA3D_BeginSurfaceCopy(ss->swc,
                                    &src.base,
                                    &dst.base,
                                    &boxes, 1);
      if(ret != PIPE_OK) {
         ss->swc->flush(ss->swc, NULL);
         ret = SVGA3D_BeginSurfaceCopy(ss->swc,
                                       &src.base,
                                       &dst.base,
                                       &boxes, 1);
         assert(ret == PIPE_OK);
      }
      *boxes = box;
      SVGA_FIFOCommitAll(ss->swc);
      pipe_mutex_unlock(ss->swc_mutex);
   }
}

static struct svga_winsys_surface *
svga_texture_view_surface(struct pipe_context *pipe,
                          struct svga_texture *tex,
                          SVGA3dSurfaceFormat format,
                          unsigned start_mip,
                          unsigned num_mip,
                          int face_pick,
                          int zslice_pick,
                          struct svga_host_surface_cache_key *key) /* OUT */
{
   struct svga_screen *ss = svga_screen(tex->base.screen);
   struct svga_winsys_surface *handle;
   uint32_t i, j;
   unsigned z_offset = 0;

   SVGA_DBG(DEBUG_PERF, 
            "svga: Create surface view: face %d zslice %d mips %d..%d\n",
            face_pick, zslice_pick, start_mip, start_mip+num_mip-1);

   key->flags = 0;
   key->format = format;
{'fraction_non_alphanumeric': 0.1153222599492215, 'fraction_numerical': 0.017068918050839682, 'mean_word_length': 2.380066170388751, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 299, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2265738', 'n_tokens_mistral': 11888, 'n_tokens_neox': 11510, 'n_words': 2409}