1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "main/mipmap.h"
25 #include "main/teximage.h"
26 #include "brw_blorp.h"
27 #include "brw_context.h"
28 #include "intel_tex.h"
29 #include "drivers/common/meta.h"
30 
31 #define FILE_DEBUG_FLAG DEBUG_BLORP
32 
33 
34 /**
35  * The GenerateMipmap() driver hook.
36  */
37 void
brw_generate_mipmap(struct gl_context * ctx,GLenum target,struct gl_texture_object * tex_obj)38 brw_generate_mipmap(struct gl_context *ctx, GLenum target,
39                     struct gl_texture_object *tex_obj)
40 {
41    struct brw_context *brw = brw_context(ctx);
42    struct gen_device_info *devinfo = &brw->screen->devinfo;
43    struct intel_texture_object *intel_obj = intel_texture_object(tex_obj);
44    const unsigned base_level = tex_obj->BaseLevel;
45    unsigned last_level, first_layer, last_layer;
46 
47    /* Blorp doesn't handle combined depth/stencil surfaces on Gen4-5 yet. */
48    if (devinfo->gen <= 5 &&
49        (tex_obj->Image[0][base_level]->_BaseFormat == GL_DEPTH_COMPONENT ||
50         tex_obj->Image[0][base_level]->_BaseFormat == GL_DEPTH_STENCIL)) {
51       _mesa_meta_GenerateMipmap(ctx, target, tex_obj);
52       return;
53    }
54 
55    /* find expected last mipmap level to generate */
56    last_level = _mesa_compute_num_levels(ctx, tex_obj, target) - 1;
57 
58    if (last_level == 0)
59       return;
60 
61    /* The texture isn't in a "complete" state yet so set the expected
62     * last_level here; we're not going through normal texture validation.
63     */
64    intel_obj->_MaxLevel = last_level;
65 
66    if (!tex_obj->Immutable) {
67       _mesa_prepare_mipmap_levels(ctx, tex_obj, base_level, last_level);
68 
69       /* At this point, memory for all the texture levels has been
70        * allocated.  However, the base level image may be in one resource
71        * while the subsequent/smaller levels may be in another resource.
72        * Finalizing the texture will copy the base images from the former
73        * resource to the latter.
74        *
75        * After this, we'll have all mipmap levels in one resource.
76        */
77       intel_finalize_mipmap_tree(brw, tex_obj);
78    }
79 
80    struct intel_mipmap_tree *mt = intel_obj->mt;
81    if (!mt) {
82       _mesa_error(ctx, GL_OUT_OF_MEMORY, "mipmap generation");
83       return;
84    }
85 
86    const mesa_format format = intel_obj->_Format;
87 
88    /* Fall back to the CPU for non-renderable cases.
89     *
90     * TODO: 3D textures require blending data from multiple slices,
91     * which means we need custom shaders.  For now, fall back.
92     */
93    if (!brw->mesa_format_supports_render[format] || target == GL_TEXTURE_3D) {
94       _mesa_generate_mipmap(ctx, target, tex_obj);
95       return;
96    }
97 
98    const struct isl_extent4d *base_size = &mt->surf.logical_level0_px;
99 
100    if (mt->target == GL_TEXTURE_CUBE_MAP) {
101       first_layer = _mesa_tex_target_to_face(target);
102       last_layer = first_layer;
103    } else {
104       first_layer = 0;
105       last_layer = base_size->array_len - 1;
106    }
107 
108    /* The GL_EXT_texture_sRGB_decode extension's issues section says:
109     *
110     *    "10) How is mipmap generation of sRGB textures affected by the
111     *     TEXTURE_SRGB_DECODE_EXT parameter?
112     *
113     *     RESOLVED:  When the TEXTURE_SRGB_DECODE parameter is DECODE_EXT
114     *     for an sRGB texture, mipmap generation should decode sRGB texels
115     *     to a linear RGB color space, perform downsampling, then encode
116     *     back to an sRGB color space.  (Issue 24 in the EXT_texture_sRGB
117     *     specification provides a rationale for why.)  When the parameter
118     *     is SKIP_DECODE_EXT instead, mipmap generation skips the encode
119     *     and decode steps during mipmap generation.  By skipping the
120     *     encode and decode steps, sRGB mipmap generation should match
121     *     the mipmap generation for a non-sRGB texture."
122     */
123    bool do_srgb = tex_obj->Sampler.sRGBDecode == GL_DECODE_EXT;
124 
125    for (unsigned dst_level = base_level + 1;
126         dst_level <= last_level;
127         dst_level++) {
128 
129       const unsigned src_level = dst_level - 1;
130 
131       for (unsigned layer = first_layer; layer <= last_layer; layer++) {
132          brw_blorp_blit_miptrees(brw, mt, src_level, layer, format,
133                                  SWIZZLE_XYZW, mt, dst_level, layer, format,
134                                  0, 0,
135                                  minify(base_size->width, src_level),
136                                  minify(base_size->height, src_level),
137                                  0, 0,
138                                  minify(base_size->width, dst_level),
139                                  minify(base_size->height, dst_level),
140                                  GL_LINEAR, false, false,
141                                  do_srgb, do_srgb);
142       }
143    }
144 }
145