aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/gallium/auxiliary/util/u_gen_mipmap.c
blob: aa8eaebb6798a4cf6f67a50b9bada7b31a4fa233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**************************************************************************
 *
 * Copyright 2008 VMware, Inc.
 * All Rights Reserved.
 * Copyright 2008  VMware, Inc.  All rights reserved.
 * Copyright 2014 Advanced Micro Devices, Inc.
 *
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS 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.
 *
 **************************************************************************/

/**
 * @file
 * Mipmap generation utility
 *  
 * @author Brian Paul, Marek Olšák
 */


#include "util/u_gen_mipmap.h"
#include "util/u_format.h"
#include "util/u_inlines.h"


/**
 * Generate mipmap images.  It's assumed all needed texture memory is
 * already allocated.
 *
 * \param pt      the texture to generate mipmap levels for
 * \param format  format of texture
 * \param first_layer  the first layer to generate mipmap levels for
 *                     (ignored for 3D textures)
 * \param last_layer  the last layer to generate mipmap levels for
 *                    (ignored for 3D textures)
 * \param base_level  the first mipmap level to use as a src
 * \param last_level  the last mipmap level to generate
 * \param filter  the minification filter used to generate mipmap levels with
 *                one of PIPE_TEX_FILTER_LINEAR, PIPE_TEX_FILTER_NEAREST
 */
boolean
util_gen_mipmap(struct pipe_context *pipe, struct pipe_resource *pt,
                enum pipe_format format, uint base_level, uint last_level,
                uint first_layer, uint last_layer, uint filter)
{
   struct pipe_screen *screen = pipe->screen;
   struct pipe_blit_info blit;
   uint dstLevel;
   boolean is_zs = util_format_is_depth_or_stencil(format);
   boolean has_depth =
      util_format_has_depth(util_format_description(format));

   /* nothing to do for stencil-only formats */
   if (is_zs && !has_depth)
      return TRUE;

   /* nothing to do for integer formats */
   if (!is_zs && util_format_is_pure_integer(format))
      return TRUE;

   if (!screen->is_format_supported(screen, format, pt->target,
                                    pt->nr_samples,
                                    PIPE_BIND_SAMPLER_VIEW |
                                    (is_zs ? PIPE_BIND_DEPTH_STENCIL :
                                     PIPE_BIND_RENDER_TARGET))) {
      return FALSE;
   }

   /* The texture object should have room for the levels which we're
    * about to generate.
    */
   assert(last_level <= pt->last_level);

   /* If this fails, why are we here? */
   assert(last_level > base_level);
   assert(filter == PIPE_TEX_FILTER_LINEAR ||
          filter == PIPE_TEX_FILTER_NEAREST);

   memset(&blit, 0, sizeof(blit));
   blit.src.resource = blit.dst.resource = pt;
   blit.src.format = blit.dst.format = format;
   /* don't set the stencil mask, stencil shouldn't be changed */
   blit.mask = is_zs ? PIPE_MASK_Z : PIPE_MASK_RGBA;
   blit.filter = filter;

   for (dstLevel = base_level + 1; dstLevel <= last_level; dstLevel++) {
      blit.src.level = dstLevel - 1;
      blit.dst.level = dstLevel;

      blit.src.box.width = u_minify(pt->width0, blit.src.level);
      blit.src.box.height = u_minify(pt->height0, blit.src.level);

      blit.dst.box.width = u_minify(pt->width0, blit.dst.level);
      blit.dst.box.height = u_minify(pt->height0, blit.dst.level);

      if (pt->target == PIPE_TEXTURE_3D) {
         /* generate all layers/slices at once */
         blit.src.box.z = blit.dst.box.z = 0;
         blit.src.box.depth = util_max_layer(pt, blit.src.level)+1;
         blit.dst.box.depth = util_max_layer(pt, blit.dst.level)+1;
      }
      else {
         blit.src.box.z = blit.dst.box.z = first_layer;
         blit.src.box.depth = blit.dst.box.depth =
            (last_layer + 1 - first_layer);
      }

      pipe->blit(pipe, &blit);
   }
   return TRUE;
}