r300g: align the height of NPOT textures to POT
[mesa.git] / src / gallium / drivers / svga / svga_screen_texture.c
index 2224c2d3945d96d102dea8a032a9be3f311be2bb..811c7466956aa6ade26b3c4921034d2046e17100 100644 (file)
@@ -27,8 +27,8 @@
 
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
-#include "pipe/p_thread.h"
+#include "util/u_inlines.h"
+#include "os/os_thread.h"
 #include "util/u_format.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
@@ -41,8 +41,6 @@
 #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.
@@ -59,21 +57,21 @@ svga_translate_format(enum pipe_format format)
 {
    switch(format) {
    
-   case PIPE_FORMAT_A8R8G8B8_UNORM:
+   case PIPE_FORMAT_B8G8R8A8_UNORM:
       return SVGA3D_A8R8G8B8;
-   case PIPE_FORMAT_X8R8G8B8_UNORM:
+   case PIPE_FORMAT_B8G8R8X8_UNORM:
       return SVGA3D_X8R8G8B8;
 
       /* Required for GL2.1:
        */
-   case PIPE_FORMAT_A8R8G8B8_SRGB:
+   case PIPE_FORMAT_B8G8R8A8_SRGB:
       return SVGA3D_A8R8G8B8;
 
-   case PIPE_FORMAT_R5G6B5_UNORM:
+   case PIPE_FORMAT_B5G6R5_UNORM:
       return SVGA3D_R5G6B5;
-   case PIPE_FORMAT_A1R5G5B5_UNORM:
+   case PIPE_FORMAT_B5G5R5A1_UNORM:
       return SVGA3D_A1R5G5B5;
-   case PIPE_FORMAT_A4R4G4B4_UNORM:
+   case PIPE_FORMAT_B4G4R4A4_UNORM:
       return SVGA3D_A4R4G4B4;
 
       
@@ -83,9 +81,9 @@ svga_translate_format(enum pipe_format format)
     */
    case PIPE_FORMAT_Z16_UNORM:
       return SVGA3D_Z_D16;
-   case PIPE_FORMAT_Z24S8_UNORM:
+   case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
       return SVGA3D_Z_D24S8;
-   case PIPE_FORMAT_Z24X8_UNORM:
+   case PIPE_FORMAT_X8Z24_UNORM:
       return SVGA3D_Z_D24X8;
 
    case PIPE_FORMAT_A8_UNORM:
@@ -111,13 +109,13 @@ 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_B8G8R8A8_UNORM:
+   case PIPE_FORMAT_B8G8R8X8_UNORM:
+   case PIPE_FORMAT_B5G5R5A1_UNORM:
+   case PIPE_FORMAT_B4G4R4A4_UNORM:
+   case PIPE_FORMAT_B5G6R5_UNORM:
+   case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
+   case PIPE_FORMAT_X8Z24_UNORM:
    case PIPE_FORMAT_Z32_UNORM:
    case PIPE_FORMAT_Z16_UNORM:
    case PIPE_FORMAT_L8_UNORM:
@@ -205,7 +203,7 @@ svga_transfer_dma(struct svga_transfer *st,
       if(transfer == SVGA3D_READ_HOST_VRAM) {
          svga_screen_flush(screen, &fence);
          sws->fence_finish(sws, fence, 0);
-         //sws->fence_reference(sws, &fence, NULL);
+         sws->fence_reference(sws, &fence, NULL);
       }
    }
    else {
@@ -235,7 +233,7 @@ svga_transfer_dma(struct svga_transfer *st,
             if(y) {
                svga_screen_flush(screen, &fence);
                sws->fence_finish(sws, fence, 0);
-               //sws->fence_reference(sws, &fence, NULL);
+               sws->fence_reference(sws, &fence, NULL);
             }
 
             hw = sws->buffer_map(sws, st->hwbuf, PIPE_BUFFER_USAGE_CPU_WRITE);
@@ -306,11 +304,23 @@ svga_texture_create(struct pipe_screen *screen,
       tex->key.numFaces = 1;
    }
 
+   tex->key.cachable = 1;
+
    if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
       tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
 
-   if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
+   if(templat->tex_usage & PIPE_TEXTURE_USAGE_DISPLAY_TARGET) {
+      tex->key.cachable = 0;
+   }
+
+   if(templat->tex_usage & PIPE_TEXTURE_USAGE_SHARED) {
+      tex->key.cachable = 0;
+   }
+
+   if(templat->tex_usage & PIPE_TEXTURE_USAGE_SCANOUT) {
       tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
+      tex->key.cachable = 0;
+   }
    
    /* 
     * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
@@ -320,7 +330,7 @@ svga_texture_create(struct pipe_screen *screen,
     */
 #if 0
    if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
-      !util_format_is_compressed(templat->format))
+      !util_format_is_s3tc(templat->format))
       tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
 #endif
    
@@ -333,8 +343,6 @@ svga_texture_create(struct pipe_screen *screen,
    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)
@@ -349,15 +357,18 @@ error1:
 }
 
 
+
+
+
 static struct pipe_texture *
-svga_texture_blanket(struct pipe_screen * screen,
-                     const struct pipe_texture *base,
-                     const unsigned *stride,
-                     struct pipe_buffer *buffer)
+svga_screen_texture_from_handle(struct pipe_screen *screen,
+                                const struct pipe_texture *base,
+                                struct winsys_handle *whandle)
 {
-   struct svga_texture *tex;
-   struct svga_buffer *sbuf = svga_buffer(buffer);
    struct svga_winsys_screen *sws = svga_winsys_screen(screen);
+   struct svga_winsys_surface *srf;
+   struct svga_texture *tex;
+   enum SVGA3dSurfaceFormat format = 0;
    assert(screen);
 
    /* Only supports one type */
@@ -367,18 +378,14 @@ svga_texture_blanket(struct pipe_screen * screen,
       return NULL;
    }
 
-   /**
-    * We currently can't do texture blanket on
-    * SVGA3D_BUFFER. Need to blit to a temporary surface?
-    */
+   srf = sws->surface_from_handle(sws, whandle, &format);
 
-   assert(sbuf->handle);
-   if (!sbuf->handle)
+   if (!srf)
       return NULL;
 
-   if (svga_translate_format(base->format) != sbuf->key.format) {
+   if (svga_translate_format(base->format) != format) {
       unsigned f1 = svga_translate_format(base->format);
-      unsigned f2 = sbuf->key.format;
+      unsigned f2 = format;
 
       /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
       if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
@@ -396,26 +403,39 @@ svga_texture_blanket(struct pipe_screen * screen,
    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;
+   if (format == 1)
+      tex->base.format = PIPE_FORMAT_B8G8R8X8_UNORM;
+   else if (format == 2)
+      tex->base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
 
    pipe_reference_init(&tex->base.reference, 1);
    tex->base.screen = screen;
 
-   SVGA_DBG(DEBUG_DMA, "blanket sid %p\n", sbuf->handle);
+   SVGA_DBG(DEBUG_DMA, "wrap surface sid %p\n", srf);
 
-   /* 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);
+   tex->handle = srf;
 
    return &tex->base;
 }
 
 
+static boolean 
+svga_screen_texture_get_handle(struct pipe_screen *screen,
+                               struct pipe_texture *texture,
+                               struct winsys_handle *whandle)
+{
+   struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
+   unsigned stride;
+
+   assert(svga_texture(texture)->key.cachable == 0);
+   svga_texture(texture)->key.cachable = 0;
+   stride = util_format_get_nblocksx(texture->format, texture->width0) *
+            util_format_get_blocksize(texture->format);
+   return sws->surface_get_handle(sws, svga_texture(texture)->handle, stride, whandle);
+}
+
+
 static void
 svga_texture_destroy(struct pipe_texture *pt)
 {
@@ -761,15 +781,17 @@ svga_surface_needs_propagation(struct pipe_surface *surf)
    return s->dirty && s->handle != tex->handle;
 }
 
-
+/* XXX: Still implementing this as if it was a screen function, but
+ * can now modify it to queue transfers on the context.
+ */
 static struct pipe_transfer *
-svga_get_tex_transfer(struct pipe_screen *screen,
-                     struct pipe_texture *texture,
-                     unsigned face, unsigned level, unsigned zslice,
-                     enum pipe_transfer_usage usage, unsigned x, unsigned y,
-                     unsigned w, unsigned h)
+svga_get_tex_transfer(struct pipe_context *pipe,
+                     struct pipe_texture *texture,
+                     unsigned face, unsigned level, unsigned zslice,
+                     enum pipe_transfer_usage usage, unsigned x, unsigned y,
+                     unsigned w, unsigned h)
 {
-   struct svga_screen *ss = svga_screen(screen);
+   struct svga_screen *ss = svga_screen(pipe->screen);
    struct svga_winsys_screen *sws = ss->sws;
    struct svga_transfer *st;
    unsigned nblocksx = util_format_get_nblocksx(texture->format, w);
@@ -837,11 +859,14 @@ no_hwbuf:
 }
 
 
+/* XXX: Still implementing this as if it was a screen function, but
+ * can now modify it to queue transfers on the context.
+ */
 static void *
-svga_transfer_map( struct pipe_screen *screen,
+svga_transfer_map( struct pipe_context *pipe,
                    struct pipe_transfer *transfer )
 {
-   struct svga_screen *ss = svga_screen(screen);
+   struct svga_screen *ss = svga_screen(pipe->screen);
    struct svga_winsys_screen *sws = ss->sws;
    struct svga_transfer *st = svga_transfer(transfer);
 
@@ -855,11 +880,14 @@ svga_transfer_map( struct pipe_screen *screen,
 }
 
 
+/* XXX: Still implementing this as if it was a screen function, but
+ * can now modify it to queue transfers on the context.
+ */
 static void
-svga_transfer_unmap(struct pipe_screen *screen,
+svga_transfer_unmap(struct pipe_context *pipe,
                     struct pipe_transfer *transfer)
 {
-   struct svga_screen *ss = svga_screen(screen);
+   struct svga_screen *ss = svga_screen(pipe->screen);
    struct svga_winsys_screen *sws = ss->sws;
    struct svga_transfer *st = svga_transfer(transfer);
    
@@ -869,10 +897,11 @@ svga_transfer_unmap(struct pipe_screen *screen,
 
 
 static void
-svga_tex_transfer_destroy(struct pipe_transfer *transfer)
+svga_tex_transfer_destroy(struct pipe_context *pipe,
+                          struct pipe_transfer *transfer)
 {
    struct svga_texture *tex = svga_texture(transfer->texture);
-   struct svga_screen *ss = svga_screen(transfer->texture->screen);
+   struct svga_screen *ss = svga_screen(pipe->screen);
    struct svga_winsys_screen *sws = ss->sws;
    struct svga_transfer *st = svga_transfer(transfer);
 
@@ -889,18 +918,26 @@ svga_tex_transfer_destroy(struct pipe_transfer *transfer)
    FREE(st);
 }
 
+
+void
+svga_init_texture_functions(struct pipe_context *pipe)
+{
+   pipe->get_tex_transfer = svga_get_tex_transfer;
+   pipe->transfer_map = svga_transfer_map;
+   pipe->transfer_unmap = svga_transfer_unmap;
+   pipe->tex_transfer_destroy = svga_tex_transfer_destroy;
+}
+
+
 void
 svga_screen_init_texture_functions(struct pipe_screen *screen)
 {
    screen->texture_create = svga_texture_create;
+   screen->texture_from_handle = svga_screen_texture_from_handle;
+   screen->texture_get_handle = svga_screen_texture_get_handle;
    screen->texture_destroy = svga_texture_destroy;
    screen->get_tex_surface = svga_get_tex_surface;
    screen->tex_surface_destroy = svga_tex_surface_destroy;
-   screen->texture_blanket = svga_texture_blanket;
-   screen->get_tex_transfer = svga_get_tex_transfer;
-   screen->transfer_map = svga_transfer_map;
-   screen->transfer_unmap = svga_transfer_unmap;
-   screen->tex_transfer_destroy = svga_tex_transfer_destroy;
 }
 
 /*********************************************************************** 
@@ -932,7 +969,7 @@ svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
       if (min_lod == 0 && max_lod >= pt->last_level)
          view = FALSE;
 
-      if (util_format_is_compressed(pt->format) && view) {
+      if (util_format_is_s3tc(pt->format) && view) {
          format = svga_translate_format_render(pt->format);
       }
 
@@ -1058,33 +1095,3 @@ svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
    pipe_texture_reference(&v->texture, NULL);
    FREE(v);
 }
-
-boolean
-svga_screen_buffer_from_texture(struct pipe_texture *texture,
-                               struct pipe_buffer **buffer,
-                               unsigned *stride)
-{
-   struct svga_texture *stex = svga_texture(texture);
-
-   *buffer = svga_screen_buffer_wrap_surface
-      (texture->screen,
-       svga_translate_format(texture->format),
-       stex->handle);
-
-   *stride = util_format_get_stride(texture->format, texture->width0);
-
-   return *buffer != NULL;
-}
-
-
-struct svga_winsys_surface *
-svga_screen_texture_get_winsys_surface(struct pipe_texture *texture)
-{
-   struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
-   struct svga_winsys_surface *vsurf = NULL;
-
-   assert(svga_texture(texture)->key.cachable == 0);
-   svga_texture(texture)->key.cachable = 0;
-   sws->surface_reference(sws, &vsurf, svga_texture(texture)->handle);
-   return vsurf;
-}