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