nouveau: Map correct mip level when using the shadow (nv30, nv40).
[mesa.git] / src / gallium / drivers / nv40 / nv40_screen.c
index ada0238511d76b63c7c82868b1dac98c0ebd4c43..ab128fecda7bedda6ff919508b6c8055febf2a97 100644 (file)
@@ -59,6 +59,8 @@ nv40_screen_get_param(struct pipe_screen *pscreen, int param)
        case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
        case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
                return 1;
+       case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
+               return 0; /* We have 4 - but unsupported currently */
        case NOUVEAU_CAP_HW_VTXBUF:
                return 1;
        case NOUVEAU_CAP_HW_IDXBUF:
@@ -137,22 +139,75 @@ static void *
 nv40_surface_map(struct pipe_screen *screen, struct pipe_surface *surface,
                 unsigned flags )
 {
-       struct pipe_winsys *ws = screen->winsys;
-       void *map;
+       struct pipe_winsys      *ws = screen->winsys;
+       struct pipe_surface     *surface_to_map;
+       void                    *map;
 
-       map = ws->buffer_map(ws, surface->buffer, flags);
+       if (!(surface->texture->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR)) {
+               struct nv40_miptree *mt = (struct nv40_miptree *)surface->texture;
+
+               if (!mt->shadow_tex) {
+                       unsigned old_tex_usage = surface->texture->tex_usage;
+                       surface->texture->tex_usage = NOUVEAU_TEXTURE_USAGE_LINEAR |
+                                                     PIPE_TEXTURE_USAGE_DYNAMIC;
+                       mt->shadow_tex = screen->texture_create(screen, surface->texture);
+                       surface->texture->tex_usage = old_tex_usage;
+
+                       assert(mt->shadow_tex->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR);
+               }
+
+               mt->shadow_surface = screen->get_tex_surface
+               (
+                       screen, mt->shadow_tex,
+                       surface->face, surface->level, surface->zslice,
+                       surface->usage
+               );
+
+               surface_to_map = mt->shadow_surface;
+       }
+       else
+               surface_to_map = surface;
+
+       assert(surface_to_map);
+
+       map = ws->buffer_map(ws, surface_to_map->buffer, flags);
        if (!map)
                return NULL;
 
-       return map + surface->offset;
+       return map + surface_to_map->offset;
 }
 
 static void
 nv40_surface_unmap(struct pipe_screen *screen, struct pipe_surface *surface)
 {
-       struct pipe_winsys *ws = screen->winsys;
+       struct pipe_winsys      *ws = screen->winsys;
+       struct pipe_surface     *surface_to_unmap;
 
-       ws->buffer_unmap(ws, surface->buffer);
+       /* TODO: Copy from shadow just before push buffer is flushed instead.
+                There are probably some programs that map/unmap excessively
+                before rendering. */
+       if (!(surface->texture->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR)) {
+               struct nv40_miptree *mt = (struct nv40_miptree *)surface->texture;
+
+               assert(mt->shadow_tex);
+
+               surface_to_unmap = mt->shadow_surface;
+       }
+       else
+               surface_to_unmap = surface;
+
+       assert(surface_to_unmap);
+
+       ws->buffer_unmap(ws, surface_to_unmap->buffer);
+
+       if (surface_to_unmap != surface) {
+               struct nv40_screen *nvscreen = nv40_screen(screen);
+
+               nvscreen->nvws->surface_copy(nvscreen->nvws,
+                                            surface, 0, 0,
+                                            surface_to_unmap, 0, 0,
+                                            surface->width, surface->height);
+       }
 }
 
 static void