mesa: default DEPTH_TEXTURE_MODE should be RED in the core profile
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_texture.c
index 8dd2df47f7f98a3d0652513a3c4bc5d2d981cb0d..da74e8f4207a6463c72e3f6921b5721552cedd67 100644 (file)
@@ -46,7 +46,7 @@ nouveau_texture_new(struct gl_context *ctx, GLuint name, GLenum target)
 {
        struct nouveau_texture *nt = CALLOC_STRUCT(nouveau_texture);
 
-       _mesa_initialize_texture_object(&nt->base, name, target);
+       _mesa_initialize_texture_object(ctx, &nt->base, name, target);
 
        return &nt->base;
 }
@@ -79,72 +79,6 @@ nouveau_teximage_free(struct gl_context *ctx, struct gl_texture_image *ti)
        nouveau_surface_ref(NULL, &nti->surface);
 }
 
-static void
-nouveau_teximage_map(struct gl_context *ctx, struct gl_texture_image *ti,
-                    int access, int x, int y, int w, int h)
-{
-       struct nouveau_teximage *nti = to_nouveau_teximage(ti);
-       struct nouveau_surface *s = &nti->surface;
-       struct nouveau_surface *st = &nti->transfer.surface;
-
-       if (s->bo) {
-               if (!(access & GL_MAP_READ_BIT) &&
-                   nouveau_bo_pending(s->bo)) {
-                       /*
-                        * Heuristic: use a bounce buffer to pipeline
-                        * teximage transfers.
-                        */
-                       st->layout = LINEAR;
-                       st->format = s->format;
-                       st->cpp = s->cpp;
-                       st->width = w;
-                       st->height = h;
-                       st->pitch = s->pitch;
-                       nti->transfer.x = x;
-                       nti->transfer.y = y;
-
-                       nti->base.Data = nouveau_get_scratch(ctx, st->pitch * h,
-                                                      &st->bo, &st->offset);
-
-               } else {
-                       int ret, flags = 0;
-
-                       if (access & GL_MAP_READ_BIT)
-                               flags |= NOUVEAU_BO_RD;
-                       if (access & GL_MAP_WRITE_BIT)
-                               flags |= NOUVEAU_BO_WR;
-
-                       if (!s->bo->map) {
-                               ret = nouveau_bo_map(s->bo, flags);
-                               assert(!ret);
-                       }
-
-                       nti->base.Data = s->bo->map + y * s->pitch + x * s->cpp;
-               }
-       }
-}
-
-static void
-nouveau_teximage_unmap(struct gl_context *ctx, struct gl_texture_image *ti)
-{
-       struct nouveau_teximage *nti = to_nouveau_teximage(ti);
-       struct nouveau_surface *s = &nti->surface;
-       struct nouveau_surface *st = &nti->transfer.surface;
-
-       if (st->bo) {
-               context_drv(ctx)->surface_copy(ctx, s, st, nti->transfer.x,
-                                              nti->transfer.y, 0, 0,
-                                              st->width, st->height);
-               nouveau_surface_ref(NULL, st);
-
-       } else if (s->bo) {
-               nouveau_bo_unmap(s->bo);
-       }
-
-       nti->base.Data = NULL;
-}
-
-
 static void
 nouveau_map_texture_image(struct gl_context *ctx,
                          struct gl_texture_image *ti,
@@ -157,13 +91,15 @@ nouveau_map_texture_image(struct gl_context *ctx,
        struct nouveau_teximage *nti = to_nouveau_teximage(ti);
        struct nouveau_surface *s = &nti->surface;
        struct nouveau_surface *st = &nti->transfer.surface;
+       struct nouveau_client *client = context_client(ctx);
 
        /* Nouveau has no support for 3D or cubemap textures. */
        assert(slice == 0);
 
        if (s->bo) {
                if (!(mode & GL_MAP_READ_BIT) &&
-                   nouveau_bo_pending(s->bo)) {
+                   nouveau_pushbuf_refd(context_push(ctx), s->bo)) {
+                       unsigned size;
                        /*
                         * Heuristic: use a bounce buffer to pipeline
                         * teximage transfers.
@@ -177,8 +113,9 @@ nouveau_map_texture_image(struct gl_context *ctx,
                        nti->transfer.x = x;
                        nti->transfer.y = y;
 
-                       *map = nouveau_get_scratch(ctx, st->pitch * h,
-                                                  &st->bo, &st->offset);
+                       size = get_format_blocksy(st->format, h) * st->pitch;
+                       *map = nouveau_get_scratch(ctx, size,
+                                         &st->bo, &st->offset);
                        *stride = st->pitch;
                } else {
                        int ret, flags = 0;
@@ -189,15 +126,19 @@ nouveau_map_texture_image(struct gl_context *ctx,
                                flags |= NOUVEAU_BO_WR;
 
                        if (!s->bo->map) {
-                               ret = nouveau_bo_map(s->bo, flags);
+                               ret = nouveau_bo_map(s->bo, flags, client);
                                assert(!ret);
                        }
 
-                       *map = s->bo->map + y * s->pitch + x * s->cpp;
+                       *map = s->bo->map +
+                               get_format_blocksy(s->format, y) * s->pitch +
+                               get_format_blocksx(s->format, x) * s->cpp;
                        *stride = s->pitch;
                }
        } else {
-               *map = nti->base.Data + y * s->pitch + x * s->cpp;
+               *map = nti->base.Buffer +
+                       get_format_blocksy(s->format, y) * s->pitch +
+                       get_format_blocksx(s->format, x) * s->cpp;
                *stride = s->pitch;
        }
 }
@@ -216,15 +157,12 @@ nouveau_unmap_texture_image(struct gl_context *ctx, struct gl_texture_image *ti,
                                               st->width, st->height);
                nouveau_surface_ref(NULL, st);
 
-       } else if (s->bo) {
-               nouveau_bo_unmap(s->bo);
        }
-
-       nti->base.Data = NULL;
 }
 
 static gl_format
-nouveau_choose_tex_format(struct gl_context *ctx, GLint internalFormat,
+nouveau_choose_tex_format(struct gl_context *ctx, GLenum target,
+                          GLint internalFormat,
                          GLenum srcFormat, GLenum srcType)
 {
        switch (internalFormat) {
@@ -289,6 +227,22 @@ nouveau_choose_tex_format(struct gl_context *ctx, GLint internalFormat,
        case GL_INTENSITY8:
                return MESA_FORMAT_I8;
 
+       case GL_RGB_S3TC:
+       case GL_RGB4_S3TC:
+       case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
+               return MESA_FORMAT_RGB_DXT1;
+
+       case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
+               return MESA_FORMAT_RGBA_DXT1;
+
+       case GL_RGBA_S3TC:
+       case GL_RGBA4_S3TC:
+       case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
+               return MESA_FORMAT_RGBA_DXT3;
+
+       case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
+               return MESA_FORMAT_RGBA_DXT5;
+
        default:
                assert(0);
        }
@@ -344,7 +298,7 @@ get_last_level(struct gl_texture_object *t)
            t->Sampler.MinFilter == GL_LINEAR || !base)
                return t->BaseLevel;
        else
-               return MIN2(t->BaseLevel + base->MaxLog2, t->MaxLevel);
+               return MIN2(t->BaseLevel + base->MaxNumLevels - 1, t->MaxLevel);
 }
 
 static void
@@ -356,7 +310,9 @@ relayout_texture(struct gl_context *ctx, struct gl_texture_object *t)
                struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
                struct nouveau_surface *s = &to_nouveau_teximage(base)->surface;
                int i, ret, last = get_last_level(t);
-               unsigned size, offset = 0,
+               enum nouveau_surface_layout layout =
+                       (_mesa_is_format_compressed(s->format) ? LINEAR : SWIZZLED);
+               unsigned size, pitch, offset = 0,
                        width = s->width,
                        height = s->height;
 
@@ -366,7 +322,8 @@ relayout_texture(struct gl_context *ctx, struct gl_texture_object *t)
 
                /* Relayout the mipmap tree. */
                for (i = t->BaseLevel; i <= last; i++) {
-                       size = width * height * s->cpp;
+                       pitch = _mesa_format_row_stride(s->format, width);
+                       size = get_format_blocksy(s->format, height) * pitch;
 
                        /* Images larger than 16B have to be aligned. */
                        if (size > 16)
@@ -374,17 +331,17 @@ relayout_texture(struct gl_context *ctx, struct gl_texture_object *t)
 
                        ss[i] = (struct nouveau_surface) {
                                .offset = offset,
-                               .layout = SWIZZLED,
+                               .layout = layout,
                                .format = s->format,
                                .width = width,
                                .height = height,
                                .cpp = s->cpp,
-                               .pitch = width * s->cpp,
+                               .pitch = pitch,
                        };
 
                        offset += size;
-                       width = MAX2(1, width / 2);
-                       height = MAX2(1, height / 2);
+                       width = minify(width, 1);
+                       height = minify(height, 1);
                }
 
                /* Get new storage. */
@@ -392,7 +349,7 @@ relayout_texture(struct gl_context *ctx, struct gl_texture_object *t)
 
                ret = nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_MAP |
                                     NOUVEAU_BO_GART | NOUVEAU_BO_VRAM,
-                                    0, size, &ss[last].bo);
+                                    0, size, NULL, &ss[last].bo);
                assert(!ret);
 
                for (i = t->BaseLevel; i < last; i++)
@@ -421,7 +378,7 @@ nouveau_texture_validate(struct gl_context *ctx, struct gl_texture_object *t)
                                          s->width, s->height, 1);
                }
 
-               FIRE_RING(context_chan(ctx));
+               PUSH_KICK(context_push(ctx));
        }
 
        return GL_TRUE;
@@ -452,44 +409,57 @@ get_teximage_placement(struct gl_texture_image *ti)
 }
 
 static void
-nouveau_teximage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
-                GLint internalFormat,
-                GLint width, GLint height, GLint depth, GLint border,
+nouveau_teximage(struct gl_context *ctx, GLint dims,
+                struct gl_texture_image *ti,
+                GLsizei imageSize,
                 GLenum format, GLenum type, const GLvoid *pixels,
                 const struct gl_pixelstore_attrib *packing,
-                struct gl_texture_object *t,
-                struct gl_texture_image *ti)
+                GLboolean compressed)
 {
+       struct gl_texture_object *t = ti->TexObject;
+       const GLuint level = ti->Level;
        struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
        struct nouveau_teximage *nti = to_nouveau_teximage(ti);
        int ret;
+       GLuint depth = compressed ? 1 : ti->Depth;
 
        /* Allocate a new bo for the image. */
        nouveau_surface_alloc(ctx, s, LINEAR, get_teximage_placement(ti),
-                             ti->TexFormat, width, height);
+                             ti->TexFormat, ti->Width, ti->Height);
        nti->base.RowStride = s->pitch / s->cpp;
 
-       pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
-                                            format, type, pixels, packing,
-                                            "glTexImage");
+       if (compressed)
+               pixels = _mesa_validate_pbo_compressed_teximage(ctx,
+                       dims, imageSize,
+                       pixels, packing, "glCompressedTexImage");
+       else
+               pixels = _mesa_validate_pbo_teximage(ctx,
+                       dims, ti->Width, ti->Height, depth, format, type,
+                       pixels, packing, "glTexImage");
+
        if (pixels) {
+               GLubyte *map;
+               int row_stride;
+
                /* Store the pixel data. */
-               nouveau_teximage_map(ctx, ti, GL_MAP_WRITE_BIT,
-                                    0, 0, width, height);
+               nouveau_map_texture_image(ctx, ti, 0,
+                                         0, 0, ti->Width, ti->Height,
+                                         GL_MAP_WRITE_BIT,
+                                         &map, &row_stride);
 
                ret = _mesa_texstore(ctx, dims, ti->_BaseFormat,
                                     ti->TexFormat,
-                                    0, 0, 0, s->pitch,
-                                     &nti->base.Data,
-                                    width, height, depth,
+                                    row_stride,
+                                    &map,
+                                    ti->Width, ti->Height, depth,
                                     format, type, pixels, packing);
                assert(ret);
 
-               nouveau_teximage_unmap(ctx, ti);
+               nouveau_unmap_texture_image(ctx, ti, 0);
                _mesa_unmap_teximage_pbo(ctx, packing);
 
                if (!validate_teximage(ctx, t, level, 0, 0, 0,
-                                      width, height, depth))
+                                      ti->Width, ti->Height, depth))
                        /* It doesn't fit, mark it as dirty. */
                        texture_dirty(t);
        }
@@ -504,123 +474,95 @@ nouveau_teximage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
        context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
 }
 
-static void
-nouveau_teximage_1d(struct gl_context *ctx, GLenum target, GLint level,
-                   GLint internalFormat,
-                   GLint width, GLint border,
-                   GLenum format, GLenum type, const GLvoid *pixels,
-                   const struct gl_pixelstore_attrib *packing,
-                   struct gl_texture_object *t,
-                   struct gl_texture_image *ti)
-{
-       nouveau_teximage(ctx, 1, target, level, internalFormat,
-                        width, 1, 1, border, format, type, pixels,
-                        packing, t, ti);
-}
 
 static void
-nouveau_teximage_2d(struct gl_context *ctx, GLenum target, GLint level,
-                   GLint internalFormat,
-                   GLint width, GLint height, GLint border,
-                   GLenum format, GLenum type, const GLvoid *pixels,
-                   const struct gl_pixelstore_attrib *packing,
-                   struct gl_texture_object *t,
-                   struct gl_texture_image *ti)
+nouveau_teximage_123d(struct gl_context *ctx, GLuint dims,
+                      struct gl_texture_image *ti,
+                      GLenum format, GLenum type, const GLvoid *pixels,
+                      const struct gl_pixelstore_attrib *packing)
 {
-       nouveau_teximage(ctx, 2, target, level, internalFormat,
-                        width, height, 1, border, format, type, pixels,
-                        packing, t, ti);
+       nouveau_teximage(ctx, dims, ti, 0, format, type, pixels,
+                        packing, GL_FALSE);
 }
 
 static void
-nouveau_teximage_3d(struct gl_context *ctx, GLenum target, GLint level,
-                   GLint internalFormat,
-                   GLint width, GLint height, GLint depth, GLint border,
-                   GLenum format, GLenum type, const GLvoid *pixels,
-                   const struct gl_pixelstore_attrib *packing,
-                   struct gl_texture_object *t,
-                   struct gl_texture_image *ti)
+nouveau_compressed_teximage(struct gl_context *ctx, GLuint dims,
+                   struct gl_texture_image *ti,
+                   GLsizei imageSize, const GLvoid *data)
 {
-       nouveau_teximage(ctx, 3, target, level, internalFormat,
-                        width, height, depth, border, format, type, pixels,
-                        packing, t, ti);
+       nouveau_teximage(ctx, 2, ti, imageSize, 0, 0, data,
+                        &ctx->Unpack, GL_TRUE);
 }
 
 static void
-nouveau_texsubimage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
+nouveau_texsubimage(struct gl_context *ctx, GLint dims,
+                   struct gl_texture_image *ti,
                    GLint xoffset, GLint yoffset, GLint zoffset,
                    GLint width, GLint height, GLint depth,
+                   GLsizei imageSize,
                    GLenum format, GLenum type, const void *pixels,
                    const struct gl_pixelstore_attrib *packing,
-                   struct gl_texture_object *t,
-                   struct gl_texture_image *ti)
+                   GLboolean compressed)
 {
-       struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
-       struct nouveau_teximage *nti = to_nouveau_teximage(ti);
        int ret;
 
-       pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
-                                            format, type, pixels, packing,
-                                            "glTexSubImage");
+       if (compressed)
+               pixels = _mesa_validate_pbo_compressed_teximage(ctx,
+                               dims, imageSize,
+                               pixels, packing, "glCompressedTexSubImage");
+       else
+               pixels = _mesa_validate_pbo_teximage(ctx,
+                               dims, width, height, depth, format, type,
+                               pixels, packing, "glTexSubImage");
+
        if (pixels) {
-               nouveau_teximage_map(ctx, ti, GL_MAP_WRITE_BIT,
-                                    xoffset, yoffset, width, height);
+               GLubyte *map;
+               int row_stride;
+
+               nouveau_map_texture_image(ctx, ti, 0,
+                                         xoffset, yoffset, width, height,
+                                         GL_MAP_WRITE_BIT, &map, &row_stride);
 
-               ret = _mesa_texstore(ctx, 3, ti->_BaseFormat, ti->TexFormat,
-                                     0, 0, 0, s->pitch,
-                                    &nti->base.Data,
+               ret = _mesa_texstore(ctx, dims, ti->_BaseFormat, ti->TexFormat,
+                                    row_stride, &map,
                                      width, height, depth,
                                     format, type, pixels, packing);
                assert(ret);
 
-               nouveau_teximage_unmap(ctx, ti);
+               nouveau_unmap_texture_image(ctx, ti, 0);
                _mesa_unmap_teximage_pbo(ctx, packing);
        }
 
-       if (!to_nouveau_texture(t)->dirty)
-               validate_teximage(ctx, t, level, xoffset, yoffset, zoffset,
+       if (!to_nouveau_texture(ti->TexObject)->dirty)
+               validate_teximage(ctx, ti->TexObject, ti->Level,
+                                 xoffset, yoffset, zoffset,
                                  width, height, depth);
 }
 
 static void
-nouveau_texsubimage_3d(struct gl_context *ctx, GLenum target, GLint level,
-                      GLint xoffset, GLint yoffset, GLint zoffset,
-                      GLint width, GLint height, GLint depth,
-                      GLenum format, GLenum type, const void *pixels,
-                      const struct gl_pixelstore_attrib *packing,
-                      struct gl_texture_object *t,
-                      struct gl_texture_image *ti)
-{
-       nouveau_texsubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
-                           width, height, depth, format, type, pixels,
-                           packing, t, ti);
-}
-
-static void
-nouveau_texsubimage_2d(struct gl_context *ctx, GLenum target, GLint level,
-                      GLint xoffset, GLint yoffset,
-                      GLint width, GLint height,
-                      GLenum format, GLenum type, const void *pixels,
-                      const struct gl_pixelstore_attrib *packing,
-                      struct gl_texture_object *t,
-                      struct gl_texture_image *ti)
+nouveau_texsubimage_123d(struct gl_context *ctx, GLuint dims,
+                         struct gl_texture_image *ti,
+                         GLint xoffset, GLint yoffset, GLint zoffset,
+                         GLint width, GLint height, GLint depth,
+                         GLenum format, GLenum type, const void *pixels,
+                         const struct gl_pixelstore_attrib *packing)
 {
-       nouveau_texsubimage(ctx, 2, target, level, xoffset, yoffset, 0,
-                           width, height, 1, format, type, pixels,
-                           packing, t, ti);
+       nouveau_texsubimage(ctx, dims, ti, xoffset, yoffset, zoffset,
+                           width, height, depth, 0, format, type, pixels,
+                           packing, GL_FALSE);
 }
 
 static void
-nouveau_texsubimage_1d(struct gl_context *ctx, GLenum target, GLint level,
-                      GLint xoffset, GLint width,
-                      GLenum format, GLenum type, const void *pixels,
-                      const struct gl_pixelstore_attrib *packing,
-                      struct gl_texture_object *t,
-                      struct gl_texture_image *ti)
+nouveau_compressed_texsubimage(struct gl_context *ctx, GLuint dims,
+                      struct gl_texture_image *ti,
+                      GLint xoffset, GLint yoffset, GLint zoffset,
+                      GLsizei width, GLint height, GLint depth,
+                      GLenum format,
+                      GLint imageSize, const void *data)
 {
-       nouveau_texsubimage(ctx, 1, target, level, xoffset, 0, 0,
-                           width, 1, 1, format, type, pixels,
-                           packing, t, ti);
+       nouveau_texsubimage(ctx, dims, ti, xoffset, yoffset, zoffset,
+                         width, height, depth, imageSize, format, 0, data,
+                         &ctx->Unpack, GL_TRUE);
 }
 
 static void
@@ -671,7 +613,7 @@ nouveau_set_texbuffer(__DRIcontext *dri_ctx,
         s->format = get_texbuffer_format(rb, format);
 
        /* Update the image fields. */
-       _mesa_init_teximage_fields(ctx, target, ti, s->width, s->height,
+       _mesa_init_teximage_fields(ctx, ti, s->width, s->height,
                                   1, 0, s->cpp, s->format);
        nti->base.RowStride = s->pitch / s->cpp;
 
@@ -685,31 +627,6 @@ nouveau_set_texbuffer(__DRIcontext *dri_ctx,
        _mesa_unlock_texture(ctx, t);
 }
 
-static void
-nouveau_texture_map(struct gl_context *ctx, struct gl_texture_object *t)
-{
-       int i;
-
-       for (i = t->BaseLevel; i < t->_MaxLevel; i++) {
-               struct gl_texture_image *ti = t->Image[0][i];
-
-               if (ti)
-                       nouveau_teximage_map(ctx, ti, GL_MAP_READ_BIT,
-                                            0, 0, ti->Width, ti->Height);
-       }
-}
-
-static void
-nouveau_texture_unmap(struct gl_context *ctx, struct gl_texture_object *t)
-{
-       int i;
-
-       for (i = t->BaseLevel; i < t->_MaxLevel; i++) {
-               if (t->Image[0][i])
-                       nouveau_teximage_unmap(ctx, t->Image[0][i]);
-       }
-}
-
 void
 nouveau_texture_functions_init(struct dd_function_table *functions)
 {
@@ -718,15 +635,11 @@ nouveau_texture_functions_init(struct dd_function_table *functions)
        functions->NewTextureImage = nouveau_teximage_new;
        functions->FreeTextureImageBuffer = nouveau_teximage_free;
        functions->ChooseTextureFormat = nouveau_choose_tex_format;
-       functions->TexImage1D = nouveau_teximage_1d;
-       functions->TexImage2D = nouveau_teximage_2d;
-       functions->TexImage3D = nouveau_teximage_3d;
-       functions->TexSubImage1D = nouveau_texsubimage_1d;
-       functions->TexSubImage2D = nouveau_texsubimage_2d;
-       functions->TexSubImage3D = nouveau_texsubimage_3d;
+       functions->TexImage = nouveau_teximage_123d;
+       functions->TexSubImage = nouveau_texsubimage_123d;
+       functions->CompressedTexImage = nouveau_compressed_teximage;
+       functions->CompressedTexSubImage = nouveau_compressed_texsubimage;
        functions->BindTexture = nouveau_bind_texture;
-       functions->MapTexture = nouveau_texture_map;
-       functions->UnmapTexture = nouveau_texture_unmap;
        functions->MapTextureImage = nouveau_map_texture_image;
        functions->UnmapTextureImage = nouveau_unmap_texture_image;
 }