st_gen_mipmap.c revision b57abd3bea29e95e5dee2524c3f1be4b26017c0f
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 "pipe/p_inlines.h"
40#include "util/u_gen_mipmap.h"
41
42#include "cso_cache/cso_cache.h"
43#include "cso_cache/cso_context.h"
44
45#include "st_debug.h"
46#include "st_context.h"
47#include "st_draw.h"
48#include "st_gen_mipmap.h"
49#include "st_program.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 */
60void
61st_init_generate_mipmap(struct st_context *st)
62{
63   st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
64}
65
66
67void
68st_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 */
79static boolean
80st_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
104static void
105fallback_generate_mipmap(GLcontext *ctx, GLenum target,
106                         struct gl_texture_object *texObj)
107{
108   struct pipe_context *pipe = ctx->st->pipe;
109   struct pipe_screen *screen = pipe->screen;
110   struct pipe_texture *pt = st_get_texobj_texture(texObj);
111   const uint baseLevel = texObj->BaseLevel;
112   const uint lastLevel = pt->last_level;
113   const uint face = _mesa_tex_target_to_face(target), zslice = 0;
114   uint dstLevel;
115   GLenum datatype;
116   GLuint comps;
117
118   if (ST_DEBUG & DEBUG_FALLBACK)
119      debug_printf("%s: fallback processing\n", __FUNCTION__);
120
121   assert(target != GL_TEXTURE_3D); /* not done yet */
122
123   _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
124                                  &datatype, &comps);
125
126   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
127      const uint srcLevel = dstLevel - 1;
128      struct pipe_transfer *srcTrans, *dstTrans;
129      const ubyte *srcData;
130      ubyte *dstData;
131      int srcStride, dstStride;
132
133      srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
134						srcLevel, zslice,
135						PIPE_TRANSFER_READ, 0, 0,
136						pt->width[srcLevel],
137						pt->height[srcLevel]);
138
139      dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
140						dstLevel, zslice,
141						PIPE_TRANSFER_WRITE, 0, 0,
142						pt->width[dstLevel],
143						pt->height[dstLevel]);
144
145      srcData = (ubyte *) screen->transfer_map(screen, srcTrans);
146      dstData = (ubyte *) screen->transfer_map(screen, dstTrans);
147
148      srcStride = srcTrans->stride / srcTrans->block.size;
149      dstStride = dstTrans->stride / dstTrans->block.size;
150
151      _mesa_generate_mipmap_level(target, datatype, comps,
152                   0 /*border*/,
153                   pt->width[srcLevel], pt->height[srcLevel], pt->depth[srcLevel],
154                   srcData,
155                   srcStride, /* stride in texels */
156                   pt->width[dstLevel], pt->height[dstLevel], pt->depth[dstLevel],
157                   dstData,
158                   dstStride); /* stride in texels */
159
160      screen->transfer_unmap(screen, srcTrans);
161      screen->transfer_unmap(screen, dstTrans);
162
163      screen->tex_transfer_destroy(srcTrans);
164      screen->tex_transfer_destroy(dstTrans);
165   }
166}
167
168
169/**
170 * Compute the expected number of mipmap levels in the texture given
171 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
172 * GL_TEXTURE_MAX_LEVEL settings.  This will tell us how many mipmap
173 * level should be generated.
174 */
175static GLuint
176compute_num_levels(GLcontext *ctx,
177                   struct gl_texture_object *texObj,
178                   GLenum target)
179{
180   if (target == GL_TEXTURE_RECTANGLE_ARB) {
181      return 1;
182   }
183   else {
184      const GLuint maxLevels = texObj->MaxLevel - texObj->BaseLevel + 1;
185      const struct gl_texture_image *baseImage =
186         _mesa_get_tex_image(ctx, texObj, target, texObj->BaseLevel);
187      GLuint size, numLevels;
188
189      size = MAX2(baseImage->Width2, baseImage->Height2);
190      size = MAX2(size, baseImage->Depth2);
191
192      numLevels = 0;
193
194      while (size > 0) {
195         numLevels++;
196         size >>= 1;
197      }
198
199      numLevels = MIN2(numLevels, maxLevels);
200
201      return numLevels;
202   }
203}
204
205
206void
207st_generate_mipmap(GLcontext *ctx, GLenum target,
208                   struct gl_texture_object *texObj)
209{
210   struct st_context *st = ctx->st;
211   struct pipe_texture *pt = st_get_texobj_texture(texObj);
212   const uint baseLevel = texObj->BaseLevel;
213   uint lastLevel;
214   uint dstLevel;
215
216   if (!pt)
217      return;
218
219   /* find expected last mipmap level */
220   lastLevel = compute_num_levels(ctx, texObj, target) - 1;
221
222   if (lastLevel == 0)
223      return;
224
225   if (pt->last_level < lastLevel) {
226      /* The current gallium texture doesn't have space for all the
227       * mipmap levels we need to generate.  So allocate a new texture.
228       */
229      struct st_texture_object *stObj = st_texture_object(texObj);
230      struct pipe_texture *oldTex = stObj->pt;
231      GLboolean needFlush;
232
233      /* create new texture with space for more levels */
234      stObj->pt = st_texture_create(st,
235                                    oldTex->target,
236                                    oldTex->format,
237                                    lastLevel,
238                                    oldTex->width[0],
239                                    oldTex->height[0],
240                                    oldTex->depth[0],
241                                    oldTex->tex_usage);
242
243      /* The texture isn't in a "complete" state yet so set the expected
244       * lastLevel here, since it won't get done in st_finalize_texture().
245       */
246      stObj->lastLevel = lastLevel;
247
248      /* This will copy the old texture's base image into the new texture
249       * which we just allocated.
250       */
251      st_finalize_texture(ctx, st->pipe, texObj, &needFlush);
252
253      /* release the old tex (will likely be freed too) */
254      pipe_texture_reference(&oldTex, NULL);
255
256      pt = stObj->pt;
257   }
258
259   assert(lastLevel <= pt->last_level);
260
261   /* Recall that the Mesa BaseLevel image is stored in the gallium
262    * texture's level[0] position.  So pass baseLevel=0 here.
263    */
264   if (!st_render_mipmap(st, target, pt, 0, lastLevel)) {
265      fallback_generate_mipmap(ctx, target, texObj);
266   }
267
268   /* Fill in the Mesa gl_texture_image fields */
269   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
270      const uint srcLevel = dstLevel - 1;
271      const struct gl_texture_image *srcImage
272         = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
273      struct gl_texture_image *dstImage;
274      struct st_texture_image *stImage;
275      uint dstWidth = pt->width[dstLevel];
276      uint dstHeight = pt->height[dstLevel];
277      uint dstDepth = pt->depth[dstLevel];
278      uint border = srcImage->Border;
279
280      dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
281      if (!dstImage) {
282         _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
283         return;
284      }
285
286      /* Free old image data */
287      if (dstImage->Data)
288         ctx->Driver.FreeTexImageData(ctx, dstImage);
289
290      /* initialize new image */
291      _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
292                                 dstDepth, border, srcImage->InternalFormat);
293
294      dstImage->TexFormat = srcImage->TexFormat;
295
296      stImage = (struct st_texture_image *) dstImage;
297      pipe_texture_reference(&stImage->pt, pt);
298   }
299}
300