1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci * Copyright 2008  VMware, Inc.  All rights reserved.
6bf215546Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
9bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
10bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
11bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
12bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
13bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
14bf215546Sopenharmony_ci * the following conditions:
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
17bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
18bf215546Sopenharmony_ci * of the Software.
19bf215546Sopenharmony_ci *
20bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci **************************************************************************/
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/**
31bf215546Sopenharmony_ci * @file
32bf215546Sopenharmony_ci * Mipmap generation utility
33bf215546Sopenharmony_ci *
34bf215546Sopenharmony_ci * @author Brian Paul, Marek Olšák
35bf215546Sopenharmony_ci */
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "util/u_gen_mipmap.h"
39bf215546Sopenharmony_ci#include "util/format/u_format.h"
40bf215546Sopenharmony_ci#include "util/u_inlines.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci/**
44bf215546Sopenharmony_ci * Generate mipmap images.  It's assumed all needed texture memory is
45bf215546Sopenharmony_ci * already allocated.
46bf215546Sopenharmony_ci *
47bf215546Sopenharmony_ci * \param pt      the texture to generate mipmap levels for
48bf215546Sopenharmony_ci * \param format  format of texture
49bf215546Sopenharmony_ci * \param first_layer  the first layer to generate mipmap levels for
50bf215546Sopenharmony_ci *                     (ignored for 3D textures)
51bf215546Sopenharmony_ci * \param last_layer  the last layer to generate mipmap levels for
52bf215546Sopenharmony_ci *                    (ignored for 3D textures)
53bf215546Sopenharmony_ci * \param base_level  the first mipmap level to use as a src
54bf215546Sopenharmony_ci * \param last_level  the last mipmap level to generate
55bf215546Sopenharmony_ci * \param filter  the minification filter used to generate mipmap levels with
56bf215546Sopenharmony_ci *                one of PIPE_TEX_FILTER_LINEAR, PIPE_TEX_FILTER_NEAREST
57bf215546Sopenharmony_ci */
58bf215546Sopenharmony_ciboolean
59bf215546Sopenharmony_ciutil_gen_mipmap(struct pipe_context *pipe, struct pipe_resource *pt,
60bf215546Sopenharmony_ci                enum pipe_format format, uint base_level, uint last_level,
61bf215546Sopenharmony_ci                uint first_layer, uint last_layer, uint filter)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci   struct pipe_screen *screen = pipe->screen;
64bf215546Sopenharmony_ci   struct pipe_blit_info blit;
65bf215546Sopenharmony_ci   uint dstLevel;
66bf215546Sopenharmony_ci   boolean is_zs = util_format_is_depth_or_stencil(format);
67bf215546Sopenharmony_ci   boolean has_depth =
68bf215546Sopenharmony_ci      util_format_has_depth(util_format_description(format));
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   /* nothing to do for stencil-only formats */
71bf215546Sopenharmony_ci   if (is_zs && !has_depth)
72bf215546Sopenharmony_ci      return TRUE;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /* nothing to do for integer formats */
75bf215546Sopenharmony_ci   if (!is_zs && util_format_is_pure_integer(format))
76bf215546Sopenharmony_ci      return TRUE;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci   if (!screen->is_format_supported(screen, format, pt->target,
79bf215546Sopenharmony_ci                                    pt->nr_samples, pt->nr_storage_samples,
80bf215546Sopenharmony_ci                                    PIPE_BIND_SAMPLER_VIEW |
81bf215546Sopenharmony_ci                                    (is_zs ? PIPE_BIND_DEPTH_STENCIL :
82bf215546Sopenharmony_ci                                     PIPE_BIND_RENDER_TARGET))) {
83bf215546Sopenharmony_ci      return FALSE;
84bf215546Sopenharmony_ci   }
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   /* The texture object should have room for the levels which we're
87bf215546Sopenharmony_ci    * about to generate.
88bf215546Sopenharmony_ci    */
89bf215546Sopenharmony_ci   assert(last_level <= pt->last_level);
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   /* If this fails, why are we here? */
92bf215546Sopenharmony_ci   assert(last_level > base_level);
93bf215546Sopenharmony_ci   assert(filter == PIPE_TEX_FILTER_LINEAR ||
94bf215546Sopenharmony_ci          filter == PIPE_TEX_FILTER_NEAREST);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   memset(&blit, 0, sizeof(blit));
97bf215546Sopenharmony_ci   blit.src.resource = blit.dst.resource = pt;
98bf215546Sopenharmony_ci   blit.src.format = blit.dst.format = format;
99bf215546Sopenharmony_ci   /* don't set the stencil mask, stencil shouldn't be changed */
100bf215546Sopenharmony_ci   blit.mask = is_zs ? PIPE_MASK_Z : PIPE_MASK_RGBA;
101bf215546Sopenharmony_ci   blit.filter = filter;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   for (dstLevel = base_level + 1; dstLevel <= last_level; dstLevel++) {
104bf215546Sopenharmony_ci      blit.src.level = dstLevel - 1;
105bf215546Sopenharmony_ci      blit.dst.level = dstLevel;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci      blit.src.box.width = u_minify(pt->width0, blit.src.level);
108bf215546Sopenharmony_ci      blit.src.box.height = u_minify(pt->height0, blit.src.level);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci      blit.dst.box.width = u_minify(pt->width0, blit.dst.level);
111bf215546Sopenharmony_ci      blit.dst.box.height = u_minify(pt->height0, blit.dst.level);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci      if (pt->target == PIPE_TEXTURE_3D) {
114bf215546Sopenharmony_ci         /* generate all layers/slices at once */
115bf215546Sopenharmony_ci         blit.src.box.z = blit.dst.box.z = 0;
116bf215546Sopenharmony_ci         blit.src.box.depth = util_num_layers(pt, blit.src.level);
117bf215546Sopenharmony_ci         blit.dst.box.depth = util_num_layers(pt, blit.dst.level);
118bf215546Sopenharmony_ci      }
119bf215546Sopenharmony_ci      else {
120bf215546Sopenharmony_ci         blit.src.box.z = blit.dst.box.z = first_layer;
121bf215546Sopenharmony_ci         blit.src.box.depth = blit.dst.box.depth =
122bf215546Sopenharmony_ci            (last_layer + 1 - first_layer);
123bf215546Sopenharmony_ci      }
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci      pipe->blit(pipe, &blit);
126bf215546Sopenharmony_ci   }
127bf215546Sopenharmony_ci   return TRUE;
128bf215546Sopenharmony_ci}
129