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