Merge commit 'origin/master' into gallium-sampler-view
[mesa.git] / src / mesa / state_tracker / st_gen_mipmap.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/macros.h"
31 #include "main/mipmap.h"
32 #include "main/teximage.h"
33 #include "main/texformat.h"
34
35 #include "shader/prog_instruction.h"
36
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_gen_mipmap.h"
42 #include "util/u_math.h"
43
44 #include "cso_cache/cso_cache.h"
45 #include "cso_cache/cso_context.h"
46
47 #include "st_debug.h"
48 #include "st_context.h"
49 #include "st_gen_mipmap.h"
50 #include "st_texture.h"
51 #include "st_cb_texture.h"
52 #include "st_inlines.h"
53
54
55 /**
56 * one-time init for generate mipmap
57 * XXX Note: there may be other times we need no-op/simple state like this.
58 * In that case, some code refactoring would be good.
59 */
60 void
61 st_init_generate_mipmap(struct st_context *st)
62 {
63 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
64 }
65
66
67 void
68 st_destroy_generate_mipmap(struct st_context *st)
69 {
70 util_destroy_gen_mipmap(st->gen_mipmap);
71 st->gen_mipmap = NULL;
72 }
73
74
75 /**
76 * Generate mipmap levels using hardware rendering.
77 * \return TRUE if successful, FALSE if not possible
78 */
79 static boolean
80 st_render_mipmap(struct st_context *st,
81 GLenum target,
82 struct pipe_texture *pt,
83 uint baseLevel, uint lastLevel)
84 {
85 struct pipe_context *pipe = st->pipe;
86 struct pipe_screen *screen = pipe->screen;
87 const uint face = _mesa_tex_target_to_face(target);
88
89 assert(target != GL_TEXTURE_3D); /* not done yet */
90
91 /* check if we can render in the texture's format */
92 if (!screen->is_format_supported(screen, pt->format, pt->target,
93 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
94 return FALSE;
95 }
96
97 util_gen_mipmap(st->gen_mipmap, pt, face, baseLevel, lastLevel,
98 PIPE_TEX_FILTER_LINEAR);
99
100 return TRUE;
101 }
102
103
104 static void
105 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
106 struct gl_texture_object *texObj)
107 {
108 struct pipe_context *pipe = ctx->st->pipe;
109 struct pipe_texture *pt = st_get_texobj_texture(texObj);
110 const uint baseLevel = texObj->BaseLevel;
111 const uint lastLevel = pt->last_level;
112 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
113 uint dstLevel;
114 GLenum datatype;
115 GLuint comps;
116
117 if (ST_DEBUG & DEBUG_FALLBACK)
118 debug_printf("%s: fallback processing\n", __FUNCTION__);
119
120 assert(target != GL_TEXTURE_3D); /* not done yet */
121
122 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
123 &datatype, &comps);
124
125 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
126 const uint srcLevel = dstLevel - 1;
127 struct pipe_transfer *srcTrans, *dstTrans;
128 const ubyte *srcData;
129 ubyte *dstData;
130 int srcStride, dstStride;
131
132 srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
133 srcLevel, zslice,
134 PIPE_TRANSFER_READ, 0, 0,
135 u_minify(pt->width0, srcLevel),
136 u_minify(pt->height0, srcLevel));
137
138 dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
139 dstLevel, zslice,
140 PIPE_TRANSFER_WRITE, 0, 0,
141 u_minify(pt->width0, dstLevel),
142 u_minify(pt->height0, dstLevel));
143
144 srcData = (ubyte *) pipe->transfer_map(pipe, srcTrans);
145 dstData = (ubyte *) pipe->transfer_map(pipe, dstTrans);
146
147 srcStride = srcTrans->stride / util_format_get_blocksize(srcTrans->texture->format);
148 dstStride = dstTrans->stride / util_format_get_blocksize(dstTrans->texture->format);
149
150 _mesa_generate_mipmap_level(target, datatype, comps,
151 0 /*border*/,
152 u_minify(pt->width0, srcLevel),
153 u_minify(pt->height0, srcLevel),
154 u_minify(pt->depth0, srcLevel),
155 srcData,
156 srcStride, /* stride in texels */
157 u_minify(pt->width0, dstLevel),
158 u_minify(pt->height0, dstLevel),
159 u_minify(pt->depth0, dstLevel),
160 dstData,
161 dstStride); /* stride in texels */
162
163 pipe->transfer_unmap(pipe, srcTrans);
164 pipe->transfer_unmap(pipe, dstTrans);
165
166 pipe->tex_transfer_destroy(pipe, srcTrans);
167 pipe->tex_transfer_destroy(pipe, dstTrans);
168 }
169 }
170
171
172 /**
173 * Compute the expected number of mipmap levels in the texture given
174 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
175 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
176 * level should be generated.
177 */
178 static GLuint
179 compute_num_levels(GLcontext *ctx,
180 struct gl_texture_object *texObj,
181 GLenum target)
182 {
183 if (target == GL_TEXTURE_RECTANGLE_ARB) {
184 return 1;
185 }
186 else {
187 const GLuint maxLevels = texObj->MaxLevel - texObj->BaseLevel + 1;
188 const struct gl_texture_image *baseImage =
189 _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
190 GLuint size, numLevels;
191
192 size = MAX2(baseImage->Width2, baseImage->Height2);
193 size = MAX2(size, baseImage->Depth2);
194
195 numLevels = 0;
196
197 while (size > 0) {
198 numLevels++;
199 size >>= 1;
200 }
201
202 numLevels = MIN2(numLevels, maxLevels);
203
204 return numLevels;
205 }
206 }
207
208
209 void
210 st_generate_mipmap(GLcontext *ctx, GLenum target,
211 struct gl_texture_object *texObj)
212 {
213 struct st_context *st = ctx->st;
214 struct pipe_texture *pt = st_get_texobj_texture(texObj);
215 const uint baseLevel = texObj->BaseLevel;
216 uint lastLevel;
217 uint dstLevel;
218
219 if (!pt)
220 return;
221
222 /* find expected last mipmap level */
223 lastLevel = compute_num_levels(ctx, texObj, target) - 1;
224
225 if (lastLevel == 0)
226 return;
227
228 if (pt->last_level < lastLevel) {
229 /* The current gallium texture doesn't have space for all the
230 * mipmap levels we need to generate. So allocate a new texture.
231 */
232 struct st_texture_object *stObj = st_texture_object(texObj);
233 struct pipe_texture *oldTex = stObj->pt;
234 GLboolean needFlush;
235
236 /* create new texture with space for more levels */
237 stObj->pt = st_texture_create(st,
238 oldTex->target,
239 oldTex->format,
240 lastLevel,
241 oldTex->width0,
242 oldTex->height0,
243 oldTex->depth0,
244 oldTex->tex_usage);
245
246 /* The texture isn't in a "complete" state yet so set the expected
247 * lastLevel here, since it won't get done in st_finalize_texture().
248 */
249 stObj->lastLevel = lastLevel;
250
251 /* This will copy the old texture's base image into the new texture
252 * which we just allocated.
253 */
254 st_finalize_texture(ctx, st->pipe, texObj, &needFlush);
255
256 /* release the old tex (will likely be freed too) */
257 pipe_texture_reference(&oldTex, NULL);
258
259 pt = stObj->pt;
260 }
261
262 assert(lastLevel <= pt->last_level);
263
264 /* Recall that the Mesa BaseLevel image is stored in the gallium
265 * texture's level[0] position. So pass baseLevel=0 here.
266 */
267 if (!st_render_mipmap(st, target, pt, 0, lastLevel)) {
268 fallback_generate_mipmap(ctx, target, texObj);
269 }
270
271 /* Fill in the Mesa gl_texture_image fields */
272 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
273 const uint srcLevel = dstLevel - 1;
274 const struct gl_texture_image *srcImage
275 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
276 struct gl_texture_image *dstImage;
277 struct st_texture_image *stImage;
278 uint dstWidth = u_minify(pt->width0, dstLevel);
279 uint dstHeight = u_minify(pt->height0, dstLevel);
280 uint dstDepth = u_minify(pt->depth0, dstLevel);
281 uint border = srcImage->Border;
282
283 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
284 if (!dstImage) {
285 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
286 return;
287 }
288
289 /* Free old image data */
290 if (dstImage->Data)
291 ctx->Driver.FreeTexImageData(ctx, dstImage);
292
293 /* initialize new image */
294 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
295 dstDepth, border, srcImage->InternalFormat);
296
297 dstImage->TexFormat = srcImage->TexFormat;
298
299 stImage = (struct st_texture_image *) dstImage;
300 pipe_texture_reference(&stImage->pt, pt);
301 }
302 }