1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "main/errors.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "main/mipmap.h"
32bf215546Sopenharmony_ci#include "main/teximage.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "pipe/p_context.h"
35bf215546Sopenharmony_ci#include "pipe/p_defines.h"
36bf215546Sopenharmony_ci#include "util/u_inlines.h"
37bf215546Sopenharmony_ci#include "util/format/u_format.h"
38bf215546Sopenharmony_ci#include "util/u_gen_mipmap.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "st_debug.h"
41bf215546Sopenharmony_ci#include "st_context.h"
42bf215546Sopenharmony_ci#include "st_texture.h"
43bf215546Sopenharmony_ci#include "st_util.h"
44bf215546Sopenharmony_ci#include "st_gen_mipmap.h"
45bf215546Sopenharmony_ci#include "st_cb_bitmap.h"
46bf215546Sopenharmony_ci#include "st_cb_texture.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_civoid
49bf215546Sopenharmony_cist_generate_mipmap(struct gl_context *ctx, GLenum target,
50bf215546Sopenharmony_ci                   struct gl_texture_object *texObj)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci   struct st_context *st = st_context(ctx);
53bf215546Sopenharmony_ci   struct pipe_resource *pt = st_get_texobj_resource(texObj);
54bf215546Sopenharmony_ci   uint baseLevel = texObj->Attrib.BaseLevel;
55bf215546Sopenharmony_ci   enum pipe_format format;
56bf215546Sopenharmony_ci   uint lastLevel, first_layer, last_layer;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   if (!pt)
59bf215546Sopenharmony_ci      return;
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   if (texObj->Immutable)
62bf215546Sopenharmony_ci      baseLevel += texObj->Attrib.MinLevel;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   /* not sure if this ultimately actually should work,
65bf215546Sopenharmony_ci      but we're not supporting multisampled textures yet. */
66bf215546Sopenharmony_ci   assert(pt->nr_samples < 2);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   /* find expected last mipmap level to generate*/
69bf215546Sopenharmony_ci   lastLevel = _mesa_compute_num_levels(ctx, texObj, target) - 1;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   if (texObj->Immutable)
72bf215546Sopenharmony_ci      lastLevel += texObj->Attrib.MinLevel;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   if (lastLevel == 0)
75bf215546Sopenharmony_ci      return;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   st_flush_bitmap_cache(st);
78bf215546Sopenharmony_ci   st_invalidate_readpix_cache(st);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   /* The texture isn't in a "complete" state yet so set the expected
81bf215546Sopenharmony_ci    * lastLevel here, since it won't get done in st_finalize_texture().
82bf215546Sopenharmony_ci    */
83bf215546Sopenharmony_ci   texObj->lastLevel = lastLevel;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   if (!texObj->Immutable) {
86bf215546Sopenharmony_ci      const GLboolean genSave = texObj->Attrib.GenerateMipmap;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci      /* Temporarily set GenerateMipmap to true so that allocate_full_mipmap()
89bf215546Sopenharmony_ci       * makes the right decision about full mipmap allocation.
90bf215546Sopenharmony_ci       */
91bf215546Sopenharmony_ci      texObj->Attrib.GenerateMipmap = GL_TRUE;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci      _mesa_prepare_mipmap_levels(ctx, texObj, baseLevel, lastLevel);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci      texObj->Attrib.GenerateMipmap = genSave;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci      /* At this point, memory for all the texture levels has been
98bf215546Sopenharmony_ci       * allocated.  However, the base level image may be in one resource
99bf215546Sopenharmony_ci       * while the subsequent/smaller levels may be in another resource.
100bf215546Sopenharmony_ci       * Finalizing the texture will copy the base images from the former
101bf215546Sopenharmony_ci       * resource to the latter.
102bf215546Sopenharmony_ci       *
103bf215546Sopenharmony_ci       * After this, we'll have all mipmap levels in one resource.
104bf215546Sopenharmony_ci       */
105bf215546Sopenharmony_ci      st_finalize_texture(ctx, st->pipe, texObj, 0);
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   pt = texObj->pt;
109bf215546Sopenharmony_ci   if (!pt) {
110bf215546Sopenharmony_ci      _mesa_error(ctx, GL_OUT_OF_MEMORY, "mipmap generation");
111bf215546Sopenharmony_ci      return;
112bf215546Sopenharmony_ci   }
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   assert(pt->last_level >= lastLevel);
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   if (pt->target == PIPE_TEXTURE_CUBE) {
117bf215546Sopenharmony_ci      first_layer = last_layer = _mesa_tex_target_to_face(target);
118bf215546Sopenharmony_ci   }
119bf215546Sopenharmony_ci   else {
120bf215546Sopenharmony_ci      first_layer = 0;
121bf215546Sopenharmony_ci      last_layer = util_max_layer(pt, baseLevel);
122bf215546Sopenharmony_ci   }
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   if (texObj->surface_based)
125bf215546Sopenharmony_ci      format = texObj->surface_format;
126bf215546Sopenharmony_ci   else
127bf215546Sopenharmony_ci      format = pt->format;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   if (texObj->Sampler.Attrib.sRGBDecode == GL_SKIP_DECODE_EXT)
130bf215546Sopenharmony_ci      format = util_format_linear(format);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   /* First see if the driver supports hardware mipmap generation,
133bf215546Sopenharmony_ci    * if not then generate the mipmap by rendering/texturing.
134bf215546Sopenharmony_ci    * If that fails, use the software fallback.
135bf215546Sopenharmony_ci    */
136bf215546Sopenharmony_ci   if (!st->screen->get_param(st->screen,
137bf215546Sopenharmony_ci                              PIPE_CAP_GENERATE_MIPMAP) ||
138bf215546Sopenharmony_ci       !st->pipe->generate_mipmap(st->pipe, pt, format, baseLevel,
139bf215546Sopenharmony_ci                                  lastLevel, first_layer, last_layer)) {
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci      if (!util_gen_mipmap(st->pipe, pt, format, baseLevel, lastLevel,
142bf215546Sopenharmony_ci                           first_layer, last_layer, PIPE_TEX_FILTER_LINEAR)) {
143bf215546Sopenharmony_ci         _mesa_generate_mipmap(ctx, target, texObj);
144bf215546Sopenharmony_ci      }
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci}
147