gl: updated glxext.h to version 27
[mesa.git] / src / gallium / drivers / nv40 / nv40_miptree.c
index 89ddf373e9e20be69652f5f1738e6e015713d8ee..85d7e1f1972dc323205dabba8434b0fdd79e294a 100644 (file)
@@ -1,9 +1,11 @@
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_inlines.h"
+#include "util/u_inlines.h"
+#include "util/u_format.h"
 #include "util/u_math.h"
 
 #include "nv40_context.h"
+#include "../nouveau/nv04_surface_2d.h"
 
 
 
@@ -31,9 +33,9 @@ nv40_miptree_layout(struct nv40_miptree *mt)
 
        for (l = 0; l <= pt->last_level; l++) {
                if (wide_pitch && (pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR))
-                       mt->level[l].pitch = align(pf_get_stride(pt->format, pt->width0), 64);
+                       mt->level[l].pitch = align(util_format_get_stride(pt->format, pt->width0), 64);
                else
-                       mt->level[l].pitch = pf_get_stride(pt->format, width);
+                       mt->level[l].pitch = util_format_get_stride(pt->format, width);
 
                mt->level[l].image_offset =
                        CALLOC(nr_faces, sizeof(unsigned));
@@ -88,8 +90,8 @@ nv40_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
        else {
                switch (pt->format) {
                /* TODO: Figure out which formats can be swizzled */
-               case PIPE_FORMAT_A8R8G8B8_UNORM:
-               case PIPE_FORMAT_X8R8G8B8_UNORM:
+               case PIPE_FORMAT_B8G8R8A8_UNORM:
+               case PIPE_FORMAT_B8G8R8X8_UNORM:
                case PIPE_FORMAT_R16_SNORM:
                {
                        if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE))
@@ -104,6 +106,12 @@ nv40_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
        if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
                buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
 
+       /* apparently we can't render to swizzled surfaces smaller than 64 bytes, so make them linear.
+        * If the user did not ask for a render target, they can still render to it, but it will cost them an extra copy.
+        * This also happens for small mipmaps of large textures. */
+       if (pt->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET && util_format_get_stride(pt->format, pt->width0) < 64)
+               mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
+
        nv40_miptree_layout(mt);
 
        mt->buffer = pscreen->buffer_create(pscreen, 256, buf_usage, mt->total_size);
@@ -190,12 +198,27 @@ nv40_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
                ns->base.offset = mt->level[level].image_offset[0];
        }
 
+       /* create a linear temporary that we can render into if necessary.
+        * Note that ns->pitch is always a multiple of 64 for linear surfaces and swizzled surfaces are POT, so
+        * ns->pitch & 63 is equivalent to (ns->pitch < 64 && swizzled)*/
+       if((ns->pitch & 63) && (ns->base.usage & (PIPE_BUFFER_USAGE_GPU_WRITE | NOUVEAU_BUFFER_USAGE_NO_RENDER)) == PIPE_BUFFER_USAGE_GPU_WRITE)
+               return &nv04_surface_wrap_for_render(pscreen, ((struct nv40_screen*)pscreen)->eng2d, ns)->base;
+
        return &ns->base;
 }
 
 static void
 nv40_miptree_surface_del(struct pipe_surface *ps)
 {
+       struct nv04_surface* ns = (struct nv04_surface*)ps;
+       if(ns->backing)
+       {
+               struct nv40_screen* screen = (struct nv40_screen*)ps->texture->screen;
+               if(ns->backing->base.usage & PIPE_BUFFER_USAGE_GPU_WRITE)
+                       screen->eng2d->copy(screen->eng2d, &ns->backing->base, 0, 0, ps, 0, 0, ns->base.width, ns->base.height);
+               nv40_miptree_surface_del(&ns->backing->base);
+       }
+
        pipe_texture_reference(&ps->texture, NULL);
        FREE(ps);
 }